blob: 90816576918d27254b4b6c6abd887944c168d7d1 [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
Bram Moolenaarca003e12006-03-17 23:19:38 +0000161 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000162
Bram Moolenaar07268702018-03-01 21:57:32 +0100163#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100164 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100165 for (i = 1; i < argc; ++i)
166 if (STRICMP(argv[i], "--clean") == 0)
167 {
168 params.clean = TRUE;
169 break;
170 }
171#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200172#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200173 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200174 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200175 if ((STRICMP(argv[i] + 1, "register") == 0
176 || STRICMP(argv[i] + 1, "unregister") == 0)
177 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200178 {
179 found_register_arg = TRUE;
180 break;
181 }
182#endif
183
184 /*
185 * Various initialisations shared with tests.
186 */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200187 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000188
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200189#ifdef VIMDLL
190 // Check if the current executable file is for the GUI subsystem.
191 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200192#elif defined(FEAT_GUI_MSWIN)
193 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200194#endif
195
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000196#ifdef FEAT_CLIENTSERVER
197 /*
198 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000199 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000200 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000201 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000202#endif
203
204 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000205 * Figure out the way to work from the command name argv[0].
206 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000207 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000208 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000209
210 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000211 * Process the command line arguments. File names are put in the global
212 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000214 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215 TIME_MSG("parsing arguments");
216
217 /*
218 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000219 * there is no terminal version, and on Windows we can't fork one off with
220 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000221 */
222#ifdef ALWAYS_USE_GUI
223 gui.starting = TRUE;
224#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000225# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000226 /*
227 * Check if the GUI can be started. Reset gui.starting if not.
228 * Don't know about other systems, stay on the safe side and don't check.
229 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000230 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000232 if (gui_init_check() == FAIL)
233 {
234 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100236 // When running "evim" or "gvim -y" we need the menus, exit if we
237 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000238 if (params.evim_mode)
239 mch_exit(1);
240 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000241 }
242# endif
243#endif
244
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000245 if (GARGCOUNT > 0)
246 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100247#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000248 /*
249 * Expand wildcards in file names.
250 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000251 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000252 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200253 start_dir = alloc(MAXPATHL);
254 if (start_dir != NULL)
255 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100256 // Temporarily add '(' and ')' to 'isfname'. These are valid
257 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000258 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000260 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000261 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200262 if (start_dir != NULL)
263 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000264 }
265#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200266 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000267 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000268
Bram Moolenaar4f974752019-02-17 17:44:42 +0100269#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000270 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100271 // Remember the number of entries in the argument list. If it changes
272 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000273 set_alist_count();
274 }
275#endif
276
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000278 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000279 {
280 /*
281 * If there is one filename, fully qualified, we have very probably
282 * been invoked from explorer, so change to the file's directory.
283 * Hint: to avoid this when typing a command use a forward slash.
284 * If the cd fails, it doesn't matter.
285 */
Bram Moolenaar05268152021-11-18 18:53:45 +0000286 if (vim_chdirfile(params.fname, "drop") == OK)
287 last_chdir_reason = "drop";
Bram Moolenaarf6303872015-04-03 17:59:43 +0200288 if (start_dir != NULL)
289 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000290 }
291#endif
292 TIME_MSG("expanding arguments");
293
294#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000295 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100296 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000297#endif
298
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100299 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300 ++RedrawingDisabled;
301
302 /*
303 * When listing swap file names, don't do cursor positioning et. al.
304 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200305 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000306 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000307
308 /*
309 * When certain to start the GUI, don't check capabilities of terminal.
310 * For GTK we can't be sure, but when started from the desktop it doesn't
311 * make sense to try using a terminal.
312 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200313#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
314 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000315 if (gui.starting
316# ifdef FEAT_GUI_GTK
317 && !isatty(2)
318# endif
319 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000320 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000321#endif
322
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000323 /*
324 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100325 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000326 * Note that we may use mch_exit() before mch_init()!
327 */
328 mch_init();
329 TIME_MSG("shell init");
330
331#ifdef USE_XSMP
332 /*
333 * For want of anywhere else to do it, try to connect to xsmp here.
334 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
335 * Hijacking -X 'no X connection' to also disable XSMP connection as that
336 * has a similar delay upon failure.
337 * Only try if SESSION_MANAGER is set to something non-null.
338 */
339 if (!x_no_connect)
340 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000341 char *p = getenv("SESSION_MANAGER");
342
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000343 if (p != NULL && *p != NUL)
344 {
345 xsmp_init();
346 TIME_MSG("xsmp init");
347 }
348 }
349#endif
350
351 /*
352 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000354 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000355
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100356#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100357 // Ensure output works usefully without a tty: buffer lines instead of
358 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100359 if (silent_mode)
360 setvbuf(stdout, NULL, _IOLBF, 0);
361#endif
362
Bram Moolenaar9404a182019-05-03 22:25:40 +0200363 // This message comes before term inits, but after setting "silent_mode"
364 // when the input is not a tty. Omit the message with --not-a-term.
365 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000366 printf(_("%d files to edit\n"), GARGCOUNT);
367
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000368 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000369 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100370 termcapinit(params.term); // set terminal name and get terminal
371 // capabilities (will set full_screen)
372 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000373 TIME_MSG("Termcap init");
374 }
375
376 /*
377 * Set the default values for the options that use Rows and Columns.
378 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100379 ui_get_shellsize(); // inits Rows and Columns
Luuk van Baal470a1412022-09-14 01:27:23 +0100380 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000381#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100382 // Set the 'diff' option now, so that it can be checked for in a .vimrc
383 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000384 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000385 diff_win_options(firstwin, FALSE);
386#endif
387
388 cmdline_row = Rows - p_ch;
389 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100390 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000391 set_init_2();
392 TIME_MSG("inits 2");
393
394 msg_scroll = TRUE;
395 no_wait_return = TRUE;
396
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100397 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100399 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000401
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200402#if defined(FEAT_TERMRESPONSE)
403 init_term_props(TRUE);
404#endif
405
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000406#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000408 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000409#endif
410
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100411 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
412 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200413 if (params.use_vimrc != NULL
414 && (STRCMP(params.use_vimrc, "NONE") == 0
415 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
416 p_lpl = FALSE;
417
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100418 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200419 exe_pre_commands(&params);
420
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100421 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200422 source_startup_scripts(&params);
423
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100424#ifdef FEAT_MZSCHEME
425 /*
426 * Newer version of MzScheme (Racket) require earlier (trampolined)
427 * initialisation via scheme_main_setup.
428 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200429 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200430 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100431 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200432 return mzscheme_main();
433#else
434 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100435#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200436}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100437#endif // NO_VIM_MAIN
438#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100439
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200440/*
441 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
442 * things simple.
443 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
444 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100445 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200446vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100447{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100448#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000449#ifdef FEAT_EVAL
450 /*
451 * Read all the plugin files.
452 * Only when compiled with +eval, since most plugins need it.
453 */
454 if (p_lpl)
455 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200456 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100457 char_u *plugin_pattern = (char_u *)
458# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
459 "plugin/*.vim"
460# else
461 "plugin/**/*.vim"
462# endif
463 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200464
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100465 // First add all package directories to 'runtimepath', so that their
466 // autoload directories can be found. Only if not done already with a
467 // :packloadall command.
468 // Make a copy of 'runtimepath', so that source_runtime does not use
469 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200470 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200471 {
472 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200473 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200474 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200475
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100476 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000478 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200479 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100480
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100481 // Only source "start" packages if not done already with a :packloadall
482 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200483 if (!did_source_packages)
484 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100485 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200486
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100487 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200488 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000489 }
490#endif
491
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000492#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100493 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000494 if (params.diff_mode && params.window_layout == 0)
495 {
496 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100497 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000498 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100499 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000500 }
501#endif
502
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000503 /*
504 * Recovery mode without a file name: List swap files.
505 * This uses the 'dir' option, therefore it must be after the
506 * initializations.
507 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200508 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000509 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200510 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000511 mch_exit(0);
512 }
513
514 /*
515 * Set a few option defaults after reading .vimrc files:
516 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
517 */
518 set_init_3();
519 TIME_MSG("inits 3");
520
521 /*
522 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
523 * Note that this overrides anything from a vimrc file.
524 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000525 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000526 p_uc = 0;
527
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528#ifdef FEAT_GUI
529 if (gui.starting)
530 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200531# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100532 // When something caused a message from a vimrc script, need to output
533 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000534 if (did_emsg || msg_didout)
535 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200536# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100538 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539 TIME_MSG("starting GUI");
540
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100541 // When running "evim" or "gvim -y" we need the menus, exit if we
542 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000543 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000544 mch_exit(1);
Luuk van Baal470a1412022-09-14 01:27:23 +0100545 firstwin->w_prev_height = firstwin->w_height; // may have changed
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546 }
547#endif
548
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000549#ifdef FEAT_VIMINFO
550 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000551 * Read in registers, history etc, but not marks, from the viminfo file.
552 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000553 */
554 if (*p_viminfo != NUL)
555 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000556 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557 TIME_MSG("reading viminfo");
558 }
559#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100560#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100561 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100562 if (get_vim_var_list(VV_OLDFILES) == NULL)
563 set_vim_var_list(VV_OLDFILES, list_alloc());
564#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000565
566#ifdef FEAT_QUICKFIX
567 /*
568 * "-q errorfile": Load the error file now.
569 * If the error file can't be read, exit before doing anything else.
570 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000571 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000572 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100573 char_u *enc = NULL;
574
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100575 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000576 if (params.use_ef != NULL)
577 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000578 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200579 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100580 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000581 {
582 out_char('\n');
583 mch_exit(3);
584 }
585 TIME_MSG("reading errorfile");
586 }
587#endif
588
589 /*
590 * Start putting things on the screen.
591 * Scroll screen down before drawing over it
592 * Clear screen now, so file message will not be cleared.
593 */
594 starting = NO_BUFFERS;
595 no_wait_return = FALSE;
596 if (!exmode_active)
597 msg_scroll = FALSE;
598
599#ifdef FEAT_GUI
600 /*
601 * This seems to be required to make callbacks to be called now, instead
602 * of after things have been put on the screen, which then may be deleted
603 * when getting a resize callback.
604 * For the Mac this handles putting files dropped on the Vim icon to
605 * global_alist.
606 */
607 if (gui.in_use)
608 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100609 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000610 TIME_MSG("GUI delay");
611 }
612#endif
613
614#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
615 qnx_clip_init();
616#endif
617
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200618#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
619 clip_init(TRUE);
620#endif
621
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100623 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000624# ifdef FEAT_GUI
625 if (!gui.in_use)
626# endif
627 {
628 setup_term_clip();
629 TIME_MSG("setup clipboard");
630 }
631#endif
632
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000633#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100634 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000635 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000636#endif
637
638 /*
639 * If "-" argument given: Read file from stdin.
640 * Do this before starting Raw mode, because it may change things that the
641 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
642 * are the same terminal: "cat | vim -".
643 * Using autocommands here may cause trouble...
644 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000645 if (params.edit_type == EDIT_STDIN && !recoverymode)
646 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000647
648#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100649 // When switching screens and something caused a message from a vimrc
650 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000651 if ((did_emsg || msg_didout) && *T_TI != NUL)
652 newline_on_exit = TRUE;
653#endif
654
655 /*
K.Takataeeec2542021-06-02 13:28:16 +0200656 * When done something that is not allowed or given an error message call
Bram Moolenaar13608d82022-08-29 15:06:50 +0100657 * wait_return(). This must be done before starttermcap(), because it may
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000658 * switch to another screen. It must be done after settmode(TMODE_RAW),
659 * because we want to react on a single key stroke.
660 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000661 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000662 */
663 settmode(TMODE_RAW);
664 TIME_MSG("setting raw mode");
665
666 if (need_wait_return || msg_didany)
667 {
668 wait_return(TRUE);
669 TIME_MSG("waiting for return");
670 }
671
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100672 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000673 TIME_MSG("start termcap");
674
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200675 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100677 scroll_region_reset(); // In case Rows changed
678 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000679
Bram Moolenaar651fca82021-11-29 20:39:38 +0000680#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200681 term_push_title(SAVE_RESTORE_BOTH);
682#endif
683
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000684 /*
685 * Don't clear the screen when starting in Ex mode, unless using the GUI.
686 */
687 if (exmode_active
688#ifdef FEAT_GUI
689 && !gui.in_use
690#endif
691 )
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100692 set_must_redraw(UPD_CLEAR);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000693 else
694 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100695 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000696 TIME_MSG("clearing screen");
697 }
698
699#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000700 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000701 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100702 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200703 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000704 TIME_MSG("getting crypt key");
705 }
706#endif
707
708 no_wait_return = TRUE;
709
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000710 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000711 * Create the requested number of windows and edit buffers in them.
712 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000714 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 TIME_MSG("opening buffers");
716
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000717#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100718 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000719 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
720#endif
721
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100722 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000723 if (exmode_active)
724 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
725
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
727 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000728 setpcmark();
729
730#ifdef FEAT_QUICKFIX
731 /*
732 * When started with "-q errorfile" jump to first error now.
733 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000734 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000735 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000736 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000737 TIME_MSG("jump to first error");
738 }
739#endif
740
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000741 /*
742 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000743 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000744 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200745 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200746 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000747
748#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000749 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000750 {
751 win_T *wp;
752
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100753 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200754 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000755 diff_win_options(wp, TRUE);
756 }
757#endif
758
759 /*
760 * Shorten any of the filenames, but only when absolute.
761 */
762 shorten_fnames(FALSE);
763
764 /*
765 * Need to jump to the tag before executing the '-c command'.
766 * Makes "vim -c '/return' -t main" work.
767 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000768 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000769 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000770 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000771
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000772 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000773 do_cmdline_cmd(IObuff);
774 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000775
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100776 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000777 if (swap_exists_did_quit)
778 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000779 }
780
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100781 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000782 if (params.n_commands > 0)
783 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000784
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100785 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200786 starting = 0;
787
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100788#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100789 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200790 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200791#endif
792
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793 RedrawingDisabled = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100794 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100797 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200798 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200799
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000800#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100801 // Requesting the termresponse is postponed until here, so that a "-c q"
802 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000803 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200804
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200805 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000806#endif
807
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100808 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000809 if (p_im)
810 need_start_insertmode = TRUE;
811
Bram Moolenaar14735512016-03-26 21:00:08 +0100812#ifdef FEAT_EVAL
813 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
814#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000815 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
816 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000817
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200818#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100819 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
820 // done after the clipboard is available and all initial commands that may
821 // modify the 'clipboard' setting have run; i.e. just before entering the
822 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200823 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200824#endif
825
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100826#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100827 // When a startup script or session file setup for diff'ing and
828 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000829 if (curwin->w_p_diff && curwin->w_p_scb)
830 {
831 update_topline();
832 check_scrollbind((linenr_T)0, 0L);
833 TIME_MSG("diff scrollbinding");
834 }
835#endif
836
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200837#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
838# ifdef VIMDLL
839 if (!gui.in_use)
840# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100841 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000842#endif
843
Bram Moolenaar4033c552017-09-16 20:54:51 +0200844#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100845 // When tab pages were created, may need to update the tab pages line and
846 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000847 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000848 {
849 out_flush();
850 gui_init_which_components(NULL);
851 gui_update_scrollbars(TRUE);
852 }
853 need_mouse_correct = TRUE;
854#endif
855
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100856 // If ":startinsert" command used, stuff a dummy command to be able to
857 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000858 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000859 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000860
861#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200862 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200863 {
864# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200865# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100866 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200867 if (gui.in_use)
868 {
869 mch_errmsg(_("netbeans is not supported with this GUI\n"));
870 mch_exit(2);
871 }
872# endif
873# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100874 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200875 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200876 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877#endif
878
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100879 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
880 // window title gets updated.
881 do_redraw = TRUE;
882
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000883 TIME_MSG("before starting main loop");
884
885 /*
886 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200887 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000888 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100890#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200891
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000892 return 0;
893}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000894
895/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200896 * Initialisation shared by main() and some tests.
897 */
898 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200899common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200900{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100901 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200902 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200903
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100904 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200905#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100906 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200907#endif
908
909#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100910 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200911#endif
912
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200913 /*
914 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100915 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200916 */
917 if ((IObuff = alloc(IOSIZE)) == NULL
918 || (NameBuff = alloc(MAXPATHL)) == NULL)
919 mch_exit(0);
920 TIME_MSG("Allocated generic buffers");
921
922#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100923 // Wait a moment for debugging NetBeans. Must be after allocating
924 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200925 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
926 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
927 TIME_MSG("NetBeans debug wait");
928#endif
929
930#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
931 /*
932 * Setup to use the current locale (for ctype() and many other things).
933 * NOTE: Translated messages with encodings other than latin1 will not
934 * work until set_init_1() has been called!
935 */
936 init_locale();
937 TIME_MSG("locale set");
938#endif
939
940#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100941 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200942#endif
943
944 /*
945 * Do a first scan of the arguments in "argv[]":
946 * -display or --display
947 * --server...
948 * --socketid
949 * --windowid
950 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200951 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200952
Bram Moolenaard0573012017-10-28 21:11:06 +0200953#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100954 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200955 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200956 TIME_MSG("GUI prepared");
957#endif
958
959#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100960 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200961 TIME_MSG("clipboard setup");
962#endif
963
964 /*
965 * Check if we have an interactive window.
966 * On the Amiga: If there is no window, we open one with a newcli command
967 * (needed for :! to * work). mch_check_win() will also handle the -d or
968 * -dev argument.
969 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100970 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200971 TIME_MSG("window checked");
972
973 /*
974 * Allocate the first window and buffer.
975 * Can't do anything without it, exit when it fails.
976 */
977 if (win_alloc_first() == FAIL)
978 mch_exit(0);
979
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100980 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200981
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100982 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200983 global_alist.id = 0;
984
985 /*
986 * Set the default values for the options.
987 * NOTE: Non-latin1 translated messages are working only after this,
988 * because this is where "has_mbyte" will be set, which is used by
989 * msg_outtrans_len_attr().
990 * First find out the home directory, needed to expand "~" in options.
991 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100992 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +0100993 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200994 TIME_MSG("inits 1");
995
996#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100997 // set v:lang and v:ctype
998 set_lang_var();
999
1000 // set v:argv
1001 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001002#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001003
1004#ifdef FEAT_SIGNS
1005 init_signs();
1006#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001007}
1008
1009/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001010 * Return TRUE when the --not-a-term argument was found.
1011 */
1012 int
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001013is_not_a_term(void)
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001014{
1015 return params.not_a_term;
1016}
1017
Bram Moolenaar7007e312021-03-27 12:11:33 +01001018/*
1019 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1020 */
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001021 int
1022is_not_a_term_or_gui(void)
Bram Moolenaar7007e312021-03-27 12:11:33 +01001023{
1024 return params.not_a_term
1025#ifdef FEAT_GUI
1026 || gui.in_use
1027#endif
1028 ;
1029}
1030
Bram Moolenaar2d12c252022-06-13 21:42:45 +01001031#if defined(FEAT_GUI) || defined(PROTO)
1032/*
1033 * If a --gui-dialog-file argument was given return the file name.
1034 * Otherwise return NULL.
1035 */
1036 char_u *
1037get_gui_dialog_file(void)
1038{
1039 return params.gui_dialog_file;
1040}
1041#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001042
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001043// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001044static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001045static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001046
1047/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001048 * Return TRUE if an operator was started but not finished yet.
1049 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001050 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001051 int
1052op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001053{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001054 return !(current_oap != NULL
1055 && !finish_op
1056 && current_oap->prev_opcount == 0
1057 && current_oap->prev_count0 == 0
1058 && current_oap->op_type == OP_NOP
1059 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001060}
1061
1062/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001063 * Return whether currently it is safe, assuming it was safe before (high level
1064 * state didn't change).
1065 */
1066 static int
1067is_safe_now(void)
1068{
1069 return stuff_empty()
1070 && typebuf.tb_len == 0
1071 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001072#ifdef FEAT_EVAL
1073 && !debug_mode
1074#endif
1075 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001076}
1077
1078/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001079 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1080 * there is no typeahead.
1081 */
1082 void
1083may_trigger_safestate(int safe)
1084{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001085 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001086
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001087#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001088 if (was_safe != is_safe)
1089 // Only log when the state changes, otherwise it happens at nearly
1090 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001091 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1092 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001093#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001094 if (is_safe)
1095 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1096 was_safe = is_safe;
1097}
1098
1099/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001100 * Something changed which causes the state possibly to be unsafe, e.g. a
1101 * character was typed. It will remain unsafe until the next call to
1102 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001103 */
1104 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001105state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001106{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001107#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001108 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001109 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001110#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001111 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001112}
1113
Dominique Pelle748b3082022-01-08 12:41:16 +00001114#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001115 int
1116get_was_safe_state(void)
1117{
1118 return was_safe;
1119}
Dominique Pelle748b3082022-01-08 12:41:16 +00001120#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001121
Dominique Pelle748b3082022-01-08 12:41:16 +00001122#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001123/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001124 * Invoked when leaving code that invokes callbacks. Then trigger
1125 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001126 */
1127 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001128may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001129{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001130 if (!was_safe)
1131 {
1132 // If the safe state was reset in state_no_longer_safe(), e.g. because
1133 // of calling feedkeys(), we check if it's now safe again (all keys
1134 // were consumed).
1135 was_safe = is_safe_now();
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001136#ifdef FEAT_EVAL
Bram Moolenaard103ee72019-09-18 21:15:31 +02001137 if (was_safe)
1138 ch_log(NULL, "SafeState: undo reset");
1139#endif
1140 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001141 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001142 {
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001143#ifdef FEAT_EVAL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001144 // Only do this message when another message was given, otherwise we
1145 // get lots of them.
1146 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1147 {
1148 int did = did_repeated_msg;
1149
1150 ch_log(NULL,
1151 "SafeState: back to waiting, triggering SafeStateAgain");
1152 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1153 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001154#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001155 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001156 }
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001157#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001158 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001159 ch_log(NULL,
1160 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001161#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001162}
Dominique Pelle748b3082022-01-08 12:41:16 +00001163#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001164
1165
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001166/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001167 * Main loop: Execute Normal mode commands until exiting Vim.
1168 * Also used to handle commands in the command-line window, until the window
1169 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 * Also used to handle ":visual" command after ":global": execute Normal mode
1171 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001172 */
1173 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001174main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001175 int cmdwin, // TRUE when working in the command-line window
1176 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001177{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001178 oparg_T oa; // operator arguments
1179 oparg_T *prev_oap; // operator arguments
1180 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001181#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001182 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001183 static linenr_T conceal_old_cursor_line = 0;
1184 static linenr_T conceal_new_cursor_line = 0;
1185 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001186#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001187
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001188 prev_oap = current_oap;
1189 current_oap = &oa;
1190
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001191#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001192 // Setup to catch a terminating error from the X server. Just ignore
1193 // it, restore the state and continue. This might not always work
1194 // properly, but at least we don't exit unexpectedly when the X server
1195 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001197 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001198 State = MODE_NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001199 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001200 got_int = TRUE;
1201 need_wait_return = FALSE;
1202 global_busy = FALSE;
1203 exmode_active = 0;
1204 skip_redraw = FALSE;
1205 RedrawingDisabled = 0;
1206 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001207 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001208# ifdef FEAT_EVAL
1209 emsg_skip = 0;
1210# endif
1211 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001212 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001213 settmode(TMODE_RAW);
1214 starttermcap();
1215 scroll_start();
1216 redraw_later_clear();
1217 }
1218#endif
1219
1220 clear_oparg(&oa);
Martin Tournoij7904fa42022-10-04 16:28:45 +01001221 while (!cmdwin || cmdwin_result == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001222 {
1223 if (stuff_empty())
1224 {
1225 did_check_timestamps = FALSE;
1226 if (need_check_timestamps)
1227 check_timestamps(FALSE);
Bram Moolenaar13608d82022-08-29 15:06:50 +01001228 if (need_wait_return) // if wait_return() still needed ...
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001229 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001230 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001231 {
1232 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001233 stuffReadbuff((char_u *)"i"); // start insert mode next
1234 // skip the fileinfo message now, because it would be shown
1235 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001236 need_fileinfo = FALSE;
1237 }
1238 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001239
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001240 // Reset "got_int" now that we got back to the main loop. Except when
1241 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1242 // the ":g" command.
1243 // For ":g/pat/vi" we reset "got_int" when used once. When used
1244 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001245 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001246 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001247 if (noexmode && global_busy && !exmode_active && previous_got_int)
1248 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001249 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1250 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001251 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001252 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001253 }
1254 else if (!global_busy || !exmode_active)
1255 {
1256 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001257 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001258 got_int = FALSE;
1259 }
1260 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001261 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001262 else
1263 previous_got_int = FALSE;
1264
Bram Moolenaar069613c2022-01-15 15:23:44 +00001265#ifdef FEAT_EVAL
1266 // At the toplevel there is no exception handling. Discard any that
1267 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1268 if (did_throw && !ex_normal_busy)
1269 discard_current_exception();
1270#endif
1271
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001272 if (!exmode_active)
1273 msg_scroll = FALSE;
1274 quit_more = FALSE;
1275
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001276 // it's not safe unless may_trigger_safestate_main() is called
1277 was_safe = FALSE;
1278
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001279 /*
1280 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1281 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1282 * update cursor and redraw.
1283 */
1284 if (skip_redraw || exmode_active)
1285 skip_redraw = FALSE;
1286 else if (do_redraw || stuff_empty())
1287 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001288#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001289 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001290 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001291#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001292#ifdef HAVE_DROP_FILE
1293 // If files were dropped while text was locked or the curbuf was
1294 // locked, this would be a good time to handle the drop.
1295 handle_any_postponed_drop();
1296#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001297#ifdef FEAT_CONCEAL
1298 if (curwin->w_p_cole == 0)
1299 conceal_update_lines = FALSE;
1300#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001301
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001302 // Trigger CursorMoved if the cursor moved.
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001303 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001304#ifdef FEAT_PROP_POPUP
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001305 || popup_visible
Bram Moolenaar3397f742019-06-02 18:40:06 +02001306#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001307#ifdef FEAT_CONCEAL
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001308 || curwin->w_p_cole > 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001309#endif
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001310 )
1311 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001312 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001313 if (has_cursormoved())
1314 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1315 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001316#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001317 if (popup_visible)
1318 popup_check_cursor_pos();
1319#endif
1320#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001321 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001322 {
1323 conceal_old_cursor_line = last_cursormoved.lnum;
1324 conceal_new_cursor_line = curwin->w_cursor.lnum;
1325 conceal_update_lines = TRUE;
1326 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001327#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001328 last_cursormoved = curwin->w_cursor;
1329 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001330
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001331#if defined(FEAT_CONCEAL)
1332 if (conceal_update_lines
1333 && (conceal_old_cursor_line != conceal_new_cursor_line
1334 || conceal_cursor_line(curwin)
1335 || need_cursor_line_redraw))
1336 {
1337 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001338 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001339 && conceal_old_cursor_line
1340 <= curbuf->b_ml.ml_line_count)
1341 redrawWinline(curwin, conceal_old_cursor_line);
1342 redrawWinline(curwin, conceal_new_cursor_line);
1343 curwin->w_valid &= ~VALID_CROW;
1344 need_cursor_line_redraw = FALSE;
1345 }
1346#endif
1347
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001348 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001349 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001350 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001351 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001352 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1353 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001354 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001355
LemonBoy09371822022-04-08 15:18:45 +01001356 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1357 // before triggering a WinScrolled autocommand.
1358 update_topline();
1359 validate_cursor();
1360
1361 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001362 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01001363
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001364 // If nothing is pending and we are going to wait for the user to
1365 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001366 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001367
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001368#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001369 // Updating diffs from changed() does not always work properly,
1370 // esp. updating folds. Do an update just before redrawing if
1371 // needed.
1372 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1373 {
1374 ex_diffupdate(NULL);
1375 curtab->tp_diff_update = FALSE;
1376 }
1377
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001378 // Scroll-binding for diff mode may have been postponed until
1379 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001380 if (diff_need_scrollbind)
1381 {
1382 check_scrollbind((linenr_T)0, 0L);
1383 diff_need_scrollbind = FALSE;
1384 }
1385#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001386#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001387 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001388 foldAdjustVisual();
1389#endif
1390#ifdef FEAT_FOLDING
1391 /*
1392 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1393 * contain the cursor.
1394 * When 'foldopen' is "all", open the fold(s) under the cursor.
1395 * This may mark the window for redrawing.
1396 */
1397 if (hasAnyFolding(curwin) && !char_avail())
1398 {
1399 foldCheckClose();
1400 if (fdo_flags & FDO_ALL)
1401 foldOpenCursor();
1402 }
1403#endif
1404
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001405 // Before redrawing, make sure w_topline is correct, and w_leftcol
1406 // if lines don't wrap, and w_skipcol if lines wrap.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001407 update_topline();
1408 validate_cursor();
1409
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001410 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001411 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001412 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001413 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001414 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001415 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001416 mch_enable_flush();
1417 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001418 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001419 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001420 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001421 if (need_maketitle)
1422 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001423#ifdef FEAT_VIMINFO
1424 curbuf->b_last_used = vim_time();
1425#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001426 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001427 if (keep_msg != NULL)
1428 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001429 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001430
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001431 if (p != NULL)
1432 {
1433 // msg_start() will set keep_msg to NULL, make a copy
1434 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1435 // check for duplicates. Never put this message in
1436 // history.
1437 msg_hist_off = TRUE;
1438 msg_attr((char *)p, keep_msg_attr);
1439 msg_hist_off = FALSE;
1440 vim_free(p);
1441 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001442 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001443 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001444 {
1445 fileinfo(FALSE, TRUE, FALSE);
1446 need_fileinfo = FALSE;
1447 }
1448
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001449 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001451 msg_didany = FALSE; // reset lines_left in msg_start()
1452 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001453 showruler(FALSE);
1454
1455 setcursor();
1456 cursor_on();
1457
1458 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001459
1460#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001461 // Now that we have drawn the first screen all the startup stuff
1462 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001463 if (time_fd != NULL)
1464 {
1465 TIME_MSG("first screen update");
1466 TIME_MSG("--- VIM STARTED ---");
1467 fclose(time_fd);
1468 time_fd = NULL;
1469 }
1470#endif
Bram Moolenaar0a60f792022-11-19 21:18:11 +00001471 // After the first screen update may start triggering WinScrolled
1472 // autocmd events. Store all the scroll positions and sizes now.
1473 may_make_initial_scroll_size_snapshot();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001474 }
1475#ifdef FEAT_GUI
1476 if (need_mouse_correct)
1477 gui_mouse_correct();
1478#endif
1479
1480 /*
1481 * Update w_curswant if w_set_curswant has been set.
1482 * Postponed until here to avoid computing w_virtcol too often.
1483 */
1484 update_curswant();
1485
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001486#ifdef FEAT_EVAL
1487 /*
1488 * May perform garbage collection when waiting for a character, but
1489 * only at the very toplevel. Otherwise we may be using a List or
1490 * Dict internally somewhere.
1491 * "may_garbage_collect" is reset in vgetc() which is invoked through
1492 * do_exmode() and normal_cmd().
1493 */
1494 may_garbage_collect = (!cmdwin && !noexmode);
1495#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001496 /*
1497 * If we're invoked as ex, do a round of ex commands.
1498 * Otherwise, get and execute a normal mode command.
1499 */
1500 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001501 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001502 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001503 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001505 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001506 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001507 {
1508#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001509 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001510 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001511 && !VIsual_active
1512 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001513 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001514 // If terminal_loop() returns OK we got a key that is handled
1515 // in Normal model. With FAIL we first need to position the
1516 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001517 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001518 normal_cmd(&oa, TRUE);
1519 }
1520 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001521#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001522 {
1523#ifdef FEAT_TERMINAL
1524 skip_term_loop = FALSE;
1525#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001526 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001527 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001528 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001529 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001530
1531theend:
1532 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001533}
1534
1535
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001536#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001537/*
1538 * Exit, but leave behind swap files for modified buffers.
1539 */
1540 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001541getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001542{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001543# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001544 // Ignore SIGHUP, because a dropped connection causes a read error, which
1545 // makes Vim exit and then handling SIGHUP causes various reentrance
1546 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001547 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001548# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001549
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001550 ml_close_notmod(); // close all not-modified buffers
1551 ml_sync_all(FALSE, FALSE); // preserve all swap files
1552 ml_close_all(FALSE); // close all memfiles, without deleting
1553 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001554}
1555#endif
1556
1557
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001558/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001559 * Exit properly. This is the only way to exit Vim after startup has
1560 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001561 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001562 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001563getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001564{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001565 exiting = TRUE;
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001566#if defined(FEAT_EVAL)
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001567 ch_log(NULL, "Exiting...");
1568#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001569
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001570 // When running in Ex mode an error causes us to exit with a non-zero exit
1571 // code. POSIX requires this, although it's not 100% clear from the
1572 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001573 if (exmode_active)
1574 exitval += ex_exitval;
1575
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001576#ifdef FEAT_EVAL
1577 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1578 set_vim_var_nr(VV_EXITING, exitval);
1579#endif
1580
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001581 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001582 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001583 windgoto((int)Rows - 1, 0);
1584
Bram Moolenaar58779852022-09-06 18:31:14 +01001585#ifdef FEAT_EVAL
1586 // Invoked all deferred functions in the function stack.
1587 invoke_all_defer();
1588#endif
1589
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001590#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001591 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001592 hash_debug_results();
1593#endif
1594
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001595#ifdef FEAT_GUI
1596 msg_didany = FALSE;
1597#endif
1598
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001599 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001600 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001601 tabpage_T *tp;
1602 tabpage_T *next_tp;
1603 buf_T *buf;
1604 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001605 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001606
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001607 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001608 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001609 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001610 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001611 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001612 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001613 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001614 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001615 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001616 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001617 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001618 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001619 bufref_T bufref;
1620
1621 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001622 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1623 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001624 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001625 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001626
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001627 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001628 next_tp = first_tabpage;
1629 break;
1630 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001631 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001632 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001633
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001634 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001635 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001636 if (buf->b_ml.ml_mfp != NULL)
1637 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001638 bufref_T bufref;
1639
1640 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001641 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001642 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001643 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001644 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001645 break;
1646 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001647
1648 // deathtrap() blocks autocommands, but we do want to trigger
1649 // VimLeavePre.
1650 if (is_autocmd_blocked())
1651 {
1652 unblock_autocmds();
1653 ++unblock;
1654 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001655 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001656 if (unblock)
1657 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001658 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001659
1660#ifdef FEAT_VIMINFO
1661 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001662 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001663 write_viminfo(NULL, FALSE);
1664#endif
1665
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001666 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001667 {
1668 int unblock = 0;
1669
1670 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1671 if (is_autocmd_blocked())
1672 {
1673 unblock_autocmds();
1674 ++unblock;
1675 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001676 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001677 if (unblock)
1678 block_autocmds();
1679 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001680
Bram Moolenaar05159a02005-02-26 23:04:13 +00001681#ifdef FEAT_PROFILE
1682 profile_dump();
1683#endif
1684
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001685 if (did_emsg
1686#ifdef FEAT_GUI
1687 || (gui.in_use && msg_didany && p_verbose > 0)
1688#endif
1689 )
1690 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001691 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001692 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001693 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001694 }
1695
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001696 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001697 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001698 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001699
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001700#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001701 job_stop_on_exit();
1702#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001703#ifdef FEAT_LUA
1704 lua_end();
1705#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001706#ifdef FEAT_MZSCHEME
1707 mzscheme_end();
1708#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001709#ifdef FEAT_TCL
1710 tcl_end();
1711#endif
1712#ifdef FEAT_RUBY
1713 ruby_end();
1714#endif
1715#ifdef FEAT_PYTHON
1716 python_end();
1717#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001718#ifdef FEAT_PYTHON3
1719 python3_end();
1720#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001721#ifdef FEAT_PERL
1722 perl_end();
1723#endif
1724#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1725 iconv_end();
1726#endif
1727#ifdef FEAT_NETBEANS_INTG
1728 netbeans_end();
1729#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001730#ifdef FEAT_CSCOPE
1731 cs_end();
1732#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001733#ifdef FEAT_EVAL
1734 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001735 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001736#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001737#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001738 free_cmd_argsW();
1739#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001740
1741 mch_exit(exitval);
1742}
1743
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001744/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001745 * Get the name of the display, before gui_prepare() removes it from
1746 * argv[]. Used for the xterm-clipboard display.
1747 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001748 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001749 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001750 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001751early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001752{
Bram Moolenaar03005972008-11-20 13:12:36 +00001753#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1754 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001755 int argc = parmp->argc;
1756 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001757 int i;
1758
1759 for (i = 1; i < argc; i++)
1760 {
1761 if (STRCMP(argv[i], "--") == 0)
1762 break;
1763# ifdef FEAT_XCLIPBOARD
1764 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001765# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001766 || STRICMP(argv[i], "--display") == 0
1767# endif
1768 )
1769 {
1770 if (i == argc - 1)
1771 mainerr_arg_missing((char_u *)argv[i]);
1772 xterm_display = argv[++i];
1773 }
1774# endif
1775# ifdef FEAT_CLIENTSERVER
1776 else if (STRICMP(argv[i], "--servername") == 0)
1777 {
1778 if (i == argc - 1)
1779 mainerr_arg_missing((char_u *)argv[i]);
1780 parmp->serverName_arg = (char_u *)argv[++i];
1781 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001782 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001783 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001784 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001785 {
1786 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001787# ifdef FEAT_GUI
1788 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001789 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001790 gui.dofork = FALSE;
1791# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001792 }
1793# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001794
Bram Moolenaar4f974752019-02-17 17:44:42 +01001795# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1796# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001797 else if (STRICMP(argv[i], "--windowid") == 0)
1798# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001799 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001800# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001801 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001802 long_u id;
1803 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001804
1805 if (i == argc - 1)
1806 mainerr_arg_missing((char_u *)argv[i]);
1807 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001808 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001809 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001810 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001811 if (count != 1)
1812 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1813 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001814# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001815 win_socket_id = id;
1816# else
1817 gtk_socket_id = id;
1818# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001819 i++;
1820 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001821# endif
1822# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001823 else if (STRICMP(argv[i], "--echo-wid") == 0)
1824 echo_wid_arg = TRUE;
1825# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001826# ifndef FEAT_NETBEANS_INTG
1827 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001828 {
1829 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1830 mch_exit(2);
1831 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001832# endif
1833
Bram Moolenaar58d98232005-07-23 22:25:46 +00001834 }
1835#endif
1836}
1837
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001838#ifndef NO_VIM_MAIN
1839/*
1840 * Get a (optional) count for a Vim argument.
1841 */
1842 static int
1843get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001844 char_u *p, // pointer to argument
1845 int *idx, // index in argument, is incremented
1846 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001847{
1848 if (vim_isdigit(p[*idx]))
1849 {
1850 def = atoi((char *)&(p[*idx]));
1851 while (vim_isdigit(p[*idx]))
1852 *idx = *idx + 1;
1853 }
1854 return def;
1855}
1856
1857/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001858 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001859 * If the executable name starts with "r" we disable shell commands.
1860 * If the next character is "e" we run in Easy mode.
1861 * If the next character is "g" we run the GUI version.
1862 * If the next characters are "view" we start in readonly mode.
1863 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1864 * If the next characters are "ex" we start in Ex mode. If it's followed
1865 * by "im" use improved Ex mode.
1866 */
1867 static void
1868parse_command_name(mparm_T *parmp)
1869{
1870 char_u *initstr;
1871
1872 initstr = gettail((char_u *)parmp->argv[0]);
1873
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001874#ifdef FEAT_EVAL
1875 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001876 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001877#endif
1878
1879 if (TOLOWER_ASC(initstr[0]) == 'r')
1880 {
1881 restricted = TRUE;
1882 ++initstr;
1883 }
1884
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001885 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001886 if (TOLOWER_ASC(initstr[0]) == 'e'
1887 && (TOLOWER_ASC(initstr[1]) == 'v'
1888 || TOLOWER_ASC(initstr[1]) == 'g'))
1889 {
1890#ifdef FEAT_GUI
1891 gui.starting = TRUE;
1892#endif
1893 parmp->evim_mode = TRUE;
1894 ++initstr;
1895 }
1896
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001897 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001898 if (TOLOWER_ASC(initstr[0]) == 'g')
1899 {
1900 main_start_gui();
1901#ifdef FEAT_GUI
1902 ++initstr;
1903#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001904#ifdef GUI_MAY_SPAWN
1905 gui.dospawn = FALSE; // No need to spawn a new process.
1906#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001907 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001908#ifdef GUI_MAY_SPAWN
1909 else
1910 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1911#endif
1912
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001913
1914 if (STRNICMP(initstr, "view", 4) == 0)
1915 {
1916 readonlymode = TRUE;
1917 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001918 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001919 initstr += 4;
1920 }
1921 else if (STRNICMP(initstr, "vim", 3) == 0)
1922 initstr += 3;
1923
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001924 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001925 if (STRICMP(initstr, "diff") == 0)
1926 {
1927#ifdef FEAT_DIFF
1928 parmp->diff_mode = TRUE;
1929#else
1930 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1931 mch_errmsg("\n");
1932 mch_exit(2);
1933#endif
1934 }
1935
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001936 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001937 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001938 if (STRNICMP(initstr, "ex", 2) == 0)
1939 {
1940 if (STRNICMP(initstr + 2, "im", 2) == 0)
1941 exmode_active = EXMODE_VIM;
1942 else
1943 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001944 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001945 }
1946}
1947
Bram Moolenaar58d98232005-07-23 22:25:46 +00001948/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001949 * Scan the command line arguments.
1950 */
1951 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001952command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001953{
1954 int argc = parmp->argc;
1955 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001956 int argv_idx; // index in argv[n][]
1957 int had_minmin = FALSE; // found "--" argument
1958 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001959 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001960 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001961 long n;
1962
1963 --argc;
1964 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001965 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001966 while (argc > 0)
1967 {
1968 /*
1969 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1970 */
1971 if (argv[0][0] == '+' && !had_minmin)
1972 {
1973 if (parmp->n_commands >= MAX_ARG_CMDS)
1974 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001975 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001976 if (argv[0][1] == NUL)
1977 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1978 else
1979 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1980 }
1981
1982 /*
1983 * Optional argument.
1984 */
1985 else if (argv[0][0] == '-' && !had_minmin)
1986 {
1987 want_argument = FALSE;
1988 c = argv[0][argv_idx++];
1989#ifdef VMS
1990 /*
1991 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1992 * and "-/X" as "-X".
1993 */
1994 if (c == '/')
1995 {
1996 c = argv[0][argv_idx++];
1997 c = TOUPPER_ASC(c);
1998 }
1999 else
2000 c = TOLOWER_ASC(c);
2001#endif
2002 switch (c)
2003 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002004 case NUL: // "vim -" read from stdin
2005 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002006 if (exmode_active)
2007 silent_mode = TRUE;
2008 else
2009 {
2010 if (parmp->edit_type != EDIT_NONE)
2011 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2012 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002013 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002014 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002015 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002016 break;
2017
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002018 case '-': // "--" don't take any more option arguments
2019 // "--help" give help message
2020 // "--version" give version message
2021 // "--clean" clean context
2022 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002023 // "--startuptime fname" write timing info
2024 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002025 // "--nofork" don't fork
2026 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002027 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002028 // "--ttyfail" exit if not a term
2029 // "--noplugin[s]" skip plugins
2030 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002031 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2032 usage();
2033 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2034 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002035 Columns = 80; // need to init Columns
2036 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002037#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002038 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002039#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002040 list_version();
2041 msg_putchar('\n');
2042 msg_didout = FALSE;
2043 mch_exit(0);
2044 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002045 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2046 {
2047 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002048#ifdef FEAT_GUI
2049 use_gvimrc = (char_u *)"NONE";
2050#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002051 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002052 set_option_value_give_err((char_u *)"vif",
2053 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002054 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002055 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2056 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002057#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002058 parmp->literal = TRUE;
2059#endif
2060 }
2061 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2062 {
2063#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002064 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002065#endif
2066 }
2067 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2068 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002069 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2070 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002071 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2072 == 0)
2073 {
2074 want_argument = TRUE;
2075 argv_idx += 15;
2076 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002077 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2078 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002079 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2080 {
2081 want_argument = TRUE;
2082 argv_idx += 3;
2083 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002084 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2085 {
2086 want_argument = TRUE;
2087 argv_idx += 11;
2088 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002089 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2090 {
2091 want_argument = TRUE;
2092 argv_idx += 3;
2093 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002094#ifdef FEAT_CLIENTSERVER
2095 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002096 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002097 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2098 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2099 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002100 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002101 if (argc > 1)
2102 {
2103 --argc;
2104 ++argv;
2105 }
2106 }
2107#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002108#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002109# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002110 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002111# else
2112 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2113# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002114 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002115 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002116 if (argc > 1)
2117 {
2118 --argc;
2119 ++argv;
2120 }
2121 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002122#endif
2123#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002124 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2125 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002126 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002127 }
2128#endif
2129 else
2130 {
2131 if (argv[0][argv_idx])
2132 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2133 had_minmin = TRUE;
2134 }
2135 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002136 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002137 break;
2138
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002139 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002140#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002141 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002142#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002143 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002144 mch_exit(2);
2145#endif
2146 break;
2147
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002148 case 'b': // "-b" binary mode
2149 // Needs to be effective before expanding file names, because
2150 // for Win32 this makes us edit a shortcut file itself,
2151 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002152 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002153 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002154 break;
2155
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002156 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002157 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002158 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002159 break;
2160
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002161 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002162 exmode_active = EXMODE_NORMAL;
2163 break;
2164
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002165 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002166 exmode_active = EXMODE_VIM;
2167 break;
2168
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002169 case 'f': // "-f" GUI: run in foreground. Amiga: open
2170 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002171#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002172 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002173#endif
2174 break;
2175
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002176 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002177 main_start_gui();
2178 break;
2179
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002180 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002181 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002182 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002183 break;
2184
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002185 case '?': // "-?" give help message (for MS-Windows)
2186 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002187#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002188 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002189 gui.starting = FALSE;
2190#endif
2191 usage();
2192 break;
2193
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002194 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002195#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002196 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002197 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002198#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002199 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002200 mch_exit(2);
2201#endif
2202 break;
2203
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002204 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002205 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002206 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002207 break;
2208
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002209 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002210 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002211 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002212
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002213 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002214 p_write = FALSE;
2215 break;
2216
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002217 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002218#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002219 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002220#endif
2221 parmp->evim_mode = TRUE;
2222 break;
2223
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002224 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002225 change_compatible(FALSE);
2226 break;
2227
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002228 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002229#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002230 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002231 if (argv[0][argv_idx] == 'b')
2232 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002233 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002234 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002235 }
2236 else
2237#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002238 parmp->no_swap_file = TRUE;
2239 break;
2240
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002241 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002242#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002243 // For some reason on MacOS X, an argument like:
2244 // -psn_0_10223617 is passed in when invoke from Finder
2245 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002246 if (argv[0][argv_idx] == 's')
2247 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002248 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002249 main_start_gui();
2250 break;
2251 }
2252#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002253 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002254 parmp->window_count = get_number_arg((char_u *)argv[0],
2255 &argv_idx, 0);
2256 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002257 break;
2258
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002259 case 'o': // "-o[N]" open N horizontal split windows
2260 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002261 parmp->window_count = get_number_arg((char_u *)argv[0],
2262 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002263 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002264 break;
2265
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002266 case 'O': // "-O[N]" open N vertical split windows
2267 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002268 parmp->window_count = get_number_arg((char_u *)argv[0],
2269 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002270 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002271 break;
2272
2273#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002274 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002275 if (parmp->edit_type != EDIT_NONE)
2276 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2277 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002278 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002279 {
2280 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2281 argv_idx = -1;
2282 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002283 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002284 want_argument = TRUE;
2285 break;
2286#endif
2287
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002288 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002289 readonlymode = TRUE;
2290 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002291 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002292 break;
2293
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002294 case 'r': // "-r" recovery mode
2295 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002296 recoverymode = 1;
2297 break;
2298
2299 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002300 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002301 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002302 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002303 want_argument = TRUE;
2304 break;
2305
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002306 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002307 if (parmp->edit_type != EDIT_NONE)
2308 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2309 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002310 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002311 {
2312 parmp->tagname = (char_u *)argv[0] + argv_idx;
2313 argv_idx = -1;
2314 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002315 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002316 want_argument = TRUE;
2317 break;
2318
2319#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002320 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002321 parmp->use_debug_break_level = 9999;
2322 break;
2323#endif
2324#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002325 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002326# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002327 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002328 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2329 want_argument = TRUE;
2330 else
2331# endif
2332 parmp->diff_mode = TRUE;
2333 break;
2334#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002335 case 'V': // "-V{N}" Verbose level
2336 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002337 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2338 if (argv[0][argv_idx] != NUL)
2339 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002340 set_option_value_give_err((char_u *)"verbosefile",
2341 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002342 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002343 }
2344 break;
2345
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002346 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002347 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002348#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002349 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002350#endif
2351 break;
2352
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002353 case 'w': // "-w{number}" set window height
2354 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002355 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2356 {
2357 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002358 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002359 break;
2360 }
2361 want_argument = TRUE;
2362 break;
2363
2364#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002365 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002366 parmp->ask_for_key = TRUE;
2367 break;
2368#endif
2369
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002370 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002371#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2372 x_no_connect = TRUE;
2373#endif
2374 break;
2375
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002376 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002377 restricted = TRUE;
2378 break;
2379
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002380 case 'c': // "-c{command}" or "-c {command}" execute
2381 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002382 if (argv[0][argv_idx] != NUL)
2383 {
2384 if (parmp->n_commands >= MAX_ARG_CMDS)
2385 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002386 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2387 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002388 argv_idx = -1;
2389 break;
2390 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002391 // FALLTHROUGH
2392 case 'S': // "-S {file}" execute Vim script
2393 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002394#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002395 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002396#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002397 case 'T': // "-T {terminal}" terminal name
2398 case 'u': // "-u {vimrc}" vim inits file
2399 case 'U': // "-U {gvimrc}" gvim inits file
2400 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002401#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002402 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002403#endif
2404 want_argument = TRUE;
2405 break;
2406
2407 default:
2408 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2409 }
2410
2411 /*
2412 * Handle option arguments with argument.
2413 */
2414 if (want_argument)
2415 {
2416 /*
2417 * Check for garbage immediately after the option letter.
2418 */
2419 if (argv[0][argv_idx] != NUL)
2420 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2421
2422 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002423 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002424 mainerr_arg_missing((char_u *)argv[0]);
2425 ++argv;
2426 argv_idx = -1;
2427
2428 switch (c)
2429 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002430 case 'c': // "-c {command}" execute command
2431 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002432 if (parmp->n_commands >= MAX_ARG_CMDS)
2433 mainerr(ME_EXTRA_CMD, NULL);
2434 if (c == 'S')
2435 {
2436 char *a;
2437
2438 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002439 // "-S" without argument: use default session file
2440 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002441 a = SESSION_FILE;
2442 else if (argv[0][0] == '-')
2443 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002444 // "-S" followed by another option: use default
2445 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002446 a = SESSION_FILE;
2447 ++argc;
2448 --argv;
2449 }
2450 else
2451 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002452 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002453 if (p == NULL)
2454 mch_exit(2);
2455 sprintf((char *)p, "so %s", a);
2456 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2457 parmp->commands[parmp->n_commands++] = p;
2458 }
2459 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002460 parmp->commands[parmp->n_commands++] =
2461 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462 break;
2463
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002464 case '-':
2465 if (argv[-1][2] == 'c')
2466 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002467 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002468 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2469 mainerr(ME_EXTRA_CMD, NULL);
2470 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002471 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002472 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002473 // --gui-dialog-file fname
2474 if (argv[-1][2] == 'g')
2475 {
2476 // without GUI ignore the argument
2477#ifdef FEAT_GUI
2478 parmp->gui_dialog_file = (char_u *)argv[0];
2479#endif
2480 }
2481
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002482 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002483 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002484 break;
2485
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002486 // case 'd': -d {device} is handled in mch_check_win() for the
2487 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002488
2489#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002490 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002491 parmp->use_ef = (char_u *)argv[0];
2492 break;
2493#endif
2494
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002495 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002496 set_option_value_give_err((char_u *)"vif",
2497 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002498 break;
2499
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002500 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002501 if (scriptin[0] != NULL)
2502 {
2503scripterror:
2504 mch_errmsg(_("Attempt to open script file again: \""));
2505 mch_errmsg(argv[-1]);
2506 mch_errmsg(" ");
2507 mch_errmsg(argv[0]);
2508 mch_errmsg("\"\n");
2509 mch_exit(2);
2510 }
2511 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2512 {
2513 mch_errmsg(_("Cannot open for reading: \""));
2514 mch_errmsg(argv[0]);
2515 mch_errmsg("\"\n");
2516 mch_exit(2);
2517 }
2518 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002519 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002520 break;
2521
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002522 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002523 parmp->tagname = (char_u *)argv[0];
2524 break;
2525
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002526 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002527 /*
2528 * The -T term argument is always available and when
2529 * HAVE_TERMLIB is supported it overrides the environment
2530 * variable TERM.
2531 */
2532#ifdef FEAT_GUI
2533 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002534 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002535 else
2536#endif
2537 parmp->term = (char_u *)argv[0];
2538 break;
2539
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002540 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 parmp->use_vimrc = (char_u *)argv[0];
2542 break;
2543
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002544 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545#ifdef FEAT_GUI
2546 use_gvimrc = (char_u *)argv[0];
2547#endif
2548 break;
2549
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002550 case 'w': // "-w {nr}" 'window' value
2551 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002552 if (vim_isdigit(*((char_u *)argv[0])))
2553 {
2554 argv_idx = 0;
2555 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002556 set_option_value_give_err((char_u *)"window",
2557 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558 argv_idx = -1;
2559 break;
2560 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002561 // FALLTHROUGH
2562 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002563 if (scriptout != NULL)
2564 goto scripterror;
2565 if ((scriptout = mch_fopen(argv[0],
2566 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2567 {
2568 mch_errmsg(_("Cannot open for script output: \""));
2569 mch_errmsg(argv[0]);
2570 mch_errmsg("\"\n");
2571 mch_exit(2);
2572 }
2573 break;
2574
Bram Moolenaar4f974752019-02-17 17:44:42 +01002575#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002576 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002577 gui_mch_set_parent(argv[0]);
2578 break;
2579#endif
2580 }
2581 }
2582 }
2583
2584 /*
2585 * File name argument.
2586 */
2587 else
2588 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002589 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002591 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2593 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2594 parmp->edit_type = EDIT_FILE;
2595
2596#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002597 // Remember if the argument was a full path before changing
2598 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2600 parmp->full_path = TRUE;
2601#endif
2602
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002603 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002604 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2605 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2606 mch_exit(2);
2607#ifdef FEAT_DIFF
2608 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2609 && !mch_isdir(alist_name(&GARGLIST[0])))
2610 {
2611 char_u *r;
2612
2613 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2614 if (r != NULL)
2615 {
2616 vim_free(p);
2617 p = r;
2618 }
2619 }
2620#endif
K.Takatab247e062022-02-07 10:45:23 +00002621#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002622 /*
2623 * If vim is invoked by non-Cygwin tools, convert away any
2624 * DOS paths, so things like .swp files are created correctly.
2625 * Look for evidence of non-Cygwin paths before we bother.
2626 * This is only for when using the Unix files.
2627 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002628 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002629 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002630 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002632# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002633 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002634# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002635 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002636# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002637 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002638 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002639 if (p == NULL)
2640 mch_exit(2);
2641 }
2642#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002643
2644#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002645 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002646 fname_case(p, 0);
2647#endif
2648
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002649 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002650#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002651 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002653 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002654#endif
2655 );
2656
Bram Moolenaar4f974752019-02-17 17:44:42 +01002657#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002658 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002659 // Remember this argument has been added to the argument list.
2660 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002661 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002662# ifdef FEAT_DIFF
2663 parmp->diff_mode
2664# else
2665 FALSE
2666# endif
2667 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002668 }
2669#endif
2670 }
2671
2672 /*
2673 * If there are no more letters after the current "-", go to next
2674 * argument. argv_idx is set to -1 when the current argument is to be
2675 * skipped.
2676 */
2677 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2678 {
2679 --argc;
2680 ++argv;
2681 argv_idx = 1;
2682 }
2683 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002684
2685#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002686 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2687 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002688 if (parmp->n_commands > 0)
2689 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002690 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002691 if (p != NULL)
2692 {
2693 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2694 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2695 vim_free(p);
2696 }
2697 }
2698#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002699}
2700
2701/*
2702 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002703 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002704 */
2705 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002706check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002707{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002708 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709
2710 input_isatty = mch_input_isatty();
2711 if (exmode_active)
2712 {
2713 if (!input_isatty)
2714 silent_mode = TRUE;
2715 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002716 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002718 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719 && !gui.starting
2720#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002721 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002722 {
2723#ifdef NBDEBUG
2724 /*
2725 * This shouldn't be necessary. But if I run netbeans with the log
2726 * output coming to the console and XOpenDisplay fails, I get vim
2727 * trying to start with input/output to my console tty. This fills my
2728 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002729 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002730 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002731 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002732 {
2733 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2734 exit(1);
2735 }
2736#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002737#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2738 if (
2739# ifdef VIMDLL
2740 !gui.starting &&
2741# endif
2742 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002743 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002744# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002745 && defined(FEAT_GETTEXT)
2746 char *s, *tofree = NULL;
2747
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002748 // Set the encoding of the error message based on $LC_ALL or
2749 // other environment variables instead of 'encoding'.
2750 // Note that the message is shown on a Cygwin terminal (e.g.
2751 // mintty) which encoding is based on $LC_ALL or etc., not the
2752 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002753 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002754 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002755 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002756 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2757 vim_free(tofree);
2758# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002759 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2760 exit(1);
2761 }
2762#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002763 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2765 if (!input_isatty)
2766 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2767 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002768 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2769 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002771 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002772 TIME_MSG("Warning delay");
2773 }
2774}
2775
2776/*
2777 * Read text from stdin.
2778 */
2779 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002780read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781{
2782 int i;
2783
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002784 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002786
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002787 no_wait_return = TRUE;
2788 i = msg_didany;
2789 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002790
2791 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002792 (void)open_buffer(TRUE, NULL, 0);
2793
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794 no_wait_return = FALSE;
2795 msg_didany = i;
2796 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002797
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002798 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002799
2800#if !(defined(AMIGA) || defined(MACOS_X))
2801 // Dup stdin from stderr to read commands from, so that shell commands
2802 // work.
2803 // TODO: why is this needed, even though readfile() has done this?
2804 close(0);
2805 vim_ignored = dup(2);
2806#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002807}
2808
2809/*
2810 * Create the requested number of windows and edit buffers in them.
2811 * Also does recovery if "recoverymode" set.
2812 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002813 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002814create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002815{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002816 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002817 int done = 0;
2818
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002819 /*
2820 * Create the number of windows that was requested.
2821 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002822 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002823 parmp->window_count = 1;
2824 if (parmp->window_count == 0)
2825 parmp->window_count = GARGCOUNT;
2826 if (parmp->window_count > 1)
2827 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002828 // Don't change the windows if there was a command in .vimrc that
2829 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002830 if (parmp->window_layout == 0)
2831 parmp->window_layout = WIN_HOR;
2832 if (parmp->window_layout == WIN_TABS)
2833 {
2834 parmp->window_count = make_tabpages(parmp->window_count);
2835 TIME_MSG("making tab pages");
2836 }
2837 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002838 {
2839 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002840 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002841 TIME_MSG("making windows");
2842 }
2843 else
2844 parmp->window_count = win_count();
2845 }
2846 else
2847 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002848
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002849 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002851 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002852 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002853 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002854 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002855 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002856 }
2857 else
2858 {
2859 /*
2860 * Open a buffer for windows that don't have one yet.
2861 * Commands in the .vimrc might have loaded a file or split the window.
2862 * Watch out for autocommands that delete a window.
2863 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002864 /*
2865 * Don't execute Win/Buf Enter/Leave autocommands here
2866 */
2867 ++autocmd_no_enter;
2868 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002869 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002870 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002872 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002873 {
2874 if (parmp->window_layout == WIN_TABS)
2875 goto_tabpage(1);
2876 else
2877 curwin = firstwin;
2878 }
2879 else if (parmp->window_layout == WIN_TABS)
2880 {
2881 if (curtab->tp_next == NULL)
2882 break;
2883 goto_tabpage(0);
2884 }
2885 else
2886 {
2887 if (curwin->w_next == NULL)
2888 break;
2889 curwin = curwin->w_next;
2890 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002891 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002892 curbuf = curwin->w_buffer;
2893 if (curbuf->b_ml.ml_mfp == NULL)
2894 {
2895#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002896 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002897 if (p_fdls >= 0)
2898 curwin->w_p_fdl = p_fdls;
2899#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002900 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002902
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002904
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002905 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002906 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002907
Bram Moolenaar84212822006-11-07 21:59:47 +00002908 if (swap_exists_action == SEA_QUIT)
2909 {
2910 if (got_int || only_one_window())
2911 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002912 // abort selected or quit and only one window
2913 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002914 getout(1);
2915 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002916 // We can't close the window, it would disturb what
2917 // happens next. Clear the file name and set the arg
2918 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002919 setfname(curbuf, NULL, NULL, FALSE);
2920 curwin->w_arg_idx = -1;
2921 swap_exists_action = SEA_NONE;
2922 }
2923 else
2924 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002925 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002926 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002927 ui_breakcheck();
2928 if (got_int)
2929 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002930 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002931 break;
2932 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002933 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002934 if (parmp->window_layout == WIN_TABS)
2935 goto_tabpage(1);
2936 else
2937 curwin = firstwin;
2938 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002939 --autocmd_no_enter;
2940 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002941 }
2942}
2943
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002944/*
2945 * If opened more than one window, start editing files in the other
2946 * windows. make_windows() has already opened the windows.
2947 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002948 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002949edit_buffers(
2950 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002951 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002952{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002953 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002954 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002955 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002956 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002957 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002958
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002959 /*
2960 * Don't execute Win/Buf Enter/Leave autocommands here
2961 */
2962 ++autocmd_no_enter;
2963 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002964
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002965 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002966 if (curwin->w_arg_idx == -1)
2967 {
2968 win_close(curwin, TRUE);
2969 advance = FALSE;
2970 }
2971
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002972 arg_idx = 1;
2973 for (i = 1; i < parmp->window_count; ++i)
2974 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002975 if (cwd != NULL)
2976 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002977 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002978 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002979 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002980 ++arg_idx;
2981 win_close(curwin, TRUE);
2982 advance = FALSE;
2983 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002984 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002985
Bram Moolenaar84212822006-11-07 21:59:47 +00002986 if (advance)
2987 {
2988 if (parmp->window_layout == WIN_TABS)
2989 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002990 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002991 break;
2992 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002993 // Temporarily reset 'shm' option to not print fileinfo when
2994 // loading the other buffers. This would overwrite the already
2995 // existing fileinfo for the first tab.
2996 if (i == 1)
2997 {
2998 char buf[100];
2999
3000 p_shm_save = vim_strsave(p_shm);
3001 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003002 set_option_value_give_err((char_u *)"shm",
3003 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003004 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003005 }
3006 else
3007 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003008 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003009 break;
3010 win_enter(curwin->w_next, FALSE);
3011 }
3012 }
3013 advance = TRUE;
3014
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003015 // Only open the file if there is no file in this window yet (that can
3016 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003017 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3018 {
3019 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003020 // Edit file from arg list, if there is one. When "Quit" selected
3021 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003022 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003023 (void)do_ecmd(0, arg_idx < GARGCOUNT
3024 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003025 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003026 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003027 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003028 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003029 if (got_int || only_one_window())
3030 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003031 // abort selected and only one window
3032 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003033 getout(1);
3034 }
3035 win_close(curwin, TRUE);
3036 advance = FALSE;
3037 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003038 if (arg_idx == GARGCOUNT - 1)
3039 arg_had_last = TRUE;
3040 ++arg_idx;
3041 }
3042 ui_breakcheck();
3043 if (got_int)
3044 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003045 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003046 break;
3047 }
3048 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003049
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003050 if (p_shm_save != NULL)
3051 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003052 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003053 vim_free(p_shm_save);
3054 }
3055
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003056 if (parmp->window_layout == WIN_TABS)
3057 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003058 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003059
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003060 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003061 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003062#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003063 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003064 while (win->w_p_pvw)
3065 {
3066 win = win->w_next;
3067 if (win == NULL)
3068 {
3069 win = firstwin;
3070 break;
3071 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003072 }
3073#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003074 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003075
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003076 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003077 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003078 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003079 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003080}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003081
3082/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003083 * Execute the commands from --cmd arguments "cmds[cnt]".
3084 */
3085 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003086exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003087{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003088 char_u **cmds = parmp->pre_commands;
3089 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003090 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003091 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003092
3093 if (cnt > 0)
3094 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003095 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003096 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003097 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003098# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003099 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003100# endif
3101 for (i = 0; i < cnt; ++i)
3102 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003103 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003104 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003105# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003106 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003107# endif
3108 TIME_MSG("--cmd commands");
3109 }
3110}
3111
3112/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003113 * Execute "+", "-c" and "-S" arguments.
3114 */
3115 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003116exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003117{
3118 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003119 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003120
3121 /*
3122 * We start commands on line 0, make "vim +/pat file" match a
3123 * pattern on line 1. But don't move the cursor when an autocommand
3124 * with g`" was used.
3125 */
3126 msg_scroll = TRUE;
3127 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3128 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003129 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003130 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003131#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003132 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003133 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003134#endif
3135 for (i = 0; i < parmp->n_commands; ++i)
3136 {
3137 do_cmdline_cmd(parmp->commands[i]);
3138 if (parmp->cmds_tofree[i])
3139 vim_free(parmp->commands[i]);
3140 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003141 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003142 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003143#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003144 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003145#endif
3146 if (curwin->w_cursor.lnum == 0)
3147 curwin->w_cursor.lnum = 1;
3148
3149 if (!exmode_active)
3150 msg_scroll = FALSE;
3151
3152#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003153 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003154 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003155 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003156#endif
3157 TIME_MSG("executing command arguments");
3158}
3159
3160/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003161 * Source startup scripts.
3162 */
3163 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003164source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003165{
3166 int i;
3167
3168 /*
3169 * For "evim" source evim.vim first of all, so that the user can overrule
3170 * any things he doesn't like.
3171 */
3172 if (parmp->evim_mode)
3173 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003175 TIME_MSG("source evim file");
3176 }
3177
3178 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003179 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003180 * nothing else.
3181 */
3182 if (parmp->use_vimrc != NULL)
3183 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003184 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003185 {
3186 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3187 != OK)
3188 emsg(e_failed_to_source_defaults);
3189 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003190 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003191 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003192 {
3193#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003194 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003195 use_gvimrc = parmp->use_vimrc;
3196#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003197 }
3198 else
3199 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003201 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003202 }
3203 }
3204 else if (!silent_mode)
3205 {
3206#ifdef AMIGA
3207 struct Process *proc = (struct Process *)FindTask(0L);
3208 APTR save_winptr = proc->pr_WindowPtr;
3209
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003210 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003211 proc->pr_WindowPtr = (APTR)-1L;
3212#endif
3213
3214 /*
3215 * Get system wide defaults, if the file name is defined.
3216 */
3217#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003219#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003220#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3222 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003223#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003224
3225 /*
3226 * Try to read initialization commands from the following places:
3227 * - environment variable VIMINIT
3228 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3229 * - second user vimrc file ($VIM/.vimrc for Dos)
3230 * - environment variable EXINIT
3231 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3232 * - second user exrc file ($VIM/.exrc for Dos)
3233 * The first that exists is used, the rest is ignored.
3234 */
3235 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3236 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3238 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003239#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003240 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003242#endif
3243#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003244 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003246#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003247#ifdef USR_VIMRC_FILE4
3248 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003250#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003251 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3253 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003254#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3256 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003257#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003258 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003259 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003260 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003261 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3262 NULL) == FAIL)
3263 emsg(e_failed_to_source_defaults);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003264 }
3265 }
3266
3267 /*
3268 * Read initialization commands from ".vimrc" or ".exrc" in current
3269 * directory. This is only done if the 'exrc' option is set.
3270 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003271 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003272 * option has been reset in environment of global ".exrc" or ".vimrc".
3273 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3274 * SYS_VIMRC_FILE.
3275 */
3276 if (p_exrc)
3277 {
3278#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003279 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003280 if (!file_owned(VIMRC_FILE))
3281#endif
3282 secure = p_secure;
3283
3284 i = FAIL;
3285 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003286 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003287#ifdef USR_VIMRC_FILE2
3288 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003289 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003290#endif
3291#ifdef USR_VIMRC_FILE3
3292 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003293 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003294#endif
3295#ifdef SYS_VIMRC_FILE
3296 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003297 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003298#endif
3299 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003301
3302 if (i == FAIL)
3303 {
3304#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003305 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003306 if (!file_owned(EXRC_FILE))
3307 secure = p_secure;
3308 else
3309 secure = 0;
3310#endif
3311 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003312 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003313#ifdef USR_EXRC_FILE2
3314 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003315 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003316#endif
3317 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 (void)do_source((char_u *)EXRC_FILE, FALSE,
3319 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003320 }
3321 }
3322 if (secure == 2)
3323 need_wait_return = TRUE;
3324 secure = 0;
3325#ifdef AMIGA
3326 proc->pr_WindowPtr = save_winptr;
3327#endif
3328 }
3329 TIME_MSG("sourcing vimrc file(s)");
3330}
3331
3332/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333 * Setup to start using the GUI. Exit with an error when not available.
3334 */
3335 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003336main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003337{
3338#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003339 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003341 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003342 mch_errmsg("\n");
3343 mch_exit(2);
3344#endif
3345}
3346
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003347#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003348
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003351 * Returns FAIL if the environment variable was not executed, OK otherwise.
3352 */
3353 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003354process_env(
3355 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003356 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003357{
3358 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003359 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003360
Bram Moolenaare31ee862020-01-07 20:59:34 +01003361 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003362
3363 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3364 {
3365 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003366 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003367 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003368 ESTACK_CHECK_SETUP
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003369 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003370 current_sctx.sc_version = 1;
3371#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003372 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003373 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003374 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003376
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003377 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003378
Bram Moolenaare31ee862020-01-07 20:59:34 +01003379 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003380 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003381 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003382 return OK;
3383 }
3384 return FAIL;
3385}
3386
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003387#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388/*
3389 * Return TRUE if we are certain the user owns the file "fname".
3390 * Used for ".vimrc" and ".exrc".
3391 * Use both stat() and lstat() for extra security.
3392 */
3393 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003394file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003396 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397# ifdef UNIX
3398 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003399# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400 uid_t uid = ((getgid() << 16) | getuid());
3401# endif
3402
3403 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3404# ifdef HAVE_LSTAT
3405 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3406# endif
3407 );
3408}
3409#endif
3410
3411/*
3412 * Give an error message main_errors["n"] and exit.
3413 */
3414 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003415mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003416 int n, // one of the ME_ defines
3417 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003419#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003420 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421#endif
3422
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003423 // If this is a Windows GUI executable, show an error dialog box.
3424#ifdef VIMDLL
3425 gui.in_use = mch_is_gui_executable();
3426#endif
3427#ifdef FEAT_GUI_MSWIN
3428 gui.starting = FALSE; // Needed to show as error.
3429#endif
3430
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003431 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003433 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003434 mch_errmsg(_(main_errors[n]));
3435 if (str != NULL)
3436 {
3437 mch_errmsg(": \"");
3438 mch_errmsg((char *)str);
3439 mch_errmsg("\"");
3440 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003441 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442
3443 mch_exit(1);
3444}
3445
3446 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003447mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003448{
3449 mainerr(ME_ARG_MISSING, str);
3450}
3451
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003452#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003453/*
3454 * print a message with three spaces prepended and '\n' appended.
3455 */
3456 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003457main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003458{
3459 mch_msg(" ");
3460 mch_msg(s);
3461 mch_msg("\n");
3462}
3463
3464/*
3465 * Print messages for "vim -h" or "vim --help" and exit.
3466 */
3467 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003468usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003469{
3470 int i;
3471 static char *(use[]) =
3472 {
3473 N_("[file ..] edit specified file(s)"),
3474 N_("- read text from stdin"),
3475 N_("-t tag edit file where tag is defined"),
3476#ifdef FEAT_QUICKFIX
3477 N_("-q [errorfile] edit file with first error")
3478#endif
3479 };
3480
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003481#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003482 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003483#endif
3484
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003485 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003486 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003487 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003488 for (i = 0; ; ++i)
3489 {
3490 mch_msg(_(" vim [arguments] "));
3491 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003492 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003493 break;
3494 mch_msg(_("\n or:"));
3495 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003496#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003497 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003498#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499
3500 mch_msg(_("\n\nArguments:\n"));
3501 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003502#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003503 main_msg(_("--literal\t\tDon't expand wildcards"));
3504#endif
3505#ifdef FEAT_OLE
3506 main_msg(_("-register\t\tRegister this gvim for OLE"));
3507 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3508#endif
3509#ifdef FEAT_GUI
3510 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3511 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3512#endif
3513 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3514 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003515 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003516 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3517#ifdef FEAT_DIFF
3518 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3519#endif
3520 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3521 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3522 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3523 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3524 main_msg(_("-M\t\t\tModifications in text not allowed"));
3525 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003526 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3528 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003529 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3530#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003531 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003532#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003533 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3534 main_msg(_("-r\t\t\tList swap files and exit"));
3535 main_msg(_("-r (with file name)\tRecover crashed session"));
3536 main_msg(_("-L\t\t\tSame as -r"));
3537#ifdef AMIGA
3538 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3539 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3540#endif
3541#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003542 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003543#endif
3544#ifdef FEAT_RIGHTLEFT
3545 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3546#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003547 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003548 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003549#ifdef FEAT_GUI
3550 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3551#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003552 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003553 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3554#ifdef FEAT_GUI
3555 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3556#endif
3557 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003558 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003559 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3560 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3561 main_msg(_("+\t\t\tStart at end of file"));
3562 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003564 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3565 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3566 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3567 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3568 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3569#ifdef FEAT_CRYPT
3570 main_msg(_("-x\t\t\tEdit encrypted files"));
3571#endif
3572#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3573# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003574 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003575# endif
3576 main_msg(_("-X\t\t\tDo not connect to X server"));
3577#endif
3578#ifdef FEAT_CLIENTSERVER
3579 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3580 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3581 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3582 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003583 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003584 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3585 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3586 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3587 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3588#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003589#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003590 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003591#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003592#ifdef FEAT_JOB_CHANNEL
K.Takata0fd7be72022-11-09 16:29:24 +00003593 main_msg(_("--log <file>\t\tStart logging to <file> early"));
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003594#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003595#ifdef FEAT_VIMINFO
3596 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3597#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003598 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003599 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3600 main_msg(_("--version\t\tPrint version information and exit"));
3601
3602#ifdef FEAT_GUI_X11
3603# ifdef FEAT_GUI_MOTIF
3604 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003605# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003606 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003607 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003608 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3609 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3610 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3611 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3612 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3613 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3614 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3615 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003616 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3617 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3618 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003619#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003620#ifdef FEAT_GUI_GTK
3621 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003622 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3623 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003624 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3625 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003626 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003627 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003628 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003629 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003630 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003631 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003632#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003633#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003634# ifdef VIMDLL
3635 if (gui.starting)
3636# endif
3637 {
3638 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3639 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3640 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003641#endif
3642
3643#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003644 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003645 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003646 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003647 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003648 gui.dofork = FALSE;
3649 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003650 else
3651#endif
3652 mch_exit(0);
3653}
3654
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003655/*
3656 * Check the result of the ATTENTION dialog:
3657 * When "Quit" selected, exit Vim.
3658 * When "Recover" selected, recover the file.
3659 */
3660 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003661check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003662{
3663 if (swap_exists_action == SEA_QUIT)
3664 getout(1);
3665 handle_swap_exists(NULL);
3666}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003667
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003668#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003669
Bram Moolenaar595297d2017-03-04 19:11:12 +01003670#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003671 static void
3672set_progpath(char_u *argv0)
3673{
3674 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003675
Bram Moolenaar4f974752019-02-17 17:44:42 +01003676# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003677 // A relative path containing a "/" will become invalid when using ":cd",
3678 // turn it into a full path.
3679 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3680 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003681 char_u *path = NULL;
3682
3683 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3684 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003685# else
3686 char_u buf[MAXPATHL + 1];
3687# ifdef PROC_EXE_LINK
3688 char linkbuf[MAXPATHL + 1];
3689 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003690
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003691 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3692 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003693 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003694 linkbuf[len] = NUL;
3695 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003696 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003697# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003698
3699 if (!mch_isFullName(val))
3700 {
3701 if (gettail(val) != val
3702 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3703 val = buf;
3704 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003705# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003706
Bram Moolenaar08cab962017-03-04 14:37:18 +01003707 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003708
Bram Moolenaar4f974752019-02-17 17:44:42 +01003709# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003710 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003711# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003712}
3713
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003714#endif // NO_VIM_MAIN