blob: 477f3d619458aedcf65add15eca6f670beef01a9 [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 Moolenaar733a69b2022-12-01 12:03:47 +00001136# ifdef FEAT_EVAL
Bram Moolenaard103ee72019-09-18 21:15:31 +02001137 if (was_safe)
1138 ch_log(NULL, "SafeState: undo reset");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001139# endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001140 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001141 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001142 {
Bram Moolenaar733a69b2022-12-01 12:03:47 +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 Moolenaar733a69b2022-12-01 12:03:47 +00001154# 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 Moolenaar733a69b2022-12-01 12:03:47 +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 Moolenaar733a69b2022-12-01 12:03:47 +00001161# 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
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001165/*
1166 * Return TRUE if there is any typeahead, pending operator or command.
1167 */
1168 int
1169work_pending(void)
1170{
1171 return op_pending() || !is_safe_now();
1172}
1173
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001174
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001175/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001176 * Main loop: Execute Normal mode commands until exiting Vim.
1177 * Also used to handle commands in the command-line window, until the window
1178 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 * Also used to handle ":visual" command after ":global": execute Normal mode
1180 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001181 */
1182 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001183main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001184 int cmdwin, // TRUE when working in the command-line window
1185 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001186{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001187 oparg_T oa; // operator arguments
1188 oparg_T *prev_oap; // operator arguments
1189 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001190#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001191 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001192 static linenr_T conceal_old_cursor_line = 0;
1193 static linenr_T conceal_new_cursor_line = 0;
1194 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001195#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001196
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001197 prev_oap = current_oap;
1198 current_oap = &oa;
1199
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001200#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001201 // Setup to catch a terminating error from the X server. Just ignore
1202 // it, restore the state and continue. This might not always work
1203 // properly, but at least we don't exit unexpectedly when the X server
1204 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001206 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001207 State = MODE_NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001208 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001209 got_int = TRUE;
1210 need_wait_return = FALSE;
1211 global_busy = FALSE;
1212 exmode_active = 0;
1213 skip_redraw = FALSE;
1214 RedrawingDisabled = 0;
1215 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001216 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001217# ifdef FEAT_EVAL
1218 emsg_skip = 0;
1219# endif
1220 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001221 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001222 settmode(TMODE_RAW);
1223 starttermcap();
1224 scroll_start();
1225 redraw_later_clear();
1226 }
1227#endif
1228
1229 clear_oparg(&oa);
Martin Tournoij7904fa42022-10-04 16:28:45 +01001230 while (!cmdwin || cmdwin_result == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001231 {
1232 if (stuff_empty())
1233 {
1234 did_check_timestamps = FALSE;
1235 if (need_check_timestamps)
1236 check_timestamps(FALSE);
Bram Moolenaar13608d82022-08-29 15:06:50 +01001237 if (need_wait_return) // if wait_return() still needed ...
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001238 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001239 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001240 {
1241 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001242 stuffReadbuff((char_u *)"i"); // start insert mode next
1243 // skip the fileinfo message now, because it would be shown
1244 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001245 need_fileinfo = FALSE;
1246 }
1247 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001248
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001249 // Reset "got_int" now that we got back to the main loop. Except when
1250 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1251 // the ":g" command.
1252 // For ":g/pat/vi" we reset "got_int" when used once. When used
1253 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001254 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001255 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001256 if (noexmode && global_busy && !exmode_active && previous_got_int)
1257 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001258 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1259 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001260 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001261 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001262 }
1263 else if (!global_busy || !exmode_active)
1264 {
1265 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001266 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001267 got_int = FALSE;
1268 }
1269 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001270 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001271 else
1272 previous_got_int = FALSE;
1273
Bram Moolenaar069613c2022-01-15 15:23:44 +00001274#ifdef FEAT_EVAL
1275 // At the toplevel there is no exception handling. Discard any that
1276 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1277 if (did_throw && !ex_normal_busy)
1278 discard_current_exception();
1279#endif
1280
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001281 if (!exmode_active)
1282 msg_scroll = FALSE;
1283 quit_more = FALSE;
1284
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001285 // it's not safe unless may_trigger_safestate_main() is called
1286 was_safe = FALSE;
1287
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001288 /*
1289 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1290 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1291 * update cursor and redraw.
1292 */
1293 if (skip_redraw || exmode_active)
1294 skip_redraw = FALSE;
1295 else if (do_redraw || stuff_empty())
1296 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001297#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001298 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001299 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001300#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001301#ifdef HAVE_DROP_FILE
1302 // If files were dropped while text was locked or the curbuf was
1303 // locked, this would be a good time to handle the drop.
1304 handle_any_postponed_drop();
1305#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001306#ifdef FEAT_CONCEAL
1307 if (curwin->w_p_cole == 0)
1308 conceal_update_lines = FALSE;
1309#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001310
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001311 // Trigger CursorMoved if the cursor moved.
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001312 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001313#ifdef FEAT_PROP_POPUP
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001314 || popup_visible
Bram Moolenaar3397f742019-06-02 18:40:06 +02001315#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001316#ifdef FEAT_CONCEAL
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001317 || curwin->w_p_cole > 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001318#endif
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001319 )
1320 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001321 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001322 if (has_cursormoved())
1323 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1324 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001325#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001326 if (popup_visible)
1327 popup_check_cursor_pos();
1328#endif
1329#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001330 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001331 {
1332 conceal_old_cursor_line = last_cursormoved.lnum;
1333 conceal_new_cursor_line = curwin->w_cursor.lnum;
1334 conceal_update_lines = TRUE;
1335 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001336#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001337 last_cursormoved = curwin->w_cursor;
1338 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001339
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001340#if defined(FEAT_CONCEAL)
1341 if (conceal_update_lines
1342 && (conceal_old_cursor_line != conceal_new_cursor_line
1343 || conceal_cursor_line(curwin)
1344 || need_cursor_line_redraw))
1345 {
1346 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001347 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001348 && conceal_old_cursor_line
1349 <= curbuf->b_ml.ml_line_count)
1350 redrawWinline(curwin, conceal_old_cursor_line);
1351 redrawWinline(curwin, conceal_new_cursor_line);
1352 curwin->w_valid &= ~VALID_CROW;
1353 need_cursor_line_redraw = FALSE;
1354 }
1355#endif
1356
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001357 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001358 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001359 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001360 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001361 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1362 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001363 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001364
LemonBoy09371822022-04-08 15:18:45 +01001365 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1366 // before triggering a WinScrolled autocommand.
1367 update_topline();
1368 validate_cursor();
1369
1370 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001371 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01001372
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001373 // If nothing is pending and we are going to wait for the user to
1374 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001375 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001376
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001377#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001378 // Updating diffs from changed() does not always work properly,
1379 // esp. updating folds. Do an update just before redrawing if
1380 // needed.
1381 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1382 {
1383 ex_diffupdate(NULL);
1384 curtab->tp_diff_update = FALSE;
1385 }
1386
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001387 // Scroll-binding for diff mode may have been postponed until
1388 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001389 if (diff_need_scrollbind)
1390 {
1391 check_scrollbind((linenr_T)0, 0L);
1392 diff_need_scrollbind = FALSE;
1393 }
1394#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001395#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001396 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001397 foldAdjustVisual();
1398#endif
1399#ifdef FEAT_FOLDING
1400 /*
1401 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1402 * contain the cursor.
1403 * When 'foldopen' is "all", open the fold(s) under the cursor.
1404 * This may mark the window for redrawing.
1405 */
1406 if (hasAnyFolding(curwin) && !char_avail())
1407 {
1408 foldCheckClose();
1409 if (fdo_flags & FDO_ALL)
1410 foldOpenCursor();
1411 }
1412#endif
1413
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001414 // Before redrawing, make sure w_topline is correct, and w_leftcol
1415 // if lines don't wrap, and w_skipcol if lines wrap.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001416 update_topline();
1417 validate_cursor();
1418
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001419 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001420 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001421 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001422 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001423 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001424 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001425 mch_enable_flush();
1426 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001427 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001428 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001429 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001430 if (need_maketitle)
1431 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001432#ifdef FEAT_VIMINFO
1433 curbuf->b_last_used = vim_time();
1434#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001435 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001436 if (keep_msg != NULL)
1437 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001438 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001439
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001440 if (p != NULL)
1441 {
1442 // msg_start() will set keep_msg to NULL, make a copy
1443 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1444 // check for duplicates. Never put this message in
1445 // history.
1446 msg_hist_off = TRUE;
1447 msg_attr((char *)p, keep_msg_attr);
1448 msg_hist_off = FALSE;
1449 vim_free(p);
1450 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001451 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001452 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001453 {
1454 fileinfo(FALSE, TRUE, FALSE);
1455 need_fileinfo = FALSE;
1456 }
1457
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001458 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001459 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001460 msg_didany = FALSE; // reset lines_left in msg_start()
1461 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001462 showruler(FALSE);
1463
1464 setcursor();
1465 cursor_on();
1466
1467 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001468
1469#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001470 // Now that we have drawn the first screen all the startup stuff
1471 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001472 if (time_fd != NULL)
1473 {
1474 TIME_MSG("first screen update");
1475 TIME_MSG("--- VIM STARTED ---");
1476 fclose(time_fd);
1477 time_fd = NULL;
1478 }
1479#endif
Bram Moolenaar0a60f792022-11-19 21:18:11 +00001480 // After the first screen update may start triggering WinScrolled
1481 // autocmd events. Store all the scroll positions and sizes now.
1482 may_make_initial_scroll_size_snapshot();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001483 }
1484#ifdef FEAT_GUI
1485 if (need_mouse_correct)
1486 gui_mouse_correct();
1487#endif
1488
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001489 // May request the keyboard protocol state now.
1490 may_send_t_RK();
1491
1492 // Update w_curswant if w_set_curswant has been set.
1493 // Postponed until here to avoid computing w_virtcol too often.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001494 update_curswant();
1495
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001496#ifdef FEAT_EVAL
1497 /*
1498 * May perform garbage collection when waiting for a character, but
1499 * only at the very toplevel. Otherwise we may be using a List or
1500 * Dict internally somewhere.
1501 * "may_garbage_collect" is reset in vgetc() which is invoked through
1502 * do_exmode() and normal_cmd().
1503 */
1504 may_garbage_collect = (!cmdwin && !noexmode);
1505#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001506 /*
1507 * If we're invoked as ex, do a round of ex commands.
1508 * Otherwise, get and execute a normal mode command.
1509 */
1510 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001511 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001512 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001513 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001514 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001515 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001516 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001517 {
1518#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001519 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001520 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001521 && !VIsual_active
1522 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001523 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001524 // If terminal_loop() returns OK we got a key that is handled
1525 // in Normal model. With FAIL we first need to position the
1526 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001527 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001528 normal_cmd(&oa, TRUE);
1529 }
1530 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001531#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001532 {
1533#ifdef FEAT_TERMINAL
1534 skip_term_loop = FALSE;
1535#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001536 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001537 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001538 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001539 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001540
1541theend:
1542 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001543}
1544
1545
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001546#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001547/*
1548 * Exit, but leave behind swap files for modified buffers.
1549 */
1550 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001551getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001552{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001553# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001554 // Ignore SIGHUP, because a dropped connection causes a read error, which
1555 // makes Vim exit and then handling SIGHUP causes various reentrance
1556 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001557 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001558# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001559
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001560 ml_close_notmod(); // close all not-modified buffers
1561 ml_sync_all(FALSE, FALSE); // preserve all swap files
1562 ml_close_all(FALSE); // close all memfiles, without deleting
1563 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001564}
1565#endif
1566
1567
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001568/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001569 * Exit properly. This is the only way to exit Vim after startup has
1570 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001571 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001572 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001573getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001574{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001575 exiting = TRUE;
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001576#if defined(FEAT_EVAL)
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001577 ch_log(NULL, "Exiting...");
1578#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001579
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001580 // When running in Ex mode an error causes us to exit with a non-zero exit
1581 // code. POSIX requires this, although it's not 100% clear from the
1582 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001583 if (exmode_active)
1584 exitval += ex_exitval;
1585
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001586#ifdef FEAT_EVAL
1587 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1588 set_vim_var_nr(VV_EXITING, exitval);
1589#endif
1590
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001591 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001592 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001593 windgoto((int)Rows - 1, 0);
1594
Bram Moolenaar58779852022-09-06 18:31:14 +01001595#ifdef FEAT_EVAL
1596 // Invoked all deferred functions in the function stack.
1597 invoke_all_defer();
1598#endif
1599
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001600#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001601 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001602 hash_debug_results();
1603#endif
1604
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001605#ifdef FEAT_GUI
1606 msg_didany = FALSE;
1607#endif
1608
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001609 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001610 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001611 tabpage_T *tp;
1612 tabpage_T *next_tp;
1613 buf_T *buf;
1614 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001615 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001616
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001617 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001618 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001619 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001620 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001621 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001622 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001623 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001624 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001625 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001626 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001627 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001628 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001629 bufref_T bufref;
1630
1631 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001632 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1633 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001634 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001635 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001636
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001637 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001638 next_tp = first_tabpage;
1639 break;
1640 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001641 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001642 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001643
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001644 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001645 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001646 if (buf->b_ml.ml_mfp != NULL)
1647 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001648 bufref_T bufref;
1649
1650 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001651 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001652 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001653 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001654 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001655 break;
1656 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001657
1658 // deathtrap() blocks autocommands, but we do want to trigger
1659 // VimLeavePre.
1660 if (is_autocmd_blocked())
1661 {
1662 unblock_autocmds();
1663 ++unblock;
1664 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001665 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001666 if (unblock)
1667 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001668 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001669
1670#ifdef FEAT_VIMINFO
1671 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001672 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001673 write_viminfo(NULL, FALSE);
1674#endif
1675
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001676 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001677 {
1678 int unblock = 0;
1679
1680 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1681 if (is_autocmd_blocked())
1682 {
1683 unblock_autocmds();
1684 ++unblock;
1685 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001686 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001687 if (unblock)
1688 block_autocmds();
1689 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001690
Bram Moolenaar05159a02005-02-26 23:04:13 +00001691#ifdef FEAT_PROFILE
1692 profile_dump();
1693#endif
1694
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001695 if (did_emsg
1696#ifdef FEAT_GUI
1697 || (gui.in_use && msg_didany && p_verbose > 0)
1698#endif
1699 )
1700 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001701 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001702 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001703 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001704 }
1705
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001706 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001707 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001708 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001709
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001710#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001711 job_stop_on_exit();
1712#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001713#ifdef FEAT_LUA
1714 lua_end();
1715#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001716#ifdef FEAT_MZSCHEME
1717 mzscheme_end();
1718#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001719#ifdef FEAT_TCL
1720 tcl_end();
1721#endif
1722#ifdef FEAT_RUBY
1723 ruby_end();
1724#endif
1725#ifdef FEAT_PYTHON
1726 python_end();
1727#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001728#ifdef FEAT_PYTHON3
1729 python3_end();
1730#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001731#ifdef FEAT_PERL
1732 perl_end();
1733#endif
1734#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1735 iconv_end();
1736#endif
1737#ifdef FEAT_NETBEANS_INTG
1738 netbeans_end();
1739#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001740#ifdef FEAT_CSCOPE
1741 cs_end();
1742#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001743#ifdef FEAT_EVAL
1744 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001745 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001746#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001747#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001748 free_cmd_argsW();
1749#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001750
1751 mch_exit(exitval);
1752}
1753
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001754/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001755 * Get the name of the display, before gui_prepare() removes it from
1756 * argv[]. Used for the xterm-clipboard display.
1757 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001758 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001759 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001760 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001761early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001762{
Bram Moolenaar03005972008-11-20 13:12:36 +00001763#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1764 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001765 int argc = parmp->argc;
1766 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001767 int i;
1768
1769 for (i = 1; i < argc; i++)
1770 {
1771 if (STRCMP(argv[i], "--") == 0)
1772 break;
1773# ifdef FEAT_XCLIPBOARD
1774 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001775# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001776 || STRICMP(argv[i], "--display") == 0
1777# endif
1778 )
1779 {
1780 if (i == argc - 1)
1781 mainerr_arg_missing((char_u *)argv[i]);
1782 xterm_display = argv[++i];
1783 }
1784# endif
1785# ifdef FEAT_CLIENTSERVER
1786 else if (STRICMP(argv[i], "--servername") == 0)
1787 {
1788 if (i == argc - 1)
1789 mainerr_arg_missing((char_u *)argv[i]);
1790 parmp->serverName_arg = (char_u *)argv[++i];
1791 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001792 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001793 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001794 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001795 {
1796 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001797# ifdef FEAT_GUI
1798 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001799 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001800 gui.dofork = FALSE;
1801# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001802 }
1803# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001804
Bram Moolenaar4f974752019-02-17 17:44:42 +01001805# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1806# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001807 else if (STRICMP(argv[i], "--windowid") == 0)
1808# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001809 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001810# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001811 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001812 long_u id;
1813 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001814
1815 if (i == argc - 1)
1816 mainerr_arg_missing((char_u *)argv[i]);
1817 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001818 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001819 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001820 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001821 if (count != 1)
1822 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1823 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001824# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001825 win_socket_id = id;
1826# else
1827 gtk_socket_id = id;
1828# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001829 i++;
1830 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001831# endif
1832# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001833 else if (STRICMP(argv[i], "--echo-wid") == 0)
1834 echo_wid_arg = TRUE;
1835# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001836# ifndef FEAT_NETBEANS_INTG
1837 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001838 {
1839 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1840 mch_exit(2);
1841 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001842# endif
1843
Bram Moolenaar58d98232005-07-23 22:25:46 +00001844 }
1845#endif
1846}
1847
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001848#ifndef NO_VIM_MAIN
1849/*
1850 * Get a (optional) count for a Vim argument.
1851 */
1852 static int
1853get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001854 char_u *p, // pointer to argument
1855 int *idx, // index in argument, is incremented
1856 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001857{
1858 if (vim_isdigit(p[*idx]))
1859 {
1860 def = atoi((char *)&(p[*idx]));
1861 while (vim_isdigit(p[*idx]))
1862 *idx = *idx + 1;
1863 }
1864 return def;
1865}
1866
1867/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001868 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001869 * If the executable name starts with "r" we disable shell commands.
1870 * If the next character is "e" we run in Easy mode.
1871 * If the next character is "g" we run the GUI version.
1872 * If the next characters are "view" we start in readonly mode.
1873 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1874 * If the next characters are "ex" we start in Ex mode. If it's followed
1875 * by "im" use improved Ex mode.
1876 */
1877 static void
1878parse_command_name(mparm_T *parmp)
1879{
1880 char_u *initstr;
1881
1882 initstr = gettail((char_u *)parmp->argv[0]);
1883
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001884#ifdef FEAT_EVAL
1885 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001886 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001887#endif
1888
1889 if (TOLOWER_ASC(initstr[0]) == 'r')
1890 {
1891 restricted = TRUE;
1892 ++initstr;
1893 }
1894
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001895 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001896 if (TOLOWER_ASC(initstr[0]) == 'e'
1897 && (TOLOWER_ASC(initstr[1]) == 'v'
1898 || TOLOWER_ASC(initstr[1]) == 'g'))
1899 {
1900#ifdef FEAT_GUI
1901 gui.starting = TRUE;
1902#endif
1903 parmp->evim_mode = TRUE;
1904 ++initstr;
1905 }
1906
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001907 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001908 if (TOLOWER_ASC(initstr[0]) == 'g')
1909 {
1910 main_start_gui();
1911#ifdef FEAT_GUI
1912 ++initstr;
1913#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001914#ifdef GUI_MAY_SPAWN
1915 gui.dospawn = FALSE; // No need to spawn a new process.
1916#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001917 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001918#ifdef GUI_MAY_SPAWN
1919 else
1920 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1921#endif
1922
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001923
1924 if (STRNICMP(initstr, "view", 4) == 0)
1925 {
1926 readonlymode = TRUE;
1927 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001928 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001929 initstr += 4;
1930 }
1931 else if (STRNICMP(initstr, "vim", 3) == 0)
1932 initstr += 3;
1933
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001934 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001935 if (STRICMP(initstr, "diff") == 0)
1936 {
1937#ifdef FEAT_DIFF
1938 parmp->diff_mode = TRUE;
1939#else
1940 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1941 mch_errmsg("\n");
1942 mch_exit(2);
1943#endif
1944 }
1945
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001946 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001947 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001948 if (STRNICMP(initstr, "ex", 2) == 0)
1949 {
1950 if (STRNICMP(initstr + 2, "im", 2) == 0)
1951 exmode_active = EXMODE_VIM;
1952 else
1953 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001954 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001955 }
1956}
1957
Bram Moolenaar58d98232005-07-23 22:25:46 +00001958/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001959 * Scan the command line arguments.
1960 */
1961 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001962command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001963{
1964 int argc = parmp->argc;
1965 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001966 int argv_idx; // index in argv[n][]
1967 int had_minmin = FALSE; // found "--" argument
1968 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001969 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001970 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001971 long n;
1972
1973 --argc;
1974 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001975 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001976 while (argc > 0)
1977 {
1978 /*
1979 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1980 */
1981 if (argv[0][0] == '+' && !had_minmin)
1982 {
1983 if (parmp->n_commands >= MAX_ARG_CMDS)
1984 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001985 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001986 if (argv[0][1] == NUL)
1987 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1988 else
1989 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1990 }
1991
1992 /*
1993 * Optional argument.
1994 */
1995 else if (argv[0][0] == '-' && !had_minmin)
1996 {
1997 want_argument = FALSE;
1998 c = argv[0][argv_idx++];
1999#ifdef VMS
2000 /*
2001 * VMS only uses upper case command lines. Interpret "-X" as "-x"
2002 * and "-/X" as "-X".
2003 */
2004 if (c == '/')
2005 {
2006 c = argv[0][argv_idx++];
2007 c = TOUPPER_ASC(c);
2008 }
2009 else
2010 c = TOLOWER_ASC(c);
2011#endif
2012 switch (c)
2013 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002014 case NUL: // "vim -" read from stdin
2015 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002016 if (exmode_active)
2017 silent_mode = TRUE;
2018 else
2019 {
2020 if (parmp->edit_type != EDIT_NONE)
2021 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2022 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002023 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002024 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002025 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002026 break;
2027
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002028 case '-': // "--" don't take any more option arguments
2029 // "--help" give help message
2030 // "--version" give version message
2031 // "--clean" clean context
2032 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002033 // "--startuptime fname" write timing info
2034 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002035 // "--nofork" don't fork
2036 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002037 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002038 // "--ttyfail" exit if not a term
2039 // "--noplugin[s]" skip plugins
2040 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002041 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2042 usage();
2043 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2044 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002045 Columns = 80; // need to init Columns
2046 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002047#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002048 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002049#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002050 list_version();
2051 msg_putchar('\n');
2052 msg_didout = FALSE;
2053 mch_exit(0);
2054 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002055 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2056 {
2057 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002058#ifdef FEAT_GUI
2059 use_gvimrc = (char_u *)"NONE";
2060#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002061 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002062 set_option_value_give_err((char_u *)"vif",
2063 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002064 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002065 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2066 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002067#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002068 parmp->literal = TRUE;
2069#endif
2070 }
2071 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2072 {
2073#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002074 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002075#endif
2076 }
2077 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2078 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002079 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2080 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002081 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2082 == 0)
2083 {
2084 want_argument = TRUE;
2085 argv_idx += 15;
2086 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002087 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2088 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002089 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2090 {
2091 want_argument = TRUE;
2092 argv_idx += 3;
2093 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002094 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2095 {
2096 want_argument = TRUE;
2097 argv_idx += 11;
2098 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002099 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2100 {
2101 want_argument = TRUE;
2102 argv_idx += 3;
2103 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002104#ifdef FEAT_CLIENTSERVER
2105 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002106 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002107 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2108 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2109 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002110 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002111 if (argc > 1)
2112 {
2113 --argc;
2114 ++argv;
2115 }
2116 }
2117#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002118#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002119# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002120 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002121# else
2122 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2123# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002124 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002125 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002126 if (argc > 1)
2127 {
2128 --argc;
2129 ++argv;
2130 }
2131 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002132#endif
2133#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002134 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2135 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002136 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002137 }
2138#endif
2139 else
2140 {
2141 if (argv[0][argv_idx])
2142 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2143 had_minmin = TRUE;
2144 }
2145 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002146 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002147 break;
2148
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002149 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002150#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002151 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002152#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002153 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002154 mch_exit(2);
2155#endif
2156 break;
2157
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002158 case 'b': // "-b" binary mode
2159 // Needs to be effective before expanding file names, because
2160 // for Win32 this makes us edit a shortcut file itself,
2161 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002162 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002163 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002164 break;
2165
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002166 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002167 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002168 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002169 break;
2170
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002171 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002172 exmode_active = EXMODE_NORMAL;
2173 break;
2174
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002175 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002176 exmode_active = EXMODE_VIM;
2177 break;
2178
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002179 case 'f': // "-f" GUI: run in foreground. Amiga: open
2180 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002181#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002182 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002183#endif
2184 break;
2185
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002186 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002187 main_start_gui();
2188 break;
2189
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002190 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002191 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002192 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002193 break;
2194
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002195 case '?': // "-?" give help message (for MS-Windows)
2196 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002197#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002198 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002199 gui.starting = FALSE;
2200#endif
2201 usage();
2202 break;
2203
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002204 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002205#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002206 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002207 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002208#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002209 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002210 mch_exit(2);
2211#endif
2212 break;
2213
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002214 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002215 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002216 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002217 break;
2218
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002219 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002220 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002221 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002222
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002223 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002224 p_write = FALSE;
2225 break;
2226
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002227 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002228#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002229 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002230#endif
2231 parmp->evim_mode = TRUE;
2232 break;
2233
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002234 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002235 change_compatible(FALSE);
2236 break;
2237
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002238 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002239#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002240 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002241 if (argv[0][argv_idx] == 'b')
2242 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002243 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002244 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002245 }
2246 else
2247#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002248 parmp->no_swap_file = TRUE;
2249 break;
2250
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002251 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002252#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002253 // For some reason on MacOS X, an argument like:
2254 // -psn_0_10223617 is passed in when invoke from Finder
2255 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002256 if (argv[0][argv_idx] == 's')
2257 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002258 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002259 main_start_gui();
2260 break;
2261 }
2262#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002263 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002264 parmp->window_count = get_number_arg((char_u *)argv[0],
2265 &argv_idx, 0);
2266 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002267 break;
2268
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002269 case 'o': // "-o[N]" open N horizontal split windows
2270 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002271 parmp->window_count = get_number_arg((char_u *)argv[0],
2272 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002273 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002274 break;
2275
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002276 case 'O': // "-O[N]" open N vertical split windows
2277 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002278 parmp->window_count = get_number_arg((char_u *)argv[0],
2279 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002280 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002281 break;
2282
2283#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002284 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002285 if (parmp->edit_type != EDIT_NONE)
2286 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2287 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002288 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002289 {
2290 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2291 argv_idx = -1;
2292 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002293 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002294 want_argument = TRUE;
2295 break;
2296#endif
2297
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002298 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002299 readonlymode = TRUE;
2300 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002301 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002302 break;
2303
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002304 case 'r': // "-r" recovery mode
2305 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002306 recoverymode = 1;
2307 break;
2308
2309 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002310 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002311 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002312 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002313 want_argument = TRUE;
2314 break;
2315
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002316 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002317 if (parmp->edit_type != EDIT_NONE)
2318 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2319 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002320 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002321 {
2322 parmp->tagname = (char_u *)argv[0] + argv_idx;
2323 argv_idx = -1;
2324 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002325 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002326 want_argument = TRUE;
2327 break;
2328
2329#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002330 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002331 parmp->use_debug_break_level = 9999;
2332 break;
2333#endif
2334#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002335 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002336# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002337 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002338 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2339 want_argument = TRUE;
2340 else
2341# endif
2342 parmp->diff_mode = TRUE;
2343 break;
2344#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002345 case 'V': // "-V{N}" Verbose level
2346 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002347 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2348 if (argv[0][argv_idx] != NUL)
2349 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002350 set_option_value_give_err((char_u *)"verbosefile",
2351 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002352 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002353 }
2354 break;
2355
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002356 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002357 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002358#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002359 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002360#endif
2361 break;
2362
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002363 case 'w': // "-w{number}" set window height
2364 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002365 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2366 {
2367 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002368 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002369 break;
2370 }
2371 want_argument = TRUE;
2372 break;
2373
2374#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002375 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002376 parmp->ask_for_key = TRUE;
2377 break;
2378#endif
2379
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002380 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002381#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2382 x_no_connect = TRUE;
2383#endif
2384 break;
2385
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002386 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002387 restricted = TRUE;
2388 break;
2389
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002390 case 'c': // "-c{command}" or "-c {command}" execute
2391 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002392 if (argv[0][argv_idx] != NUL)
2393 {
2394 if (parmp->n_commands >= MAX_ARG_CMDS)
2395 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002396 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2397 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002398 argv_idx = -1;
2399 break;
2400 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002401 // FALLTHROUGH
2402 case 'S': // "-S {file}" execute Vim script
2403 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002404#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002405 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002406#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002407 case 'T': // "-T {terminal}" terminal name
2408 case 'u': // "-u {vimrc}" vim inits file
2409 case 'U': // "-U {gvimrc}" gvim inits file
2410 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002411#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002412 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002413#endif
2414 want_argument = TRUE;
2415 break;
2416
2417 default:
2418 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2419 }
2420
2421 /*
2422 * Handle option arguments with argument.
2423 */
2424 if (want_argument)
2425 {
2426 /*
2427 * Check for garbage immediately after the option letter.
2428 */
2429 if (argv[0][argv_idx] != NUL)
2430 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2431
2432 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002433 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002434 mainerr_arg_missing((char_u *)argv[0]);
2435 ++argv;
2436 argv_idx = -1;
2437
2438 switch (c)
2439 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002440 case 'c': // "-c {command}" execute command
2441 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002442 if (parmp->n_commands >= MAX_ARG_CMDS)
2443 mainerr(ME_EXTRA_CMD, NULL);
2444 if (c == 'S')
2445 {
2446 char *a;
2447
2448 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002449 // "-S" without argument: use default session file
2450 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002451 a = SESSION_FILE;
2452 else if (argv[0][0] == '-')
2453 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002454 // "-S" followed by another option: use default
2455 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456 a = SESSION_FILE;
2457 ++argc;
2458 --argv;
2459 }
2460 else
2461 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002462 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002463 if (p == NULL)
2464 mch_exit(2);
2465 sprintf((char *)p, "so %s", a);
2466 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2467 parmp->commands[parmp->n_commands++] = p;
2468 }
2469 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002470 parmp->commands[parmp->n_commands++] =
2471 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472 break;
2473
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002474 case '-':
2475 if (argv[-1][2] == 'c')
2476 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002477 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002478 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2479 mainerr(ME_EXTRA_CMD, NULL);
2480 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002481 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002482 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002483 // --gui-dialog-file fname
2484 if (argv[-1][2] == 'g')
2485 {
2486 // without GUI ignore the argument
2487#ifdef FEAT_GUI
2488 parmp->gui_dialog_file = (char_u *)argv[0];
2489#endif
2490 }
2491
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002492 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002493 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002494 break;
2495
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002496 // case 'd': -d {device} is handled in mch_check_win() for the
2497 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002498
2499#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002500 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002501 parmp->use_ef = (char_u *)argv[0];
2502 break;
2503#endif
2504
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002505 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002506 set_option_value_give_err((char_u *)"vif",
2507 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508 break;
2509
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002510 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002511 if (scriptin[0] != NULL)
2512 {
2513scripterror:
2514 mch_errmsg(_("Attempt to open script file again: \""));
2515 mch_errmsg(argv[-1]);
2516 mch_errmsg(" ");
2517 mch_errmsg(argv[0]);
2518 mch_errmsg("\"\n");
2519 mch_exit(2);
2520 }
2521 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2522 {
2523 mch_errmsg(_("Cannot open for reading: \""));
2524 mch_errmsg(argv[0]);
2525 mch_errmsg("\"\n");
2526 mch_exit(2);
2527 }
2528 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002529 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002530 break;
2531
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002532 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002533 parmp->tagname = (char_u *)argv[0];
2534 break;
2535
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002536 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 /*
2538 * The -T term argument is always available and when
2539 * HAVE_TERMLIB is supported it overrides the environment
2540 * variable TERM.
2541 */
2542#ifdef FEAT_GUI
2543 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002544 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545 else
2546#endif
2547 parmp->term = (char_u *)argv[0];
2548 break;
2549
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002550 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551 parmp->use_vimrc = (char_u *)argv[0];
2552 break;
2553
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002554 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002555#ifdef FEAT_GUI
2556 use_gvimrc = (char_u *)argv[0];
2557#endif
2558 break;
2559
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002560 case 'w': // "-w {nr}" 'window' value
2561 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562 if (vim_isdigit(*((char_u *)argv[0])))
2563 {
2564 argv_idx = 0;
2565 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002566 set_option_value_give_err((char_u *)"window",
2567 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568 argv_idx = -1;
2569 break;
2570 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002571 // FALLTHROUGH
2572 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573 if (scriptout != NULL)
2574 goto scripterror;
2575 if ((scriptout = mch_fopen(argv[0],
2576 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2577 {
2578 mch_errmsg(_("Cannot open for script output: \""));
2579 mch_errmsg(argv[0]);
2580 mch_errmsg("\"\n");
2581 mch_exit(2);
2582 }
2583 break;
2584
Bram Moolenaar4f974752019-02-17 17:44:42 +01002585#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002586 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587 gui_mch_set_parent(argv[0]);
2588 break;
2589#endif
2590 }
2591 }
2592 }
2593
2594 /*
2595 * File name argument.
2596 */
2597 else
2598 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002599 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002600
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002601 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002602 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2603 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2604 parmp->edit_type = EDIT_FILE;
2605
2606#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002607 // Remember if the argument was a full path before changing
2608 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002609 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2610 parmp->full_path = TRUE;
2611#endif
2612
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002613 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002614 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2615 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2616 mch_exit(2);
2617#ifdef FEAT_DIFF
2618 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2619 && !mch_isdir(alist_name(&GARGLIST[0])))
2620 {
2621 char_u *r;
2622
2623 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2624 if (r != NULL)
2625 {
2626 vim_free(p);
2627 p = r;
2628 }
2629 }
2630#endif
K.Takatab247e062022-02-07 10:45:23 +00002631#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002632 /*
2633 * If vim is invoked by non-Cygwin tools, convert away any
2634 * DOS paths, so things like .swp files are created correctly.
2635 * Look for evidence of non-Cygwin paths before we bother.
2636 * This is only for when using the Unix files.
2637 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002638 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002639 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002640 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002641
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002642# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002643 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002644# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002645 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002646# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002647 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002648 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002649 if (p == NULL)
2650 mch_exit(2);
2651 }
2652#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002653
2654#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002655 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002656 fname_case(p, 0);
2657#endif
2658
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002659 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002660#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002661 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002662#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002663 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664#endif
2665 );
2666
Bram Moolenaar4f974752019-02-17 17:44:42 +01002667#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002668 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002669 // Remember this argument has been added to the argument list.
2670 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002671 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002672# ifdef FEAT_DIFF
2673 parmp->diff_mode
2674# else
2675 FALSE
2676# endif
2677 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002678 }
2679#endif
2680 }
2681
2682 /*
2683 * If there are no more letters after the current "-", go to next
2684 * argument. argv_idx is set to -1 when the current argument is to be
2685 * skipped.
2686 */
2687 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2688 {
2689 --argc;
2690 ++argv;
2691 argv_idx = 1;
2692 }
2693 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002694
2695#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002696 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2697 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002698 if (parmp->n_commands > 0)
2699 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002700 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002701 if (p != NULL)
2702 {
2703 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2704 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2705 vim_free(p);
2706 }
2707 }
2708#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709}
2710
2711/*
2712 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002713 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002714 */
2715 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002716check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002718 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719
2720 input_isatty = mch_input_isatty();
2721 if (exmode_active)
2722 {
2723 if (!input_isatty)
2724 silent_mode = TRUE;
2725 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002726 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002728 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002729 && !gui.starting
2730#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002731 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002732 {
2733#ifdef NBDEBUG
2734 /*
2735 * This shouldn't be necessary. But if I run netbeans with the log
2736 * output coming to the console and XOpenDisplay fails, I get vim
2737 * trying to start with input/output to my console tty. This fills my
2738 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002739 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002740 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002741 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002742 {
2743 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2744 exit(1);
2745 }
2746#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002747#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2748 if (
2749# ifdef VIMDLL
2750 !gui.starting &&
2751# endif
2752 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002753 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002754# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002755 && defined(FEAT_GETTEXT)
2756 char *s, *tofree = NULL;
2757
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002758 // Set the encoding of the error message based on $LC_ALL or
2759 // other environment variables instead of 'encoding'.
2760 // Note that the message is shown on a Cygwin terminal (e.g.
2761 // mintty) which encoding is based on $LC_ALL or etc., not the
2762 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002763 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002764 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002765 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002766 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2767 vim_free(tofree);
2768# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002769 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2770 exit(1);
2771 }
2772#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002773 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2775 if (!input_isatty)
2776 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2777 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002778 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2779 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002780 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002781 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002782 TIME_MSG("Warning delay");
2783 }
2784}
2785
2786/*
2787 * Read text from stdin.
2788 */
2789 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002790read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002791{
2792 int i;
2793
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002794 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002795 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002796
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002797 no_wait_return = TRUE;
2798 i = msg_didany;
2799 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002800
2801 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002802 (void)open_buffer(TRUE, NULL, 0);
2803
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002804 no_wait_return = FALSE;
2805 msg_didany = i;
2806 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002807
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002808 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002809
2810#if !(defined(AMIGA) || defined(MACOS_X))
2811 // Dup stdin from stderr to read commands from, so that shell commands
2812 // work.
2813 // TODO: why is this needed, even though readfile() has done this?
2814 close(0);
2815 vim_ignored = dup(2);
2816#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002817}
2818
2819/*
2820 * Create the requested number of windows and edit buffers in them.
2821 * Also does recovery if "recoverymode" set.
2822 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002823 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002824create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002825{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002826 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002827 int done = 0;
2828
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002829 /*
2830 * Create the number of windows that was requested.
2831 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002832 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002833 parmp->window_count = 1;
2834 if (parmp->window_count == 0)
2835 parmp->window_count = GARGCOUNT;
2836 if (parmp->window_count > 1)
2837 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002838 // Don't change the windows if there was a command in .vimrc that
2839 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002840 if (parmp->window_layout == 0)
2841 parmp->window_layout = WIN_HOR;
2842 if (parmp->window_layout == WIN_TABS)
2843 {
2844 parmp->window_count = make_tabpages(parmp->window_count);
2845 TIME_MSG("making tab pages");
2846 }
2847 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002848 {
2849 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002850 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002851 TIME_MSG("making windows");
2852 }
2853 else
2854 parmp->window_count = win_count();
2855 }
2856 else
2857 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002858
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002859 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002860 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002861 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002862 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002863 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002864 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002865 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002866 }
2867 else
2868 {
2869 /*
2870 * Open a buffer for windows that don't have one yet.
2871 * Commands in the .vimrc might have loaded a file or split the window.
2872 * Watch out for autocommands that delete a window.
2873 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002874 /*
2875 * Don't execute Win/Buf Enter/Leave autocommands here
2876 */
2877 ++autocmd_no_enter;
2878 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002879 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002880 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002881 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002882 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002883 {
2884 if (parmp->window_layout == WIN_TABS)
2885 goto_tabpage(1);
2886 else
2887 curwin = firstwin;
2888 }
2889 else if (parmp->window_layout == WIN_TABS)
2890 {
2891 if (curtab->tp_next == NULL)
2892 break;
2893 goto_tabpage(0);
2894 }
2895 else
2896 {
2897 if (curwin->w_next == NULL)
2898 break;
2899 curwin = curwin->w_next;
2900 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002901 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002902 curbuf = curwin->w_buffer;
2903 if (curbuf->b_ml.ml_mfp == NULL)
2904 {
2905#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002906 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002907 if (p_fdls >= 0)
2908 curwin->w_p_fdl = p_fdls;
2909#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002910 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002911 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002912
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002913 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002914
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002915 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002916 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002917
Bram Moolenaar84212822006-11-07 21:59:47 +00002918 if (swap_exists_action == SEA_QUIT)
2919 {
2920 if (got_int || only_one_window())
2921 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002922 // abort selected or quit and only one window
2923 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002924 getout(1);
2925 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002926 // We can't close the window, it would disturb what
2927 // happens next. Clear the file name and set the arg
2928 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002929 setfname(curbuf, NULL, NULL, FALSE);
2930 curwin->w_arg_idx = -1;
2931 swap_exists_action = SEA_NONE;
2932 }
2933 else
2934 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002935 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002936 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002937 ui_breakcheck();
2938 if (got_int)
2939 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002940 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002941 break;
2942 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002943 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002944 if (parmp->window_layout == WIN_TABS)
2945 goto_tabpage(1);
2946 else
2947 curwin = firstwin;
2948 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002949 --autocmd_no_enter;
2950 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002951 }
2952}
2953
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002954/*
2955 * If opened more than one window, start editing files in the other
2956 * windows. make_windows() has already opened the windows.
2957 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002958 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002959edit_buffers(
2960 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002961 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002962{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002963 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002964 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002965 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002966 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002967 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002968
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002969 /*
2970 * Don't execute Win/Buf Enter/Leave autocommands here
2971 */
2972 ++autocmd_no_enter;
2973 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002974
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002975 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002976 if (curwin->w_arg_idx == -1)
2977 {
2978 win_close(curwin, TRUE);
2979 advance = FALSE;
2980 }
2981
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002982 arg_idx = 1;
2983 for (i = 1; i < parmp->window_count; ++i)
2984 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002985 if (cwd != NULL)
2986 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002987 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002988 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002989 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002990 ++arg_idx;
2991 win_close(curwin, TRUE);
2992 advance = FALSE;
2993 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002994 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002995
Bram Moolenaar84212822006-11-07 21:59:47 +00002996 if (advance)
2997 {
2998 if (parmp->window_layout == WIN_TABS)
2999 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003000 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003001 break;
3002 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003003 // Temporarily reset 'shm' option to not print fileinfo when
3004 // loading the other buffers. This would overwrite the already
3005 // existing fileinfo for the first tab.
3006 if (i == 1)
3007 {
3008 char buf[100];
3009
3010 p_shm_save = vim_strsave(p_shm);
3011 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003012 set_option_value_give_err((char_u *)"shm",
3013 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003014 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003015 }
3016 else
3017 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003018 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003019 break;
3020 win_enter(curwin->w_next, FALSE);
3021 }
3022 }
3023 advance = TRUE;
3024
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003025 // Only open the file if there is no file in this window yet (that can
3026 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003027 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3028 {
3029 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003030 // Edit file from arg list, if there is one. When "Quit" selected
3031 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003032 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003033 (void)do_ecmd(0, arg_idx < GARGCOUNT
3034 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003035 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003036 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003037 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003038 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003039 if (got_int || only_one_window())
3040 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003041 // abort selected and only one window
3042 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003043 getout(1);
3044 }
3045 win_close(curwin, TRUE);
3046 advance = FALSE;
3047 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003048 if (arg_idx == GARGCOUNT - 1)
3049 arg_had_last = TRUE;
3050 ++arg_idx;
3051 }
3052 ui_breakcheck();
3053 if (got_int)
3054 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003055 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003056 break;
3057 }
3058 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003059
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003060 if (p_shm_save != NULL)
3061 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003062 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003063 vim_free(p_shm_save);
3064 }
3065
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003066 if (parmp->window_layout == WIN_TABS)
3067 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003068 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003069
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003070 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003071 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003072#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003073 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003074 while (win->w_p_pvw)
3075 {
3076 win = win->w_next;
3077 if (win == NULL)
3078 {
3079 win = firstwin;
3080 break;
3081 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003082 }
3083#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003084 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003085
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003086 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003087 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003088 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003089 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003090}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003091
3092/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003093 * Execute the commands from --cmd arguments "cmds[cnt]".
3094 */
3095 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003096exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003097{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003098 char_u **cmds = parmp->pre_commands;
3099 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003100 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003101 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003102
3103 if (cnt > 0)
3104 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003105 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003106 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003107 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003108# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003109 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003110# endif
3111 for (i = 0; i < cnt; ++i)
3112 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003113 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003114 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003115# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003116 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003117# endif
3118 TIME_MSG("--cmd commands");
3119 }
3120}
3121
3122/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003123 * Execute "+", "-c" and "-S" arguments.
3124 */
3125 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003126exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003127{
3128 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003129 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003130
3131 /*
3132 * We start commands on line 0, make "vim +/pat file" match a
3133 * pattern on line 1. But don't move the cursor when an autocommand
3134 * with g`" was used.
3135 */
3136 msg_scroll = TRUE;
3137 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3138 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003139 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003140 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003141#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003142 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003143 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003144#endif
3145 for (i = 0; i < parmp->n_commands; ++i)
3146 {
3147 do_cmdline_cmd(parmp->commands[i]);
3148 if (parmp->cmds_tofree[i])
3149 vim_free(parmp->commands[i]);
3150 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003151 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003152 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003153#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003154 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003155#endif
3156 if (curwin->w_cursor.lnum == 0)
3157 curwin->w_cursor.lnum = 1;
3158
3159 if (!exmode_active)
3160 msg_scroll = FALSE;
3161
3162#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003163 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003164 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003165 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003166#endif
3167 TIME_MSG("executing command arguments");
3168}
3169
3170/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003171 * Source startup scripts.
3172 */
3173 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003174source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003175{
3176 int i;
3177
3178 /*
3179 * For "evim" source evim.vim first of all, so that the user can overrule
3180 * any things he doesn't like.
3181 */
3182 if (parmp->evim_mode)
3183 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003185 TIME_MSG("source evim file");
3186 }
3187
3188 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003189 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003190 * nothing else.
3191 */
3192 if (parmp->use_vimrc != NULL)
3193 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003194 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003195 {
3196 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3197 != OK)
3198 emsg(e_failed_to_source_defaults);
3199 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003200 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003201 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003202 {
3203#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003204 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003205 use_gvimrc = parmp->use_vimrc;
3206#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003207 }
3208 else
3209 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003211 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003212 }
3213 }
3214 else if (!silent_mode)
3215 {
3216#ifdef AMIGA
3217 struct Process *proc = (struct Process *)FindTask(0L);
3218 APTR save_winptr = proc->pr_WindowPtr;
3219
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003220 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003221 proc->pr_WindowPtr = (APTR)-1L;
3222#endif
3223
3224 /*
3225 * Get system wide defaults, if the file name is defined.
3226 */
3227#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003229#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003230#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3232 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003233#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003234
3235 /*
3236 * Try to read initialization commands from the following places:
3237 * - environment variable VIMINIT
3238 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3239 * - second user vimrc file ($VIM/.vimrc for Dos)
3240 * - environment variable EXINIT
3241 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3242 * - second user exrc file ($VIM/.exrc for Dos)
3243 * The first that exists is used, the rest is ignored.
3244 */
3245 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3246 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3248 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003249#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003250 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003252#endif
3253#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003254 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003256#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003257#ifdef USR_VIMRC_FILE4
3258 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003260#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003261 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3263 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003264#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3266 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003267#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003268 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003269 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003270 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003271 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3272 NULL) == FAIL)
3273 emsg(e_failed_to_source_defaults);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003274 }
3275 }
3276
3277 /*
3278 * Read initialization commands from ".vimrc" or ".exrc" in current
3279 * directory. This is only done if the 'exrc' option is set.
3280 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003281 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003282 * option has been reset in environment of global ".exrc" or ".vimrc".
3283 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3284 * SYS_VIMRC_FILE.
3285 */
3286 if (p_exrc)
3287 {
3288#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003289 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003290 if (!file_owned(VIMRC_FILE))
3291#endif
3292 secure = p_secure;
3293
3294 i = FAIL;
3295 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003296 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003297#ifdef USR_VIMRC_FILE2
3298 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003299 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003300#endif
3301#ifdef USR_VIMRC_FILE3
3302 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003303 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003304#endif
3305#ifdef SYS_VIMRC_FILE
3306 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003307 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003308#endif
3309 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003311
3312 if (i == FAIL)
3313 {
3314#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003315 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003316 if (!file_owned(EXRC_FILE))
3317 secure = p_secure;
3318 else
3319 secure = 0;
3320#endif
3321 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003322 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003323#ifdef USR_EXRC_FILE2
3324 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003325 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003326#endif
3327 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 (void)do_source((char_u *)EXRC_FILE, FALSE,
3329 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003330 }
3331 }
3332 if (secure == 2)
3333 need_wait_return = TRUE;
3334 secure = 0;
3335#ifdef AMIGA
3336 proc->pr_WindowPtr = save_winptr;
3337#endif
3338 }
3339 TIME_MSG("sourcing vimrc file(s)");
3340}
3341
3342/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003343 * Setup to start using the GUI. Exit with an error when not available.
3344 */
3345 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003346main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003347{
3348#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003349 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003351 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352 mch_errmsg("\n");
3353 mch_exit(2);
3354#endif
3355}
3356
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003357#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003358
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003359/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003361 * Returns FAIL if the environment variable was not executed, OK otherwise.
3362 */
3363 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003364process_env(
3365 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003366 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003367{
3368 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003369 sctx_T save_current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003370
Bram Moolenaare31ee862020-01-07 20:59:34 +01003371 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003372
3373 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3374 {
3375 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003376 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003377 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003378 ESTACK_CHECK_SETUP
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003379 save_current_sctx = current_sctx;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003380 current_sctx.sc_version = 1;
3381#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003382 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003383 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003384 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003385#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003386
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003388
Bram Moolenaare31ee862020-01-07 20:59:34 +01003389 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003390 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003391 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003392 return OK;
3393 }
3394 return FAIL;
3395}
3396
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003397#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003398/*
3399 * Return TRUE if we are certain the user owns the file "fname".
3400 * Used for ".vimrc" and ".exrc".
3401 * Use both stat() and lstat() for extra security.
3402 */
3403 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003404file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003406 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003407# ifdef UNIX
3408 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003409# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003410 uid_t uid = ((getgid() << 16) | getuid());
3411# endif
3412
3413 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3414# ifdef HAVE_LSTAT
3415 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3416# endif
3417 );
3418}
3419#endif
3420
3421/*
3422 * Give an error message main_errors["n"] and exit.
3423 */
3424 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003425mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003426 int n, // one of the ME_ defines
3427 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003429#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003430 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003431#endif
3432
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003433 // If this is a Windows GUI executable, show an error dialog box.
3434#ifdef VIMDLL
3435 gui.in_use = mch_is_gui_executable();
3436#endif
3437#ifdef FEAT_GUI_MSWIN
3438 gui.starting = FALSE; // Needed to show as error.
3439#endif
3440
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003441 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003443 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003444 mch_errmsg(_(main_errors[n]));
3445 if (str != NULL)
3446 {
3447 mch_errmsg(": \"");
3448 mch_errmsg((char *)str);
3449 mch_errmsg("\"");
3450 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003451 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003452
3453 mch_exit(1);
3454}
3455
3456 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003457mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003458{
3459 mainerr(ME_ARG_MISSING, str);
3460}
3461
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003462#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003463/*
3464 * print a message with three spaces prepended and '\n' appended.
3465 */
3466 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003467main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003468{
3469 mch_msg(" ");
3470 mch_msg(s);
3471 mch_msg("\n");
3472}
3473
3474/*
3475 * Print messages for "vim -h" or "vim --help" and exit.
3476 */
3477 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003478usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003479{
3480 int i;
3481 static char *(use[]) =
3482 {
3483 N_("[file ..] edit specified file(s)"),
3484 N_("- read text from stdin"),
3485 N_("-t tag edit file where tag is defined"),
3486#ifdef FEAT_QUICKFIX
3487 N_("-q [errorfile] edit file with first error")
3488#endif
3489 };
3490
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003491#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003492 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003493#endif
3494
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003495 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003496 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003497 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003498 for (i = 0; ; ++i)
3499 {
3500 mch_msg(_(" vim [arguments] "));
3501 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003502 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003503 break;
3504 mch_msg(_("\n or:"));
3505 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003506#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003507 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003508#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003509
3510 mch_msg(_("\n\nArguments:\n"));
3511 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003512#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003513 main_msg(_("--literal\t\tDon't expand wildcards"));
3514#endif
3515#ifdef FEAT_OLE
3516 main_msg(_("-register\t\tRegister this gvim for OLE"));
3517 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3518#endif
3519#ifdef FEAT_GUI
3520 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3521 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3522#endif
3523 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3524 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003525 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003526 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3527#ifdef FEAT_DIFF
3528 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3529#endif
3530 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3531 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3532 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3533 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3534 main_msg(_("-M\t\t\tModifications in text not allowed"));
3535 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003536 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003537 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3538 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003539 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3540#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003542#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003543 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3544 main_msg(_("-r\t\t\tList swap files and exit"));
3545 main_msg(_("-r (with file name)\tRecover crashed session"));
3546 main_msg(_("-L\t\t\tSame as -r"));
3547#ifdef AMIGA
3548 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3549 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3550#endif
3551#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003552 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003553#endif
3554#ifdef FEAT_RIGHTLEFT
3555 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3556#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003557 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003558 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003559#ifdef FEAT_GUI
3560 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3561#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003562 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3564#ifdef FEAT_GUI
3565 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3566#endif
3567 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003568 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003569 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3570 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3571 main_msg(_("+\t\t\tStart at end of file"));
3572 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003573 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003574 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3575 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3576 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3577 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3578 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3579#ifdef FEAT_CRYPT
3580 main_msg(_("-x\t\t\tEdit encrypted files"));
3581#endif
3582#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3583# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003584 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003585# endif
3586 main_msg(_("-X\t\t\tDo not connect to X server"));
3587#endif
3588#ifdef FEAT_CLIENTSERVER
3589 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3590 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3591 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3592 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003593 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003594 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3595 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3596 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3597 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3598#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003599#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003600 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003601#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003602#ifdef FEAT_JOB_CHANNEL
K.Takata0fd7be72022-11-09 16:29:24 +00003603 main_msg(_("--log <file>\t\tStart logging to <file> early"));
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003604#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003605#ifdef FEAT_VIMINFO
3606 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3607#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003608 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003609 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3610 main_msg(_("--version\t\tPrint version information and exit"));
3611
3612#ifdef FEAT_GUI_X11
3613# ifdef FEAT_GUI_MOTIF
3614 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003615# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003616 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003617 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003618 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3619 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3620 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3621 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3622 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3623 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3624 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3625 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003626 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3627 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3628 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003629#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003630#ifdef FEAT_GUI_GTK
3631 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003632 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3633 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003634 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3635 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003636 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003637 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003638 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003639 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003640 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003641 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003642#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003643#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003644# ifdef VIMDLL
3645 if (gui.starting)
3646# endif
3647 {
3648 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3649 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3650 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003651#endif
3652
3653#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003654 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003655 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003656 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003657 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003658 gui.dofork = FALSE;
3659 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003660 else
3661#endif
3662 mch_exit(0);
3663}
3664
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003665/*
3666 * Check the result of the ATTENTION dialog:
3667 * When "Quit" selected, exit Vim.
3668 * When "Recover" selected, recover the file.
3669 */
3670 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003671check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003672{
3673 if (swap_exists_action == SEA_QUIT)
3674 getout(1);
3675 handle_swap_exists(NULL);
3676}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003677
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003678#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003679
Bram Moolenaar595297d2017-03-04 19:11:12 +01003680#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003681 static void
3682set_progpath(char_u *argv0)
3683{
3684 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003685
Bram Moolenaar4f974752019-02-17 17:44:42 +01003686# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003687 // A relative path containing a "/" will become invalid when using ":cd",
3688 // turn it into a full path.
3689 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3690 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003691 char_u *path = NULL;
3692
3693 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3694 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003695# else
3696 char_u buf[MAXPATHL + 1];
3697# ifdef PROC_EXE_LINK
3698 char linkbuf[MAXPATHL + 1];
3699 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003700
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003701 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3702 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003703 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003704 linkbuf[len] = NUL;
3705 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003706 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003707# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003708
3709 if (!mch_isFullName(val))
3710 {
3711 if (gettail(val) != val
3712 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3713 val = buf;
3714 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003715# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003716
Bram Moolenaar08cab962017-03-04 14:37:18 +01003717 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003718
Bram Moolenaar4f974752019-02-17 17:44:42 +01003719# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003720 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003721# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003722}
3723
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003724#endif // NO_VIM_MAIN