blob: 6d59b09ba4a66bdefdfa8424c07e19a070f73591 [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. */
436 if (params.use_vimrc != NULL && STRCMP(params.use_vimrc, "NONE") == 0)
437 p_lpl = FALSE;
438
Bram Moolenaar58d98232005-07-23 22:25:46 +0000439 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000440 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000441
Bram Moolenaar58d98232005-07-23 22:25:46 +0000442 /* Source startup scripts. */
443 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444
445#ifdef FEAT_EVAL
446 /*
447 * Read all the plugin files.
448 * Only when compiled with +eval, since most plugins need it.
449 */
450 if (p_lpl)
451 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000452# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar66459b72016-08-06 19:01:55 +0200453 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000454# else
Bram Moolenaar66459b72016-08-06 19:01:55 +0200455 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000456# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000457 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100458
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100459 ex_packloadall(NULL);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100460 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200461
462# ifdef VMS /* Somehow VMS doesn't handle the "**". */
463 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
464# else
465 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
466# endif
467 TIME_MSG("loading after plugins");
468
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000469 }
470#endif
471
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000472#ifdef FEAT_DIFF
473 /* Decide about window layout for diff mode after reading vimrc. */
474 if (params.diff_mode && params.window_layout == 0)
475 {
476 if (diffopt_horizontal())
477 params.window_layout = WIN_HOR; /* use horizontal split */
478 else
479 params.window_layout = WIN_VER; /* use vertical split */
480 }
481#endif
482
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000483 /*
484 * Recovery mode without a file name: List swap files.
485 * This uses the 'dir' option, therefore it must be after the
486 * initializations.
487 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200488 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000489 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200490 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000491 mch_exit(0);
492 }
493
494 /*
495 * Set a few option defaults after reading .vimrc files:
496 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
497 */
498 set_init_3();
499 TIME_MSG("inits 3");
500
501 /*
502 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
503 * Note that this overrides anything from a vimrc file.
504 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000505 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000506 p_uc = 0;
507
508#ifdef FEAT_FKMAP
509 if (curwin->w_p_rl && p_altkeymap)
510 {
511 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
512# ifdef FEAT_ARABIC
513 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
514# endif
515 p_fkmap = TRUE; /* Set the Farsi keymap mode */
516 }
517#endif
518
519#ifdef FEAT_GUI
520 if (gui.starting)
521 {
522#if defined(UNIX) || defined(VMS)
523 /* When something caused a message from a vimrc script, need to output
524 * an extra newline before the shell prompt. */
525 if (did_emsg || msg_didout)
526 putchar('\n');
527#endif
528
529 gui_start(); /* will set full_screen to TRUE */
530 TIME_MSG("starting GUI");
531
532 /* When running "evim" or "gvim -y" we need the menus, exit if we
533 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000534 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535 mch_exit(1);
536 }
537#endif
538
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539#ifdef FEAT_VIMINFO
540 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000541 * Read in registers, history etc, but not marks, from the viminfo file.
542 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000543 */
544 if (*p_viminfo != NUL)
545 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000546 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547 TIME_MSG("reading viminfo");
548 }
549#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100550#ifdef FEAT_EVAL
551 /* It's better to make v:oldfiles an empty list than NULL. */
552 if (get_vim_var_list(VV_OLDFILES) == NULL)
553 set_vim_var_list(VV_OLDFILES, list_alloc());
554#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555
556#ifdef FEAT_QUICKFIX
557 /*
558 * "-q errorfile": Load the error file now.
559 * If the error file can't be read, exit before doing anything else.
560 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000561 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100563 char_u *enc = NULL;
564
565# ifdef FEAT_MBYTE
566 enc = p_menc;
567# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000568 if (params.use_ef != NULL)
569 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000570 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200571 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100572 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000573 {
574 out_char('\n');
575 mch_exit(3);
576 }
577 TIME_MSG("reading errorfile");
578 }
579#endif
580
581 /*
582 * Start putting things on the screen.
583 * Scroll screen down before drawing over it
584 * Clear screen now, so file message will not be cleared.
585 */
586 starting = NO_BUFFERS;
587 no_wait_return = FALSE;
588 if (!exmode_active)
589 msg_scroll = FALSE;
590
591#ifdef FEAT_GUI
592 /*
593 * This seems to be required to make callbacks to be called now, instead
594 * of after things have been put on the screen, which then may be deleted
595 * when getting a resize callback.
596 * For the Mac this handles putting files dropped on the Vim icon to
597 * global_alist.
598 */
599 if (gui.in_use)
600 {
601# ifdef FEAT_SUN_WORKSHOP
602 if (!usingSunWorkShop)
603# endif
604 gui_wait_for_chars(50L);
605 TIME_MSG("GUI delay");
606 }
607#endif
608
609#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
610 qnx_clip_init();
611#endif
612
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200613#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
614 clip_init(TRUE);
615#endif
616
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000617#ifdef FEAT_XCLIPBOARD
618 /* Start using the X clipboard, unless the GUI was started. */
619# ifdef FEAT_GUI
620 if (!gui.in_use)
621# endif
622 {
623 setup_term_clip();
624 TIME_MSG("setup clipboard");
625 }
626#endif
627
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000628#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000629 /* Prepare for being a Vim server. */
630 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000631#endif
632
633 /*
634 * If "-" argument given: Read file from stdin.
635 * Do this before starting Raw mode, because it may change things that the
636 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
637 * are the same terminal: "cat | vim -".
638 * Using autocommands here may cause trouble...
639 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000640 if (params.edit_type == EDIT_STDIN && !recoverymode)
641 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000642
643#if defined(UNIX) || defined(VMS)
644 /* When switching screens and something caused a message from a vimrc
645 * script, need to output an extra newline on exit. */
646 if ((did_emsg || msg_didout) && *T_TI != NUL)
647 newline_on_exit = TRUE;
648#endif
649
650 /*
651 * When done something that is not allowed or error message call
652 * wait_return. This must be done before starttermcap(), because it may
653 * switch to another screen. It must be done after settmode(TMODE_RAW),
654 * because we want to react on a single key stroke.
655 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000656 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000657 */
658 settmode(TMODE_RAW);
659 TIME_MSG("setting raw mode");
660
661 if (need_wait_return || msg_didany)
662 {
663 wait_return(TRUE);
664 TIME_MSG("waiting for return");
665 }
666
667 starttermcap(); /* start termcap if not done by wait_return() */
668 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200669#if defined(FEAT_TERMRESPONSE)
670# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200671 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200672# endif
673 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100674#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000675
676#ifdef FEAT_MOUSE
677 setmouse(); /* may start using the mouse */
678#endif
679 if (scroll_region)
680 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000681 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000682
683 /*
684 * Don't clear the screen when starting in Ex mode, unless using the GUI.
685 */
686 if (exmode_active
687#ifdef FEAT_GUI
688 && !gui.in_use
689#endif
690 )
691 must_redraw = CLEAR;
692 else
693 {
694 screenclear(); /* clear screen */
695 TIME_MSG("clearing screen");
696 }
697
698#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000699 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000700 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100701 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200702 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 TIME_MSG("getting crypt key");
704 }
705#endif
706
707 no_wait_return = TRUE;
708
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000709 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000710 * Create the requested number of windows and edit buffers in them.
711 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000713 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000714 TIME_MSG("opening buffers");
715
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000716#ifdef FEAT_EVAL
717 /* clear v:swapcommand */
718 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
719#endif
720
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 /* Ex starts at last line of the file */
722 if (exmode_active)
723 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
724
725#ifdef FEAT_AUTOCMD
726 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
727 TIME_MSG("BufEnter autocommands");
728#endif
729 setpcmark();
730
731#ifdef FEAT_QUICKFIX
732 /*
733 * When started with "-q errorfile" jump to first error now.
734 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000735 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000737 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738 TIME_MSG("jump to first error");
739 }
740#endif
741
742#ifdef FEAT_WINDOWS
743 /*
744 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000745 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000746 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200747 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000748#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200749 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000750
751#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000752 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753 {
754 win_T *wp;
755
756 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200757 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000758 diff_win_options(wp, TRUE);
759 }
760#endif
761
762 /*
763 * Shorten any of the filenames, but only when absolute.
764 */
765 shorten_fnames(FALSE);
766
767 /*
768 * Need to jump to the tag before executing the '-c command'.
769 * Makes "vim -c '/return' -t main" work.
770 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000771 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000772 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000773#if defined(HAS_SWAP_EXISTS_ACTION)
774 swap_exists_did_quit = FALSE;
775#endif
776
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000777 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000778 do_cmdline_cmd(IObuff);
779 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000780
781#if defined(HAS_SWAP_EXISTS_ACTION)
782 /* If the user doesn't want to edit the file then we quit here. */
783 if (swap_exists_did_quit)
784 getout(1);
785#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000786 }
787
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000788 /* Execute any "+", "-c" and "-S" arguments. */
789 if (params.n_commands > 0)
790 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000791
792 RedrawingDisabled = 0;
793 redraw_all_later(NOT_VALID);
794 no_wait_return = FALSE;
795 starting = 0;
796
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200797 /* 'autochdir' has been postponed */
798 DO_AUTOCHDIR
799
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000800#ifdef FEAT_TERMRESPONSE
801 /* Requesting the termresponse is postponed until here, so that a "-c q"
802 * argument doesn't make it appear in the shell Vim was started from. */
803 may_req_termresponse();
804#endif
805
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806 /* start in insert mode */
807 if (p_im)
808 need_start_insertmode = TRUE;
809
Bram Moolenaar14735512016-03-26 21:00:08 +0100810#ifdef FEAT_EVAL
811 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
812#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813#ifdef FEAT_AUTOCMD
814 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
815 TIME_MSG("VimEnter autocommands");
816#endif
817
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200818#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
819 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
820 * done after the clipboard is available and all initial commands that may
821 * modify the 'clipboard' setting have run; i.e. just before entering the
822 * main loop. */
823 {
824 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100825
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200826 adjust_clip_reg(&default_regname);
827 set_reg_var(default_regname);
828 }
829#endif
830
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000831#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
832 /* When a startup script or session file setup for diff'ing and
833 * scrollbind, sync the scrollbind now. */
834 if (curwin->w_p_diff && curwin->w_p_scb)
835 {
836 update_topline();
837 check_scrollbind((linenr_T)0, 0L);
838 TIME_MSG("diff scrollbinding");
839 }
840#endif
841
842#if defined(WIN3264) && !defined(FEAT_GUI_W32)
843 mch_set_winsize_now(); /* Allow winsize changes from now on */
844#endif
845
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000846#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
847 /* When tab pages were created, may need to update the tab pages line and
848 * scrollbars. This is skipped while creating them. */
849 if (first_tabpage->tp_next != NULL)
850 {
851 out_flush();
852 gui_init_which_components(NULL);
853 gui_update_scrollbars(TRUE);
854 }
855 need_mouse_correct = TRUE;
856#endif
857
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000858 /* If ":startinsert" command used, stuff a dummy command to be able to
859 * call normal_cmd(), which will then start Insert mode. */
860 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000861 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000862
863#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200864 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200865 {
866# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200867# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200868 && !defined(FEAT_GUI_W32)
869 if (gui.in_use)
870 {
871 mch_errmsg(_("netbeans is not supported with this GUI\n"));
872 mch_exit(2);
873 }
874# endif
875# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000876 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200877 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200878 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000879#endif
880
881 TIME_MSG("before starting main loop");
882
883 /*
884 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100885 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000886 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000887
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200888#endif /* NO_VIM_MAIN */
889
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000890 return 0;
891}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000892
893/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200894 * Initialisation shared by main() and some tests.
895 */
896 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200897common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200898{
899
900#ifdef FEAT_MBYTE
901 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
902#endif
903#ifdef FEAT_EVAL
904 eval_init(); /* init global variables */
905#endif
906
907#ifdef __QNXNTO__
908 qnx_init(); /* PhAttach() for clipboard, (and gui) */
909#endif
910
911#ifdef MAC_OS_CLASSIC
912 /* Prepare for possibly starting GUI sometime */
913 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200914 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200915 TIME_MSG("GUI prepared");
916#endif
917
918 /* Init the table of Normal mode commands. */
919 init_normal_cmds();
920
921#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
922 make_version(); /* Construct the long version string. */
923#endif
924
925 /*
926 * Allocate space for the generic buffers (needed for set_init_1() and
927 * EMSG2()).
928 */
929 if ((IObuff = alloc(IOSIZE)) == NULL
930 || (NameBuff = alloc(MAXPATHL)) == NULL)
931 mch_exit(0);
932 TIME_MSG("Allocated generic buffers");
933
934#ifdef NBDEBUG
935 /* Wait a moment for debugging NetBeans. Must be after allocating
936 * NameBuff. */
937 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
938 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
939 TIME_MSG("NetBeans debug wait");
940#endif
941
942#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
943 /*
944 * Setup to use the current locale (for ctype() and many other things).
945 * NOTE: Translated messages with encodings other than latin1 will not
946 * work until set_init_1() has been called!
947 */
948 init_locale();
949 TIME_MSG("locale set");
950#endif
951
952#ifdef FEAT_GUI
953 gui.dofork = TRUE; /* default is to use fork() */
954#endif
955
956 /*
957 * Do a first scan of the arguments in "argv[]":
958 * -display or --display
959 * --server...
960 * --socketid
961 * --windowid
962 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200963 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200964
965#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200966 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200967#endif
968#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
969 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200970 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200971 TIME_MSG("GUI prepared");
972#endif
973
974#ifdef FEAT_CLIPBOARD
975 clip_init(FALSE); /* Initialise clipboard stuff */
976 TIME_MSG("clipboard setup");
977#endif
978
979 /*
980 * Check if we have an interactive window.
981 * On the Amiga: If there is no window, we open one with a newcli command
982 * (needed for :! to * work). mch_check_win() will also handle the -d or
983 * -dev argument.
984 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100985 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200986 TIME_MSG("window checked");
987
988 /*
989 * Allocate the first window and buffer.
990 * Can't do anything without it, exit when it fails.
991 */
992 if (win_alloc_first() == FAIL)
993 mch_exit(0);
994
995 init_yank(); /* init yank buffers */
996
997 alist_init(&global_alist); /* Init the argument list to empty. */
998 global_alist.id = 0;
999
1000 /*
1001 * Set the default values for the options.
1002 * NOTE: Non-latin1 translated messages are working only after this,
1003 * because this is where "has_mbyte" will be set, which is used by
1004 * msg_outtrans_len_attr().
1005 * First find out the home directory, needed to expand "~" in options.
1006 */
1007 init_homedir(); /* find real value of $HOME */
1008 set_init_1();
1009 TIME_MSG("inits 1");
1010
1011#ifdef FEAT_EVAL
1012 set_lang_var(); /* set v:lang and v:ctype */
1013#endif
1014}
1015
1016/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001017 * Return TRUE when the --not-a-term argument was found.
1018 */
1019 int
1020is_not_a_term()
1021{
1022 return params.not_a_term;
1023}
1024
1025/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026 * Main loop: Execute Normal mode commands until exiting Vim.
1027 * Also used to handle commands in the command-line window, until the window
1028 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 * Also used to handle ":visual" command after ":global": execute Normal mode
1030 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001031 */
1032 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001033main_loop(
1034 int cmdwin, /* TRUE when working in the command-line window */
1035 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001036{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001037 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001038 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001039#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001040 /* these are static to avoid a compiler warning */
1041 static linenr_T conceal_old_cursor_line = 0;
1042 static linenr_T conceal_new_cursor_line = 0;
1043 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001044#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001045
1046#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1047 /* Setup to catch a terminating error from the X server. Just ignore
1048 * it, restore the state and continue. This might not always work
1049 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001050 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001051 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001052 {
1053 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001054 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001055 got_int = TRUE;
1056 need_wait_return = FALSE;
1057 global_busy = FALSE;
1058 exmode_active = 0;
1059 skip_redraw = FALSE;
1060 RedrawingDisabled = 0;
1061 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001062 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001063# ifdef FEAT_EVAL
1064 emsg_skip = 0;
1065# endif
1066 emsg_off = 0;
1067# ifdef FEAT_MOUSE
1068 setmouse();
1069# endif
1070 settmode(TMODE_RAW);
1071 starttermcap();
1072 scroll_start();
1073 redraw_later_clear();
1074 }
1075#endif
1076
1077 clear_oparg(&oa);
1078 while (!cmdwin
1079#ifdef FEAT_CMDWIN
1080 || cmdwin_result == 0
1081#endif
1082 )
1083 {
1084 if (stuff_empty())
1085 {
1086 did_check_timestamps = FALSE;
1087 if (need_check_timestamps)
1088 check_timestamps(FALSE);
1089 if (need_wait_return) /* if wait_return still needed ... */
1090 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001091 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001092 {
1093 need_start_insertmode = FALSE;
1094 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1095 /* skip the fileinfo message now, because it would be shown
1096 * after insert mode finishes! */
1097 need_fileinfo = FALSE;
1098 }
1099 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001100
1101 /* Reset "got_int" now that we got back to the main loop. Except when
1102 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1103 * the ":g" command.
1104 * For ":g/pat/vi" we reset "got_int" when used once. When used
1105 * a second time we go back to Ex mode and abort the ":g" command. */
1106 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001107 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001108 if (noexmode && global_busy && !exmode_active && previous_got_int)
1109 {
1110 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1111 * used and keep "got_int" set, so that it aborts ":g". */
1112 exmode_active = EXMODE_NORMAL;
1113 State = NORMAL;
1114 }
1115 else if (!global_busy || !exmode_active)
1116 {
1117 if (!quit_more)
1118 (void)vgetc(); /* flush all buffers */
1119 got_int = FALSE;
1120 }
1121 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001122 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001123 else
1124 previous_got_int = FALSE;
1125
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001126 if (!exmode_active)
1127 msg_scroll = FALSE;
1128 quit_more = FALSE;
1129
1130 /*
1131 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1132 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1133 * update cursor and redraw.
1134 */
1135 if (skip_redraw || exmode_active)
1136 skip_redraw = FALSE;
1137 else if (do_redraw || stuff_empty())
1138 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001139# ifdef FEAT_GUI
1140 /* If ui_breakcheck() was used a resize may have been postponed. */
1141 gui_may_resize_shell();
1142# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001143#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001144 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001145 if (!finish_op && (
1146# ifdef FEAT_AUTOCMD
1147 has_cursormoved()
1148# endif
1149# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1150 ||
1151# endif
1152# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001153 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001154# endif
1155 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001156# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001157 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001158# endif
1159 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001160 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001161# ifdef FEAT_AUTOCMD
1162 if (has_cursormoved())
1163 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1164 FALSE, curbuf);
1165# endif
1166# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001167 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001168 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001169# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001170 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001171# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001172 conceal_new_cursor_line = curwin->w_cursor.lnum;
1173 conceal_update_lines = TRUE;
1174 }
1175# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001176# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001177 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001178# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001179 }
1180#endif
1181
Bram Moolenaar186628f2013-03-19 13:33:23 +01001182#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001183 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001184 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001185 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001186 {
1187 if (last_changedtick_buf == curbuf)
1188 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1189 FALSE, curbuf);
1190 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001191 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001192 }
1193#endif
1194
Bram Moolenaar33aec762006-01-22 23:30:12 +00001195#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1196 /* Scroll-binding for diff mode may have been postponed until
1197 * here. Avoids doing it for every change. */
1198 if (diff_need_scrollbind)
1199 {
1200 check_scrollbind((linenr_T)0, 0L);
1201 diff_need_scrollbind = FALSE;
1202 }
1203#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001204#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001205 /* Include a closed fold completely in the Visual area. */
1206 foldAdjustVisual();
1207#endif
1208#ifdef FEAT_FOLDING
1209 /*
1210 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1211 * contain the cursor.
1212 * When 'foldopen' is "all", open the fold(s) under the cursor.
1213 * This may mark the window for redrawing.
1214 */
1215 if (hasAnyFolding(curwin) && !char_avail())
1216 {
1217 foldCheckClose();
1218 if (fdo_flags & FDO_ALL)
1219 foldOpenCursor();
1220 }
1221#endif
1222
1223 /*
1224 * Before redrawing, make sure w_topline is correct, and w_leftcol
1225 * if lines don't wrap, and w_skipcol if lines wrap.
1226 */
1227 update_topline();
1228 validate_cursor();
1229
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001230 if (VIsual_active)
1231 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001232 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001233 update_screen(0);
1234 else if (redraw_cmdline || clear_cmdline)
1235 showmode();
1236#ifdef FEAT_WINDOWS
1237 redraw_statuslines();
1238#endif
1239#ifdef FEAT_TITLE
1240 if (need_maketitle)
1241 maketitle();
1242#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001243#ifdef FEAT_VIMINFO
1244 curbuf->b_last_used = vim_time();
1245#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001246 /* display message after redraw */
1247 if (keep_msg != NULL)
1248 {
1249 char_u *p;
1250
1251 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001252 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1253 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001254 p = keep_msg;
1255 msg_attr(p, keep_msg_attr);
1256 vim_free(p);
1257 }
1258 if (need_fileinfo) /* show file info after redraw */
1259 {
1260 fileinfo(FALSE, TRUE, FALSE);
1261 need_fileinfo = FALSE;
1262 }
1263
1264 emsg_on_display = FALSE; /* can delete error message now */
1265 did_emsg = FALSE;
1266 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001267 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001268 showruler(FALSE);
1269
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001270# if defined(FEAT_CONCEAL)
1271 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001272 && (conceal_old_cursor_line != conceal_new_cursor_line
1273 || conceal_cursor_line(curwin)
1274 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001275 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001276 if (conceal_old_cursor_line != conceal_new_cursor_line
1277 && conceal_old_cursor_line
1278 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001279 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001280 update_single_line(curwin, conceal_new_cursor_line);
1281 curwin->w_valid &= ~VALID_CROW;
1282 }
1283# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001284 setcursor();
1285 cursor_on();
1286
1287 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001288
1289#ifdef STARTUPTIME
1290 /* Now that we have drawn the first screen all the startup stuff
1291 * has been done, close any file for startup messages. */
1292 if (time_fd != NULL)
1293 {
1294 TIME_MSG("first screen update");
1295 TIME_MSG("--- VIM STARTED ---");
1296 fclose(time_fd);
1297 time_fd = NULL;
1298 }
1299#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001300 }
1301#ifdef FEAT_GUI
1302 if (need_mouse_correct)
1303 gui_mouse_correct();
1304#endif
1305
1306 /*
1307 * Update w_curswant if w_set_curswant has been set.
1308 * Postponed until here to avoid computing w_virtcol too often.
1309 */
1310 update_curswant();
1311
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001312#ifdef FEAT_EVAL
1313 /*
1314 * May perform garbage collection when waiting for a character, but
1315 * only at the very toplevel. Otherwise we may be using a List or
1316 * Dict internally somewhere.
1317 * "may_garbage_collect" is reset in vgetc() which is invoked through
1318 * do_exmode() and normal_cmd().
1319 */
1320 may_garbage_collect = (!cmdwin && !noexmode);
1321#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001322 /*
1323 * If we're invoked as ex, do a round of ex commands.
1324 * Otherwise, get and execute a normal mode command.
1325 */
1326 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001327 {
1328 if (noexmode) /* End of ":global/path/visual" commands */
1329 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001330 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001331 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001332 else
1333 normal_cmd(&oa, TRUE);
1334 }
1335}
1336
1337
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001338#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001339/*
1340 * Exit, but leave behind swap files for modified buffers.
1341 */
1342 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001343getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001344{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001345# if defined(SIGHUP) && defined(SIG_IGN)
1346 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1347 * makes Vim exit and then handling SIGHUP causes various reentrance
1348 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001349 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001350# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001351
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001352 ml_close_notmod(); /* close all not-modified buffers */
1353 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1354 ml_close_all(FALSE); /* close all memfiles, without deleting */
1355 getout(exitval); /* exit Vim properly */
1356}
1357#endif
1358
1359
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001360/*
1361 * Exit properly.
1362 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001363 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001364getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365{
1366#ifdef FEAT_AUTOCMD
1367 buf_T *buf;
1368 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001369 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370#endif
1371
1372 exiting = TRUE;
1373
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001374 /* When running in Ex mode an error causes us to exit with a non-zero exit
1375 * code. POSIX requires this, although it's not 100% clear from the
1376 * standard. */
1377 if (exmode_active)
1378 exitval += ex_exitval;
1379
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001380 /* Position the cursor on the last screen line, below all the text */
1381#ifdef FEAT_GUI
1382 if (!gui.in_use)
1383#endif
1384 windgoto((int)Rows - 1, 0);
1385
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001386#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1387 /* Optionally print hashtable efficiency. */
1388 hash_debug_results();
1389#endif
1390
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001391#ifdef FEAT_GUI
1392 msg_didany = FALSE;
1393#endif
1394
1395#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001396 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001397 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001398 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1399# if defined FEAT_WINDOWS
1400 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001402 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001403 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001404 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001405 if (wp->w_buffer == NULL)
1406 /* Autocmd must have close the buffer already, skip. */
1407 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001408 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001409 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001410 {
1411 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1412 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001413 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001414 /* start all over, autocommands may mess up the lists */
1415 next_tp = first_tabpage;
1416 break;
1417 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001418 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001419 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001420# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001421 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1422 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001423# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001424
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001425 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001426 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001427 if (buf->b_ml.ml_mfp != NULL)
1428 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001429 bufref_T bufref;
1430
1431 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001433 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001434 if (!bufref_valid(&bufref))
1435 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001436 break;
1437 }
1438 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1439 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001440#endif
1441
1442#ifdef FEAT_VIMINFO
1443 if (*p_viminfo != NUL)
1444 /* Write out the registers, history, marks etc, to the viminfo file */
1445 write_viminfo(NULL, FALSE);
1446#endif
1447
1448#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001449 if (get_vim_var_nr(VV_DYING) <= 1)
1450 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001451#endif
1452
Bram Moolenaar05159a02005-02-26 23:04:13 +00001453#ifdef FEAT_PROFILE
1454 profile_dump();
1455#endif
1456
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001457 if (did_emsg
1458#ifdef FEAT_GUI
1459 || (gui.in_use && msg_didany && p_verbose > 0)
1460#endif
1461 )
1462 {
1463 /* give the user a chance to read the (error) message */
1464 no_wait_return = FALSE;
1465 wait_return(FALSE);
1466 }
1467
1468#ifdef FEAT_AUTOCMD
1469 /* Position the cursor again, the autocommands may have moved it */
1470# ifdef FEAT_GUI
1471 if (!gui.in_use)
1472# endif
1473 windgoto((int)Rows - 1, 0);
1474#endif
1475
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001476#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001477 job_stop_on_exit();
1478#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001479#ifdef FEAT_LUA
1480 lua_end();
1481#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001482#ifdef FEAT_MZSCHEME
1483 mzscheme_end();
1484#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001485#ifdef FEAT_TCL
1486 tcl_end();
1487#endif
1488#ifdef FEAT_RUBY
1489 ruby_end();
1490#endif
1491#ifdef FEAT_PYTHON
1492 python_end();
1493#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001494#ifdef FEAT_PYTHON3
1495 python3_end();
1496#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001497#ifdef FEAT_PERL
1498 perl_end();
1499#endif
1500#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1501 iconv_end();
1502#endif
1503#ifdef FEAT_NETBEANS_INTG
1504 netbeans_end();
1505#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001506#ifdef FEAT_CSCOPE
1507 cs_end();
1508#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001509#ifdef FEAT_EVAL
1510 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001511 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001512#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001513#if defined(WIN32) && defined(FEAT_MBYTE)
1514 free_cmd_argsW();
1515#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001516
1517 mch_exit(exitval);
1518}
1519
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001520#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1521/*
1522 * Setup to use the current locale (for ctype() and many other things).
1523 */
1524 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001525init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001526{
1527 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001528
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001529# ifdef FEAT_GUI_GTK
1530 /* Tell Gtk not to change our locale settings. */
1531 gtk_disable_setlocale();
1532# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001533# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1534 /* Make sure strtod() uses a decimal point, not a comma. */
1535 setlocale(LC_NUMERIC, "C");
1536# endif
1537
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001538# ifdef WIN32
1539 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1540 * text while it's expecting text in the current locale. This call avoids
1541 * that. */
1542 setlocale(LC_CTYPE, "C");
1543# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001544
1545# ifdef FEAT_GETTEXT
1546 {
1547 int mustfree = FALSE;
1548 char_u *p;
1549
1550# ifdef DYNAMIC_GETTEXT
1551 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001552 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001553# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001554 /* expand_env() doesn't work yet, because g_chartab[] is not
1555 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001556 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1557 if (p != NULL && *p != NUL)
1558 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001559 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001560 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1561 }
1562 if (mustfree)
1563 vim_free(p);
1564 textdomain(VIMPACKAGE);
1565 }
1566# endif
1567}
1568#endif
1569
1570/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001571 * Get the name of the display, before gui_prepare() removes it from
1572 * argv[]. Used for the xterm-clipboard display.
1573 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001574 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001575 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001576 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001577early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001578{
Bram Moolenaar03005972008-11-20 13:12:36 +00001579#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1580 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001581 int argc = parmp->argc;
1582 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001583 int i;
1584
1585 for (i = 1; i < argc; i++)
1586 {
1587 if (STRCMP(argv[i], "--") == 0)
1588 break;
1589# ifdef FEAT_XCLIPBOARD
1590 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001591# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001592 || STRICMP(argv[i], "--display") == 0
1593# endif
1594 )
1595 {
1596 if (i == argc - 1)
1597 mainerr_arg_missing((char_u *)argv[i]);
1598 xterm_display = argv[++i];
1599 }
1600# endif
1601# ifdef FEAT_CLIENTSERVER
1602 else if (STRICMP(argv[i], "--servername") == 0)
1603 {
1604 if (i == argc - 1)
1605 mainerr_arg_missing((char_u *)argv[i]);
1606 parmp->serverName_arg = (char_u *)argv[++i];
1607 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001608 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001609 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001610 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001611 {
1612 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001613# ifdef FEAT_GUI
1614 if (strstr(argv[i], "-wait") != 0)
1615 /* don't fork() when starting the GUI to edit files ourself */
1616 gui.dofork = FALSE;
1617# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001618 }
1619# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001620
1621# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1622# ifdef FEAT_GUI_W32
1623 else if (STRICMP(argv[i], "--windowid") == 0)
1624# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001625 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001626# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001627 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001628 long_u id;
1629 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001630
1631 if (i == argc - 1)
1632 mainerr_arg_missing((char_u *)argv[i]);
1633 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001634 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001635 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001636 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001637 if (count != 1)
1638 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1639 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001640# ifdef FEAT_GUI_W32
1641 win_socket_id = id;
1642# else
1643 gtk_socket_id = id;
1644# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001645 i++;
1646 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001647# endif
1648# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001649 else if (STRICMP(argv[i], "--echo-wid") == 0)
1650 echo_wid_arg = TRUE;
1651# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001652# ifndef FEAT_NETBEANS_INTG
1653 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001654 {
1655 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1656 mch_exit(2);
1657 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001658# endif
1659
Bram Moolenaar58d98232005-07-23 22:25:46 +00001660 }
1661#endif
1662}
1663
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001664#ifndef NO_VIM_MAIN
1665/*
1666 * Get a (optional) count for a Vim argument.
1667 */
1668 static int
1669get_number_arg(
1670 char_u *p, /* pointer to argument */
1671 int *idx, /* index in argument, is incremented */
1672 int def) /* default value */
1673{
1674 if (vim_isdigit(p[*idx]))
1675 {
1676 def = atoi((char *)&(p[*idx]));
1677 while (vim_isdigit(p[*idx]))
1678 *idx = *idx + 1;
1679 }
1680 return def;
1681}
1682
1683/*
1684 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1685 * If the executable name starts with "r" we disable shell commands.
1686 * If the next character is "e" we run in Easy mode.
1687 * If the next character is "g" we run the GUI version.
1688 * If the next characters are "view" we start in readonly mode.
1689 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1690 * If the next characters are "ex" we start in Ex mode. If it's followed
1691 * by "im" use improved Ex mode.
1692 */
1693 static void
1694parse_command_name(mparm_T *parmp)
1695{
1696 char_u *initstr;
1697
1698 initstr = gettail((char_u *)parmp->argv[0]);
1699
1700#ifdef MACOS_X_UNIX
1701 /* An issue has been seen when launching Vim in such a way that
1702 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1703 * executable or a symbolic link of it. Until this issue is resolved
1704 * we prohibit the GUI from being used.
1705 */
1706 if (STRCMP(initstr, parmp->argv[0]) == 0)
1707 disallow_gui = TRUE;
1708
1709 /* TODO: On MacOS X default to gui if argv[0] ends in:
1710 * /Vim.app/Contents/MacOS/Vim */
1711#endif
1712
1713#ifdef FEAT_EVAL
1714 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001715 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001716#endif
1717
1718 if (TOLOWER_ASC(initstr[0]) == 'r')
1719 {
1720 restricted = TRUE;
1721 ++initstr;
1722 }
1723
1724 /* Use evim mode for "evim" and "egvim", not for "editor". */
1725 if (TOLOWER_ASC(initstr[0]) == 'e'
1726 && (TOLOWER_ASC(initstr[1]) == 'v'
1727 || TOLOWER_ASC(initstr[1]) == 'g'))
1728 {
1729#ifdef FEAT_GUI
1730 gui.starting = TRUE;
1731#endif
1732 parmp->evim_mode = TRUE;
1733 ++initstr;
1734 }
1735
1736 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1737 if (TOLOWER_ASC(initstr[0]) == 'g')
1738 {
1739 main_start_gui();
1740#ifdef FEAT_GUI
1741 ++initstr;
1742#endif
1743 }
1744
1745 if (STRNICMP(initstr, "view", 4) == 0)
1746 {
1747 readonlymode = TRUE;
1748 curbuf->b_p_ro = TRUE;
1749 p_uc = 10000; /* don't update very often */
1750 initstr += 4;
1751 }
1752 else if (STRNICMP(initstr, "vim", 3) == 0)
1753 initstr += 3;
1754
1755 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1756 if (STRICMP(initstr, "diff") == 0)
1757 {
1758#ifdef FEAT_DIFF
1759 parmp->diff_mode = TRUE;
1760#else
1761 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1762 mch_errmsg("\n");
1763 mch_exit(2);
1764#endif
1765 }
1766
1767 if (STRNICMP(initstr, "ex", 2) == 0)
1768 {
1769 if (STRNICMP(initstr + 2, "im", 2) == 0)
1770 exmode_active = EXMODE_VIM;
1771 else
1772 exmode_active = EXMODE_NORMAL;
1773 change_compatible(TRUE); /* set 'compatible' */
1774 }
1775}
1776
Bram Moolenaar58d98232005-07-23 22:25:46 +00001777/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001778 * Scan the command line arguments.
1779 */
1780 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001781command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001782{
1783 int argc = parmp->argc;
1784 char **argv = parmp->argv;
1785 int argv_idx; /* index in argv[n][] */
1786 int had_minmin = FALSE; /* found "--" argument */
1787 int want_argument; /* option argument with argument */
1788 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001789 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001790 long n;
1791
1792 --argc;
1793 ++argv;
1794 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1795 while (argc > 0)
1796 {
1797 /*
1798 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1799 */
1800 if (argv[0][0] == '+' && !had_minmin)
1801 {
1802 if (parmp->n_commands >= MAX_ARG_CMDS)
1803 mainerr(ME_EXTRA_CMD, NULL);
1804 argv_idx = -1; /* skip to next argument */
1805 if (argv[0][1] == NUL)
1806 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1807 else
1808 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1809 }
1810
1811 /*
1812 * Optional argument.
1813 */
1814 else if (argv[0][0] == '-' && !had_minmin)
1815 {
1816 want_argument = FALSE;
1817 c = argv[0][argv_idx++];
1818#ifdef VMS
1819 /*
1820 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1821 * and "-/X" as "-X".
1822 */
1823 if (c == '/')
1824 {
1825 c = argv[0][argv_idx++];
1826 c = TOUPPER_ASC(c);
1827 }
1828 else
1829 c = TOLOWER_ASC(c);
1830#endif
1831 switch (c)
1832 {
1833 case NUL: /* "vim -" read from stdin */
1834 /* "ex -" silent mode */
1835 if (exmode_active)
1836 silent_mode = TRUE;
1837 else
1838 {
1839 if (parmp->edit_type != EDIT_NONE)
1840 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1841 parmp->edit_type = EDIT_STDIN;
1842 read_cmd_fd = 2; /* read from stderr instead of stdin */
1843 }
1844 argv_idx = -1; /* skip to next argument */
1845 break;
1846
1847 case '-': /* "--" don't take any more option arguments */
1848 /* "--help" give help message */
1849 /* "--version" give version message */
1850 /* "--literal" take files literally */
1851 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001852 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001853 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001854 /* "--noplugin[s]" skip plugins */
1855 /* "--cmd <cmd>" execute cmd before vimrc */
1856 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1857 usage();
1858 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1859 {
1860 Columns = 80; /* need to init Columns */
1861 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1862 list_version();
1863 msg_putchar('\n');
1864 msg_didout = FALSE;
1865 mch_exit(0);
1866 }
1867 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1868 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001869#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001870 parmp->literal = TRUE;
1871#endif
1872 }
1873 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1874 {
1875#ifdef FEAT_GUI
1876 gui.dofork = FALSE; /* don't fork() when starting GUI */
1877#endif
1878 }
1879 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1880 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001881 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1882 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001883 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1884 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001885 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1886 {
1887 want_argument = TRUE;
1888 argv_idx += 3;
1889 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001890 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1891 {
1892 want_argument = TRUE;
1893 argv_idx += 11;
1894 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001895#ifdef FEAT_CLIENTSERVER
1896 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1897 ; /* already processed -- no arg */
1898 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1899 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1900 {
1901 /* already processed -- snatch the following arg */
1902 if (argc > 1)
1903 {
1904 --argc;
1905 ++argv;
1906 }
1907 }
1908#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001909#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1910# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001911 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001912# else
1913 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1914# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001915 {
1916 /* already processed -- snatch the following arg */
1917 if (argc > 1)
1918 {
1919 --argc;
1920 ++argv;
1921 }
1922 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001923#endif
1924#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001925 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1926 {
1927 /* already processed, skip */
1928 }
1929#endif
1930 else
1931 {
1932 if (argv[0][argv_idx])
1933 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1934 had_minmin = TRUE;
1935 }
1936 if (!want_argument)
1937 argv_idx = -1; /* skip to next argument */
1938 break;
1939
1940 case 'A': /* "-A" start in Arabic mode */
1941#ifdef FEAT_ARABIC
1942 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1943#else
1944 mch_errmsg(_(e_noarabic));
1945 mch_exit(2);
1946#endif
1947 break;
1948
1949 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001950 /* Needs to be effective before expanding file names, because
1951 * for Win32 this makes us edit a shortcut file itself,
1952 * instead of the file it links to. */
1953 set_options_bin(curbuf->b_p_bin, 1, 0);
1954 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001955 break;
1956
1957 case 'C': /* "-C" Compatible */
1958 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001959 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001960 break;
1961
1962 case 'e': /* "-e" Ex mode */
1963 exmode_active = EXMODE_NORMAL;
1964 break;
1965
1966 case 'E': /* "-E" Improved Ex mode */
1967 exmode_active = EXMODE_VIM;
1968 break;
1969
1970 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1971 window directly, not with newcli */
1972#ifdef FEAT_GUI
1973 gui.dofork = FALSE; /* don't fork() when starting GUI */
1974#endif
1975 break;
1976
1977 case 'g': /* "-g" start GUI */
1978 main_start_gui();
1979 break;
1980
1981 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1982#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001983 p_fkmap = TRUE;
1984 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001985#else
1986 mch_errmsg(_(e_nofarsi));
1987 mch_exit(2);
1988#endif
1989 break;
1990
1991 case 'h': /* "-h" give help message */
1992#ifdef FEAT_GUI_GNOME
1993 /* Tell usage() to exit for "gvim". */
1994 gui.starting = FALSE;
1995#endif
1996 usage();
1997 break;
1998
1999 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2000#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002001 p_hkmap = TRUE;
2002 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002003#else
2004 mch_errmsg(_(e_nohebrew));
2005 mch_exit(2);
2006#endif
2007 break;
2008
2009 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2010#ifdef FEAT_LISP
2011 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2012 p_sm = TRUE;
2013#endif
2014 break;
2015
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002016 case 'M': /* "-M" no changes or writing of files */
2017 reset_modifiable();
2018 /* FALLTHROUGH */
2019
2020 case 'm': /* "-m" no writing of files */
2021 p_write = FALSE;
2022 break;
2023
2024 case 'y': /* "-y" easy mode */
2025#ifdef FEAT_GUI
2026 gui.starting = TRUE; /* start GUI a bit later */
2027#endif
2028 parmp->evim_mode = TRUE;
2029 break;
2030
2031 case 'N': /* "-N" Nocompatible */
2032 change_compatible(FALSE);
2033 break;
2034
2035 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002036#ifdef FEAT_NETBEANS_INTG
2037 /* checking for "-nb", netbeans parameters */
2038 if (argv[0][argv_idx] == 'b')
2039 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002040 netbeansArg = argv[0];
2041 argv_idx = -1; /* skip to next argument */
2042 }
2043 else
2044#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002045 parmp->no_swap_file = TRUE;
2046 break;
2047
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002048 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002049#ifdef TARGET_API_MAC_OSX
2050 /* For some reason on MacOS X, an argument like:
2051 -psn_0_10223617 is passed in when invoke from Finder
2052 or with the 'open' command */
2053 if (argv[0][argv_idx] == 's')
2054 {
2055 argv_idx = -1; /* bypass full -psn */
2056 main_start_gui();
2057 break;
2058 }
2059#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002060#ifdef FEAT_WINDOWS
2061 /* default is 0: open window for each file */
2062 parmp->window_count = get_number_arg((char_u *)argv[0],
2063 &argv_idx, 0);
2064 parmp->window_layout = WIN_TABS;
2065#endif
2066 break;
2067
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002068 case 'o': /* "-o[N]" open N horizontal split windows */
2069#ifdef FEAT_WINDOWS
2070 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002071 parmp->window_count = get_number_arg((char_u *)argv[0],
2072 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002073 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002074#endif
2075 break;
2076
2077 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002078#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002079 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002080 parmp->window_count = get_number_arg((char_u *)argv[0],
2081 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002082 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002083#endif
2084 break;
2085
2086#ifdef FEAT_QUICKFIX
2087 case 'q': /* "-q" QuickFix mode */
2088 if (parmp->edit_type != EDIT_NONE)
2089 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2090 parmp->edit_type = EDIT_QF;
2091 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2092 {
2093 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2094 argv_idx = -1;
2095 }
2096 else if (argc > 1) /* "-q {errorfile}" */
2097 want_argument = TRUE;
2098 break;
2099#endif
2100
2101 case 'R': /* "-R" readonly mode */
2102 readonlymode = TRUE;
2103 curbuf->b_p_ro = TRUE;
2104 p_uc = 10000; /* don't update very often */
2105 break;
2106
2107 case 'r': /* "-r" recovery mode */
2108 case 'L': /* "-L" recovery mode */
2109 recoverymode = 1;
2110 break;
2111
2112 case 's':
2113 if (exmode_active) /* "-s" silent (batch) mode */
2114 silent_mode = TRUE;
2115 else /* "-s {scriptin}" read from script file */
2116 want_argument = TRUE;
2117 break;
2118
2119 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2120 if (parmp->edit_type != EDIT_NONE)
2121 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2122 parmp->edit_type = EDIT_TAG;
2123 if (argv[0][argv_idx]) /* "-t{tag}" */
2124 {
2125 parmp->tagname = (char_u *)argv[0] + argv_idx;
2126 argv_idx = -1;
2127 }
2128 else /* "-t {tag}" */
2129 want_argument = TRUE;
2130 break;
2131
2132#ifdef FEAT_EVAL
2133 case 'D': /* "-D" Debugging */
2134 parmp->use_debug_break_level = 9999;
2135 break;
2136#endif
2137#ifdef FEAT_DIFF
2138 case 'd': /* "-d" 'diff' */
2139# ifdef AMIGA
2140 /* check for "-dev {device}" */
2141 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2142 want_argument = TRUE;
2143 else
2144# endif
2145 parmp->diff_mode = TRUE;
2146 break;
2147#endif
2148 case 'V': /* "-V{N}" Verbose level */
2149 /* default is 10: a little bit verbose */
2150 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2151 if (argv[0][argv_idx] != NUL)
2152 {
2153 set_option_value((char_u *)"verbosefile", 0L,
2154 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002155 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002156 }
2157 break;
2158
2159 case 'v': /* "-v" Vi-mode (as if called "vi") */
2160 exmode_active = 0;
2161#ifdef FEAT_GUI
2162 gui.starting = FALSE; /* don't start GUI */
2163#endif
2164 break;
2165
2166 case 'w': /* "-w{number}" set window height */
2167 /* "-w {scriptout}" write to script */
2168 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2169 {
2170 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2171 set_option_value((char_u *)"window", n, NULL, 0);
2172 break;
2173 }
2174 want_argument = TRUE;
2175 break;
2176
2177#ifdef FEAT_CRYPT
2178 case 'x': /* "-x" encrypted reading/writing of files */
2179 parmp->ask_for_key = TRUE;
2180 break;
2181#endif
2182
2183 case 'X': /* "-X" don't connect to X server */
2184#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2185 x_no_connect = TRUE;
2186#endif
2187 break;
2188
2189 case 'Z': /* "-Z" restricted mode */
2190 restricted = TRUE;
2191 break;
2192
2193 case 'c': /* "-c{command}" or "-c {command}" execute
2194 command */
2195 if (argv[0][argv_idx] != NUL)
2196 {
2197 if (parmp->n_commands >= MAX_ARG_CMDS)
2198 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002199 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2200 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002201 argv_idx = -1;
2202 break;
2203 }
2204 /*FALLTHROUGH*/
2205 case 'S': /* "-S {file}" execute Vim script */
2206 case 'i': /* "-i {viminfo}" use for viminfo */
2207#ifndef FEAT_DIFF
2208 case 'd': /* "-d {device}" device (for Amiga) */
2209#endif
2210 case 'T': /* "-T {terminal}" terminal name */
2211 case 'u': /* "-u {vimrc}" vim inits file */
2212 case 'U': /* "-U {gvimrc}" gvim inits file */
2213 case 'W': /* "-W {scriptout}" overwrite */
2214#ifdef FEAT_GUI_W32
2215 case 'P': /* "-P {parent title}" MDI parent */
2216#endif
2217 want_argument = TRUE;
2218 break;
2219
2220 default:
2221 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2222 }
2223
2224 /*
2225 * Handle option arguments with argument.
2226 */
2227 if (want_argument)
2228 {
2229 /*
2230 * Check for garbage immediately after the option letter.
2231 */
2232 if (argv[0][argv_idx] != NUL)
2233 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2234
2235 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002236 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002237 mainerr_arg_missing((char_u *)argv[0]);
2238 ++argv;
2239 argv_idx = -1;
2240
2241 switch (c)
2242 {
2243 case 'c': /* "-c {command}" execute command */
2244 case 'S': /* "-S {file}" execute Vim script */
2245 if (parmp->n_commands >= MAX_ARG_CMDS)
2246 mainerr(ME_EXTRA_CMD, NULL);
2247 if (c == 'S')
2248 {
2249 char *a;
2250
2251 if (argc < 1)
2252 /* "-S" without argument: use default session file
2253 * name. */
2254 a = SESSION_FILE;
2255 else if (argv[0][0] == '-')
2256 {
2257 /* "-S" followed by another option: use default
2258 * session file name. */
2259 a = SESSION_FILE;
2260 ++argc;
2261 --argv;
2262 }
2263 else
2264 a = argv[0];
2265 p = alloc((unsigned)(STRLEN(a) + 4));
2266 if (p == NULL)
2267 mch_exit(2);
2268 sprintf((char *)p, "so %s", a);
2269 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2270 parmp->commands[parmp->n_commands++] = p;
2271 }
2272 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002273 parmp->commands[parmp->n_commands++] =
2274 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002275 break;
2276
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002277 case '-':
2278 if (argv[-1][2] == 'c')
2279 {
2280 /* "--cmd {command}" execute command */
2281 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2282 mainerr(ME_EXTRA_CMD, NULL);
2283 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002284 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002285 }
2286 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002287 break;
2288
2289 /* case 'd': -d {device} is handled in mch_check_win() for the
2290 * Amiga */
2291
2292#ifdef FEAT_QUICKFIX
2293 case 'q': /* "-q {errorfile}" QuickFix mode */
2294 parmp->use_ef = (char_u *)argv[0];
2295 break;
2296#endif
2297
2298 case 'i': /* "-i {viminfo}" use for viminfo */
2299 use_viminfo = (char_u *)argv[0];
2300 break;
2301
2302 case 's': /* "-s {scriptin}" read from script file */
2303 if (scriptin[0] != NULL)
2304 {
2305scripterror:
2306 mch_errmsg(_("Attempt to open script file again: \""));
2307 mch_errmsg(argv[-1]);
2308 mch_errmsg(" ");
2309 mch_errmsg(argv[0]);
2310 mch_errmsg("\"\n");
2311 mch_exit(2);
2312 }
2313 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2314 {
2315 mch_errmsg(_("Cannot open for reading: \""));
2316 mch_errmsg(argv[0]);
2317 mch_errmsg("\"\n");
2318 mch_exit(2);
2319 }
2320 if (save_typebuf() == FAIL)
2321 mch_exit(2); /* out of memory */
2322 break;
2323
2324 case 't': /* "-t {tag}" */
2325 parmp->tagname = (char_u *)argv[0];
2326 break;
2327
2328 case 'T': /* "-T {terminal}" terminal name */
2329 /*
2330 * The -T term argument is always available and when
2331 * HAVE_TERMLIB is supported it overrides the environment
2332 * variable TERM.
2333 */
2334#ifdef FEAT_GUI
2335 if (term_is_gui((char_u *)argv[0]))
2336 gui.starting = TRUE; /* start GUI a bit later */
2337 else
2338#endif
2339 parmp->term = (char_u *)argv[0];
2340 break;
2341
2342 case 'u': /* "-u {vimrc}" vim inits file */
2343 parmp->use_vimrc = (char_u *)argv[0];
2344 break;
2345
2346 case 'U': /* "-U {gvimrc}" gvim inits file */
2347#ifdef FEAT_GUI
2348 use_gvimrc = (char_u *)argv[0];
2349#endif
2350 break;
2351
2352 case 'w': /* "-w {nr}" 'window' value */
2353 /* "-w {scriptout}" append to script file */
2354 if (vim_isdigit(*((char_u *)argv[0])))
2355 {
2356 argv_idx = 0;
2357 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2358 set_option_value((char_u *)"window", n, NULL, 0);
2359 argv_idx = -1;
2360 break;
2361 }
2362 /*FALLTHROUGH*/
2363 case 'W': /* "-W {scriptout}" overwrite script file */
2364 if (scriptout != NULL)
2365 goto scripterror;
2366 if ((scriptout = mch_fopen(argv[0],
2367 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2368 {
2369 mch_errmsg(_("Cannot open for script output: \""));
2370 mch_errmsg(argv[0]);
2371 mch_errmsg("\"\n");
2372 mch_exit(2);
2373 }
2374 break;
2375
2376#ifdef FEAT_GUI_W32
2377 case 'P': /* "-P {parent title}" MDI parent */
2378 gui_mch_set_parent(argv[0]);
2379 break;
2380#endif
2381 }
2382 }
2383 }
2384
2385 /*
2386 * File name argument.
2387 */
2388 else
2389 {
2390 argv_idx = -1; /* skip to next argument */
2391
2392 /* Check for only one type of editing. */
2393 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2394 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2395 parmp->edit_type = EDIT_FILE;
2396
2397#ifdef MSWIN
2398 /* Remember if the argument was a full path before changing
2399 * slashes to backslashes. */
2400 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2401 parmp->full_path = TRUE;
2402#endif
2403
2404 /* Add the file to the global argument list. */
2405 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2406 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2407 mch_exit(2);
2408#ifdef FEAT_DIFF
2409 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2410 && !mch_isdir(alist_name(&GARGLIST[0])))
2411 {
2412 char_u *r;
2413
2414 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2415 if (r != NULL)
2416 {
2417 vim_free(p);
2418 p = r;
2419 }
2420 }
2421#endif
2422#if defined(__CYGWIN32__) && !defined(WIN32)
2423 /*
2424 * If vim is invoked by non-Cygwin tools, convert away any
2425 * DOS paths, so things like .swp files are created correctly.
2426 * Look for evidence of non-Cygwin paths before we bother.
2427 * This is only for when using the Unix files.
2428 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002429 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002430 {
2431 char posix_path[PATH_MAX];
2432
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002433# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2434 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2435# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002436 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002437# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002438 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002439 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 if (p == NULL)
2441 mch_exit(2);
2442 }
2443#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002444
2445#ifdef USE_FNAME_CASE
2446 /* Make the case of the file name match the actual file. */
2447 fname_case(p, 0);
2448#endif
2449
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002450 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002451#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002452 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002453#else
2454 2 /* add buffer number now and use curbuf */
2455#endif
2456 );
2457
2458#if defined(FEAT_MBYTE) && defined(WIN32)
2459 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460 /* Remember this argument has been added to the argument list.
2461 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002462 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002463# ifdef FEAT_DIFF
2464 parmp->diff_mode
2465# else
2466 FALSE
2467# endif
2468 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469 }
2470#endif
2471 }
2472
2473 /*
2474 * If there are no more letters after the current "-", go to next
2475 * argument. argv_idx is set to -1 when the current argument is to be
2476 * skipped.
2477 */
2478 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2479 {
2480 --argc;
2481 ++argv;
2482 argv_idx = 1;
2483 }
2484 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002485
2486#ifdef FEAT_EVAL
2487 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2488 * one. */
2489 if (parmp->n_commands > 0)
2490 {
2491 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2492 if (p != NULL)
2493 {
2494 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2495 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2496 vim_free(p);
2497 }
2498 }
2499#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002500}
2501
2502/*
2503 * Print a warning if stdout is not a terminal.
2504 * When starting in Ex mode and commands come from a file, set Silent mode.
2505 */
2506 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002507check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508{
2509 int input_isatty; /* is active input a terminal? */
2510
2511 input_isatty = mch_input_isatty();
2512 if (exmode_active)
2513 {
2514 if (!input_isatty)
2515 silent_mode = TRUE;
2516 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002517 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002518#ifdef FEAT_GUI
2519 /* don't want the delay when started from the desktop */
2520 && !gui.starting
2521#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002522 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002523 {
2524#ifdef NBDEBUG
2525 /*
2526 * This shouldn't be necessary. But if I run netbeans with the log
2527 * output coming to the console and XOpenDisplay fails, I get vim
2528 * trying to start with input/output to my console tty. This fills my
2529 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002530 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002531 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002532 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002533 {
2534 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2535 exit(1);
2536 }
2537#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002538#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2539 if (is_cygpty_used())
2540 {
2541 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2542 exit(1);
2543 }
2544#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002545 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2547 if (!input_isatty)
2548 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2549 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002550 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2551 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002552 if (scriptin[0] == NULL)
2553 ui_delay(2000L, TRUE);
2554 TIME_MSG("Warning delay");
2555 }
2556}
2557
2558/*
2559 * Read text from stdin.
2560 */
2561 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002562read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002563{
2564 int i;
2565
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002566#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567 /* When getting the ATTENTION prompt here, use a dialog */
2568 swap_exists_action = SEA_DIALOG;
2569#endif
2570 no_wait_return = TRUE;
2571 i = msg_didany;
2572 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002573 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 no_wait_return = FALSE;
2575 msg_didany = i;
2576 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002577#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002578 check_swap_exists_action();
2579#endif
2580#if !(defined(AMIGA) || defined(MACOS))
2581 /*
2582 * Close stdin and dup it from stderr. Required for GPM to work
2583 * properly, and for running external commands.
2584 * Is there any other system that cannot do this?
2585 */
2586 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002587 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002588#endif
2589}
2590
2591/*
2592 * Create the requested number of windows and edit buffers in them.
2593 * Also does recovery if "recoverymode" set.
2594 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002596create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597{
2598#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002599 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002600 int done = 0;
2601
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002602 /*
2603 * Create the number of windows that was requested.
2604 */
2605 if (parmp->window_count == -1) /* was not set */
2606 parmp->window_count = 1;
2607 if (parmp->window_count == 0)
2608 parmp->window_count = GARGCOUNT;
2609 if (parmp->window_count > 1)
2610 {
2611 /* Don't change the windows if there was a command in .vimrc that
2612 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002613 if (parmp->window_layout == 0)
2614 parmp->window_layout = WIN_HOR;
2615 if (parmp->window_layout == WIN_TABS)
2616 {
2617 parmp->window_count = make_tabpages(parmp->window_count);
2618 TIME_MSG("making tab pages");
2619 }
2620 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621 {
2622 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002623 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002624 TIME_MSG("making windows");
2625 }
2626 else
2627 parmp->window_count = win_count();
2628 }
2629 else
2630 parmp->window_count = 1;
2631#endif
2632
2633 if (recoverymode) /* do recover */
2634 {
2635 msg_scroll = TRUE; /* scroll message up */
2636 ml_recover();
2637 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2638 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002639 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002640 }
2641 else
2642 {
2643 /*
2644 * Open a buffer for windows that don't have one yet.
2645 * Commands in the .vimrc might have loaded a file or split the window.
2646 * Watch out for autocommands that delete a window.
2647 */
2648#ifdef FEAT_AUTOCMD
2649 /*
2650 * Don't execute Win/Buf Enter/Leave autocommands here
2651 */
2652 ++autocmd_no_enter;
2653 ++autocmd_no_leave;
2654#endif
2655#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002656 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002657 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002658 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002659 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002660 {
2661 if (parmp->window_layout == WIN_TABS)
2662 goto_tabpage(1);
2663 else
2664 curwin = firstwin;
2665 }
2666 else if (parmp->window_layout == WIN_TABS)
2667 {
2668 if (curtab->tp_next == NULL)
2669 break;
2670 goto_tabpage(0);
2671 }
2672 else
2673 {
2674 if (curwin->w_next == NULL)
2675 break;
2676 curwin = curwin->w_next;
2677 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002678 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002679#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002680 curbuf = curwin->w_buffer;
2681 if (curbuf->b_ml.ml_mfp == NULL)
2682 {
2683#ifdef FEAT_FOLDING
2684 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2685 if (p_fdls >= 0)
2686 curwin->w_p_fdl = p_fdls;
2687#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002688#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002689 /* When getting the ATTENTION prompt here, use a dialog */
2690 swap_exists_action = SEA_DIALOG;
2691#endif
2692 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002693
2694 /* create memfile, read file */
2695 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002696
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002697#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002698 if (swap_exists_action == SEA_QUIT)
2699 {
2700 if (got_int || only_one_window())
2701 {
2702 /* abort selected or quit and only one window */
2703 did_emsg = FALSE; /* avoid hit-enter prompt */
2704 getout(1);
2705 }
2706 /* We can't close the window, it would disturb what
2707 * happens next. Clear the file name and set the arg
2708 * index to -1 to delete it later. */
2709 setfname(curbuf, NULL, NULL, FALSE);
2710 curwin->w_arg_idx = -1;
2711 swap_exists_action = SEA_NONE;
2712 }
2713 else
2714 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002715#endif
2716#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002717 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718#endif
2719 }
2720#ifdef FEAT_WINDOWS
2721 ui_breakcheck();
2722 if (got_int)
2723 {
2724 (void)vgetc(); /* only break the file loading, not the rest */
2725 break;
2726 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002728#endif
2729#ifdef FEAT_WINDOWS
2730 if (parmp->window_layout == WIN_TABS)
2731 goto_tabpage(1);
2732 else
2733 curwin = firstwin;
2734 curbuf = curwin->w_buffer;
2735#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002736#ifdef FEAT_AUTOCMD
2737 --autocmd_no_enter;
2738 --autocmd_no_leave;
2739#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002740 }
2741}
2742
2743#ifdef FEAT_WINDOWS
2744 /*
2745 * If opened more than one window, start editing files in the other
2746 * windows. make_windows() has already opened the windows.
2747 */
2748 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002749edit_buffers(
2750 mparm_T *parmp,
2751 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752{
2753 int arg_idx; /* index in argument list */
2754 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002755 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002756 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757
2758# ifdef FEAT_AUTOCMD
2759 /*
2760 * Don't execute Win/Buf Enter/Leave autocommands here
2761 */
2762 ++autocmd_no_enter;
2763 ++autocmd_no_leave;
2764# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002765
2766 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2767 if (curwin->w_arg_idx == -1)
2768 {
2769 win_close(curwin, TRUE);
2770 advance = FALSE;
2771 }
2772
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773 arg_idx = 1;
2774 for (i = 1; i < parmp->window_count; ++i)
2775 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002776 if (cwd != NULL)
2777 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002778 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2779 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002780 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002781 ++arg_idx;
2782 win_close(curwin, TRUE);
2783 advance = FALSE;
2784 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002785 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002786
Bram Moolenaar84212822006-11-07 21:59:47 +00002787 if (advance)
2788 {
2789 if (parmp->window_layout == WIN_TABS)
2790 {
2791 if (curtab->tp_next == NULL) /* just checking */
2792 break;
2793 goto_tabpage(0);
2794 }
2795 else
2796 {
2797 if (curwin->w_next == NULL) /* just checking */
2798 break;
2799 win_enter(curwin->w_next, FALSE);
2800 }
2801 }
2802 advance = TRUE;
2803
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002804 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002805 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002806 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2807 {
2808 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002809 /* Edit file from arg list, if there is one. When "Quit" selected
2810 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002811# ifdef HAS_SWAP_EXISTS_ACTION
2812 swap_exists_did_quit = FALSE;
2813# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002814 (void)do_ecmd(0, arg_idx < GARGCOUNT
2815 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002816 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002817# ifdef HAS_SWAP_EXISTS_ACTION
2818 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002819 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002820 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002821 if (got_int || only_one_window())
2822 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002823 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002824 did_emsg = FALSE; /* avoid hit-enter prompt */
2825 getout(1);
2826 }
2827 win_close(curwin, TRUE);
2828 advance = FALSE;
2829 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002830# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002831 if (arg_idx == GARGCOUNT - 1)
2832 arg_had_last = TRUE;
2833 ++arg_idx;
2834 }
2835 ui_breakcheck();
2836 if (got_int)
2837 {
2838 (void)vgetc(); /* only break the file loading, not the rest */
2839 break;
2840 }
2841 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002842
2843 if (parmp->window_layout == WIN_TABS)
2844 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002845# ifdef FEAT_AUTOCMD
2846 --autocmd_no_enter;
2847# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002848
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002849 /* make the first window the current window */
2850 win = firstwin;
2851#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2852 /* Avoid making a preview window the current window. */
2853 while (win->w_p_pvw)
2854 {
2855 win = win->w_next;
2856 if (win == NULL)
2857 {
2858 win = firstwin;
2859 break;
2860 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002861 }
2862#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002863 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002864
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002865# ifdef FEAT_AUTOCMD
2866 --autocmd_no_leave;
2867# endif
2868 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002869 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002870 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2871}
2872#endif /* FEAT_WINDOWS */
2873
2874/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002875 * Execute the commands from --cmd arguments "cmds[cnt]".
2876 */
2877 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002878exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002879{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002880 char_u **cmds = parmp->pre_commands;
2881 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002882 int i;
2883
2884 if (cnt > 0)
2885 {
2886 curwin->w_cursor.lnum = 0; /* just in case.. */
2887 sourcing_name = (char_u *)_("pre-vimrc command line");
2888# ifdef FEAT_EVAL
2889 current_SID = SID_CMDARG;
2890# endif
2891 for (i = 0; i < cnt; ++i)
2892 do_cmdline_cmd(cmds[i]);
2893 sourcing_name = NULL;
2894# ifdef FEAT_EVAL
2895 current_SID = 0;
2896# endif
2897 TIME_MSG("--cmd commands");
2898 }
2899}
2900
2901/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002902 * Execute "+", "-c" and "-S" arguments.
2903 */
2904 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002905exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002906{
2907 int i;
2908
2909 /*
2910 * We start commands on line 0, make "vim +/pat file" match a
2911 * pattern on line 1. But don't move the cursor when an autocommand
2912 * with g`" was used.
2913 */
2914 msg_scroll = TRUE;
2915 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2916 curwin->w_cursor.lnum = 0;
2917 sourcing_name = (char_u *)"command line";
2918#ifdef FEAT_EVAL
2919 current_SID = SID_CARG;
2920#endif
2921 for (i = 0; i < parmp->n_commands; ++i)
2922 {
2923 do_cmdline_cmd(parmp->commands[i]);
2924 if (parmp->cmds_tofree[i])
2925 vim_free(parmp->commands[i]);
2926 }
2927 sourcing_name = NULL;
2928#ifdef FEAT_EVAL
2929 current_SID = 0;
2930#endif
2931 if (curwin->w_cursor.lnum == 0)
2932 curwin->w_cursor.lnum = 1;
2933
2934 if (!exmode_active)
2935 msg_scroll = FALSE;
2936
2937#ifdef FEAT_QUICKFIX
2938 /* When started with "-q errorfile" jump to first error again. */
2939 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002940 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002941#endif
2942 TIME_MSG("executing command arguments");
2943}
2944
2945/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002946 * Source startup scripts.
2947 */
2948 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002949source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002950{
2951 int i;
2952
2953 /*
2954 * For "evim" source evim.vim first of all, so that the user can overrule
2955 * any things he doesn't like.
2956 */
2957 if (parmp->evim_mode)
2958 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002959 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002960 TIME_MSG("source evim file");
2961 }
2962
2963 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002964 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002965 * nothing else.
2966 */
2967 if (parmp->use_vimrc != NULL)
2968 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002969 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2970 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002971 {
2972#ifdef FEAT_GUI
2973 if (use_gvimrc == NULL) /* don't load gvimrc either */
2974 use_gvimrc = parmp->use_vimrc;
2975#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002976 }
2977 else
2978 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002979 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002980 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2981 }
2982 }
2983 else if (!silent_mode)
2984 {
2985#ifdef AMIGA
2986 struct Process *proc = (struct Process *)FindTask(0L);
2987 APTR save_winptr = proc->pr_WindowPtr;
2988
2989 /* Avoid a requester here for a volume that doesn't exist. */
2990 proc->pr_WindowPtr = (APTR)-1L;
2991#endif
2992
2993 /*
2994 * Get system wide defaults, if the file name is defined.
2995 */
2996#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002997 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002998#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002999#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003000 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003001#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003002
3003 /*
3004 * Try to read initialization commands from the following places:
3005 * - environment variable VIMINIT
3006 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3007 * - second user vimrc file ($VIM/.vimrc for Dos)
3008 * - environment variable EXINIT
3009 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3010 * - second user exrc file ($VIM/.exrc for Dos)
3011 * The first that exists is used, the rest is ignored.
3012 */
3013 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3014 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003015 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003016#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003017 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3018 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003019#endif
3020#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003021 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3022 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003023#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003024#ifdef USR_VIMRC_FILE4
3025 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3026 DOSO_VIMRC) == FAIL
3027#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003028 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003029 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003030#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003031 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003032#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003033 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003034 {
3035 /* When no .vimrc file was found: source defaults.vim. */
3036 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003037 }
3038 }
3039
3040 /*
3041 * Read initialization commands from ".vimrc" or ".exrc" in current
3042 * directory. This is only done if the 'exrc' option is set.
3043 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003044 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003045 * option has been reset in environment of global ".exrc" or ".vimrc".
3046 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3047 * SYS_VIMRC_FILE.
3048 */
3049 if (p_exrc)
3050 {
3051#if defined(UNIX) || defined(VMS)
3052 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3053 if (!file_owned(VIMRC_FILE))
3054#endif
3055 secure = p_secure;
3056
3057 i = FAIL;
3058 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3059 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3060#ifdef USR_VIMRC_FILE2
3061 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3062 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3063#endif
3064#ifdef USR_VIMRC_FILE3
3065 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3066 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3067#endif
3068#ifdef SYS_VIMRC_FILE
3069 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3070 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3071#endif
3072 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003073 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003074
3075 if (i == FAIL)
3076 {
3077#if defined(UNIX) || defined(VMS)
3078 /* if ".exrc" is not owned by user set 'secure' mode */
3079 if (!file_owned(EXRC_FILE))
3080 secure = p_secure;
3081 else
3082 secure = 0;
3083#endif
3084 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3085 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3086#ifdef USR_EXRC_FILE2
3087 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3088 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3089#endif
3090 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003091 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003092 }
3093 }
3094 if (secure == 2)
3095 need_wait_return = TRUE;
3096 secure = 0;
3097#ifdef AMIGA
3098 proc->pr_WindowPtr = save_winptr;
3099#endif
3100 }
3101 TIME_MSG("sourcing vimrc file(s)");
3102}
3103
3104/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003105 * Setup to start using the GUI. Exit with an error when not available.
3106 */
3107 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003108main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003109{
3110#ifdef FEAT_GUI
3111 gui.starting = TRUE; /* start GUI a bit later */
3112#else
3113 mch_errmsg(_(e_nogvim));
3114 mch_errmsg("\n");
3115 mch_exit(2);
3116#endif
3117}
3118
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003119#endif /* NO_VIM_MAIN */
3120
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003121/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003122 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003123 * Returns FAIL if the environment variable was not executed, OK otherwise.
3124 */
3125 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003126process_env(
3127 char_u *env,
3128 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003129{
3130 char_u *initstr;
3131 char_u *save_sourcing_name;
3132 linenr_T save_sourcing_lnum;
3133#ifdef FEAT_EVAL
3134 scid_T save_sid;
3135#endif
3136
3137 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3138 {
3139 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003140 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003141 save_sourcing_name = sourcing_name;
3142 save_sourcing_lnum = sourcing_lnum;
3143 sourcing_name = env;
3144 sourcing_lnum = 0;
3145#ifdef FEAT_EVAL
3146 save_sid = current_SID;
3147 current_SID = SID_ENV;
3148#endif
3149 do_cmdline_cmd(initstr);
3150 sourcing_name = save_sourcing_name;
3151 sourcing_lnum = save_sourcing_lnum;
3152#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003153 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003154#endif
3155 return OK;
3156 }
3157 return FAIL;
3158}
3159
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003160#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003161/*
3162 * Return TRUE if we are certain the user owns the file "fname".
3163 * Used for ".vimrc" and ".exrc".
3164 * Use both stat() and lstat() for extra security.
3165 */
3166 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003167file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003168{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003169 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003170# ifdef UNIX
3171 uid_t uid = getuid();
3172# else /* VMS */
3173 uid_t uid = ((getgid() << 16) | getuid());
3174# endif
3175
3176 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3177# ifdef HAVE_LSTAT
3178 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3179# endif
3180 );
3181}
3182#endif
3183
3184/*
3185 * Give an error message main_errors["n"] and exit.
3186 */
3187 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003188mainerr(
3189 int n, /* one of the ME_ defines */
3190 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003191{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003192#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003193 reset_signals(); /* kill us with CTRL-C here, if you like */
3194#endif
3195
3196 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003197 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003198 mch_errmsg(_(main_errors[n]));
3199 if (str != NULL)
3200 {
3201 mch_errmsg(": \"");
3202 mch_errmsg((char *)str);
3203 mch_errmsg("\"");
3204 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003205 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003206
3207 mch_exit(1);
3208}
3209
3210 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003211mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003212{
3213 mainerr(ME_ARG_MISSING, str);
3214}
3215
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003216#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003217/*
3218 * print a message with three spaces prepended and '\n' appended.
3219 */
3220 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003221main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003222{
3223 mch_msg(" ");
3224 mch_msg(s);
3225 mch_msg("\n");
3226}
3227
3228/*
3229 * Print messages for "vim -h" or "vim --help" and exit.
3230 */
3231 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003232usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003233{
3234 int i;
3235 static char *(use[]) =
3236 {
3237 N_("[file ..] edit specified file(s)"),
3238 N_("- read text from stdin"),
3239 N_("-t tag edit file where tag is defined"),
3240#ifdef FEAT_QUICKFIX
3241 N_("-q [errorfile] edit file with first error")
3242#endif
3243 };
3244
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003245#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003246 reset_signals(); /* kill us with CTRL-C here, if you like */
3247#endif
3248
3249 mch_msg(longVersion);
3250 mch_msg(_("\n\nusage:"));
3251 for (i = 0; ; ++i)
3252 {
3253 mch_msg(_(" vim [arguments] "));
3254 mch_msg(_(use[i]));
3255 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3256 break;
3257 mch_msg(_("\n or:"));
3258 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003259#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003260 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003261#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003262
3263 mch_msg(_("\n\nArguments:\n"));
3264 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003265#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003266 main_msg(_("--literal\t\tDon't expand wildcards"));
3267#endif
3268#ifdef FEAT_OLE
3269 main_msg(_("-register\t\tRegister this gvim for OLE"));
3270 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3271#endif
3272#ifdef FEAT_GUI
3273 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3274 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3275#endif
3276 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3277 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003278 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003279 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3280#ifdef FEAT_DIFF
3281 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3282#endif
3283 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3284 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3285 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3286 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3287 main_msg(_("-M\t\t\tModifications in text not allowed"));
3288 main_msg(_("-b\t\t\tBinary mode"));
3289#ifdef FEAT_LISP
3290 main_msg(_("-l\t\t\tLisp mode"));
3291#endif
3292 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3293 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003294 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3295#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003296 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003297#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3299 main_msg(_("-r\t\t\tList swap files and exit"));
3300 main_msg(_("-r (with file name)\tRecover crashed session"));
3301 main_msg(_("-L\t\t\tSame as -r"));
3302#ifdef AMIGA
3303 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3304 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3305#endif
3306#ifdef FEAT_ARABIC
3307 main_msg(_("-A\t\t\tstart in Arabic mode"));
3308#endif
3309#ifdef FEAT_RIGHTLEFT
3310 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3311#endif
3312#ifdef FEAT_FKMAP
3313 main_msg(_("-F\t\t\tStart in Farsi mode"));
3314#endif
3315 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003316 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003317 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3319#ifdef FEAT_GUI
3320 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3321#endif
3322 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003323#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003324 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003325 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3326 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003327#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 main_msg(_("+\t\t\tStart at end of file"));
3329 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3332 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3333 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3334 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3335 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3336#ifdef FEAT_CRYPT
3337 main_msg(_("-x\t\t\tEdit encrypted files"));
3338#endif
3339#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3340# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3341 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3342# endif
3343 main_msg(_("-X\t\t\tDo not connect to X server"));
3344#endif
3345#ifdef FEAT_CLIENTSERVER
3346 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3347 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3348 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3349 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003350# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003351 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003352# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3354 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3355 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3356 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3357#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003358#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003359 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003360#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003361#ifdef FEAT_VIMINFO
3362 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3363#endif
3364 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3365 main_msg(_("--version\t\tPrint version information and exit"));
3366
3367#ifdef FEAT_GUI_X11
3368# ifdef FEAT_GUI_MOTIF
3369 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3370# else
3371# ifdef FEAT_GUI_ATHENA
3372# ifdef FEAT_GUI_NEXTAW
3373 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3374# else
3375 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3376# endif
3377# endif
3378# endif
3379 main_msg(_("-display <display>\tRun vim on <display>"));
3380 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003381 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3382 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3383 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3384 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3385 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3386 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3387 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3388 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3389# ifdef FEAT_GUI_ATHENA
3390 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3391# endif
3392 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3393 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3394 main_msg(_("-xrm <resource>\tSet the specified resource"));
3395#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396#ifdef FEAT_GUI_GTK
3397 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3398 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3399 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3400 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3401 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003404 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406#ifdef FEAT_GUI_W32
3407 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003408 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409#endif
3410
3411#ifdef FEAT_GUI_GNOME
3412 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3413 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003414 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003416 gui.dofork = FALSE;
3417 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418 else
3419#endif
3420 mch_exit(0);
3421}
3422
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003423#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424/*
3425 * Check the result of the ATTENTION dialog:
3426 * When "Quit" selected, exit Vim.
3427 * When "Recover" selected, recover the file.
3428 */
3429 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003430check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003431{
3432 if (swap_exists_action == SEA_QUIT)
3433 getout(1);
3434 handle_swap_exists(NULL);
3435}
3436#endif
3437
Bram Moolenaar08cab962017-03-04 14:37:18 +01003438#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003439
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003440#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003441static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442
3443static struct timeval prev_timeval;
3444
Bram Moolenaar3f269672009-11-03 11:11:11 +00003445# ifdef WIN3264
3446/*
3447 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3448 */
3449 static int
3450gettimeofday(struct timeval *tv, char *dummy)
3451{
3452 long t = clock();
3453 tv->tv_sec = t / CLOCKS_PER_SEC;
3454 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3455 return 0;
3456}
3457# endif
3458
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003459/*
3460 * Save the previous time before doing something that could nest.
3461 * set "*tv_rel" to the time elapsed so far.
3462 */
3463 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003464time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003465{
3466 *((struct timeval *)tv_rel) = prev_timeval;
3467 gettimeofday(&prev_timeval, NULL);
3468 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3469 - ((struct timeval *)tv_rel)->tv_usec;
3470 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3471 - ((struct timeval *)tv_rel)->tv_sec;
3472 if (((struct timeval *)tv_rel)->tv_usec < 0)
3473 {
3474 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3475 --((struct timeval *)tv_rel)->tv_sec;
3476 }
3477 *(struct timeval *)tv_start = prev_timeval;
3478}
3479
3480/*
3481 * Compute the previous time after doing something that could nest.
3482 * Subtract "*tp" from prev_timeval;
3483 * Note: The arguments are (void *) to avoid trouble with systems that don't
3484 * have struct timeval.
3485 */
3486 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003487time_pop(
3488 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003489{
3490 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3491 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3492 if (prev_timeval.tv_usec < 0)
3493 {
3494 prev_timeval.tv_usec += 1000000;
3495 --prev_timeval.tv_sec;
3496 }
3497}
3498
3499 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003500time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003501{
3502 long usec;
3503 long msec;
3504
3505 usec = now->tv_usec - then->tv_usec;
3506 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3507 usec = usec % 1000L;
3508 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3509}
3510
3511 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003512time_msg(
3513 char *mesg,
3514 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003515 (struct timeval *) */
3516{
3517 static struct timeval start;
3518 struct timeval now;
3519
3520 if (time_fd != NULL)
3521 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003522 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523 {
3524 gettimeofday(&start, NULL);
3525 prev_timeval = start;
3526 fprintf(time_fd, "\n\ntimes in msec\n");
3527 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3528 fprintf(time_fd, " clock elapsed: other lines\n\n");
3529 }
3530 gettimeofday(&now, NULL);
3531 time_diff(&start, &now);
3532 if (((struct timeval *)tv_start) != NULL)
3533 {
3534 fprintf(time_fd, " ");
3535 time_diff(((struct timeval *)tv_start), &now);
3536 }
3537 fprintf(time_fd, " ");
3538 time_diff(&prev_timeval, &now);
3539 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003540 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541 }
3542}
3543
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003544#endif
3545
Bram Moolenaar595297d2017-03-04 19:11:12 +01003546#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003547 static void
3548set_progpath(char_u *argv0)
3549{
3550 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003551
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003552# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003553 char buf[PATH_MAX + 1];
3554 ssize_t len;
3555
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003556 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003557 if (len > 0)
3558 {
3559 buf[len] = NUL;
3560 val = (char_u *)buf;
3561 }
3562# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003563 /* A relative path containing a "/" will become invalid when using ":cd",
3564 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003565 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3566 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003567# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003568 char_u *path = NULL;
3569
3570 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3571 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003572# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003573 char_u buf[MAXPATHL];
3574
Bram Moolenaar43663192017-03-05 14:29:12 +01003575 if (!mch_isFullName(argv0))
3576 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003577 if (gettail(argv0) != argv0
3578 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3579 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003580 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003581# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003582# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003583
Bram Moolenaar08cab962017-03-04 14:37:18 +01003584 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003585
Bram Moolenaar066029e2017-03-05 15:19:32 +01003586# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003587 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003588# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003589}
3590
3591#endif /* NO_VIM_MAIN */
3592
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003593#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003594
3595/*
3596 * Common code for the X command server and the Win32 command server.
3597 */
3598
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003599static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003600
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003601/*
3602 * Do the client-server stuff, unless "--servername ''" was used.
3603 */
3604 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003605exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003606{
3607 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3608 {
3609# ifdef WIN32
3610 /* Initialise the client/server messaging infrastructure. */
3611 serverInitMessaging();
3612# endif
3613
3614 /*
3615 * When a command server argument was found, execute it. This may
3616 * exit Vim when it was successful. Otherwise it's executed further
3617 * on. Remember the encoding used here in "serverStrEnc".
3618 */
3619 if (parmp->serverArg)
3620 {
3621 cmdsrv_main(&parmp->argc, parmp->argv,
3622 parmp->serverName_arg, &parmp->serverStr);
3623# ifdef FEAT_MBYTE
3624 parmp->serverStrEnc = vim_strsave(p_enc);
3625# endif
3626 }
3627
3628 /* If we're still running, get the name to register ourselves.
3629 * On Win32 can register right now, for X11 need to setup the
3630 * clipboard first, it's further down. */
3631 parmp->servername = serverMakeName(parmp->serverName_arg,
3632 parmp->argv[0]);
3633# ifdef WIN32
3634 if (parmp->servername != NULL)
3635 {
3636 serverSetName(parmp->servername);
3637 vim_free(parmp->servername);
3638 }
3639# endif
3640 }
3641}
3642
3643/*
3644 * Prepare for running as a Vim server.
3645 */
3646 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003647prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003648{
3649# if defined(FEAT_X11)
3650 /*
3651 * Register for remote command execution with :serversend and --remote
3652 * unless there was a -X or a --servername '' on the command line.
3653 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003654 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003655 */
3656 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3657# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003658 (gui.in_use
3659# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003660 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003661# endif
3662 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003663# endif
3664 parmp->serverName_arg != NULL))
3665 {
3666 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3667 vim_free(parmp->servername);
3668 TIME_MSG("register server name");
3669 }
3670 else
3671 serverDelayedStartName = parmp->servername;
3672# endif
3673
3674 /*
3675 * Execute command ourselves if we're here because the send failed (or
3676 * else we would have exited above).
3677 */
3678 if (parmp->serverStr != NULL)
3679 {
3680 char_u *p;
3681
3682 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3683 parmp->serverStr, &p));
3684 vim_free(p);
3685 }
3686}
3687
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003688 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003689cmdsrv_main(
3690 int *argc,
3691 char **argv,
3692 char_u *serverName_arg,
3693 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003694{
3695 char_u *res;
3696 int i;
3697 char_u *sname;
3698 int ret;
3699 int didone = FALSE;
3700 int exiterr = 0;
3701 char **newArgV = argv + 1;
3702 int newArgC = 1,
3703 Argc = *argc;
3704 int argtype;
3705#define ARGTYPE_OTHER 0
3706#define ARGTYPE_EDIT 1
3707#define ARGTYPE_EDIT_WAIT 2
3708#define ARGTYPE_SEND 3
3709 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003710 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003711# ifndef FEAT_X11
3712 HWND srv;
3713# else
3714 Window srv;
3715
3716 setup_term_clip();
3717# endif
3718
3719 sname = serverMakeName(serverName_arg, argv[0]);
3720 if (sname == NULL)
3721 return;
3722
3723 /*
3724 * Execute the command server related arguments and remove them
3725 * from the argc/argv array; We may have to return into main()
3726 */
3727 for (i = 1; i < Argc; i++)
3728 {
3729 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003730 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003731 {
3732 for (; i < *argc; i++)
3733 {
3734 *newArgV++ = argv[i];
3735 newArgC++;
3736 }
3737 break;
3738 }
3739
Bram Moolenaareb94e552006-03-11 21:35:11 +00003740 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003741 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003742 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3743 {
3744 char *p = argv[i] + 8;
3745
3746 argtype = ARGTYPE_EDIT;
3747 while (*p != NUL)
3748 {
3749 if (STRNICMP(p, "-wait", 5) == 0)
3750 {
3751 argtype = ARGTYPE_EDIT_WAIT;
3752 p += 5;
3753 }
3754 else if (STRNICMP(p, "-silent", 7) == 0)
3755 {
3756 silent = TRUE;
3757 p += 7;
3758 }
3759 else if (STRNICMP(p, "-tab", 4) == 0)
3760 {
3761 tabs = TRUE;
3762 p += 4;
3763 }
3764 else
3765 {
3766 argtype = ARGTYPE_OTHER;
3767 break;
3768 }
3769 }
3770 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003771 else
3772 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003773
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003774 if (argtype != ARGTYPE_OTHER)
3775 {
3776 if (i == *argc - 1)
3777 mainerr_arg_missing((char_u *)argv[i]);
3778 if (argtype == ARGTYPE_SEND)
3779 {
3780 *serverStr = (char_u *)argv[i + 1];
3781 i++;
3782 }
3783 else
3784 {
3785 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003786 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003787 if (*serverStr == NULL)
3788 {
3789 /* Probably out of memory, exit. */
3790 didone = TRUE;
3791 exiterr = 1;
3792 break;
3793 }
3794 Argc = i;
3795 }
3796# ifdef FEAT_X11
3797 if (xterm_dpy == NULL)
3798 {
3799 mch_errmsg(_("No display"));
3800 ret = -1;
3801 }
3802 else
3803 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003804 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003805# else
3806 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003807 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003808# endif
3809 if (ret < 0)
3810 {
3811 if (argtype == ARGTYPE_SEND)
3812 {
3813 /* Failed to send, abort. */
3814 mch_errmsg(_(": Send failed.\n"));
3815 didone = TRUE;
3816 exiterr = 1;
3817 }
3818 else if (!silent)
3819 /* Let vim start normally. */
3820 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3821 break;
3822 }
3823
3824# ifdef FEAT_GUI_W32
3825 /* Guess that when the server name starts with "g" it's a GUI
3826 * server, which we can bring to the foreground here.
3827 * Foreground() in the server doesn't work very well. */
3828 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3829 SetForegroundWindow(srv);
3830# endif
3831
3832 /*
3833 * For --remote-wait: Wait until the server did edit each
3834 * file. Also detect that the server no longer runs.
3835 */
3836 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3837 {
3838 int numFiles = *argc - i - 1;
3839 int j;
3840 char_u *done = alloc(numFiles);
3841 char_u *p;
3842# ifdef FEAT_GUI_W32
3843 NOTIFYICONDATA ni;
3844 int count = 0;
3845 extern HWND message_window;
3846# endif
3847
3848 if (numFiles > 0 && argv[i + 1][0] == '+')
3849 /* Skip "+cmd" argument, don't wait for it to be edited. */
3850 --numFiles;
3851
3852# ifdef FEAT_GUI_W32
3853 ni.cbSize = sizeof(ni);
3854 ni.hWnd = message_window;
3855 ni.uID = 0;
3856 ni.uFlags = NIF_ICON|NIF_TIP;
3857 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3858 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3859 Shell_NotifyIcon(NIM_ADD, &ni);
3860# endif
3861
3862 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003863 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003864 while (memchr(done, 0, numFiles) != NULL)
3865 {
3866# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003867 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003868 if (p == NULL)
3869 break;
3870# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003871 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003872 break;
3873# endif
3874 j = atoi((char *)p);
3875 if (j >= 0 && j < numFiles)
3876 {
3877# ifdef FEAT_GUI_W32
3878 ++count;
3879 sprintf(ni.szTip, _("%d of %d edited"),
3880 count, numFiles);
3881 Shell_NotifyIcon(NIM_MODIFY, &ni);
3882# endif
3883 done[j] = 1;
3884 }
3885 }
3886# ifdef FEAT_GUI_W32
3887 Shell_NotifyIcon(NIM_DELETE, &ni);
3888# endif
3889 }
3890 }
3891 else if (STRICMP(argv[i], "--remote-expr") == 0)
3892 {
3893 if (i == *argc - 1)
3894 mainerr_arg_missing((char_u *)argv[i]);
3895# ifdef WIN32
3896 /* Win32 always works? */
3897 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003898 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003899# else
3900 if (xterm_dpy == NULL)
3901 mch_errmsg(_("No display: Send expression failed.\n"));
3902 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003903 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003904# endif
3905 {
3906 if (res != NULL && *res != NUL)
3907 {
3908 /* Output error from remote */
3909 mch_errmsg((char *)res);
3910 vim_free(res);
3911 res = NULL;
3912 }
3913 mch_errmsg(_(": Send expression failed.\n"));
3914 }
3915 }
3916 else if (STRICMP(argv[i], "--serverlist") == 0)
3917 {
3918# ifdef WIN32
3919 /* Win32 always works? */
3920 res = serverGetVimNames();
3921# else
3922 if (xterm_dpy != NULL)
3923 res = serverGetVimNames(xterm_dpy);
3924# endif
3925 if (called_emsg)
3926 mch_errmsg("\n");
3927 }
3928 else if (STRICMP(argv[i], "--servername") == 0)
3929 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003930 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003931 i++;
3932 continue;
3933 }
3934 else
3935 {
3936 *newArgV++ = argv[i];
3937 newArgC++;
3938 continue;
3939 }
3940 didone = TRUE;
3941 if (res != NULL && *res != NUL)
3942 {
3943 mch_msg((char *)res);
3944 if (res[STRLEN(res) - 1] != '\n')
3945 mch_msg("\n");
3946 }
3947 vim_free(res);
3948 }
3949
3950 if (didone)
3951 {
3952 display_errors(); /* display any collected messages */
3953 exit(exiterr); /* Mission accomplished - get out */
3954 }
3955
3956 /* Return back into main() */
3957 *argc = newArgC;
3958 vim_free(sname);
3959}
3960
3961/*
3962 * Build a ":drop" command to send to a Vim server.
3963 */
3964 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003965build_drop_cmd(
3966 int filec,
3967 char **filev,
3968 int tabs, /* Use ":tab drop" instead of ":drop". */
3969 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003970{
3971 garray_T ga;
3972 int i;
3973 char_u *inicmd = NULL;
3974 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003975 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003976 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003977
3978 if (filec > 0 && filev[0][0] == '+')
3979 {
3980 inicmd = (char_u *)filev[0] + 1;
3981 filev++;
3982 filec--;
3983 }
3984 /* Check if we have at least one argument. */
3985 if (filec <= 0)
3986 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003987
3988 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003989 cwd = alloc(MAXPATHL);
3990 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003991 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003992 if (mch_dirname(cwd, MAXPATHL) != OK)
3993 {
3994 vim_free(cwd);
3995 return NULL;
3996 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003997 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003998#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003999 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004000#else
4001 PATH_ESC_CHARS,
4002#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004003 '\\', TRUE);
4004 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004005 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004006 return NULL;
4007 ga_init2(&ga, 1, 100);
4008 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004009 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004010
4011 /* Call inputsave() so that a prompt for an encryption key works. */
4012 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4013 if (tabs)
4014 ga_concat(&ga, (char_u *)"tab ");
4015 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004016 for (i = 0; i < filec; i++)
4017 {
4018 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004019 * do it again in the Vim server. On MS-Windows only escape
4020 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004021 p = vim_strsave_escaped((char_u *)filev[i],
4022#ifdef UNIX
4023 PATH_ESC_CHARS
4024#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004025 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004026#endif
4027 );
4028 if (p == NULL)
4029 {
4030 vim_free(ga.ga_data);
4031 return NULL;
4032 }
4033 ga_concat(&ga, (char_u *)" ");
4034 ga_concat(&ga, p);
4035 vim_free(p);
4036 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004037 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4038
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004039 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4040 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004041 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4042
4043 /* Switch back to the correct current directory (prior to temporary path
4044 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004045 * correct after the :drop command. With line breaks and spaces:
4046 * if !exists('+acd') || !&acd
4047 * if haslocaldir()
4048 * cd -
4049 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004050 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004051 * cd -
4052 * endif
4053 * endif
4054 */
4055 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004056 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004057 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004058 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004059 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004060
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004061 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004062 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4063 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004064 if (inicmd != NULL)
4065 {
4066 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4067 * the following commands to be inserted as text. Use a "|",
4068 * hopefully "inicmd" does allow this... */
4069 ga_concat(&ga, inicmd);
4070 ga_concat(&ga, (char_u *)"|");
4071 }
4072 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4073 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004074 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004075 ga_append(&ga, NUL);
4076 return ga.ga_data;
4077}
4078
4079/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004080 * Make our basic server name: use the specified "arg" if given, otherwise use
4081 * the tail of the command "cmd" we were started with.
4082 * Return the name in allocated memory. This doesn't include a serial number.
4083 */
4084 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004085serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004086{
4087 char_u *p;
4088
4089 if (arg != NULL && *arg != NUL)
4090 p = vim_strsave_up(arg);
4091 else
4092 {
4093 p = vim_strsave_up(gettail((char_u *)cmd));
4094 /* Remove .exe or .bat from the name. */
4095 if (p != NULL && vim_strchr(p, '.') != NULL)
4096 *vim_strchr(p, '.') = NUL;
4097 }
4098 return p;
4099}
4100#endif /* FEAT_CLIENTSERVER */
4101
4102#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4103/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004104 * Replace termcodes such as <CR> and insert as key presses if there is room.
4105 */
4106 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004107server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004108{
4109 char_u *ptr = NULL;
4110 char_u *cpo_save = p_cpo;
4111
4112 /* Set 'cpoptions' the way we want it.
4113 * B set - backslashes are *not* treated specially
4114 * k set - keycodes are *not* reverse-engineered
4115 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004116 * The last but one parameter of replace_termcodes() is TRUE so that the
4117 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004118 */
4119 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004120 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004121 p_cpo = cpo_save;
4122
4123 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4124 {
4125 /*
4126 * Add the string to the input stream.
4127 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4128 *
4129 * First clear typed characters from the typeahead buffer, there could
4130 * be half a mapping there. Then append to the existing string, so
4131 * that multiple commands from a client are concatenated.
4132 */
4133 if (typebuf.tb_maplen < typebuf.tb_len)
4134 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4135 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4136
4137 /* Let input_available() know we inserted text in the typeahead
4138 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004139 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004140 }
4141 vim_free((char_u *)ptr);
4142}
4143
4144/*
4145 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004146 */
4147 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004148eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004149{
4150 char_u *res;
4151 int save_dbl = debug_break_level;
4152 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004153 void *fc;
4154
4155 /* Evaluate the expression at the toplevel, don't use variables local to
4156 * the calling function. */
4157 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004158
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004159 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4160 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004161 debug_break_level = -1;
4162 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004163 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4164 * to be typed. Do generate errors so that try/catch works. */
4165 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004166
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004167 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004168
4169 debug_break_level = save_dbl;
4170 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004171 --emsg_silent;
4172 if (emsg_silent < 0)
4173 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004174 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004175
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004176 /* A client can tell us to redraw, but not to display the cursor, so do
4177 * that here. */
4178 setcursor();
4179 out_flush();
4180#ifdef FEAT_GUI
4181 if (gui.in_use)
4182 gui_update_cursor(FALSE, FALSE);
4183#endif
4184
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004185 return res;
4186}
4187
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004188/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004189 * Evaluate a command or expression sent to ourselves.
4190 */
4191 int
4192sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4193{
4194 if (asExpr)
4195 {
4196 char_u *ret;
4197
4198 ret = eval_client_expr_to_string(cmd);
4199 if (result != NULL)
4200 {
4201 if (ret == NULL)
4202 {
4203 char *err = _(e_invexprmsg);
4204 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4205 char_u *msg;
4206
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004207 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004208 if (msg != NULL)
4209 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4210 *result = msg;
4211 }
4212 else
4213 *result = ret;
4214 }
4215 else
4216 vim_free(ret);
4217 return ret == NULL ? -1 : 0;
4218 }
4219 server_to_input_buf(cmd);
4220 return 0;
4221}
4222
4223/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004224 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4225 * return an allocated string. Otherwise return "data".
4226 * "*tofree" is set to the result when it needs to be freed later.
4227 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004228 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004229serverConvert(
4230 char_u *client_enc UNUSED,
4231 char_u *data,
4232 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004233{
4234 char_u *res = data;
4235
4236 *tofree = NULL;
4237# ifdef FEAT_MBYTE
4238 if (client_enc != NULL && p_enc != NULL)
4239 {
4240 vimconv_T vimconv;
4241
4242 vimconv.vc_type = CONV_NONE;
4243 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4244 && vimconv.vc_type != CONV_NONE)
4245 {
4246 res = string_convert(&vimconv, data, NULL);
4247 if (res == NULL)
4248 res = data;
4249 else
4250 *tofree = res;
4251 }
4252 convert_setup(&vimconv, NULL, NULL);
4253 }
4254# endif
4255 return res;
4256}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004257#endif