blob: 0299c61a7954f090ccf6590b37653a62073e507a [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 Moolenaare76062c2022-11-28 18:51:43 +0000126 autocmd_init();
127
Bram Moolenaar99685e62013-05-11 13:56:18 +0200128#ifdef FEAT_RUBY
129 {
130 int ruby_stack_start;
131 vim_ruby_init((void *)&ruby_stack_start);
132 }
133#endif
134
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000135#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000137#endif
138
139#ifdef MEM_PROFILE
140 atexit(vim_mem_profile_dump);
141#endif
142
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100143#if defined(STARTUPTIME) || defined(FEAT_JOB_CHANNEL)
144 // Need to find "--startuptime" and "--log" before actually parsing
145 // arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100146 for (i = 1; i < argc - 1; ++i)
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100147 {
148# ifdef STARTUPTIME
149 if (STRICMP(argv[i], "--startuptime") == 0 && time_fd == NULL)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000150 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000151 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000152 TIME_MSG("--- VIM STARTING ---");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000153 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100154# endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000155# ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100156 if (STRICMP(argv[i], "--log") == 0)
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100157 ch_logfile((char_u *)(argv[i + 1]), (char_u *)"ao");
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100158# endif
159 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000160#endif
161
Bram Moolenaar07268702018-03-01 21:57:32 +0100162#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100163 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100164 for (i = 1; i < argc; ++i)
165 if (STRICMP(argv[i], "--clean") == 0)
166 {
167 params.clean = TRUE;
168 break;
169 }
170#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200171#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200172 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200173 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200174 if ((STRICMP(argv[i] + 1, "register") == 0
175 || STRICMP(argv[i] + 1, "unregister") == 0)
176 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200177 {
178 found_register_arg = TRUE;
179 break;
180 }
181#endif
182
183 /*
184 * Various initialisations shared with tests.
185 */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200186 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000187
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200188#ifdef VIMDLL
189 // Check if the current executable file is for the GUI subsystem.
190 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200191#elif defined(FEAT_GUI_MSWIN)
192 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200193#endif
194
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195#ifdef FEAT_CLIENTSERVER
196 /*
197 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000198 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000199 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000200 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000201#endif
202
203 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000204 * Figure out the way to work from the command name argv[0].
205 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000206 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000207 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000208
209 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000210 * Process the command line arguments. File names are put in the global
211 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000213 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214 TIME_MSG("parsing arguments");
215
216 /*
217 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000218 * there is no terminal version, and on Windows we can't fork one off with
219 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000220 */
221#ifdef ALWAYS_USE_GUI
222 gui.starting = TRUE;
223#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000224# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000225 /*
226 * Check if the GUI can be started. Reset gui.starting if not.
227 * Don't know about other systems, stay on the safe side and don't check.
228 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000229 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000231 if (gui_init_check() == FAIL)
232 {
233 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100235 // When running "evim" or "gvim -y" we need the menus, exit if we
236 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000237 if (params.evim_mode)
238 mch_exit(1);
239 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 }
241# endif
242#endif
243
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 if (GARGCOUNT > 0)
245 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100246#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000247 /*
248 * Expand wildcards in file names.
249 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000250 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000251 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200252 start_dir = alloc(MAXPATHL);
253 if (start_dir != NULL)
254 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100255 // Temporarily add '(' and ')' to 'isfname'. These are valid
256 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000257 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000258 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000259 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000260 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200261 if (start_dir != NULL)
262 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000263 }
264#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200265 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000266 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000267
Bram Moolenaar4f974752019-02-17 17:44:42 +0100268#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000269 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100270 // Remember the number of entries in the argument list. If it changes
271 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000272 set_alist_count();
273 }
274#endif
275
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000276#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000277 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000278 {
279 /*
280 * If there is one filename, fully qualified, we have very probably
281 * been invoked from explorer, so change to the file's directory.
282 * Hint: to avoid this when typing a command use a forward slash.
283 * If the cd fails, it doesn't matter.
284 */
Bram Moolenaar05268152021-11-18 18:53:45 +0000285 if (vim_chdirfile(params.fname, "drop") == OK)
286 last_chdir_reason = "drop";
Bram Moolenaarf6303872015-04-03 17:59:43 +0200287 if (start_dir != NULL)
288 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000289 }
290#endif
291 TIME_MSG("expanding arguments");
292
293#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000294 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100295 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000296#endif
297
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100298 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000299 ++RedrawingDisabled;
300
301 /*
302 * When listing swap file names, don't do cursor positioning et. al.
303 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200304 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000305 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000306
307 /*
308 * When certain to start the GUI, don't check capabilities of terminal.
309 * For GTK we can't be sure, but when started from the desktop it doesn't
310 * make sense to try using a terminal.
311 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200312#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
313 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000314 if (gui.starting
315# ifdef FEAT_GUI_GTK
316 && !isatty(2)
317# endif
318 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000319 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000320#endif
321
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000322 /*
323 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100324 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000325 * Note that we may use mch_exit() before mch_init()!
326 */
327 mch_init();
328 TIME_MSG("shell init");
329
330#ifdef USE_XSMP
331 /*
332 * For want of anywhere else to do it, try to connect to xsmp here.
333 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
334 * Hijacking -X 'no X connection' to also disable XSMP connection as that
335 * has a similar delay upon failure.
336 * Only try if SESSION_MANAGER is set to something non-null.
337 */
338 if (!x_no_connect)
339 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000340 char *p = getenv("SESSION_MANAGER");
341
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000342 if (p != NULL && *p != NUL)
343 {
344 xsmp_init();
345 TIME_MSG("xsmp init");
346 }
347 }
348#endif
349
350 /*
351 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000352 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000354
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100355#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100356 // Ensure output works usefully without a tty: buffer lines instead of
357 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100358 if (silent_mode)
359 setvbuf(stdout, NULL, _IOLBF, 0);
360#endif
361
Bram Moolenaar9404a182019-05-03 22:25:40 +0200362 // This message comes before term inits, but after setting "silent_mode"
363 // when the input is not a tty. Omit the message with --not-a-term.
364 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000365 printf(_("%d files to edit\n"), GARGCOUNT);
366
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000368 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100369 termcapinit(params.term); // set terminal name and get terminal
370 // capabilities (will set full_screen)
371 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372 TIME_MSG("Termcap init");
373 }
374
375 /*
376 * Set the default values for the options that use Rows and Columns.
377 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100378 ui_get_shellsize(); // inits Rows and Columns
Luuk van Baal470a1412022-09-14 01:27:23 +0100379 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100381 // Set the 'diff' option now, so that it can be checked for in a .vimrc
382 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000383 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 diff_win_options(firstwin, FALSE);
385#endif
386
387 cmdline_row = Rows - p_ch;
388 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100389 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000390 set_init_2();
391 TIME_MSG("inits 2");
392
393 msg_scroll = TRUE;
394 no_wait_return = TRUE;
395
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100396 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100398 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200401#if defined(FEAT_TERMRESPONSE)
402 init_term_props(TRUE);
403#endif
404
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000405#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100406 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000407 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408#endif
409
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100410 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
411 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200412 if (params.use_vimrc != NULL
413 && (STRCMP(params.use_vimrc, "NONE") == 0
414 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
415 p_lpl = FALSE;
416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200418 exe_pre_commands(&params);
419
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100420 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200421 source_startup_scripts(&params);
422
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100423#ifdef FEAT_MZSCHEME
424 /*
425 * Newer version of MzScheme (Racket) require earlier (trampolined)
426 * initialisation via scheme_main_setup.
427 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200428 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200429 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100430 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200431 return mzscheme_main();
432#else
433 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100434#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200435}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100436#endif // NO_VIM_MAIN
437#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100438
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200439/*
440 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
441 * things simple.
442 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
443 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100444 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200445vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100446{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100447#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000448#ifdef FEAT_EVAL
449 /*
450 * Read all the plugin files.
451 * Only when compiled with +eval, since most plugins need it.
452 */
453 if (p_lpl)
454 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200455 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100456 char_u *plugin_pattern = (char_u *)
457# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
458 "plugin/*.vim"
459# else
460 "plugin/**/*.vim"
461# endif
462 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200463
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100464 // First add all package directories to 'runtimepath', so that their
465 // autoload directories can be found. Only if not done already with a
466 // :packloadall command.
467 // Make a copy of 'runtimepath', so that source_runtime does not use
468 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200469 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200470 {
471 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200472 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200473 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200474
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100475 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000477 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200478 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100479
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100480 // Only source "start" packages if not done already with a :packloadall
481 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200482 if (!did_source_packages)
483 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100484 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200485
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100486 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200487 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000488 }
489#endif
490
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000491#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100492 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000493 if (params.diff_mode && params.window_layout == 0)
494 {
495 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100496 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000497 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100498 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000499 }
500#endif
501
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000502 /*
503 * Recovery mode without a file name: List swap files.
504 * This uses the 'dir' option, therefore it must be after the
505 * initializations.
506 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200507 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000508 {
Bram Moolenaarc216a7a2022-12-05 13:50:55 +0000509 recover_names(NULL, TRUE, NULL, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 mch_exit(0);
511 }
512
513 /*
514 * Set a few option defaults after reading .vimrc files:
515 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
516 */
517 set_init_3();
518 TIME_MSG("inits 3");
519
520 /*
521 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
522 * Note that this overrides anything from a vimrc file.
523 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000524 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000525 p_uc = 0;
526
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527#ifdef FEAT_GUI
528 if (gui.starting)
529 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200530# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100531 // When something caused a message from a vimrc script, need to output
532 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000533 if (did_emsg || msg_didout)
534 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200535# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000536
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100537 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538 TIME_MSG("starting GUI");
539
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100540 // When running "evim" or "gvim -y" we need the menus, exit if we
541 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000542 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000543 mch_exit(1);
Luuk van Baal470a1412022-09-14 01:27:23 +0100544 firstwin->w_prev_height = firstwin->w_height; // may have changed
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000545 }
546#endif
547
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548#ifdef FEAT_VIMINFO
549 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000550 * Read in registers, history etc, but not marks, from the viminfo file.
551 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000552 */
553 if (*p_viminfo != NUL)
554 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000555 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556 TIME_MSG("reading viminfo");
557 }
558#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100559#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100560 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100561 if (get_vim_var_list(VV_OLDFILES) == NULL)
562 set_vim_var_list(VV_OLDFILES, list_alloc());
563#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564
565#ifdef FEAT_QUICKFIX
566 /*
567 * "-q errorfile": Load the error file now.
568 * If the error file can't be read, exit before doing anything else.
569 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000570 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000571 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100572 char_u *enc = NULL;
573
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100574 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000575 if (params.use_ef != NULL)
576 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000577 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200578 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100579 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000580 {
581 out_char('\n');
582 mch_exit(3);
583 }
584 TIME_MSG("reading errorfile");
585 }
586#endif
587
588 /*
589 * Start putting things on the screen.
590 * Scroll screen down before drawing over it
591 * Clear screen now, so file message will not be cleared.
592 */
593 starting = NO_BUFFERS;
594 no_wait_return = FALSE;
595 if (!exmode_active)
596 msg_scroll = FALSE;
597
598#ifdef FEAT_GUI
599 /*
600 * This seems to be required to make callbacks to be called now, instead
601 * of after things have been put on the screen, which then may be deleted
602 * when getting a resize callback.
603 * For the Mac this handles putting files dropped on the Vim icon to
604 * global_alist.
605 */
606 if (gui.in_use)
607 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100608 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000609 TIME_MSG("GUI delay");
610 }
611#endif
612
613#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
614 qnx_clip_init();
615#endif
616
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200617#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
618 clip_init(TRUE);
619#endif
620
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000621#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100622 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000623# ifdef FEAT_GUI
624 if (!gui.in_use)
625# endif
626 {
627 setup_term_clip();
628 TIME_MSG("setup clipboard");
629 }
630#endif
631
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000632#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100633 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000634 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635#endif
636
637 /*
638 * If "-" argument given: Read file from stdin.
639 * Do this before starting Raw mode, because it may change things that the
640 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
641 * are the same terminal: "cat | vim -".
642 * Using autocommands here may cause trouble...
643 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000644 if (params.edit_type == EDIT_STDIN && !recoverymode)
645 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000646
647#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100648 // When switching screens and something caused a message from a vimrc
649 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000650 if ((did_emsg || msg_didout) && *T_TI != NUL)
651 newline_on_exit = TRUE;
652#endif
653
654 /*
K.Takataeeec2542021-06-02 13:28:16 +0200655 * When done something that is not allowed or given an error message call
Bram Moolenaar13608d82022-08-29 15:06:50 +0100656 * wait_return(). This must be done before starttermcap(), because it may
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000657 * switch to another screen. It must be done after settmode(TMODE_RAW),
658 * because we want to react on a single key stroke.
659 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000660 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000661 */
662 settmode(TMODE_RAW);
663 TIME_MSG("setting raw mode");
664
665 if (need_wait_return || msg_didany)
666 {
667 wait_return(TRUE);
668 TIME_MSG("waiting for return");
669 }
670
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100671 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000672 TIME_MSG("start termcap");
673
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200674 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000675 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100676 scroll_region_reset(); // In case Rows changed
677 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000678
Bram Moolenaar651fca82021-11-29 20:39:38 +0000679#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200680 term_push_title(SAVE_RESTORE_BOTH);
681#endif
682
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000683 /*
684 * Don't clear the screen when starting in Ex mode, unless using the GUI.
685 */
686 if (exmode_active
687#ifdef FEAT_GUI
688 && !gui.in_use
689#endif
690 )
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100691 set_must_redraw(UPD_CLEAR);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000692 else
693 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100694 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000695 TIME_MSG("clearing screen");
696 }
697
698#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000699 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000700 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100701 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200702 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 TIME_MSG("getting crypt key");
704 }
705#endif
706
707 no_wait_return = TRUE;
708
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000709 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000710 * Create the requested number of windows and edit buffers in them.
711 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000713 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000714 TIME_MSG("opening buffers");
715
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000716#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100717 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000718 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
719#endif
720
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100721 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000722 if (exmode_active)
723 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
724
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
726 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 setpcmark();
728
729#ifdef FEAT_QUICKFIX
730 /*
731 * When started with "-q errorfile" jump to first error now.
732 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000733 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000734 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000735 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 TIME_MSG("jump to first error");
737 }
738#endif
739
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740 /*
741 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000742 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000743 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200744 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200745 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000746
747#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000748 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749 {
750 win_T *wp;
751
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100752 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200753 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 diff_win_options(wp, TRUE);
755 }
756#endif
757
758 /*
759 * Shorten any of the filenames, but only when absolute.
760 */
761 shorten_fnames(FALSE);
762
763 /*
764 * Need to jump to the tag before executing the '-c command'.
765 * Makes "vim -c '/return' -t main" work.
766 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000767 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000769 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000770
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000771 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000772 do_cmdline_cmd(IObuff);
773 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000774
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100775 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000776 if (swap_exists_did_quit)
777 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000778 }
779
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100780 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000781 if (params.n_commands > 0)
782 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000783
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100784 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200785 starting = 0;
786
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100787#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100788 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200789 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200790#endif
791
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000792 RedrawingDisabled = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100793 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100796 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200797 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200798
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000799#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100800 // Requesting the termresponse is postponed until here, so that a "-c q"
801 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000802 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200803
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200804 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000805#endif
806
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100807 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000808 if (p_im)
809 need_start_insertmode = TRUE;
810
Bram Moolenaar14735512016-03-26 21:00:08 +0100811#ifdef FEAT_EVAL
812 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
813#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
815 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000816
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200817#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100818 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
819 // done after the clipboard is available and all initial commands that may
820 // modify the 'clipboard' setting have run; i.e. just before entering the
821 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200822 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200823#endif
824
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100825#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100826 // When a startup script or session file setup for diff'ing and
827 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000828 if (curwin->w_p_diff && curwin->w_p_scb)
829 {
830 update_topline();
831 check_scrollbind((linenr_T)0, 0L);
832 TIME_MSG("diff scrollbinding");
833 }
834#endif
835
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200836#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
837# ifdef VIMDLL
838 if (!gui.in_use)
839# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100840 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841#endif
842
Bram Moolenaar4033c552017-09-16 20:54:51 +0200843#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100844 // When tab pages were created, may need to update the tab pages line and
845 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000846 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000847 {
848 out_flush();
849 gui_init_which_components(NULL);
850 gui_update_scrollbars(TRUE);
851 }
852 need_mouse_correct = TRUE;
853#endif
854
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100855 // If ":startinsert" command used, stuff a dummy command to be able to
856 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000857 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000858 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000859
860#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200861 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200862 {
863# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200864# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100865 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200866 if (gui.in_use)
867 {
868 mch_errmsg(_("netbeans is not supported with this GUI\n"));
869 mch_exit(2);
870 }
871# endif
872# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100873 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200874 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200875 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000876#endif
877
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100878 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
879 // window title gets updated.
880 do_redraw = TRUE;
881
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882 TIME_MSG("before starting main loop");
883
884 /*
885 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200886 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000887 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000888
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100889#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200890
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891 return 0;
892}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893
894/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200895 * Initialisation shared by main() and some tests.
896 */
897 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200898common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200899{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100900 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200901 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200902
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100903 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200904#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100905 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200906#endif
907
908#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100909 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200910#endif
911
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200912 /*
913 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200915 */
916 if ((IObuff = alloc(IOSIZE)) == NULL
917 || (NameBuff = alloc(MAXPATHL)) == NULL)
918 mch_exit(0);
919 TIME_MSG("Allocated generic buffers");
920
921#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100922 // Wait a moment for debugging NetBeans. Must be after allocating
923 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200924 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
925 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
926 TIME_MSG("NetBeans debug wait");
927#endif
928
929#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
930 /*
931 * Setup to use the current locale (for ctype() and many other things).
932 * NOTE: Translated messages with encodings other than latin1 will not
933 * work until set_init_1() has been called!
934 */
935 init_locale();
936 TIME_MSG("locale set");
937#endif
938
939#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100940 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200941#endif
942
943 /*
944 * Do a first scan of the arguments in "argv[]":
945 * -display or --display
946 * --server...
947 * --socketid
948 * --windowid
949 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200950 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200951
Bram Moolenaard0573012017-10-28 21:11:06 +0200952#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100953 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200954 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200955 TIME_MSG("GUI prepared");
956#endif
957
958#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100959 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200960 TIME_MSG("clipboard setup");
961#endif
962
963 /*
964 * Check if we have an interactive window.
965 * On the Amiga: If there is no window, we open one with a newcli command
966 * (needed for :! to * work). mch_check_win() will also handle the -d or
967 * -dev argument.
968 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100969 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200970 TIME_MSG("window checked");
971
972 /*
973 * Allocate the first window and buffer.
974 * Can't do anything without it, exit when it fails.
975 */
976 if (win_alloc_first() == FAIL)
977 mch_exit(0);
978
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100979 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200980
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100981 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200982 global_alist.id = 0;
983
984 /*
985 * Set the default values for the options.
986 * NOTE: Non-latin1 translated messages are working only after this,
987 * because this is where "has_mbyte" will be set, which is used by
988 * msg_outtrans_len_attr().
989 * First find out the home directory, needed to expand "~" in options.
990 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100991 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +0100992 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200993 TIME_MSG("inits 1");
994
995#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100996 // set v:lang and v:ctype
997 set_lang_var();
998
999 // set v:argv
1000 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001001#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001002
1003#ifdef FEAT_SIGNS
1004 init_signs();
1005#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001006}
1007
1008/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001009 * Return TRUE when the --not-a-term argument was found.
1010 */
1011 int
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001012is_not_a_term(void)
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001013{
1014 return params.not_a_term;
1015}
1016
Bram Moolenaar7007e312021-03-27 12:11:33 +01001017/*
1018 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1019 */
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001020 int
1021is_not_a_term_or_gui(void)
Bram Moolenaar7007e312021-03-27 12:11:33 +01001022{
1023 return params.not_a_term
1024#ifdef FEAT_GUI
1025 || gui.in_use
1026#endif
1027 ;
1028}
1029
Bram Moolenaar2d12c252022-06-13 21:42:45 +01001030#if defined(FEAT_GUI) || defined(PROTO)
1031/*
1032 * If a --gui-dialog-file argument was given return the file name.
1033 * Otherwise return NULL.
1034 */
1035 char_u *
1036get_gui_dialog_file(void)
1037{
1038 return params.gui_dialog_file;
1039}
1040#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001041
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001042// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001043static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001044static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001045
1046/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001047 * Return TRUE if an operator was started but not finished yet.
1048 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001049 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001050 int
1051op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001052{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001053 return !(current_oap != NULL
1054 && !finish_op
1055 && current_oap->prev_opcount == 0
1056 && current_oap->prev_count0 == 0
1057 && current_oap->op_type == OP_NOP
1058 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001059}
1060
1061/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001062 * Return whether currently it is safe, assuming it was safe before (high level
1063 * state didn't change).
1064 */
1065 static int
1066is_safe_now(void)
1067{
1068 return stuff_empty()
1069 && typebuf.tb_len == 0
1070 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001071#ifdef FEAT_EVAL
1072 && !debug_mode
1073#endif
1074 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001075}
1076
1077/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001078 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1079 * there is no typeahead.
1080 */
1081 void
1082may_trigger_safestate(int safe)
1083{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001084 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001085
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001086#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001087 if (was_safe != is_safe)
1088 // Only log when the state changes, otherwise it happens at nearly
1089 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001090 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1091 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001092#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001093 if (is_safe)
1094 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1095 was_safe = is_safe;
1096}
1097
1098/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001099 * Something changed which causes the state possibly to be unsafe, e.g. a
1100 * character was typed. It will remain unsafe until the next call to
1101 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001102 */
1103 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001104state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001105{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001106#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001107 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001108 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001109#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001110 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001111}
1112
Dominique Pelle748b3082022-01-08 12:41:16 +00001113#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001114 int
1115get_was_safe_state(void)
1116{
1117 return was_safe;
1118}
Dominique Pelle748b3082022-01-08 12:41:16 +00001119#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001120
Dominique Pelle748b3082022-01-08 12:41:16 +00001121#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001122/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001123 * Invoked when leaving code that invokes callbacks. Then trigger
1124 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001125 */
1126 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001127may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001128{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001129 if (!was_safe)
1130 {
1131 // If the safe state was reset in state_no_longer_safe(), e.g. because
1132 // of calling feedkeys(), we check if it's now safe again (all keys
1133 // were consumed).
1134 was_safe = is_safe_now();
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001135# ifdef FEAT_EVAL
Bram Moolenaard103ee72019-09-18 21:15:31 +02001136 if (was_safe)
1137 ch_log(NULL, "SafeState: undo reset");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001138# endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001139 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001140 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001141 {
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001142# ifdef FEAT_EVAL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001143 // Only do this message when another message was given, otherwise we
1144 // get lots of them.
1145 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1146 {
1147 int did = did_repeated_msg;
1148
1149 ch_log(NULL,
1150 "SafeState: back to waiting, triggering SafeStateAgain");
1151 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1152 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001153# endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001154 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001155 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001156# ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001157 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001158 ch_log(NULL,
1159 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001160# endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001161}
Dominique Pelle748b3082022-01-08 12:41:16 +00001162#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001163
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001164/*
1165 * Return TRUE if there is any typeahead, pending operator or command.
1166 */
1167 int
1168work_pending(void)
1169{
1170 return op_pending() || !is_safe_now();
1171}
1172
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001173
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001174/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001175 * Main loop: Execute Normal mode commands until exiting Vim.
1176 * Also used to handle commands in the command-line window, until the window
1177 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 * Also used to handle ":visual" command after ":global": execute Normal mode
1179 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001180 */
1181 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001182main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001183 int cmdwin, // TRUE when working in the command-line window
1184 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001185{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001186 oparg_T oa; // operator arguments
1187 oparg_T *prev_oap; // operator arguments
1188 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001189#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001190 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001191 static linenr_T conceal_old_cursor_line = 0;
1192 static linenr_T conceal_new_cursor_line = 0;
1193 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001194#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001195
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001196 prev_oap = current_oap;
1197 current_oap = &oa;
1198
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001199#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001200 // Setup to catch a terminating error from the X server. Just ignore
1201 // it, restore the state and continue. This might not always work
1202 // properly, but at least we don't exit unexpectedly when the X server
1203 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001205 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001206 State = MODE_NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001207 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001208 got_int = TRUE;
1209 need_wait_return = FALSE;
1210 global_busy = FALSE;
1211 exmode_active = 0;
1212 skip_redraw = FALSE;
1213 RedrawingDisabled = 0;
1214 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001215 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001216# ifdef FEAT_EVAL
1217 emsg_skip = 0;
1218# endif
1219 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001220 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001221 settmode(TMODE_RAW);
1222 starttermcap();
1223 scroll_start();
1224 redraw_later_clear();
1225 }
1226#endif
1227
1228 clear_oparg(&oa);
Martin Tournoij7904fa42022-10-04 16:28:45 +01001229 while (!cmdwin || cmdwin_result == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001230 {
1231 if (stuff_empty())
1232 {
1233 did_check_timestamps = FALSE;
1234 if (need_check_timestamps)
1235 check_timestamps(FALSE);
Bram Moolenaar13608d82022-08-29 15:06:50 +01001236 if (need_wait_return) // if wait_return() still needed ...
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001237 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001238 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001239 {
1240 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001241 stuffReadbuff((char_u *)"i"); // start insert mode next
1242 // skip the fileinfo message now, because it would be shown
1243 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001244 need_fileinfo = FALSE;
1245 }
1246 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001247
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001248 // Reset "got_int" now that we got back to the main loop. Except when
1249 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1250 // the ":g" command.
1251 // For ":g/pat/vi" we reset "got_int" when used once. When used
1252 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001253 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001254 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001255 if (noexmode && global_busy && !exmode_active && previous_got_int)
1256 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001257 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1258 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001259 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001260 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001261 }
1262 else if (!global_busy || !exmode_active)
1263 {
1264 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001265 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001266 got_int = FALSE;
1267 }
1268 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001269 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001270 else
1271 previous_got_int = FALSE;
1272
Bram Moolenaar069613c2022-01-15 15:23:44 +00001273#ifdef FEAT_EVAL
1274 // At the toplevel there is no exception handling. Discard any that
1275 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1276 if (did_throw && !ex_normal_busy)
1277 discard_current_exception();
1278#endif
1279
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 if (!exmode_active)
1281 msg_scroll = FALSE;
1282 quit_more = FALSE;
1283
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001284 // it's not safe unless may_trigger_safestate_main() is called
1285 was_safe = FALSE;
1286
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001287 /*
1288 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1289 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1290 * update cursor and redraw.
1291 */
1292 if (skip_redraw || exmode_active)
1293 skip_redraw = FALSE;
1294 else if (do_redraw || stuff_empty())
1295 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001296#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001297 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001298 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001299#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001300#ifdef HAVE_DROP_FILE
1301 // If files were dropped while text was locked or the curbuf was
1302 // locked, this would be a good time to handle the drop.
1303 handle_any_postponed_drop();
1304#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001305#ifdef FEAT_CONCEAL
1306 if (curwin->w_p_cole == 0)
1307 conceal_update_lines = FALSE;
1308#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001309
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001310 // Trigger CursorMoved if the cursor moved.
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001311 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001312#ifdef FEAT_PROP_POPUP
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001313 || popup_visible
Bram Moolenaar3397f742019-06-02 18:40:06 +02001314#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001315#ifdef FEAT_CONCEAL
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001316 || curwin->w_p_cole > 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001317#endif
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001318 )
1319 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001320 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001321 if (has_cursormoved())
1322 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1323 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001324#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001325 if (popup_visible)
1326 popup_check_cursor_pos();
1327#endif
1328#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001329 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001330 {
1331 conceal_old_cursor_line = last_cursormoved.lnum;
1332 conceal_new_cursor_line = curwin->w_cursor.lnum;
1333 conceal_update_lines = TRUE;
1334 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001335#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001336 last_cursormoved = curwin->w_cursor;
1337 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001338
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001339#if defined(FEAT_CONCEAL)
1340 if (conceal_update_lines
1341 && (conceal_old_cursor_line != conceal_new_cursor_line
1342 || conceal_cursor_line(curwin)
1343 || need_cursor_line_redraw))
1344 {
1345 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001346 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001347 && conceal_old_cursor_line
1348 <= curbuf->b_ml.ml_line_count)
1349 redrawWinline(curwin, conceal_old_cursor_line);
1350 redrawWinline(curwin, conceal_new_cursor_line);
1351 curwin->w_valid &= ~VALID_CROW;
1352 need_cursor_line_redraw = FALSE;
1353 }
1354#endif
1355
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001356 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001357 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001358 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001359 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001360 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1361 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001362 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001363
LemonBoy09371822022-04-08 15:18:45 +01001364 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1365 // before triggering a WinScrolled autocommand.
1366 update_topline();
1367 validate_cursor();
1368
1369 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001370 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01001371
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001372 // If nothing is pending and we are going to wait for the user to
1373 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001374 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001375
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001376#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001377 // Updating diffs from changed() does not always work properly,
1378 // esp. updating folds. Do an update just before redrawing if
1379 // needed.
1380 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1381 {
1382 ex_diffupdate(NULL);
1383 curtab->tp_diff_update = FALSE;
1384 }
1385
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001386 // Scroll-binding for diff mode may have been postponed until
1387 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001388 if (diff_need_scrollbind)
1389 {
1390 check_scrollbind((linenr_T)0, 0L);
1391 diff_need_scrollbind = FALSE;
1392 }
1393#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001394#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001395 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396 foldAdjustVisual();
1397#endif
1398#ifdef FEAT_FOLDING
1399 /*
1400 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1401 * contain the cursor.
1402 * When 'foldopen' is "all", open the fold(s) under the cursor.
1403 * This may mark the window for redrawing.
1404 */
1405 if (hasAnyFolding(curwin) && !char_avail())
1406 {
1407 foldCheckClose();
1408 if (fdo_flags & FDO_ALL)
1409 foldOpenCursor();
1410 }
1411#endif
1412
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001413 // Before redrawing, make sure w_topline is correct, and w_leftcol
1414 // if lines don't wrap, and w_skipcol if lines wrap.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001415 update_topline();
1416 validate_cursor();
1417
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001418 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001419 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001420 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001421 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001422 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001423 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001424 mch_enable_flush();
1425 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001426 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001427 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001428 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001429 if (need_maketitle)
1430 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001431#ifdef FEAT_VIMINFO
1432 curbuf->b_last_used = vim_time();
1433#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001434 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001435 if (keep_msg != NULL)
1436 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001437 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001438
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001439 if (p != NULL)
1440 {
1441 // msg_start() will set keep_msg to NULL, make a copy
1442 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1443 // check for duplicates. Never put this message in
1444 // history.
1445 msg_hist_off = TRUE;
1446 msg_attr((char *)p, keep_msg_attr);
1447 msg_hist_off = FALSE;
1448 vim_free(p);
1449 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001451 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001452 {
1453 fileinfo(FALSE, TRUE, FALSE);
1454 need_fileinfo = FALSE;
1455 }
1456
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001457 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001458 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001459 msg_didany = FALSE; // reset lines_left in msg_start()
1460 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001461 showruler(FALSE);
1462
1463 setcursor();
1464 cursor_on();
1465
1466 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001467
1468#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001469 // Now that we have drawn the first screen all the startup stuff
1470 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001471 if (time_fd != NULL)
1472 {
1473 TIME_MSG("first screen update");
1474 TIME_MSG("--- VIM STARTED ---");
1475 fclose(time_fd);
1476 time_fd = NULL;
1477 }
1478#endif
Bram Moolenaar0a60f792022-11-19 21:18:11 +00001479 // After the first screen update may start triggering WinScrolled
1480 // autocmd events. Store all the scroll positions and sizes now.
1481 may_make_initial_scroll_size_snapshot();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001482 }
1483#ifdef FEAT_GUI
1484 if (need_mouse_correct)
1485 gui_mouse_correct();
1486#endif
1487
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001488 // May request the keyboard protocol state now.
1489 may_send_t_RK();
1490
1491 // Update w_curswant if w_set_curswant has been set.
1492 // Postponed until here to avoid computing w_virtcol too often.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001493 update_curswant();
1494
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001495#ifdef FEAT_EVAL
1496 /*
1497 * May perform garbage collection when waiting for a character, but
1498 * only at the very toplevel. Otherwise we may be using a List or
1499 * Dict internally somewhere.
1500 * "may_garbage_collect" is reset in vgetc() which is invoked through
1501 * do_exmode() and normal_cmd().
1502 */
1503 may_garbage_collect = (!cmdwin && !noexmode);
1504#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001505 /*
1506 * If we're invoked as ex, do a round of ex commands.
1507 * Otherwise, get and execute a normal mode command.
1508 */
1509 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001510 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001511 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001512 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001513 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001514 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001515 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001516 {
1517#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001518 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001519 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001520 && !VIsual_active
1521 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001522 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001523 // If terminal_loop() returns OK we got a key that is handled
1524 // in Normal model. With FAIL we first need to position the
1525 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001526 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001527 normal_cmd(&oa, TRUE);
1528 }
1529 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001530#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001531 {
1532#ifdef FEAT_TERMINAL
1533 skip_term_loop = FALSE;
1534#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001535 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001536 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001537 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001538 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001539
1540theend:
1541 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001542}
1543
1544
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001545#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001546/*
1547 * Exit, but leave behind swap files for modified buffers.
1548 */
1549 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001550getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001551{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001552# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001553 // Ignore SIGHUP, because a dropped connection causes a read error, which
1554 // makes Vim exit and then handling SIGHUP causes various reentrance
1555 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001556 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001557# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001558
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001559 ml_close_notmod(); // close all not-modified buffers
1560 ml_sync_all(FALSE, FALSE); // preserve all swap files
1561 ml_close_all(FALSE); // close all memfiles, without deleting
1562 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001563}
1564#endif
1565
1566
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001567/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001568 * Exit properly. This is the only way to exit Vim after startup has
1569 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001570 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001571 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001572getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001573{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001574 exiting = TRUE;
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001575#if defined(FEAT_EVAL)
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001576 ch_log(NULL, "Exiting...");
1577#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001578
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001579 // When running in Ex mode an error causes us to exit with a non-zero exit
1580 // code. POSIX requires this, although it's not 100% clear from the
1581 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001582 if (exmode_active)
1583 exitval += ex_exitval;
1584
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001585#ifdef FEAT_EVAL
1586 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1587 set_vim_var_nr(VV_EXITING, exitval);
1588#endif
1589
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001590 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001591 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001592 windgoto((int)Rows - 1, 0);
1593
Bram Moolenaar58779852022-09-06 18:31:14 +01001594#ifdef FEAT_EVAL
1595 // Invoked all deferred functions in the function stack.
1596 invoke_all_defer();
1597#endif
1598
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001599#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001600 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001601 hash_debug_results();
1602#endif
1603
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001604#ifdef FEAT_GUI
1605 msg_didany = FALSE;
1606#endif
1607
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001608 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001609 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001610 tabpage_T *tp;
1611 tabpage_T *next_tp;
1612 buf_T *buf;
1613 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001614 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001615
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001616 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001617 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001618 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001619 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001620 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001621 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001622 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001623 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001624 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001625 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001626 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001627 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001628 bufref_T bufref;
1629
1630 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001631 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1632 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001633 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001634 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001635
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001636 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001637 next_tp = first_tabpage;
1638 break;
1639 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001640 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001641 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001642
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001643 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001644 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001645 if (buf->b_ml.ml_mfp != NULL)
1646 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001647 bufref_T bufref;
1648
1649 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001650 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001651 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001652 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001653 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001654 break;
1655 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001656
1657 // deathtrap() blocks autocommands, but we do want to trigger
1658 // VimLeavePre.
1659 if (is_autocmd_blocked())
1660 {
1661 unblock_autocmds();
1662 ++unblock;
1663 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001664 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001665 if (unblock)
1666 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001667 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001668
1669#ifdef FEAT_VIMINFO
1670 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001671 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001672 write_viminfo(NULL, FALSE);
1673#endif
1674
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001675 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001676 {
1677 int unblock = 0;
1678
1679 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1680 if (is_autocmd_blocked())
1681 {
1682 unblock_autocmds();
1683 ++unblock;
1684 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001685 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001686 if (unblock)
1687 block_autocmds();
1688 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001689
Bram Moolenaar05159a02005-02-26 23:04:13 +00001690#ifdef FEAT_PROFILE
1691 profile_dump();
1692#endif
1693
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001694 if (did_emsg
1695#ifdef FEAT_GUI
1696 || (gui.in_use && msg_didany && p_verbose > 0)
1697#endif
1698 )
1699 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001700 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001701 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001702 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001703 }
1704
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001705 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001706 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001707 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001708
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001709#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001710 job_stop_on_exit();
1711#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001712#ifdef FEAT_LUA
1713 lua_end();
1714#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001715#ifdef FEAT_MZSCHEME
1716 mzscheme_end();
1717#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001718#ifdef FEAT_TCL
1719 tcl_end();
1720#endif
1721#ifdef FEAT_RUBY
1722 ruby_end();
1723#endif
1724#ifdef FEAT_PYTHON
1725 python_end();
1726#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001727#ifdef FEAT_PYTHON3
1728 python3_end();
1729#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001730#ifdef FEAT_PERL
1731 perl_end();
1732#endif
1733#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1734 iconv_end();
1735#endif
1736#ifdef FEAT_NETBEANS_INTG
1737 netbeans_end();
1738#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001739#ifdef FEAT_CSCOPE
1740 cs_end();
1741#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001742#ifdef FEAT_EVAL
1743 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001744 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001745#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001746#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001747 free_cmd_argsW();
1748#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001749
1750 mch_exit(exitval);
1751}
1752
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001753/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001754 * Get the name of the display, before gui_prepare() removes it from
1755 * argv[]. Used for the xterm-clipboard display.
1756 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001757 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001759 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001760early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001761{
Bram Moolenaar03005972008-11-20 13:12:36 +00001762#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1763 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001764 int argc = parmp->argc;
1765 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001766 int i;
1767
1768 for (i = 1; i < argc; i++)
1769 {
1770 if (STRCMP(argv[i], "--") == 0)
1771 break;
1772# ifdef FEAT_XCLIPBOARD
1773 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001774# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001775 || STRICMP(argv[i], "--display") == 0
1776# endif
1777 )
1778 {
1779 if (i == argc - 1)
1780 mainerr_arg_missing((char_u *)argv[i]);
1781 xterm_display = argv[++i];
1782 }
1783# endif
1784# ifdef FEAT_CLIENTSERVER
1785 else if (STRICMP(argv[i], "--servername") == 0)
1786 {
1787 if (i == argc - 1)
1788 mainerr_arg_missing((char_u *)argv[i]);
1789 parmp->serverName_arg = (char_u *)argv[++i];
1790 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001791 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001792 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001793 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001794 {
1795 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001796# ifdef FEAT_GUI
1797 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001798 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001799 gui.dofork = FALSE;
1800# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001801 }
1802# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001803
Bram Moolenaar4f974752019-02-17 17:44:42 +01001804# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1805# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001806 else if (STRICMP(argv[i], "--windowid") == 0)
1807# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001808 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001809# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001810 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001811 long_u id;
1812 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001813
1814 if (i == argc - 1)
1815 mainerr_arg_missing((char_u *)argv[i]);
1816 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001817 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001818 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001819 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001820 if (count != 1)
1821 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1822 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001823# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001824 win_socket_id = id;
1825# else
1826 gtk_socket_id = id;
1827# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001828 i++;
1829 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001830# endif
1831# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001832 else if (STRICMP(argv[i], "--echo-wid") == 0)
1833 echo_wid_arg = TRUE;
1834# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001835# ifndef FEAT_NETBEANS_INTG
1836 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001837 {
1838 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1839 mch_exit(2);
1840 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001841# endif
1842
Bram Moolenaar58d98232005-07-23 22:25:46 +00001843 }
1844#endif
1845}
1846
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001847#ifndef NO_VIM_MAIN
1848/*
1849 * Get a (optional) count for a Vim argument.
1850 */
1851 static int
1852get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001853 char_u *p, // pointer to argument
1854 int *idx, // index in argument, is incremented
1855 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001856{
1857 if (vim_isdigit(p[*idx]))
1858 {
1859 def = atoi((char *)&(p[*idx]));
1860 while (vim_isdigit(p[*idx]))
1861 *idx = *idx + 1;
1862 }
1863 return def;
1864}
1865
1866/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001867 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001868 * If the executable name starts with "r" we disable shell commands.
1869 * If the next character is "e" we run in Easy mode.
1870 * If the next character is "g" we run the GUI version.
1871 * If the next characters are "view" we start in readonly mode.
1872 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1873 * If the next characters are "ex" we start in Ex mode. If it's followed
1874 * by "im" use improved Ex mode.
1875 */
1876 static void
1877parse_command_name(mparm_T *parmp)
1878{
1879 char_u *initstr;
1880
1881 initstr = gettail((char_u *)parmp->argv[0]);
1882
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001883#ifdef FEAT_EVAL
1884 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001885 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001886#endif
1887
1888 if (TOLOWER_ASC(initstr[0]) == 'r')
1889 {
1890 restricted = TRUE;
1891 ++initstr;
1892 }
1893
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001894 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001895 if (TOLOWER_ASC(initstr[0]) == 'e'
1896 && (TOLOWER_ASC(initstr[1]) == 'v'
1897 || TOLOWER_ASC(initstr[1]) == 'g'))
1898 {
1899#ifdef FEAT_GUI
1900 gui.starting = TRUE;
1901#endif
1902 parmp->evim_mode = TRUE;
1903 ++initstr;
1904 }
1905
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001906 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001907 if (TOLOWER_ASC(initstr[0]) == 'g')
1908 {
1909 main_start_gui();
1910#ifdef FEAT_GUI
1911 ++initstr;
1912#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001913#ifdef GUI_MAY_SPAWN
1914 gui.dospawn = FALSE; // No need to spawn a new process.
1915#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001916 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001917#ifdef GUI_MAY_SPAWN
1918 else
1919 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1920#endif
1921
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001922
1923 if (STRNICMP(initstr, "view", 4) == 0)
1924 {
1925 readonlymode = TRUE;
1926 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001927 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001928 initstr += 4;
1929 }
1930 else if (STRNICMP(initstr, "vim", 3) == 0)
1931 initstr += 3;
1932
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001933 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001934 if (STRICMP(initstr, "diff") == 0)
1935 {
1936#ifdef FEAT_DIFF
1937 parmp->diff_mode = TRUE;
1938#else
1939 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1940 mch_errmsg("\n");
1941 mch_exit(2);
1942#endif
1943 }
1944
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001945 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001946 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001947 if (STRNICMP(initstr, "ex", 2) == 0)
1948 {
1949 if (STRNICMP(initstr + 2, "im", 2) == 0)
1950 exmode_active = EXMODE_VIM;
1951 else
1952 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001953 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001954 }
1955}
1956
Bram Moolenaar58d98232005-07-23 22:25:46 +00001957/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001958 * Scan the command line arguments.
1959 */
1960 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001961command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001962{
1963 int argc = parmp->argc;
1964 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001965 int argv_idx; // index in argv[n][]
1966 int had_minmin = FALSE; // found "--" argument
1967 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001968 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001969 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001970 long n;
1971
1972 --argc;
1973 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001974 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001975 while (argc > 0)
1976 {
1977 /*
1978 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1979 */
1980 if (argv[0][0] == '+' && !had_minmin)
1981 {
1982 if (parmp->n_commands >= MAX_ARG_CMDS)
1983 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001984 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001985 if (argv[0][1] == NUL)
1986 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1987 else
1988 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1989 }
1990
1991 /*
1992 * Optional argument.
1993 */
1994 else if (argv[0][0] == '-' && !had_minmin)
1995 {
1996 want_argument = FALSE;
1997 c = argv[0][argv_idx++];
1998#ifdef VMS
1999 /*
2000 * VMS only uses upper case command lines. Interpret "-X" as "-x"
2001 * and "-/X" as "-X".
2002 */
2003 if (c == '/')
2004 {
2005 c = argv[0][argv_idx++];
2006 c = TOUPPER_ASC(c);
2007 }
2008 else
2009 c = TOLOWER_ASC(c);
2010#endif
2011 switch (c)
2012 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002013 case NUL: // "vim -" read from stdin
2014 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002015 if (exmode_active)
2016 silent_mode = TRUE;
2017 else
2018 {
2019 if (parmp->edit_type != EDIT_NONE)
2020 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2021 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002022 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002023 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002024 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002025 break;
2026
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002027 case '-': // "--" don't take any more option arguments
2028 // "--help" give help message
2029 // "--version" give version message
2030 // "--clean" clean context
2031 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002032 // "--startuptime fname" write timing info
2033 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002034 // "--nofork" don't fork
2035 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002036 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002037 // "--ttyfail" exit if not a term
2038 // "--noplugin[s]" skip plugins
2039 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002040 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2041 usage();
2042 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2043 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002044 Columns = 80; // need to init Columns
2045 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002046#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002047 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002048#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002049 list_version();
2050 msg_putchar('\n');
2051 msg_didout = FALSE;
2052 mch_exit(0);
2053 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002054 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2055 {
2056 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002057#ifdef FEAT_GUI
2058 use_gvimrc = (char_u *)"NONE";
2059#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002060 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002061 set_option_value_give_err((char_u *)"vif",
2062 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002063 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002064 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2065 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002066#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002067 parmp->literal = TRUE;
2068#endif
2069 }
2070 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2071 {
2072#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002073 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002074#endif
2075 }
2076 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2077 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002078 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2079 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002080 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2081 == 0)
2082 {
2083 want_argument = TRUE;
2084 argv_idx += 15;
2085 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002086 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2087 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002088 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2089 {
2090 want_argument = TRUE;
2091 argv_idx += 3;
2092 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002093 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2094 {
2095 want_argument = TRUE;
2096 argv_idx += 11;
2097 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002098 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2099 {
2100 want_argument = TRUE;
2101 argv_idx += 3;
2102 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002103#ifdef FEAT_CLIENTSERVER
2104 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002105 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002106 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2107 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2108 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002109 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002110 if (argc > 1)
2111 {
2112 --argc;
2113 ++argv;
2114 }
2115 }
2116#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002117#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002118# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002119 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002120# else
2121 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2122# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002123 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002124 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002125 if (argc > 1)
2126 {
2127 --argc;
2128 ++argv;
2129 }
2130 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002131#endif
2132#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002133 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2134 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002135 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002136 }
2137#endif
2138 else
2139 {
2140 if (argv[0][argv_idx])
2141 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2142 had_minmin = TRUE;
2143 }
2144 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002145 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002146 break;
2147
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002148 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002149#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002150 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002151#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002152 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002153 mch_exit(2);
2154#endif
2155 break;
2156
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002157 case 'b': // "-b" binary mode
2158 // Needs to be effective before expanding file names, because
2159 // for Win32 this makes us edit a shortcut file itself,
2160 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002161 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002162 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002163 break;
2164
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002165 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002166 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002167 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002168 break;
2169
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002170 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002171 exmode_active = EXMODE_NORMAL;
2172 break;
2173
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002174 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002175 exmode_active = EXMODE_VIM;
2176 break;
2177
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002178 case 'f': // "-f" GUI: run in foreground. Amiga: open
2179 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002180#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002181 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002182#endif
2183 break;
2184
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002185 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002186 main_start_gui();
2187 break;
2188
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002189 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002190 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002191 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002192 break;
2193
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002194 case '?': // "-?" give help message (for MS-Windows)
2195 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002196#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002197 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002198 gui.starting = FALSE;
2199#endif
2200 usage();
2201 break;
2202
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002203 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002204#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002205 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002206 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002207#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002208 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002209 mch_exit(2);
2210#endif
2211 break;
2212
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002213 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002214 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002215 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002216 break;
2217
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002218 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002219 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002220 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002221
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002222 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002223 p_write = FALSE;
2224 break;
2225
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002226 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002227#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002228 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002229#endif
2230 parmp->evim_mode = TRUE;
2231 break;
2232
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002233 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002234 change_compatible(FALSE);
2235 break;
2236
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002237 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002238#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002239 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002240 if (argv[0][argv_idx] == 'b')
2241 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002242 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002243 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002244 }
2245 else
2246#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002247 parmp->no_swap_file = TRUE;
2248 break;
2249
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002250 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002251#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002252 // For some reason on MacOS X, an argument like:
2253 // -psn_0_10223617 is passed in when invoke from Finder
2254 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002255 if (argv[0][argv_idx] == 's')
2256 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002257 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002258 main_start_gui();
2259 break;
2260 }
2261#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002262 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002263 parmp->window_count = get_number_arg((char_u *)argv[0],
2264 &argv_idx, 0);
2265 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002266 break;
2267
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002268 case 'o': // "-o[N]" open N horizontal split windows
2269 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002270 parmp->window_count = get_number_arg((char_u *)argv[0],
2271 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002272 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002273 break;
2274
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002275 case 'O': // "-O[N]" open N vertical split windows
2276 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002277 parmp->window_count = get_number_arg((char_u *)argv[0],
2278 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002279 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002280 break;
2281
2282#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002283 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002284 if (parmp->edit_type != EDIT_NONE)
2285 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2286 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002287 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002288 {
2289 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2290 argv_idx = -1;
2291 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002292 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002293 want_argument = TRUE;
2294 break;
2295#endif
2296
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002297 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002298 readonlymode = TRUE;
2299 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002300 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002301 break;
2302
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002303 case 'r': // "-r" recovery mode
2304 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002305 recoverymode = 1;
2306 break;
2307
2308 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002309 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002310 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002311 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002312 want_argument = TRUE;
2313 break;
2314
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002315 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002316 if (parmp->edit_type != EDIT_NONE)
2317 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2318 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002319 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002320 {
2321 parmp->tagname = (char_u *)argv[0] + argv_idx;
2322 argv_idx = -1;
2323 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002324 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002325 want_argument = TRUE;
2326 break;
2327
2328#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002329 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002330 parmp->use_debug_break_level = 9999;
2331 break;
2332#endif
2333#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002334 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002335# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002336 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002337 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2338 want_argument = TRUE;
2339 else
2340# endif
2341 parmp->diff_mode = TRUE;
2342 break;
2343#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002344 case 'V': // "-V{N}" Verbose level
2345 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002346 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2347 if (argv[0][argv_idx] != NUL)
2348 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002349 set_option_value_give_err((char_u *)"verbosefile",
2350 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002351 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002352 }
2353 break;
2354
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002355 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002356 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002357#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002358 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002359#endif
2360 break;
2361
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002362 case 'w': // "-w{number}" set window height
2363 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002364 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2365 {
2366 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002367 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002368 break;
2369 }
2370 want_argument = TRUE;
2371 break;
2372
2373#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002374 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002375 parmp->ask_for_key = TRUE;
2376 break;
2377#endif
2378
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002379 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002380#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2381 x_no_connect = TRUE;
2382#endif
2383 break;
2384
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002385 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002386 restricted = TRUE;
2387 break;
2388
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002389 case 'c': // "-c{command}" or "-c {command}" execute
2390 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002391 if (argv[0][argv_idx] != NUL)
2392 {
2393 if (parmp->n_commands >= MAX_ARG_CMDS)
2394 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002395 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2396 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002397 argv_idx = -1;
2398 break;
2399 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002400 // FALLTHROUGH
2401 case 'S': // "-S {file}" execute Vim script
2402 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002403#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002404 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002405#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002406 case 'T': // "-T {terminal}" terminal name
2407 case 'u': // "-u {vimrc}" vim inits file
2408 case 'U': // "-U {gvimrc}" gvim inits file
2409 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002410#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002411 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002412#endif
2413 want_argument = TRUE;
2414 break;
2415
2416 default:
2417 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2418 }
2419
2420 /*
2421 * Handle option arguments with argument.
2422 */
2423 if (want_argument)
2424 {
2425 /*
2426 * Check for garbage immediately after the option letter.
2427 */
2428 if (argv[0][argv_idx] != NUL)
2429 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2430
2431 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002432 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002433 mainerr_arg_missing((char_u *)argv[0]);
2434 ++argv;
2435 argv_idx = -1;
2436
2437 switch (c)
2438 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002439 case 'c': // "-c {command}" execute command
2440 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002441 if (parmp->n_commands >= MAX_ARG_CMDS)
2442 mainerr(ME_EXTRA_CMD, NULL);
2443 if (c == 'S')
2444 {
2445 char *a;
2446
2447 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002448 // "-S" without argument: use default session file
2449 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002450 a = SESSION_FILE;
2451 else if (argv[0][0] == '-')
2452 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002453 // "-S" followed by another option: use default
2454 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002455 a = SESSION_FILE;
2456 ++argc;
2457 --argv;
2458 }
2459 else
2460 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002461 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462 if (p == NULL)
2463 mch_exit(2);
2464 sprintf((char *)p, "so %s", a);
2465 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2466 parmp->commands[parmp->n_commands++] = p;
2467 }
2468 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002469 parmp->commands[parmp->n_commands++] =
2470 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002471 break;
2472
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002473 case '-':
2474 if (argv[-1][2] == 'c')
2475 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002476 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002477 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2478 mainerr(ME_EXTRA_CMD, NULL);
2479 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002480 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002481 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002482 // --gui-dialog-file fname
2483 if (argv[-1][2] == 'g')
2484 {
2485 // without GUI ignore the argument
2486#ifdef FEAT_GUI
2487 parmp->gui_dialog_file = (char_u *)argv[0];
2488#endif
2489 }
2490
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002491 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002492 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002493 break;
2494
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002495 // case 'd': -d {device} is handled in mch_check_win() for the
2496 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002497
2498#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002499 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002500 parmp->use_ef = (char_u *)argv[0];
2501 break;
2502#endif
2503
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002504 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002505 set_option_value_give_err((char_u *)"vif",
2506 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002507 break;
2508
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002509 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002510 if (scriptin[0] != NULL)
2511 {
2512scripterror:
2513 mch_errmsg(_("Attempt to open script file again: \""));
2514 mch_errmsg(argv[-1]);
2515 mch_errmsg(" ");
2516 mch_errmsg(argv[0]);
2517 mch_errmsg("\"\n");
2518 mch_exit(2);
2519 }
2520 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2521 {
2522 mch_errmsg(_("Cannot open for reading: \""));
2523 mch_errmsg(argv[0]);
2524 mch_errmsg("\"\n");
2525 mch_exit(2);
2526 }
2527 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002528 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002529 break;
2530
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002531 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002532 parmp->tagname = (char_u *)argv[0];
2533 break;
2534
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002535 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002536 /*
2537 * The -T term argument is always available and when
2538 * HAVE_TERMLIB is supported it overrides the environment
2539 * variable TERM.
2540 */
2541#ifdef FEAT_GUI
2542 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002543 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002544 else
2545#endif
2546 parmp->term = (char_u *)argv[0];
2547 break;
2548
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002549 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002550 parmp->use_vimrc = (char_u *)argv[0];
2551 break;
2552
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002553 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002554#ifdef FEAT_GUI
2555 use_gvimrc = (char_u *)argv[0];
2556#endif
2557 break;
2558
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002559 case 'w': // "-w {nr}" 'window' value
2560 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002561 if (vim_isdigit(*((char_u *)argv[0])))
2562 {
2563 argv_idx = 0;
2564 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002565 set_option_value_give_err((char_u *)"window",
2566 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567 argv_idx = -1;
2568 break;
2569 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002570 // FALLTHROUGH
2571 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002572 if (scriptout != NULL)
2573 goto scripterror;
2574 if ((scriptout = mch_fopen(argv[0],
2575 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2576 {
2577 mch_errmsg(_("Cannot open for script output: \""));
2578 mch_errmsg(argv[0]);
2579 mch_errmsg("\"\n");
2580 mch_exit(2);
2581 }
2582 break;
2583
Bram Moolenaar4f974752019-02-17 17:44:42 +01002584#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002585 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002586 gui_mch_set_parent(argv[0]);
2587 break;
2588#endif
2589 }
2590 }
2591 }
2592
2593 /*
2594 * File name argument.
2595 */
2596 else
2597 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002598 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002600 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002601 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2602 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2603 parmp->edit_type = EDIT_FILE;
2604
2605#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002606 // Remember if the argument was a full path before changing
2607 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002608 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2609 parmp->full_path = TRUE;
2610#endif
2611
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002612 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002613 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2614 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2615 mch_exit(2);
2616#ifdef FEAT_DIFF
2617 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2618 && !mch_isdir(alist_name(&GARGLIST[0])))
2619 {
2620 char_u *r;
2621
2622 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2623 if (r != NULL)
2624 {
2625 vim_free(p);
2626 p = r;
2627 }
2628 }
2629#endif
K.Takatab247e062022-02-07 10:45:23 +00002630#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631 /*
2632 * If vim is invoked by non-Cygwin tools, convert away any
2633 * DOS paths, so things like .swp files are created correctly.
2634 * Look for evidence of non-Cygwin paths before we bother.
2635 * This is only for when using the Unix files.
2636 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002637 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002638 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002639 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002640
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002641# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002642 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002643# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002644 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002645# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002646 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002647 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002648 if (p == NULL)
2649 mch_exit(2);
2650 }
2651#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002652
2653#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002654 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002655 fname_case(p, 0);
2656#endif
2657
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002658 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002659#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002660 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002661#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002662 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002663#endif
2664 );
2665
Bram Moolenaar4f974752019-02-17 17:44:42 +01002666#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002668 // Remember this argument has been added to the argument list.
2669 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002670 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002671# ifdef FEAT_DIFF
2672 parmp->diff_mode
2673# else
2674 FALSE
2675# endif
2676 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 }
2678#endif
2679 }
2680
2681 /*
2682 * If there are no more letters after the current "-", go to next
2683 * argument. argv_idx is set to -1 when the current argument is to be
2684 * skipped.
2685 */
2686 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2687 {
2688 --argc;
2689 ++argv;
2690 argv_idx = 1;
2691 }
2692 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002693
2694#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002695 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2696 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002697 if (parmp->n_commands > 0)
2698 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002699 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002700 if (p != NULL)
2701 {
2702 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2703 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2704 vim_free(p);
2705 }
2706 }
2707#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708}
2709
2710/*
2711 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002712 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002713 */
2714 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002715check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002716{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002717 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718
2719 input_isatty = mch_input_isatty();
2720 if (exmode_active)
2721 {
2722 if (!input_isatty)
2723 silent_mode = TRUE;
2724 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002725 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002727 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728 && !gui.starting
2729#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002730 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002731 {
2732#ifdef NBDEBUG
2733 /*
2734 * This shouldn't be necessary. But if I run netbeans with the log
2735 * output coming to the console and XOpenDisplay fails, I get vim
2736 * trying to start with input/output to my console tty. This fills my
2737 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002738 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002740 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002741 {
2742 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2743 exit(1);
2744 }
2745#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002746#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2747 if (
2748# ifdef VIMDLL
2749 !gui.starting &&
2750# endif
2751 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002752 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002753# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002754 && defined(FEAT_GETTEXT)
2755 char *s, *tofree = NULL;
2756
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002757 // Set the encoding of the error message based on $LC_ALL or
2758 // other environment variables instead of 'encoding'.
2759 // Note that the message is shown on a Cygwin terminal (e.g.
2760 // mintty) which encoding is based on $LC_ALL or etc., not the
2761 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002762 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002763 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002764 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002765 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2766 vim_free(tofree);
2767# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002768 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2769 exit(1);
2770 }
2771#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002772 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2774 if (!input_isatty)
2775 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2776 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002777 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2778 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002780 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781 TIME_MSG("Warning delay");
2782 }
2783}
2784
2785/*
2786 * Read text from stdin.
2787 */
2788 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002789read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002790{
2791 int i;
2792
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002793 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002795
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796 no_wait_return = TRUE;
2797 i = msg_didany;
2798 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002799
2800 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002801 (void)open_buffer(TRUE, NULL, 0);
2802
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002803 no_wait_return = FALSE;
2804 msg_didany = i;
2805 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002806
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002807 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002808
2809#if !(defined(AMIGA) || defined(MACOS_X))
2810 // Dup stdin from stderr to read commands from, so that shell commands
2811 // work.
2812 // TODO: why is this needed, even though readfile() has done this?
2813 close(0);
2814 vim_ignored = dup(2);
2815#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816}
2817
2818/*
2819 * Create the requested number of windows and edit buffers in them.
2820 * Also does recovery if "recoverymode" set.
2821 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002822 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002823create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002824{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002825 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002826 int done = 0;
2827
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002828 /*
2829 * Create the number of windows that was requested.
2830 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002831 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002832 parmp->window_count = 1;
2833 if (parmp->window_count == 0)
2834 parmp->window_count = GARGCOUNT;
2835 if (parmp->window_count > 1)
2836 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002837 // Don't change the windows if there was a command in .vimrc that
2838 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002839 if (parmp->window_layout == 0)
2840 parmp->window_layout = WIN_HOR;
2841 if (parmp->window_layout == WIN_TABS)
2842 {
2843 parmp->window_count = make_tabpages(parmp->window_count);
2844 TIME_MSG("making tab pages");
2845 }
2846 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002847 {
2848 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002849 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850 TIME_MSG("making windows");
2851 }
2852 else
2853 parmp->window_count = win_count();
2854 }
2855 else
2856 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002857
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002858 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002859 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002860 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002861 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002862 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002863 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002864 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002865 }
2866 else
2867 {
2868 /*
2869 * Open a buffer for windows that don't have one yet.
2870 * Commands in the .vimrc might have loaded a file or split the window.
2871 * Watch out for autocommands that delete a window.
2872 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002873 /*
2874 * Don't execute Win/Buf Enter/Leave autocommands here
2875 */
2876 ++autocmd_no_enter;
2877 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002878 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002879 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002880 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002881 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002882 {
2883 if (parmp->window_layout == WIN_TABS)
2884 goto_tabpage(1);
2885 else
2886 curwin = firstwin;
2887 }
2888 else if (parmp->window_layout == WIN_TABS)
2889 {
2890 if (curtab->tp_next == NULL)
2891 break;
2892 goto_tabpage(0);
2893 }
2894 else
2895 {
2896 if (curwin->w_next == NULL)
2897 break;
2898 curwin = curwin->w_next;
2899 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002900 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 curbuf = curwin->w_buffer;
2902 if (curbuf->b_ml.ml_mfp == NULL)
2903 {
2904#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002905 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002906 if (p_fdls >= 0)
2907 curwin->w_p_fdl = p_fdls;
2908#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002909 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002910 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002911
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002912 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002913
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002914 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002915 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002916
Bram Moolenaar84212822006-11-07 21:59:47 +00002917 if (swap_exists_action == SEA_QUIT)
2918 {
2919 if (got_int || only_one_window())
2920 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002921 // abort selected or quit and only one window
2922 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002923 getout(1);
2924 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002925 // We can't close the window, it would disturb what
2926 // happens next. Clear the file name and set the arg
2927 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002928 setfname(curbuf, NULL, NULL, FALSE);
2929 curwin->w_arg_idx = -1;
2930 swap_exists_action = SEA_NONE;
2931 }
2932 else
2933 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002934 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002935 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002936 ui_breakcheck();
2937 if (got_int)
2938 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002939 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002940 break;
2941 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002942 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002943 if (parmp->window_layout == WIN_TABS)
2944 goto_tabpage(1);
2945 else
2946 curwin = firstwin;
2947 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002948 --autocmd_no_enter;
2949 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002950 }
2951}
2952
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002953/*
2954 * If opened more than one window, start editing files in the other
2955 * windows. make_windows() has already opened the windows.
2956 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002957 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002958edit_buffers(
2959 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002960 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002961{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002962 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002963 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002964 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002965 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002966 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002967
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002968 /*
2969 * Don't execute Win/Buf Enter/Leave autocommands here
2970 */
2971 ++autocmd_no_enter;
2972 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002973
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002974 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002975 if (curwin->w_arg_idx == -1)
2976 {
2977 win_close(curwin, TRUE);
2978 advance = FALSE;
2979 }
2980
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002981 arg_idx = 1;
2982 for (i = 1; i < parmp->window_count; ++i)
2983 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002984 if (cwd != NULL)
2985 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002986 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002987 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002988 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002989 ++arg_idx;
2990 win_close(curwin, TRUE);
2991 advance = FALSE;
2992 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002993 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002994
Bram Moolenaar84212822006-11-07 21:59:47 +00002995 if (advance)
2996 {
2997 if (parmp->window_layout == WIN_TABS)
2998 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002999 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003000 break;
3001 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003002 // Temporarily reset 'shm' option to not print fileinfo when
3003 // loading the other buffers. This would overwrite the already
3004 // existing fileinfo for the first tab.
3005 if (i == 1)
3006 {
3007 char buf[100];
3008
3009 p_shm_save = vim_strsave(p_shm);
3010 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003011 set_option_value_give_err((char_u *)"shm",
3012 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003013 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003014 }
3015 else
3016 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003017 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003018 break;
3019 win_enter(curwin->w_next, FALSE);
3020 }
3021 }
3022 advance = TRUE;
3023
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003024 // Only open the file if there is no file in this window yet (that can
3025 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003026 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3027 {
3028 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003029 // Edit file from arg list, if there is one. When "Quit" selected
3030 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003031 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003032 (void)do_ecmd(0, arg_idx < GARGCOUNT
3033 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003034 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003035 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003036 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003037 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003038 if (got_int || only_one_window())
3039 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003040 // abort selected and only one window
3041 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003042 getout(1);
3043 }
3044 win_close(curwin, TRUE);
3045 advance = FALSE;
3046 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003047 if (arg_idx == GARGCOUNT - 1)
3048 arg_had_last = TRUE;
3049 ++arg_idx;
3050 }
3051 ui_breakcheck();
3052 if (got_int)
3053 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003054 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003055 break;
3056 }
3057 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003058
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003059 if (p_shm_save != NULL)
3060 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003061 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003062 vim_free(p_shm_save);
3063 }
3064
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003065 if (parmp->window_layout == WIN_TABS)
3066 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003067 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003068
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003069 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003070 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003071#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003072 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003073 while (win->w_p_pvw)
3074 {
3075 win = win->w_next;
3076 if (win == NULL)
3077 {
3078 win = firstwin;
3079 break;
3080 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003081 }
3082#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003083 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003084
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003085 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003086 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003087 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003088 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003089}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003090
3091/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003092 * Execute the commands from --cmd arguments "cmds[cnt]".
3093 */
3094 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003095exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003096{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003097 char_u **cmds = parmp->pre_commands;
3098 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003099 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003100 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003101
3102 if (cnt > 0)
3103 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003104 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003105 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003106 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003107# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003108 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003109# endif
3110 for (i = 0; i < cnt; ++i)
3111 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003112 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003113 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003114# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003115 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003116# endif
3117 TIME_MSG("--cmd commands");
3118 }
3119}
3120
3121/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003122 * Execute "+", "-c" and "-S" arguments.
3123 */
3124 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003125exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003126{
3127 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003128 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003129
3130 /*
3131 * We start commands on line 0, make "vim +/pat file" match a
3132 * pattern on line 1. But don't move the cursor when an autocommand
3133 * with g`" was used.
3134 */
3135 msg_scroll = TRUE;
3136 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3137 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003138 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003139 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003140#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003141 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003142 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003143#endif
3144 for (i = 0; i < parmp->n_commands; ++i)
3145 {
3146 do_cmdline_cmd(parmp->commands[i]);
3147 if (parmp->cmds_tofree[i])
3148 vim_free(parmp->commands[i]);
3149 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003150 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003151 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003152#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003153 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003154#endif
3155 if (curwin->w_cursor.lnum == 0)
3156 curwin->w_cursor.lnum = 1;
3157
3158 if (!exmode_active)
3159 msg_scroll = FALSE;
3160
3161#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003162 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003163 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003164 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003165#endif
3166 TIME_MSG("executing command arguments");
3167}
3168
3169/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003170 * Source startup scripts.
3171 */
3172 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003173source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003174{
3175 int i;
3176
3177 /*
3178 * For "evim" source evim.vim first of all, so that the user can overrule
3179 * any things he doesn't like.
3180 */
3181 if (parmp->evim_mode)
3182 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003184 TIME_MSG("source evim file");
3185 }
3186
3187 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003188 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003189 * nothing else.
3190 */
3191 if (parmp->use_vimrc != NULL)
3192 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003193 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003194 {
3195 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3196 != OK)
3197 emsg(e_failed_to_source_defaults);
3198 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003199 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003200 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003201 {
3202#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003203 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003204 use_gvimrc = parmp->use_vimrc;
3205#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003206 }
3207 else
3208 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003210 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003211 }
3212 }
3213 else if (!silent_mode)
3214 {
3215#ifdef AMIGA
3216 struct Process *proc = (struct Process *)FindTask(0L);
3217 APTR save_winptr = proc->pr_WindowPtr;
3218
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003219 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003220 proc->pr_WindowPtr = (APTR)-1L;
3221#endif
3222
3223 /*
3224 * Get system wide defaults, if the file name is defined.
3225 */
3226#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003228#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003229#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3231 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003232#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003233
3234 /*
3235 * Try to read initialization commands from the following places:
3236 * - environment variable VIMINIT
3237 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3238 * - second user vimrc file ($VIM/.vimrc for Dos)
3239 * - environment variable EXINIT
3240 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3241 * - second user exrc file ($VIM/.exrc for Dos)
3242 * The first that exists is used, the rest is ignored.
3243 */
3244 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3245 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3247 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003248#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003249 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003251#endif
3252#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003253 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003255#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003256#ifdef USR_VIMRC_FILE4
3257 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003259#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003260 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3262 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003263#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003264 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3265 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003266#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003267 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003268 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003269 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003270 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3271 NULL) == FAIL)
3272 emsg(e_failed_to_source_defaults);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003273 }
3274 }
3275
3276 /*
3277 * Read initialization commands from ".vimrc" or ".exrc" in current
3278 * directory. This is only done if the 'exrc' option is set.
3279 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003280 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003281 * option has been reset in environment of global ".exrc" or ".vimrc".
3282 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3283 * SYS_VIMRC_FILE.
3284 */
3285 if (p_exrc)
3286 {
3287#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003288 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003289 if (!file_owned(VIMRC_FILE))
3290#endif
3291 secure = p_secure;
3292
3293 i = FAIL;
3294 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003295 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003296#ifdef USR_VIMRC_FILE2
3297 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003298 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003299#endif
3300#ifdef USR_VIMRC_FILE3
3301 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003302 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003303#endif
3304#ifdef SYS_VIMRC_FILE
3305 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003306 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003307#endif
3308 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003310
3311 if (i == FAIL)
3312 {
3313#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003314 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003315 if (!file_owned(EXRC_FILE))
3316 secure = p_secure;
3317 else
3318 secure = 0;
3319#endif
3320 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003321 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003322#ifdef USR_EXRC_FILE2
3323 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003324 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003325#endif
3326 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 (void)do_source((char_u *)EXRC_FILE, FALSE,
3328 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003329 }
3330 }
3331 if (secure == 2)
3332 need_wait_return = TRUE;
3333 secure = 0;
3334#ifdef AMIGA
3335 proc->pr_WindowPtr = save_winptr;
3336#endif
3337 }
3338 TIME_MSG("sourcing vimrc file(s)");
3339}
3340
3341/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003342 * Setup to start using the GUI. Exit with an error when not available.
3343 */
3344 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003345main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003346{
3347#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003348 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003350 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003351 mch_errmsg("\n");
3352 mch_exit(2);
3353#endif
3354}
3355
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003356#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003357
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003358/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003360 * Returns FAIL if the environment variable was not executed, OK otherwise.
3361 */
3362 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003363process_env(
3364 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003365 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003366{
3367 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003368 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003369
Bram Moolenaare31ee862020-01-07 20:59:34 +01003370 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003371
3372 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3373 {
3374 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003375 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003376 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003377 ESTACK_CHECK_SETUP
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003378 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003379 current_sctx.sc_version = 1;
3380#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003381 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003382 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003383 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003384#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003385
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003387
Bram Moolenaare31ee862020-01-07 20:59:34 +01003388 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003389 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003390 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391 return OK;
3392 }
3393 return FAIL;
3394}
3395
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003396#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397/*
3398 * Return TRUE if we are certain the user owns the file "fname".
3399 * Used for ".vimrc" and ".exrc".
3400 * Use both stat() and lstat() for extra security.
3401 */
3402 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003403file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003405 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406# ifdef UNIX
3407 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003408# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409 uid_t uid = ((getgid() << 16) | getuid());
3410# endif
3411
3412 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3413# ifdef HAVE_LSTAT
3414 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3415# endif
3416 );
3417}
3418#endif
3419
3420/*
3421 * Give an error message main_errors["n"] and exit.
3422 */
3423 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003424mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003425 int n, // one of the ME_ defines
3426 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003428#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003429 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430#endif
3431
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003432 // If this is a Windows GUI executable, show an error dialog box.
3433#ifdef VIMDLL
3434 gui.in_use = mch_is_gui_executable();
3435#endif
3436#ifdef FEAT_GUI_MSWIN
3437 gui.starting = FALSE; // Needed to show as error.
3438#endif
3439
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003440 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003441 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003442 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443 mch_errmsg(_(main_errors[n]));
3444 if (str != NULL)
3445 {
3446 mch_errmsg(": \"");
3447 mch_errmsg((char *)str);
3448 mch_errmsg("\"");
3449 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003450 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003451
3452 mch_exit(1);
3453}
3454
3455 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003456mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003457{
3458 mainerr(ME_ARG_MISSING, str);
3459}
3460
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003461#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003462/*
3463 * print a message with three spaces prepended and '\n' appended.
3464 */
3465 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003466main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003467{
3468 mch_msg(" ");
3469 mch_msg(s);
3470 mch_msg("\n");
3471}
3472
3473/*
3474 * Print messages for "vim -h" or "vim --help" and exit.
3475 */
3476 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003477usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003478{
3479 int i;
3480 static char *(use[]) =
3481 {
3482 N_("[file ..] edit specified file(s)"),
3483 N_("- read text from stdin"),
3484 N_("-t tag edit file where tag is defined"),
3485#ifdef FEAT_QUICKFIX
3486 N_("-q [errorfile] edit file with first error")
3487#endif
3488 };
3489
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003490#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003491 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003492#endif
3493
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003494 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003495 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003496 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003497 for (i = 0; ; ++i)
3498 {
3499 mch_msg(_(" vim [arguments] "));
3500 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003501 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003502 break;
3503 mch_msg(_("\n or:"));
3504 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003505#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003506 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003507#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003508
3509 mch_msg(_("\n\nArguments:\n"));
3510 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003511#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003512 main_msg(_("--literal\t\tDon't expand wildcards"));
3513#endif
3514#ifdef FEAT_OLE
3515 main_msg(_("-register\t\tRegister this gvim for OLE"));
3516 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3517#endif
3518#ifdef FEAT_GUI
3519 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3520 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3521#endif
3522 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3523 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003524 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003525 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3526#ifdef FEAT_DIFF
3527 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3528#endif
3529 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3530 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3531 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3532 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3533 main_msg(_("-M\t\t\tModifications in text not allowed"));
3534 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003536 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3537 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003538 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3539#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003540 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003541#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003542 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3543 main_msg(_("-r\t\t\tList swap files and exit"));
3544 main_msg(_("-r (with file name)\tRecover crashed session"));
3545 main_msg(_("-L\t\t\tSame as -r"));
3546#ifdef AMIGA
3547 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3548 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3549#endif
3550#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003551 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003552#endif
3553#ifdef FEAT_RIGHTLEFT
3554 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3555#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003556 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003557 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003558#ifdef FEAT_GUI
3559 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3560#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003561 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003562 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3563#ifdef FEAT_GUI
3564 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3565#endif
3566 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003567 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003568 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3569 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3570 main_msg(_("+\t\t\tStart at end of file"));
3571 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003572 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3574 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3575 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3576 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3577 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3578#ifdef FEAT_CRYPT
3579 main_msg(_("-x\t\t\tEdit encrypted files"));
3580#endif
3581#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3582# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003583 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003584# endif
3585 main_msg(_("-X\t\t\tDo not connect to X server"));
3586#endif
3587#ifdef FEAT_CLIENTSERVER
3588 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3589 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3590 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3591 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003592 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003593 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3594 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3595 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3596 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3597#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003598#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003599 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003600#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003601#ifdef FEAT_JOB_CHANNEL
K.Takata0fd7be72022-11-09 16:29:24 +00003602 main_msg(_("--log <file>\t\tStart logging to <file> early"));
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003603#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003604#ifdef FEAT_VIMINFO
3605 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3606#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003607 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003608 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3609 main_msg(_("--version\t\tPrint version information and exit"));
3610
3611#ifdef FEAT_GUI_X11
3612# ifdef FEAT_GUI_MOTIF
3613 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003614# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003615 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003616 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003617 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3618 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3619 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3620 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3621 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3622 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3623 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3624 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003625 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3626 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3627 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003628#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003629#ifdef FEAT_GUI_GTK
3630 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003631 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3632 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003633 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3634 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003635 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003636 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003637 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003638 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003639 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003640 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003641#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003642#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003643# ifdef VIMDLL
3644 if (gui.starting)
3645# endif
3646 {
3647 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3648 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3649 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003650#endif
3651
3652#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003653 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003654 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003655 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003656 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003657 gui.dofork = FALSE;
3658 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003659 else
3660#endif
3661 mch_exit(0);
3662}
3663
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003664/*
3665 * Check the result of the ATTENTION dialog:
3666 * When "Quit" selected, exit Vim.
3667 * When "Recover" selected, recover the file.
3668 */
3669 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003670check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003671{
3672 if (swap_exists_action == SEA_QUIT)
3673 getout(1);
3674 handle_swap_exists(NULL);
3675}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003676
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003677#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003678
Bram Moolenaar595297d2017-03-04 19:11:12 +01003679#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003680 static void
3681set_progpath(char_u *argv0)
3682{
3683 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003684
Bram Moolenaar4f974752019-02-17 17:44:42 +01003685# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003686 // A relative path containing a "/" will become invalid when using ":cd",
3687 // turn it into a full path.
3688 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3689 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003690 char_u *path = NULL;
3691
3692 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3693 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003694# else
3695 char_u buf[MAXPATHL + 1];
3696# ifdef PROC_EXE_LINK
3697 char linkbuf[MAXPATHL + 1];
3698 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003699
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003700 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3701 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003702 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003703 linkbuf[len] = NUL;
3704 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003705 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003706# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003707
3708 if (!mch_isFullName(val))
3709 {
3710 if (gettail(val) != val
3711 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3712 val = buf;
3713 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003714# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003715
Bram Moolenaar08cab962017-03-04 14:37:18 +01003716 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003717
Bram Moolenaar4f974752019-02-17 17:44:42 +01003718# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003719 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003720# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003721}
3722
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003723#endif // NO_VIM_MAIN