blob: 0e8e82cd4f8cc7f602f54cb037cde34bbcda843b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
K.Takata972db232022-02-04 10:45:38 +000014# include <cygwin/version.h>
15# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010016 // cygwin_conv_path()
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017# include <limits.h>
18#endif
19
Bram Moolenaarafde13b2019-04-28 19:46:49 +020020#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020021# include "iscygpty.h"
22#endif
23
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010024// Values for edit_type.
25#define EDIT_NONE 0 // no edit type yet
26#define EDIT_FILE 1 // file name argument[s] given, use argument list
27#define EDIT_STDIN 2 // read file from stdin
28#define EDIT_TAG 3 // tag name argument given, use tagname
29#define EDIT_QF 4 // start in quickfix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +000030
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010031#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010032static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000033#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020035static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010036#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010037static void usage(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010038static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010039static void command_line_scan(mparm_T *parmp);
40static void check_tty(mparm_T *parmp);
41static void read_stdin(void);
42static void create_windows(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void exe_pre_commands(mparm_T *parmp);
45static void exe_commands(mparm_T *parmp);
46static void source_startup_scripts(mparm_T *parmp);
47static void main_start_gui(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void check_swap_exists_action(void);
Bram Moolenaar08cab962017-03-04 14:37:18 +010049# ifdef FEAT_EVAL
50static void set_progpath(char_u *argv0);
51# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000052#endif
53
54
Bram Moolenaarb4210b32004-06-13 14:51:16 +000055/*
56 * Different types of error messages.
57 */
58static char *(main_errors[]) =
59{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000060 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000061#define ME_UNKNOWN_OPTION 0
62 N_("Too many edit arguments"),
63#define ME_TOO_MANY_ARGS 1
64 N_("Argument missing after"),
65#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000066 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000067#define ME_GARBAGE 3
68 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
69#define ME_EXTRA_CMD 4
70 N_("Invalid argument for"),
71#define ME_INVALID_ARG 5
72};
73
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010074#ifndef PROTO // don't want a prototype for main()
Bram Moolenaara6044292017-04-02 18:19:53 +020075
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010076// Various parameters passed between main() and other functions.
Bram Moolenaara6044292017-04-02 18:19:53 +020077static mparm_T params;
78
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010079#ifndef NO_VIM_MAIN // skip this for unittests
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020080
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010081static char_u *start_dir = NULL; // current working dir on startup
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020082
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020083static int has_dash_c_arg = FALSE;
84
Bram Moolenaarafde13b2019-04-28 19:46:49 +020085# ifdef VIMDLL
86__declspec(dllexport)
87# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000088 int
Bram Moolenaar796cc422019-04-03 20:31:00 +020089# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +000090VimMain
91# else
92main
93# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010094(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000095{
Bram Moolenaar77780b62018-03-01 23:10:45 +010096#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +000097 int i;
98#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000099
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100 /*
101 * Do any system-specific initialisations. These can NOT use IObuff or
102 * NameBuff. Thus emsg2() cannot be called!
103 */
104 mch_early_init();
105
Bram Moolenaar4f974752019-02-17 17:44:42 +0100106#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +0200107 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200108 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200109 * convert when 'encoding' changes. Get the unexpanded arguments.
110 */
111 argc = get_cmd_argsW(&argv);
112#endif
113
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100114 // Many variables are in "params" so that we can pass them to invoked
115 // functions without a lot of arguments. "argc" and "argv" are also
116 // copied, so that they can be changed.
Bram Moolenaara80faa82020-04-12 19:37:17 +0200117 CLEAR_FIELD(params);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000118 params.argc = argc;
119 params.argv = argv;
120 params.want_full_screen = TRUE;
121#ifdef FEAT_EVAL
122 params.use_debug_break_level = -1;
123#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000124 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000125
Bram Moolenaare76062c2022-11-28 18:51:43 +0000126 autocmd_init();
127
Bram Moolenaar99685e62013-05-11 13:56:18 +0200128#ifdef FEAT_RUBY
129 {
130 int ruby_stack_start;
131 vim_ruby_init((void *)&ruby_stack_start);
132 }
133#endif
134
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000135#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000137#endif
138
139#ifdef MEM_PROFILE
140 atexit(vim_mem_profile_dump);
141#endif
142
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100143#if defined(STARTUPTIME) || defined(FEAT_JOB_CHANNEL)
144 // Need to find "--startuptime" and "--log" before actually parsing
145 // arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100146 for (i = 1; i < argc - 1; ++i)
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100147 {
148# ifdef STARTUPTIME
149 if (STRICMP(argv[i], "--startuptime") == 0 && time_fd == NULL)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000150 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000151 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000152 TIME_MSG("--- VIM STARTING ---");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000153 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100154# endif
Bram Moolenaar4c5678f2022-11-30 18:12:19 +0000155# ifdef FEAT_EVAL
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100156 if (STRICMP(argv[i], "--log") == 0)
Bram Moolenaar1d97db32022-06-04 22:15:54 +0100157 ch_logfile((char_u *)(argv[i + 1]), (char_u *)"ao");
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +0100158# endif
159 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000160#endif
161
Bram Moolenaar07268702018-03-01 21:57:32 +0100162#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100163 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100164 for (i = 1; i < argc; ++i)
165 if (STRICMP(argv[i], "--clean") == 0)
166 {
167 params.clean = TRUE;
168 break;
169 }
170#endif
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200171#ifdef MSWIN
Bram Moolenaar4a228972021-05-01 22:41:39 +0200172 // Need to find "-register" and "-unregister" before loading any libraries.
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200173 for (i = 1; i < argc; ++i)
Bram Moolenaar4a228972021-05-01 22:41:39 +0200174 if ((STRICMP(argv[i] + 1, "register") == 0
175 || STRICMP(argv[i] + 1, "unregister") == 0)
176 && (argv[i][0] == '-' || argv[i][0] == '/'))
Bram Moolenaar3d0e7a92021-05-01 17:46:03 +0200177 {
178 found_register_arg = TRUE;
179 break;
180 }
181#endif
182
183 /*
184 * Various initialisations shared with tests.
185 */
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200186 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000187
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200188#ifdef VIMDLL
189 // Check if the current executable file is for the GUI subsystem.
190 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200191#elif defined(FEAT_GUI_MSWIN)
192 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200193#endif
194
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195#ifdef FEAT_CLIENTSERVER
196 /*
197 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000198 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000199 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000200 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000201#endif
202
203 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000204 * Figure out the way to work from the command name argv[0].
205 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000206 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000207 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000208
209 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000210 * Process the command line arguments. File names are put in the global
211 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000213 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214 TIME_MSG("parsing arguments");
215
216 /*
217 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000218 * there is no terminal version, and on Windows we can't fork one off with
219 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000220 */
221#ifdef ALWAYS_USE_GUI
222 gui.starting = TRUE;
223#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000224# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000225 /*
226 * Check if the GUI can be started. Reset gui.starting if not.
227 * Don't know about other systems, stay on the safe side and don't check.
228 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000229 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000231 if (gui_init_check() == FAIL)
232 {
233 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100235 // When running "evim" or "gvim -y" we need the menus, exit if we
236 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000237 if (params.evim_mode)
238 mch_exit(1);
239 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 }
241# endif
242#endif
243
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 if (GARGCOUNT > 0)
245 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100246#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000247 /*
248 * Expand wildcards in file names.
249 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000250 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000251 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200252 start_dir = alloc(MAXPATHL);
253 if (start_dir != NULL)
254 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100255 // Temporarily add '(' and ')' to 'isfname'. These are valid
256 // filename characters but are excluded from 'isfname' to make
Dominique Pelleaf4a61a2021-12-27 17:21:41 +0000257 // "gf" work on a file name in parentheses (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000258 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000259 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000260 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200261 if (start_dir != NULL)
262 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000263 }
264#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200265 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000266 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000267
Bram Moolenaar4f974752019-02-17 17:44:42 +0100268#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000269 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100270 // Remember the number of entries in the argument list. If it changes
271 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000272 set_alist_count();
273 }
274#endif
275
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000276#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000277 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000278 {
279 /*
280 * If there is one filename, fully qualified, we have very probably
281 * been invoked from explorer, so change to the file's directory.
282 * Hint: to avoid this when typing a command use a forward slash.
283 * If the cd fails, it doesn't matter.
284 */
Bram Moolenaar05268152021-11-18 18:53:45 +0000285 if (vim_chdirfile(params.fname, "drop") == OK)
286 last_chdir_reason = "drop";
Bram Moolenaarf6303872015-04-03 17:59:43 +0200287 if (start_dir != NULL)
288 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000289 }
290#endif
291 TIME_MSG("expanding arguments");
292
293#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000294 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100295 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000296#endif
297
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100298 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000299 ++RedrawingDisabled;
300
301 /*
302 * When listing swap file names, don't do cursor positioning et. al.
303 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200304 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000305 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000306
307 /*
Bram Moolenaarc174c2e2023-03-25 20:06:49 +0000308 * When certain to start the GUI, don't check terminal capabilities.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000309 * For GTK we can't be sure, but when started from the desktop it doesn't
310 * make sense to try using a terminal.
311 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200312#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
313 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000314 if (gui.starting
315# ifdef FEAT_GUI_GTK
316 && !isatty(2)
317# endif
318 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000319 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000320#endif
321
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000322 /*
323 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100324 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000325 * Note that we may use mch_exit() before mch_init()!
326 */
327 mch_init();
328 TIME_MSG("shell init");
329
330#ifdef USE_XSMP
331 /*
332 * For want of anywhere else to do it, try to connect to xsmp here.
333 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
334 * Hijacking -X 'no X connection' to also disable XSMP connection as that
335 * has a similar delay upon failure.
336 * Only try if SESSION_MANAGER is set to something non-null.
337 */
338 if (!x_no_connect)
339 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000340 char *p = getenv("SESSION_MANAGER");
341
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000342 if (p != NULL && *p != NUL)
343 {
344 xsmp_init();
345 TIME_MSG("xsmp init");
346 }
347 }
348#endif
349
350 /*
351 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000352 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000354
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100355#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100356 // Ensure output works usefully without a tty: buffer lines instead of
357 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100358 if (silent_mode)
359 setvbuf(stdout, NULL, _IOLBF, 0);
360#endif
361
Bram Moolenaar9404a182019-05-03 22:25:40 +0200362 // This message comes before term inits, but after setting "silent_mode"
363 // when the input is not a tty. Omit the message with --not-a-term.
364 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000365 printf(_("%d files to edit\n"), GARGCOUNT);
366
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000368 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100369 termcapinit(params.term); // set terminal name and get terminal
370 // capabilities (will set full_screen)
371 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372 TIME_MSG("Termcap init");
373 }
374
375 /*
376 * Set the default values for the options that use Rows and Columns.
377 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100378 ui_get_shellsize(); // inits Rows and Columns
Luuk van Baal470a1412022-09-14 01:27:23 +0100379 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100381 // Set the 'diff' option now, so that it can be checked for in a .vimrc
382 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000383 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 diff_win_options(firstwin, FALSE);
385#endif
386
387 cmdline_row = Rows - p_ch;
388 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100389 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000390 set_init_2();
391 TIME_MSG("inits 2");
392
393 msg_scroll = TRUE;
394 no_wait_return = TRUE;
395
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100396 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100398 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200401#if defined(FEAT_TERMRESPONSE)
402 init_term_props(TRUE);
403#endif
404
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000405#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100406 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000407 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408#endif
409
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100410 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
411 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200412 if (params.use_vimrc != NULL
413 && (STRCMP(params.use_vimrc, "NONE") == 0
414 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
415 p_lpl = FALSE;
416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200418 exe_pre_commands(&params);
419
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100420 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200421 source_startup_scripts(&params);
422
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100423#ifdef FEAT_MZSCHEME
424 /*
425 * Newer version of MzScheme (Racket) require earlier (trampolined)
426 * initialisation via scheme_main_setup.
427 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200428 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200429 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100430 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200431 return mzscheme_main();
432#else
433 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100434#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200435}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100436#endif // NO_VIM_MAIN
437#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100438
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200439/*
440 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
441 * things simple.
442 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
443 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100444 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200445vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100446{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100447#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000448#ifdef FEAT_EVAL
449 /*
450 * Read all the plugin files.
451 * Only when compiled with +eval, since most plugins need it.
452 */
453 if (p_lpl)
454 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200455 char_u *rtp_copy = NULL;
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100456 char_u *plugin_pattern = (char_u *)
457# if defined(VMS) || defined(AMIGA) // VMS and Amiga don't handle the "**".
458 "plugin/*.vim"
459# else
460 "plugin/**/*.vim"
461# endif
462 ;
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200463
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100464 // First add all package directories to 'runtimepath', so that their
465 // autoload directories can be found. Only if not done already with a
466 // :packloadall command.
467 // Make a copy of 'runtimepath', so that source_runtime does not use
468 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200469 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200470 {
471 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200472 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200473 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200474
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100475 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy, plugin_pattern,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000477 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200478 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100479
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100480 // Only source "start" packages if not done already with a :packloadall
481 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200482 if (!did_source_packages)
483 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100484 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200485
Bram Moolenaar6ee874d2020-11-27 19:01:31 +0100486 source_runtime(plugin_pattern, DIP_ALL | DIP_AFTER);
Bram Moolenaar66459b72016-08-06 19:01:55 +0200487 TIME_MSG("loading after plugins");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000488 }
489#endif
490
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000491#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100492 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000493 if (params.diff_mode && params.window_layout == 0)
494 {
495 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100496 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000497 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100498 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000499 }
500#endif
501
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000502 /*
503 * Recovery mode without a file name: List swap files.
504 * This uses the 'dir' option, therefore it must be after the
505 * initializations.
506 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200507 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000508 {
Bram Moolenaarc216a7a2022-12-05 13:50:55 +0000509 recover_names(NULL, TRUE, NULL, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 mch_exit(0);
511 }
512
513 /*
514 * Set a few option defaults after reading .vimrc files:
515 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
516 */
517 set_init_3();
518 TIME_MSG("inits 3");
519
520 /*
521 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
522 * Note that this overrides anything from a vimrc file.
523 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000524 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000525 p_uc = 0;
526
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527#ifdef FEAT_GUI
528 if (gui.starting)
529 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200530# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100531 // When something caused a message from a vimrc script, need to output
532 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000533 if (did_emsg || msg_didout)
534 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200535# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000536
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100537 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538 TIME_MSG("starting GUI");
539
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100540 // When running "evim" or "gvim -y" we need the menus, exit if we
541 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000542 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000543 mch_exit(1);
Luuk van Baal470a1412022-09-14 01:27:23 +0100544 firstwin->w_prev_height = firstwin->w_height; // may have changed
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000545 }
546#endif
547
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548#ifdef FEAT_VIMINFO
549 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000550 * Read in registers, history etc, but not marks, from the viminfo file.
551 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000552 */
553 if (*p_viminfo != NUL)
554 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000555 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556 TIME_MSG("reading viminfo");
557 }
558#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100559#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100560 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100561 if (get_vim_var_list(VV_OLDFILES) == NULL)
562 set_vim_var_list(VV_OLDFILES, list_alloc());
563#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564
565#ifdef FEAT_QUICKFIX
566 /*
567 * "-q errorfile": Load the error file now.
568 * If the error file can't be read, exit before doing anything else.
569 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000570 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000571 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100572 char_u *enc = NULL;
573
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100574 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000575 if (params.use_ef != NULL)
576 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000577 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200578 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100579 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000580 {
581 out_char('\n');
582 mch_exit(3);
583 }
584 TIME_MSG("reading errorfile");
585 }
586#endif
587
588 /*
589 * Start putting things on the screen.
590 * Scroll screen down before drawing over it
591 * Clear screen now, so file message will not be cleared.
592 */
593 starting = NO_BUFFERS;
594 no_wait_return = FALSE;
595 if (!exmode_active)
596 msg_scroll = FALSE;
597
598#ifdef FEAT_GUI
599 /*
600 * This seems to be required to make callbacks to be called now, instead
601 * of after things have been put on the screen, which then may be deleted
602 * when getting a resize callback.
603 * For the Mac this handles putting files dropped on the Vim icon to
604 * global_alist.
605 */
606 if (gui.in_use)
607 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100608 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000609 TIME_MSG("GUI delay");
610 }
611#endif
612
613#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
614 qnx_clip_init();
615#endif
616
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200617#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
618 clip_init(TRUE);
619#endif
620
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000621#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100622 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000623# ifdef FEAT_GUI
624 if (!gui.in_use)
625# endif
626 {
627 setup_term_clip();
628 TIME_MSG("setup clipboard");
629 }
630#endif
631
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000632#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100633 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000634 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635#endif
636
637 /*
638 * If "-" argument given: Read file from stdin.
639 * Do this before starting Raw mode, because it may change things that the
640 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
641 * are the same terminal: "cat | vim -".
642 * Using autocommands here may cause trouble...
643 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000644 if (params.edit_type == EDIT_STDIN && !recoverymode)
645 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000646
647#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100648 // When switching screens and something caused a message from a vimrc
649 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000650 if ((did_emsg || msg_didout) && *T_TI != NUL)
651 newline_on_exit = TRUE;
652#endif
653
654 /*
K.Takataeeec2542021-06-02 13:28:16 +0200655 * When done something that is not allowed or given an error message call
Bram Moolenaar13608d82022-08-29 15:06:50 +0100656 * wait_return(). This must be done before starttermcap(), because it may
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000657 * switch to another screen. It must be done after settmode(TMODE_RAW),
658 * because we want to react on a single key stroke.
659 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000660 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000661 */
662 settmode(TMODE_RAW);
663 TIME_MSG("setting raw mode");
664
665 if (need_wait_return || msg_didany)
666 {
667 wait_return(TRUE);
668 TIME_MSG("waiting for return");
669 }
670
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100671 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000672 TIME_MSG("start termcap");
673
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200674 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000675 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100676 scroll_region_reset(); // In case Rows changed
677 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000678
Bram Moolenaar651fca82021-11-29 20:39:38 +0000679#if defined(UNIX) || defined(VMS) || defined(MACOS_X)
Bram Moolenaar40385db2018-08-07 22:31:44 +0200680 term_push_title(SAVE_RESTORE_BOTH);
681#endif
682
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000683 /*
684 * Don't clear the screen when starting in Ex mode, unless using the GUI.
685 */
686 if (exmode_active
687#ifdef FEAT_GUI
688 && !gui.in_use
689#endif
690 )
Bram Moolenaar471c0fa2022-08-22 15:19:16 +0100691 set_must_redraw(UPD_CLEAR);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000692 else
693 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100694 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000695 TIME_MSG("clearing screen");
696 }
697
698#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000699 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000700 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100701 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200702 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 TIME_MSG("getting crypt key");
704 }
705#endif
706
707 no_wait_return = TRUE;
708
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000709 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000710 * Create the requested number of windows and edit buffers in them.
711 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000713 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000714 TIME_MSG("opening buffers");
715
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000716#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100717 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000718 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
719#endif
720
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100721 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000722 if (exmode_active)
723 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
724
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
726 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 setpcmark();
728
729#ifdef FEAT_QUICKFIX
730 /*
731 * When started with "-q errorfile" jump to first error now.
732 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000733 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000734 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000735 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 TIME_MSG("jump to first error");
737 }
738#endif
739
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740 /*
741 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000742 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000743 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200744 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200745 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000746
747#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000748 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749 {
750 win_T *wp;
751
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100752 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200753 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 diff_win_options(wp, TRUE);
755 }
756#endif
757
758 /*
759 * Shorten any of the filenames, but only when absolute.
760 */
761 shorten_fnames(FALSE);
762
763 /*
764 * Need to jump to the tag before executing the '-c command'.
765 * Makes "vim -c '/return' -t main" work.
766 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000767 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000769 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000770
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000771 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000772 do_cmdline_cmd(IObuff);
773 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000774
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100775 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000776 if (swap_exists_did_quit)
777 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000778 }
779
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100780 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000781 if (params.n_commands > 0)
782 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000783
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100784 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200785 starting = 0;
786
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100787#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100788 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200789 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200790#endif
791
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000792 RedrawingDisabled = 0;
Bram Moolenaara4d158b2022-08-14 14:17:45 +0100793 redraw_all_later(UPD_NOT_VALID);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100796 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200797 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200798
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000799#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100800 // Requesting the termresponse is postponed until here, so that a "-c q"
801 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000802 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200803
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200804 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000805#endif
806
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100807 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000808 if (p_im)
809 need_start_insertmode = TRUE;
810
Bram Moolenaar14735512016-03-26 21:00:08 +0100811#ifdef FEAT_EVAL
812 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
813#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
815 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000816
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200817#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100818 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
819 // done after the clipboard is available and all initial commands that may
820 // modify the 'clipboard' setting have run; i.e. just before entering the
821 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200822 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200823#endif
824
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100825#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100826 // When a startup script or session file setup for diff'ing and
827 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000828 if (curwin->w_p_diff && curwin->w_p_scb)
829 {
830 update_topline();
831 check_scrollbind((linenr_T)0, 0L);
832 TIME_MSG("diff scrollbinding");
833 }
834#endif
835
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200836#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
837# ifdef VIMDLL
838 if (!gui.in_use)
839# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100840 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841#endif
842
Bram Moolenaar4033c552017-09-16 20:54:51 +0200843#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100844 // When tab pages were created, may need to update the tab pages line and
845 // scrollbars. This is skipped while creating them.
h-east6073f132021-12-24 11:57:06 +0000846 if (gui.in_use && first_tabpage->tp_next != NULL)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000847 {
848 out_flush();
849 gui_init_which_components(NULL);
850 gui_update_scrollbars(TRUE);
851 }
852 need_mouse_correct = TRUE;
853#endif
854
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100855 // If ":startinsert" command used, stuff a dummy command to be able to
856 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000857 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000858 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000859
860#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200861 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200862 {
863# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200864# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100865 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200866 if (gui.in_use)
867 {
868 mch_errmsg(_("netbeans is not supported with this GUI\n"));
869 mch_exit(2);
870 }
871# endif
872# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100873 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200874 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200875 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000876#endif
877
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100878 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
879 // window title gets updated.
880 do_redraw = TRUE;
881
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882 TIME_MSG("before starting main loop");
883
884 /*
885 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200886 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000887 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000888
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100889#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200890
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891 return 0;
892}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893
894/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200895 * Initialisation shared by main() and some tests.
896 */
897 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200898common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200899{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100900 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200901 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200902
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100903 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200904#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100905 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200906#endif
907
908#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100909 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200910#endif
911
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200912 /*
913 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100914 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200915 */
916 if ((IObuff = alloc(IOSIZE)) == NULL
917 || (NameBuff = alloc(MAXPATHL)) == NULL)
918 mch_exit(0);
919 TIME_MSG("Allocated generic buffers");
920
921#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100922 // Wait a moment for debugging NetBeans. Must be after allocating
923 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200924 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
925 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
926 TIME_MSG("NetBeans debug wait");
927#endif
928
929#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
930 /*
931 * Setup to use the current locale (for ctype() and many other things).
932 * NOTE: Translated messages with encodings other than latin1 will not
933 * work until set_init_1() has been called!
934 */
935 init_locale();
936 TIME_MSG("locale set");
937#endif
938
939#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100940 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200941#endif
942
943 /*
944 * Do a first scan of the arguments in "argv[]":
945 * -display or --display
946 * --server...
947 * --socketid
948 * --windowid
949 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200950 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200951
Bram Moolenaard0573012017-10-28 21:11:06 +0200952#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100953 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200954 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200955 TIME_MSG("GUI prepared");
956#endif
957
958#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100959 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200960 TIME_MSG("clipboard setup");
961#endif
962
963 /*
964 * Check if we have an interactive window.
965 * On the Amiga: If there is no window, we open one with a newcli command
966 * (needed for :! to * work). mch_check_win() will also handle the -d or
967 * -dev argument.
968 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100969 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200970 TIME_MSG("window checked");
971
972 /*
973 * Allocate the first window and buffer.
974 * Can't do anything without it, exit when it fails.
975 */
976 if (win_alloc_first() == FAIL)
977 mch_exit(0);
978
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100979 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200980
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100981 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200982 global_alist.id = 0;
983
984 /*
985 * Set the default values for the options.
986 * NOTE: Non-latin1 translated messages are working only after this,
987 * because this is where "has_mbyte" will be set, which is used by
988 * msg_outtrans_len_attr().
989 * First find out the home directory, needed to expand "~" in options.
990 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100991 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +0100992 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200993 TIME_MSG("inits 1");
994
995#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100996 // set v:lang and v:ctype
997 set_lang_var();
998
999 // set v:argv
1000 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001001#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001002
1003#ifdef FEAT_SIGNS
1004 init_signs();
1005#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001006}
1007
1008/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001009 * Return TRUE when the --not-a-term argument was found.
1010 */
1011 int
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001012is_not_a_term(void)
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001013{
1014 return params.not_a_term;
1015}
1016
Bram Moolenaar7007e312021-03-27 12:11:33 +01001017/*
1018 * Return TRUE when the --not-a-term argument was found or the GUI is in use.
1019 */
Bram Moolenaarf7e73022022-09-24 13:10:04 +01001020 int
1021is_not_a_term_or_gui(void)
Bram Moolenaar7007e312021-03-27 12:11:33 +01001022{
1023 return params.not_a_term
1024#ifdef FEAT_GUI
1025 || gui.in_use
1026#endif
1027 ;
1028}
1029
Bram Moolenaar2d12c252022-06-13 21:42:45 +01001030#if defined(FEAT_GUI) || defined(PROTO)
1031/*
1032 * If a --gui-dialog-file argument was given return the file name.
1033 * Otherwise return NULL.
1034 */
1035 char_u *
1036get_gui_dialog_file(void)
1037{
1038 return params.gui_dialog_file;
1039}
1040#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001041
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001042// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001043static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001044static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001045
1046/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001047 * Return TRUE if an operator was started but not finished yet.
1048 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001049 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001050 int
1051op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001052{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001053 return !(current_oap != NULL
1054 && !finish_op
1055 && current_oap->prev_opcount == 0
1056 && current_oap->prev_count0 == 0
1057 && current_oap->op_type == OP_NOP
1058 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001059}
1060
1061/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001062 * Return whether currently it is safe, assuming it was safe before (high level
1063 * state didn't change).
1064 */
1065 static int
1066is_safe_now(void)
1067{
1068 return stuff_empty()
1069 && typebuf.tb_len == 0
1070 && scriptin[curscript] == NULL
Bram Moolenaar4fa13462022-02-05 12:39:24 +00001071#ifdef FEAT_EVAL
1072 && !debug_mode
1073#endif
1074 && !global_busy;
Bram Moolenaard103ee72019-09-18 21:15:31 +02001075}
1076
1077/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001078 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1079 * there is no typeahead.
1080 */
1081 void
1082may_trigger_safestate(int safe)
1083{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001084 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001085
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001086#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001087 if (was_safe != is_safe)
1088 // Only log when the state changes, otherwise it happens at nearly
1089 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001090 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1091 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001092#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001093 if (is_safe)
1094 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1095 was_safe = is_safe;
1096}
1097
1098/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001099 * Something changed which causes the state possibly to be unsafe, e.g. a
1100 * character was typed. It will remain unsafe until the next call to
1101 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001102 */
1103 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001104state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001105{
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001106#ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001107 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001108 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001109#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001110 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001111}
1112
Dominique Pelle748b3082022-01-08 12:41:16 +00001113#if defined(FEAT_EVAL) || defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001114 int
1115get_was_safe_state(void)
1116{
1117 return was_safe;
1118}
Dominique Pelle748b3082022-01-08 12:41:16 +00001119#endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001120
Dominique Pelle748b3082022-01-08 12:41:16 +00001121#if defined(MESSAGE_QUEUE) || defined(PROTO)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001122/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001123 * Invoked when leaving code that invokes callbacks. Then trigger
1124 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001125 */
1126 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001127may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001128{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001129 if (!was_safe)
1130 {
1131 // If the safe state was reset in state_no_longer_safe(), e.g. because
1132 // of calling feedkeys(), we check if it's now safe again (all keys
1133 // were consumed).
1134 was_safe = is_safe_now();
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001135# ifdef FEAT_EVAL
Bram Moolenaard103ee72019-09-18 21:15:31 +02001136 if (was_safe)
1137 ch_log(NULL, "SafeState: undo reset");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001138# endif
Bram Moolenaard103ee72019-09-18 21:15:31 +02001139 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001140 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001141 {
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001142# ifdef FEAT_EVAL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001143 // Only do this message when another message was given, otherwise we
1144 // get lots of them.
1145 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1146 {
1147 int did = did_repeated_msg;
1148
1149 ch_log(NULL,
1150 "SafeState: back to waiting, triggering SafeStateAgain");
1151 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1152 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001153# endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001154 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001155 }
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001156# ifdef FEAT_EVAL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001157 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001158 ch_log(NULL,
1159 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001160# endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001161}
Dominique Pelle748b3082022-01-08 12:41:16 +00001162#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001163
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001164/*
1165 * Return TRUE if there is any typeahead, pending operator or command.
1166 */
1167 int
1168work_pending(void)
1169{
1170 return op_pending() || !is_safe_now();
1171}
1172
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001173
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001174/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001175 * Main loop: Execute Normal mode commands until exiting Vim.
1176 * Also used to handle commands in the command-line window, until the window
1177 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 * Also used to handle ":visual" command after ":global": execute Normal mode
1179 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001180 */
1181 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001182main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001183 int cmdwin, // TRUE when working in the command-line window
1184 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001185{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001186 oparg_T oa; // operator arguments
1187 oparg_T *prev_oap; // operator arguments
1188 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001189#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001190 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001191 static linenr_T conceal_old_cursor_line = 0;
1192 static linenr_T conceal_new_cursor_line = 0;
1193 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001194#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001195
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001196 prev_oap = current_oap;
1197 current_oap = &oa;
1198
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001199#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001200 // Setup to catch a terminating error from the X server. Just ignore
1201 // it, restore the state and continue. This might not always work
1202 // properly, but at least we don't exit unexpectedly when the X server
1203 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001205 {
Bram Moolenaar24959102022-05-07 20:01:16 +01001206 State = MODE_NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001207 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001208 got_int = TRUE;
1209 need_wait_return = FALSE;
1210 global_busy = FALSE;
1211 exmode_active = 0;
1212 skip_redraw = FALSE;
1213 RedrawingDisabled = 0;
1214 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001215 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001216# ifdef FEAT_EVAL
1217 emsg_skip = 0;
1218# endif
1219 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001220 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001221 settmode(TMODE_RAW);
1222 starttermcap();
1223 scroll_start();
1224 redraw_later_clear();
1225 }
1226#endif
1227
1228 clear_oparg(&oa);
Martin Tournoij7904fa42022-10-04 16:28:45 +01001229 while (!cmdwin || cmdwin_result == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001230 {
1231 if (stuff_empty())
1232 {
1233 did_check_timestamps = FALSE;
1234 if (need_check_timestamps)
1235 check_timestamps(FALSE);
Bram Moolenaar13608d82022-08-29 15:06:50 +01001236 if (need_wait_return) // if wait_return() still needed ...
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001237 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001238 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001239 {
1240 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001241 stuffReadbuff((char_u *)"i"); // start insert mode next
1242 // skip the fileinfo message now, because it would be shown
1243 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001244 need_fileinfo = FALSE;
1245 }
1246 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001247
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001248 // Reset "got_int" now that we got back to the main loop. Except when
1249 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1250 // the ":g" command.
1251 // For ":g/pat/vi" we reset "got_int" when used once. When used
1252 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001253 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001254 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001255 if (noexmode && global_busy && !exmode_active && previous_got_int)
1256 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001257 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1258 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001259 exmode_active = EXMODE_NORMAL;
Bram Moolenaar24959102022-05-07 20:01:16 +01001260 State = MODE_NORMAL;
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001261 }
1262 else if (!global_busy || !exmode_active)
1263 {
1264 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001265 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001266 got_int = FALSE;
1267 }
1268 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001269 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001270 else
1271 previous_got_int = FALSE;
1272
Bram Moolenaar069613c2022-01-15 15:23:44 +00001273#ifdef FEAT_EVAL
1274 // At the toplevel there is no exception handling. Discard any that
1275 // may be hanging around (e.g. from "interrupt" at the debug prompt).
1276 if (did_throw && !ex_normal_busy)
1277 discard_current_exception();
1278#endif
1279
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 if (!exmode_active)
1281 msg_scroll = FALSE;
1282 quit_more = FALSE;
1283
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001284 // it's not safe unless may_trigger_safestate_main() is called
1285 was_safe = FALSE;
1286
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001287 /*
1288 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1289 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1290 * update cursor and redraw.
1291 */
1292 if (skip_redraw || exmode_active)
Bram Moolenaarc174c2e2023-03-25 20:06:49 +00001293 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001294 skip_redraw = FALSE;
Bram Moolenaarc174c2e2023-03-25 20:06:49 +00001295 setcursor();
1296 cursor_on();
1297 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001298 else if (do_redraw || stuff_empty())
1299 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001300#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001301 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001302 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001303#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001304#ifdef HAVE_DROP_FILE
1305 // If files were dropped while text was locked or the curbuf was
1306 // locked, this would be a good time to handle the drop.
1307 handle_any_postponed_drop();
1308#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001309#ifdef FEAT_CONCEAL
1310 if (curwin->w_p_cole == 0)
1311 conceal_update_lines = FALSE;
1312#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001313
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001314 // Trigger CursorMoved if the cursor moved.
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001315 if (!finish_op && (has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001316#ifdef FEAT_PROP_POPUP
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001317 || popup_visible
Bram Moolenaar3397f742019-06-02 18:40:06 +02001318#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001319#ifdef FEAT_CONCEAL
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001320 || curwin->w_p_cole > 0
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001321#endif
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001322 )
1323 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001324 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001325 if (has_cursormoved())
1326 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1327 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001328#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001329 if (popup_visible)
1330 popup_check_cursor_pos();
1331#endif
1332#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001333 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001334 {
1335 conceal_old_cursor_line = last_cursormoved.lnum;
1336 conceal_new_cursor_line = curwin->w_cursor.lnum;
1337 conceal_update_lines = TRUE;
1338 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001339#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001340 last_cursormoved = curwin->w_cursor;
1341 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001342
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001343#if defined(FEAT_CONCEAL)
1344 if (conceal_update_lines
1345 && (conceal_old_cursor_line != conceal_new_cursor_line
1346 || conceal_cursor_line(curwin)
1347 || need_cursor_line_redraw))
1348 {
1349 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001350 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001351 && conceal_old_cursor_line
1352 <= curbuf->b_ml.ml_line_count)
1353 redrawWinline(curwin, conceal_old_cursor_line);
1354 redrawWinline(curwin, conceal_new_cursor_line);
1355 curwin->w_valid &= ~VALID_CROW;
1356 need_cursor_line_redraw = FALSE;
1357 }
1358#endif
1359
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001360 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001361 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001362 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001363 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001364 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1365 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001366 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001367
LemonBoy09371822022-04-08 15:18:45 +01001368 // Ensure curwin->w_topline and curwin->w_leftcol are up to date
1369 // before triggering a WinScrolled autocommand.
1370 update_topline();
1371 validate_cursor();
1372
1373 if (!finish_op)
Bram Moolenaar35fc61c2022-11-22 12:40:50 +00001374 may_trigger_win_scrolled_resized();
LemonBoy09371822022-04-08 15:18:45 +01001375
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001376 // If nothing is pending and we are going to wait for the user to
1377 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001378 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001379
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001380#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001381 // Updating diffs from changed() does not always work properly,
1382 // esp. updating folds. Do an update just before redrawing if
1383 // needed.
1384 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1385 {
1386 ex_diffupdate(NULL);
1387 curtab->tp_diff_update = FALSE;
1388 }
1389
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001390 // Scroll-binding for diff mode may have been postponed until
1391 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001392 if (diff_need_scrollbind)
1393 {
1394 check_scrollbind((linenr_T)0, 0L);
1395 diff_need_scrollbind = FALSE;
1396 }
1397#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001398#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001399 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001400 foldAdjustVisual();
1401#endif
1402#ifdef FEAT_FOLDING
1403 /*
1404 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1405 * contain the cursor.
1406 * When 'foldopen' is "all", open the fold(s) under the cursor.
1407 * This may mark the window for redrawing.
1408 */
1409 if (hasAnyFolding(curwin) && !char_avail())
1410 {
1411 foldCheckClose();
1412 if (fdo_flags & FDO_ALL)
1413 foldOpenCursor();
1414 }
1415#endif
1416
Bram Moolenaar24fe33a2022-11-24 00:09:02 +00001417 // Before redrawing, make sure w_topline is correct, and w_leftcol
1418 // if lines don't wrap, and w_skipcol if lines wrap.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001419 update_topline();
1420 validate_cursor();
1421
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001422 if (VIsual_active)
Bram Moolenaara4d158b2022-08-14 14:17:45 +01001423 update_curbuf(UPD_INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001424 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001425 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001426 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001427 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001428 mch_enable_flush();
1429 }
Bram Moolenaar7a1d3282022-06-16 13:04:45 +01001430 else if (redraw_cmdline || clear_cmdline || redraw_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001432 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001433 if (need_maketitle)
1434 maketitle();
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001435#ifdef FEAT_VIMINFO
1436 curbuf->b_last_used = vim_time();
1437#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001438 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001439 if (keep_msg != NULL)
1440 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001441 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001442
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001443 if (p != NULL)
1444 {
1445 // msg_start() will set keep_msg to NULL, make a copy
1446 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1447 // check for duplicates. Never put this message in
1448 // history.
1449 msg_hist_off = TRUE;
1450 msg_attr((char *)p, keep_msg_attr);
1451 msg_hist_off = FALSE;
1452 vim_free(p);
1453 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001454 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001455 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001456 {
1457 fileinfo(FALSE, TRUE, FALSE);
1458 need_fileinfo = FALSE;
1459 }
1460
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001461 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001462 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001463 msg_didany = FALSE; // reset lines_left in msg_start()
1464 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001465 showruler(FALSE);
1466
1467 setcursor();
1468 cursor_on();
1469
1470 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001471
1472#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001473 // Now that we have drawn the first screen all the startup stuff
1474 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001475 if (time_fd != NULL)
1476 {
1477 TIME_MSG("first screen update");
1478 TIME_MSG("--- VIM STARTED ---");
1479 fclose(time_fd);
1480 time_fd = NULL;
1481 }
1482#endif
Bram Moolenaar0a60f792022-11-19 21:18:11 +00001483 // After the first screen update may start triggering WinScrolled
1484 // autocmd events. Store all the scroll positions and sizes now.
1485 may_make_initial_scroll_size_snapshot();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001486 }
1487#ifdef FEAT_GUI
1488 if (need_mouse_correct)
1489 gui_mouse_correct();
1490#endif
1491
Bram Moolenaar733a69b2022-12-01 12:03:47 +00001492 // May request the keyboard protocol state now.
1493 may_send_t_RK();
1494
1495 // Update w_curswant if w_set_curswant has been set.
1496 // Postponed until here to avoid computing w_virtcol too often.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001497 update_curswant();
1498
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001499#ifdef FEAT_EVAL
1500 /*
1501 * May perform garbage collection when waiting for a character, but
1502 * only at the very toplevel. Otherwise we may be using a List or
1503 * Dict internally somewhere.
1504 * "may_garbage_collect" is reset in vgetc() which is invoked through
1505 * do_exmode() and normal_cmd().
1506 */
1507 may_garbage_collect = (!cmdwin && !noexmode);
1508#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001509 /*
1510 * If we're invoked as ex, do a round of ex commands.
1511 * Otherwise, get and execute a normal mode command.
1512 */
1513 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001514 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001515 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001516 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001517 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001518 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001519 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001520 {
1521#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001522 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001523 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001524 && !VIsual_active
1525 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001526 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001527 // If terminal_loop() returns OK we got a key that is handled
1528 // in Normal model. With FAIL we first need to position the
1529 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001530 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001531 normal_cmd(&oa, TRUE);
1532 }
1533 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001534#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001535 {
1536#ifdef FEAT_TERMINAL
1537 skip_term_loop = FALSE;
1538#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001539 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001540 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001541 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001542 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001543
1544theend:
1545 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001546}
1547
1548
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001549#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001550/*
1551 * Exit, but leave behind swap files for modified buffers.
1552 */
1553 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001554getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001555{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001556# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001557 // Ignore SIGHUP, because a dropped connection causes a read error, which
1558 // makes Vim exit and then handling SIGHUP causes various reentrance
1559 // problems.
ichizok378447f2023-05-11 22:25:42 +01001560 mch_signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001561# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001562
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001563 ml_close_notmod(); // close all not-modified buffers
1564 ml_sync_all(FALSE, FALSE); // preserve all swap files
1565 ml_close_all(FALSE); // close all memfiles, without deleting
1566 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001567}
1568#endif
1569
1570
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001571/*
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001572 * Exit properly. This is the only way to exit Vim after startup has
1573 * succeeded. We are certain to exit here, no way to abort it.
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001574 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001575 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001576getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001577{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001578 exiting = TRUE;
Bram Moolenaar4c5678f2022-11-30 18:12:19 +00001579#if defined(FEAT_EVAL)
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001580 ch_log(NULL, "Exiting...");
1581#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001582
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001583 // When running in Ex mode an error causes us to exit with a non-zero exit
1584 // code. POSIX requires this, although it's not 100% clear from the
1585 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001586 if (exmode_active)
1587 exitval += ex_exitval;
1588
Bram Moolenaarf0068c52020-11-30 17:42:10 +01001589#ifdef FEAT_EVAL
1590 set_vim_var_type(VV_EXITING, VAR_NUMBER);
1591 set_vim_var_nr(VV_EXITING, exitval);
1592#endif
1593
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001594 // Position the cursor on the last screen line, below all the text
Bram Moolenaar7007e312021-03-27 12:11:33 +01001595 if (!is_not_a_term_or_gui())
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001596 windgoto((int)Rows - 1, 0);
1597
Bram Moolenaar58779852022-09-06 18:31:14 +01001598#ifdef FEAT_EVAL
1599 // Invoked all deferred functions in the function stack.
1600 invoke_all_defer();
1601#endif
1602
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001603#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001604 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001605 hash_debug_results();
1606#endif
1607
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001608#ifdef FEAT_GUI
1609 msg_didany = FALSE;
1610#endif
1611
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001612 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001613 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001614 tabpage_T *tp;
1615 tabpage_T *next_tp;
1616 buf_T *buf;
1617 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001618 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001619
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001620 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001621 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001622 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001623 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001624 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001625 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001626 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001627 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001628 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001629 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001630 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001631 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001632 bufref_T bufref;
1633
1634 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001635 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1636 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001637 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001638 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001639
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001640 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001641 next_tp = first_tabpage;
1642 break;
1643 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001644 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001645 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001646
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001647 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001648 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001649 if (buf->b_ml.ml_mfp != NULL)
1650 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001651 bufref_T bufref;
1652
1653 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001654 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001655 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001656 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001657 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001658 break;
1659 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001660
1661 // deathtrap() blocks autocommands, but we do want to trigger
1662 // VimLeavePre.
1663 if (is_autocmd_blocked())
1664 {
1665 unblock_autocmds();
1666 ++unblock;
1667 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001668 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001669 if (unblock)
1670 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001671 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001672
1673#ifdef FEAT_VIMINFO
1674 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001675 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001676 write_viminfo(NULL, FALSE);
1677#endif
1678
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001679 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001680 {
1681 int unblock = 0;
1682
1683 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1684 if (is_autocmd_blocked())
1685 {
1686 unblock_autocmds();
1687 ++unblock;
1688 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001689 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001690 if (unblock)
1691 block_autocmds();
1692 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001693
Bram Moolenaar05159a02005-02-26 23:04:13 +00001694#ifdef FEAT_PROFILE
1695 profile_dump();
1696#endif
1697
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001698 if (did_emsg
1699#ifdef FEAT_GUI
1700 || (gui.in_use && msg_didany && p_verbose > 0)
1701#endif
1702 )
1703 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001704 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001705 no_wait_return = FALSE;
Volodymyr Kot7f0c4b42021-11-21 12:27:13 +00001706 wait_return(FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001707 }
1708
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001709 // Position the cursor again, the autocommands may have moved it
Bram Moolenaar7007e312021-03-27 12:11:33 +01001710 if (!is_not_a_term_or_gui())
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001711 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001712
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001713#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001714 job_stop_on_exit();
1715#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001716#ifdef FEAT_LUA
1717 lua_end();
1718#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001719#ifdef FEAT_MZSCHEME
1720 mzscheme_end();
1721#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001722#ifdef FEAT_TCL
1723 tcl_end();
1724#endif
1725#ifdef FEAT_RUBY
1726 ruby_end();
1727#endif
1728#ifdef FEAT_PYTHON
1729 python_end();
1730#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001731#ifdef FEAT_PYTHON3
1732 python3_end();
1733#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001734#ifdef FEAT_PERL
1735 perl_end();
1736#endif
1737#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1738 iconv_end();
1739#endif
1740#ifdef FEAT_NETBEANS_INTG
1741 netbeans_end();
1742#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001743#ifdef FEAT_CSCOPE
1744 cs_end();
1745#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001746#ifdef FEAT_EVAL
1747 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001748 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001749#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001750#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001751 free_cmd_argsW();
1752#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001753
1754 mch_exit(exitval);
1755}
1756
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001757/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 * Get the name of the display, before gui_prepare() removes it from
1759 * argv[]. Used for the xterm-clipboard display.
1760 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001761 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001762 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001763 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001764early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001765{
Bram Moolenaar03005972008-11-20 13:12:36 +00001766#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1767 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001768 int argc = parmp->argc;
1769 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001770 int i;
1771
1772 for (i = 1; i < argc; i++)
1773 {
1774 if (STRCMP(argv[i], "--") == 0)
1775 break;
1776# ifdef FEAT_XCLIPBOARD
1777 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001778# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001779 || STRICMP(argv[i], "--display") == 0
1780# endif
1781 )
1782 {
1783 if (i == argc - 1)
1784 mainerr_arg_missing((char_u *)argv[i]);
1785 xterm_display = argv[++i];
1786 }
1787# endif
1788# ifdef FEAT_CLIENTSERVER
1789 else if (STRICMP(argv[i], "--servername") == 0)
1790 {
1791 if (i == argc - 1)
1792 mainerr_arg_missing((char_u *)argv[i]);
1793 parmp->serverName_arg = (char_u *)argv[++i];
1794 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001795 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001796 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001797 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001798 {
1799 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001800# ifdef FEAT_GUI
1801 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001802 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001803 gui.dofork = FALSE;
1804# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001805 }
1806# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001807
Bram Moolenaar4f974752019-02-17 17:44:42 +01001808# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1809# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001810 else if (STRICMP(argv[i], "--windowid") == 0)
1811# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001812 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001813# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001814 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001815 long_u id;
1816 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001817
1818 if (i == argc - 1)
1819 mainerr_arg_missing((char_u *)argv[i]);
1820 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001821 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001822 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001823 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001824 if (count != 1)
1825 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1826 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001827# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001828 win_socket_id = id;
1829# else
1830 gtk_socket_id = id;
1831# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001832 i++;
1833 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001834# endif
1835# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001836 else if (STRICMP(argv[i], "--echo-wid") == 0)
1837 echo_wid_arg = TRUE;
1838# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001839# ifndef FEAT_NETBEANS_INTG
1840 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001841 {
1842 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1843 mch_exit(2);
1844 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001845# endif
1846
Bram Moolenaar58d98232005-07-23 22:25:46 +00001847 }
1848#endif
1849}
1850
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001851#ifndef NO_VIM_MAIN
1852/*
1853 * Get a (optional) count for a Vim argument.
1854 */
1855 static int
1856get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001857 char_u *p, // pointer to argument
1858 int *idx, // index in argument, is incremented
1859 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001860{
1861 if (vim_isdigit(p[*idx]))
1862 {
1863 def = atoi((char *)&(p[*idx]));
1864 while (vim_isdigit(p[*idx]))
1865 *idx = *idx + 1;
1866 }
1867 return def;
1868}
1869
1870/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001871 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001872 * If the executable name starts with "r" we disable shell commands.
1873 * If the next character is "e" we run in Easy mode.
1874 * If the next character is "g" we run the GUI version.
1875 * If the next characters are "view" we start in readonly mode.
1876 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1877 * If the next characters are "ex" we start in Ex mode. If it's followed
1878 * by "im" use improved Ex mode.
1879 */
1880 static void
1881parse_command_name(mparm_T *parmp)
1882{
1883 char_u *initstr;
1884
1885 initstr = gettail((char_u *)parmp->argv[0]);
1886
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001887#ifdef FEAT_EVAL
1888 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001889 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001890#endif
1891
1892 if (TOLOWER_ASC(initstr[0]) == 'r')
1893 {
1894 restricted = TRUE;
1895 ++initstr;
1896 }
1897
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001898 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001899 if (TOLOWER_ASC(initstr[0]) == 'e'
1900 && (TOLOWER_ASC(initstr[1]) == 'v'
1901 || TOLOWER_ASC(initstr[1]) == 'g'))
1902 {
1903#ifdef FEAT_GUI
1904 gui.starting = TRUE;
1905#endif
1906 parmp->evim_mode = TRUE;
1907 ++initstr;
1908 }
1909
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001910 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001911 if (TOLOWER_ASC(initstr[0]) == 'g')
1912 {
1913 main_start_gui();
1914#ifdef FEAT_GUI
1915 ++initstr;
1916#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001917#ifdef GUI_MAY_SPAWN
1918 gui.dospawn = FALSE; // No need to spawn a new process.
1919#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001920 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001921#ifdef GUI_MAY_SPAWN
1922 else
1923 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1924#endif
1925
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001926
1927 if (STRNICMP(initstr, "view", 4) == 0)
1928 {
1929 readonlymode = TRUE;
1930 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001931 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001932 initstr += 4;
1933 }
1934 else if (STRNICMP(initstr, "vim", 3) == 0)
1935 initstr += 3;
1936
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001937 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001938 if (STRICMP(initstr, "diff") == 0)
1939 {
1940#ifdef FEAT_DIFF
1941 parmp->diff_mode = TRUE;
1942#else
1943 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1944 mch_errmsg("\n");
1945 mch_exit(2);
1946#endif
1947 }
1948
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001949 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001950 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001951 if (STRNICMP(initstr, "ex", 2) == 0)
1952 {
1953 if (STRNICMP(initstr + 2, "im", 2) == 0)
1954 exmode_active = EXMODE_VIM;
1955 else
1956 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001957 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001958 }
1959}
1960
Bram Moolenaar58d98232005-07-23 22:25:46 +00001961/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001962 * Scan the command line arguments.
1963 */
1964 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001965command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001966{
1967 int argc = parmp->argc;
1968 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001969 int argv_idx; // index in argv[n][]
1970 int had_minmin = FALSE; // found "--" argument
1971 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001972 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001973 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001974 long n;
1975
1976 --argc;
1977 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001978 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001979 while (argc > 0)
1980 {
1981 /*
1982 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1983 */
1984 if (argv[0][0] == '+' && !had_minmin)
1985 {
1986 if (parmp->n_commands >= MAX_ARG_CMDS)
1987 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001988 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001989 if (argv[0][1] == NUL)
1990 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1991 else
1992 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1993 }
1994
1995 /*
1996 * Optional argument.
1997 */
1998 else if (argv[0][0] == '-' && !had_minmin)
1999 {
2000 want_argument = FALSE;
2001 c = argv[0][argv_idx++];
2002#ifdef VMS
2003 /*
2004 * VMS only uses upper case command lines. Interpret "-X" as "-x"
2005 * and "-/X" as "-X".
2006 */
2007 if (c == '/')
2008 {
2009 c = argv[0][argv_idx++];
2010 c = TOUPPER_ASC(c);
2011 }
2012 else
2013 c = TOLOWER_ASC(c);
2014#endif
2015 switch (c)
2016 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002017 case NUL: // "vim -" read from stdin
2018 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002019 if (exmode_active)
2020 silent_mode = TRUE;
2021 else
2022 {
2023 if (parmp->edit_type != EDIT_NONE)
2024 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2025 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002026 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002027 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002028 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002029 break;
2030
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002031 case '-': // "--" don't take any more option arguments
2032 // "--help" give help message
2033 // "--version" give version message
2034 // "--clean" clean context
2035 // "--literal" take files literally
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002036 // "--startuptime fname" write timing info
2037 // "--log fname" start logging early
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002038 // "--nofork" don't fork
2039 // "--not-a-term" don't warn for not a term
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002040 // "--gui-dialog-file fname" write dialog text
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002041 // "--ttyfail" exit if not a term
2042 // "--noplugin[s]" skip plugins
2043 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002044 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2045 usage();
2046 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2047 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002048 Columns = 80; // need to init Columns
2049 info_message = TRUE; // use mch_msg(), not mch_errmsg()
K.Takata33b25d12022-01-13 16:06:45 +00002050#if defined(FEAT_GUI) && !defined(ALWAYS_USE_GUI) && !defined(VIMDLL)
Bram Moolenaar3b678042021-02-11 19:08:05 +01002051 gui.starting = FALSE; // not starting GUI, will exit
Bram Moolenaar0bcadf12021-02-11 19:18:58 +01002052#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002053 list_version();
2054 msg_putchar('\n');
2055 msg_didout = FALSE;
2056 mch_exit(0);
2057 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002058 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2059 {
2060 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002061#ifdef FEAT_GUI
2062 use_gvimrc = (char_u *)"NONE";
2063#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002064 parmp->clean = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002065 set_option_value_give_err((char_u *)"vif",
2066 0L, (char_u *)"NONE", 0);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002067 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002068 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2069 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002070#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071 parmp->literal = TRUE;
2072#endif
2073 }
2074 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2075 {
2076#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002077 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002078#endif
2079 }
2080 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2081 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002082 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2083 parmp->not_a_term = TRUE;
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002084 else if (STRNICMP(argv[0] + argv_idx, "gui-dialog-file", 15)
2085 == 0)
2086 {
2087 want_argument = TRUE;
2088 argv_idx += 15;
2089 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002090 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2091 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002092 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2093 {
2094 want_argument = TRUE;
2095 argv_idx += 3;
2096 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002097 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2098 {
2099 want_argument = TRUE;
2100 argv_idx += 11;
2101 }
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002102 else if (STRNICMP(argv[0] + argv_idx, "log", 3) == 0)
2103 {
2104 want_argument = TRUE;
2105 argv_idx += 3;
2106 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002107#ifdef FEAT_CLIENTSERVER
2108 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002109 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002110 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2111 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2112 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002113 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002114 if (argc > 1)
2115 {
2116 --argc;
2117 ++argv;
2118 }
2119 }
2120#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002121#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002122# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002123 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002124# else
2125 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2126# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002127 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002128 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002129 if (argc > 1)
2130 {
2131 --argc;
2132 ++argv;
2133 }
2134 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002135#endif
2136#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002137 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2138 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002139 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002140 }
2141#endif
2142 else
2143 {
2144 if (argv[0][argv_idx])
2145 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2146 had_minmin = TRUE;
2147 }
2148 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002149 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002150 break;
2151
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002152 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002153#ifdef FEAT_ARABIC
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002154 set_option_value_give_err((char_u *)"arabic", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002155#else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00002156 mch_errmsg(_(e_arabic_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002157 mch_exit(2);
2158#endif
2159 break;
2160
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002161 case 'b': // "-b" binary mode
2162 // Needs to be effective before expanding file names, because
2163 // for Win32 this makes us edit a shortcut file itself,
2164 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002165 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002166 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002167 break;
2168
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002169 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002170 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002171 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002172 break;
2173
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002174 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002175 exmode_active = EXMODE_NORMAL;
2176 break;
2177
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002178 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002179 exmode_active = EXMODE_VIM;
2180 break;
2181
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002182 case 'f': // "-f" GUI: run in foreground. Amiga: open
2183 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002184#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002185 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002186#endif
2187 break;
2188
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002189 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002190 main_start_gui();
2191 break;
2192
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002193 case 'F': // "-F" was for Farsi mode
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002194 mch_errmsg(_(e_farsi_support_has_been_removed));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002195 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002196 break;
2197
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002198 case '?': // "-?" give help message (for MS-Windows)
2199 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002200#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002201 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002202 gui.starting = FALSE;
2203#endif
2204 usage();
2205 break;
2206
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002207 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002208#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002209 p_hkmap = TRUE;
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002210 set_option_value_give_err((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002211#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002212 mch_errmsg(_(e_hebrew_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002213 mch_exit(2);
2214#endif
2215 break;
2216
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002217 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002218 set_option_value_give_err((char_u *)"lisp", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002219 p_sm = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002220 break;
2221
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002222 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002223 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002224 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002225
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002226 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002227 p_write = FALSE;
2228 break;
2229
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002230 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002231#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002232 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002233#endif
2234 parmp->evim_mode = TRUE;
2235 break;
2236
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002237 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002238 change_compatible(FALSE);
2239 break;
2240
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002241 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002242#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002243 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002244 if (argv[0][argv_idx] == 'b')
2245 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002246 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002247 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002248 }
2249 else
2250#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002251 parmp->no_swap_file = TRUE;
2252 break;
2253
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002254 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002255#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002256 // For some reason on MacOS X, an argument like:
2257 // -psn_0_10223617 is passed in when invoke from Finder
2258 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002259 if (argv[0][argv_idx] == 's')
2260 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002261 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002262 main_start_gui();
2263 break;
2264 }
2265#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002266 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002267 parmp->window_count = get_number_arg((char_u *)argv[0],
2268 &argv_idx, 0);
2269 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002270 break;
2271
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002272 case 'o': // "-o[N]" open N horizontal split windows
2273 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002274 parmp->window_count = get_number_arg((char_u *)argv[0],
2275 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002276 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002277 break;
2278
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002279 case 'O': // "-O[N]" open N vertical split windows
2280 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002281 parmp->window_count = get_number_arg((char_u *)argv[0],
2282 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002283 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002284 break;
2285
2286#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002287 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002288 if (parmp->edit_type != EDIT_NONE)
2289 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2290 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002291 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002292 {
2293 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2294 argv_idx = -1;
2295 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002296 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002297 want_argument = TRUE;
2298 break;
2299#endif
2300
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002301 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002302 readonlymode = TRUE;
2303 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002304 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002305 break;
2306
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002307 case 'r': // "-r" recovery mode
2308 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002309 recoverymode = 1;
2310 break;
2311
2312 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002313 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002314 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002315 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002316 want_argument = TRUE;
2317 break;
2318
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002319 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002320 if (parmp->edit_type != EDIT_NONE)
2321 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2322 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002323 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002324 {
2325 parmp->tagname = (char_u *)argv[0] + argv_idx;
2326 argv_idx = -1;
2327 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002328 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002329 want_argument = TRUE;
2330 break;
2331
2332#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002333 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002334 parmp->use_debug_break_level = 9999;
2335 break;
2336#endif
2337#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002338 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002339# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002340 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002341 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2342 want_argument = TRUE;
2343 else
2344# endif
2345 parmp->diff_mode = TRUE;
2346 break;
2347#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002348 case 'V': // "-V{N}" Verbose level
2349 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002350 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2351 if (argv[0][argv_idx] != NUL)
2352 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002353 set_option_value_give_err((char_u *)"verbosefile",
2354 0L, (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002355 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002356 }
2357 break;
2358
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002359 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002360 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002361#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002362 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002363#endif
2364 break;
2365
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002366 case 'w': // "-w{number}" set window height
2367 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002368 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2369 {
2370 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002371 set_option_value_give_err((char_u *)"window", n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002372 break;
2373 }
2374 want_argument = TRUE;
2375 break;
2376
2377#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002378 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002379 parmp->ask_for_key = TRUE;
2380 break;
2381#endif
2382
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002383 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002384#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2385 x_no_connect = TRUE;
2386#endif
2387 break;
2388
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002389 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002390 restricted = TRUE;
2391 break;
2392
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002393 case 'c': // "-c{command}" or "-c {command}" execute
2394 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002395 if (argv[0][argv_idx] != NUL)
2396 {
2397 if (parmp->n_commands >= MAX_ARG_CMDS)
2398 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002399 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2400 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002401 argv_idx = -1;
2402 break;
2403 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002404 // FALLTHROUGH
2405 case 'S': // "-S {file}" execute Vim script
2406 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002407#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002408 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002409#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002410 case 'T': // "-T {terminal}" terminal name
2411 case 'u': // "-u {vimrc}" vim inits file
2412 case 'U': // "-U {gvimrc}" gvim inits file
2413 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002414#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002415 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002416#endif
2417 want_argument = TRUE;
2418 break;
2419
2420 default:
2421 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2422 }
2423
2424 /*
2425 * Handle option arguments with argument.
2426 */
2427 if (want_argument)
2428 {
2429 /*
2430 * Check for garbage immediately after the option letter.
2431 */
2432 if (argv[0][argv_idx] != NUL)
2433 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2434
2435 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002436 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 mainerr_arg_missing((char_u *)argv[0]);
2438 ++argv;
2439 argv_idx = -1;
2440
2441 switch (c)
2442 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002443 case 'c': // "-c {command}" execute command
2444 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002445 if (parmp->n_commands >= MAX_ARG_CMDS)
2446 mainerr(ME_EXTRA_CMD, NULL);
2447 if (c == 'S')
2448 {
2449 char *a;
2450
2451 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002452 // "-S" without argument: use default session file
2453 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454 a = SESSION_FILE;
2455 else if (argv[0][0] == '-')
2456 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002457 // "-S" followed by another option: use default
2458 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459 a = SESSION_FILE;
2460 ++argc;
2461 --argv;
2462 }
2463 else
2464 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002465 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002466 if (p == NULL)
2467 mch_exit(2);
2468 sprintf((char *)p, "so %s", a);
2469 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2470 parmp->commands[parmp->n_commands++] = p;
2471 }
2472 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002473 parmp->commands[parmp->n_commands++] =
2474 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002475 break;
2476
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002477 case '-':
2478 if (argv[-1][2] == 'c')
2479 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002480 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002481 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2482 mainerr(ME_EXTRA_CMD, NULL);
2483 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002484 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002485 }
Bram Moolenaar2d12c252022-06-13 21:42:45 +01002486 // --gui-dialog-file fname
2487 if (argv[-1][2] == 'g')
2488 {
2489 // without GUI ignore the argument
2490#ifdef FEAT_GUI
2491 parmp->gui_dialog_file = (char_u *)argv[0];
2492#endif
2493 }
2494
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002495 // "--startuptime <file>" already handled
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01002496 // "--log <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002497 break;
2498
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002499 // case 'd': -d {device} is handled in mch_check_win() for the
2500 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002501
2502#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002503 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002504 parmp->use_ef = (char_u *)argv[0];
2505 break;
2506#endif
2507
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002508 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002509 set_option_value_give_err((char_u *)"vif",
2510 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002511 break;
2512
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002513 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002514 if (scriptin[0] != NULL)
2515 {
2516scripterror:
2517 mch_errmsg(_("Attempt to open script file again: \""));
2518 mch_errmsg(argv[-1]);
2519 mch_errmsg(" ");
2520 mch_errmsg(argv[0]);
2521 mch_errmsg("\"\n");
2522 mch_exit(2);
2523 }
2524 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2525 {
2526 mch_errmsg(_("Cannot open for reading: \""));
2527 mch_errmsg(argv[0]);
2528 mch_errmsg("\"\n");
2529 mch_exit(2);
2530 }
2531 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002532 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002533 break;
2534
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002535 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002536 parmp->tagname = (char_u *)argv[0];
2537 break;
2538
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002539 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002540 /*
2541 * The -T term argument is always available and when
2542 * HAVE_TERMLIB is supported it overrides the environment
2543 * variable TERM.
2544 */
2545#ifdef FEAT_GUI
2546 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002547 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002548 else
2549#endif
2550 parmp->term = (char_u *)argv[0];
2551 break;
2552
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002553 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002554 parmp->use_vimrc = (char_u *)argv[0];
2555 break;
2556
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002557 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558#ifdef FEAT_GUI
2559 use_gvimrc = (char_u *)argv[0];
2560#endif
2561 break;
2562
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002563 case 'w': // "-w {nr}" 'window' value
2564 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002565 if (vim_isdigit(*((char_u *)argv[0])))
2566 {
2567 argv_idx = 0;
2568 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01002569 set_option_value_give_err((char_u *)"window",
2570 n, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002571 argv_idx = -1;
2572 break;
2573 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002574 // FALLTHROUGH
2575 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002576 if (scriptout != NULL)
2577 goto scripterror;
2578 if ((scriptout = mch_fopen(argv[0],
2579 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2580 {
2581 mch_errmsg(_("Cannot open for script output: \""));
2582 mch_errmsg(argv[0]);
2583 mch_errmsg("\"\n");
2584 mch_exit(2);
2585 }
2586 break;
2587
Bram Moolenaar4f974752019-02-17 17:44:42 +01002588#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002589 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590 gui_mch_set_parent(argv[0]);
2591 break;
2592#endif
2593 }
2594 }
2595 }
2596
2597 /*
2598 * File name argument.
2599 */
2600 else
2601 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002602 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002603
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002604 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002605 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2606 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2607 parmp->edit_type = EDIT_FILE;
2608
2609#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002610 // Remember if the argument was a full path before changing
2611 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002612 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2613 parmp->full_path = TRUE;
2614#endif
2615
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002616 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002617 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2618 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2619 mch_exit(2);
2620#ifdef FEAT_DIFF
2621 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2622 && !mch_isdir(alist_name(&GARGLIST[0])))
2623 {
2624 char_u *r;
2625
2626 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2627 if (r != NULL)
2628 {
2629 vim_free(p);
2630 p = r;
2631 }
2632 }
2633#endif
K.Takatab247e062022-02-07 10:45:23 +00002634#ifdef __CYGWIN__
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002635 /*
2636 * If vim is invoked by non-Cygwin tools, convert away any
2637 * DOS paths, so things like .swp files are created correctly.
2638 * Look for evidence of non-Cygwin paths before we bother.
2639 * This is only for when using the Unix files.
2640 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002641 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002642 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002643 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002644
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002645# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002646 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002647# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002648 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002649# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002650 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002651 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652 if (p == NULL)
2653 mch_exit(2);
2654 }
2655#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002656
2657#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002658 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002659 fname_case(p, 0);
2660#endif
2661
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002662 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002663#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002664 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002665#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002666 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667#endif
2668 );
2669
Bram Moolenaar4f974752019-02-17 17:44:42 +01002670#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002671 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002672 // Remember this argument has been added to the argument list.
2673 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002674 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002675# ifdef FEAT_DIFF
2676 parmp->diff_mode
2677# else
2678 FALSE
2679# endif
2680 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002681 }
2682#endif
2683 }
2684
2685 /*
2686 * If there are no more letters after the current "-", go to next
2687 * argument. argv_idx is set to -1 when the current argument is to be
2688 * skipped.
2689 */
2690 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2691 {
2692 --argc;
2693 ++argv;
2694 argv_idx = 1;
2695 }
2696 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002697
2698#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002699 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2700 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002701 if (parmp->n_commands > 0)
2702 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002703 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002704 if (p != NULL)
2705 {
2706 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2707 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2708 vim_free(p);
2709 }
2710 }
2711#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002712}
2713
2714/*
2715 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002716 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717 */
2718 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002719check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002720{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002721 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002722
2723 input_isatty = mch_input_isatty();
2724 if (exmode_active)
2725 {
2726 if (!input_isatty)
2727 silent_mode = TRUE;
2728 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002729 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002730#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002731 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002732 && !gui.starting
2733#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002734 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002735 {
2736#ifdef NBDEBUG
2737 /*
2738 * This shouldn't be necessary. But if I run netbeans with the log
2739 * output coming to the console and XOpenDisplay fails, I get vim
2740 * trying to start with input/output to my console tty. This fills my
2741 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002742 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002743 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002744 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002745 {
2746 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2747 exit(1);
2748 }
2749#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002750#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2751 if (
2752# ifdef VIMDLL
2753 !gui.starting &&
2754# endif
2755 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002756 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002757# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002758 && defined(FEAT_GETTEXT)
2759 char *s, *tofree = NULL;
2760
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002761 // Set the encoding of the error message based on $LC_ALL or
2762 // other environment variables instead of 'encoding'.
2763 // Note that the message is shown on a Cygwin terminal (e.g.
2764 // mintty) which encoding is based on $LC_ALL or etc., not the
2765 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002766 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002767 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002768 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002769 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2770 vim_free(tofree);
2771# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002772 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2773 exit(1);
2774 }
2775#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002776 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002777 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2778 if (!input_isatty)
2779 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2780 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002781 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2782 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002784 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785 TIME_MSG("Warning delay");
2786 }
2787}
2788
2789/*
2790 * Read text from stdin.
2791 */
2792 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002793read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794{
2795 int i;
2796
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002797 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002798 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002799
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002800 no_wait_return = TRUE;
2801 i = msg_didany;
2802 set_buflisted(TRUE);
Bram Moolenaar204ade62020-10-11 14:58:46 +02002803
2804 // Create memfile and read from stdin.
Bram Moolenaar204ade62020-10-11 14:58:46 +02002805 (void)open_buffer(TRUE, NULL, 0);
2806
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002807 no_wait_return = FALSE;
2808 msg_didany = i;
2809 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002810
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002811 check_swap_exists_action();
Bram Moolenaar8e1cbb52020-12-08 19:36:21 +01002812
2813#if !(defined(AMIGA) || defined(MACOS_X))
2814 // Dup stdin from stderr to read commands from, so that shell commands
2815 // work.
2816 // TODO: why is this needed, even though readfile() has done this?
2817 close(0);
2818 vim_ignored = dup(2);
2819#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002820}
2821
2822/*
2823 * Create the requested number of windows and edit buffers in them.
2824 * Also does recovery if "recoverymode" set.
2825 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002826 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002827create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002828{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002829 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002830 int done = 0;
2831
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002832 /*
2833 * Create the number of windows that was requested.
2834 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002835 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002836 parmp->window_count = 1;
2837 if (parmp->window_count == 0)
2838 parmp->window_count = GARGCOUNT;
2839 if (parmp->window_count > 1)
2840 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002841 // Don't change the windows if there was a command in .vimrc that
2842 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002843 if (parmp->window_layout == 0)
2844 parmp->window_layout = WIN_HOR;
2845 if (parmp->window_layout == WIN_TABS)
2846 {
2847 parmp->window_count = make_tabpages(parmp->window_count);
2848 TIME_MSG("making tab pages");
2849 }
2850 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002851 {
2852 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002853 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002854 TIME_MSG("making windows");
2855 }
2856 else
2857 parmp->window_count = win_count();
2858 }
2859 else
2860 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002861
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002862 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002863 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002864 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002865 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002866 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002867 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002868 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002869 }
2870 else
2871 {
2872 /*
2873 * Open a buffer for windows that don't have one yet.
2874 * Commands in the .vimrc might have loaded a file or split the window.
2875 * Watch out for autocommands that delete a window.
2876 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002877 /*
2878 * Don't execute Win/Buf Enter/Leave autocommands here
2879 */
2880 ++autocmd_no_enter;
2881 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002882 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002883 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002884 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002885 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002886 {
2887 if (parmp->window_layout == WIN_TABS)
2888 goto_tabpage(1);
2889 else
2890 curwin = firstwin;
2891 }
2892 else if (parmp->window_layout == WIN_TABS)
2893 {
2894 if (curtab->tp_next == NULL)
2895 break;
2896 goto_tabpage(0);
2897 }
2898 else
2899 {
2900 if (curwin->w_next == NULL)
2901 break;
2902 curwin = curwin->w_next;
2903 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002904 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002905 curbuf = curwin->w_buffer;
2906 if (curbuf->b_ml.ml_mfp == NULL)
2907 {
2908#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002909 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002910 if (p_fdls >= 0)
2911 curwin->w_p_fdl = p_fdls;
2912#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002913 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002914 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002915
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002916 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002917
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002918 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002919 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002920
Bram Moolenaar84212822006-11-07 21:59:47 +00002921 if (swap_exists_action == SEA_QUIT)
2922 {
2923 if (got_int || only_one_window())
2924 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002925 // abort selected or quit and only one window
2926 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002927 getout(1);
2928 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002929 // We can't close the window, it would disturb what
2930 // happens next. Clear the file name and set the arg
2931 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002932 setfname(curbuf, NULL, NULL, FALSE);
2933 curwin->w_arg_idx = -1;
2934 swap_exists_action = SEA_NONE;
2935 }
2936 else
2937 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002938 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002939 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002940 ui_breakcheck();
2941 if (got_int)
2942 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002943 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002944 break;
2945 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002946 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002947 if (parmp->window_layout == WIN_TABS)
2948 goto_tabpage(1);
2949 else
2950 curwin = firstwin;
2951 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002952 --autocmd_no_enter;
2953 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002954 }
2955}
2956
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002957/*
2958 * If opened more than one window, start editing files in the other
2959 * windows. make_windows() has already opened the windows.
2960 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002961 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002962edit_buffers(
2963 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002964 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002965{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002966 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002967 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002968 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002969 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002970 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002971
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002972 /*
2973 * Don't execute Win/Buf Enter/Leave autocommands here
2974 */
2975 ++autocmd_no_enter;
2976 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002977
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002978 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002979 if (curwin->w_arg_idx == -1)
2980 {
2981 win_close(curwin, TRUE);
2982 advance = FALSE;
2983 }
2984
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002985 arg_idx = 1;
2986 for (i = 1; i < parmp->window_count; ++i)
2987 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002988 if (cwd != NULL)
2989 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002990 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002991 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002992 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002993 ++arg_idx;
2994 win_close(curwin, TRUE);
2995 advance = FALSE;
2996 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002997 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002998
Bram Moolenaar84212822006-11-07 21:59:47 +00002999 if (advance)
3000 {
3001 if (parmp->window_layout == WIN_TABS)
3002 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003003 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003004 break;
3005 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003006 // Temporarily reset 'shm' option to not print fileinfo when
3007 // loading the other buffers. This would overwrite the already
3008 // existing fileinfo for the first tab.
3009 if (i == 1)
3010 {
3011 char buf[100];
3012
3013 p_shm_save = vim_strsave(p_shm);
3014 vim_snprintf(buf, 100, "F%s", p_shm);
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003015 set_option_value_give_err((char_u *)"shm",
3016 0L, (char_u *)buf, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003017 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003018 }
3019 else
3020 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003021 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003022 break;
3023 win_enter(curwin->w_next, FALSE);
3024 }
3025 }
3026 advance = TRUE;
3027
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003028 // Only open the file if there is no file in this window yet (that can
3029 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003030 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3031 {
3032 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003033 // Edit file from arg list, if there is one. When "Quit" selected
3034 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003035 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003036 (void)do_ecmd(0, arg_idx < GARGCOUNT
3037 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003038 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003039 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003040 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003041 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003042 if (got_int || only_one_window())
3043 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003044 // abort selected and only one window
3045 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003046 getout(1);
3047 }
3048 win_close(curwin, TRUE);
3049 advance = FALSE;
3050 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003051 if (arg_idx == GARGCOUNT - 1)
3052 arg_had_last = TRUE;
3053 ++arg_idx;
3054 }
3055 ui_breakcheck();
3056 if (got_int)
3057 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003058 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003059 break;
3060 }
3061 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003062
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003063 if (p_shm_save != NULL)
3064 {
Bram Moolenaar31e5c602022-04-15 13:53:33 +01003065 set_option_value_give_err((char_u *)"shm", 0L, p_shm_save, 0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003066 vim_free(p_shm_save);
3067 }
3068
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003069 if (parmp->window_layout == WIN_TABS)
3070 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003071 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003072
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003073 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003074 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003075#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003076 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003077 while (win->w_p_pvw)
3078 {
3079 win = win->w_next;
3080 if (win == NULL)
3081 {
3082 win = firstwin;
3083 break;
3084 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003085 }
3086#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003087 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003088
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003089 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003090 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003091 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003092 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003093}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003094
3095/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003096 * Execute the commands from --cmd arguments "cmds[cnt]".
3097 */
3098 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003099exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003100{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003101 char_u **cmds = parmp->pre_commands;
3102 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003103 int i;
ichizok7e5fe382023-04-15 13:17:50 +01003104 ESTACK_CHECK_DECLARATION;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003105
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003106 if (cnt <= 0)
3107 return;
3108
3109 curwin->w_cursor.lnum = 0; // just in case..
3110 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
ichizok7e5fe382023-04-15 13:17:50 +01003111 ESTACK_CHECK_SETUP;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003112# ifdef FEAT_EVAL
ichizok7e5fe382023-04-15 13:17:50 +01003113 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003114# endif
ichizok7e5fe382023-04-15 13:17:50 +01003115 for (i = 0; i < cnt; ++i)
3116 do_cmdline_cmd(cmds[i]);
3117 ESTACK_CHECK_NOW;
3118 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003119# ifdef FEAT_EVAL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003120 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003121# endif
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003122 TIME_MSG("--cmd commands");
Bram Moolenaar58d98232005-07-23 22:25:46 +00003123}
3124
3125/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003126 * Execute "+", "-c" and "-S" arguments.
3127 */
3128 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003129exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003130{
3131 int i;
ichizok7e5fe382023-04-15 13:17:50 +01003132 ESTACK_CHECK_DECLARATION;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003133
3134 /*
3135 * We start commands on line 0, make "vim +/pat file" match a
3136 * pattern on line 1. But don't move the cursor when an autocommand
3137 * with g`" was used.
3138 */
3139 msg_scroll = TRUE;
3140 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3141 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003142 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
ichizok7e5fe382023-04-15 13:17:50 +01003143 ESTACK_CHECK_SETUP;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003144#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003145 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003146 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003147#endif
3148 for (i = 0; i < parmp->n_commands; ++i)
3149 {
3150 do_cmdline_cmd(parmp->commands[i]);
3151 if (parmp->cmds_tofree[i])
3152 vim_free(parmp->commands[i]);
3153 }
ichizok7e5fe382023-04-15 13:17:50 +01003154 ESTACK_CHECK_NOW;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003155 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003156#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003157 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003158#endif
3159 if (curwin->w_cursor.lnum == 0)
3160 curwin->w_cursor.lnum = 1;
3161
3162 if (!exmode_active)
3163 msg_scroll = FALSE;
3164
3165#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003166 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003167 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003168 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003169#endif
3170 TIME_MSG("executing command arguments");
3171}
3172
3173/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003174 * Source startup scripts.
3175 */
3176 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003177source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003178{
3179 int i;
3180
3181 /*
3182 * For "evim" source evim.vim first of all, so that the user can overrule
3183 * any things he doesn't like.
3184 */
3185 if (parmp->evim_mode)
3186 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003188 TIME_MSG("source evim file");
3189 }
3190
3191 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003192 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003193 * nothing else.
3194 */
3195 if (parmp->use_vimrc != NULL)
3196 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003197 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003198 {
3199 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL)
3200 != OK)
Bram Moolenaar50809a42023-05-20 16:39:07 +01003201 emsg(_(e_failed_to_source_defaults));
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003202 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003203 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003204 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003205 {
3206#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003207 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003208 use_gvimrc = parmp->use_vimrc;
3209#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003210 }
3211 else
3212 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00003214 semsg(_(e_cannot_read_from_str_2), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003215 }
3216 }
3217 else if (!silent_mode)
3218 {
3219#ifdef AMIGA
3220 struct Process *proc = (struct Process *)FindTask(0L);
3221 APTR save_winptr = proc->pr_WindowPtr;
3222
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003223 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003224 proc->pr_WindowPtr = (APTR)-1L;
3225#endif
3226
3227 /*
3228 * Get system wide defaults, if the file name is defined.
3229 */
3230#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003232#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003233#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3235 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003236#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003237
3238 /*
3239 * Try to read initialization commands from the following places:
3240 * - environment variable VIMINIT
3241 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3242 * - second user vimrc file ($VIM/.vimrc for Dos)
3243 * - environment variable EXINIT
3244 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3245 * - second user exrc file ($VIM/.exrc for Dos)
3246 * The first that exists is used, the rest is ignored.
3247 */
3248 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3249 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3251 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003252#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003253 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003255#endif
3256#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003257 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003259#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003260#ifdef USR_VIMRC_FILE4
3261 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003263#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003264 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3266 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003267#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3269 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003270#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003271 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003272 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003273 // When no .vimrc file was found: source defaults.vim.
Christian Brabandt1d3a14e2021-05-29 19:53:50 +02003274 if (do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE,
3275 NULL) == FAIL)
Bram Moolenaar50809a42023-05-20 16:39:07 +01003276 emsg(_(e_failed_to_source_defaults));
Bram Moolenaar58d98232005-07-23 22:25:46 +00003277 }
3278 }
3279
3280 /*
3281 * Read initialization commands from ".vimrc" or ".exrc" in current
3282 * directory. This is only done if the 'exrc' option is set.
3283 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003284 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003285 * option has been reset in environment of global ".exrc" or ".vimrc".
3286 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3287 * SYS_VIMRC_FILE.
3288 */
3289 if (p_exrc)
3290 {
3291#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003292 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003293 if (!file_owned(VIMRC_FILE))
3294#endif
3295 secure = p_secure;
3296
3297 i = FAIL;
3298 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003299 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003300#ifdef USR_VIMRC_FILE2
3301 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003302 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003303#endif
3304#ifdef USR_VIMRC_FILE3
3305 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003306 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003307#endif
3308#ifdef SYS_VIMRC_FILE
3309 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003310 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003311#endif
3312 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003314
3315 if (i == FAIL)
3316 {
3317#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003318 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003319 if (!file_owned(EXRC_FILE))
3320 secure = p_secure;
3321 else
3322 secure = 0;
3323#endif
3324 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003325 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003326#ifdef USR_EXRC_FILE2
3327 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003328 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003329#endif
3330 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 (void)do_source((char_u *)EXRC_FILE, FALSE,
3332 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003333 }
3334 }
3335 if (secure == 2)
3336 need_wait_return = TRUE;
3337 secure = 0;
3338#ifdef AMIGA
3339 proc->pr_WindowPtr = save_winptr;
3340#endif
3341 }
3342 TIME_MSG("sourcing vimrc file(s)");
3343}
3344
3345/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003346 * Setup to start using the GUI. Exit with an error when not available.
3347 */
3348 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003349main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350{
3351#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003352 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353#else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003354 mch_errmsg(_(e_gui_cannot_be_used_not_enabled_at_compile_time));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003355 mch_errmsg("\n");
3356 mch_exit(2);
3357#endif
3358}
3359
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003360#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003361
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003362/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003364 * Returns FAIL if the environment variable was not executed, OK otherwise.
3365 */
3366 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003367process_env(
3368 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003369 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003370{
3371 char_u *initstr;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003372 sctx_T save_current_sctx;
ichizok7e5fe382023-04-15 13:17:50 +01003373 ESTACK_CHECK_DECLARATION;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003374
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003375 if ((initstr = mch_getenv(env)) == NULL || *initstr == NUL)
3376 return FAIL;
3377
3378 if (is_viminit)
3379 vimrc_found(NULL, NULL);
3380 estack_push(ETYPE_ENV, env, 0);
ichizok7e5fe382023-04-15 13:17:50 +01003381 ESTACK_CHECK_SETUP;
3382 save_current_sctx = current_sctx;
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003383 current_sctx.sc_version = 1;
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003384#ifdef FEAT_EVAL
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003385 current_sctx.sc_sid = SID_ENV;
3386 current_sctx.sc_seq = 0;
3387 current_sctx.sc_lnum = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388#endif
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01003389
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003390 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003391
ichizok7e5fe382023-04-15 13:17:50 +01003392 ESTACK_CHECK_NOW;
3393 estack_pop();
Yegappan Lakshmanane8575982023-01-14 12:32:28 +00003394 current_sctx = save_current_sctx;
3395 return OK;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396}
3397
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003398#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399/*
3400 * Return TRUE if we are certain the user owns the file "fname".
3401 * Used for ".vimrc" and ".exrc".
3402 * Use both stat() and lstat() for extra security.
3403 */
3404 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003405file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003407 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408# ifdef UNIX
3409 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003410# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003411 uid_t uid = ((getgid() << 16) | getuid());
3412# endif
3413
3414 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3415# ifdef HAVE_LSTAT
3416 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3417# endif
3418 );
3419}
3420#endif
3421
3422/*
3423 * Give an error message main_errors["n"] and exit.
3424 */
3425 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003426mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003427 int n, // one of the ME_ defines
3428 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003429{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003430#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003431 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432#endif
3433
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003434 // If this is a Windows GUI executable, show an error dialog box.
3435#ifdef VIMDLL
3436 gui.in_use = mch_is_gui_executable();
3437#endif
3438#ifdef FEAT_GUI_MSWIN
3439 gui.starting = FALSE; // Needed to show as error.
3440#endif
3441
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003442 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003444 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003445 mch_errmsg(_(main_errors[n]));
3446 if (str != NULL)
3447 {
3448 mch_errmsg(": \"");
3449 mch_errmsg((char *)str);
3450 mch_errmsg("\"");
3451 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003452 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003453
3454 mch_exit(1);
3455}
3456
3457 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003458mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003459{
3460 mainerr(ME_ARG_MISSING, str);
3461}
3462
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003463#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464/*
3465 * print a message with three spaces prepended and '\n' appended.
3466 */
3467 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003468main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003469{
3470 mch_msg(" ");
3471 mch_msg(s);
3472 mch_msg("\n");
3473}
3474
3475/*
3476 * Print messages for "vim -h" or "vim --help" and exit.
3477 */
3478 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003479usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003480{
3481 int i;
3482 static char *(use[]) =
3483 {
3484 N_("[file ..] edit specified file(s)"),
3485 N_("- read text from stdin"),
3486 N_("-t tag edit file where tag is defined"),
3487#ifdef FEAT_QUICKFIX
3488 N_("-q [errorfile] edit file with first error")
3489#endif
3490 };
3491
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003492#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003493 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003494#endif
3495
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003496 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003497 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003498 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499 for (i = 0; ; ++i)
3500 {
3501 mch_msg(_(" vim [arguments] "));
3502 mch_msg(_(use[i]));
K.Takataeeec2542021-06-02 13:28:16 +02003503 if (i == ARRAY_LENGTH(use) - 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003504 break;
3505 mch_msg(_("\n or:"));
3506 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003507#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003508 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003509#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003510
3511 mch_msg(_("\n\nArguments:\n"));
3512 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003513#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003514 main_msg(_("--literal\t\tDon't expand wildcards"));
3515#endif
3516#ifdef FEAT_OLE
3517 main_msg(_("-register\t\tRegister this gvim for OLE"));
3518 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3519#endif
3520#ifdef FEAT_GUI
3521 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3522 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3523#endif
3524 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3525 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003526 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3528#ifdef FEAT_DIFF
3529 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3530#endif
3531 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3532 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3533 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3534 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3535 main_msg(_("-M\t\t\tModifications in text not allowed"));
3536 main_msg(_("-b\t\t\tBinary mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003537 main_msg(_("-l\t\t\tLisp mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003538 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3539 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003540 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3541#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003542 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003543#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003544 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3545 main_msg(_("-r\t\t\tList swap files and exit"));
3546 main_msg(_("-r (with file name)\tRecover crashed session"));
3547 main_msg(_("-L\t\t\tSame as -r"));
3548#ifdef AMIGA
3549 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3550 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3551#endif
3552#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003553 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003554#endif
3555#ifdef FEAT_RIGHTLEFT
3556 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3557#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003558 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003559 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2d12c252022-06-13 21:42:45 +01003560#ifdef FEAT_GUI
3561 main_msg(_("--gui-dialog-file {fname} For testing: write dialog text"));
3562#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003563 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003564 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3565#ifdef FEAT_GUI
3566 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3567#endif
3568 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003569 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003570 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3571 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3572 main_msg(_("+\t\t\tStart at end of file"));
3573 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003574 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003575 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3576 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3577 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3578 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3579 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3580#ifdef FEAT_CRYPT
3581 main_msg(_("-x\t\t\tEdit encrypted files"));
3582#endif
3583#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3584# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003585 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003586# endif
3587 main_msg(_("-X\t\t\tDo not connect to X server"));
3588#endif
3589#ifdef FEAT_CLIENTSERVER
3590 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3591 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3592 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3593 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003594 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003595 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3596 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3597 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3598 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3599#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003600#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003601 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003602#endif
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003603#ifdef FEAT_JOB_CHANNEL
K.Takata0fd7be72022-11-09 16:29:24 +00003604 main_msg(_("--log <file>\t\tStart logging to <file> early"));
Bram Moolenaarc9a9a0a2022-04-12 15:09:23 +01003605#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606#ifdef FEAT_VIMINFO
3607 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3608#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003609 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003610 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3611 main_msg(_("--version\t\tPrint version information and exit"));
3612
3613#ifdef FEAT_GUI_X11
3614# ifdef FEAT_GUI_MOTIF
3615 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003616# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003617 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003618 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003619 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3620 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3621 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3622 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3623 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3624 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3625 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3626 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003627 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3628 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3629 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003630#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003631#ifdef FEAT_GUI_GTK
3632 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003633 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3634 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003635 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3636 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
Bram Moolenaarf8c52e82021-03-17 12:27:23 +01003637 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003638 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003639 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003640 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003641 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003642 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003643#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003644#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003645# ifdef VIMDLL
3646 if (gui.starting)
3647# endif
3648 {
3649 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3650 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3651 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003652#endif
3653
3654#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003655 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003656 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003657 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003658 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003659 gui.dofork = FALSE;
3660 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003661 else
3662#endif
3663 mch_exit(0);
3664}
3665
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003666/*
3667 * Check the result of the ATTENTION dialog:
3668 * When "Quit" selected, exit Vim.
3669 * When "Recover" selected, recover the file.
3670 */
3671 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003672check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003673{
3674 if (swap_exists_action == SEA_QUIT)
3675 getout(1);
3676 handle_swap_exists(NULL);
3677}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003678
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003679#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003680
Bram Moolenaar595297d2017-03-04 19:11:12 +01003681#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003682 static void
3683set_progpath(char_u *argv0)
3684{
3685 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003686
Bram Moolenaar4f974752019-02-17 17:44:42 +01003687# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003688 // A relative path containing a "/" will become invalid when using ":cd",
3689 // turn it into a full path.
3690 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3691 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003692 char_u *path = NULL;
3693
3694 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3695 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003696# else
3697 char_u buf[MAXPATHL + 1];
3698# ifdef PROC_EXE_LINK
3699 char linkbuf[MAXPATHL + 1];
3700 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003701
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003702 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3703 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003704 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003705 linkbuf[len] = NUL;
3706 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003707 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003708# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003709
3710 if (!mch_isFullName(val))
3711 {
3712 if (gettail(val) != val
3713 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3714 val = buf;
3715 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003716# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003717
Bram Moolenaar08cab962017-03-04 14:37:18 +01003718 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003719
Bram Moolenaar4f974752019-02-17 17:44:42 +01003720# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003721 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003722# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003723}
3724
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003725#endif // NO_VIM_MAIN