blob: f9b1920733e632a5752ec3b1d34495ed019516d1 [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)
155 ch_logfile((char_u *)(argv[i + 1]), (char_u *)"a");
156# endif
157 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000158#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000159 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000160
Bram Moolenaar07268702018-03-01 21:57:32 +0100161#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100162 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100163 for (i = 1; i < argc; ++i)
164 if (STRICMP(argv[i], "--clean") == 0)
165 {
166 params.clean = TRUE;
167 break;
168 }
169#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200170#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200171 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200172 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200173 if ((STRICMP(argv[i] + 1, "register") == 0
174 || STRICMP(argv[i] + 1, "unregister") == 0)
175 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200176 {
177 found_register_arg = TRUE;
178 break;
179 }
180#endif
181
182 /*
183 * Various initialisations shared with tests.
184 */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200185 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000186
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200187#ifdef VIMDLL
188 // Check if the current executable file is for the GUI subsystem.
189 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200190#elif defined(FEAT_GUI_MSWIN)
191 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200192#endif
193
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000194#ifdef FEAT_CLIENTSERVER
195 /*
196 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000199 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000200#endif
201
202 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203 * Figure out the way to work from the command name argv[0].
204 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000206 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000207
208 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000209 * Process the command line arguments. File names are put in the global
210 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000211 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000212 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213 TIME_MSG("parsing arguments");
214
215 /*
216 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000217 * there is no terminal version, and on Windows we can't fork one off with
218 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000219 */
220#ifdef ALWAYS_USE_GUI
221 gui.starting = TRUE;
222#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000223# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 /*
225 * Check if the GUI can be started. Reset gui.starting if not.
226 * Don't know about other systems, stay on the safe side and don't check.
227 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000228 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000230 if (gui_init_check() == FAIL)
231 {
232 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100234 // When running "evim" or "gvim -y" we need the menus, exit if we
235 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000236 if (params.evim_mode)
237 mch_exit(1);
238 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 }
240# endif
241#endif
242
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243 if (GARGCOUNT > 0)
244 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100245#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 /*
247 * Expand wildcards in file names.
248 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000249 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200251 start_dir = alloc(MAXPATHL);
252 if (start_dir != NULL)
253 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100254 // Temporarily add '(' and ')' to 'isfname'. These are valid
255 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000256 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000257 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000258 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200260 if (start_dir != NULL)
261 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262 }
263#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200264 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000265 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000266
Bram Moolenaar4f974752019-02-17 17:44:42 +0100267#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000268 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100269 // Remember the number of entries in the argument list. If it changes
270 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000271 set_alist_count();
272 }
273#endif
274
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000275#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000276 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277 {
278 /*
279 * If there is one filename, fully qualified, we have very probably
280 * been invoked from explorer, so change to the file's directory.
281 * Hint: to avoid this when typing a command use a forward slash.
282 * If the cd fails, it doesn't matter.
283 */
Bram Moolenaar05268152021-11-18 18:53:45 +0000284 if (vim_chdirfile(params.fname, "drop") == OK)
285 last_chdir_reason = "drop";
Bram Moolenaarf6303872015-04-03 17:59:43 +0200286 if (start_dir != NULL)
287 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288 }
289#endif
290 TIME_MSG("expanding arguments");
291
292#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000293 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100294 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000295#endif
296
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100297 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 ++RedrawingDisabled;
299
300 /*
301 * When listing swap file names, don't do cursor positioning et. al.
302 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200303 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000304 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305
306 /*
307 * When certain to start the GUI, don't check capabilities of terminal.
308 * For GTK we can't be sure, but when started from the desktop it doesn't
309 * make sense to try using a terminal.
310 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200311#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
312 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000313 if (gui.starting
314# ifdef FEAT_GUI_GTK
315 && !isatty(2)
316# endif
317 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000318 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000319#endif
320
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000321 /*
322 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100323 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324 * Note that we may use mch_exit() before mch_init()!
325 */
326 mch_init();
327 TIME_MSG("shell init");
328
329#ifdef USE_XSMP
330 /*
331 * For want of anywhere else to do it, try to connect to xsmp here.
332 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
333 * Hijacking -X 'no X connection' to also disable XSMP connection as that
334 * has a similar delay upon failure.
335 * Only try if SESSION_MANAGER is set to something non-null.
336 */
337 if (!x_no_connect)
338 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000339 char *p = getenv("SESSION_MANAGER");
340
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000341 if (p != NULL && *p != NUL)
342 {
343 xsmp_init();
344 TIME_MSG("xsmp init");
345 }
346 }
347#endif
348
349 /*
350 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000351 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000352 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100354#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100355 // Ensure output works usefully without a tty: buffer lines instead of
356 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100357 if (silent_mode)
358 setvbuf(stdout, NULL, _IOLBF, 0);
359#endif
360
Bram Moolenaar9404a182019-05-03 22:25:40 +0200361 // This message comes before term inits, but after setting "silent_mode"
362 // when the input is not a tty. Omit the message with --not-a-term.
363 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000364 printf(_("%d files to edit\n"), GARGCOUNT);
365
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000366 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100368 termcapinit(params.term); // set terminal name and get terminal
369 // capabilities (will set full_screen)
370 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 TIME_MSG("Termcap init");
372 }
373
374 /*
375 * Set the default values for the options that use Rows and Columns.
376 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100377 ui_get_shellsize(); // inits Rows and Columns
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000378 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100380 // Set the 'diff' option now, so that it can be checked for in a .vimrc
381 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000382 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 diff_win_options(firstwin, FALSE);
384#endif
385
386 cmdline_row = Rows - p_ch;
387 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100388 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389 set_init_2();
390 TIME_MSG("inits 2");
391
392 msg_scroll = TRUE;
393 no_wait_return = TRUE;
394
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100395 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000396
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100397 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200400#if defined(FEAT_TERMRESPONSE)
401 init_term_props(TRUE);
402#endif
403
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100405 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000406 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407#endif
408
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100409 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
410 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200411 if (params.use_vimrc != NULL
412 && (STRCMP(params.use_vimrc, "NONE") == 0
413 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
414 p_lpl = FALSE;
415
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100416 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200417 exe_pre_commands(&params);
418
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100419 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200420 source_startup_scripts(&params);
421
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100422#ifdef FEAT_MZSCHEME
423 /*
424 * Newer version of MzScheme (Racket) require earlier (trampolined)
425 * initialisation via scheme_main_setup.
426 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200427 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200428 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100429 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200430 return mzscheme_main();
431#else
432 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100433#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200434}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100435#endif // NO_VIM_MAIN
436#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100437
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200438/*
439 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
440 * things simple.
441 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
442 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100443 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200444vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100445{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100446#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447#ifdef FEAT_EVAL
448 /*
449 * Read all the plugin files.
450 * Only when compiled with +eval, since most plugins need it.
451 */
452 if (p_lpl)
453 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200454 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100455 char_u *plugin_pattern = (char_u *)
456# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
457 "plugin/*.vim"
458# else
459 "plugin/**/*.vim"
460# endif
461 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100463 // First add all package directories to 'runtimepath', so that their
464 // autoload directories can be found. Only if not done already with a
465 // :packloadall command.
466 // Make a copy of 'runtimepath', so that source_runtime does not use
467 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200468 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200469 {
470 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200471 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200472 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200473
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100474 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000476 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200477 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100478
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100479 // Only source "start" packages if not done already with a :packloadall
480 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200481 if (!did_source_packages)
482 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100483 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200484
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100485 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200486 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000487 }
488#endif
489
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000490#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100491 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000492 if (params.diff_mode && params.window_layout == 0)
493 {
494 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100495 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000496 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100497 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000498 }
499#endif
500
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 /*
502 * Recovery mode without a file name: List swap files.
503 * This uses the 'dir' option, therefore it must be after the
504 * initializations.
505 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200506 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200508 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000509 mch_exit(0);
510 }
511
512 /*
513 * Set a few option defaults after reading .vimrc files:
514 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
515 */
516 set_init_3();
517 TIME_MSG("inits 3");
518
519 /*
520 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
521 * Note that this overrides anything from a vimrc file.
522 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000523 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000524 p_uc = 0;
525
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000526#ifdef FEAT_GUI
527 if (gui.starting)
528 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200529# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100530 // When something caused a message from a vimrc script, need to output
531 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532 if (did_emsg || msg_didout)
533 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200534# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100536 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 TIME_MSG("starting GUI");
538
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100539 // When running "evim" or "gvim -y" we need the menus, exit if we
540 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000541 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 mch_exit(1);
543 }
544#endif
545
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546#ifdef FEAT_VIMINFO
547 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000548 * Read in registers, history etc, but not marks, from the viminfo file.
549 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000550 */
551 if (*p_viminfo != NUL)
552 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000553 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000554 TIME_MSG("reading viminfo");
555 }
556#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100557#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100558 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100559 if (get_vim_var_list(VV_OLDFILES) == NULL)
560 set_vim_var_list(VV_OLDFILES, list_alloc());
561#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562
563#ifdef FEAT_QUICKFIX
564 /*
565 * "-q errorfile": Load the error file now.
566 * If the error file can't be read, exit before doing anything else.
567 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000568 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000569 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100570 char_u *enc = NULL;
571
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100572 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000573 if (params.use_ef != NULL)
574 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000575 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200576 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100577 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578 {
579 out_char('\n');
580 mch_exit(3);
581 }
582 TIME_MSG("reading errorfile");
583 }
584#endif
585
586 /*
587 * Start putting things on the screen.
588 * Scroll screen down before drawing over it
589 * Clear screen now, so file message will not be cleared.
590 */
591 starting = NO_BUFFERS;
592 no_wait_return = FALSE;
593 if (!exmode_active)
594 msg_scroll = FALSE;
595
596#ifdef FEAT_GUI
597 /*
598 * This seems to be required to make callbacks to be called now, instead
599 * of after things have been put on the screen, which then may be deleted
600 * when getting a resize callback.
601 * For the Mac this handles putting files dropped on the Vim icon to
602 * global_alist.
603 */
604 if (gui.in_use)
605 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100606 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000607 TIME_MSG("GUI delay");
608 }
609#endif
610
611#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
612 qnx_clip_init();
613#endif
614
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200615#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
616 clip_init(TRUE);
617#endif
618
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000619#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100620 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000621# ifdef FEAT_GUI
622 if (!gui.in_use)
623# endif
624 {
625 setup_term_clip();
626 TIME_MSG("setup clipboard");
627 }
628#endif
629
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000630#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100631 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000632 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000633#endif
634
635 /*
636 * If "-" argument given: Read file from stdin.
637 * Do this before starting Raw mode, because it may change things that the
638 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
639 * are the same terminal: "cat | vim -".
640 * Using autocommands here may cause trouble...
641 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000642 if (params.edit_type == EDIT_STDIN && !recoverymode)
643 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000644
645#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100646 // When switching screens and something caused a message from a vimrc
647 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000648 if ((did_emsg || msg_didout) && *T_TI != NUL)
649 newline_on_exit = TRUE;
650#endif
651
652 /*
K.Takataeeec2542021-06-02 13:28:16 +0200653 * When done something that is not allowed or given an error message call
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000654 * wait_return. This must be done before starttermcap(), because it may
655 * switch to another screen. It must be done after settmode(TMODE_RAW),
656 * because we want to react on a single key stroke.
657 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000658 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000659 */
660 settmode(TMODE_RAW);
661 TIME_MSG("setting raw mode");
662
663 if (need_wait_return || msg_didany)
664 {
665 wait_return(TRUE);
666 TIME_MSG("waiting for return");
667 }
668
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100669 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000670 TIME_MSG("start termcap");
671
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200672 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000673 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100674 scroll_region_reset(); // In case Rows changed
675 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676
Bram Moolenaar651fca82021-11-29 20:39:38 +0000677#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200678 term_push_title(SAVE_RESTORE_BOTH);
679#endif
680
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000681 /*
682 * Don't clear the screen when starting in Ex mode, unless using the GUI.
683 */
684 if (exmode_active
685#ifdef FEAT_GUI
686 && !gui.in_use
687#endif
688 )
689 must_redraw = CLEAR;
690 else
691 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100692 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000693 TIME_MSG("clearing screen");
694 }
695
696#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000697 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000698 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100699 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200700 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000701 TIME_MSG("getting crypt key");
702 }
703#endif
704
705 no_wait_return = TRUE;
706
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000707 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000708 * Create the requested number of windows and edit buffers in them.
709 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000710 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000711 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 TIME_MSG("opening buffers");
713
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000714#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100715 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000716 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
717#endif
718
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100719 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000720 if (exmode_active)
721 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
722
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000723 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
724 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 setpcmark();
726
727#ifdef FEAT_QUICKFIX
728 /*
729 * When started with "-q errorfile" jump to first error now.
730 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000731 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000733 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000734 TIME_MSG("jump to first error");
735 }
736#endif
737
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738 /*
739 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000740 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000741 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200742 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200743 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000744
745#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000746 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000747 {
748 win_T *wp;
749
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100750 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200751 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000752 diff_win_options(wp, TRUE);
753 }
754#endif
755
756 /*
757 * Shorten any of the filenames, but only when absolute.
758 */
759 shorten_fnames(FALSE);
760
761 /*
762 * Need to jump to the tag before executing the '-c command'.
763 * Makes "vim -c '/return' -t main" work.
764 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000765 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000766 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000767 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000768
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000769 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000770 do_cmdline_cmd(IObuff);
771 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000772
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100773 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000774 if (swap_exists_did_quit)
775 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000776 }
777
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100778 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000779 if (params.n_commands > 0)
780 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000781
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100782 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200783 starting = 0;
784
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100785#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100786 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200787 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200788#endif
789
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000790 RedrawingDisabled = 0;
791 redraw_all_later(NOT_VALID);
792 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100794 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200795 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200796
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000797#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100798 // Requesting the termresponse is postponed until here, so that a "-c q"
799 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000800 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200801
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200802 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000803#endif
804
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100805 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806 if (p_im)
807 need_start_insertmode = TRUE;
808
Bram Moolenaar14735512016-03-26 21:00:08 +0100809#ifdef FEAT_EVAL
810 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
811#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000812 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
813 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200815#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100816 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
817 // done after the clipboard is available and all initial commands that may
818 // modify the 'clipboard' setting have run; i.e. just before entering the
819 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200820 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200821#endif
822
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100823#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100824 // When a startup script or session file setup for diff'ing and
825 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000826 if (curwin->w_p_diff && curwin->w_p_scb)
827 {
828 update_topline();
829 check_scrollbind((linenr_T)0, 0L);
830 TIME_MSG("diff scrollbinding");
831 }
832#endif
833
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200834#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
835# ifdef VIMDLL
836 if (!gui.in_use)
837# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100838 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000839#endif
840
Bram Moolenaar4033c552017-09-16 20:54:51 +0200841#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100842 // When tab pages were created, may need to update the tab pages line and
843 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000844 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000845 {
846 out_flush();
847 gui_init_which_components(NULL);
848 gui_update_scrollbars(TRUE);
849 }
850 need_mouse_correct = TRUE;
851#endif
852
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100853 // If ":startinsert" command used, stuff a dummy command to be able to
854 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000855 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000856 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000857
858#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200859 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200860 {
861# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200862# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100863 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200864 if (gui.in_use)
865 {
866 mch_errmsg(_("netbeans is not supported with this GUI\n"));
867 mch_exit(2);
868 }
869# endif
870# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100871 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200872 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200873 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000874#endif
875
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100876 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
877 // window title gets updated.
878 do_redraw = TRUE;
879
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880 TIME_MSG("before starting main loop");
881
882 /*
883 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200884 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000885 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100887#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200888
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889 return 0;
890}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891
892/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200893 * Initialisation shared by main() and some tests.
894 */
895 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200896common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200897{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100898 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200899 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200900
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100901 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200902#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100903 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200904#endif
905
906#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100907 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200908#endif
909
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200910 /*
911 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100912 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200913 */
914 if ((IObuff = alloc(IOSIZE)) == NULL
915 || (NameBuff = alloc(MAXPATHL)) == NULL)
916 mch_exit(0);
917 TIME_MSG("Allocated generic buffers");
918
919#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100920 // Wait a moment for debugging NetBeans. Must be after allocating
921 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200922 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
923 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
924 TIME_MSG("NetBeans debug wait");
925#endif
926
927#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
928 /*
929 * Setup to use the current locale (for ctype() and many other things).
930 * NOTE: Translated messages with encodings other than latin1 will not
931 * work until set_init_1() has been called!
932 */
933 init_locale();
934 TIME_MSG("locale set");
935#endif
936
937#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100938 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200939#endif
940
941 /*
942 * Do a first scan of the arguments in "argv[]":
943 * -display or --display
944 * --server...
945 * --socketid
946 * --windowid
947 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200948 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200949
Bram Moolenaard0573012017-10-28 21:11:06 +0200950#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100951 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200952 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200953 TIME_MSG("GUI prepared");
954#endif
955
956#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100957 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200958 TIME_MSG("clipboard setup");
959#endif
960
961 /*
962 * Check if we have an interactive window.
963 * On the Amiga: If there is no window, we open one with a newcli command
964 * (needed for :! to * work). mch_check_win() will also handle the -d or
965 * -dev argument.
966 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100967 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200968 TIME_MSG("window checked");
969
970 /*
971 * Allocate the first window and buffer.
972 * Can't do anything without it, exit when it fails.
973 */
974 if (win_alloc_first() == FAIL)
975 mch_exit(0);
976
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100977 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200978
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100979 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200980 global_alist.id = 0;
981
982 /*
983 * Set the default values for the options.
984 * NOTE: Non-latin1 translated messages are working only after this,
985 * because this is where "has_mbyte" will be set, which is used by
986 * msg_outtrans_len_attr().
987 * First find out the home directory, needed to expand "~" in options.
988 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100989 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +0100990 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200991 TIME_MSG("inits 1");
992
993#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100994 // set v:lang and v:ctype
995 set_lang_var();
996
997 // set v:argv
998 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200999#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001000
1001#ifdef FEAT_SIGNS
1002 init_signs();
1003#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001004}
1005
1006/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001007 * Return TRUE when the --not-a-term argument was found.
1008 */
1009 int
1010is_not_a_term()
1011{
1012 return params.not_a_term;
1013}
1014
Bram Moolenaar7007e312021-03-27 12:11:33 +01001015/*
1016 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1017 */
1018 static int
1019is_not_a_term_or_gui()
1020{
1021 return params.not_a_term
1022#ifdef FEAT_GUI
1023 || gui.in_use
1024#endif
1025 ;
1026}
1027
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001028
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001029// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001030static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001031static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001032
1033/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001034 * Return TRUE if an operator was started but not finished yet.
1035 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001036 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001037 int
1038op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001039{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001040 return !(current_oap != NULL
1041 && !finish_op
1042 && current_oap->prev_opcount == 0
1043 && current_oap->prev_count0 == 0
1044 && current_oap->op_type == OP_NOP
1045 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001046}
1047
1048/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001049 * Return whether currently it is safe, assuming it was safe before (high level
1050 * state didn't change).
1051 */
1052 static int
1053is_safe_now(void)
1054{
1055 return stuff_empty()
1056 && typebuf.tb_len == 0
1057 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001058#ifdef FEAT_EVAL
1059 && !debug_mode
1060#endif
1061 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001062}
1063
1064/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001065 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1066 * there is no typeahead.
1067 */
1068 void
1069may_trigger_safestate(int safe)
1070{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001071 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001072
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001073#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001074 if (was_safe != is_safe)
1075 // Only log when the state changes, otherwise it happens at nearly
1076 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001077 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1078 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001079#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001080 if (is_safe)
1081 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1082 was_safe = is_safe;
1083}
1084
1085/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001086 * Something changed which causes the state possibly to be unsafe, e.g. a
1087 * character was typed. It will remain unsafe until the next call to
1088 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001089 */
1090 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001091state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001092{
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001093#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001094 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001095 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001096#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001097 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001098}
1099
Dominique Pelle748b3082022-01-08 12:41:16 +00001100#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001101 int
1102get_was_safe_state(void)
1103{
1104 return was_safe;
1105}
Dominique Pelle748b3082022-01-08 12:41:16 +00001106#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001107
Dominique Pelle748b3082022-01-08 12:41:16 +00001108#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001109/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001110 * Invoked when leaving code that invokes callbacks. Then trigger
1111 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001112 */
1113 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001114may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001115{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001116 if (!was_safe)
1117 {
1118 // If the safe state was reset in state_no_longer_safe(), e.g. because
1119 // of calling feedkeys(), we check if it's now safe again (all keys
1120 // were consumed).
1121 was_safe = is_safe_now();
1122#ifdef FEAT_JOB_CHANNEL
1123 if (was_safe)
1124 ch_log(NULL, "SafeState: undo reset");
1125#endif
1126 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001127 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001128 {
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001129#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001130 // Only do this message when another message was given, otherwise we
1131 // get lots of them.
1132 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1133 {
1134 int did = did_repeated_msg;
1135
1136 ch_log(NULL,
1137 "SafeState: back to waiting, triggering SafeStateAgain");
1138 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1139 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001140#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001141 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001142 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001143#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001144 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001145 ch_log(NULL,
1146 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001147#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001148}
Dominique Pelle748b3082022-01-08 12:41:16 +00001149#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001150
1151
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001152/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001153 * Main loop: Execute Normal mode commands until exiting Vim.
1154 * Also used to handle commands in the command-line window, until the window
1155 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 * Also used to handle ":visual" command after ":global": execute Normal mode
1157 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001158 */
1159 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001160main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001161 int cmdwin, // TRUE when working in the command-line window
1162 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001163{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001164 oparg_T oa; // operator arguments
1165 oparg_T *prev_oap; // operator arguments
1166 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001168 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001169 static linenr_T conceal_old_cursor_line = 0;
1170 static linenr_T conceal_new_cursor_line = 0;
1171 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001172#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001173
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001174 prev_oap = current_oap;
1175 current_oap = &oa;
1176
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001177#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001178 // Setup to catch a terminating error from the X server. Just ignore
1179 // it, restore the state and continue. This might not always work
1180 // properly, but at least we don't exit unexpectedly when the X server
1181 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001183 {
1184 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001185 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001186 got_int = TRUE;
1187 need_wait_return = FALSE;
1188 global_busy = FALSE;
1189 exmode_active = 0;
1190 skip_redraw = FALSE;
1191 RedrawingDisabled = 0;
1192 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001193 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001194# ifdef FEAT_EVAL
1195 emsg_skip = 0;
1196# endif
1197 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001198 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001199 settmode(TMODE_RAW);
1200 starttermcap();
1201 scroll_start();
1202 redraw_later_clear();
1203 }
1204#endif
1205
1206 clear_oparg(&oa);
1207 while (!cmdwin
1208#ifdef FEAT_CMDWIN
1209 || cmdwin_result == 0
1210#endif
1211 )
1212 {
1213 if (stuff_empty())
1214 {
1215 did_check_timestamps = FALSE;
1216 if (need_check_timestamps)
1217 check_timestamps(FALSE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001218 if (need_wait_return) // if wait_return still needed ...
1219 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001220 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001221 {
1222 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001223 stuffReadbuff((char_u *)"i"); // start insert mode next
1224 // skip the fileinfo message now, because it would be shown
1225 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001226 need_fileinfo = FALSE;
1227 }
1228 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001229
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001230 // Reset "got_int" now that we got back to the main loop. Except when
1231 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1232 // the ":g" command.
1233 // For ":g/pat/vi" we reset "got_int" when used once. When used
1234 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001235 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001236 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001237 if (noexmode && global_busy && !exmode_active && previous_got_int)
1238 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001239 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1240 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001241 exmode_active = EXMODE_NORMAL;
1242 State = NORMAL;
1243 }
1244 else if (!global_busy || !exmode_active)
1245 {
1246 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001247 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001248 got_int = FALSE;
1249 }
1250 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001251 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001252 else
1253 previous_got_int = FALSE;
1254
Bram Moolenaar069613c2022-01-15 15:23:44 +00001255#ifdef FEAT_EVAL
1256 // At the toplevel there is no exception handling. Discard any that
1257 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1258 if (did_throw && !ex_normal_busy)
1259 discard_current_exception();
1260#endif
1261
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001262 if (!exmode_active)
1263 msg_scroll = FALSE;
1264 quit_more = FALSE;
1265
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001266 // it's not safe unless may_trigger_safestate_main() is called
1267 was_safe = FALSE;
1268
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001269 /*
1270 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1271 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1272 * update cursor and redraw.
1273 */
1274 if (skip_redraw || exmode_active)
1275 skip_redraw = FALSE;
1276 else if (do_redraw || stuff_empty())
1277 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001278#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001279 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001280 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001281#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001282#ifdef HAVE_DROP_FILE
1283 // If files were dropped while text was locked or the curbuf was
1284 // locked, this would be a good time to handle the drop.
1285 handle_any_postponed_drop();
1286#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001287#ifdef FEAT_CONCEAL
1288 if (curwin->w_p_cole == 0)
1289 conceal_update_lines = FALSE;
1290#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001291
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001292 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001293 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001294 has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001295#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001296 || popup_visible
1297#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001298#ifdef FEAT_CONCEAL
1299 || curwin->w_p_cole > 0
1300#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001301 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001302 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001303 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001304 if (has_cursormoved())
1305 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1306 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001307#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001308 if (popup_visible)
1309 popup_check_cursor_pos();
1310#endif
1311#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001312 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001313 {
1314 conceal_old_cursor_line = last_cursormoved.lnum;
1315 conceal_new_cursor_line = curwin->w_cursor.lnum;
1316 conceal_update_lines = TRUE;
1317 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001318#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001319 last_cursormoved = curwin->w_cursor;
1320 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001321
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001322#if defined(FEAT_CONCEAL)
1323 if (conceal_update_lines
1324 && (conceal_old_cursor_line != conceal_new_cursor_line
1325 || conceal_cursor_line(curwin)
1326 || need_cursor_line_redraw))
1327 {
1328 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001329 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001330 && conceal_old_cursor_line
1331 <= curbuf->b_ml.ml_line_count)
1332 redrawWinline(curwin, conceal_old_cursor_line);
1333 redrawWinline(curwin, conceal_new_cursor_line);
1334 curwin->w_valid &= ~VALID_CROW;
1335 need_cursor_line_redraw = FALSE;
1336 }
1337#endif
1338
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001339 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001340 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001341 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001342 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001343 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1344 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001345 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001346
LemonBoy09371822022-04-08 15:18:45 +01001347 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1348 // before triggering a WinScrolled autocommand.
1349 update_topline();
1350 validate_cursor();
1351
1352 if (!finish_op)
zeertzjqd58862d2022-04-12 11:32:48 +01001353 may_trigger_winscrolled();
LemonBoy09371822022-04-08 15:18:45 +01001354
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001355 // If nothing is pending and we are going to wait for the user to
1356 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001357 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001358
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001359#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001360 // Updating diffs from changed() does not always work properly,
1361 // esp. updating folds. Do an update just before redrawing if
1362 // needed.
1363 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1364 {
1365 ex_diffupdate(NULL);
1366 curtab->tp_diff_update = FALSE;
1367 }
1368
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001369 // Scroll-binding for diff mode may have been postponed until
1370 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001371 if (diff_need_scrollbind)
1372 {
1373 check_scrollbind((linenr_T)0, 0L);
1374 diff_need_scrollbind = FALSE;
1375 }
1376#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001377#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001378 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001379 foldAdjustVisual();
1380#endif
1381#ifdef FEAT_FOLDING
1382 /*
1383 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1384 * contain the cursor.
1385 * When 'foldopen' is "all", open the fold(s) under the cursor.
1386 * This may mark the window for redrawing.
1387 */
1388 if (hasAnyFolding(curwin) && !char_avail())
1389 {
1390 foldCheckClose();
1391 if (fdo_flags & FDO_ALL)
1392 foldOpenCursor();
1393 }
1394#endif
1395
1396 /*
1397 * Before redrawing, make sure w_topline is correct, and w_leftcol
1398 * if lines don't wrap, and w_skipcol if lines wrap.
1399 */
1400 update_topline();
1401 validate_cursor();
1402
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001403 if (VIsual_active)
Bram Moolenaar017ba072019-09-14 21:01:23 +02001404 update_curbuf(INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001405 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001406 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001407 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001408 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001409 mch_enable_flush();
1410 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001411 else if (redraw_cmdline || clear_cmdline)
1412 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001414 if (need_maketitle)
1415 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001416#ifdef FEAT_VIMINFO
1417 curbuf->b_last_used = vim_time();
1418#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001419 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001420 if (keep_msg != NULL)
1421 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001422 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001424 if (p != NULL)
1425 {
1426 // msg_start() will set keep_msg to NULL, make a copy
1427 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1428 // check for duplicates. Never put this message in
1429 // history.
1430 msg_hist_off = TRUE;
1431 msg_attr((char *)p, keep_msg_attr);
1432 msg_hist_off = FALSE;
1433 vim_free(p);
1434 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001435 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001436 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001437 {
1438 fileinfo(FALSE, TRUE, FALSE);
1439 need_fileinfo = FALSE;
1440 }
1441
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001442 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001443 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001444 msg_didany = FALSE; // reset lines_left in msg_start()
1445 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001446 showruler(FALSE);
1447
1448 setcursor();
1449 cursor_on();
1450
1451 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001452
1453#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001454 // Now that we have drawn the first screen all the startup stuff
1455 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001456 if (time_fd != NULL)
1457 {
1458 TIME_MSG("first screen update");
1459 TIME_MSG("--- VIM STARTED ---");
1460 fclose(time_fd);
1461 time_fd = NULL;
1462 }
1463#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001464 }
1465#ifdef FEAT_GUI
1466 if (need_mouse_correct)
1467 gui_mouse_correct();
1468#endif
1469
1470 /*
1471 * Update w_curswant if w_set_curswant has been set.
1472 * Postponed until here to avoid computing w_virtcol too often.
1473 */
1474 update_curswant();
1475
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001476#ifdef FEAT_EVAL
1477 /*
1478 * May perform garbage collection when waiting for a character, but
1479 * only at the very toplevel. Otherwise we may be using a List or
1480 * Dict internally somewhere.
1481 * "may_garbage_collect" is reset in vgetc() which is invoked through
1482 * do_exmode() and normal_cmd().
1483 */
1484 may_garbage_collect = (!cmdwin && !noexmode);
1485#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001486 /*
1487 * If we're invoked as ex, do a round of ex commands.
1488 * Otherwise, get and execute a normal mode command.
1489 */
1490 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001491 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001492 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001493 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001494 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001495 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001496 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001497 {
1498#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001499 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001500 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001501 && !VIsual_active
1502 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001503 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001504 // If terminal_loop() returns OK we got a key that is handled
1505 // in Normal model. With FAIL we first need to position the
1506 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001507 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001508 normal_cmd(&oa, TRUE);
1509 }
1510 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001511#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001512 {
1513#ifdef FEAT_TERMINAL
1514 skip_term_loop = FALSE;
1515#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001516 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001517 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001518 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001519 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001520
1521theend:
1522 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001523}
1524
1525
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001526#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001527/*
1528 * Exit, but leave behind swap files for modified buffers.
1529 */
1530 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001531getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001532{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001533# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001534 // Ignore SIGHUP, because a dropped connection causes a read error, which
1535 // makes Vim exit and then handling SIGHUP causes various reentrance
1536 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001537 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001538# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001539
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001540 ml_close_notmod(); // close all not-modified buffers
1541 ml_sync_all(FALSE, FALSE); // preserve all swap files
1542 ml_close_all(FALSE); // close all memfiles, without deleting
1543 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001544}
1545#endif
1546
1547
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001548/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001549 * Exit properly. This is the only way to exit Vim after startup has
1550 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001551 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001552 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001553getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001554{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001555 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001556#if defined(FEAT_JOB_CHANNEL)
1557 ch_log(NULL, "Exiting...");
1558#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001559
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001560 // When running in Ex mode an error causes us to exit with a non-zero exit
1561 // code. POSIX requires this, although it's not 100% clear from the
1562 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001563 if (exmode_active)
1564 exitval += ex_exitval;
1565
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001566#ifdef FEAT_EVAL
1567 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1568 set_vim_var_nr(VV_EXITING, exitval);
1569#endif
1570
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001571 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001572 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001573 windgoto((int)Rows - 1, 0);
1574
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001575#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001576 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001577 hash_debug_results();
1578#endif
1579
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001580#ifdef FEAT_GUI
1581 msg_didany = FALSE;
1582#endif
1583
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001584 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001585 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001586 tabpage_T *tp;
1587 tabpage_T *next_tp;
1588 buf_T *buf;
1589 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001590 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001591
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001592 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001593 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001594 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001595 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001596 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001597 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001598 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001599 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001600 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001601 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001602 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001603 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001604 bufref_T bufref;
1605
1606 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001607 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1608 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001609 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001610 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001611
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001612 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001613 next_tp = first_tabpage;
1614 break;
1615 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001616 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001617 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001618
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001619 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001620 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001621 if (buf->b_ml.ml_mfp != NULL)
1622 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001623 bufref_T bufref;
1624
1625 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001626 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001627 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001628 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001629 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001630 break;
1631 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001632
1633 // deathtrap() blocks autocommands, but we do want to trigger
1634 // VimLeavePre.
1635 if (is_autocmd_blocked())
1636 {
1637 unblock_autocmds();
1638 ++unblock;
1639 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001640 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001641 if (unblock)
1642 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001643 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001644
1645#ifdef FEAT_VIMINFO
1646 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001647 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001648 write_viminfo(NULL, FALSE);
1649#endif
1650
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001651 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001652 {
1653 int unblock = 0;
1654
1655 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1656 if (is_autocmd_blocked())
1657 {
1658 unblock_autocmds();
1659 ++unblock;
1660 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001661 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001662 if (unblock)
1663 block_autocmds();
1664 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001665
Bram Moolenaar05159a02005-02-26 23:04:13 +00001666#ifdef FEAT_PROFILE
1667 profile_dump();
1668#endif
1669
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001670 if (did_emsg
1671#ifdef FEAT_GUI
1672 || (gui.in_use && msg_didany && p_verbose > 0)
1673#endif
1674 )
1675 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001676 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001677 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001678 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001679 }
1680
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001681 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001682 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001683 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001684
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001685#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001686 job_stop_on_exit();
1687#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001688#ifdef FEAT_LUA
1689 lua_end();
1690#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001691#ifdef FEAT_MZSCHEME
1692 mzscheme_end();
1693#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001694#ifdef FEAT_TCL
1695 tcl_end();
1696#endif
1697#ifdef FEAT_RUBY
1698 ruby_end();
1699#endif
1700#ifdef FEAT_PYTHON
1701 python_end();
1702#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001703#ifdef FEAT_PYTHON3
1704 python3_end();
1705#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001706#ifdef FEAT_PERL
1707 perl_end();
1708#endif
1709#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1710 iconv_end();
1711#endif
1712#ifdef FEAT_NETBEANS_INTG
1713 netbeans_end();
1714#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001715#ifdef FEAT_CSCOPE
1716 cs_end();
1717#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001718#ifdef FEAT_EVAL
1719 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001720 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001721#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001722#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001723 free_cmd_argsW();
1724#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001725
1726 mch_exit(exitval);
1727}
1728
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001729/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001730 * Get the name of the display, before gui_prepare() removes it from
1731 * argv[]. Used for the xterm-clipboard display.
1732 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001733 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001734 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001735 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001736early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001737{
Bram Moolenaar03005972008-11-20 13:12:36 +00001738#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1739 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001740 int argc = parmp->argc;
1741 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001742 int i;
1743
1744 for (i = 1; i < argc; i++)
1745 {
1746 if (STRCMP(argv[i], "--") == 0)
1747 break;
1748# ifdef FEAT_XCLIPBOARD
1749 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001750# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001751 || STRICMP(argv[i], "--display") == 0
1752# endif
1753 )
1754 {
1755 if (i == argc - 1)
1756 mainerr_arg_missing((char_u *)argv[i]);
1757 xterm_display = argv[++i];
1758 }
1759# endif
1760# ifdef FEAT_CLIENTSERVER
1761 else if (STRICMP(argv[i], "--servername") == 0)
1762 {
1763 if (i == argc - 1)
1764 mainerr_arg_missing((char_u *)argv[i]);
1765 parmp->serverName_arg = (char_u *)argv[++i];
1766 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001767 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001768 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001769 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001770 {
1771 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001772# ifdef FEAT_GUI
1773 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001774 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001775 gui.dofork = FALSE;
1776# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001777 }
1778# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001779
Bram Moolenaar4f974752019-02-17 17:44:42 +01001780# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1781# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001782 else if (STRICMP(argv[i], "--windowid") == 0)
1783# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001784 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001785# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001786 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001787 long_u id;
1788 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001789
1790 if (i == argc - 1)
1791 mainerr_arg_missing((char_u *)argv[i]);
1792 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001793 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001794 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001795 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001796 if (count != 1)
1797 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1798 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001799# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001800 win_socket_id = id;
1801# else
1802 gtk_socket_id = id;
1803# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001804 i++;
1805 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001806# endif
1807# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001808 else if (STRICMP(argv[i], "--echo-wid") == 0)
1809 echo_wid_arg = TRUE;
1810# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001811# ifndef FEAT_NETBEANS_INTG
1812 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001813 {
1814 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1815 mch_exit(2);
1816 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001817# endif
1818
Bram Moolenaar58d98232005-07-23 22:25:46 +00001819 }
1820#endif
1821}
1822
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001823#ifndef NO_VIM_MAIN
1824/*
1825 * Get a (optional) count for a Vim argument.
1826 */
1827 static int
1828get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001829 char_u *p, // pointer to argument
1830 int *idx, // index in argument, is incremented
1831 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001832{
1833 if (vim_isdigit(p[*idx]))
1834 {
1835 def = atoi((char *)&(p[*idx]));
1836 while (vim_isdigit(p[*idx]))
1837 *idx = *idx + 1;
1838 }
1839 return def;
1840}
1841
1842/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001843 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001844 * If the executable name starts with "r" we disable shell commands.
1845 * If the next character is "e" we run in Easy mode.
1846 * If the next character is "g" we run the GUI version.
1847 * If the next characters are "view" we start in readonly mode.
1848 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1849 * If the next characters are "ex" we start in Ex mode. If it's followed
1850 * by "im" use improved Ex mode.
1851 */
1852 static void
1853parse_command_name(mparm_T *parmp)
1854{
1855 char_u *initstr;
1856
1857 initstr = gettail((char_u *)parmp->argv[0]);
1858
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001859#ifdef FEAT_EVAL
1860 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001861 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001862#endif
1863
1864 if (TOLOWER_ASC(initstr[0]) == 'r')
1865 {
1866 restricted = TRUE;
1867 ++initstr;
1868 }
1869
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001870 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001871 if (TOLOWER_ASC(initstr[0]) == 'e'
1872 && (TOLOWER_ASC(initstr[1]) == 'v'
1873 || TOLOWER_ASC(initstr[1]) == 'g'))
1874 {
1875#ifdef FEAT_GUI
1876 gui.starting = TRUE;
1877#endif
1878 parmp->evim_mode = TRUE;
1879 ++initstr;
1880 }
1881
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001882 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001883 if (TOLOWER_ASC(initstr[0]) == 'g')
1884 {
1885 main_start_gui();
1886#ifdef FEAT_GUI
1887 ++initstr;
1888#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001889#ifdef GUI_MAY_SPAWN
1890 gui.dospawn = FALSE; // No need to spawn a new process.
1891#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001892 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001893#ifdef GUI_MAY_SPAWN
1894 else
1895 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1896#endif
1897
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001898
1899 if (STRNICMP(initstr, "view", 4) == 0)
1900 {
1901 readonlymode = TRUE;
1902 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001903 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001904 initstr += 4;
1905 }
1906 else if (STRNICMP(initstr, "vim", 3) == 0)
1907 initstr += 3;
1908
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001909 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001910 if (STRICMP(initstr, "diff") == 0)
1911 {
1912#ifdef FEAT_DIFF
1913 parmp->diff_mode = TRUE;
1914#else
1915 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1916 mch_errmsg("\n");
1917 mch_exit(2);
1918#endif
1919 }
1920
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001921 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001922 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001923 if (STRNICMP(initstr, "ex", 2) == 0)
1924 {
1925 if (STRNICMP(initstr + 2, "im", 2) == 0)
1926 exmode_active = EXMODE_VIM;
1927 else
1928 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001929 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001930 }
1931}
1932
Bram Moolenaar58d98232005-07-23 22:25:46 +00001933/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001934 * Scan the command line arguments.
1935 */
1936 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001937command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001938{
1939 int argc = parmp->argc;
1940 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001941 int argv_idx; // index in argv[n][]
1942 int had_minmin = FALSE; // found "--" argument
1943 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001944 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001945 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001946 long n;
1947
1948 --argc;
1949 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001950 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001951 while (argc > 0)
1952 {
1953 /*
1954 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1955 */
1956 if (argv[0][0] == '+' && !had_minmin)
1957 {
1958 if (parmp->n_commands >= MAX_ARG_CMDS)
1959 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001960 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001961 if (argv[0][1] == NUL)
1962 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1963 else
1964 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1965 }
1966
1967 /*
1968 * Optional argument.
1969 */
1970 else if (argv[0][0] == '-' && !had_minmin)
1971 {
1972 want_argument = FALSE;
1973 c = argv[0][argv_idx++];
1974#ifdef VMS
1975 /*
1976 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1977 * and "-/X" as "-X".
1978 */
1979 if (c == '/')
1980 {
1981 c = argv[0][argv_idx++];
1982 c = TOUPPER_ASC(c);
1983 }
1984 else
1985 c = TOLOWER_ASC(c);
1986#endif
1987 switch (c)
1988 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001989 case NUL: // "vim -" read from stdin
1990 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001991 if (exmode_active)
1992 silent_mode = TRUE;
1993 else
1994 {
1995 if (parmp->edit_type != EDIT_NONE)
1996 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1997 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001998 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001999 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002000 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002001 break;
2002
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002003 case '-': // "--" don't take any more option arguments
2004 // "--help" give help message
2005 // "--version" give version message
2006 // "--clean" clean context
2007 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002008 // "--startuptime fname" write timing info
2009 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002010 // "--nofork" don't fork
2011 // "--not-a-term" don't warn for not a term
2012 // "--ttyfail" exit if not a term
2013 // "--noplugin[s]" skip plugins
2014 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002015 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2016 usage();
2017 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2018 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002019 Columns = 80; // need to init Columns
2020 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002021#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002022 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002023#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002024 list_version();
2025 msg_putchar('\n');
2026 msg_didout = FALSE;
2027 mch_exit(0);
2028 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002029 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2030 {
2031 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002032#ifdef FEAT_GUI
2033 use_gvimrc = (char_u *)"NONE";
2034#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002035 parmp->clean = TRUE;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002036 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
2037 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002038 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2039 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002040#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002041 parmp->literal = TRUE;
2042#endif
2043 }
2044 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2045 {
2046#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002047 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002048#endif
2049 }
2050 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2051 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002052 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2053 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002054 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2055 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002056 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2057 {
2058 want_argument = TRUE;
2059 argv_idx += 3;
2060 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002061 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2062 {
2063 want_argument = TRUE;
2064 argv_idx += 11;
2065 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002066 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2067 {
2068 want_argument = TRUE;
2069 argv_idx += 3;
2070 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071#ifdef FEAT_CLIENTSERVER
2072 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002073 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002074 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2075 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2076 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002077 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002078 if (argc > 1)
2079 {
2080 --argc;
2081 ++argv;
2082 }
2083 }
2084#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002085#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002086# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002087 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002088# else
2089 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2090# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002091 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002092 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002093 if (argc > 1)
2094 {
2095 --argc;
2096 ++argv;
2097 }
2098 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002099#endif
2100#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002101 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2102 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002103 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002104 }
2105#endif
2106 else
2107 {
2108 if (argv[0][argv_idx])
2109 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2110 had_minmin = TRUE;
2111 }
2112 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002113 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002114 break;
2115
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002116 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002117#ifdef FEAT_ARABIC
2118 set_option_value((char_u *)"arabic", 1L, NULL, 0);
2119#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002120 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002121 mch_exit(2);
2122#endif
2123 break;
2124
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002125 case 'b': // "-b" binary mode
2126 // Needs to be effective before expanding file names, because
2127 // for Win32 this makes us edit a shortcut file itself,
2128 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002129 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002130 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002131 break;
2132
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002133 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002134 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002135 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002136 break;
2137
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002138 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002139 exmode_active = EXMODE_NORMAL;
2140 break;
2141
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002142 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002143 exmode_active = EXMODE_VIM;
2144 break;
2145
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002146 case 'f': // "-f" GUI: run in foreground. Amiga: open
2147 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002148#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002149 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002150#endif
2151 break;
2152
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002153 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002154 main_start_gui();
2155 break;
2156
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002157 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002158 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002159 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002160 break;
2161
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002162 case '?': // "-?" give help message (for MS-Windows)
2163 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002164#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002165 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002166 gui.starting = FALSE;
2167#endif
2168 usage();
2169 break;
2170
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002171 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002172#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002173 p_hkmap = TRUE;
2174 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002175#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002176 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002177 mch_exit(2);
2178#endif
2179 break;
2180
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002181 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002182#ifdef FEAT_LISP
2183 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2184 p_sm = TRUE;
2185#endif
2186 break;
2187
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002188 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002189 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002190 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002191
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002192 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002193 p_write = FALSE;
2194 break;
2195
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002196 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002197#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002198 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002199#endif
2200 parmp->evim_mode = TRUE;
2201 break;
2202
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002203 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002204 change_compatible(FALSE);
2205 break;
2206
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002207 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002208#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002209 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002210 if (argv[0][argv_idx] == 'b')
2211 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002212 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002213 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002214 }
2215 else
2216#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002217 parmp->no_swap_file = TRUE;
2218 break;
2219
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002220 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002221#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002222 // For some reason on MacOS X, an argument like:
2223 // -psn_0_10223617 is passed in when invoke from Finder
2224 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002225 if (argv[0][argv_idx] == 's')
2226 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002227 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002228 main_start_gui();
2229 break;
2230 }
2231#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002232 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002233 parmp->window_count = get_number_arg((char_u *)argv[0],
2234 &argv_idx, 0);
2235 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002236 break;
2237
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002238 case 'o': // "-o[N]" open N horizontal split windows
2239 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002240 parmp->window_count = get_number_arg((char_u *)argv[0],
2241 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002242 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002243 break;
2244
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002245 case 'O': // "-O[N]" open N vertical split windows
2246 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002247 parmp->window_count = get_number_arg((char_u *)argv[0],
2248 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002249 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002250 break;
2251
2252#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002253 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002254 if (parmp->edit_type != EDIT_NONE)
2255 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2256 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002257 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002258 {
2259 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2260 argv_idx = -1;
2261 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002262 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002263 want_argument = TRUE;
2264 break;
2265#endif
2266
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002267 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002268 readonlymode = TRUE;
2269 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002270 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002271 break;
2272
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002273 case 'r': // "-r" recovery mode
2274 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002275 recoverymode = 1;
2276 break;
2277
2278 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002279 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002280 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002281 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002282 want_argument = TRUE;
2283 break;
2284
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002285 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002286 if (parmp->edit_type != EDIT_NONE)
2287 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2288 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002289 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002290 {
2291 parmp->tagname = (char_u *)argv[0] + argv_idx;
2292 argv_idx = -1;
2293 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002294 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002295 want_argument = TRUE;
2296 break;
2297
2298#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002299 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002300 parmp->use_debug_break_level = 9999;
2301 break;
2302#endif
2303#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002304 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002305# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002306 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002307 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2308 want_argument = TRUE;
2309 else
2310# endif
2311 parmp->diff_mode = TRUE;
2312 break;
2313#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002314 case 'V': // "-V{N}" Verbose level
2315 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002316 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2317 if (argv[0][argv_idx] != NUL)
2318 {
2319 set_option_value((char_u *)"verbosefile", 0L,
2320 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002321 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002322 }
2323 break;
2324
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002325 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002326 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002327#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002328 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002329#endif
2330 break;
2331
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002332 case 'w': // "-w{number}" set window height
2333 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002334 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2335 {
2336 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2337 set_option_value((char_u *)"window", n, NULL, 0);
2338 break;
2339 }
2340 want_argument = TRUE;
2341 break;
2342
2343#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002344 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002345 parmp->ask_for_key = TRUE;
2346 break;
2347#endif
2348
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002349 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002350#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2351 x_no_connect = TRUE;
2352#endif
2353 break;
2354
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002355 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002356 restricted = TRUE;
2357 break;
2358
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002359 case 'c': // "-c{command}" or "-c {command}" execute
2360 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002361 if (argv[0][argv_idx] != NUL)
2362 {
2363 if (parmp->n_commands >= MAX_ARG_CMDS)
2364 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002365 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2366 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002367 argv_idx = -1;
2368 break;
2369 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002370 // FALLTHROUGH
2371 case 'S': // "-S {file}" execute Vim script
2372 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002373#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002374 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002375#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002376 case 'T': // "-T {terminal}" terminal name
2377 case 'u': // "-u {vimrc}" vim inits file
2378 case 'U': // "-U {gvimrc}" gvim inits file
2379 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002380#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002381 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002382#endif
2383 want_argument = TRUE;
2384 break;
2385
2386 default:
2387 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2388 }
2389
2390 /*
2391 * Handle option arguments with argument.
2392 */
2393 if (want_argument)
2394 {
2395 /*
2396 * Check for garbage immediately after the option letter.
2397 */
2398 if (argv[0][argv_idx] != NUL)
2399 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2400
2401 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002402 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002403 mainerr_arg_missing((char_u *)argv[0]);
2404 ++argv;
2405 argv_idx = -1;
2406
2407 switch (c)
2408 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002409 case 'c': // "-c {command}" execute command
2410 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002411 if (parmp->n_commands >= MAX_ARG_CMDS)
2412 mainerr(ME_EXTRA_CMD, NULL);
2413 if (c == 'S')
2414 {
2415 char *a;
2416
2417 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002418 // "-S" without argument: use default session file
2419 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002420 a = SESSION_FILE;
2421 else if (argv[0][0] == '-')
2422 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002423 // "-S" followed by another option: use default
2424 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002425 a = SESSION_FILE;
2426 ++argc;
2427 --argv;
2428 }
2429 else
2430 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002431 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002432 if (p == NULL)
2433 mch_exit(2);
2434 sprintf((char *)p, "so %s", a);
2435 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2436 parmp->commands[parmp->n_commands++] = p;
2437 }
2438 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002439 parmp->commands[parmp->n_commands++] =
2440 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002441 break;
2442
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002443 case '-':
2444 if (argv[-1][2] == 'c')
2445 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002446 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002447 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2448 mainerr(ME_EXTRA_CMD, NULL);
2449 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002450 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002451 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002452 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002453 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454 break;
2455
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002456 // case 'd': -d {device} is handled in mch_check_win() for the
2457 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002458
2459#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002460 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002461 parmp->use_ef = (char_u *)argv[0];
2462 break;
2463#endif
2464
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002465 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002466 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002467 break;
2468
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002469 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002470 if (scriptin[0] != NULL)
2471 {
2472scripterror:
2473 mch_errmsg(_("Attempt to open script file again: \""));
2474 mch_errmsg(argv[-1]);
2475 mch_errmsg(" ");
2476 mch_errmsg(argv[0]);
2477 mch_errmsg("\"\n");
2478 mch_exit(2);
2479 }
2480 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2481 {
2482 mch_errmsg(_("Cannot open for reading: \""));
2483 mch_errmsg(argv[0]);
2484 mch_errmsg("\"\n");
2485 mch_exit(2);
2486 }
2487 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002488 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002489 break;
2490
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002491 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002492 parmp->tagname = (char_u *)argv[0];
2493 break;
2494
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002495 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002496 /*
2497 * The -T term argument is always available and when
2498 * HAVE_TERMLIB is supported it overrides the environment
2499 * variable TERM.
2500 */
2501#ifdef FEAT_GUI
2502 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002503 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002504 else
2505#endif
2506 parmp->term = (char_u *)argv[0];
2507 break;
2508
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002509 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002510 parmp->use_vimrc = (char_u *)argv[0];
2511 break;
2512
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002513 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002514#ifdef FEAT_GUI
2515 use_gvimrc = (char_u *)argv[0];
2516#endif
2517 break;
2518
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002519 case 'w': // "-w {nr}" 'window' value
2520 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002521 if (vim_isdigit(*((char_u *)argv[0])))
2522 {
2523 argv_idx = 0;
2524 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2525 set_option_value((char_u *)"window", n, NULL, 0);
2526 argv_idx = -1;
2527 break;
2528 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002529 // FALLTHROUGH
2530 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002531 if (scriptout != NULL)
2532 goto scripterror;
2533 if ((scriptout = mch_fopen(argv[0],
2534 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2535 {
2536 mch_errmsg(_("Cannot open for script output: \""));
2537 mch_errmsg(argv[0]);
2538 mch_errmsg("\"\n");
2539 mch_exit(2);
2540 }
2541 break;
2542
Bram Moolenaar4f974752019-02-17 17:44:42 +01002543#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002544 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545 gui_mch_set_parent(argv[0]);
2546 break;
2547#endif
2548 }
2549 }
2550 }
2551
2552 /*
2553 * File name argument.
2554 */
2555 else
2556 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002557 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002559 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2561 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2562 parmp->edit_type = EDIT_FILE;
2563
2564#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002565 // Remember if the argument was a full path before changing
2566 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2568 parmp->full_path = TRUE;
2569#endif
2570
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002571 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002572 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2573 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2574 mch_exit(2);
2575#ifdef FEAT_DIFF
2576 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2577 && !mch_isdir(alist_name(&GARGLIST[0])))
2578 {
2579 char_u *r;
2580
2581 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2582 if (r != NULL)
2583 {
2584 vim_free(p);
2585 p = r;
2586 }
2587 }
2588#endif
K.Takatab247e062022-02-07 10:45:23 +00002589#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590 /*
2591 * If vim is invoked by non-Cygwin tools, convert away any
2592 * DOS paths, so things like .swp files are created correctly.
2593 * Look for evidence of non-Cygwin paths before we bother.
2594 * This is only for when using the Unix files.
2595 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002596 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002598 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002600# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002601 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002602# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002603 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002604# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002605 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002606 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002607 if (p == NULL)
2608 mch_exit(2);
2609 }
2610#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002611
2612#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002613 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002614 fname_case(p, 0);
2615#endif
2616
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002617 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002618#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002619 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002620#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002621 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002622#endif
2623 );
2624
Bram Moolenaar4f974752019-02-17 17:44:42 +01002625#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002626 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002627 // Remember this argument has been added to the argument list.
2628 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002629 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002630# ifdef FEAT_DIFF
2631 parmp->diff_mode
2632# else
2633 FALSE
2634# endif
2635 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002636 }
2637#endif
2638 }
2639
2640 /*
2641 * If there are no more letters after the current "-", go to next
2642 * argument. argv_idx is set to -1 when the current argument is to be
2643 * skipped.
2644 */
2645 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2646 {
2647 --argc;
2648 ++argv;
2649 argv_idx = 1;
2650 }
2651 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002652
2653#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002654 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2655 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002656 if (parmp->n_commands > 0)
2657 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002658 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002659 if (p != NULL)
2660 {
2661 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2662 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2663 vim_free(p);
2664 }
2665 }
2666#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667}
2668
2669/*
2670 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002671 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002672 */
2673 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002674check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002675{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002676 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677
2678 input_isatty = mch_input_isatty();
2679 if (exmode_active)
2680 {
2681 if (!input_isatty)
2682 silent_mode = TRUE;
2683 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002684 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002685#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002686 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002687 && !gui.starting
2688#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002689 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690 {
2691#ifdef NBDEBUG
2692 /*
2693 * This shouldn't be necessary. But if I run netbeans with the log
2694 * output coming to the console and XOpenDisplay fails, I get vim
2695 * trying to start with input/output to my console tty. This fills my
2696 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002697 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002698 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002699 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002700 {
2701 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2702 exit(1);
2703 }
2704#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002705#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2706 if (
2707# ifdef VIMDLL
2708 !gui.starting &&
2709# endif
2710 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002711 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002712# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002713 && defined(FEAT_GETTEXT)
2714 char *s, *tofree = NULL;
2715
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002716 // Set the encoding of the error message based on $LC_ALL or
2717 // other environment variables instead of 'encoding'.
2718 // Note that the message is shown on a Cygwin terminal (e.g.
2719 // mintty) which encoding is based on $LC_ALL or etc., not the
2720 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002721 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002722 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002723 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002724 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2725 vim_free(tofree);
2726# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002727 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2728 exit(1);
2729 }
2730#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002731 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002732 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2733 if (!input_isatty)
2734 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2735 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002736 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2737 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002738 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002739 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002740 TIME_MSG("Warning delay");
2741 }
2742}
2743
2744/*
2745 * Read text from stdin.
2746 */
2747 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002748read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002749{
2750 int i;
2751
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002752 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002753 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002754
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002755 no_wait_return = TRUE;
2756 i = msg_didany;
2757 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002758
2759 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002760 (void)open_buffer(TRUE, NULL, 0);
2761
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002762 no_wait_return = FALSE;
2763 msg_didany = i;
2764 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002765
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002766 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002767
2768#if !(defined(AMIGA) || defined(MACOS_X))
2769 // Dup stdin from stderr to read commands from, so that shell commands
2770 // work.
2771 // TODO: why is this needed, even though readfile() has done this?
2772 close(0);
2773 vim_ignored = dup(2);
2774#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002775}
2776
2777/*
2778 * Create the requested number of windows and edit buffers in them.
2779 * Also does recovery if "recoverymode" set.
2780 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002782create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002784 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002785 int done = 0;
2786
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002787 /*
2788 * Create the number of windows that was requested.
2789 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002790 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002791 parmp->window_count = 1;
2792 if (parmp->window_count == 0)
2793 parmp->window_count = GARGCOUNT;
2794 if (parmp->window_count > 1)
2795 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002796 // Don't change the windows if there was a command in .vimrc that
2797 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002798 if (parmp->window_layout == 0)
2799 parmp->window_layout = WIN_HOR;
2800 if (parmp->window_layout == WIN_TABS)
2801 {
2802 parmp->window_count = make_tabpages(parmp->window_count);
2803 TIME_MSG("making tab pages");
2804 }
2805 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002806 {
2807 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002808 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002809 TIME_MSG("making windows");
2810 }
2811 else
2812 parmp->window_count = win_count();
2813 }
2814 else
2815 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002817 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002818 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002819 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002820 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002821 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002822 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002823 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002824 }
2825 else
2826 {
2827 /*
2828 * Open a buffer for windows that don't have one yet.
2829 * Commands in the .vimrc might have loaded a file or split the window.
2830 * Watch out for autocommands that delete a window.
2831 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002832 /*
2833 * Don't execute Win/Buf Enter/Leave autocommands here
2834 */
2835 ++autocmd_no_enter;
2836 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002837 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002838 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002839 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002840 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002841 {
2842 if (parmp->window_layout == WIN_TABS)
2843 goto_tabpage(1);
2844 else
2845 curwin = firstwin;
2846 }
2847 else if (parmp->window_layout == WIN_TABS)
2848 {
2849 if (curtab->tp_next == NULL)
2850 break;
2851 goto_tabpage(0);
2852 }
2853 else
2854 {
2855 if (curwin->w_next == NULL)
2856 break;
2857 curwin = curwin->w_next;
2858 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002859 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002860 curbuf = curwin->w_buffer;
2861 if (curbuf->b_ml.ml_mfp == NULL)
2862 {
2863#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002864 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002865 if (p_fdls >= 0)
2866 curwin->w_p_fdl = p_fdls;
2867#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002868 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002869 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002870
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002872
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002873 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002874 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875
Bram Moolenaar84212822006-11-07 21:59:47 +00002876 if (swap_exists_action == SEA_QUIT)
2877 {
2878 if (got_int || only_one_window())
2879 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002880 // abort selected or quit and only one window
2881 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002882 getout(1);
2883 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002884 // We can't close the window, it would disturb what
2885 // happens next. Clear the file name and set the arg
2886 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002887 setfname(curbuf, NULL, NULL, FALSE);
2888 curwin->w_arg_idx = -1;
2889 swap_exists_action = SEA_NONE;
2890 }
2891 else
2892 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002893 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002894 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002895 ui_breakcheck();
2896 if (got_int)
2897 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002898 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 break;
2900 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002902 if (parmp->window_layout == WIN_TABS)
2903 goto_tabpage(1);
2904 else
2905 curwin = firstwin;
2906 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002907 --autocmd_no_enter;
2908 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002909 }
2910}
2911
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002912/*
2913 * If opened more than one window, start editing files in the other
2914 * windows. make_windows() has already opened the windows.
2915 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002916 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002917edit_buffers(
2918 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002919 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002920{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002921 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002922 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002923 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002924 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002925 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002926
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002927 /*
2928 * Don't execute Win/Buf Enter/Leave autocommands here
2929 */
2930 ++autocmd_no_enter;
2931 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002932
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002933 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002934 if (curwin->w_arg_idx == -1)
2935 {
2936 win_close(curwin, TRUE);
2937 advance = FALSE;
2938 }
2939
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002940 arg_idx = 1;
2941 for (i = 1; i < parmp->window_count; ++i)
2942 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002943 if (cwd != NULL)
2944 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002945 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002946 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002947 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002948 ++arg_idx;
2949 win_close(curwin, TRUE);
2950 advance = FALSE;
2951 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002952 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002953
Bram Moolenaar84212822006-11-07 21:59:47 +00002954 if (advance)
2955 {
2956 if (parmp->window_layout == WIN_TABS)
2957 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002958 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002959 break;
2960 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002961 // Temporarily reset 'shm' option to not print fileinfo when
2962 // loading the other buffers. This would overwrite the already
2963 // existing fileinfo for the first tab.
2964 if (i == 1)
2965 {
2966 char buf[100];
2967
2968 p_shm_save = vim_strsave(p_shm);
2969 vim_snprintf(buf, 100, "F%s", p_shm);
2970 set_option_value((char_u *)"shm", 0L, (char_u *)buf, 0);
2971 }
Bram Moolenaar84212822006-11-07 21:59:47 +00002972 }
2973 else
2974 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002975 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002976 break;
2977 win_enter(curwin->w_next, FALSE);
2978 }
2979 }
2980 advance = TRUE;
2981
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002982 // Only open the file if there is no file in this window yet (that can
2983 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002984 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2985 {
2986 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002987 // Edit file from arg list, if there is one. When "Quit" selected
2988 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002989 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002990 (void)do_ecmd(0, arg_idx < GARGCOUNT
2991 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002992 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002993 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002994 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002995 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00002996 if (got_int || only_one_window())
2997 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002998 // abort selected and only one window
2999 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003000 getout(1);
3001 }
3002 win_close(curwin, TRUE);
3003 advance = FALSE;
3004 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003005 if (arg_idx == GARGCOUNT - 1)
3006 arg_had_last = TRUE;
3007 ++arg_idx;
3008 }
3009 ui_breakcheck();
3010 if (got_int)
3011 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003012 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003013 break;
3014 }
3015 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003016
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003017 if (p_shm_save != NULL)
3018 {
3019 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
3020 vim_free(p_shm_save);
3021 }
3022
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003023 if (parmp->window_layout == WIN_TABS)
3024 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003025 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003026
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003027 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003028 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003029#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003030 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003031 while (win->w_p_pvw)
3032 {
3033 win = win->w_next;
3034 if (win == NULL)
3035 {
3036 win = firstwin;
3037 break;
3038 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003039 }
3040#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003041 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003042
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003043 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003044 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003045 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003046 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003047}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003048
3049/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003050 * Execute the commands from --cmd arguments "cmds[cnt]".
3051 */
3052 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003053exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003054{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003055 char_u **cmds = parmp->pre_commands;
3056 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003057 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003058 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003059
3060 if (cnt > 0)
3061 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003062 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003063 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003064 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003065# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003066 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003067# endif
3068 for (i = 0; i < cnt; ++i)
3069 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003070 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003071 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003072# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003073 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003074# endif
3075 TIME_MSG("--cmd commands");
3076 }
3077}
3078
3079/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003080 * Execute "+", "-c" and "-S" arguments.
3081 */
3082 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003083exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003084{
3085 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003086 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003087
3088 /*
3089 * We start commands on line 0, make "vim +/pat file" match a
3090 * pattern on line 1. But don't move the cursor when an autocommand
3091 * with g`" was used.
3092 */
3093 msg_scroll = TRUE;
3094 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3095 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003096 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003097 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003098#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003099 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003100 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003101#endif
3102 for (i = 0; i < parmp->n_commands; ++i)
3103 {
3104 do_cmdline_cmd(parmp->commands[i]);
3105 if (parmp->cmds_tofree[i])
3106 vim_free(parmp->commands[i]);
3107 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003108 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003109 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003110#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003111 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003112#endif
3113 if (curwin->w_cursor.lnum == 0)
3114 curwin->w_cursor.lnum = 1;
3115
3116 if (!exmode_active)
3117 msg_scroll = FALSE;
3118
3119#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003120 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003121 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003122 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003123#endif
3124 TIME_MSG("executing command arguments");
3125}
3126
3127/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003128 * Source startup scripts.
3129 */
3130 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003131source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003132{
3133 int i;
3134
3135 /*
3136 * For "evim" source evim.vim first of all, so that the user can overrule
3137 * any things he doesn't like.
3138 */
3139 if (parmp->evim_mode)
3140 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003142 TIME_MSG("source evim file");
3143 }
3144
3145 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003146 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003147 * nothing else.
3148 */
3149 if (parmp->use_vimrc != NULL)
3150 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003151 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003152 {
3153 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3154 != OK)
3155 emsg(e_failed_to_source_defaults);
3156 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003157 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003158 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003159 {
3160#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003161 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003162 use_gvimrc = parmp->use_vimrc;
3163#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003164 }
3165 else
3166 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003168 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003169 }
3170 }
3171 else if (!silent_mode)
3172 {
3173#ifdef AMIGA
3174 struct Process *proc = (struct Process *)FindTask(0L);
3175 APTR save_winptr = proc->pr_WindowPtr;
3176
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003177 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003178 proc->pr_WindowPtr = (APTR)-1L;
3179#endif
3180
3181 /*
3182 * Get system wide defaults, if the file name is defined.
3183 */
3184#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003186#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003187#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3189 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003190#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003191
3192 /*
3193 * Try to read initialization commands from the following places:
3194 * - environment variable VIMINIT
3195 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3196 * - second user vimrc file ($VIM/.vimrc for Dos)
3197 * - environment variable EXINIT
3198 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3199 * - second user exrc file ($VIM/.exrc for Dos)
3200 * The first that exists is used, the rest is ignored.
3201 */
3202 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3203 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3205 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003206#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003207 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003209#endif
3210#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003211 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003213#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003214#ifdef USR_VIMRC_FILE4
3215 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003217#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003218 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3220 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003221#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3223 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003224#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003225 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003226 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003227 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003228 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3229 NULL) == FAIL)
3230 emsg(e_failed_to_source_defaults);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003231 }
3232 }
3233
3234 /*
3235 * Read initialization commands from ".vimrc" or ".exrc" in current
3236 * directory. This is only done if the 'exrc' option is set.
3237 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003238 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003239 * option has been reset in environment of global ".exrc" or ".vimrc".
3240 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3241 * SYS_VIMRC_FILE.
3242 */
3243 if (p_exrc)
3244 {
3245#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003246 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003247 if (!file_owned(VIMRC_FILE))
3248#endif
3249 secure = p_secure;
3250
3251 i = FAIL;
3252 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003253 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003254#ifdef USR_VIMRC_FILE2
3255 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003256 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003257#endif
3258#ifdef USR_VIMRC_FILE3
3259 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003260 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003261#endif
3262#ifdef SYS_VIMRC_FILE
3263 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003264 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003265#endif
3266 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003268
3269 if (i == FAIL)
3270 {
3271#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003272 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003273 if (!file_owned(EXRC_FILE))
3274 secure = p_secure;
3275 else
3276 secure = 0;
3277#endif
3278 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003279 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003280#ifdef USR_EXRC_FILE2
3281 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003282 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003283#endif
3284 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 (void)do_source((char_u *)EXRC_FILE, FALSE,
3286 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003287 }
3288 }
3289 if (secure == 2)
3290 need_wait_return = TRUE;
3291 secure = 0;
3292#ifdef AMIGA
3293 proc->pr_WindowPtr = save_winptr;
3294#endif
3295 }
3296 TIME_MSG("sourcing vimrc file(s)");
3297}
3298
3299/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003300 * Setup to start using the GUI. Exit with an error when not available.
3301 */
3302 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003303main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003304{
3305#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003306 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003307#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003308 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003309 mch_errmsg("\n");
3310 mch_exit(2);
3311#endif
3312}
3313
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003314#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003315
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003316/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 * Returns FAIL if the environment variable was not executed, OK otherwise.
3319 */
3320 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003321process_env(
3322 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003323 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003324{
3325 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003326 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003327
Bram Moolenaare31ee862020-01-07 20:59:34 +01003328 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003329
3330 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3331 {
3332 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003333 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003334 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003335 ESTACK_CHECK_SETUP
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003336 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003337 current_sctx.sc_version = 1;
3338#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003339 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003340 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003341 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003342#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003343
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003344 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003345
Bram Moolenaare31ee862020-01-07 20:59:34 +01003346 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003347 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003348 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349 return OK;
3350 }
3351 return FAIL;
3352}
3353
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003354#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003355/*
3356 * Return TRUE if we are certain the user owns the file "fname".
3357 * Used for ".vimrc" and ".exrc".
3358 * Use both stat() and lstat() for extra security.
3359 */
3360 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003361file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003362{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003363 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003364# ifdef UNIX
3365 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003366# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003367 uid_t uid = ((getgid() << 16) | getuid());
3368# endif
3369
3370 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3371# ifdef HAVE_LSTAT
3372 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3373# endif
3374 );
3375}
3376#endif
3377
3378/*
3379 * Give an error message main_errors["n"] and exit.
3380 */
3381 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003382mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003383 int n, // one of the ME_ defines
3384 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003385{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003386#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003387 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388#endif
3389
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003390 // If this is a Windows GUI executable, show an error dialog box.
3391#ifdef VIMDLL
3392 gui.in_use = mch_is_gui_executable();
3393#endif
3394#ifdef FEAT_GUI_MSWIN
3395 gui.starting = FALSE; // Needed to show as error.
3396#endif
3397
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003398 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003400 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003401 mch_errmsg(_(main_errors[n]));
3402 if (str != NULL)
3403 {
3404 mch_errmsg(": \"");
3405 mch_errmsg((char *)str);
3406 mch_errmsg("\"");
3407 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003408 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409
3410 mch_exit(1);
3411}
3412
3413 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003414mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415{
3416 mainerr(ME_ARG_MISSING, str);
3417}
3418
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003419#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003420/*
3421 * print a message with three spaces prepended and '\n' appended.
3422 */
3423 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003424main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425{
3426 mch_msg(" ");
3427 mch_msg(s);
3428 mch_msg("\n");
3429}
3430
3431/*
3432 * Print messages for "vim -h" or "vim --help" and exit.
3433 */
3434 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003435usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436{
3437 int i;
3438 static char *(use[]) =
3439 {
3440 N_("[file ..] edit specified file(s)"),
3441 N_("- read text from stdin"),
3442 N_("-t tag edit file where tag is defined"),
3443#ifdef FEAT_QUICKFIX
3444 N_("-q [errorfile] edit file with first error")
3445#endif
3446 };
3447
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003448#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003449 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450#endif
3451
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003452 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003453 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003454 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003455 for (i = 0; ; ++i)
3456 {
3457 mch_msg(_(" vim [arguments] "));
3458 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003459 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003460 break;
3461 mch_msg(_("\n or:"));
3462 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003463#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003464 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003465#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003466
3467 mch_msg(_("\n\nArguments:\n"));
3468 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003469#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003470 main_msg(_("--literal\t\tDon't expand wildcards"));
3471#endif
3472#ifdef FEAT_OLE
3473 main_msg(_("-register\t\tRegister this gvim for OLE"));
3474 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3475#endif
3476#ifdef FEAT_GUI
3477 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3478 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3479#endif
3480 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3481 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003482 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003483 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3484#ifdef FEAT_DIFF
3485 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3486#endif
3487 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3488 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3489 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3490 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3491 main_msg(_("-M\t\t\tModifications in text not allowed"));
3492 main_msg(_("-b\t\t\tBinary mode"));
3493#ifdef FEAT_LISP
3494 main_msg(_("-l\t\t\tLisp mode"));
3495#endif
3496 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3497 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003498 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3499#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003500 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003501#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003502 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3503 main_msg(_("-r\t\t\tList swap files and exit"));
3504 main_msg(_("-r (with file name)\tRecover crashed session"));
3505 main_msg(_("-L\t\t\tSame as -r"));
3506#ifdef AMIGA
3507 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3508 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3509#endif
3510#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003511 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003512#endif
3513#ifdef FEAT_RIGHTLEFT
3514 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3515#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003516 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003517 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003518 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003519 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3520#ifdef FEAT_GUI
3521 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3522#endif
3523 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003524 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003525 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3526 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3527 main_msg(_("+\t\t\tStart at end of file"));
3528 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003530 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3531 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3532 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3533 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3534 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3535#ifdef FEAT_CRYPT
3536 main_msg(_("-x\t\t\tEdit encrypted files"));
3537#endif
3538#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3539# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003540 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541# endif
3542 main_msg(_("-X\t\t\tDo not connect to X server"));
3543#endif
3544#ifdef FEAT_CLIENTSERVER
3545 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3546 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3547 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3548 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003549 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003550 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3551 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3552 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3553 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3554#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003555#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003556 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003557#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003558#ifdef FEAT_JOB_CHANNEL
3559 main_msg(_("--log <file>\tStart logging to <file> early"));
3560#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003561#ifdef FEAT_VIMINFO
3562 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3563#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003564 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003565 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3566 main_msg(_("--version\t\tPrint version information and exit"));
3567
3568#ifdef FEAT_GUI_X11
3569# ifdef FEAT_GUI_MOTIF
3570 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003571# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003572 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003573 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003574 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3575 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3576 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3577 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3578 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3579 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3580 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3581 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003582 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3583 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3584 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003585#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003586#ifdef FEAT_GUI_GTK
3587 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003588 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3589 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003590 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3591 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003592 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003593 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003594 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003595 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003596 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003597 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003598#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003599#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003600# ifdef VIMDLL
3601 if (gui.starting)
3602# endif
3603 {
3604 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3605 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3606 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003607#endif
3608
3609#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003610 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003611 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003612 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003613 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003614 gui.dofork = FALSE;
3615 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003616 else
3617#endif
3618 mch_exit(0);
3619}
3620
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003621/*
3622 * Check the result of the ATTENTION dialog:
3623 * When "Quit" selected, exit Vim.
3624 * When "Recover" selected, recover the file.
3625 */
3626 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003627check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003628{
3629 if (swap_exists_action == SEA_QUIT)
3630 getout(1);
3631 handle_swap_exists(NULL);
3632}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003633
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003634#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003635
Bram Moolenaar595297d2017-03-04 19:11:12 +01003636#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003637 static void
3638set_progpath(char_u *argv0)
3639{
3640 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003641
Bram Moolenaar4f974752019-02-17 17:44:42 +01003642# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003643 // A relative path containing a "/" will become invalid when using ":cd",
3644 // turn it into a full path.
3645 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3646 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003647 char_u *path = NULL;
3648
3649 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3650 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003651# else
3652 char_u buf[MAXPATHL + 1];
3653# ifdef PROC_EXE_LINK
3654 char linkbuf[MAXPATHL + 1];
3655 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003656
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003657 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3658 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003659 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003660 linkbuf[len] = NUL;
3661 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003662 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003663# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003664
3665 if (!mch_isFullName(val))
3666 {
3667 if (gettail(val) != val
3668 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3669 val = buf;
3670 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003671# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003672
Bram Moolenaar08cab962017-03-04 14:37:18 +01003673 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003674
Bram Moolenaar4f974752019-02-17 17:44:42 +01003675# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003676 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003677# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003678}
3679
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003680#endif // NO_VIM_MAIN