blob: 39e67e998793cb58d9c5efbcac63263287243b88 [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__
Bram Moolenaar4f974752019-02-17 17:44:42 +010014# ifndef MSWIN
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010016# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
17 // cygwin_conv_path()
Bram Moolenaarb4210b32004-06-13 14:51:16 +000018# endif
19# include <limits.h>
20#endif
21
Bram Moolenaarafde13b2019-04-28 19:46:49 +020022#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020023# include "iscygpty.h"
24#endif
25
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010026// Values for edit_type.
27#define EDIT_NONE 0 // no edit type yet
28#define EDIT_FILE 1 // file name argument[s] given, use argument list
29#define EDIT_STDIN 2 // read file from stdin
30#define EDIT_TAG 3 // tag name argument given, use tagname
31#define EDIT_QF 4 // start in quickfix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +000032
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010033#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000035#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020037# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
38static void init_locale(void);
39# endif
40static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010041#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void usage(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010043static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010044static void command_line_scan(mparm_T *parmp);
45static void check_tty(mparm_T *parmp);
46static void read_stdin(void);
47static void create_windows(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010048static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010049static void exe_pre_commands(mparm_T *parmp);
50static void exe_commands(mparm_T *parmp);
51static void source_startup_scripts(mparm_T *parmp);
52static void main_start_gui(void);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010053static void check_swap_exists_action(void);
Bram Moolenaar08cab962017-03-04 14:37:18 +010054# ifdef FEAT_EVAL
55static void set_progpath(char_u *argv0);
56# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000057#endif
58
59
Bram Moolenaarb4210b32004-06-13 14:51:16 +000060/*
61 * Different types of error messages.
62 */
63static char *(main_errors[]) =
64{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000065 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000066#define ME_UNKNOWN_OPTION 0
67 N_("Too many edit arguments"),
68#define ME_TOO_MANY_ARGS 1
69 N_("Argument missing after"),
70#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000071 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000072#define ME_GARBAGE 3
73 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
74#define ME_EXTRA_CMD 4
75 N_("Invalid argument for"),
76#define ME_INVALID_ARG 5
77};
78
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010079#ifndef PROTO // don't want a prototype for main()
Bram Moolenaara6044292017-04-02 18:19:53 +020080
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010081// Various parameters passed between main() and other functions.
Bram Moolenaara6044292017-04-02 18:19:53 +020082static mparm_T params;
83
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010084#ifndef NO_VIM_MAIN // skip this for unittests
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020085
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010086static char_u *start_dir = NULL; // current working dir on startup
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020087
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020088static int has_dash_c_arg = FALSE;
89
Bram Moolenaarafde13b2019-04-28 19:46:49 +020090# ifdef VIMDLL
91__declspec(dllexport)
92# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000093 int
Bram Moolenaar796cc422019-04-03 20:31:00 +020094# ifdef MSWIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +000095VimMain
96# else
97main
98# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +010099(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100{
Bram Moolenaar77780b62018-03-01 23:10:45 +0100101#if defined(STARTUPTIME) || defined(CLEAN_RUNTIMEPATH)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000102 int i;
103#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000104
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000105 /*
106 * Do any system-specific initialisations. These can NOT use IObuff or
107 * NameBuff. Thus emsg2() cannot be called!
108 */
109 mch_early_init();
110
Bram Moolenaar4f974752019-02-17 17:44:42 +0100111#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +0200112 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200113 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200114 * convert when 'encoding' changes. Get the unexpanded arguments.
115 */
116 argc = get_cmd_argsW(&argv);
117#endif
118
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100119 // Many variables are in "params" so that we can pass them to invoked
120 // functions without a lot of arguments. "argc" and "argv" are also
121 // copied, so that they can be changed.
Bram Moolenaara80faa82020-04-12 19:37:17 +0200122 CLEAR_FIELD(params);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000123 params.argc = argc;
124 params.argv = argv;
125 params.want_full_screen = TRUE;
126#ifdef FEAT_EVAL
127 params.use_debug_break_level = -1;
128#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000129 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000130
Bram Moolenaar99685e62013-05-11 13:56:18 +0200131#ifdef FEAT_RUBY
132 {
133 int ruby_stack_start;
134 vim_ruby_init((void *)&ruby_stack_start);
135 }
136#endif
137
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000138#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000139 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000140#endif
141
142#ifdef MEM_PROFILE
143 atexit(vim_mem_profile_dump);
144#endif
145
146#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100147 // Need to find "--startuptime" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100148 for (i = 1; i < argc - 1; ++i)
149 if (STRICMP(argv[i], "--startuptime") == 0)
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 ---");
153 break;
154 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000155#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000156 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000157
Bram Moolenaar07268702018-03-01 21:57:32 +0100158#ifdef CLEAN_RUNTIMEPATH
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100159 // Need to find "--clean" before actually parsing arguments.
Bram Moolenaar07268702018-03-01 21:57:32 +0100160 for (i = 1; i < argc; ++i)
161 if (STRICMP(argv[i], "--clean") == 0)
162 {
163 params.clean = TRUE;
164 break;
165 }
166#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200167 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000168
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200169#ifdef VIMDLL
170 // Check if the current executable file is for the GUI subsystem.
171 gui.starting = mch_is_gui_executable();
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +0200172#elif defined(FEAT_GUI_MSWIN)
173 gui.starting = TRUE;
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200174#endif
175
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176#ifdef FEAT_CLIENTSERVER
177 /*
178 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000179 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000180 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000181 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000182#endif
183
184 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000185 * Figure out the way to work from the command name argv[0].
186 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000187 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000188 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000189
190 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 * Process the command line arguments. File names are put in the global
192 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000193 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000194 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195 TIME_MSG("parsing arguments");
196
197 /*
198 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000199 * there is no terminal version, and on Windows we can't fork one off with
200 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000201 */
202#ifdef ALWAYS_USE_GUI
203 gui.starting = TRUE;
204#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000205# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000206 /*
207 * Check if the GUI can be started. Reset gui.starting if not.
208 * Don't know about other systems, stay on the safe side and don't check.
209 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000210 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000211 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000212 if (gui_init_check() == FAIL)
213 {
214 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100216 // When running "evim" or "gvim -y" we need the menus, exit if we
217 // don't have them.
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000218 if (params.evim_mode)
219 mch_exit(1);
220 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000221 }
222# endif
223#endif
224
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000225 if (GARGCOUNT > 0)
226 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100227#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000228 /*
229 * Expand wildcards in file names.
230 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000231 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200233 start_dir = alloc(MAXPATHL);
234 if (start_dir != NULL)
235 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100236 // Temporarily add '(' and ')' to 'isfname'. These are valid
237 // filename characters but are excluded from 'isfname' to make
238 // "gf" work on a file name in parenthesis (e.g.: see vim.h).
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000240 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000241 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200242 if (start_dir != NULL)
243 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 }
245#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200246 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000247 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000248
Bram Moolenaar4f974752019-02-17 17:44:42 +0100249#ifdef MSWIN
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000250 {
251 extern void set_alist_count(void);
252
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100253 // Remember the number of entries in the argument list. If it changes
254 // we don't react on setting 'encoding'.
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000255 set_alist_count();
256 }
257#endif
258
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000260 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000261 {
262 /*
263 * If there is one filename, fully qualified, we have very probably
264 * been invoked from explorer, so change to the file's directory.
265 * Hint: to avoid this when typing a command use a forward slash.
266 * If the cd fails, it doesn't matter.
267 */
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100268 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200269 if (start_dir != NULL)
270 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000271 }
272#endif
273 TIME_MSG("expanding arguments");
274
275#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000276 if (params.diff_mode && params.window_count == -1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100277 params.window_count = 0; // open up to 3 windows
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000278#endif
279
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100280 // Don't redraw until much later.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000281 ++RedrawingDisabled;
282
283 /*
284 * When listing swap file names, don't do cursor positioning et. al.
285 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200286 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000287 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288
289 /*
290 * When certain to start the GUI, don't check capabilities of terminal.
291 * For GTK we can't be sure, but when started from the desktop it doesn't
292 * make sense to try using a terminal.
293 */
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200294#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) \
295 || defined(VIMDLL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000296 if (gui.starting
297# ifdef FEAT_GUI_GTK
298 && !isatty(2)
299# endif
300 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000301 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302#endif
303
Bram Moolenaard0573012017-10-28 21:11:06 +0200304#if defined(FEAT_GUI_MAC) && defined(MACOS_X_DARWIN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100305 // When the GUI is started from Finder, need to display messages in a
306 // message box. isatty(2) returns TRUE anyway, thus we need to check the
307 // name to know we're not started from a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000308 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000309 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000310 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000311
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100312 // Avoid always using "/" as the current directory. Note that when
313 // started from Finder the arglist will be filled later in
314 // HandleODocAE() and "fname" will be NULL.
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000315 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
316 && STRCMP(NameBuff, "/") == 0)
317 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200318 if (params.fname != NULL)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100319 (void)vim_chdirfile(params.fname, "drop");
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000320 else
321 {
322 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
323 vim_chdir(NameBuff);
324 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200325 if (start_dir != NULL)
326 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000327 }
328 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329#endif
330
331 /*
332 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100333 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000334 * Note that we may use mch_exit() before mch_init()!
335 */
336 mch_init();
337 TIME_MSG("shell init");
338
339#ifdef USE_XSMP
340 /*
341 * For want of anywhere else to do it, try to connect to xsmp here.
342 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
343 * Hijacking -X 'no X connection' to also disable XSMP connection as that
344 * has a similar delay upon failure.
345 * Only try if SESSION_MANAGER is set to something non-null.
346 */
347 if (!x_no_connect)
348 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000349 char *p = getenv("SESSION_MANAGER");
350
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000351 if (p != NULL && *p != NUL)
352 {
353 xsmp_init();
354 TIME_MSG("xsmp init");
355 }
356 }
357#endif
358
359 /*
360 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000361 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000362 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000363
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100364#ifdef _IOLBF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100365 // Ensure output works usefully without a tty: buffer lines instead of
366 // fully buffered.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +0100367 if (silent_mode)
368 setvbuf(stdout, NULL, _IOLBF, 0);
369#endif
370
Bram Moolenaar9404a182019-05-03 22:25:40 +0200371 // This message comes before term inits, but after setting "silent_mode"
372 // when the input is not a tty. Omit the message with --not-a-term.
373 if (GARGCOUNT > 1 && !silent_mode && !is_not_a_term())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000374 printf(_("%d files to edit\n"), GARGCOUNT);
375
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000376 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000377 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100378 termcapinit(params.term); // set terminal name and get terminal
379 // capabilities (will set full_screen)
380 screen_start(); // don't know where cursor is now
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000381 TIME_MSG("Termcap init");
382 }
383
384 /*
385 * Set the default values for the options that use Rows and Columns.
386 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100387 ui_get_shellsize(); // inits Rows and Columns
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000388 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100390 // Set the 'diff' option now, so that it can be checked for in a .vimrc
391 // file. There is no buffer yet though.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000392 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000393 diff_win_options(firstwin, FALSE);
394#endif
395
396 cmdline_row = Rows - p_ch;
397 msg_row = cmdline_row;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100398 screenalloc(FALSE); // allocate screen buffers
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399 set_init_2();
400 TIME_MSG("inits 2");
401
402 msg_scroll = TRUE;
403 no_wait_return = TRUE;
404
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100405 init_mappings(); // set up initial mappings
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000406
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100407 init_highlight(TRUE, FALSE); // set the default highlight groups
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000409
Bram Moolenaar0c0eddd2020-06-13 15:47:25 +0200410#if defined(FEAT_TERMRESPONSE)
411 init_term_props(TRUE);
412#endif
413
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000414#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100415 // Set the break level after the terminal is initialized.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000416 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000417#endif
418
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100419 // Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
420 // Allows for setting 'loadplugins' there.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200421 if (params.use_vimrc != NULL
422 && (STRCMP(params.use_vimrc, "NONE") == 0
423 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
424 p_lpl = FALSE;
425
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100426 // Execute --cmd arguments.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200427 exe_pre_commands(&params);
428
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100429 // Source startup scripts.
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200430 source_startup_scripts(&params);
431
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100432#ifdef FEAT_MZSCHEME
433 /*
434 * Newer version of MzScheme (Racket) require earlier (trampolined)
435 * initialisation via scheme_main_setup.
436 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200437 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200438 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100439 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200440 return mzscheme_main();
441#else
442 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100443#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200444}
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100445#endif // NO_VIM_MAIN
446#endif // PROTO
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100447
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200448/*
449 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
450 * things simple.
451 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
452 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100453 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200454vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100455{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100456#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000457#ifdef FEAT_EVAL
458 /*
459 * Read all the plugin files.
460 * Only when compiled with +eval, since most plugins need it.
461 */
462 if (p_lpl)
463 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200464 char_u *rtp_copy = NULL;
465
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100466 // First add all package directories to 'runtimepath', so that their
467 // autoload directories can be found. Only if not done already with a
468 // :packloadall command.
469 // Make a copy of 'runtimepath', so that source_runtime does not use
470 // the pack directories.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200471 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200472 {
473 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200474 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200475 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200476
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200477 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100478# ifdef VMS // Somehow VMS doesn't handle the "**".
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200479 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000480# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200481 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000482# endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483 DIP_ALL | DIP_NOAFTER, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000484 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200485 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100486
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100487 // Only source "start" packages if not done already with a :packloadall
488 // command.
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200489 if (!did_source_packages)
490 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100491 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200492
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100493# ifdef VMS // Somehow VMS doesn't handle the "**".
Bram Moolenaar66459b72016-08-06 19:01:55 +0200494 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
495# else
496 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
497# endif
498 TIME_MSG("loading after plugins");
499
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000500 }
501#endif
502
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000503#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100504 // Decide about window layout for diff mode after reading vimrc.
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000505 if (params.diff_mode && params.window_layout == 0)
506 {
507 if (diffopt_horizontal())
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100508 params.window_layout = WIN_HOR; // use horizontal split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000509 else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100510 params.window_layout = WIN_VER; // use vertical split
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000511 }
512#endif
513
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000514 /*
515 * Recovery mode without a file name: List swap files.
516 * This uses the 'dir' option, therefore it must be after the
517 * initializations.
518 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200519 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000520 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200521 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000522 mch_exit(0);
523 }
524
525 /*
526 * Set a few option defaults after reading .vimrc files:
527 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
528 */
529 set_init_3();
530 TIME_MSG("inits 3");
531
532 /*
533 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
534 * Note that this overrides anything from a vimrc file.
535 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000536 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 p_uc = 0;
538
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539#ifdef FEAT_GUI
540 if (gui.starting)
541 {
Bram Moolenaarc7226682019-08-17 16:33:23 +0200542# if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100543 // When something caused a message from a vimrc script, need to output
544 // an extra newline before the shell prompt.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000545 if (did_emsg || msg_didout)
546 putchar('\n');
Bram Moolenaarc7226682019-08-17 16:33:23 +0200547# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100549 gui_start(NULL); // will set full_screen to TRUE
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000550 TIME_MSG("starting GUI");
551
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100552 // When running "evim" or "gvim -y" we need the menus, exit if we
553 // don't have them.
Bram Moolenaar58d98232005-07-23 22:25:46 +0000554 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555 mch_exit(1);
556 }
557#endif
558
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000559#ifdef FEAT_VIMINFO
560 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000561 * Read in registers, history etc, but not marks, from the viminfo file.
562 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000563 */
564 if (*p_viminfo != NUL)
565 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000566 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000567 TIME_MSG("reading viminfo");
568 }
569#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100570#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100571 // It's better to make v:oldfiles an empty list than NULL.
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100572 if (get_vim_var_list(VV_OLDFILES) == NULL)
573 set_vim_var_list(VV_OLDFILES, list_alloc());
574#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000575
576#ifdef FEAT_QUICKFIX
577 /*
578 * "-q errorfile": Load the error file now.
579 * If the error file can't be read, exit before doing anything else.
580 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000581 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000582 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100583 char_u *enc = NULL;
584
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100585 enc = p_menc;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000586 if (params.use_ef != NULL)
587 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000588 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200589 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100590 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000591 {
592 out_char('\n');
593 mch_exit(3);
594 }
595 TIME_MSG("reading errorfile");
596 }
597#endif
598
599 /*
600 * Start putting things on the screen.
601 * Scroll screen down before drawing over it
602 * Clear screen now, so file message will not be cleared.
603 */
604 starting = NO_BUFFERS;
605 no_wait_return = FALSE;
606 if (!exmode_active)
607 msg_scroll = FALSE;
608
609#ifdef FEAT_GUI
610 /*
611 * This seems to be required to make callbacks to be called now, instead
612 * of after things have been put on the screen, which then may be deleted
613 * when getting a resize callback.
614 * For the Mac this handles putting files dropped on the Vim icon to
615 * global_alist.
616 */
617 if (gui.in_use)
618 {
Bram Moolenaarbb1969b2019-01-17 15:45:25 +0100619 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000620 TIME_MSG("GUI delay");
621 }
622#endif
623
624#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
625 qnx_clip_init();
626#endif
627
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200628#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
629 clip_init(TRUE);
630#endif
631
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000632#ifdef FEAT_XCLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100633 // Start using the X clipboard, unless the GUI was started.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000634# ifdef FEAT_GUI
635 if (!gui.in_use)
636# endif
637 {
638 setup_term_clip();
639 TIME_MSG("setup clipboard");
640 }
641#endif
642
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000643#ifdef FEAT_CLIENTSERVER
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100644 // Prepare for being a Vim server.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000645 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000646#endif
647
648 /*
649 * If "-" argument given: Read file from stdin.
650 * Do this before starting Raw mode, because it may change things that the
651 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
652 * are the same terminal: "cat | vim -".
653 * Using autocommands here may cause trouble...
654 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000655 if (params.edit_type == EDIT_STDIN && !recoverymode)
656 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000657
658#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100659 // When switching screens and something caused a message from a vimrc
660 // script, need to output an extra newline on exit.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000661 if ((did_emsg || msg_didout) && *T_TI != NUL)
662 newline_on_exit = TRUE;
663#endif
664
665 /*
666 * When done something that is not allowed or error message call
667 * wait_return. This must be done before starttermcap(), because it may
668 * switch to another screen. It must be done after settmode(TMODE_RAW),
669 * because we want to react on a single key stroke.
670 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000671 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000672 */
673 settmode(TMODE_RAW);
674 TIME_MSG("setting raw mode");
675
676 if (need_wait_return || msg_didany)
677 {
678 wait_return(TRUE);
679 TIME_MSG("waiting for return");
680 }
681
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100682 starttermcap(); // start termcap if not done by wait_return()
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000683 TIME_MSG("start termcap");
684
Bram Moolenaarb20b9e12019-09-21 20:48:04 +0200685 setmouse(); // may start using the mouse
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000686 if (scroll_region)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100687 scroll_region_reset(); // In case Rows changed
688 scroll_start(); // may scroll the screen to the right position
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000689
Bram Moolenaar980bab42018-08-07 22:42:53 +0200690#if defined(FEAT_TITLE) && (defined(UNIX) || defined(VMS) || defined(MACOS_X))
Bram Moolenaar40385db2018-08-07 22:31:44 +0200691 term_push_title(SAVE_RESTORE_BOTH);
692#endif
693
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000694 /*
695 * Don't clear the screen when starting in Ex mode, unless using the GUI.
696 */
697 if (exmode_active
698#ifdef FEAT_GUI
699 && !gui.in_use
700#endif
701 )
702 must_redraw = CLEAR;
703 else
704 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100705 screenclear(); // clear screen
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000706 TIME_MSG("clearing screen");
707 }
708
709#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000710 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100712 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200713 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000714 TIME_MSG("getting crypt key");
715 }
716#endif
717
718 no_wait_return = TRUE;
719
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000720 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000721 * Create the requested number of windows and edit buffers in them.
722 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000723 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000724 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 TIME_MSG("opening buffers");
726
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000727#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100728 // clear v:swapcommand
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000729 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
730#endif
731
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100732 // Ex starts at last line of the file
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000733 if (exmode_active)
734 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
735
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
737 TIME_MSG("BufEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738 setpcmark();
739
740#ifdef FEAT_QUICKFIX
741 /*
742 * When started with "-q errorfile" jump to first error now.
743 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000744 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000745 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000746 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000747 TIME_MSG("jump to first error");
748 }
749#endif
750
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000751 /*
752 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000753 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200755 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200756 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000757
758#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000759 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000760 {
761 win_T *wp;
762
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100763 // set options in each window for "vimdiff".
Bram Moolenaar29323592016-07-24 22:04:11 +0200764 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000765 diff_win_options(wp, TRUE);
766 }
767#endif
768
769 /*
770 * Shorten any of the filenames, but only when absolute.
771 */
772 shorten_fnames(FALSE);
773
774 /*
775 * Need to jump to the tag before executing the '-c command'.
776 * Makes "vim -c '/return' -t main" work.
777 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000778 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000779 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000780 swap_exists_did_quit = FALSE;
Bram Moolenaar146522e2005-12-16 21:55:46 +0000781
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000782 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000783 do_cmdline_cmd(IObuff);
784 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000785
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100786 // If the user doesn't want to edit the file then we quit here.
Bram Moolenaar146522e2005-12-16 21:55:46 +0000787 if (swap_exists_did_quit)
788 getout(1);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000789 }
790
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100791 // Execute any "+", "-c" and "-S" arguments.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000792 if (params.n_commands > 0)
793 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100795 // Must come before the may_req_ calls.
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200796 starting = 0;
797
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100798#if defined(FEAT_TERMRESPONSE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100799 // Must be done before redrawing, puts a few characters on the screen.
Bram Moolenaara45551a2020-06-09 15:57:37 +0200800 check_terminal_behavior();
Bram Moolenaar976787d2017-06-04 15:45:50 +0200801#endif
802
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000803 RedrawingDisabled = 0;
804 redraw_all_later(NOT_VALID);
805 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100807 // 'autochdir' has been postponed
Bram Moolenaar6f470022018-04-10 18:47:20 +0200808 DO_AUTOCHDIR;
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200809
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000810#ifdef FEAT_TERMRESPONSE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100811 // Requesting the termresponse is postponed until here, so that a "-c q"
812 // argument doesn't make it appear in the shell Vim was started from.
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000813 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200814
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200815 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000816#endif
817
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100818 // start in insert mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000819 if (p_im)
820 need_start_insertmode = TRUE;
821
Bram Moolenaar14735512016-03-26 21:00:08 +0100822#ifdef FEAT_EVAL
823 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
824#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
826 TIME_MSG("VimEnter autocommands");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000827
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200828#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100829 // Adjust default register name for "unnamed" in 'clipboard'. Can only be
830 // done after the clipboard is available and all initial commands that may
831 // modify the 'clipboard' setting have run; i.e. just before entering the
832 // main loop.
Bram Moolenaar439c0362020-06-06 15:58:03 +0200833 reset_reg_var();
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200834#endif
835
Bram Moolenaar8a3bb562018-03-04 20:14:14 +0100836#if defined(FEAT_DIFF)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100837 // When a startup script or session file setup for diff'ing and
838 // scrollbind, sync the scrollbind now.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000839 if (curwin->w_p_diff && curwin->w_p_scb)
840 {
841 update_topline();
842 check_scrollbind((linenr_T)0, 0L);
843 TIME_MSG("diff scrollbinding");
844 }
845#endif
846
Bram Moolenaarafde13b2019-04-28 19:46:49 +0200847#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
848# ifdef VIMDLL
849 if (!gui.in_use)
850# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100851 mch_set_winsize_now(); // Allow winsize changes from now on
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000852#endif
853
Bram Moolenaar4033c552017-09-16 20:54:51 +0200854#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100855 // When tab pages were created, may need to update the tab pages line and
856 // scrollbars. This is skipped while creating them.
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000857 if (first_tabpage->tp_next != NULL)
858 {
859 out_flush();
860 gui_init_which_components(NULL);
861 gui_update_scrollbars(TRUE);
862 }
863 need_mouse_correct = TRUE;
864#endif
865
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100866 // If ":startinsert" command used, stuff a dummy command to be able to
867 // call normal_cmd(), which will then start Insert mode.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000869 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000870
871#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200872 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200873 {
874# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200875# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar4f974752019-02-17 17:44:42 +0100876 && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200877 if (gui.in_use)
878 {
879 mch_errmsg(_("netbeans is not supported with this GUI\n"));
880 mch_exit(2);
881 }
882# endif
883# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100884 // Tell the client that it can start sending commands.
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200885 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200886 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000887#endif
888
Bram Moolenaare7ddf4e2020-02-03 21:29:30 +0100889 // Redraw at least once, also when 'lazyredraw' is set, to make sure the
890 // window title gets updated.
891 do_redraw = TRUE;
892
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893 TIME_MSG("before starting main loop");
894
895 /*
896 * Call the main command loop. This never returns.
Bram Moolenaar92d147b2018-07-29 17:35:23 +0200897 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000898 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000899
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100900#endif // NO_VIM_MAIN
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200901
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000902 return 0;
903}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000904
905/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200906 * Initialisation shared by main() and some tests.
907 */
908 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200909common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200910{
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100911 estack_init();
Bram Moolenaar438d1762018-09-30 17:11:48 +0200912 cmdline_init();
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200913
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100914 (void)mb_init(); // init mb_bytelen_tab[] to ones
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200915#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100916 eval_init(); // init global variables
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200917#endif
918
919#ifdef __QNXNTO__
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100920 qnx_init(); // PhAttach() for clipboard, (and gui)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200921#endif
922
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100923 // Init the table of Normal mode commands.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200924 init_normal_cmds();
925
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200926 /*
927 * Allocate space for the generic buffers (needed for set_init_1() and
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100928 * emsg()).
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200929 */
930 if ((IObuff = alloc(IOSIZE)) == NULL
931 || (NameBuff = alloc(MAXPATHL)) == NULL)
932 mch_exit(0);
933 TIME_MSG("Allocated generic buffers");
934
935#ifdef NBDEBUG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100936 // Wait a moment for debugging NetBeans. Must be after allocating
937 // NameBuff.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200938 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
939 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
940 TIME_MSG("NetBeans debug wait");
941#endif
942
943#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
944 /*
945 * Setup to use the current locale (for ctype() and many other things).
946 * NOTE: Translated messages with encodings other than latin1 will not
947 * work until set_init_1() has been called!
948 */
949 init_locale();
950 TIME_MSG("locale set");
951#endif
952
953#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100954 gui.dofork = TRUE; // default is to use fork()
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200955#endif
956
957 /*
958 * Do a first scan of the arguments in "argv[]":
959 * -display or --display
960 * --server...
961 * --socketid
962 * --windowid
963 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200964 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200965
Bram Moolenaard0573012017-10-28 21:11:06 +0200966#if defined(FEAT_GUI)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100967 // Prepare for possibly starting GUI sometime
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200968 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200969 TIME_MSG("GUI prepared");
970#endif
971
972#ifdef FEAT_CLIPBOARD
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100973 clip_init(FALSE); // Initialise clipboard stuff
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200974 TIME_MSG("clipboard setup");
975#endif
976
977 /*
978 * Check if we have an interactive window.
979 * On the Amiga: If there is no window, we open one with a newcli command
980 * (needed for :! to * work). mch_check_win() will also handle the -d or
981 * -dev argument.
982 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100983 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200984 TIME_MSG("window checked");
985
986 /*
987 * Allocate the first window and buffer.
988 * Can't do anything without it, exit when it fails.
989 */
990 if (win_alloc_first() == FAIL)
991 mch_exit(0);
992
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100993 init_yank(); // init yank buffers
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200994
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100995 alist_init(&global_alist); // Init the argument list to empty.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200996 global_alist.id = 0;
997
998 /*
999 * Set the default values for the options.
1000 * NOTE: Non-latin1 translated messages are working only after this,
1001 * because this is where "has_mbyte" will be set, which is used by
1002 * msg_outtrans_len_attr().
1003 * First find out the home directory, needed to expand "~" in options.
1004 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001005 init_homedir(); // find real value of $HOME
Bram Moolenaar07268702018-03-01 21:57:32 +01001006 set_init_1(paramp->clean);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001007 TIME_MSG("inits 1");
1008
1009#ifdef FEAT_EVAL
Bram Moolenaar69bf6342019-10-29 04:16:57 +01001010 // set v:lang and v:ctype
1011 set_lang_var();
1012
1013 // set v:argv
1014 set_argv_var(paramp->argv, paramp->argc);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001015#endif
Bram Moolenaar6436cd82018-12-27 00:28:33 +01001016
1017#ifdef FEAT_SIGNS
1018 init_signs();
1019#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001020}
1021
1022/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001023 * Return TRUE when the --not-a-term argument was found.
1024 */
1025 int
1026is_not_a_term()
1027{
1028 return params.not_a_term;
1029}
1030
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001031
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001032// When TRUE in a safe state when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001033static int was_safe = FALSE;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001034static oparg_T *current_oap = NULL;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001035
1036/*
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001037 * Return TRUE if an operator was started but not finished yet.
1038 * Includes typing a count or a register name.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001039 */
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001040 int
1041op_pending(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001042{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001043 return !(current_oap != NULL
1044 && !finish_op
1045 && current_oap->prev_opcount == 0
1046 && current_oap->prev_count0 == 0
1047 && current_oap->op_type == OP_NOP
1048 && current_oap->regname == NUL);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001049}
1050
1051/*
Bram Moolenaard103ee72019-09-18 21:15:31 +02001052 * Return whether currently it is safe, assuming it was safe before (high level
1053 * state didn't change).
1054 */
1055 static int
1056is_safe_now(void)
1057{
1058 return stuff_empty()
1059 && typebuf.tb_len == 0
1060 && scriptin[curscript] == NULL
1061 && !global_busy;
1062}
1063
1064/*
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001065 * Trigger SafeState if currently in s safe state, that is "safe" is TRUE and
1066 * there is no typeahead.
1067 */
1068 void
1069may_trigger_safestate(int safe)
1070{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001071 int is_safe = safe && is_safe_now();
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001072
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001073#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001074 if (was_safe != is_safe)
1075 // Only log when the state changes, otherwise it happens at nearly
1076 // every key stroke.
Bram Moolenaard103ee72019-09-18 21:15:31 +02001077 ch_log(NULL, is_safe ? "SafeState: Start triggering"
1078 : "SafeState: Stop triggering");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001079#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001080 if (is_safe)
1081 apply_autocmds(EVENT_SAFESTATE, NULL, NULL, FALSE, curbuf);
1082 was_safe = is_safe;
1083}
1084
1085/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001086 * Something changed which causes the state possibly to be unsafe, e.g. a
1087 * character was typed. It will remain unsafe until the next call to
1088 * may_trigger_safestate().
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001089 */
1090 void
Bram Moolenaard103ee72019-09-18 21:15:31 +02001091state_no_longer_safe(char *reason UNUSED)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001092{
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001093#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001094 if (was_safe)
Bram Moolenaard103ee72019-09-18 21:15:31 +02001095 ch_log(NULL, "SafeState: reset: %s", reason);
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001096#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001097 was_safe = FALSE;
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001098}
1099
Bram Moolenaard103ee72019-09-18 21:15:31 +02001100 int
1101get_was_safe_state(void)
1102{
1103 return was_safe;
1104}
1105
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001106/*
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001107 * Invoked when leaving code that invokes callbacks. Then trigger
1108 * SafeStateAgain, if it was safe when starting to wait for a character.
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001109 */
1110 void
Bram Moolenaar37d18072019-09-17 20:28:38 +02001111may_trigger_safestateagain(void)
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001112{
Bram Moolenaard103ee72019-09-18 21:15:31 +02001113 if (!was_safe)
1114 {
1115 // If the safe state was reset in state_no_longer_safe(), e.g. because
1116 // of calling feedkeys(), we check if it's now safe again (all keys
1117 // were consumed).
1118 was_safe = is_safe_now();
1119#ifdef FEAT_JOB_CHANNEL
1120 if (was_safe)
1121 ch_log(NULL, "SafeState: undo reset");
1122#endif
1123 }
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001124 if (was_safe)
Bram Moolenaar37d18072019-09-17 20:28:38 +02001125 {
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001126#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar101e9922019-09-25 21:43:11 +02001127 // Only do this message when another message was given, otherwise we
1128 // get lots of them.
1129 if ((did_repeated_msg & REPEATED_MSG_SAFESTATE) == 0)
1130 {
1131 int did = did_repeated_msg;
1132
1133 ch_log(NULL,
1134 "SafeState: back to waiting, triggering SafeStateAgain");
1135 did_repeated_msg = did | REPEATED_MSG_SAFESTATE;
1136 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001137#endif
Bram Moolenaar69198cb2019-09-16 21:58:13 +02001138 apply_autocmds(EVENT_SAFESTATEAGAIN, NULL, NULL, FALSE, curbuf);
Bram Moolenaar37d18072019-09-17 20:28:38 +02001139 }
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001140#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar37d18072019-09-17 20:28:38 +02001141 else
Bram Moolenaard103ee72019-09-18 21:15:31 +02001142 ch_log(NULL,
1143 "SafeState: back to waiting, not triggering SafeStateAgain");
Bram Moolenaar397c6a12019-09-17 20:54:31 +02001144#endif
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001145}
1146
1147
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001148/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001149 * Main loop: Execute Normal mode commands until exiting Vim.
1150 * Also used to handle commands in the command-line window, until the window
1151 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 * Also used to handle ":visual" command after ":global": execute Normal mode
1153 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001154 */
1155 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001156main_loop(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001157 int cmdwin, // TRUE when working in the command-line window
1158 int noexmode) // TRUE when return on entering Ex mode
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001159{
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001160 oparg_T oa; // operator arguments
1161 oparg_T *prev_oap; // operator arguments
1162 volatile int previous_got_int = FALSE; // "got_int" was TRUE
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001163#ifdef FEAT_CONCEAL
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001164 // these are static to avoid a compiler warning
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001165 static linenr_T conceal_old_cursor_line = 0;
1166 static linenr_T conceal_new_cursor_line = 0;
1167 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001168#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001169
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001170 prev_oap = current_oap;
1171 current_oap = &oa;
1172
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001173#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001174 // Setup to catch a terminating error from the X server. Just ignore
1175 // it, restore the state and continue. This might not always work
1176 // properly, but at least we don't exit unexpectedly when the X server
1177 // exits while Vim is running in a console.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001178 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001179 {
1180 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001181 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001182 got_int = TRUE;
1183 need_wait_return = FALSE;
1184 global_busy = FALSE;
1185 exmode_active = 0;
1186 skip_redraw = FALSE;
1187 RedrawingDisabled = 0;
1188 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001189 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001190# ifdef FEAT_EVAL
1191 emsg_skip = 0;
1192# endif
1193 emsg_off = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001194 setmouse();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001195 settmode(TMODE_RAW);
1196 starttermcap();
1197 scroll_start();
1198 redraw_later_clear();
1199 }
1200#endif
1201
1202 clear_oparg(&oa);
1203 while (!cmdwin
1204#ifdef FEAT_CMDWIN
1205 || cmdwin_result == 0
1206#endif
1207 )
1208 {
1209 if (stuff_empty())
1210 {
1211 did_check_timestamps = FALSE;
1212 if (need_check_timestamps)
1213 check_timestamps(FALSE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001214 if (need_wait_return) // if wait_return still needed ...
1215 wait_return(FALSE); // ... call it now
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001216 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001217 {
1218 need_start_insertmode = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001219 stuffReadbuff((char_u *)"i"); // start insert mode next
1220 // skip the fileinfo message now, because it would be shown
1221 // after insert mode finishes!
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001222 need_fileinfo = FALSE;
1223 }
1224 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001225
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001226 // Reset "got_int" now that we got back to the main loop. Except when
1227 // inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1228 // the ":g" command.
1229 // For ":g/pat/vi" we reset "got_int" when used once. When used
1230 // a second time we go back to Ex mode and abort the ":g" command.
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001231 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001232 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001233 if (noexmode && global_busy && !exmode_active && previous_got_int)
1234 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001235 // Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1236 // used and keep "got_int" set, so that it aborts ":g".
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001237 exmode_active = EXMODE_NORMAL;
1238 State = NORMAL;
1239 }
1240 else if (!global_busy || !exmode_active)
1241 {
1242 if (!quit_more)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001243 (void)vgetc(); // flush all buffers
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001244 got_int = FALSE;
1245 }
1246 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001247 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001248 else
1249 previous_got_int = FALSE;
1250
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001251 if (!exmode_active)
1252 msg_scroll = FALSE;
1253 quit_more = FALSE;
1254
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001255 // it's not safe unless may_trigger_safestate_main() is called
1256 was_safe = FALSE;
1257
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001258 /*
1259 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1260 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1261 * update cursor and redraw.
1262 */
1263 if (skip_redraw || exmode_active)
1264 skip_redraw = FALSE;
1265 else if (do_redraw || stuff_empty())
1266 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001267#ifdef FEAT_GUI
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001268 // If ui_breakcheck() was used a resize may have been postponed.
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001269 gui_may_resize_shell();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001270#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001271#ifdef HAVE_DROP_FILE
1272 // If files were dropped while text was locked or the curbuf was
1273 // locked, this would be a good time to handle the drop.
1274 handle_any_postponed_drop();
1275#endif
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001276#ifdef FEAT_CONCEAL
1277 if (curwin->w_p_cole == 0)
1278 conceal_update_lines = FALSE;
1279#endif
Bram Moolenaar92d147b2018-07-29 17:35:23 +02001280
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001281 // Trigger CursorMoved if the cursor moved.
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001282 if (!finish_op && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001283 has_cursormoved()
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001284#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001285 || popup_visible
1286#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001287#ifdef FEAT_CONCEAL
1288 || curwin->w_p_cole > 0
1289#endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001290 )
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001291 && !EQUAL_POS(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001292 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001293 if (has_cursormoved())
1294 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1295 FALSE, curbuf);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01001296#ifdef FEAT_PROP_POPUP
Bram Moolenaar3397f742019-06-02 18:40:06 +02001297 if (popup_visible)
1298 popup_check_cursor_pos();
1299#endif
1300#ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001301 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001302 {
1303 conceal_old_cursor_line = last_cursormoved.lnum;
1304 conceal_new_cursor_line = curwin->w_cursor.lnum;
1305 conceal_update_lines = TRUE;
1306 }
Bram Moolenaar3397f742019-06-02 18:40:06 +02001307#endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001308 last_cursormoved = curwin->w_cursor;
1309 }
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001310
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001311#if defined(FEAT_CONCEAL)
1312 if (conceal_update_lines
1313 && (conceal_old_cursor_line != conceal_new_cursor_line
1314 || conceal_cursor_line(curwin)
1315 || need_cursor_line_redraw))
1316 {
1317 if (conceal_old_cursor_line != conceal_new_cursor_line
Bram Moolenaarbbee8d52019-01-14 21:51:40 +01001318 && conceal_old_cursor_line != 0
Bram Moolenaar535d5b62019-01-11 20:45:36 +01001319 && conceal_old_cursor_line
1320 <= curbuf->b_ml.ml_line_count)
1321 redrawWinline(curwin, conceal_old_cursor_line);
1322 redrawWinline(curwin, conceal_new_cursor_line);
1323 curwin->w_valid &= ~VALID_CROW;
1324 need_cursor_line_redraw = FALSE;
1325 }
1326#endif
1327
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001328 // Trigger TextChanged if b:changedtick differs.
Bram Moolenaar186628f2013-03-19 13:33:23 +01001329 if (!finish_op && has_textchanged()
Bram Moolenaar5a093432018-02-10 18:15:19 +01001330 && curbuf->b_last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001331 {
Bram Moolenaar5a093432018-02-10 18:15:19 +01001332 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL, FALSE, curbuf);
1333 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001334 }
Bram Moolenaar186628f2013-03-19 13:33:23 +01001335
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001336 // If nothing is pending and we are going to wait for the user to
1337 // type a character, trigger SafeState.
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001338 may_trigger_safestate(!op_pending() && restart_edit == 0);
Bram Moolenaar8aeec402019-09-15 23:02:04 +02001339
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01001340#if defined(FEAT_DIFF)
Bram Moolenaare3521d92018-09-16 14:10:31 +02001341 // Updating diffs from changed() does not always work properly,
1342 // esp. updating folds. Do an update just before redrawing if
1343 // needed.
1344 if (curtab->tp_diff_update || curtab->tp_diff_invalid)
1345 {
1346 ex_diffupdate(NULL);
1347 curtab->tp_diff_update = FALSE;
1348 }
1349
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001350 // Scroll-binding for diff mode may have been postponed until
1351 // here. Avoids doing it for every change.
Bram Moolenaar33aec762006-01-22 23:30:12 +00001352 if (diff_need_scrollbind)
1353 {
1354 check_scrollbind((linenr_T)0, 0L);
1355 diff_need_scrollbind = FALSE;
1356 }
1357#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001358#if defined(FEAT_FOLDING)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001359 // Include a closed fold completely in the Visual area.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001360 foldAdjustVisual();
1361#endif
1362#ifdef FEAT_FOLDING
1363 /*
1364 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1365 * contain the cursor.
1366 * When 'foldopen' is "all", open the fold(s) under the cursor.
1367 * This may mark the window for redrawing.
1368 */
1369 if (hasAnyFolding(curwin) && !char_avail())
1370 {
1371 foldCheckClose();
1372 if (fdo_flags & FDO_ALL)
1373 foldOpenCursor();
1374 }
1375#endif
1376
1377 /*
1378 * Before redrawing, make sure w_topline is correct, and w_leftcol
1379 * if lines don't wrap, and w_skipcol if lines wrap.
1380 */
1381 update_topline();
1382 validate_cursor();
1383
Bram Moolenaar017ba072019-09-14 21:01:23 +02001384#ifdef FEAT_SYN_HL
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001385 // Might need to update for 'cursorline'.
1386 // When 'cursorlineopt' is "screenline" need to redraw always.
1387 if (curwin->w_p_cul
1388 && (curwin->w_last_cursorline != curwin->w_cursor.lnum
1389 || (curwin->w_p_culopt_flags & CULOPT_SCRLINE))
1390 && !char_avail())
1391 redraw_later(VALID);
Bram Moolenaar017ba072019-09-14 21:01:23 +02001392#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001393 if (VIsual_active)
Bram Moolenaar017ba072019-09-14 21:01:23 +02001394 update_curbuf(INVERTED); // update inverted part
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001395 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001396 {
Bram Moolenaar017ba072019-09-14 21:01:23 +02001397 mch_disable_flush(); // Stop issuing gui_mch_flush().
Bram Moolenaar11a58af2019-10-24 22:32:31 +02001398 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001399 mch_enable_flush();
1400 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401 else if (redraw_cmdline || clear_cmdline)
1402 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001403 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001404#ifdef FEAT_TITLE
1405 if (need_maketitle)
1406 maketitle();
1407#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001408#ifdef FEAT_VIMINFO
1409 curbuf->b_last_used = vim_time();
1410#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001411 // display message after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001412 if (keep_msg != NULL)
1413 {
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001414 char_u *p = vim_strsave(keep_msg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001415
Bram Moolenaare5fbd732019-09-09 20:04:13 +02001416 if (p != NULL)
1417 {
1418 // msg_start() will set keep_msg to NULL, make a copy
1419 // first. Don't reset keep_msg, msg_attr_keep() uses it to
1420 // check for duplicates. Never put this message in
1421 // history.
1422 msg_hist_off = TRUE;
1423 msg_attr((char *)p, keep_msg_attr);
1424 msg_hist_off = FALSE;
1425 vim_free(p);
1426 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001427 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001428 if (need_fileinfo) // show file info after redraw
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001429 {
1430 fileinfo(FALSE, TRUE, FALSE);
1431 need_fileinfo = FALSE;
1432 }
1433
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001434 emsg_on_display = FALSE; // can delete error message now
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001435 did_emsg = FALSE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001436 msg_didany = FALSE; // reset lines_left in msg_start()
1437 may_clear_sb_text(); // clear scroll-back text on next msg
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001438 showruler(FALSE);
1439
1440 setcursor();
1441 cursor_on();
1442
1443 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001444
1445#ifdef STARTUPTIME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001446 // Now that we have drawn the first screen all the startup stuff
1447 // has been done, close any file for startup messages.
Bram Moolenaar3f269672009-11-03 11:11:11 +00001448 if (time_fd != NULL)
1449 {
1450 TIME_MSG("first screen update");
1451 TIME_MSG("--- VIM STARTED ---");
1452 fclose(time_fd);
1453 time_fd = NULL;
1454 }
1455#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001456 }
1457#ifdef FEAT_GUI
1458 if (need_mouse_correct)
1459 gui_mouse_correct();
1460#endif
1461
1462 /*
1463 * Update w_curswant if w_set_curswant has been set.
1464 * Postponed until here to avoid computing w_virtcol too often.
1465 */
1466 update_curswant();
1467
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001468#ifdef FEAT_EVAL
1469 /*
1470 * May perform garbage collection when waiting for a character, but
1471 * only at the very toplevel. Otherwise we may be using a List or
1472 * Dict internally somewhere.
1473 * "may_garbage_collect" is reset in vgetc() which is invoked through
1474 * do_exmode() and normal_cmd().
1475 */
1476 may_garbage_collect = (!cmdwin && !noexmode);
1477#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001478 /*
1479 * If we're invoked as ex, do a round of ex commands.
1480 * Otherwise, get and execute a normal mode command.
1481 */
1482 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001483 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001484 if (noexmode) // End of ":global/path/visual" commands
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001485 goto theend;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001486 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001487 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001488 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001489 {
1490#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001491 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001492 && oa.op_type == OP_NOP && oa.regname == NUL
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001493 && !VIsual_active
1494 && !skip_term_loop)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001495 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001496 // If terminal_loop() returns OK we got a key that is handled
1497 // in Normal model. With FAIL we first need to position the
1498 // cursor and the screen needs to be redrawn.
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001499 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001500 normal_cmd(&oa, TRUE);
1501 }
1502 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001503#endif
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001504 {
1505#ifdef FEAT_TERMINAL
1506 skip_term_loop = FALSE;
1507#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001508 normal_cmd(&oa, TRUE);
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02001509 }
Bram Moolenaar938783d2017-07-16 20:13:26 +02001510 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001511 }
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001512
1513theend:
1514 current_oap = prev_oap;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001515}
1516
1517
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001518#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001519/*
1520 * Exit, but leave behind swap files for modified buffers.
1521 */
1522 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001523getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001524{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001525# if defined(SIGHUP) && defined(SIG_IGN)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001526 // Ignore SIGHUP, because a dropped connection causes a read error, which
1527 // makes Vim exit and then handling SIGHUP causes various reentrance
1528 // problems.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001529 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001530# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001531
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001532 ml_close_notmod(); // close all not-modified buffers
1533 ml_sync_all(FALSE, FALSE); // preserve all swap files
1534 ml_close_all(FALSE); // close all memfiles, without deleting
1535 getout(exitval); // exit Vim properly
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001536}
1537#endif
1538
1539
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001540/*
1541 * Exit properly.
1542 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001543 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001544getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001545{
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001546 exiting = TRUE;
Bram Moolenaar0bd052b2018-03-22 20:33:56 +01001547#if defined(FEAT_JOB_CHANNEL)
1548 ch_log(NULL, "Exiting...");
1549#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001550
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001551 // When running in Ex mode an error causes us to exit with a non-zero exit
1552 // code. POSIX requires this, although it's not 100% clear from the
1553 // standard.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001554 if (exmode_active)
1555 exitval += ex_exitval;
1556
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001557 // Position the cursor on the last screen line, below all the text
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001558#ifdef FEAT_GUI
1559 if (!gui.in_use)
1560#endif
1561 windgoto((int)Rows - 1, 0);
1562
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001563#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001564 // Optionally print hashtable efficiency.
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001565 hash_debug_results();
1566#endif
1567
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001568#ifdef FEAT_GUI
1569 msg_didany = FALSE;
1570#endif
1571
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001572 if (v_dying <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001573 {
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001574 tabpage_T *tp;
1575 tabpage_T *next_tp;
1576 buf_T *buf;
1577 win_T *wp;
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001578 int unblock = 0;
Bram Moolenaardf2c7742018-04-16 17:06:09 +02001579
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001580 // Trigger BufWinLeave for all windows, but only once per buffer.
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001581 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001582 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001583 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001584 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001585 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001586 if (wp->w_buffer == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001587 // Autocmd must have close the buffer already, skip.
Bram Moolenaar802418d2013-01-17 14:00:11 +01001588 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001589 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001590 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001591 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001592 bufref_T bufref;
1593
1594 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001595 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1596 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001597 if (bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001598 CHANGEDTICK(buf) = -1; // note we did it already
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001599
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001600 // start all over, autocommands may mess up the lists
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001601 next_tp = first_tabpage;
1602 break;
1603 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001604 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001605 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001606
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001607 // Trigger BufUnload for buffers that are loaded
Bram Moolenaar29323592016-07-24 22:04:11 +02001608 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001609 if (buf->b_ml.ml_mfp != NULL)
1610 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001611 bufref_T bufref;
1612
1613 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001614 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001615 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001616 if (!bufref_valid(&bufref))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001617 // autocmd deleted the buffer
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001618 break;
1619 }
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001620
1621 // deathtrap() blocks autocommands, but we do want to trigger
1622 // VimLeavePre.
1623 if (is_autocmd_blocked())
1624 {
1625 unblock_autocmds();
1626 ++unblock;
1627 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001628 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar129d6bf2020-05-16 16:08:35 +02001629 if (unblock)
1630 block_autocmds();
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001631 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001632
1633#ifdef FEAT_VIMINFO
1634 if (*p_viminfo != NUL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001635 // Write out the registers, history, marks etc, to the viminfo file
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001636 write_viminfo(NULL, FALSE);
1637#endif
1638
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001639 if (v_dying <= 1)
Bram Moolenaarc7226682019-08-17 16:33:23 +02001640 {
1641 int unblock = 0;
1642
1643 // deathtrap() blocks autocommands, but we do want to trigger VimLeave.
1644 if (is_autocmd_blocked())
1645 {
1646 unblock_autocmds();
1647 ++unblock;
1648 }
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001649 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarc7226682019-08-17 16:33:23 +02001650 if (unblock)
1651 block_autocmds();
1652 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001653
Bram Moolenaar05159a02005-02-26 23:04:13 +00001654#ifdef FEAT_PROFILE
1655 profile_dump();
1656#endif
1657
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001658 if (did_emsg
1659#ifdef FEAT_GUI
1660 || (gui.in_use && msg_didany && p_verbose > 0)
1661#endif
1662 )
1663 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001664 // give the user a chance to read the (error) message
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001665 no_wait_return = FALSE;
1666 wait_return(FALSE);
1667 }
1668
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001669 // Position the cursor again, the autocommands may have moved it
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001670#ifdef FEAT_GUI
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001671 if (!gui.in_use)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001672#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001673 windgoto((int)Rows - 1, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001674
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001675#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001676 job_stop_on_exit();
1677#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001678#ifdef FEAT_LUA
1679 lua_end();
1680#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001681#ifdef FEAT_MZSCHEME
1682 mzscheme_end();
1683#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001684#ifdef FEAT_TCL
1685 tcl_end();
1686#endif
1687#ifdef FEAT_RUBY
1688 ruby_end();
1689#endif
1690#ifdef FEAT_PYTHON
1691 python_end();
1692#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001693#ifdef FEAT_PYTHON3
1694 python3_end();
1695#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001696#ifdef FEAT_PERL
1697 perl_end();
1698#endif
1699#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1700 iconv_end();
1701#endif
1702#ifdef FEAT_NETBEANS_INTG
1703 netbeans_end();
1704#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001705#ifdef FEAT_CSCOPE
1706 cs_end();
1707#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001708#ifdef FEAT_EVAL
1709 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001710 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001711#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01001712#ifdef MSWIN
Bram Moolenaar14993322014-09-09 12:25:33 +02001713 free_cmd_argsW();
1714#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001715
1716 mch_exit(exitval);
1717}
1718
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001719#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1720/*
1721 * Setup to use the current locale (for ctype() and many other things).
1722 */
1723 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001724init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001725{
1726 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001727
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001728# ifdef FEAT_GUI_GTK
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001729 // Tell Gtk not to change our locale settings.
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001730 gtk_disable_setlocale();
1731# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001732# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001733 // Make sure strtod() uses a decimal point, not a comma.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001734 setlocale(LC_NUMERIC, "C");
1735# endif
1736
Bram Moolenaar4f974752019-02-17 17:44:42 +01001737# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001738 // Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1739 // text while it's expecting text in the current locale. This call avoids
1740 // that.
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001741 setlocale(LC_CTYPE, "C");
1742# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001743
1744# ifdef FEAT_GETTEXT
1745 {
1746 int mustfree = FALSE;
1747 char_u *p;
1748
1749# ifdef DYNAMIC_GETTEXT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001750 // Initialize the gettext library
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001751 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001752# endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001753 // expand_env() doesn't work yet, because g_chartab[] is not
1754 // initialized yet, call vim_getenv() directly
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001755 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1756 if (p != NULL && *p != NUL)
1757 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001758 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001759 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1760 }
1761 if (mustfree)
1762 vim_free(p);
1763 textdomain(VIMPACKAGE);
1764 }
1765# endif
1766}
1767#endif
1768
1769/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001770 * Get the name of the display, before gui_prepare() removes it from
1771 * argv[]. Used for the xterm-clipboard display.
1772 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001773 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001774 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001775 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001776early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001777{
Bram Moolenaar03005972008-11-20 13:12:36 +00001778#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1779 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001780 int argc = parmp->argc;
1781 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001782 int i;
1783
1784 for (i = 1; i < argc; i++)
1785 {
1786 if (STRCMP(argv[i], "--") == 0)
1787 break;
1788# ifdef FEAT_XCLIPBOARD
1789 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001790# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001791 || STRICMP(argv[i], "--display") == 0
1792# endif
1793 )
1794 {
1795 if (i == argc - 1)
1796 mainerr_arg_missing((char_u *)argv[i]);
1797 xterm_display = argv[++i];
1798 }
1799# endif
1800# ifdef FEAT_CLIENTSERVER
1801 else if (STRICMP(argv[i], "--servername") == 0)
1802 {
1803 if (i == argc - 1)
1804 mainerr_arg_missing((char_u *)argv[i]);
1805 parmp->serverName_arg = (char_u *)argv[++i];
1806 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001807 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001808 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001809 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001810 {
1811 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001812# ifdef FEAT_GUI
1813 if (strstr(argv[i], "-wait") != 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001814 // don't fork() when starting the GUI to edit files ourself
Bram Moolenaareb94e552006-03-11 21:35:11 +00001815 gui.dofork = FALSE;
1816# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001817 }
1818# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001819
Bram Moolenaar4f974752019-02-17 17:44:42 +01001820# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
1821# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001822 else if (STRICMP(argv[i], "--windowid") == 0)
1823# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001824 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001825# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001826 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001827 long_u id;
1828 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001829
1830 if (i == argc - 1)
1831 mainerr_arg_missing((char_u *)argv[i]);
1832 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001833 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001834 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001835 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001836 if (count != 1)
1837 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1838 else
Bram Moolenaar4f974752019-02-17 17:44:42 +01001839# ifdef FEAT_GUI_MSWIN
Bram Moolenaar78e17622007-08-30 10:26:19 +00001840 win_socket_id = id;
1841# else
1842 gtk_socket_id = id;
1843# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001844 i++;
1845 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001846# endif
1847# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001848 else if (STRICMP(argv[i], "--echo-wid") == 0)
1849 echo_wid_arg = TRUE;
1850# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001851# ifndef FEAT_NETBEANS_INTG
1852 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001853 {
1854 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1855 mch_exit(2);
1856 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001857# endif
1858
Bram Moolenaar58d98232005-07-23 22:25:46 +00001859 }
1860#endif
1861}
1862
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001863#ifndef NO_VIM_MAIN
1864/*
1865 * Get a (optional) count for a Vim argument.
1866 */
1867 static int
1868get_number_arg(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001869 char_u *p, // pointer to argument
1870 int *idx, // index in argument, is incremented
1871 int def) // default value
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001872{
1873 if (vim_isdigit(p[*idx]))
1874 {
1875 def = atoi((char *)&(p[*idx]));
1876 while (vim_isdigit(p[*idx]))
1877 *idx = *idx + 1;
1878 }
1879 return def;
1880}
1881
1882/*
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001883 * Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001884 * If the executable name starts with "r" we disable shell commands.
1885 * If the next character is "e" we run in Easy mode.
1886 * If the next character is "g" we run the GUI version.
1887 * If the next characters are "view" we start in readonly mode.
1888 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1889 * If the next characters are "ex" we start in Ex mode. If it's followed
1890 * by "im" use improved Ex mode.
1891 */
1892 static void
1893parse_command_name(mparm_T *parmp)
1894{
1895 char_u *initstr;
1896
1897 initstr = gettail((char_u *)parmp->argv[0]);
1898
Bram Moolenaard0573012017-10-28 21:11:06 +02001899#ifdef FEAT_GUI_MAC
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001900 // An issue has been seen when launching Vim in such a way that
1901 // $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1902 // executable or a symbolic link of it. Until this issue is resolved
1903 // we prohibit the GUI from being used.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001904 if (STRCMP(initstr, parmp->argv[0]) == 0)
1905 disallow_gui = TRUE;
1906
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001907 // TODO: On MacOS X default to gui if argv[0] ends in:
1908 // /Vim.app/Contents/MacOS/Vim
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001909#endif
1910
1911#ifdef FEAT_EVAL
1912 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001913 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001914#endif
1915
1916 if (TOLOWER_ASC(initstr[0]) == 'r')
1917 {
1918 restricted = TRUE;
1919 ++initstr;
1920 }
1921
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001922 // Use evim mode for "evim" and "egvim", not for "editor".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001923 if (TOLOWER_ASC(initstr[0]) == 'e'
1924 && (TOLOWER_ASC(initstr[1]) == 'v'
1925 || TOLOWER_ASC(initstr[1]) == 'g'))
1926 {
1927#ifdef FEAT_GUI
1928 gui.starting = TRUE;
1929#endif
1930 parmp->evim_mode = TRUE;
1931 ++initstr;
1932 }
1933
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001934 // "gvim" starts the GUI. Also accept "Gvim" for MS-Windows.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001935 if (TOLOWER_ASC(initstr[0]) == 'g')
1936 {
1937 main_start_gui();
1938#ifdef FEAT_GUI
1939 ++initstr;
1940#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001941#ifdef GUI_MAY_SPAWN
1942 gui.dospawn = FALSE; // No need to spawn a new process.
1943#endif
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001944 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02001945#ifdef GUI_MAY_SPAWN
1946 else
1947 gui.dospawn = TRUE; // Not "gvim". Need to spawn gvim.exe.
1948#endif
1949
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001950
1951 if (STRNICMP(initstr, "view", 4) == 0)
1952 {
1953 readonlymode = TRUE;
1954 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001955 p_uc = 10000; // don't update very often
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001956 initstr += 4;
1957 }
1958 else if (STRNICMP(initstr, "vim", 3) == 0)
1959 initstr += 3;
1960
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001961 // Catch "[r][g]vimdiff" and "[r][g]viewdiff".
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001962 if (STRICMP(initstr, "diff") == 0)
1963 {
1964#ifdef FEAT_DIFF
1965 parmp->diff_mode = TRUE;
1966#else
1967 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1968 mch_errmsg("\n");
1969 mch_exit(2);
1970#endif
1971 }
1972
Bram Moolenaarcde0ff32020-04-04 14:00:39 +02001973 // Checking for "ex" here may catch some weird names, such as "vimex" or
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001974 // "viewex", we assume the user knows that.
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001975 if (STRNICMP(initstr, "ex", 2) == 0)
1976 {
1977 if (STRNICMP(initstr + 2, "im", 2) == 0)
1978 exmode_active = EXMODE_VIM;
1979 else
1980 exmode_active = EXMODE_NORMAL;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001981 change_compatible(TRUE); // set 'compatible'
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001982 }
1983}
1984
Bram Moolenaar58d98232005-07-23 22:25:46 +00001985/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001986 * Scan the command line arguments.
1987 */
1988 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001989command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001990{
1991 int argc = parmp->argc;
1992 char **argv = parmp->argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001993 int argv_idx; // index in argv[n][]
1994 int had_minmin = FALSE; // found "--" argument
1995 int want_argument; // option argument with argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001996 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001997 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001998 long n;
1999
2000 --argc;
2001 ++argv;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002002 argv_idx = 1; // active option letter is argv[0][argv_idx]
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002003 while (argc > 0)
2004 {
2005 /*
2006 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
2007 */
2008 if (argv[0][0] == '+' && !had_minmin)
2009 {
2010 if (parmp->n_commands >= MAX_ARG_CMDS)
2011 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002012 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002013 if (argv[0][1] == NUL)
2014 parmp->commands[parmp->n_commands++] = (char_u *)"$";
2015 else
2016 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
2017 }
2018
2019 /*
2020 * Optional argument.
2021 */
2022 else if (argv[0][0] == '-' && !had_minmin)
2023 {
2024 want_argument = FALSE;
2025 c = argv[0][argv_idx++];
2026#ifdef VMS
2027 /*
2028 * VMS only uses upper case command lines. Interpret "-X" as "-x"
2029 * and "-/X" as "-X".
2030 */
2031 if (c == '/')
2032 {
2033 c = argv[0][argv_idx++];
2034 c = TOUPPER_ASC(c);
2035 }
2036 else
2037 c = TOLOWER_ASC(c);
2038#endif
2039 switch (c)
2040 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002041 case NUL: // "vim -" read from stdin
2042 // "ex -" silent mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002043 if (exmode_active)
2044 silent_mode = TRUE;
2045 else
2046 {
2047 if (parmp->edit_type != EDIT_NONE)
2048 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2049 parmp->edit_type = EDIT_STDIN;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002050 read_cmd_fd = 2; // read from stderr instead of stdin
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002051 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002052 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002053 break;
2054
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002055 case '-': // "--" don't take any more option arguments
2056 // "--help" give help message
2057 // "--version" give version message
2058 // "--clean" clean context
2059 // "--literal" take files literally
2060 // "--nofork" don't fork
2061 // "--not-a-term" don't warn for not a term
2062 // "--ttyfail" exit if not a term
2063 // "--noplugin[s]" skip plugins
2064 // "--cmd <cmd>" execute cmd before vimrc
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002065 if (STRICMP(argv[0] + argv_idx, "help") == 0)
2066 usage();
2067 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
2068 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002069 Columns = 80; // need to init Columns
2070 info_message = TRUE; // use mch_msg(), not mch_errmsg()
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071 list_version();
2072 msg_putchar('\n');
2073 msg_didout = FALSE;
2074 mch_exit(0);
2075 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002076 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
2077 {
2078 parmp->use_vimrc = (char_u *)"DEFAULTS";
Bram Moolenaar62dd4522018-03-14 21:20:02 +01002079#ifdef FEAT_GUI
2080 use_gvimrc = (char_u *)"NONE";
2081#endif
Bram Moolenaar07268702018-03-01 21:57:32 +01002082 parmp->clean = TRUE;
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002083 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
2084 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002085 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
2086 {
Bram Moolenaar53076832015-12-31 19:53:21 +01002087#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002088 parmp->literal = TRUE;
2089#endif
2090 }
2091 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
2092 {
2093#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002094 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002095#endif
2096 }
2097 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
2098 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002099 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
2100 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002101 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
2102 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002103 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
2104 {
2105 want_argument = TRUE;
2106 argv_idx += 3;
2107 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002108 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
2109 {
2110 want_argument = TRUE;
2111 argv_idx += 11;
2112 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002113#ifdef FEAT_CLIENTSERVER
2114 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002115 ; // already processed -- no arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002116 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
2117 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
2118 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002119 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002120 if (argc > 1)
2121 {
2122 --argc;
2123 ++argv;
2124 }
2125 }
2126#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002127#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_MSWIN)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002128# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002129 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00002130# else
2131 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
2132# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002133 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002134 // already processed -- snatch the following arg
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002135 if (argc > 1)
2136 {
2137 --argc;
2138 ++argv;
2139 }
2140 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00002141#endif
2142#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002143 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
2144 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002145 // already processed, skip
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002146 }
2147#endif
2148 else
2149 {
2150 if (argv[0][argv_idx])
2151 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2152 had_minmin = TRUE;
2153 }
2154 if (!want_argument)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002155 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002156 break;
2157
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002158 case 'A': // "-A" start in Arabic mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002159#ifdef FEAT_ARABIC
2160 set_option_value((char_u *)"arabic", 1L, NULL, 0);
2161#else
2162 mch_errmsg(_(e_noarabic));
2163 mch_exit(2);
2164#endif
2165 break;
2166
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002167 case 'b': // "-b" binary mode
2168 // Needs to be effective before expanding file names, because
2169 // for Win32 this makes us edit a shortcut file itself,
2170 // instead of the file it links to.
Bram Moolenaar231334e2005-07-25 20:46:57 +00002171 set_options_bin(curbuf->b_p_bin, 1, 0);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002172 curbuf->b_p_bin = 1; // binary file I/O
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002173 break;
2174
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002175 case 'C': // "-C" Compatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002176 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002177 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002178 break;
2179
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002180 case 'e': // "-e" Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002181 exmode_active = EXMODE_NORMAL;
2182 break;
2183
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002184 case 'E': // "-E" Improved Ex mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002185 exmode_active = EXMODE_VIM;
2186 break;
2187
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002188 case 'f': // "-f" GUI: run in foreground. Amiga: open
2189 // window directly, not with newcli
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002190#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002191 gui.dofork = FALSE; // don't fork() when starting GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002192#endif
2193 break;
2194
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002195 case 'g': // "-g" start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002196 main_start_gui();
2197 break;
2198
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002199 case 'F': // "-F" was for Farsi mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002200 mch_errmsg(_(e_nofarsi));
2201 mch_exit(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002202 break;
2203
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002204 case '?': // "-?" give help message (for MS-Windows)
2205 case 'h': // "-h" give help message
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002206#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002207 // Tell usage() to exit for "gvim".
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002208 gui.starting = FALSE;
2209#endif
2210 usage();
2211 break;
2212
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002213 case 'H': // "-H" start in Hebrew mode: rl + hkmap set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002214#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002215 p_hkmap = TRUE;
2216 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002217#else
2218 mch_errmsg(_(e_nohebrew));
2219 mch_exit(2);
2220#endif
2221 break;
2222
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002223 case 'l': // "-l" lisp mode, 'lisp' and 'showmatch' on
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002224#ifdef FEAT_LISP
2225 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2226 p_sm = TRUE;
2227#endif
2228 break;
2229
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002230 case 'M': // "-M" no changes or writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002231 reset_modifiable();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002232 // FALLTHROUGH
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002233
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002234 case 'm': // "-m" no writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002235 p_write = FALSE;
2236 break;
2237
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002238 case 'y': // "-y" easy mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002239#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002240 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002241#endif
2242 parmp->evim_mode = TRUE;
2243 break;
2244
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002245 case 'N': // "-N" Nocompatible
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002246 change_compatible(FALSE);
2247 break;
2248
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002249 case 'n': // "-n" no swap file
Bram Moolenaar67c53842010-05-22 18:28:27 +02002250#ifdef FEAT_NETBEANS_INTG
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002251 // checking for "-nb", netbeans parameters
Bram Moolenaar67c53842010-05-22 18:28:27 +02002252 if (argv[0][argv_idx] == 'b')
2253 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002254 netbeansArg = argv[0];
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002255 argv_idx = -1; // skip to next argument
Bram Moolenaar67c53842010-05-22 18:28:27 +02002256 }
2257 else
2258#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002259 parmp->no_swap_file = TRUE;
2260 break;
2261
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002262 case 'p': // "-p[N]" open N tab pages
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002263#ifdef TARGET_API_MAC_OSX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002264 // For some reason on MacOS X, an argument like:
2265 // -psn_0_10223617 is passed in when invoke from Finder
2266 // or with the 'open' command
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002267 if (argv[0][argv_idx] == 's')
2268 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002269 argv_idx = -1; // bypass full -psn
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002270 main_start_gui();
2271 break;
2272 }
2273#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002274 // default is 0: open window for each file
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002275 parmp->window_count = get_number_arg((char_u *)argv[0],
2276 &argv_idx, 0);
2277 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002278 break;
2279
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002280 case 'o': // "-o[N]" open N horizontal split windows
2281 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002282 parmp->window_count = get_number_arg((char_u *)argv[0],
2283 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002284 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002285 break;
2286
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002287 case 'O': // "-O[N]" open N vertical split windows
2288 // default is 0: open window for each file
Bram Moolenaar231334e2005-07-25 20:46:57 +00002289 parmp->window_count = get_number_arg((char_u *)argv[0],
2290 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002291 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002292 break;
2293
2294#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002295 case 'q': // "-q" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002296 if (parmp->edit_type != EDIT_NONE)
2297 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2298 parmp->edit_type = EDIT_QF;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002299 if (argv[0][argv_idx]) // "-q{errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002300 {
2301 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2302 argv_idx = -1;
2303 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002304 else if (argc > 1) // "-q {errorfile}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002305 want_argument = TRUE;
2306 break;
2307#endif
2308
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002309 case 'R': // "-R" readonly mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002310 readonlymode = TRUE;
2311 curbuf->b_p_ro = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002312 p_uc = 10000; // don't update very often
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002313 break;
2314
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002315 case 'r': // "-r" recovery mode
2316 case 'L': // "-L" recovery mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002317 recoverymode = 1;
2318 break;
2319
2320 case 's':
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002321 if (exmode_active) // "-s" silent (batch) mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002322 silent_mode = TRUE;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002323 else // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002324 want_argument = TRUE;
2325 break;
2326
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002327 case 't': // "-t {tag}" or "-t{tag}" jump to tag
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002328 if (parmp->edit_type != EDIT_NONE)
2329 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2330 parmp->edit_type = EDIT_TAG;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002331 if (argv[0][argv_idx]) // "-t{tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002332 {
2333 parmp->tagname = (char_u *)argv[0] + argv_idx;
2334 argv_idx = -1;
2335 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002336 else // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002337 want_argument = TRUE;
2338 break;
2339
2340#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002341 case 'D': // "-D" Debugging
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002342 parmp->use_debug_break_level = 9999;
2343 break;
2344#endif
2345#ifdef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002346 case 'd': // "-d" 'diff'
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002347# ifdef AMIGA
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002348 // check for "-dev {device}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002349 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2350 want_argument = TRUE;
2351 else
2352# endif
2353 parmp->diff_mode = TRUE;
2354 break;
2355#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002356 case 'V': // "-V{N}" Verbose level
2357 // default is 10: a little bit verbose
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002358 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2359 if (argv[0][argv_idx] != NUL)
2360 {
2361 set_option_value((char_u *)"verbosefile", 0L,
2362 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002363 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002364 }
2365 break;
2366
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002367 case 'v': // "-v" Vi-mode (as if called "vi")
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002368 exmode_active = 0;
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002369#if defined(FEAT_GUI) && !defined(VIMDLL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002370 gui.starting = FALSE; // don't start GUI
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002371#endif
2372 break;
2373
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002374 case 'w': // "-w{number}" set window height
2375 // "-w {scriptout}" write to script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002376 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2377 {
2378 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2379 set_option_value((char_u *)"window", n, NULL, 0);
2380 break;
2381 }
2382 want_argument = TRUE;
2383 break;
2384
2385#ifdef FEAT_CRYPT
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002386 case 'x': // "-x" encrypted reading/writing of files
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002387 parmp->ask_for_key = TRUE;
2388 break;
2389#endif
2390
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002391 case 'X': // "-X" don't connect to X server
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002392#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2393 x_no_connect = TRUE;
2394#endif
2395 break;
2396
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002397 case 'Z': // "-Z" restricted mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002398 restricted = TRUE;
2399 break;
2400
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002401 case 'c': // "-c{command}" or "-c {command}" execute
2402 // command
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002403 if (argv[0][argv_idx] != NUL)
2404 {
2405 if (parmp->n_commands >= MAX_ARG_CMDS)
2406 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002407 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2408 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002409 argv_idx = -1;
2410 break;
2411 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002412 // FALLTHROUGH
2413 case 'S': // "-S {file}" execute Vim script
2414 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002415#ifndef FEAT_DIFF
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002416 case 'd': // "-d {device}" device (for Amiga)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002417#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002418 case 'T': // "-T {terminal}" terminal name
2419 case 'u': // "-u {vimrc}" vim inits file
2420 case 'U': // "-U {gvimrc}" gvim inits file
2421 case 'W': // "-W {scriptout}" overwrite
Bram Moolenaar4f974752019-02-17 17:44:42 +01002422#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002423 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002424#endif
2425 want_argument = TRUE;
2426 break;
2427
2428 default:
2429 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2430 }
2431
2432 /*
2433 * Handle option arguments with argument.
2434 */
2435 if (want_argument)
2436 {
2437 /*
2438 * Check for garbage immediately after the option letter.
2439 */
2440 if (argv[0][argv_idx] != NUL)
2441 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2442
2443 --argc;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002444 if (argc < 1 && c != 'S') // -S has an optional argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002445 mainerr_arg_missing((char_u *)argv[0]);
2446 ++argv;
2447 argv_idx = -1;
2448
2449 switch (c)
2450 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002451 case 'c': // "-c {command}" execute command
2452 case 'S': // "-S {file}" execute Vim script
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002453 if (parmp->n_commands >= MAX_ARG_CMDS)
2454 mainerr(ME_EXTRA_CMD, NULL);
2455 if (c == 'S')
2456 {
2457 char *a;
2458
2459 if (argc < 1)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002460 // "-S" without argument: use default session file
2461 // name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462 a = SESSION_FILE;
2463 else if (argv[0][0] == '-')
2464 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002465 // "-S" followed by another option: use default
2466 // session file name.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002467 a = SESSION_FILE;
2468 ++argc;
2469 --argv;
2470 }
2471 else
2472 a = argv[0];
Bram Moolenaar964b3742019-05-24 18:54:09 +02002473 p = alloc(STRLEN(a) + 4);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002474 if (p == NULL)
2475 mch_exit(2);
2476 sprintf((char *)p, "so %s", a);
2477 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2478 parmp->commands[parmp->n_commands++] = p;
2479 }
2480 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002481 parmp->commands[parmp->n_commands++] =
2482 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002483 break;
2484
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002485 case '-':
2486 if (argv[-1][2] == 'c')
2487 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002488 // "--cmd {command}" execute command
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002489 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2490 mainerr(ME_EXTRA_CMD, NULL);
2491 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002492 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002493 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002494 // "--startuptime <file>" already handled
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002495 break;
2496
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002497 // case 'd': -d {device} is handled in mch_check_win() for the
2498 // Amiga
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002499
2500#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002501 case 'q': // "-q {errorfile}" QuickFix mode
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002502 parmp->use_ef = (char_u *)argv[0];
2503 break;
2504#endif
2505
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002506 case 'i': // "-i {viminfo}" use for viminfo
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002507 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508 break;
2509
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002510 case 's': // "-s {scriptin}" read from script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002511 if (scriptin[0] != NULL)
2512 {
2513scripterror:
2514 mch_errmsg(_("Attempt to open script file again: \""));
2515 mch_errmsg(argv[-1]);
2516 mch_errmsg(" ");
2517 mch_errmsg(argv[0]);
2518 mch_errmsg("\"\n");
2519 mch_exit(2);
2520 }
2521 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2522 {
2523 mch_errmsg(_("Cannot open for reading: \""));
2524 mch_errmsg(argv[0]);
2525 mch_errmsg("\"\n");
2526 mch_exit(2);
2527 }
2528 if (save_typebuf() == FAIL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002529 mch_exit(2); // out of memory
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002530 break;
2531
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002532 case 't': // "-t {tag}"
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002533 parmp->tagname = (char_u *)argv[0];
2534 break;
2535
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002536 case 'T': // "-T {terminal}" terminal name
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 /*
2538 * The -T term argument is always available and when
2539 * HAVE_TERMLIB is supported it overrides the environment
2540 * variable TERM.
2541 */
2542#ifdef FEAT_GUI
2543 if (term_is_gui((char_u *)argv[0]))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002544 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545 else
2546#endif
2547 parmp->term = (char_u *)argv[0];
2548 break;
2549
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002550 case 'u': // "-u {vimrc}" vim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551 parmp->use_vimrc = (char_u *)argv[0];
2552 break;
2553
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002554 case 'U': // "-U {gvimrc}" gvim inits file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002555#ifdef FEAT_GUI
2556 use_gvimrc = (char_u *)argv[0];
2557#endif
2558 break;
2559
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002560 case 'w': // "-w {nr}" 'window' value
2561 // "-w {scriptout}" append to script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562 if (vim_isdigit(*((char_u *)argv[0])))
2563 {
2564 argv_idx = 0;
2565 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2566 set_option_value((char_u *)"window", n, NULL, 0);
2567 argv_idx = -1;
2568 break;
2569 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002570 // FALLTHROUGH
2571 case 'W': // "-W {scriptout}" overwrite script file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002572 if (scriptout != NULL)
2573 goto scripterror;
2574 if ((scriptout = mch_fopen(argv[0],
2575 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2576 {
2577 mch_errmsg(_("Cannot open for script output: \""));
2578 mch_errmsg(argv[0]);
2579 mch_errmsg("\"\n");
2580 mch_exit(2);
2581 }
2582 break;
2583
Bram Moolenaar4f974752019-02-17 17:44:42 +01002584#ifdef FEAT_GUI_MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002585 case 'P': // "-P {parent title}" MDI parent
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002586 gui_mch_set_parent(argv[0]);
2587 break;
2588#endif
2589 }
2590 }
2591 }
2592
2593 /*
2594 * File name argument.
2595 */
2596 else
2597 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002598 argv_idx = -1; // skip to next argument
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002600 // Check for only one type of editing.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002601 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2602 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2603 parmp->edit_type = EDIT_FILE;
2604
2605#ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002606 // Remember if the argument was a full path before changing
2607 // slashes to backslashes.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002608 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2609 parmp->full_path = TRUE;
2610#endif
2611
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002612 // Add the file to the global argument list.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002613 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2614 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2615 mch_exit(2);
2616#ifdef FEAT_DIFF
2617 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2618 && !mch_isdir(alist_name(&GARGLIST[0])))
2619 {
2620 char_u *r;
2621
2622 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2623 if (r != NULL)
2624 {
2625 vim_free(p);
2626 p = r;
2627 }
2628 }
2629#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01002630#if defined(__CYGWIN32__) && !defined(MSWIN)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631 /*
2632 * If vim is invoked by non-Cygwin tools, convert away any
2633 * DOS paths, so things like .swp files are created correctly.
2634 * Look for evidence of non-Cygwin paths before we bother.
2635 * This is only for when using the Unix files.
2636 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002637 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002638 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002639 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002640
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002641# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002642 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002643# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002644 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002645# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002646 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002647 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002648 if (p == NULL)
2649 mch_exit(2);
2650 }
2651#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002652
2653#ifdef USE_FNAME_CASE
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002654 // Make the case of the file name match the actual file.
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002655 fname_case(p, 0);
2656#endif
2657
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002658 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002659#ifdef EXPAND_FILENAMES
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002660 parmp->literal ? 2 : 0 // add buffer nr after exp.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002661#else
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002662 2 // add buffer number now and use curbuf
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002663#endif
2664 );
2665
Bram Moolenaar4f974752019-02-17 17:44:42 +01002666#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002668 // Remember this argument has been added to the argument list.
2669 // Needed when 'encoding' is changed.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002670 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002671# ifdef FEAT_DIFF
2672 parmp->diff_mode
2673# else
2674 FALSE
2675# endif
2676 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 }
2678#endif
2679 }
2680
2681 /*
2682 * If there are no more letters after the current "-", go to next
2683 * argument. argv_idx is set to -1 when the current argument is to be
2684 * skipped.
2685 */
2686 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2687 {
2688 --argc;
2689 ++argv;
2690 argv_idx = 1;
2691 }
2692 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002693
2694#ifdef FEAT_EVAL
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002695 // If there is a "+123" or "-c" command, set v:swapcommand to the first
2696 // one.
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002697 if (parmp->n_commands > 0)
2698 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02002699 p = alloc(STRLEN(parmp->commands[0]) + 3);
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002700 if (p != NULL)
2701 {
2702 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2703 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2704 vim_free(p);
2705 }
2706 }
2707#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708}
2709
2710/*
2711 * Print a warning if stdout is not a terminal.
Bram Moolenaar42b23fa2018-02-03 14:46:45 +01002712 * When starting in Ex mode and commands come from a file, set silent_mode.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002713 */
2714 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002715check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002716{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002717 int input_isatty; // is active input a terminal?
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718
2719 input_isatty = mch_input_isatty();
2720 if (exmode_active)
2721 {
2722 if (!input_isatty)
2723 silent_mode = TRUE;
2724 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002725 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002727 // don't want the delay when started from the desktop
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728 && !gui.starting
2729#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002730 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002731 {
2732#ifdef NBDEBUG
2733 /*
2734 * This shouldn't be necessary. But if I run netbeans with the log
2735 * output coming to the console and XOpenDisplay fails, I get vim
2736 * trying to start with input/output to my console tty. This fills my
2737 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002738 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002740 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002741 {
2742 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2743 exit(1);
2744 }
2745#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02002746#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
2747 if (
2748# ifdef VIMDLL
2749 !gui.starting &&
2750# endif
2751 is_cygpty_used())
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002752 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01002753# if defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
Bram Moolenaar2a027452017-09-26 19:10:37 +02002754 && defined(FEAT_GETTEXT)
2755 char *s, *tofree = NULL;
2756
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002757 // Set the encoding of the error message based on $LC_ALL or
2758 // other environment variables instead of 'encoding'.
2759 // Note that the message is shown on a Cygwin terminal (e.g.
2760 // mintty) which encoding is based on $LC_ALL or etc., not the
2761 // current codepage used by normal Win32 console programs.
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002762 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002763 if (s == NULL)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002764 s = "utf-8"; // Use "utf-8" by default.
Bram Moolenaar2a027452017-09-26 19:10:37 +02002765 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2766 vim_free(tofree);
2767# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002768 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2769 exit(1);
2770 }
2771#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002772 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2774 if (!input_isatty)
2775 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2776 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002777 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2778 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779 if (scriptin[0] == NULL)
Bram Moolenaareda1da02019-11-17 17:06:33 +01002780 ui_delay(2005L, TRUE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781 TIME_MSG("Warning delay");
2782 }
2783}
2784
2785/*
2786 * Read text from stdin.
2787 */
2788 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002789read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002790{
2791 int i;
2792
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002793 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002795
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796 no_wait_return = TRUE;
2797 i = msg_didany;
2798 set_buflisted(TRUE);
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002799 (void)open_buffer(TRUE, NULL, 0); // create memfile and read file
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002800 no_wait_return = FALSE;
2801 msg_didany = i;
2802 TIME_MSG("reading stdin");
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002803
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002804 check_swap_exists_action();
Bram Moolenaard0573012017-10-28 21:11:06 +02002805#if !(defined(AMIGA) || defined(MACOS_X))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002806 /*
2807 * Close stdin and dup it from stderr. Required for GPM to work
2808 * properly, and for running external commands.
2809 * Is there any other system that cannot do this?
2810 */
2811 close(0);
Bram Moolenaar42335f52018-09-13 15:33:43 +02002812 vim_ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002813#endif
2814}
2815
2816/*
2817 * Create the requested number of windows and edit buffers in them.
2818 * Also does recovery if "recoverymode" set.
2819 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002820 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002821create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002822{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002823 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002824 int done = 0;
2825
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002826 /*
2827 * Create the number of windows that was requested.
2828 */
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002829 if (parmp->window_count == -1) // was not set
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002830 parmp->window_count = 1;
2831 if (parmp->window_count == 0)
2832 parmp->window_count = GARGCOUNT;
2833 if (parmp->window_count > 1)
2834 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002835 // Don't change the windows if there was a command in .vimrc that
2836 // already split some windows
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002837 if (parmp->window_layout == 0)
2838 parmp->window_layout = WIN_HOR;
2839 if (parmp->window_layout == WIN_TABS)
2840 {
2841 parmp->window_count = make_tabpages(parmp->window_count);
2842 TIME_MSG("making tab pages");
2843 }
2844 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002845 {
2846 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002847 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002848 TIME_MSG("making windows");
2849 }
2850 else
2851 parmp->window_count = win_count();
2852 }
2853 else
2854 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002855
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002856 if (recoverymode) // do recover
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002857 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002858 msg_scroll = TRUE; // scroll message up
Bram Moolenaar99499b12019-05-23 21:35:48 +02002859 ml_recover(TRUE);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002860 if (curbuf->b_ml.ml_mfp == NULL) // failed
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002861 getout(1);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002862 do_modelines(0); // do modelines
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002863 }
2864 else
2865 {
2866 /*
2867 * Open a buffer for windows that don't have one yet.
2868 * Commands in the .vimrc might have loaded a file or split the window.
2869 * Watch out for autocommands that delete a window.
2870 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871 /*
2872 * Don't execute Win/Buf Enter/Leave autocommands here
2873 */
2874 ++autocmd_no_enter;
2875 ++autocmd_no_leave;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002876 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002877 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002878 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002879 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002880 {
2881 if (parmp->window_layout == WIN_TABS)
2882 goto_tabpage(1);
2883 else
2884 curwin = firstwin;
2885 }
2886 else if (parmp->window_layout == WIN_TABS)
2887 {
2888 if (curtab->tp_next == NULL)
2889 break;
2890 goto_tabpage(0);
2891 }
2892 else
2893 {
2894 if (curwin->w_next == NULL)
2895 break;
2896 curwin = curwin->w_next;
2897 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002898 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 curbuf = curwin->w_buffer;
2900 if (curbuf->b_ml.ml_mfp == NULL)
2901 {
2902#ifdef FEAT_FOLDING
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002903 // Set 'foldlevel' to 'foldlevelstart' if it's not negative.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002904 if (p_fdls >= 0)
2905 curwin->w_p_fdl = p_fdls;
2906#endif
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002907 // When getting the ATTENTION prompt here, use a dialog
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002908 swap_exists_action = SEA_DIALOG;
Bram Moolenaar67cf86b2019-04-28 22:25:38 +02002909
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002910 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002911
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002912 // create memfile, read file
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002913 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002914
Bram Moolenaar84212822006-11-07 21:59:47 +00002915 if (swap_exists_action == SEA_QUIT)
2916 {
2917 if (got_int || only_one_window())
2918 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002919 // abort selected or quit and only one window
2920 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00002921 getout(1);
2922 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002923 // We can't close the window, it would disturb what
2924 // happens next. Clear the file name and set the arg
2925 // index to -1 to delete it later.
Bram Moolenaar84212822006-11-07 21:59:47 +00002926 setfname(curbuf, NULL, NULL, FALSE);
2927 curwin->w_arg_idx = -1;
2928 swap_exists_action = SEA_NONE;
2929 }
2930 else
2931 handle_swap_exists(NULL);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002932 dorewind = TRUE; // start again
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002933 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002934 ui_breakcheck();
2935 if (got_int)
2936 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002937 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002938 break;
2939 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002940 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002941 if (parmp->window_layout == WIN_TABS)
2942 goto_tabpage(1);
2943 else
2944 curwin = firstwin;
2945 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002946 --autocmd_no_enter;
2947 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002948 }
2949}
2950
Bram Moolenaar9810cfb2019-12-11 21:23:00 +01002951/*
2952 * If opened more than one window, start editing files in the other
2953 * windows. make_windows() has already opened the windows.
2954 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002955 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002956edit_buffers(
2957 mparm_T *parmp,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002958 char_u *cwd) // current working dir
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002959{
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002960 int arg_idx; // index in argument list
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002961 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002962 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002963 win_T *win;
Bram Moolenaarc75e8122019-04-21 15:55:10 +02002964 char_u *p_shm_save = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002965
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002966 /*
2967 * Don't execute Win/Buf Enter/Leave autocommands here
2968 */
2969 ++autocmd_no_enter;
2970 ++autocmd_no_leave;
Bram Moolenaar84212822006-11-07 21:59:47 +00002971
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002972 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002973 if (curwin->w_arg_idx == -1)
2974 {
2975 win_close(curwin, TRUE);
2976 advance = FALSE;
2977 }
2978
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002979 arg_idx = 1;
2980 for (i = 1; i < parmp->window_count; ++i)
2981 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002982 if (cwd != NULL)
2983 mch_chdir((char *)cwd);
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002984 // When w_arg_idx is -1 remove the window (see create_windows()).
Bram Moolenaar84212822006-11-07 21:59:47 +00002985 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002986 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002987 ++arg_idx;
2988 win_close(curwin, TRUE);
2989 advance = FALSE;
2990 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002991 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002992
Bram Moolenaar84212822006-11-07 21:59:47 +00002993 if (advance)
2994 {
2995 if (parmp->window_layout == WIN_TABS)
2996 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01002997 if (curtab->tp_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00002998 break;
2999 goto_tabpage(0);
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003000 // Temporarily reset 'shm' option to not print fileinfo when
3001 // loading the other buffers. This would overwrite the already
3002 // existing fileinfo for the first tab.
3003 if (i == 1)
3004 {
3005 char buf[100];
3006
3007 p_shm_save = vim_strsave(p_shm);
3008 vim_snprintf(buf, 100, "F%s", p_shm);
3009 set_option_value((char_u *)"shm", 0L, (char_u *)buf, 0);
3010 }
Bram Moolenaar84212822006-11-07 21:59:47 +00003011 }
3012 else
3013 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003014 if (curwin->w_next == NULL) // just checking
Bram Moolenaar84212822006-11-07 21:59:47 +00003015 break;
3016 win_enter(curwin->w_next, FALSE);
3017 }
3018 }
3019 advance = TRUE;
3020
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003021 // Only open the file if there is no file in this window yet (that can
3022 // happen when .vimrc contains ":sall").
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003023 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
3024 {
3025 curwin->w_arg_idx = arg_idx;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003026 // Edit file from arg list, if there is one. When "Quit" selected
3027 // at the ATTENTION prompt close the window.
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003028 swap_exists_did_quit = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003029 (void)do_ecmd(0, arg_idx < GARGCOUNT
3030 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003031 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00003032 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00003033 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003034 // abort or quit selected
Bram Moolenaar84212822006-11-07 21:59:47 +00003035 if (got_int || only_one_window())
3036 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003037 // abort selected and only one window
3038 did_emsg = FALSE; // avoid hit-enter prompt
Bram Moolenaar84212822006-11-07 21:59:47 +00003039 getout(1);
3040 }
3041 win_close(curwin, TRUE);
3042 advance = FALSE;
3043 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003044 if (arg_idx == GARGCOUNT - 1)
3045 arg_had_last = TRUE;
3046 ++arg_idx;
3047 }
3048 ui_breakcheck();
3049 if (got_int)
3050 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003051 (void)vgetc(); // only break the file loading, not the rest
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003052 break;
3053 }
3054 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003055
Bram Moolenaarc75e8122019-04-21 15:55:10 +02003056 if (p_shm_save != NULL)
3057 {
3058 set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
3059 vim_free(p_shm_save);
3060 }
3061
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003062 if (parmp->window_layout == WIN_TABS)
3063 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003064 --autocmd_no_enter;
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003065
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003066 // make the first window the current window
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003067 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02003068#if defined(FEAT_QUICKFIX)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003069 // Avoid making a preview window the current window.
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003070 while (win->w_p_pvw)
3071 {
3072 win = win->w_next;
3073 if (win == NULL)
3074 {
3075 win = firstwin;
3076 break;
3077 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003078 }
3079#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02003080 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02003081
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003082 --autocmd_no_leave;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003083 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003084 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003085 win_equal(curwin, FALSE, 'b'); // adjust heights
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003086}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003087
3088/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003089 * Execute the commands from --cmd arguments "cmds[cnt]".
3090 */
3091 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003092exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003093{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003094 char_u **cmds = parmp->pre_commands;
3095 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003096 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003097 ESTACK_CHECK_DECLARATION
Bram Moolenaar58d98232005-07-23 22:25:46 +00003098
3099 if (cnt > 0)
3100 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003101 curwin->w_cursor.lnum = 0; // just in case..
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003102 estack_push(ETYPE_ARGS, (char_u *)_("pre-vimrc command line"), 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003103 ESTACK_CHECK_SETUP
Bram Moolenaar58d98232005-07-23 22:25:46 +00003104# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003105 current_sctx.sc_sid = SID_CMDARG;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003106# endif
3107 for (i = 0; i < cnt; ++i)
3108 do_cmdline_cmd(cmds[i]);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003109 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003110 estack_pop();
Bram Moolenaar58d98232005-07-23 22:25:46 +00003111# ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003112 current_sctx.sc_sid = 0;
Bram Moolenaar58d98232005-07-23 22:25:46 +00003113# endif
3114 TIME_MSG("--cmd commands");
3115 }
3116}
3117
3118/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003119 * Execute "+", "-c" and "-S" arguments.
3120 */
3121 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003122exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003123{
3124 int i;
Bram Moolenaare31ee862020-01-07 20:59:34 +01003125 ESTACK_CHECK_DECLARATION
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003126
3127 /*
3128 * We start commands on line 0, make "vim +/pat file" match a
3129 * pattern on line 1. But don't move the cursor when an autocommand
3130 * with g`" was used.
3131 */
3132 msg_scroll = TRUE;
3133 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
3134 curwin->w_cursor.lnum = 0;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003135 estack_push(ETYPE_ARGS, (char_u *)"command line", 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003136 ESTACK_CHECK_SETUP
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003137#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003138 current_sctx.sc_sid = SID_CARG;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003139 current_sctx.sc_seq = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003140#endif
3141 for (i = 0; i < parmp->n_commands; ++i)
3142 {
3143 do_cmdline_cmd(parmp->commands[i]);
3144 if (parmp->cmds_tofree[i])
3145 vim_free(parmp->commands[i]);
3146 }
Bram Moolenaare31ee862020-01-07 20:59:34 +01003147 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003148 estack_pop();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003149#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003150 current_sctx.sc_sid = 0;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003151#endif
3152 if (curwin->w_cursor.lnum == 0)
3153 curwin->w_cursor.lnum = 1;
3154
3155 if (!exmode_active)
3156 msg_scroll = FALSE;
3157
3158#ifdef FEAT_QUICKFIX
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003159 // When started with "-q errorfile" jump to first error again.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003160 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003161 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003162#endif
3163 TIME_MSG("executing command arguments");
3164}
3165
3166/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00003167 * Source startup scripts.
3168 */
3169 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003170source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003171{
3172 int i;
3173
3174 /*
3175 * For "evim" source evim.vim first of all, so that the user can overrule
3176 * any things he doesn't like.
3177 */
3178 if (parmp->evim_mode)
3179 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003181 TIME_MSG("source evim file");
3182 }
3183
3184 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003185 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003186 * nothing else.
3187 */
3188 if (parmp->use_vimrc != NULL)
3189 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003190 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003192 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003193 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003194 {
3195#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003196 if (use_gvimrc == NULL) // don't load gvimrc either
Bram Moolenaar58d98232005-07-23 22:25:46 +00003197 use_gvimrc = parmp->use_vimrc;
3198#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003199 }
3200 else
3201 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE, NULL) != OK)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003203 semsg(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003204 }
3205 }
3206 else if (!silent_mode)
3207 {
3208#ifdef AMIGA
3209 struct Process *proc = (struct Process *)FindTask(0L);
3210 APTR save_winptr = proc->pr_WindowPtr;
3211
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003212 // Avoid a requester here for a volume that doesn't exist.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003213 proc->pr_WindowPtr = (APTR)-1L;
3214#endif
3215
3216 /*
3217 * Get system wide defaults, if the file name is defined.
3218 */
3219#ifdef SYS_VIMRC_FILE
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003221#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003222#ifdef MACOS_X
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE,
3224 DOSO_NONE, NULL);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003225#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003226
3227 /*
3228 * Try to read initialization commands from the following places:
3229 * - environment variable VIMINIT
3230 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3231 * - second user vimrc file ($VIM/.vimrc for Dos)
3232 * - environment variable EXINIT
3233 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3234 * - second user exrc file ($VIM/.exrc for Dos)
3235 * The first that exists is used, the rest is ignored.
3236 */
3237 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3238 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 if (do_source((char_u *)USR_VIMRC_FILE, TRUE,
3240 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003241#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003242 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003244#endif
3245#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003246 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003248#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003249#ifdef USR_VIMRC_FILE4
3250 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 DOSO_VIMRC, NULL) == FAIL
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003252#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003253 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 && do_source((char_u *)USR_EXRC_FILE, FALSE,
3255 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003256#ifdef USR_EXRC_FILE2
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 && do_source((char_u *)USR_EXRC_FILE2, FALSE,
3258 DOSO_NONE, NULL) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003259#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003260 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003261 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003262 // When no .vimrc file was found: source defaults.vim.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003264 }
3265 }
3266
3267 /*
3268 * Read initialization commands from ".vimrc" or ".exrc" in current
3269 * directory. This is only done if the 'exrc' option is set.
3270 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003271 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003272 * option has been reset in environment of global ".exrc" or ".vimrc".
3273 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3274 * SYS_VIMRC_FILE.
3275 */
3276 if (p_exrc)
3277 {
3278#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003279 // If ".vimrc" file is not owned by user, set 'secure' mode.
Bram Moolenaar58d98232005-07-23 22:25:46 +00003280 if (!file_owned(VIMRC_FILE))
3281#endif
3282 secure = p_secure;
3283
3284 i = FAIL;
3285 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003286 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003287#ifdef USR_VIMRC_FILE2
3288 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003289 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003290#endif
3291#ifdef USR_VIMRC_FILE3
3292 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003293 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003294#endif
3295#ifdef SYS_VIMRC_FILE
3296 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003297 (char_u *)VIMRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003298#endif
3299 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003301
3302 if (i == FAIL)
3303 {
3304#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003305 // if ".exrc" is not owned by user set 'secure' mode
Bram Moolenaar58d98232005-07-23 22:25:46 +00003306 if (!file_owned(EXRC_FILE))
3307 secure = p_secure;
3308 else
3309 secure = 0;
3310#endif
3311 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003312 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003313#ifdef USR_EXRC_FILE2
3314 && fullpathcmp((char_u *)USR_EXRC_FILE2,
Bram Moolenaar99499b12019-05-23 21:35:48 +02003315 (char_u *)EXRC_FILE, FALSE, TRUE) != FPC_SAME
Bram Moolenaar58d98232005-07-23 22:25:46 +00003316#endif
3317 )
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 (void)do_source((char_u *)EXRC_FILE, FALSE,
3319 DOSO_NONE, NULL);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003320 }
3321 }
3322 if (secure == 2)
3323 need_wait_return = TRUE;
3324 secure = 0;
3325#ifdef AMIGA
3326 proc->pr_WindowPtr = save_winptr;
3327#endif
3328 }
3329 TIME_MSG("sourcing vimrc file(s)");
3330}
3331
3332/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333 * Setup to start using the GUI. Exit with an error when not available.
3334 */
3335 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003336main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003337{
3338#ifdef FEAT_GUI
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003339 gui.starting = TRUE; // start GUI a bit later
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340#else
3341 mch_errmsg(_(e_nogvim));
3342 mch_errmsg("\n");
3343 mch_exit(2);
3344#endif
3345}
3346
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003347#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003348
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 * Get an environment variable and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003351 * Returns FAIL if the environment variable was not executed, OK otherwise.
3352 */
3353 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003354process_env(
3355 char_u *env,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003356 int is_viminit) // when TRUE, called for VIMINIT
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003357{
3358 char_u *initstr;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003359#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003360 sctx_T save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003361#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +01003362 ESTACK_CHECK_DECLARATION
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003363
3364 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3365 {
3366 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003367 vimrc_found(NULL, NULL);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003368 estack_push(ETYPE_ENV, env, 0);
Bram Moolenaare31ee862020-01-07 20:59:34 +01003369 ESTACK_CHECK_SETUP
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003370#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003371 save_current_sctx = current_sctx;
3372 current_sctx.sc_sid = SID_ENV;
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01003373 current_sctx.sc_seq = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003374 current_sctx.sc_lnum = 0;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003375 current_sctx.sc_version = 1;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376#endif
3377 do_cmdline_cmd(initstr);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003378
Bram Moolenaare31ee862020-01-07 20:59:34 +01003379 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003380 estack_pop();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003381#ifdef FEAT_EVAL
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02003382 current_sctx = save_current_sctx;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003383#endif
3384 return OK;
3385 }
3386 return FAIL;
3387}
3388
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003389#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003390/*
3391 * Return TRUE if we are certain the user owns the file "fname".
3392 * Used for ".vimrc" and ".exrc".
3393 * Use both stat() and lstat() for extra security.
3394 */
3395 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003396file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003398 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399# ifdef UNIX
3400 uid_t uid = getuid();
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003401# else // VMS
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402 uid_t uid = ((getgid() << 16) | getuid());
3403# endif
3404
3405 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3406# ifdef HAVE_LSTAT
3407 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3408# endif
3409 );
3410}
3411#endif
3412
3413/*
3414 * Give an error message main_errors["n"] and exit.
3415 */
3416 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003417mainerr(
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003418 int n, // one of the ME_ defines
3419 char_u *str) // extra argument or NULL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003420{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003421#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003422 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003423#endif
3424
Bram Moolenaar0b75f7c2019-05-08 22:28:46 +02003425 // If this is a Windows GUI executable, show an error dialog box.
3426#ifdef VIMDLL
3427 gui.in_use = mch_is_gui_executable();
3428#endif
3429#ifdef FEAT_GUI_MSWIN
3430 gui.starting = FALSE; // Needed to show as error.
3431#endif
3432
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003433 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003434 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003435 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436 mch_errmsg(_(main_errors[n]));
3437 if (str != NULL)
3438 {
3439 mch_errmsg(": \"");
3440 mch_errmsg((char *)str);
3441 mch_errmsg("\"");
3442 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003443 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003444
3445 mch_exit(1);
3446}
3447
3448 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003449mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450{
3451 mainerr(ME_ARG_MISSING, str);
3452}
3453
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003454#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003455/*
3456 * print a message with three spaces prepended and '\n' appended.
3457 */
3458 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003459main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003460{
3461 mch_msg(" ");
3462 mch_msg(s);
3463 mch_msg("\n");
3464}
3465
3466/*
3467 * Print messages for "vim -h" or "vim --help" and exit.
3468 */
3469 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003470usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003471{
3472 int i;
3473 static char *(use[]) =
3474 {
3475 N_("[file ..] edit specified file(s)"),
3476 N_("- read text from stdin"),
3477 N_("-t tag edit file where tag is defined"),
3478#ifdef FEAT_QUICKFIX
3479 N_("-q [errorfile] edit file with first error")
3480#endif
3481 };
3482
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003483#if defined(UNIX) || defined(VMS)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003484 reset_signals(); // kill us with CTRL-C here, if you like
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003485#endif
3486
Bram Moolenaar35fb6fb2018-06-23 16:12:21 +02003487 init_longVersion();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003488 mch_msg(longVersion);
Bram Moolenaar32aaf5a2018-06-21 21:38:33 +02003489 mch_msg(_("\n\nUsage:"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490 for (i = 0; ; ++i)
3491 {
3492 mch_msg(_(" vim [arguments] "));
3493 mch_msg(_(use[i]));
3494 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3495 break;
3496 mch_msg(_("\n or:"));
3497 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003498#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003499 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003500#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003501
3502 mch_msg(_("\n\nArguments:\n"));
3503 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003504#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003505 main_msg(_("--literal\t\tDon't expand wildcards"));
3506#endif
3507#ifdef FEAT_OLE
3508 main_msg(_("-register\t\tRegister this gvim for OLE"));
3509 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3510#endif
3511#ifdef FEAT_GUI
3512 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3513 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3514#endif
3515 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3516 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003517 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003518 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3519#ifdef FEAT_DIFF
3520 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3521#endif
3522 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3523 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3524 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3525 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3526 main_msg(_("-M\t\t\tModifications in text not allowed"));
3527 main_msg(_("-b\t\t\tBinary mode"));
3528#ifdef FEAT_LISP
3529 main_msg(_("-l\t\t\tLisp mode"));
3530#endif
3531 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3532 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003533 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3534#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003536#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003537 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3538 main_msg(_("-r\t\t\tList swap files and exit"));
3539 main_msg(_("-r (with file name)\tRecover crashed session"));
3540 main_msg(_("-L\t\t\tSame as -r"));
3541#ifdef AMIGA
3542 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3543 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3544#endif
3545#ifdef FEAT_ARABIC
Bram Moolenaar1d4754f2018-06-19 17:49:24 +02003546 main_msg(_("-A\t\t\tStart in Arabic mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003547#endif
3548#ifdef FEAT_RIGHTLEFT
3549 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3550#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003551 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003552 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003553 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003554 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3555#ifdef FEAT_GUI
3556 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3557#endif
3558 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003559 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003560 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3561 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3562 main_msg(_("+\t\t\tStart at end of file"));
3563 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003564 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003565 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3566 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3567 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3568 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3569 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3570#ifdef FEAT_CRYPT
3571 main_msg(_("-x\t\t\tEdit encrypted files"));
3572#endif
3573#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3574# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
Bram Moolenaar86181df2020-05-11 23:14:04 +02003575 main_msg(_("-display <display>\tConnect Vim to this particular X-server"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003576# endif
3577 main_msg(_("-X\t\t\tDo not connect to X server"));
3578#endif
3579#ifdef FEAT_CLIENTSERVER
3580 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3581 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3582 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3583 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003584 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003585 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3586 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3587 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3588 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3589#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003590#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003591 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003592#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003593#ifdef FEAT_VIMINFO
3594 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3595#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003596 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003597 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3598 main_msg(_("--version\t\tPrint version information and exit"));
3599
3600#ifdef FEAT_GUI_X11
3601# ifdef FEAT_GUI_MOTIF
3602 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3603# else
3604# ifdef FEAT_GUI_ATHENA
3605# ifdef FEAT_GUI_NEXTAW
3606 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3607# else
3608 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3609# endif
3610# endif
3611# endif
Bram Moolenaar86181df2020-05-11 23:14:04 +02003612 main_msg(_("-display <display>\tRun Vim on <display>"));
Bram Moolenaar9e6ba8c2020-05-12 22:21:26 +02003613 main_msg(_("-iconic\t\tStart Vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003614 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3615 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3616 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3617 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3618 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3619 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3620 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3621 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3622# ifdef FEAT_GUI_ATHENA
3623 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3624# endif
3625 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3626 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3627 main_msg(_("-xrm <resource>\tSet the specified resource"));
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003628#endif // FEAT_GUI_X11
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003629#ifdef FEAT_GUI_GTK
3630 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3631 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3632 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3633 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
Bram Moolenaar86181df2020-05-11 23:14:04 +02003634 main_msg(_("-display <display>\tRun Vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003635 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003636 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003637 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003638#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01003639#ifdef FEAT_GUI_MSWIN
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003640# ifdef VIMDLL
3641 if (gui.starting)
3642# endif
3643 {
3644 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
3645 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
3646 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003647#endif
3648
3649#ifdef FEAT_GUI_GNOME
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003650 // Gnome gives extra messages for --help if we continue, but not for -h.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003651 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003652 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003653 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003654 gui.dofork = FALSE;
3655 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003656 else
3657#endif
3658 mch_exit(0);
3659}
3660
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003661/*
3662 * Check the result of the ATTENTION dialog:
3663 * When "Quit" selected, exit Vim.
3664 * When "Recover" selected, recover the file.
3665 */
3666 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003667check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003668{
3669 if (swap_exists_action == SEA_QUIT)
3670 getout(1);
3671 handle_swap_exists(NULL);
3672}
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003673
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003674#endif // NO_VIM_MAIN
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003675
Bram Moolenaar595297d2017-03-04 19:11:12 +01003676#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003677 static void
3678set_progpath(char_u *argv0)
3679{
3680 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003681
Bram Moolenaar4f974752019-02-17 17:44:42 +01003682# ifdef MSWIN
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003683 // A relative path containing a "/" will become invalid when using ":cd",
3684 // turn it into a full path.
3685 // On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3686 // this.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003687 char_u *path = NULL;
3688
3689 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3690 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003691# else
3692 char_u buf[MAXPATHL + 1];
3693# ifdef PROC_EXE_LINK
3694 char linkbuf[MAXPATHL + 1];
3695 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003696
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003697 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3698 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003699 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003700 linkbuf[len] = NUL;
3701 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003702 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003703# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003704
3705 if (!mch_isFullName(val))
3706 {
3707 if (gettail(val) != val
3708 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3709 val = buf;
3710 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003711# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003712
Bram Moolenaar08cab962017-03-04 14:37:18 +01003713 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003714
Bram Moolenaar4f974752019-02-17 17:44:42 +01003715# ifdef MSWIN
Bram Moolenaar43663192017-03-05 14:29:12 +01003716 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003717# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003718}
3719
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01003720#endif // NO_VIM_MAIN