blob: 268c256270eeee3812826c62464568675297caf1 [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__
14# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
16# 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 Moolenaar97ff9b92016-06-26 20:37:46 +020022#if defined(WIN3264) && !defined(FEAT_GUI_W32)
23# include "iscygpty.h"
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* 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 */
32
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 main_msg(char *s);
43static void usage(void);
44static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void command_line_scan(mparm_T *parmp);
47static void check_tty(mparm_T *parmp);
48static void read_stdin(void);
49static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010050# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010051static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010052# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010053static void exe_pre_commands(mparm_T *parmp);
54static void exe_commands(mparm_T *parmp);
55static void source_startup_scripts(mparm_T *parmp);
56static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010057# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010058static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010059# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +010060# ifdef FEAT_EVAL
61static void set_progpath(char_u *argv0);
62# endif
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010063# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void exec_on_server(mparm_T *parmp);
65static void prepare_server(mparm_T *parmp);
66static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
67static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010068# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000069#endif
70
71
Bram Moolenaarb4210b32004-06-13 14:51:16 +000072/*
73 * Different types of error messages.
74 */
75static char *(main_errors[]) =
76{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000077 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000078#define ME_UNKNOWN_OPTION 0
79 N_("Too many edit arguments"),
80#define ME_TOO_MANY_ARGS 1
81 N_("Argument missing after"),
82#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000083 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000084#define ME_GARBAGE 3
85 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
86#define ME_EXTRA_CMD 4
87 N_("Invalid argument for"),
88#define ME_INVALID_ARG 5
89};
90
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010091#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaara6044292017-04-02 18:19:53 +020092
93/* Various parameters passed between main() and other functions. */
94static mparm_T params;
95
Bram Moolenaar8866d272012-11-28 15:55:42 +010096#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020097
98static char_u *start_dir = NULL; /* current working dir on startup */
99
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +0200100static int has_dash_c_arg = FALSE;
101
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000102 int
103# ifdef VIMDLL
104_export
105# endif
106# ifdef FEAT_GUI_MSWIN
107# ifdef __BORLANDC__
108_cdecl
109# endif
110VimMain
111# else
112main
113# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100114(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000115{
Bram Moolenaar3f269672009-11-03 11:11:11 +0000116#ifdef STARTUPTIME
117 int i;
118#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000119
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120 /*
121 * Do any system-specific initialisations. These can NOT use IObuff or
122 * NameBuff. Thus emsg2() cannot be called!
123 */
124 mch_early_init();
125
Bram Moolenaar14993322014-09-09 12:25:33 +0200126#if defined(WIN32) && defined(FEAT_MBYTE)
127 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200128 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200129 * convert when 'encoding' changes. Get the unexpanded arguments.
130 */
131 argc = get_cmd_argsW(&argv);
132#endif
133
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 /* Many variables are in "params" so that we can pass them to invoked
135 * functions without a lot of arguments. "argc" and "argv" are also
136 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000137 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000138 params.argc = argc;
139 params.argv = argv;
140 params.want_full_screen = TRUE;
141#ifdef FEAT_EVAL
142 params.use_debug_break_level = -1;
143#endif
144#ifdef FEAT_WINDOWS
145 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000146#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000147
Bram Moolenaar99685e62013-05-11 13:56:18 +0200148#ifdef FEAT_RUBY
149 {
150 int ruby_stack_start;
151 vim_ruby_init((void *)&ruby_stack_start);
152 }
153#endif
154
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000155#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000156 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000157#endif
158
159#ifdef MEM_PROFILE
160 atexit(vim_mem_profile_dump);
161#endif
162
163#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200164 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000165 for (i = 1; i < argc; ++i)
166 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000167 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000168 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000169 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000170 TIME_MSG("--- VIM STARTING ---");
171 break;
172 }
173 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000175 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200177 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178
179#ifdef FEAT_CLIENTSERVER
180 /*
181 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000182 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000183 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000184 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000185#endif
186
187 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000188 * Figure out the way to work from the command name argv[0].
189 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000190 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000192
193 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000194 * Process the command line arguments. File names are put in the global
195 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000196 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198 TIME_MSG("parsing arguments");
199
200 /*
201 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000202 * there is no terminal version, and on Windows we can't fork one off with
203 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000204 */
205#ifdef ALWAYS_USE_GUI
206 gui.starting = TRUE;
207#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000208# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000209 /*
210 * Check if the GUI can be started. Reset gui.starting if not.
211 * Don't know about other systems, stay on the safe side and don't check.
212 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000213 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000215 if (gui_init_check() == FAIL)
216 {
217 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000219 /* When running "evim" or "gvim -y" we need the menus, exit if we
220 * don't have them. */
221 if (params.evim_mode)
222 mch_exit(1);
223 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 }
225# endif
226#endif
227
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000228 if (GARGCOUNT > 0)
229 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100230#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231 /*
232 * Expand wildcards in file names.
233 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000234 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200236 start_dir = alloc(MAXPATHL);
237 if (start_dir != NULL)
238 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 /* Temporarily add '(' and ')' to 'isfname'. These are valid
240 * filename characters but are excluded from 'isfname' to make
241 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
242 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000243 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200245 if (start_dir != NULL)
246 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000247 }
248#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200249 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000251
252#if defined(WIN32) && defined(FEAT_MBYTE)
253 {
254 extern void set_alist_count(void);
255
256 /* Remember the number of entries in the argument list. If it changes
257 * we don't react on setting 'encoding'. */
258 set_alist_count();
259 }
260#endif
261
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000263 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000264 {
265 /*
266 * If there is one filename, fully qualified, we have very probably
267 * been invoked from explorer, so change to the file's directory.
268 * Hint: to avoid this when typing a command use a forward slash.
269 * If the cd fails, it doesn't matter.
270 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200271 (void)vim_chdirfile(params.fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200272 if (start_dir != NULL)
273 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000274 }
275#endif
276 TIME_MSG("expanding arguments");
277
278#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000279 if (params.diff_mode && params.window_count == -1)
280 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000281#endif
282
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000283 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000284 ++RedrawingDisabled;
285
286 /*
287 * When listing swap file names, don't do cursor positioning et. al.
288 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200289 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000290 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291
292 /*
293 * When certain to start the GUI, don't check capabilities of terminal.
294 * For GTK we can't be sure, but when started from the desktop it doesn't
295 * make sense to try using a terminal.
296 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000297#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 if (gui.starting
299# ifdef FEAT_GUI_GTK
300 && !isatty(2)
301# endif
302 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000303 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000304#endif
305
306#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
307 /* When the GUI is started from Finder, need to display messages in a
308 * message box. isatty(2) returns TRUE anyway, thus we need to check the
309 * name to know we're not started from a terminal. */
310 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000311 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000312 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000313
314 /* Avoid always using "/" as the current directory. Note that when
315 * started from Finder the arglist will be filled later in
316 * HandleODocAE() and "fname" will be NULL. */
317 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
318 && STRCMP(NameBuff, "/") == 0)
319 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200320 if (params.fname != NULL)
321 (void)vim_chdirfile(params.fname);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000322 else
323 {
324 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
325 vim_chdir(NameBuff);
326 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200327 if (start_dir != NULL)
328 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000329 }
330 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331#endif
332
333 /*
334 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100335 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336 * Note that we may use mch_exit() before mch_init()!
337 */
338 mch_init();
339 TIME_MSG("shell init");
340
341#ifdef USE_XSMP
342 /*
343 * For want of anywhere else to do it, try to connect to xsmp here.
344 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
345 * Hijacking -X 'no X connection' to also disable XSMP connection as that
346 * has a similar delay upon failure.
347 * Only try if SESSION_MANAGER is set to something non-null.
348 */
349 if (!x_no_connect)
350 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000351 char *p = getenv("SESSION_MANAGER");
352
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353 if (p != NULL && *p != NUL)
354 {
355 xsmp_init();
356 TIME_MSG("xsmp init");
357 }
358 }
359#endif
360
361 /*
362 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000363 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000366 /* This message comes before term inits, but after setting "silent_mode"
367 * when the input is not a tty. */
368 if (GARGCOUNT > 1 && !silent_mode)
369 printf(_("%d files to edit\n"), GARGCOUNT);
370
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000371 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000373 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000374 capabilities (will set full_screen) */
375 screen_start(); /* don't know where cursor is now */
376 TIME_MSG("Termcap init");
377 }
378
379 /*
380 * Set the default values for the options that use Rows and Columns.
381 */
382 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000383 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384#ifdef FEAT_DIFF
385 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
386 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000387 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388 diff_win_options(firstwin, FALSE);
389#endif
390
391 cmdline_row = Rows - p_ch;
392 msg_row = cmdline_row;
393 screenalloc(FALSE); /* allocate screen buffers */
394 set_init_2();
395 TIME_MSG("inits 2");
396
397 msg_scroll = TRUE;
398 no_wait_return = TRUE;
399
400 init_mappings(); /* set up initial mappings */
401
402 init_highlight(TRUE, FALSE); /* set the default highlight groups */
403 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404
405#ifdef FEAT_EVAL
406 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000407 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408#endif
409
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100410#ifdef FEAT_MZSCHEME
411 /*
412 * Newer version of MzScheme (Racket) require earlier (trampolined)
413 * initialisation via scheme_main_setup.
414 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200415 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100416 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200417 return mzscheme_main();
418#else
419 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100420#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200421}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100422#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200423#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100424
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200425/*
426 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
427 * things simple.
428 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
429 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100430 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200431vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100432{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100433#ifndef NO_VIM_MAIN
Bram Moolenaar66459b72016-08-06 19:01:55 +0200434 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
435 * Allows for setting 'loadplugins' there. */
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200436 if (params.use_vimrc != NULL
437 && (STRCMP(params.use_vimrc, "NONE") == 0
438 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
Bram Moolenaar66459b72016-08-06 19:01:55 +0200439 p_lpl = FALSE;
440
Bram Moolenaar58d98232005-07-23 22:25:46 +0000441 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000442 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000443
Bram Moolenaar58d98232005-07-23 22:25:46 +0000444 /* Source startup scripts. */
445 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000446
447#ifdef FEAT_EVAL
448 /*
449 * Read all the plugin files.
450 * Only when compiled with +eval, since most plugins need it.
451 */
452 if (p_lpl)
453 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200454 char_u *rtp_copy = NULL;
455
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200456 /* First add all package directories to 'runtimepath', so that their
457 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200458 * :packloadall command.
459 * Make a copy of 'runtimepath', so that source_runtime does not use
460 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200461 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462 {
463 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200464 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200465 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200466
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200467 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000468# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200469 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000470# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200471 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000472# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200473 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000474 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200475 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100476
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200477 /* Only source "start" packages if not done already with a :packloadall
478 * command. */
479 if (!did_source_packages)
480 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100481 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200482
483# ifdef VMS /* Somehow VMS doesn't handle the "**". */
484 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
485# else
486 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
487# endif
488 TIME_MSG("loading after plugins");
489
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000490 }
491#endif
492
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000493#ifdef FEAT_DIFF
494 /* Decide about window layout for diff mode after reading vimrc. */
495 if (params.diff_mode && params.window_layout == 0)
496 {
497 if (diffopt_horizontal())
498 params.window_layout = WIN_HOR; /* use horizontal split */
499 else
500 params.window_layout = WIN_VER; /* use vertical split */
501 }
502#endif
503
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000504 /*
505 * Recovery mode without a file name: List swap files.
506 * This uses the 'dir' option, therefore it must be after the
507 * initializations.
508 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200509 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200511 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000512 mch_exit(0);
513 }
514
515 /*
516 * Set a few option defaults after reading .vimrc files:
517 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
518 */
519 set_init_3();
520 TIME_MSG("inits 3");
521
522 /*
523 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
524 * Note that this overrides anything from a vimrc file.
525 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000526 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527 p_uc = 0;
528
529#ifdef FEAT_FKMAP
530 if (curwin->w_p_rl && p_altkeymap)
531 {
532 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
533# ifdef FEAT_ARABIC
534 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
535# endif
536 p_fkmap = TRUE; /* Set the Farsi keymap mode */
537 }
538#endif
539
540#ifdef FEAT_GUI
541 if (gui.starting)
542 {
543#if defined(UNIX) || defined(VMS)
544 /* When something caused a message from a vimrc script, need to output
545 * an extra newline before the shell prompt. */
546 if (did_emsg || msg_didout)
547 putchar('\n');
548#endif
549
550 gui_start(); /* will set full_screen to TRUE */
551 TIME_MSG("starting GUI");
552
553 /* When running "evim" or "gvim -y" we need the menus, exit if we
554 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000555 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556 mch_exit(1);
557 }
558#endif
559
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000560#ifdef FEAT_VIMINFO
561 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000562 * Read in registers, history etc, but not marks, from the viminfo file.
563 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564 */
565 if (*p_viminfo != NUL)
566 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000567 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000568 TIME_MSG("reading viminfo");
569 }
570#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100571#ifdef FEAT_EVAL
572 /* It's better to make v:oldfiles an empty list than NULL. */
573 if (get_vim_var_list(VV_OLDFILES) == NULL)
574 set_vim_var_list(VV_OLDFILES, list_alloc());
575#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000576
577#ifdef FEAT_QUICKFIX
578 /*
579 * "-q errorfile": Load the error file now.
580 * If the error file can't be read, exit before doing anything else.
581 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000582 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000583 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100584 char_u *enc = NULL;
585
586# ifdef FEAT_MBYTE
587 enc = p_menc;
588# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000589 if (params.use_ef != NULL)
590 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000591 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200592 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100593 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000594 {
595 out_char('\n');
596 mch_exit(3);
597 }
598 TIME_MSG("reading errorfile");
599 }
600#endif
601
602 /*
603 * Start putting things on the screen.
604 * Scroll screen down before drawing over it
605 * Clear screen now, so file message will not be cleared.
606 */
607 starting = NO_BUFFERS;
608 no_wait_return = FALSE;
609 if (!exmode_active)
610 msg_scroll = FALSE;
611
612#ifdef FEAT_GUI
613 /*
614 * This seems to be required to make callbacks to be called now, instead
615 * of after things have been put on the screen, which then may be deleted
616 * when getting a resize callback.
617 * For the Mac this handles putting files dropped on the Vim icon to
618 * global_alist.
619 */
620 if (gui.in_use)
621 {
622# ifdef FEAT_SUN_WORKSHOP
623 if (!usingSunWorkShop)
624# endif
625 gui_wait_for_chars(50L);
626 TIME_MSG("GUI delay");
627 }
628#endif
629
630#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
631 qnx_clip_init();
632#endif
633
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200634#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
635 clip_init(TRUE);
636#endif
637
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000638#ifdef FEAT_XCLIPBOARD
639 /* Start using the X clipboard, unless the GUI was started. */
640# ifdef FEAT_GUI
641 if (!gui.in_use)
642# endif
643 {
644 setup_term_clip();
645 TIME_MSG("setup clipboard");
646 }
647#endif
648
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000649#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000650 /* Prepare for being a Vim server. */
651 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000652#endif
653
654 /*
655 * If "-" argument given: Read file from stdin.
656 * Do this before starting Raw mode, because it may change things that the
657 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
658 * are the same terminal: "cat | vim -".
659 * Using autocommands here may cause trouble...
660 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000661 if (params.edit_type == EDIT_STDIN && !recoverymode)
662 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000663
664#if defined(UNIX) || defined(VMS)
665 /* When switching screens and something caused a message from a vimrc
666 * script, need to output an extra newline on exit. */
667 if ((did_emsg || msg_didout) && *T_TI != NUL)
668 newline_on_exit = TRUE;
669#endif
670
671 /*
672 * When done something that is not allowed or error message call
673 * wait_return. This must be done before starttermcap(), because it may
674 * switch to another screen. It must be done after settmode(TMODE_RAW),
675 * because we want to react on a single key stroke.
676 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000677 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000678 */
679 settmode(TMODE_RAW);
680 TIME_MSG("setting raw mode");
681
682 if (need_wait_return || msg_didany)
683 {
684 wait_return(TRUE);
685 TIME_MSG("waiting for return");
686 }
687
688 starttermcap(); /* start termcap if not done by wait_return() */
689 TIME_MSG("start termcap");
690
691#ifdef FEAT_MOUSE
692 setmouse(); /* may start using the mouse */
693#endif
694 if (scroll_region)
695 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000696 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000697
698 /*
699 * Don't clear the screen when starting in Ex mode, unless using the GUI.
700 */
701 if (exmode_active
702#ifdef FEAT_GUI
703 && !gui.in_use
704#endif
705 )
706 must_redraw = CLEAR;
707 else
708 {
709 screenclear(); /* clear screen */
710 TIME_MSG("clearing screen");
711 }
712
713#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000714 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100716 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200717 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 TIME_MSG("getting crypt key");
719 }
720#endif
721
722 no_wait_return = TRUE;
723
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000725 * Create the requested number of windows and edit buffers in them.
726 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000728 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729 TIME_MSG("opening buffers");
730
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000731#ifdef FEAT_EVAL
732 /* clear v:swapcommand */
733 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
734#endif
735
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 /* Ex starts at last line of the file */
737 if (exmode_active)
738 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
739
740#ifdef FEAT_AUTOCMD
741 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
742 TIME_MSG("BufEnter autocommands");
743#endif
744 setpcmark();
745
746#ifdef FEAT_QUICKFIX
747 /*
748 * When started with "-q errorfile" jump to first error now.
749 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000750 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000751 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000752 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753 TIME_MSG("jump to first error");
754 }
755#endif
756
757#ifdef FEAT_WINDOWS
758 /*
759 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000760 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000761 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200762 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000763#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200764 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000765
766#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000767 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 {
769 win_T *wp;
770
771 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200772 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000773 diff_win_options(wp, TRUE);
774 }
775#endif
776
777 /*
778 * Shorten any of the filenames, but only when absolute.
779 */
780 shorten_fnames(FALSE);
781
782 /*
783 * Need to jump to the tag before executing the '-c command'.
784 * Makes "vim -c '/return' -t main" work.
785 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000786 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000787 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000788#if defined(HAS_SWAP_EXISTS_ACTION)
789 swap_exists_did_quit = FALSE;
790#endif
791
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000792 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793 do_cmdline_cmd(IObuff);
794 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000795
796#if defined(HAS_SWAP_EXISTS_ACTION)
797 /* If the user doesn't want to edit the file then we quit here. */
798 if (swap_exists_did_quit)
799 getout(1);
800#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801 }
802
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000803 /* Execute any "+", "-c" and "-S" arguments. */
804 if (params.n_commands > 0)
805 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200807 /* Must come before the may_req_ calls. */
808 starting = 0;
809
Bram Moolenaar976787d2017-06-04 15:45:50 +0200810#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
811 /* Must be done before redrawing, puts a few characters on the screen. */
812 may_req_ambiguous_char_width();
813#endif
814
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000815 RedrawingDisabled = 0;
816 redraw_all_later(NOT_VALID);
817 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000818
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200819 /* 'autochdir' has been postponed */
820 DO_AUTOCHDIR
821
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000822#ifdef FEAT_TERMRESPONSE
823 /* Requesting the termresponse is postponed until here, so that a "-c q"
824 * argument doesn't make it appear in the shell Vim was started from. */
825 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200826
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200827 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000828#endif
829
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000830 /* start in insert mode */
831 if (p_im)
832 need_start_insertmode = TRUE;
833
Bram Moolenaar14735512016-03-26 21:00:08 +0100834#ifdef FEAT_EVAL
835 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
836#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000837#ifdef FEAT_AUTOCMD
838 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
839 TIME_MSG("VimEnter autocommands");
840#endif
841
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200842#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
843 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
844 * done after the clipboard is available and all initial commands that may
845 * modify the 'clipboard' setting have run; i.e. just before entering the
846 * main loop. */
847 {
848 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100849
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200850 adjust_clip_reg(&default_regname);
851 set_reg_var(default_regname);
852 }
853#endif
854
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000855#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
856 /* When a startup script or session file setup for diff'ing and
857 * scrollbind, sync the scrollbind now. */
858 if (curwin->w_p_diff && curwin->w_p_scb)
859 {
860 update_topline();
861 check_scrollbind((linenr_T)0, 0L);
862 TIME_MSG("diff scrollbinding");
863 }
864#endif
865
866#if defined(WIN3264) && !defined(FEAT_GUI_W32)
867 mch_set_winsize_now(); /* Allow winsize changes from now on */
868#endif
869
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000870#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
871 /* When tab pages were created, may need to update the tab pages line and
872 * scrollbars. This is skipped while creating them. */
873 if (first_tabpage->tp_next != NULL)
874 {
875 out_flush();
876 gui_init_which_components(NULL);
877 gui_update_scrollbars(TRUE);
878 }
879 need_mouse_correct = TRUE;
880#endif
881
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882 /* If ":startinsert" command used, stuff a dummy command to be able to
883 * call normal_cmd(), which will then start Insert mode. */
884 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000885 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886
887#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200888 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200889 {
890# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200891# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200892 && !defined(FEAT_GUI_W32)
893 if (gui.in_use)
894 {
895 mch_errmsg(_("netbeans is not supported with this GUI\n"));
896 mch_exit(2);
897 }
898# endif
899# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000900 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200901 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200902 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000903#endif
904
905 TIME_MSG("before starting main loop");
906
907 /*
908 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100909 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000911
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200912#endif /* NO_VIM_MAIN */
913
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000914 return 0;
915}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000916
917/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200918 * Initialisation shared by main() and some tests.
919 */
920 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200921common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200922{
923
924#ifdef FEAT_MBYTE
925 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
926#endif
927#ifdef FEAT_EVAL
928 eval_init(); /* init global variables */
929#endif
930
931#ifdef __QNXNTO__
932 qnx_init(); /* PhAttach() for clipboard, (and gui) */
933#endif
934
935#ifdef MAC_OS_CLASSIC
936 /* Prepare for possibly starting GUI sometime */
937 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200938 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200939 TIME_MSG("GUI prepared");
940#endif
941
942 /* Init the table of Normal mode commands. */
943 init_normal_cmds();
944
945#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
946 make_version(); /* Construct the long version string. */
947#endif
948
949 /*
950 * Allocate space for the generic buffers (needed for set_init_1() and
951 * EMSG2()).
952 */
953 if ((IObuff = alloc(IOSIZE)) == NULL
954 || (NameBuff = alloc(MAXPATHL)) == NULL)
955 mch_exit(0);
956 TIME_MSG("Allocated generic buffers");
957
958#ifdef NBDEBUG
959 /* Wait a moment for debugging NetBeans. Must be after allocating
960 * NameBuff. */
961 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
962 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
963 TIME_MSG("NetBeans debug wait");
964#endif
965
966#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
967 /*
968 * Setup to use the current locale (for ctype() and many other things).
969 * NOTE: Translated messages with encodings other than latin1 will not
970 * work until set_init_1() has been called!
971 */
972 init_locale();
973 TIME_MSG("locale set");
974#endif
975
976#ifdef FEAT_GUI
977 gui.dofork = TRUE; /* default is to use fork() */
978#endif
979
980 /*
981 * Do a first scan of the arguments in "argv[]":
982 * -display or --display
983 * --server...
984 * --socketid
985 * --windowid
986 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200987 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200988
989#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200990 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200991#endif
992#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
993 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200994 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200995 TIME_MSG("GUI prepared");
996#endif
997
998#ifdef FEAT_CLIPBOARD
999 clip_init(FALSE); /* Initialise clipboard stuff */
1000 TIME_MSG("clipboard setup");
1001#endif
1002
1003 /*
1004 * Check if we have an interactive window.
1005 * On the Amiga: If there is no window, we open one with a newcli command
1006 * (needed for :! to * work). mch_check_win() will also handle the -d or
1007 * -dev argument.
1008 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001009 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001010 TIME_MSG("window checked");
1011
1012 /*
1013 * Allocate the first window and buffer.
1014 * Can't do anything without it, exit when it fails.
1015 */
1016 if (win_alloc_first() == FAIL)
1017 mch_exit(0);
1018
1019 init_yank(); /* init yank buffers */
1020
1021 alist_init(&global_alist); /* Init the argument list to empty. */
1022 global_alist.id = 0;
1023
1024 /*
1025 * Set the default values for the options.
1026 * NOTE: Non-latin1 translated messages are working only after this,
1027 * because this is where "has_mbyte" will be set, which is used by
1028 * msg_outtrans_len_attr().
1029 * First find out the home directory, needed to expand "~" in options.
1030 */
1031 init_homedir(); /* find real value of $HOME */
1032 set_init_1();
1033 TIME_MSG("inits 1");
1034
1035#ifdef FEAT_EVAL
1036 set_lang_var(); /* set v:lang and v:ctype */
1037#endif
1038}
1039
1040/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001041 * Return TRUE when the --not-a-term argument was found.
1042 */
1043 int
1044is_not_a_term()
1045{
1046 return params.not_a_term;
1047}
1048
1049/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001050 * Main loop: Execute Normal mode commands until exiting Vim.
1051 * Also used to handle commands in the command-line window, until the window
1052 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 * Also used to handle ":visual" command after ":global": execute Normal mode
1054 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001055 */
1056 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001057main_loop(
1058 int cmdwin, /* TRUE when working in the command-line window */
1059 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001060{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001061 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001062 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001063#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001064 /* these are static to avoid a compiler warning */
1065 static linenr_T conceal_old_cursor_line = 0;
1066 static linenr_T conceal_new_cursor_line = 0;
1067 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001068#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001069
1070#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1071 /* Setup to catch a terminating error from the X server. Just ignore
1072 * it, restore the state and continue. This might not always work
1073 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001074 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001076 {
1077 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001078 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001079 got_int = TRUE;
1080 need_wait_return = FALSE;
1081 global_busy = FALSE;
1082 exmode_active = 0;
1083 skip_redraw = FALSE;
1084 RedrawingDisabled = 0;
1085 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001086 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001087# ifdef FEAT_EVAL
1088 emsg_skip = 0;
1089# endif
1090 emsg_off = 0;
1091# ifdef FEAT_MOUSE
1092 setmouse();
1093# endif
1094 settmode(TMODE_RAW);
1095 starttermcap();
1096 scroll_start();
1097 redraw_later_clear();
1098 }
1099#endif
1100
1101 clear_oparg(&oa);
1102 while (!cmdwin
1103#ifdef FEAT_CMDWIN
1104 || cmdwin_result == 0
1105#endif
1106 )
1107 {
1108 if (stuff_empty())
1109 {
1110 did_check_timestamps = FALSE;
1111 if (need_check_timestamps)
1112 check_timestamps(FALSE);
1113 if (need_wait_return) /* if wait_return still needed ... */
1114 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001115 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001116 {
1117 need_start_insertmode = FALSE;
1118 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1119 /* skip the fileinfo message now, because it would be shown
1120 * after insert mode finishes! */
1121 need_fileinfo = FALSE;
1122 }
1123 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001124
1125 /* Reset "got_int" now that we got back to the main loop. Except when
1126 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1127 * the ":g" command.
1128 * For ":g/pat/vi" we reset "got_int" when used once. When used
1129 * a second time we go back to Ex mode and abort the ":g" command. */
1130 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001131 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001132 if (noexmode && global_busy && !exmode_active && previous_got_int)
1133 {
1134 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1135 * used and keep "got_int" set, so that it aborts ":g". */
1136 exmode_active = EXMODE_NORMAL;
1137 State = NORMAL;
1138 }
1139 else if (!global_busy || !exmode_active)
1140 {
1141 if (!quit_more)
1142 (void)vgetc(); /* flush all buffers */
1143 got_int = FALSE;
1144 }
1145 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001146 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001147 else
1148 previous_got_int = FALSE;
1149
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001150 if (!exmode_active)
1151 msg_scroll = FALSE;
1152 quit_more = FALSE;
1153
1154 /*
1155 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1156 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1157 * update cursor and redraw.
1158 */
1159 if (skip_redraw || exmode_active)
1160 skip_redraw = FALSE;
1161 else if (do_redraw || stuff_empty())
1162 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001163# ifdef FEAT_GUI
1164 /* If ui_breakcheck() was used a resize may have been postponed. */
1165 gui_may_resize_shell();
1166# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001168 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001169 if (!finish_op && (
1170# ifdef FEAT_AUTOCMD
1171 has_cursormoved()
1172# endif
1173# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1174 ||
1175# endif
1176# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001177 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001178# endif
1179 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001180# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001181 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001182# endif
1183 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001184 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001185# ifdef FEAT_AUTOCMD
1186 if (has_cursormoved())
1187 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1188 FALSE, curbuf);
1189# endif
1190# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001191 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001192 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001193# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001194 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001195# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001196 conceal_new_cursor_line = curwin->w_cursor.lnum;
1197 conceal_update_lines = TRUE;
1198 }
1199# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001200# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001201 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001202# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001203 }
1204#endif
1205
Bram Moolenaar186628f2013-03-19 13:33:23 +01001206#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001207 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001208 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001209 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001210 {
1211 if (last_changedtick_buf == curbuf)
1212 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1213 FALSE, curbuf);
1214 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001215 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001216 }
1217#endif
1218
Bram Moolenaar33aec762006-01-22 23:30:12 +00001219#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1220 /* Scroll-binding for diff mode may have been postponed until
1221 * here. Avoids doing it for every change. */
1222 if (diff_need_scrollbind)
1223 {
1224 check_scrollbind((linenr_T)0, 0L);
1225 diff_need_scrollbind = FALSE;
1226 }
1227#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001228#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 /* Include a closed fold completely in the Visual area. */
1230 foldAdjustVisual();
1231#endif
1232#ifdef FEAT_FOLDING
1233 /*
1234 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1235 * contain the cursor.
1236 * When 'foldopen' is "all", open the fold(s) under the cursor.
1237 * This may mark the window for redrawing.
1238 */
1239 if (hasAnyFolding(curwin) && !char_avail())
1240 {
1241 foldCheckClose();
1242 if (fdo_flags & FDO_ALL)
1243 foldOpenCursor();
1244 }
1245#endif
1246
1247 /*
1248 * Before redrawing, make sure w_topline is correct, and w_leftcol
1249 * if lines don't wrap, and w_skipcol if lines wrap.
1250 */
1251 update_topline();
1252 validate_cursor();
1253
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001254 if (VIsual_active)
1255 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001256 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001257 update_screen(0);
1258 else if (redraw_cmdline || clear_cmdline)
1259 showmode();
1260#ifdef FEAT_WINDOWS
1261 redraw_statuslines();
1262#endif
1263#ifdef FEAT_TITLE
1264 if (need_maketitle)
1265 maketitle();
1266#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001267#ifdef FEAT_VIMINFO
1268 curbuf->b_last_used = vim_time();
1269#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001270 /* display message after redraw */
1271 if (keep_msg != NULL)
1272 {
1273 char_u *p;
1274
1275 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001276 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1277 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001278 p = keep_msg;
1279 msg_attr(p, keep_msg_attr);
1280 vim_free(p);
1281 }
1282 if (need_fileinfo) /* show file info after redraw */
1283 {
1284 fileinfo(FALSE, TRUE, FALSE);
1285 need_fileinfo = FALSE;
1286 }
1287
1288 emsg_on_display = FALSE; /* can delete error message now */
1289 did_emsg = FALSE;
1290 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001291 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001292 showruler(FALSE);
1293
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001294# if defined(FEAT_CONCEAL)
1295 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001296 && (conceal_old_cursor_line != conceal_new_cursor_line
1297 || conceal_cursor_line(curwin)
1298 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001299 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001300 if (conceal_old_cursor_line != conceal_new_cursor_line
1301 && conceal_old_cursor_line
1302 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001303 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001304 update_single_line(curwin, conceal_new_cursor_line);
1305 curwin->w_valid &= ~VALID_CROW;
1306 }
1307# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001308 setcursor();
1309 cursor_on();
1310
1311 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001312
1313#ifdef STARTUPTIME
1314 /* Now that we have drawn the first screen all the startup stuff
1315 * has been done, close any file for startup messages. */
1316 if (time_fd != NULL)
1317 {
1318 TIME_MSG("first screen update");
1319 TIME_MSG("--- VIM STARTED ---");
1320 fclose(time_fd);
1321 time_fd = NULL;
1322 }
1323#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001324 }
1325#ifdef FEAT_GUI
1326 if (need_mouse_correct)
1327 gui_mouse_correct();
1328#endif
1329
1330 /*
1331 * Update w_curswant if w_set_curswant has been set.
1332 * Postponed until here to avoid computing w_virtcol too often.
1333 */
1334 update_curswant();
1335
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001336#ifdef FEAT_EVAL
1337 /*
1338 * May perform garbage collection when waiting for a character, but
1339 * only at the very toplevel. Otherwise we may be using a List or
1340 * Dict internally somewhere.
1341 * "may_garbage_collect" is reset in vgetc() which is invoked through
1342 * do_exmode() and normal_cmd().
1343 */
1344 may_garbage_collect = (!cmdwin && !noexmode);
1345#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001346 /*
1347 * If we're invoked as ex, do a round of ex commands.
1348 * Otherwise, get and execute a normal mode command.
1349 */
1350 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001351 {
1352 if (noexmode) /* End of ":global/path/visual" commands */
1353 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001354 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001355 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001356 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001357 {
1358#ifdef FEAT_TERMINAL
1359 if (curbuf->b_term != NULL && oa.op_type == OP_NOP
1360 && oa.regname == NUL)
1361 terminal_loop();
1362#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001363 normal_cmd(&oa, TRUE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001364 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365 }
1366}
1367
1368
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001369#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370/*
1371 * Exit, but leave behind swap files for modified buffers.
1372 */
1373 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001374getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001375{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001376# if defined(SIGHUP) && defined(SIG_IGN)
1377 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1378 * makes Vim exit and then handling SIGHUP causes various reentrance
1379 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001380 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001381# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001382
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383 ml_close_notmod(); /* close all not-modified buffers */
1384 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1385 ml_close_all(FALSE); /* close all memfiles, without deleting */
1386 getout(exitval); /* exit Vim properly */
1387}
1388#endif
1389
1390
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001391/*
1392 * Exit properly.
1393 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001394 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001395getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396{
1397#ifdef FEAT_AUTOCMD
1398 buf_T *buf;
1399 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001400 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401#endif
1402
1403 exiting = TRUE;
1404
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001405 /* When running in Ex mode an error causes us to exit with a non-zero exit
1406 * code. POSIX requires this, although it's not 100% clear from the
1407 * standard. */
1408 if (exmode_active)
1409 exitval += ex_exitval;
1410
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001411 /* Position the cursor on the last screen line, below all the text */
1412#ifdef FEAT_GUI
1413 if (!gui.in_use)
1414#endif
1415 windgoto((int)Rows - 1, 0);
1416
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001417#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1418 /* Optionally print hashtable efficiency. */
1419 hash_debug_results();
1420#endif
1421
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001422#ifdef FEAT_GUI
1423 msg_didany = FALSE;
1424#endif
1425
1426#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001427 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001428 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001429 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1430# if defined FEAT_WINDOWS
1431 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001432 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001434 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001436 if (wp->w_buffer == NULL)
1437 /* Autocmd must have close the buffer already, skip. */
1438 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001440 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001441 {
1442 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1443 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001444 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001445 /* start all over, autocommands may mess up the lists */
1446 next_tp = first_tabpage;
1447 break;
1448 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001449 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001451# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001452 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1453 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001454# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001455
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001456 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001457 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001458 if (buf->b_ml.ml_mfp != NULL)
1459 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001460 bufref_T bufref;
1461
1462 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001463 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001464 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001465 if (!bufref_valid(&bufref))
1466 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001467 break;
1468 }
1469 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1470 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001471#endif
1472
1473#ifdef FEAT_VIMINFO
1474 if (*p_viminfo != NUL)
1475 /* Write out the registers, history, marks etc, to the viminfo file */
1476 write_viminfo(NULL, FALSE);
1477#endif
1478
1479#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001480 if (get_vim_var_nr(VV_DYING) <= 1)
1481 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001482#endif
1483
Bram Moolenaar05159a02005-02-26 23:04:13 +00001484#ifdef FEAT_PROFILE
1485 profile_dump();
1486#endif
1487
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001488 if (did_emsg
1489#ifdef FEAT_GUI
1490 || (gui.in_use && msg_didany && p_verbose > 0)
1491#endif
1492 )
1493 {
1494 /* give the user a chance to read the (error) message */
1495 no_wait_return = FALSE;
1496 wait_return(FALSE);
1497 }
1498
1499#ifdef FEAT_AUTOCMD
1500 /* Position the cursor again, the autocommands may have moved it */
1501# ifdef FEAT_GUI
1502 if (!gui.in_use)
1503# endif
1504 windgoto((int)Rows - 1, 0);
1505#endif
1506
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001507#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001508 job_stop_on_exit();
1509#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001510#ifdef FEAT_LUA
1511 lua_end();
1512#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001513#ifdef FEAT_MZSCHEME
1514 mzscheme_end();
1515#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001516#ifdef FEAT_TCL
1517 tcl_end();
1518#endif
1519#ifdef FEAT_RUBY
1520 ruby_end();
1521#endif
1522#ifdef FEAT_PYTHON
1523 python_end();
1524#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001525#ifdef FEAT_PYTHON3
1526 python3_end();
1527#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001528#ifdef FEAT_PERL
1529 perl_end();
1530#endif
1531#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1532 iconv_end();
1533#endif
1534#ifdef FEAT_NETBEANS_INTG
1535 netbeans_end();
1536#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001537#ifdef FEAT_CSCOPE
1538 cs_end();
1539#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001540#ifdef FEAT_EVAL
1541 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001542 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001543#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001544#if defined(WIN32) && defined(FEAT_MBYTE)
1545 free_cmd_argsW();
1546#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001547
1548 mch_exit(exitval);
1549}
1550
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001551#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1552/*
1553 * Setup to use the current locale (for ctype() and many other things).
1554 */
1555 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001556init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001557{
1558 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001560# ifdef FEAT_GUI_GTK
1561 /* Tell Gtk not to change our locale settings. */
1562 gtk_disable_setlocale();
1563# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001564# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1565 /* Make sure strtod() uses a decimal point, not a comma. */
1566 setlocale(LC_NUMERIC, "C");
1567# endif
1568
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001569# ifdef WIN32
1570 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1571 * text while it's expecting text in the current locale. This call avoids
1572 * that. */
1573 setlocale(LC_CTYPE, "C");
1574# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001575
1576# ifdef FEAT_GETTEXT
1577 {
1578 int mustfree = FALSE;
1579 char_u *p;
1580
1581# ifdef DYNAMIC_GETTEXT
1582 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001583 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001584# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001585 /* expand_env() doesn't work yet, because g_chartab[] is not
1586 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001587 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1588 if (p != NULL && *p != NUL)
1589 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001590 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001591 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1592 }
1593 if (mustfree)
1594 vim_free(p);
1595 textdomain(VIMPACKAGE);
1596 }
1597# endif
1598}
1599#endif
1600
1601/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602 * Get the name of the display, before gui_prepare() removes it from
1603 * argv[]. Used for the xterm-clipboard display.
1604 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001605 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001606 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001607 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001608early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001609{
Bram Moolenaar03005972008-11-20 13:12:36 +00001610#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1611 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001612 int argc = parmp->argc;
1613 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001614 int i;
1615
1616 for (i = 1; i < argc; i++)
1617 {
1618 if (STRCMP(argv[i], "--") == 0)
1619 break;
1620# ifdef FEAT_XCLIPBOARD
1621 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001622# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001623 || STRICMP(argv[i], "--display") == 0
1624# endif
1625 )
1626 {
1627 if (i == argc - 1)
1628 mainerr_arg_missing((char_u *)argv[i]);
1629 xterm_display = argv[++i];
1630 }
1631# endif
1632# ifdef FEAT_CLIENTSERVER
1633 else if (STRICMP(argv[i], "--servername") == 0)
1634 {
1635 if (i == argc - 1)
1636 mainerr_arg_missing((char_u *)argv[i]);
1637 parmp->serverName_arg = (char_u *)argv[++i];
1638 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001639 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001640 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001641 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001642 {
1643 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001644# ifdef FEAT_GUI
1645 if (strstr(argv[i], "-wait") != 0)
1646 /* don't fork() when starting the GUI to edit files ourself */
1647 gui.dofork = FALSE;
1648# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001649 }
1650# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001651
1652# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1653# ifdef FEAT_GUI_W32
1654 else if (STRICMP(argv[i], "--windowid") == 0)
1655# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001656 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001657# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001658 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001659 long_u id;
1660 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001661
1662 if (i == argc - 1)
1663 mainerr_arg_missing((char_u *)argv[i]);
1664 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001665 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001666 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001667 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001668 if (count != 1)
1669 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1670 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001671# ifdef FEAT_GUI_W32
1672 win_socket_id = id;
1673# else
1674 gtk_socket_id = id;
1675# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001676 i++;
1677 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001678# endif
1679# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001680 else if (STRICMP(argv[i], "--echo-wid") == 0)
1681 echo_wid_arg = TRUE;
1682# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001683# ifndef FEAT_NETBEANS_INTG
1684 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001685 {
1686 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1687 mch_exit(2);
1688 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001689# endif
1690
Bram Moolenaar58d98232005-07-23 22:25:46 +00001691 }
1692#endif
1693}
1694
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001695#ifndef NO_VIM_MAIN
1696/*
1697 * Get a (optional) count for a Vim argument.
1698 */
1699 static int
1700get_number_arg(
1701 char_u *p, /* pointer to argument */
1702 int *idx, /* index in argument, is incremented */
1703 int def) /* default value */
1704{
1705 if (vim_isdigit(p[*idx]))
1706 {
1707 def = atoi((char *)&(p[*idx]));
1708 while (vim_isdigit(p[*idx]))
1709 *idx = *idx + 1;
1710 }
1711 return def;
1712}
1713
1714/*
1715 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1716 * If the executable name starts with "r" we disable shell commands.
1717 * If the next character is "e" we run in Easy mode.
1718 * If the next character is "g" we run the GUI version.
1719 * If the next characters are "view" we start in readonly mode.
1720 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1721 * If the next characters are "ex" we start in Ex mode. If it's followed
1722 * by "im" use improved Ex mode.
1723 */
1724 static void
1725parse_command_name(mparm_T *parmp)
1726{
1727 char_u *initstr;
1728
1729 initstr = gettail((char_u *)parmp->argv[0]);
1730
1731#ifdef MACOS_X_UNIX
1732 /* An issue has been seen when launching Vim in such a way that
1733 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1734 * executable or a symbolic link of it. Until this issue is resolved
1735 * we prohibit the GUI from being used.
1736 */
1737 if (STRCMP(initstr, parmp->argv[0]) == 0)
1738 disallow_gui = TRUE;
1739
1740 /* TODO: On MacOS X default to gui if argv[0] ends in:
1741 * /Vim.app/Contents/MacOS/Vim */
1742#endif
1743
1744#ifdef FEAT_EVAL
1745 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001746 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001747#endif
1748
1749 if (TOLOWER_ASC(initstr[0]) == 'r')
1750 {
1751 restricted = TRUE;
1752 ++initstr;
1753 }
1754
1755 /* Use evim mode for "evim" and "egvim", not for "editor". */
1756 if (TOLOWER_ASC(initstr[0]) == 'e'
1757 && (TOLOWER_ASC(initstr[1]) == 'v'
1758 || TOLOWER_ASC(initstr[1]) == 'g'))
1759 {
1760#ifdef FEAT_GUI
1761 gui.starting = TRUE;
1762#endif
1763 parmp->evim_mode = TRUE;
1764 ++initstr;
1765 }
1766
1767 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1768 if (TOLOWER_ASC(initstr[0]) == 'g')
1769 {
1770 main_start_gui();
1771#ifdef FEAT_GUI
1772 ++initstr;
1773#endif
1774 }
1775
1776 if (STRNICMP(initstr, "view", 4) == 0)
1777 {
1778 readonlymode = TRUE;
1779 curbuf->b_p_ro = TRUE;
1780 p_uc = 10000; /* don't update very often */
1781 initstr += 4;
1782 }
1783 else if (STRNICMP(initstr, "vim", 3) == 0)
1784 initstr += 3;
1785
1786 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1787 if (STRICMP(initstr, "diff") == 0)
1788 {
1789#ifdef FEAT_DIFF
1790 parmp->diff_mode = TRUE;
1791#else
1792 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1793 mch_errmsg("\n");
1794 mch_exit(2);
1795#endif
1796 }
1797
1798 if (STRNICMP(initstr, "ex", 2) == 0)
1799 {
1800 if (STRNICMP(initstr + 2, "im", 2) == 0)
1801 exmode_active = EXMODE_VIM;
1802 else
1803 exmode_active = EXMODE_NORMAL;
1804 change_compatible(TRUE); /* set 'compatible' */
1805 }
1806}
1807
Bram Moolenaar58d98232005-07-23 22:25:46 +00001808/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001809 * Scan the command line arguments.
1810 */
1811 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001812command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001813{
1814 int argc = parmp->argc;
1815 char **argv = parmp->argv;
1816 int argv_idx; /* index in argv[n][] */
1817 int had_minmin = FALSE; /* found "--" argument */
1818 int want_argument; /* option argument with argument */
1819 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001820 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001821 long n;
1822
1823 --argc;
1824 ++argv;
1825 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1826 while (argc > 0)
1827 {
1828 /*
1829 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1830 */
1831 if (argv[0][0] == '+' && !had_minmin)
1832 {
1833 if (parmp->n_commands >= MAX_ARG_CMDS)
1834 mainerr(ME_EXTRA_CMD, NULL);
1835 argv_idx = -1; /* skip to next argument */
1836 if (argv[0][1] == NUL)
1837 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1838 else
1839 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1840 }
1841
1842 /*
1843 * Optional argument.
1844 */
1845 else if (argv[0][0] == '-' && !had_minmin)
1846 {
1847 want_argument = FALSE;
1848 c = argv[0][argv_idx++];
1849#ifdef VMS
1850 /*
1851 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1852 * and "-/X" as "-X".
1853 */
1854 if (c == '/')
1855 {
1856 c = argv[0][argv_idx++];
1857 c = TOUPPER_ASC(c);
1858 }
1859 else
1860 c = TOLOWER_ASC(c);
1861#endif
1862 switch (c)
1863 {
1864 case NUL: /* "vim -" read from stdin */
1865 /* "ex -" silent mode */
1866 if (exmode_active)
1867 silent_mode = TRUE;
1868 else
1869 {
1870 if (parmp->edit_type != EDIT_NONE)
1871 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1872 parmp->edit_type = EDIT_STDIN;
1873 read_cmd_fd = 2; /* read from stderr instead of stdin */
1874 }
1875 argv_idx = -1; /* skip to next argument */
1876 break;
1877
1878 case '-': /* "--" don't take any more option arguments */
1879 /* "--help" give help message */
1880 /* "--version" give version message */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001881 /* "--clean" clean context */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001882 /* "--literal" take files literally */
1883 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001884 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001885 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001886 /* "--noplugin[s]" skip plugins */
1887 /* "--cmd <cmd>" execute cmd before vimrc */
1888 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1889 usage();
1890 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1891 {
1892 Columns = 80; /* need to init Columns */
1893 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1894 list_version();
1895 msg_putchar('\n');
1896 msg_didout = FALSE;
1897 mch_exit(0);
1898 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001899 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
1900 {
1901 parmp->use_vimrc = (char_u *)"DEFAULTS";
1902 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
1903 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001904 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1905 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001906#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001907 parmp->literal = TRUE;
1908#endif
1909 }
1910 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1911 {
1912#ifdef FEAT_GUI
1913 gui.dofork = FALSE; /* don't fork() when starting GUI */
1914#endif
1915 }
1916 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1917 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001918 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1919 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001920 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1921 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001922 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1923 {
1924 want_argument = TRUE;
1925 argv_idx += 3;
1926 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001927 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1928 {
1929 want_argument = TRUE;
1930 argv_idx += 11;
1931 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001932#ifdef FEAT_CLIENTSERVER
1933 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1934 ; /* already processed -- no arg */
1935 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1936 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1937 {
1938 /* already processed -- snatch the following arg */
1939 if (argc > 1)
1940 {
1941 --argc;
1942 ++argv;
1943 }
1944 }
1945#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001946#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1947# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001948 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001949# else
1950 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1951# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001952 {
1953 /* already processed -- snatch the following arg */
1954 if (argc > 1)
1955 {
1956 --argc;
1957 ++argv;
1958 }
1959 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001960#endif
1961#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001962 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1963 {
1964 /* already processed, skip */
1965 }
1966#endif
1967 else
1968 {
1969 if (argv[0][argv_idx])
1970 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1971 had_minmin = TRUE;
1972 }
1973 if (!want_argument)
1974 argv_idx = -1; /* skip to next argument */
1975 break;
1976
1977 case 'A': /* "-A" start in Arabic mode */
1978#ifdef FEAT_ARABIC
1979 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1980#else
1981 mch_errmsg(_(e_noarabic));
1982 mch_exit(2);
1983#endif
1984 break;
1985
1986 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001987 /* Needs to be effective before expanding file names, because
1988 * for Win32 this makes us edit a shortcut file itself,
1989 * instead of the file it links to. */
1990 set_options_bin(curbuf->b_p_bin, 1, 0);
1991 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001992 break;
1993
1994 case 'C': /* "-C" Compatible */
1995 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001996 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001997 break;
1998
1999 case 'e': /* "-e" Ex mode */
2000 exmode_active = EXMODE_NORMAL;
2001 break;
2002
2003 case 'E': /* "-E" Improved Ex mode */
2004 exmode_active = EXMODE_VIM;
2005 break;
2006
2007 case 'f': /* "-f" GUI: run in foreground. Amiga: open
2008 window directly, not with newcli */
2009#ifdef FEAT_GUI
2010 gui.dofork = FALSE; /* don't fork() when starting GUI */
2011#endif
2012 break;
2013
2014 case 'g': /* "-g" start GUI */
2015 main_start_gui();
2016 break;
2017
2018 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2019#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002020 p_fkmap = TRUE;
2021 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002022#else
2023 mch_errmsg(_(e_nofarsi));
2024 mch_exit(2);
2025#endif
2026 break;
2027
2028 case 'h': /* "-h" give help message */
2029#ifdef FEAT_GUI_GNOME
2030 /* Tell usage() to exit for "gvim". */
2031 gui.starting = FALSE;
2032#endif
2033 usage();
2034 break;
2035
2036 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2037#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002038 p_hkmap = TRUE;
2039 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002040#else
2041 mch_errmsg(_(e_nohebrew));
2042 mch_exit(2);
2043#endif
2044 break;
2045
2046 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2047#ifdef FEAT_LISP
2048 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2049 p_sm = TRUE;
2050#endif
2051 break;
2052
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002053 case 'M': /* "-M" no changes or writing of files */
2054 reset_modifiable();
2055 /* FALLTHROUGH */
2056
2057 case 'm': /* "-m" no writing of files */
2058 p_write = FALSE;
2059 break;
2060
2061 case 'y': /* "-y" easy mode */
2062#ifdef FEAT_GUI
2063 gui.starting = TRUE; /* start GUI a bit later */
2064#endif
2065 parmp->evim_mode = TRUE;
2066 break;
2067
2068 case 'N': /* "-N" Nocompatible */
2069 change_compatible(FALSE);
2070 break;
2071
2072 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002073#ifdef FEAT_NETBEANS_INTG
2074 /* checking for "-nb", netbeans parameters */
2075 if (argv[0][argv_idx] == 'b')
2076 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002077 netbeansArg = argv[0];
2078 argv_idx = -1; /* skip to next argument */
2079 }
2080 else
2081#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002082 parmp->no_swap_file = TRUE;
2083 break;
2084
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002085 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002086#ifdef TARGET_API_MAC_OSX
2087 /* For some reason on MacOS X, an argument like:
2088 -psn_0_10223617 is passed in when invoke from Finder
2089 or with the 'open' command */
2090 if (argv[0][argv_idx] == 's')
2091 {
2092 argv_idx = -1; /* bypass full -psn */
2093 main_start_gui();
2094 break;
2095 }
2096#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002097#ifdef FEAT_WINDOWS
2098 /* default is 0: open window for each file */
2099 parmp->window_count = get_number_arg((char_u *)argv[0],
2100 &argv_idx, 0);
2101 parmp->window_layout = WIN_TABS;
2102#endif
2103 break;
2104
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002105 case 'o': /* "-o[N]" open N horizontal split windows */
2106#ifdef FEAT_WINDOWS
2107 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002108 parmp->window_count = get_number_arg((char_u *)argv[0],
2109 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002110 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002111#endif
2112 break;
2113
2114 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002115#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002116 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002117 parmp->window_count = get_number_arg((char_u *)argv[0],
2118 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002119 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002120#endif
2121 break;
2122
2123#ifdef FEAT_QUICKFIX
2124 case 'q': /* "-q" QuickFix mode */
2125 if (parmp->edit_type != EDIT_NONE)
2126 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2127 parmp->edit_type = EDIT_QF;
2128 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2129 {
2130 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2131 argv_idx = -1;
2132 }
2133 else if (argc > 1) /* "-q {errorfile}" */
2134 want_argument = TRUE;
2135 break;
2136#endif
2137
2138 case 'R': /* "-R" readonly mode */
2139 readonlymode = TRUE;
2140 curbuf->b_p_ro = TRUE;
2141 p_uc = 10000; /* don't update very often */
2142 break;
2143
2144 case 'r': /* "-r" recovery mode */
2145 case 'L': /* "-L" recovery mode */
2146 recoverymode = 1;
2147 break;
2148
2149 case 's':
2150 if (exmode_active) /* "-s" silent (batch) mode */
2151 silent_mode = TRUE;
2152 else /* "-s {scriptin}" read from script file */
2153 want_argument = TRUE;
2154 break;
2155
2156 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2157 if (parmp->edit_type != EDIT_NONE)
2158 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2159 parmp->edit_type = EDIT_TAG;
2160 if (argv[0][argv_idx]) /* "-t{tag}" */
2161 {
2162 parmp->tagname = (char_u *)argv[0] + argv_idx;
2163 argv_idx = -1;
2164 }
2165 else /* "-t {tag}" */
2166 want_argument = TRUE;
2167 break;
2168
2169#ifdef FEAT_EVAL
2170 case 'D': /* "-D" Debugging */
2171 parmp->use_debug_break_level = 9999;
2172 break;
2173#endif
2174#ifdef FEAT_DIFF
2175 case 'd': /* "-d" 'diff' */
2176# ifdef AMIGA
2177 /* check for "-dev {device}" */
2178 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2179 want_argument = TRUE;
2180 else
2181# endif
2182 parmp->diff_mode = TRUE;
2183 break;
2184#endif
2185 case 'V': /* "-V{N}" Verbose level */
2186 /* default is 10: a little bit verbose */
2187 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2188 if (argv[0][argv_idx] != NUL)
2189 {
2190 set_option_value((char_u *)"verbosefile", 0L,
2191 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002192 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002193 }
2194 break;
2195
2196 case 'v': /* "-v" Vi-mode (as if called "vi") */
2197 exmode_active = 0;
2198#ifdef FEAT_GUI
2199 gui.starting = FALSE; /* don't start GUI */
2200#endif
2201 break;
2202
2203 case 'w': /* "-w{number}" set window height */
2204 /* "-w {scriptout}" write to script */
2205 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2206 {
2207 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2208 set_option_value((char_u *)"window", n, NULL, 0);
2209 break;
2210 }
2211 want_argument = TRUE;
2212 break;
2213
2214#ifdef FEAT_CRYPT
2215 case 'x': /* "-x" encrypted reading/writing of files */
2216 parmp->ask_for_key = TRUE;
2217 break;
2218#endif
2219
2220 case 'X': /* "-X" don't connect to X server */
2221#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2222 x_no_connect = TRUE;
2223#endif
2224 break;
2225
2226 case 'Z': /* "-Z" restricted mode */
2227 restricted = TRUE;
2228 break;
2229
2230 case 'c': /* "-c{command}" or "-c {command}" execute
2231 command */
2232 if (argv[0][argv_idx] != NUL)
2233 {
2234 if (parmp->n_commands >= MAX_ARG_CMDS)
2235 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002236 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2237 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002238 argv_idx = -1;
2239 break;
2240 }
2241 /*FALLTHROUGH*/
2242 case 'S': /* "-S {file}" execute Vim script */
2243 case 'i': /* "-i {viminfo}" use for viminfo */
2244#ifndef FEAT_DIFF
2245 case 'd': /* "-d {device}" device (for Amiga) */
2246#endif
2247 case 'T': /* "-T {terminal}" terminal name */
2248 case 'u': /* "-u {vimrc}" vim inits file */
2249 case 'U': /* "-U {gvimrc}" gvim inits file */
2250 case 'W': /* "-W {scriptout}" overwrite */
2251#ifdef FEAT_GUI_W32
2252 case 'P': /* "-P {parent title}" MDI parent */
2253#endif
2254 want_argument = TRUE;
2255 break;
2256
2257 default:
2258 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2259 }
2260
2261 /*
2262 * Handle option arguments with argument.
2263 */
2264 if (want_argument)
2265 {
2266 /*
2267 * Check for garbage immediately after the option letter.
2268 */
2269 if (argv[0][argv_idx] != NUL)
2270 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2271
2272 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002273 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002274 mainerr_arg_missing((char_u *)argv[0]);
2275 ++argv;
2276 argv_idx = -1;
2277
2278 switch (c)
2279 {
2280 case 'c': /* "-c {command}" execute command */
2281 case 'S': /* "-S {file}" execute Vim script */
2282 if (parmp->n_commands >= MAX_ARG_CMDS)
2283 mainerr(ME_EXTRA_CMD, NULL);
2284 if (c == 'S')
2285 {
2286 char *a;
2287
2288 if (argc < 1)
2289 /* "-S" without argument: use default session file
2290 * name. */
2291 a = SESSION_FILE;
2292 else if (argv[0][0] == '-')
2293 {
2294 /* "-S" followed by another option: use default
2295 * session file name. */
2296 a = SESSION_FILE;
2297 ++argc;
2298 --argv;
2299 }
2300 else
2301 a = argv[0];
2302 p = alloc((unsigned)(STRLEN(a) + 4));
2303 if (p == NULL)
2304 mch_exit(2);
2305 sprintf((char *)p, "so %s", a);
2306 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2307 parmp->commands[parmp->n_commands++] = p;
2308 }
2309 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002310 parmp->commands[parmp->n_commands++] =
2311 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002312 break;
2313
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002314 case '-':
2315 if (argv[-1][2] == 'c')
2316 {
2317 /* "--cmd {command}" execute command */
2318 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2319 mainerr(ME_EXTRA_CMD, NULL);
2320 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002321 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002322 }
2323 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002324 break;
2325
2326 /* case 'd': -d {device} is handled in mch_check_win() for the
2327 * Amiga */
2328
2329#ifdef FEAT_QUICKFIX
2330 case 'q': /* "-q {errorfile}" QuickFix mode */
2331 parmp->use_ef = (char_u *)argv[0];
2332 break;
2333#endif
2334
2335 case 'i': /* "-i {viminfo}" use for viminfo */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002336 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002337 break;
2338
2339 case 's': /* "-s {scriptin}" read from script file */
2340 if (scriptin[0] != NULL)
2341 {
2342scripterror:
2343 mch_errmsg(_("Attempt to open script file again: \""));
2344 mch_errmsg(argv[-1]);
2345 mch_errmsg(" ");
2346 mch_errmsg(argv[0]);
2347 mch_errmsg("\"\n");
2348 mch_exit(2);
2349 }
2350 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2351 {
2352 mch_errmsg(_("Cannot open for reading: \""));
2353 mch_errmsg(argv[0]);
2354 mch_errmsg("\"\n");
2355 mch_exit(2);
2356 }
2357 if (save_typebuf() == FAIL)
2358 mch_exit(2); /* out of memory */
2359 break;
2360
2361 case 't': /* "-t {tag}" */
2362 parmp->tagname = (char_u *)argv[0];
2363 break;
2364
2365 case 'T': /* "-T {terminal}" terminal name */
2366 /*
2367 * The -T term argument is always available and when
2368 * HAVE_TERMLIB is supported it overrides the environment
2369 * variable TERM.
2370 */
2371#ifdef FEAT_GUI
2372 if (term_is_gui((char_u *)argv[0]))
2373 gui.starting = TRUE; /* start GUI a bit later */
2374 else
2375#endif
2376 parmp->term = (char_u *)argv[0];
2377 break;
2378
2379 case 'u': /* "-u {vimrc}" vim inits file */
2380 parmp->use_vimrc = (char_u *)argv[0];
2381 break;
2382
2383 case 'U': /* "-U {gvimrc}" gvim inits file */
2384#ifdef FEAT_GUI
2385 use_gvimrc = (char_u *)argv[0];
2386#endif
2387 break;
2388
2389 case 'w': /* "-w {nr}" 'window' value */
2390 /* "-w {scriptout}" append to script file */
2391 if (vim_isdigit(*((char_u *)argv[0])))
2392 {
2393 argv_idx = 0;
2394 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2395 set_option_value((char_u *)"window", n, NULL, 0);
2396 argv_idx = -1;
2397 break;
2398 }
2399 /*FALLTHROUGH*/
2400 case 'W': /* "-W {scriptout}" overwrite script file */
2401 if (scriptout != NULL)
2402 goto scripterror;
2403 if ((scriptout = mch_fopen(argv[0],
2404 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2405 {
2406 mch_errmsg(_("Cannot open for script output: \""));
2407 mch_errmsg(argv[0]);
2408 mch_errmsg("\"\n");
2409 mch_exit(2);
2410 }
2411 break;
2412
2413#ifdef FEAT_GUI_W32
2414 case 'P': /* "-P {parent title}" MDI parent */
2415 gui_mch_set_parent(argv[0]);
2416 break;
2417#endif
2418 }
2419 }
2420 }
2421
2422 /*
2423 * File name argument.
2424 */
2425 else
2426 {
2427 argv_idx = -1; /* skip to next argument */
2428
2429 /* Check for only one type of editing. */
2430 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2431 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2432 parmp->edit_type = EDIT_FILE;
2433
2434#ifdef MSWIN
2435 /* Remember if the argument was a full path before changing
2436 * slashes to backslashes. */
2437 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2438 parmp->full_path = TRUE;
2439#endif
2440
2441 /* Add the file to the global argument list. */
2442 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2443 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2444 mch_exit(2);
2445#ifdef FEAT_DIFF
2446 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2447 && !mch_isdir(alist_name(&GARGLIST[0])))
2448 {
2449 char_u *r;
2450
2451 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2452 if (r != NULL)
2453 {
2454 vim_free(p);
2455 p = r;
2456 }
2457 }
2458#endif
2459#if defined(__CYGWIN32__) && !defined(WIN32)
2460 /*
2461 * If vim is invoked by non-Cygwin tools, convert away any
2462 * DOS paths, so things like .swp files are created correctly.
2463 * Look for evidence of non-Cygwin paths before we bother.
2464 * This is only for when using the Unix files.
2465 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002466 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002467 {
2468 char posix_path[PATH_MAX];
2469
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002470# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2471 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2472# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002474# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002475 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002476 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002477 if (p == NULL)
2478 mch_exit(2);
2479 }
2480#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002481
2482#ifdef USE_FNAME_CASE
2483 /* Make the case of the file name match the actual file. */
2484 fname_case(p, 0);
2485#endif
2486
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002487 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002488#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002489 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002490#else
2491 2 /* add buffer number now and use curbuf */
2492#endif
2493 );
2494
2495#if defined(FEAT_MBYTE) && defined(WIN32)
2496 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002497 /* Remember this argument has been added to the argument list.
2498 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002499 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002500# ifdef FEAT_DIFF
2501 parmp->diff_mode
2502# else
2503 FALSE
2504# endif
2505 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002506 }
2507#endif
2508 }
2509
2510 /*
2511 * If there are no more letters after the current "-", go to next
2512 * argument. argv_idx is set to -1 when the current argument is to be
2513 * skipped.
2514 */
2515 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2516 {
2517 --argc;
2518 ++argv;
2519 argv_idx = 1;
2520 }
2521 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002522
2523#ifdef FEAT_EVAL
2524 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2525 * one. */
2526 if (parmp->n_commands > 0)
2527 {
2528 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2529 if (p != NULL)
2530 {
2531 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2532 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2533 vim_free(p);
2534 }
2535 }
2536#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537}
2538
2539/*
2540 * Print a warning if stdout is not a terminal.
2541 * When starting in Ex mode and commands come from a file, set Silent mode.
2542 */
2543 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002544check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545{
2546 int input_isatty; /* is active input a terminal? */
2547
2548 input_isatty = mch_input_isatty();
2549 if (exmode_active)
2550 {
2551 if (!input_isatty)
2552 silent_mode = TRUE;
2553 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002554 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002555#ifdef FEAT_GUI
2556 /* don't want the delay when started from the desktop */
2557 && !gui.starting
2558#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002559 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560 {
2561#ifdef NBDEBUG
2562 /*
2563 * This shouldn't be necessary. But if I run netbeans with the log
2564 * output coming to the console and XOpenDisplay fails, I get vim
2565 * trying to start with input/output to my console tty. This fills my
2566 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002567 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002569 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002570 {
2571 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2572 exit(1);
2573 }
2574#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002575#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2576 if (is_cygpty_used())
2577 {
2578 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2579 exit(1);
2580 }
2581#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002582 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002583 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2584 if (!input_isatty)
2585 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2586 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002587 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2588 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 if (scriptin[0] == NULL)
2590 ui_delay(2000L, TRUE);
2591 TIME_MSG("Warning delay");
2592 }
2593}
2594
2595/*
2596 * Read text from stdin.
2597 */
2598 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002599read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002600{
2601 int i;
2602
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002603#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002604 /* When getting the ATTENTION prompt here, use a dialog */
2605 swap_exists_action = SEA_DIALOG;
2606#endif
2607 no_wait_return = TRUE;
2608 i = msg_didany;
2609 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002610 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002611 no_wait_return = FALSE;
2612 msg_didany = i;
2613 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002614#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002615 check_swap_exists_action();
2616#endif
2617#if !(defined(AMIGA) || defined(MACOS))
2618 /*
2619 * Close stdin and dup it from stderr. Required for GPM to work
2620 * properly, and for running external commands.
2621 * Is there any other system that cannot do this?
2622 */
2623 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002624 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625#endif
2626}
2627
2628/*
2629 * Create the requested number of windows and edit buffers in them.
2630 * Also does recovery if "recoverymode" set.
2631 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002632 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002633create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002634{
2635#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002636 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002637 int done = 0;
2638
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002639 /*
2640 * Create the number of windows that was requested.
2641 */
2642 if (parmp->window_count == -1) /* was not set */
2643 parmp->window_count = 1;
2644 if (parmp->window_count == 0)
2645 parmp->window_count = GARGCOUNT;
2646 if (parmp->window_count > 1)
2647 {
2648 /* Don't change the windows if there was a command in .vimrc that
2649 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002650 if (parmp->window_layout == 0)
2651 parmp->window_layout = WIN_HOR;
2652 if (parmp->window_layout == WIN_TABS)
2653 {
2654 parmp->window_count = make_tabpages(parmp->window_count);
2655 TIME_MSG("making tab pages");
2656 }
2657 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002658 {
2659 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002660 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002661 TIME_MSG("making windows");
2662 }
2663 else
2664 parmp->window_count = win_count();
2665 }
2666 else
2667 parmp->window_count = 1;
2668#endif
2669
2670 if (recoverymode) /* do recover */
2671 {
2672 msg_scroll = TRUE; /* scroll message up */
2673 ml_recover();
2674 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2675 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002676 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 }
2678 else
2679 {
2680 /*
2681 * Open a buffer for windows that don't have one yet.
2682 * Commands in the .vimrc might have loaded a file or split the window.
2683 * Watch out for autocommands that delete a window.
2684 */
2685#ifdef FEAT_AUTOCMD
2686 /*
2687 * Don't execute Win/Buf Enter/Leave autocommands here
2688 */
2689 ++autocmd_no_enter;
2690 ++autocmd_no_leave;
2691#endif
2692#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002693 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002694 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002695 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002696 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002697 {
2698 if (parmp->window_layout == WIN_TABS)
2699 goto_tabpage(1);
2700 else
2701 curwin = firstwin;
2702 }
2703 else if (parmp->window_layout == WIN_TABS)
2704 {
2705 if (curtab->tp_next == NULL)
2706 break;
2707 goto_tabpage(0);
2708 }
2709 else
2710 {
2711 if (curwin->w_next == NULL)
2712 break;
2713 curwin = curwin->w_next;
2714 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002715 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002716#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717 curbuf = curwin->w_buffer;
2718 if (curbuf->b_ml.ml_mfp == NULL)
2719 {
2720#ifdef FEAT_FOLDING
2721 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2722 if (p_fdls >= 0)
2723 curwin->w_p_fdl = p_fdls;
2724#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002725#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726 /* When getting the ATTENTION prompt here, use a dialog */
2727 swap_exists_action = SEA_DIALOG;
2728#endif
2729 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002730
2731 /* create memfile, read file */
2732 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002733
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002734#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002735 if (swap_exists_action == SEA_QUIT)
2736 {
2737 if (got_int || only_one_window())
2738 {
2739 /* abort selected or quit and only one window */
2740 did_emsg = FALSE; /* avoid hit-enter prompt */
2741 getout(1);
2742 }
2743 /* We can't close the window, it would disturb what
2744 * happens next. Clear the file name and set the arg
2745 * index to -1 to delete it later. */
2746 setfname(curbuf, NULL, NULL, FALSE);
2747 curwin->w_arg_idx = -1;
2748 swap_exists_action = SEA_NONE;
2749 }
2750 else
2751 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752#endif
2753#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002754 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002755#endif
2756 }
2757#ifdef FEAT_WINDOWS
2758 ui_breakcheck();
2759 if (got_int)
2760 {
2761 (void)vgetc(); /* only break the file loading, not the rest */
2762 break;
2763 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002765#endif
2766#ifdef FEAT_WINDOWS
2767 if (parmp->window_layout == WIN_TABS)
2768 goto_tabpage(1);
2769 else
2770 curwin = firstwin;
2771 curbuf = curwin->w_buffer;
2772#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773#ifdef FEAT_AUTOCMD
2774 --autocmd_no_enter;
2775 --autocmd_no_leave;
2776#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002777 }
2778}
2779
2780#ifdef FEAT_WINDOWS
2781 /*
2782 * If opened more than one window, start editing files in the other
2783 * windows. make_windows() has already opened the windows.
2784 */
2785 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002786edit_buffers(
2787 mparm_T *parmp,
2788 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002789{
2790 int arg_idx; /* index in argument list */
2791 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002792 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002793 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794
2795# ifdef FEAT_AUTOCMD
2796 /*
2797 * Don't execute Win/Buf Enter/Leave autocommands here
2798 */
2799 ++autocmd_no_enter;
2800 ++autocmd_no_leave;
2801# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002802
2803 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2804 if (curwin->w_arg_idx == -1)
2805 {
2806 win_close(curwin, TRUE);
2807 advance = FALSE;
2808 }
2809
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810 arg_idx = 1;
2811 for (i = 1; i < parmp->window_count; ++i)
2812 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002813 if (cwd != NULL)
2814 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002815 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2816 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002817 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002818 ++arg_idx;
2819 win_close(curwin, TRUE);
2820 advance = FALSE;
2821 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002822 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002823
Bram Moolenaar84212822006-11-07 21:59:47 +00002824 if (advance)
2825 {
2826 if (parmp->window_layout == WIN_TABS)
2827 {
2828 if (curtab->tp_next == NULL) /* just checking */
2829 break;
2830 goto_tabpage(0);
2831 }
2832 else
2833 {
2834 if (curwin->w_next == NULL) /* just checking */
2835 break;
2836 win_enter(curwin->w_next, FALSE);
2837 }
2838 }
2839 advance = TRUE;
2840
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002841 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002842 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002843 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2844 {
2845 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002846 /* Edit file from arg list, if there is one. When "Quit" selected
2847 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002848# ifdef HAS_SWAP_EXISTS_ACTION
2849 swap_exists_did_quit = FALSE;
2850# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002851 (void)do_ecmd(0, arg_idx < GARGCOUNT
2852 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002853 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002854# ifdef HAS_SWAP_EXISTS_ACTION
2855 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002856 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002857 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002858 if (got_int || only_one_window())
2859 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002860 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002861 did_emsg = FALSE; /* avoid hit-enter prompt */
2862 getout(1);
2863 }
2864 win_close(curwin, TRUE);
2865 advance = FALSE;
2866 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002867# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002868 if (arg_idx == GARGCOUNT - 1)
2869 arg_had_last = TRUE;
2870 ++arg_idx;
2871 }
2872 ui_breakcheck();
2873 if (got_int)
2874 {
2875 (void)vgetc(); /* only break the file loading, not the rest */
2876 break;
2877 }
2878 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002879
2880 if (parmp->window_layout == WIN_TABS)
2881 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002882# ifdef FEAT_AUTOCMD
2883 --autocmd_no_enter;
2884# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002885
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002886 /* make the first window the current window */
2887 win = firstwin;
2888#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2889 /* Avoid making a preview window the current window. */
2890 while (win->w_p_pvw)
2891 {
2892 win = win->w_next;
2893 if (win == NULL)
2894 {
2895 win = firstwin;
2896 break;
2897 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002898 }
2899#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002900 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002901
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002902# ifdef FEAT_AUTOCMD
2903 --autocmd_no_leave;
2904# endif
2905 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002906 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002907 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2908}
2909#endif /* FEAT_WINDOWS */
2910
2911/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002912 * Execute the commands from --cmd arguments "cmds[cnt]".
2913 */
2914 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002915exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002916{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002917 char_u **cmds = parmp->pre_commands;
2918 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002919 int i;
2920
2921 if (cnt > 0)
2922 {
2923 curwin->w_cursor.lnum = 0; /* just in case.. */
2924 sourcing_name = (char_u *)_("pre-vimrc command line");
2925# ifdef FEAT_EVAL
2926 current_SID = SID_CMDARG;
2927# endif
2928 for (i = 0; i < cnt; ++i)
2929 do_cmdline_cmd(cmds[i]);
2930 sourcing_name = NULL;
2931# ifdef FEAT_EVAL
2932 current_SID = 0;
2933# endif
2934 TIME_MSG("--cmd commands");
2935 }
2936}
2937
2938/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002939 * Execute "+", "-c" and "-S" arguments.
2940 */
2941 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002942exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002943{
2944 int i;
2945
2946 /*
2947 * We start commands on line 0, make "vim +/pat file" match a
2948 * pattern on line 1. But don't move the cursor when an autocommand
2949 * with g`" was used.
2950 */
2951 msg_scroll = TRUE;
2952 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2953 curwin->w_cursor.lnum = 0;
2954 sourcing_name = (char_u *)"command line";
2955#ifdef FEAT_EVAL
2956 current_SID = SID_CARG;
2957#endif
2958 for (i = 0; i < parmp->n_commands; ++i)
2959 {
2960 do_cmdline_cmd(parmp->commands[i]);
2961 if (parmp->cmds_tofree[i])
2962 vim_free(parmp->commands[i]);
2963 }
2964 sourcing_name = NULL;
2965#ifdef FEAT_EVAL
2966 current_SID = 0;
2967#endif
2968 if (curwin->w_cursor.lnum == 0)
2969 curwin->w_cursor.lnum = 1;
2970
2971 if (!exmode_active)
2972 msg_scroll = FALSE;
2973
2974#ifdef FEAT_QUICKFIX
2975 /* When started with "-q errorfile" jump to first error again. */
2976 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002977 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002978#endif
2979 TIME_MSG("executing command arguments");
2980}
2981
2982/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002983 * Source startup scripts.
2984 */
2985 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002986source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002987{
2988 int i;
2989
2990 /*
2991 * For "evim" source evim.vim first of all, so that the user can overrule
2992 * any things he doesn't like.
2993 */
2994 if (parmp->evim_mode)
2995 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002996 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002997 TIME_MSG("source evim file");
2998 }
2999
3000 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003001 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003002 * nothing else.
3003 */
3004 if (parmp->use_vimrc != NULL)
3005 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003006 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
3007 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
3008 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003009 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003010 {
3011#ifdef FEAT_GUI
3012 if (use_gvimrc == NULL) /* don't load gvimrc either */
3013 use_gvimrc = parmp->use_vimrc;
3014#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003015 }
3016 else
3017 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003018 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003019 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3020 }
3021 }
3022 else if (!silent_mode)
3023 {
3024#ifdef AMIGA
3025 struct Process *proc = (struct Process *)FindTask(0L);
3026 APTR save_winptr = proc->pr_WindowPtr;
3027
3028 /* Avoid a requester here for a volume that doesn't exist. */
3029 proc->pr_WindowPtr = (APTR)-1L;
3030#endif
3031
3032 /*
3033 * Get system wide defaults, if the file name is defined.
3034 */
3035#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003036 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003037#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003038#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003039 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003040#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003041
3042 /*
3043 * Try to read initialization commands from the following places:
3044 * - environment variable VIMINIT
3045 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3046 * - second user vimrc file ($VIM/.vimrc for Dos)
3047 * - environment variable EXINIT
3048 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3049 * - second user exrc file ($VIM/.exrc for Dos)
3050 * The first that exists is used, the rest is ignored.
3051 */
3052 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3053 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003054 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003055#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003056 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3057 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003058#endif
3059#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003060 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3061 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003062#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003063#ifdef USR_VIMRC_FILE4
3064 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3065 DOSO_VIMRC) == FAIL
3066#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003067 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003068 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003069#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003070 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003071#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003072 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003073 {
3074 /* When no .vimrc file was found: source defaults.vim. */
3075 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003076 }
3077 }
3078
3079 /*
3080 * Read initialization commands from ".vimrc" or ".exrc" in current
3081 * directory. This is only done if the 'exrc' option is set.
3082 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003083 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003084 * option has been reset in environment of global ".exrc" or ".vimrc".
3085 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3086 * SYS_VIMRC_FILE.
3087 */
3088 if (p_exrc)
3089 {
3090#if defined(UNIX) || defined(VMS)
3091 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3092 if (!file_owned(VIMRC_FILE))
3093#endif
3094 secure = p_secure;
3095
3096 i = FAIL;
3097 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3098 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3099#ifdef USR_VIMRC_FILE2
3100 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3101 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3102#endif
3103#ifdef USR_VIMRC_FILE3
3104 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3105 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3106#endif
3107#ifdef SYS_VIMRC_FILE
3108 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3109 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3110#endif
3111 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003112 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003113
3114 if (i == FAIL)
3115 {
3116#if defined(UNIX) || defined(VMS)
3117 /* if ".exrc" is not owned by user set 'secure' mode */
3118 if (!file_owned(EXRC_FILE))
3119 secure = p_secure;
3120 else
3121 secure = 0;
3122#endif
3123 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3124 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3125#ifdef USR_EXRC_FILE2
3126 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3127 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3128#endif
3129 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003130 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003131 }
3132 }
3133 if (secure == 2)
3134 need_wait_return = TRUE;
3135 secure = 0;
3136#ifdef AMIGA
3137 proc->pr_WindowPtr = save_winptr;
3138#endif
3139 }
3140 TIME_MSG("sourcing vimrc file(s)");
3141}
3142
3143/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003144 * Setup to start using the GUI. Exit with an error when not available.
3145 */
3146 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003147main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003148{
3149#ifdef FEAT_GUI
3150 gui.starting = TRUE; /* start GUI a bit later */
3151#else
3152 mch_errmsg(_(e_nogvim));
3153 mch_errmsg("\n");
3154 mch_exit(2);
3155#endif
3156}
3157
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003158#endif /* NO_VIM_MAIN */
3159
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003160/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003161 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003162 * Returns FAIL if the environment variable was not executed, OK otherwise.
3163 */
3164 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003165process_env(
3166 char_u *env,
3167 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003168{
3169 char_u *initstr;
3170 char_u *save_sourcing_name;
3171 linenr_T save_sourcing_lnum;
3172#ifdef FEAT_EVAL
3173 scid_T save_sid;
3174#endif
3175
3176 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3177 {
3178 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003179 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003180 save_sourcing_name = sourcing_name;
3181 save_sourcing_lnum = sourcing_lnum;
3182 sourcing_name = env;
3183 sourcing_lnum = 0;
3184#ifdef FEAT_EVAL
3185 save_sid = current_SID;
3186 current_SID = SID_ENV;
3187#endif
3188 do_cmdline_cmd(initstr);
3189 sourcing_name = save_sourcing_name;
3190 sourcing_lnum = save_sourcing_lnum;
3191#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003192 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003193#endif
3194 return OK;
3195 }
3196 return FAIL;
3197}
3198
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003199#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003200/*
3201 * Return TRUE if we are certain the user owns the file "fname".
3202 * Used for ".vimrc" and ".exrc".
3203 * Use both stat() and lstat() for extra security.
3204 */
3205 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003206file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003207{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003208 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003209# ifdef UNIX
3210 uid_t uid = getuid();
3211# else /* VMS */
3212 uid_t uid = ((getgid() << 16) | getuid());
3213# endif
3214
3215 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3216# ifdef HAVE_LSTAT
3217 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3218# endif
3219 );
3220}
3221#endif
3222
3223/*
3224 * Give an error message main_errors["n"] and exit.
3225 */
3226 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003227mainerr(
3228 int n, /* one of the ME_ defines */
3229 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003230{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003231#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003232 reset_signals(); /* kill us with CTRL-C here, if you like */
3233#endif
3234
3235 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003236 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003237 mch_errmsg(_(main_errors[n]));
3238 if (str != NULL)
3239 {
3240 mch_errmsg(": \"");
3241 mch_errmsg((char *)str);
3242 mch_errmsg("\"");
3243 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003244 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003245
3246 mch_exit(1);
3247}
3248
3249 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003250mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003251{
3252 mainerr(ME_ARG_MISSING, str);
3253}
3254
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003255#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256/*
3257 * print a message with three spaces prepended and '\n' appended.
3258 */
3259 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003260main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003261{
3262 mch_msg(" ");
3263 mch_msg(s);
3264 mch_msg("\n");
3265}
3266
3267/*
3268 * Print messages for "vim -h" or "vim --help" and exit.
3269 */
3270 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003271usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003272{
3273 int i;
3274 static char *(use[]) =
3275 {
3276 N_("[file ..] edit specified file(s)"),
3277 N_("- read text from stdin"),
3278 N_("-t tag edit file where tag is defined"),
3279#ifdef FEAT_QUICKFIX
3280 N_("-q [errorfile] edit file with first error")
3281#endif
3282 };
3283
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003284#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003285 reset_signals(); /* kill us with CTRL-C here, if you like */
3286#endif
3287
3288 mch_msg(longVersion);
3289 mch_msg(_("\n\nusage:"));
3290 for (i = 0; ; ++i)
3291 {
3292 mch_msg(_(" vim [arguments] "));
3293 mch_msg(_(use[i]));
3294 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3295 break;
3296 mch_msg(_("\n or:"));
3297 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003298#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003299 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003300#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301
3302 mch_msg(_("\n\nArguments:\n"));
3303 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003304#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003305 main_msg(_("--literal\t\tDon't expand wildcards"));
3306#endif
3307#ifdef FEAT_OLE
3308 main_msg(_("-register\t\tRegister this gvim for OLE"));
3309 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3310#endif
3311#ifdef FEAT_GUI
3312 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3313 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3314#endif
3315 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3316 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003317 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3319#ifdef FEAT_DIFF
3320 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3321#endif
3322 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3323 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3324 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3325 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3326 main_msg(_("-M\t\t\tModifications in text not allowed"));
3327 main_msg(_("-b\t\t\tBinary mode"));
3328#ifdef FEAT_LISP
3329 main_msg(_("-l\t\t\tLisp mode"));
3330#endif
3331 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3332 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003333 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3334#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003335 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003336#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003337 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3338 main_msg(_("-r\t\t\tList swap files and exit"));
3339 main_msg(_("-r (with file name)\tRecover crashed session"));
3340 main_msg(_("-L\t\t\tSame as -r"));
3341#ifdef AMIGA
3342 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3343 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3344#endif
3345#ifdef FEAT_ARABIC
3346 main_msg(_("-A\t\t\tstart in Arabic mode"));
3347#endif
3348#ifdef FEAT_RIGHTLEFT
3349 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3350#endif
3351#ifdef FEAT_FKMAP
3352 main_msg(_("-F\t\t\tStart in Farsi mode"));
3353#endif
3354 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003355 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003356 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003357 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3358#ifdef FEAT_GUI
3359 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3360#endif
3361 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003362#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003363 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003364 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3365 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003366#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003367 main_msg(_("+\t\t\tStart at end of file"));
3368 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003369 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003370 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3371 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3372 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3373 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3374 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3375#ifdef FEAT_CRYPT
3376 main_msg(_("-x\t\t\tEdit encrypted files"));
3377#endif
3378#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3379# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3380 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3381# endif
3382 main_msg(_("-X\t\t\tDo not connect to X server"));
3383#endif
3384#ifdef FEAT_CLIENTSERVER
3385 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3386 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3387 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3388 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003389# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003390 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003391# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003392 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3393 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3394 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3395 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3396#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003397#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003398 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003399#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400#ifdef FEAT_VIMINFO
3401 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3402#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003403 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3405 main_msg(_("--version\t\tPrint version information and exit"));
3406
3407#ifdef FEAT_GUI_X11
3408# ifdef FEAT_GUI_MOTIF
3409 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3410# else
3411# ifdef FEAT_GUI_ATHENA
3412# ifdef FEAT_GUI_NEXTAW
3413 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3414# else
3415 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3416# endif
3417# endif
3418# endif
3419 main_msg(_("-display <display>\tRun vim on <display>"));
3420 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3422 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3423 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3424 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3425 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3426 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3427 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3428 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3429# ifdef FEAT_GUI_ATHENA
3430 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3431# endif
3432 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3433 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3434 main_msg(_("-xrm <resource>\tSet the specified resource"));
3435#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436#ifdef FEAT_GUI_GTK
3437 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3438 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3439 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3440 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3441 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003444 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003445#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003446#ifdef FEAT_GUI_W32
3447 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003448 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003449#endif
3450
3451#ifdef FEAT_GUI_GNOME
3452 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3453 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003454 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003455 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003456 gui.dofork = FALSE;
3457 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003458 else
3459#endif
3460 mch_exit(0);
3461}
3462
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003463#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464/*
3465 * Check the result of the ATTENTION dialog:
3466 * When "Quit" selected, exit Vim.
3467 * When "Recover" selected, recover the file.
3468 */
3469 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003470check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003471{
3472 if (swap_exists_action == SEA_QUIT)
3473 getout(1);
3474 handle_swap_exists(NULL);
3475}
3476#endif
3477
Bram Moolenaar08cab962017-03-04 14:37:18 +01003478#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003479
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003480#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003481static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003482
3483static struct timeval prev_timeval;
3484
Bram Moolenaar3f269672009-11-03 11:11:11 +00003485# ifdef WIN3264
3486/*
3487 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3488 */
3489 static int
3490gettimeofday(struct timeval *tv, char *dummy)
3491{
3492 long t = clock();
3493 tv->tv_sec = t / CLOCKS_PER_SEC;
3494 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3495 return 0;
3496}
3497# endif
3498
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499/*
3500 * Save the previous time before doing something that could nest.
3501 * set "*tv_rel" to the time elapsed so far.
3502 */
3503 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003504time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003505{
3506 *((struct timeval *)tv_rel) = prev_timeval;
3507 gettimeofday(&prev_timeval, NULL);
3508 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3509 - ((struct timeval *)tv_rel)->tv_usec;
3510 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3511 - ((struct timeval *)tv_rel)->tv_sec;
3512 if (((struct timeval *)tv_rel)->tv_usec < 0)
3513 {
3514 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3515 --((struct timeval *)tv_rel)->tv_sec;
3516 }
3517 *(struct timeval *)tv_start = prev_timeval;
3518}
3519
3520/*
3521 * Compute the previous time after doing something that could nest.
3522 * Subtract "*tp" from prev_timeval;
3523 * Note: The arguments are (void *) to avoid trouble with systems that don't
3524 * have struct timeval.
3525 */
3526 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003527time_pop(
3528 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529{
3530 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3531 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3532 if (prev_timeval.tv_usec < 0)
3533 {
3534 prev_timeval.tv_usec += 1000000;
3535 --prev_timeval.tv_sec;
3536 }
3537}
3538
3539 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003540time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541{
3542 long usec;
3543 long msec;
3544
3545 usec = now->tv_usec - then->tv_usec;
3546 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3547 usec = usec % 1000L;
3548 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3549}
3550
3551 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003552time_msg(
3553 char *mesg,
3554 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003555 (struct timeval *) */
3556{
3557 static struct timeval start;
3558 struct timeval now;
3559
3560 if (time_fd != NULL)
3561 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003562 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003563 {
3564 gettimeofday(&start, NULL);
3565 prev_timeval = start;
3566 fprintf(time_fd, "\n\ntimes in msec\n");
3567 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3568 fprintf(time_fd, " clock elapsed: other lines\n\n");
3569 }
3570 gettimeofday(&now, NULL);
3571 time_diff(&start, &now);
3572 if (((struct timeval *)tv_start) != NULL)
3573 {
3574 fprintf(time_fd, " ");
3575 time_diff(((struct timeval *)tv_start), &now);
3576 }
3577 fprintf(time_fd, " ");
3578 time_diff(&prev_timeval, &now);
3579 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003580 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003581 }
3582}
3583
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003584#endif
3585
Bram Moolenaar595297d2017-03-04 19:11:12 +01003586#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003587 static void
3588set_progpath(char_u *argv0)
3589{
3590 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003591
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003592# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003593 char buf[PATH_MAX + 1];
3594 ssize_t len;
3595
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003596 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003597 if (len > 0)
3598 {
3599 buf[len] = NUL;
3600 val = (char_u *)buf;
3601 }
3602# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003603 /* A relative path containing a "/" will become invalid when using ":cd",
3604 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003605 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3606 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003607# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003608 char_u *path = NULL;
3609
3610 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3611 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003612# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003613 char_u buf[MAXPATHL];
3614
Bram Moolenaar43663192017-03-05 14:29:12 +01003615 if (!mch_isFullName(argv0))
3616 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003617 if (gettail(argv0) != argv0
3618 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3619 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003620 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003621# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003622# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003623
Bram Moolenaar08cab962017-03-04 14:37:18 +01003624 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003625
Bram Moolenaar066029e2017-03-05 15:19:32 +01003626# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003627 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003628# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003629}
3630
3631#endif /* NO_VIM_MAIN */
3632
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003633#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003634
3635/*
3636 * Common code for the X command server and the Win32 command server.
3637 */
3638
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003639static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003640
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003641/*
3642 * Do the client-server stuff, unless "--servername ''" was used.
3643 */
3644 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003645exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003646{
3647 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3648 {
3649# ifdef WIN32
3650 /* Initialise the client/server messaging infrastructure. */
3651 serverInitMessaging();
3652# endif
3653
3654 /*
3655 * When a command server argument was found, execute it. This may
3656 * exit Vim when it was successful. Otherwise it's executed further
3657 * on. Remember the encoding used here in "serverStrEnc".
3658 */
3659 if (parmp->serverArg)
3660 {
3661 cmdsrv_main(&parmp->argc, parmp->argv,
3662 parmp->serverName_arg, &parmp->serverStr);
3663# ifdef FEAT_MBYTE
3664 parmp->serverStrEnc = vim_strsave(p_enc);
3665# endif
3666 }
3667
3668 /* If we're still running, get the name to register ourselves.
3669 * On Win32 can register right now, for X11 need to setup the
3670 * clipboard first, it's further down. */
3671 parmp->servername = serverMakeName(parmp->serverName_arg,
3672 parmp->argv[0]);
3673# ifdef WIN32
3674 if (parmp->servername != NULL)
3675 {
3676 serverSetName(parmp->servername);
3677 vim_free(parmp->servername);
3678 }
3679# endif
3680 }
3681}
3682
3683/*
3684 * Prepare for running as a Vim server.
3685 */
3686 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003687prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003688{
3689# if defined(FEAT_X11)
3690 /*
3691 * Register for remote command execution with :serversend and --remote
3692 * unless there was a -X or a --servername '' on the command line.
3693 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003694 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003695 */
3696 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3697# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003698 (gui.in_use
3699# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003700 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003701# endif
3702 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003703# endif
3704 parmp->serverName_arg != NULL))
3705 {
3706 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3707 vim_free(parmp->servername);
3708 TIME_MSG("register server name");
3709 }
3710 else
3711 serverDelayedStartName = parmp->servername;
3712# endif
3713
3714 /*
3715 * Execute command ourselves if we're here because the send failed (or
3716 * else we would have exited above).
3717 */
3718 if (parmp->serverStr != NULL)
3719 {
3720 char_u *p;
3721
3722 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3723 parmp->serverStr, &p));
3724 vim_free(p);
3725 }
3726}
3727
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003728 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003729cmdsrv_main(
3730 int *argc,
3731 char **argv,
3732 char_u *serverName_arg,
3733 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003734{
3735 char_u *res;
3736 int i;
3737 char_u *sname;
3738 int ret;
3739 int didone = FALSE;
3740 int exiterr = 0;
3741 char **newArgV = argv + 1;
3742 int newArgC = 1,
3743 Argc = *argc;
3744 int argtype;
3745#define ARGTYPE_OTHER 0
3746#define ARGTYPE_EDIT 1
3747#define ARGTYPE_EDIT_WAIT 2
3748#define ARGTYPE_SEND 3
3749 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003750 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003751# ifndef FEAT_X11
3752 HWND srv;
3753# else
3754 Window srv;
3755
3756 setup_term_clip();
3757# endif
3758
3759 sname = serverMakeName(serverName_arg, argv[0]);
3760 if (sname == NULL)
3761 return;
3762
3763 /*
3764 * Execute the command server related arguments and remove them
3765 * from the argc/argv array; We may have to return into main()
3766 */
3767 for (i = 1; i < Argc; i++)
3768 {
3769 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003770 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003771 {
3772 for (; i < *argc; i++)
3773 {
3774 *newArgV++ = argv[i];
3775 newArgC++;
3776 }
3777 break;
3778 }
3779
Bram Moolenaareb94e552006-03-11 21:35:11 +00003780 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003781 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003782 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3783 {
3784 char *p = argv[i] + 8;
3785
3786 argtype = ARGTYPE_EDIT;
3787 while (*p != NUL)
3788 {
3789 if (STRNICMP(p, "-wait", 5) == 0)
3790 {
3791 argtype = ARGTYPE_EDIT_WAIT;
3792 p += 5;
3793 }
3794 else if (STRNICMP(p, "-silent", 7) == 0)
3795 {
3796 silent = TRUE;
3797 p += 7;
3798 }
3799 else if (STRNICMP(p, "-tab", 4) == 0)
3800 {
3801 tabs = TRUE;
3802 p += 4;
3803 }
3804 else
3805 {
3806 argtype = ARGTYPE_OTHER;
3807 break;
3808 }
3809 }
3810 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003811 else
3812 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003813
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003814 if (argtype != ARGTYPE_OTHER)
3815 {
3816 if (i == *argc - 1)
3817 mainerr_arg_missing((char_u *)argv[i]);
3818 if (argtype == ARGTYPE_SEND)
3819 {
3820 *serverStr = (char_u *)argv[i + 1];
3821 i++;
3822 }
3823 else
3824 {
3825 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003826 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003827 if (*serverStr == NULL)
3828 {
3829 /* Probably out of memory, exit. */
3830 didone = TRUE;
3831 exiterr = 1;
3832 break;
3833 }
3834 Argc = i;
3835 }
3836# ifdef FEAT_X11
3837 if (xterm_dpy == NULL)
3838 {
3839 mch_errmsg(_("No display"));
3840 ret = -1;
3841 }
3842 else
3843 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003844 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003845# else
3846 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003847 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003848# endif
3849 if (ret < 0)
3850 {
3851 if (argtype == ARGTYPE_SEND)
3852 {
3853 /* Failed to send, abort. */
3854 mch_errmsg(_(": Send failed.\n"));
3855 didone = TRUE;
3856 exiterr = 1;
3857 }
3858 else if (!silent)
3859 /* Let vim start normally. */
3860 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3861 break;
3862 }
3863
3864# ifdef FEAT_GUI_W32
3865 /* Guess that when the server name starts with "g" it's a GUI
3866 * server, which we can bring to the foreground here.
3867 * Foreground() in the server doesn't work very well. */
3868 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3869 SetForegroundWindow(srv);
3870# endif
3871
3872 /*
3873 * For --remote-wait: Wait until the server did edit each
3874 * file. Also detect that the server no longer runs.
3875 */
3876 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3877 {
3878 int numFiles = *argc - i - 1;
3879 int j;
3880 char_u *done = alloc(numFiles);
3881 char_u *p;
3882# ifdef FEAT_GUI_W32
3883 NOTIFYICONDATA ni;
3884 int count = 0;
3885 extern HWND message_window;
3886# endif
3887
3888 if (numFiles > 0 && argv[i + 1][0] == '+')
3889 /* Skip "+cmd" argument, don't wait for it to be edited. */
3890 --numFiles;
3891
3892# ifdef FEAT_GUI_W32
3893 ni.cbSize = sizeof(ni);
3894 ni.hWnd = message_window;
3895 ni.uID = 0;
3896 ni.uFlags = NIF_ICON|NIF_TIP;
3897 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3898 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3899 Shell_NotifyIcon(NIM_ADD, &ni);
3900# endif
3901
3902 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003903 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003904 while (memchr(done, 0, numFiles) != NULL)
3905 {
3906# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003907 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003908 if (p == NULL)
3909 break;
3910# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003911 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003912 break;
3913# endif
3914 j = atoi((char *)p);
3915 if (j >= 0 && j < numFiles)
3916 {
3917# ifdef FEAT_GUI_W32
3918 ++count;
3919 sprintf(ni.szTip, _("%d of %d edited"),
3920 count, numFiles);
3921 Shell_NotifyIcon(NIM_MODIFY, &ni);
3922# endif
3923 done[j] = 1;
3924 }
3925 }
3926# ifdef FEAT_GUI_W32
3927 Shell_NotifyIcon(NIM_DELETE, &ni);
3928# endif
3929 }
3930 }
3931 else if (STRICMP(argv[i], "--remote-expr") == 0)
3932 {
3933 if (i == *argc - 1)
3934 mainerr_arg_missing((char_u *)argv[i]);
3935# ifdef WIN32
3936 /* Win32 always works? */
3937 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003938 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003939# else
3940 if (xterm_dpy == NULL)
3941 mch_errmsg(_("No display: Send expression failed.\n"));
3942 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003943 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003944# endif
3945 {
3946 if (res != NULL && *res != NUL)
3947 {
3948 /* Output error from remote */
3949 mch_errmsg((char *)res);
3950 vim_free(res);
3951 res = NULL;
3952 }
3953 mch_errmsg(_(": Send expression failed.\n"));
3954 }
3955 }
3956 else if (STRICMP(argv[i], "--serverlist") == 0)
3957 {
3958# ifdef WIN32
3959 /* Win32 always works? */
3960 res = serverGetVimNames();
3961# else
3962 if (xterm_dpy != NULL)
3963 res = serverGetVimNames(xterm_dpy);
3964# endif
3965 if (called_emsg)
3966 mch_errmsg("\n");
3967 }
3968 else if (STRICMP(argv[i], "--servername") == 0)
3969 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003970 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003971 i++;
3972 continue;
3973 }
3974 else
3975 {
3976 *newArgV++ = argv[i];
3977 newArgC++;
3978 continue;
3979 }
3980 didone = TRUE;
3981 if (res != NULL && *res != NUL)
3982 {
3983 mch_msg((char *)res);
3984 if (res[STRLEN(res) - 1] != '\n')
3985 mch_msg("\n");
3986 }
3987 vim_free(res);
3988 }
3989
3990 if (didone)
3991 {
3992 display_errors(); /* display any collected messages */
3993 exit(exiterr); /* Mission accomplished - get out */
3994 }
3995
3996 /* Return back into main() */
3997 *argc = newArgC;
3998 vim_free(sname);
3999}
4000
4001/*
4002 * Build a ":drop" command to send to a Vim server.
4003 */
4004 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004005build_drop_cmd(
4006 int filec,
4007 char **filev,
4008 int tabs, /* Use ":tab drop" instead of ":drop". */
4009 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004010{
4011 garray_T ga;
4012 int i;
4013 char_u *inicmd = NULL;
4014 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004015 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004016 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004017
4018 if (filec > 0 && filev[0][0] == '+')
4019 {
4020 inicmd = (char_u *)filev[0] + 1;
4021 filev++;
4022 filec--;
4023 }
4024 /* Check if we have at least one argument. */
4025 if (filec <= 0)
4026 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004027
4028 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004029 cwd = alloc(MAXPATHL);
4030 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004031 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004032 if (mch_dirname(cwd, MAXPATHL) != OK)
4033 {
4034 vim_free(cwd);
4035 return NULL;
4036 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004037 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004038#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004039 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004040#else
4041 PATH_ESC_CHARS,
4042#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004043 '\\', TRUE);
4044 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004045 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004046 return NULL;
4047 ga_init2(&ga, 1, 100);
4048 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004049 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004050
4051 /* Call inputsave() so that a prompt for an encryption key works. */
4052 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4053 if (tabs)
4054 ga_concat(&ga, (char_u *)"tab ");
4055 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004056 for (i = 0; i < filec; i++)
4057 {
4058 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004059 * do it again in the Vim server. On MS-Windows only escape
4060 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004061 p = vim_strsave_escaped((char_u *)filev[i],
4062#ifdef UNIX
4063 PATH_ESC_CHARS
4064#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004065 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004066#endif
4067 );
4068 if (p == NULL)
4069 {
4070 vim_free(ga.ga_data);
4071 return NULL;
4072 }
4073 ga_concat(&ga, (char_u *)" ");
4074 ga_concat(&ga, p);
4075 vim_free(p);
4076 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004077 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4078
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004079 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4080 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004081 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4082
4083 /* Switch back to the correct current directory (prior to temporary path
4084 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004085 * correct after the :drop command. With line breaks and spaces:
4086 * if !exists('+acd') || !&acd
4087 * if haslocaldir()
4088 * cd -
4089 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004090 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004091 * cd -
4092 * endif
4093 * endif
4094 */
4095 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004096 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004097 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004098 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004099 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004100
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004101 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004102 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4103 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004104 if (inicmd != NULL)
4105 {
4106 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4107 * the following commands to be inserted as text. Use a "|",
4108 * hopefully "inicmd" does allow this... */
4109 ga_concat(&ga, inicmd);
4110 ga_concat(&ga, (char_u *)"|");
4111 }
4112 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4113 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004114 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004115 ga_append(&ga, NUL);
4116 return ga.ga_data;
4117}
4118
4119/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004120 * Make our basic server name: use the specified "arg" if given, otherwise use
4121 * the tail of the command "cmd" we were started with.
4122 * Return the name in allocated memory. This doesn't include a serial number.
4123 */
4124 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004125serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004126{
4127 char_u *p;
4128
4129 if (arg != NULL && *arg != NUL)
4130 p = vim_strsave_up(arg);
4131 else
4132 {
4133 p = vim_strsave_up(gettail((char_u *)cmd));
4134 /* Remove .exe or .bat from the name. */
4135 if (p != NULL && vim_strchr(p, '.') != NULL)
4136 *vim_strchr(p, '.') = NUL;
4137 }
4138 return p;
4139}
4140#endif /* FEAT_CLIENTSERVER */
4141
4142#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4143/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004144 * Replace termcodes such as <CR> and insert as key presses if there is room.
4145 */
4146 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004147server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004148{
4149 char_u *ptr = NULL;
4150 char_u *cpo_save = p_cpo;
4151
4152 /* Set 'cpoptions' the way we want it.
4153 * B set - backslashes are *not* treated specially
4154 * k set - keycodes are *not* reverse-engineered
4155 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004156 * The last but one parameter of replace_termcodes() is TRUE so that the
4157 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004158 */
4159 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004160 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004161 p_cpo = cpo_save;
4162
4163 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4164 {
4165 /*
4166 * Add the string to the input stream.
4167 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4168 *
4169 * First clear typed characters from the typeahead buffer, there could
4170 * be half a mapping there. Then append to the existing string, so
4171 * that multiple commands from a client are concatenated.
4172 */
4173 if (typebuf.tb_maplen < typebuf.tb_len)
4174 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4175 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4176
4177 /* Let input_available() know we inserted text in the typeahead
4178 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004179 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004180 }
4181 vim_free((char_u *)ptr);
4182}
4183
4184/*
4185 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004186 */
4187 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004188eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004189{
4190 char_u *res;
4191 int save_dbl = debug_break_level;
4192 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004193 void *fc;
4194
4195 /* Evaluate the expression at the toplevel, don't use variables local to
4196 * the calling function. */
4197 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004198
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004199 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4200 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004201 debug_break_level = -1;
4202 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004203 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4204 * to be typed. Do generate errors so that try/catch works. */
4205 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004206
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004207 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004208
4209 debug_break_level = save_dbl;
4210 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004211 --emsg_silent;
4212 if (emsg_silent < 0)
4213 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004214 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004215
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004216 /* A client can tell us to redraw, but not to display the cursor, so do
4217 * that here. */
4218 setcursor();
4219 out_flush();
4220#ifdef FEAT_GUI
4221 if (gui.in_use)
4222 gui_update_cursor(FALSE, FALSE);
4223#endif
4224
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004225 return res;
4226}
4227
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004228/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004229 * Evaluate a command or expression sent to ourselves.
4230 */
4231 int
4232sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4233{
4234 if (asExpr)
4235 {
4236 char_u *ret;
4237
4238 ret = eval_client_expr_to_string(cmd);
4239 if (result != NULL)
4240 {
4241 if (ret == NULL)
4242 {
4243 char *err = _(e_invexprmsg);
4244 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4245 char_u *msg;
4246
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004247 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004248 if (msg != NULL)
4249 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4250 *result = msg;
4251 }
4252 else
4253 *result = ret;
4254 }
4255 else
4256 vim_free(ret);
4257 return ret == NULL ? -1 : 0;
4258 }
4259 server_to_input_buf(cmd);
4260 return 0;
4261}
4262
4263/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004264 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4265 * return an allocated string. Otherwise return "data".
4266 * "*tofree" is set to the result when it needs to be freed later.
4267 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004268 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004269serverConvert(
4270 char_u *client_enc UNUSED,
4271 char_u *data,
4272 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004273{
4274 char_u *res = data;
4275
4276 *tofree = NULL;
4277# ifdef FEAT_MBYTE
4278 if (client_enc != NULL && p_enc != NULL)
4279 {
4280 vimconv_T vimconv;
4281
4282 vimconv.vc_type = CONV_NONE;
4283 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4284 && vimconv.vc_type != CONV_NONE)
4285 {
4286 res = string_convert(&vimconv, data, NULL);
4287 if (res == NULL)
4288 res = data;
4289 else
4290 *tofree = res;
4291 }
4292 convert_setup(&vimconv, NULL, NULL);
4293 }
4294# endif
4295 return res;
4296}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004297#endif