blob: e3db0b2a916ebe072488aa468b2430e13f945d02 [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 Moolenaar8866d272012-11-28 15:55:42 +010092#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020093
94static char_u *start_dir = NULL; /* current working dir on startup */
95
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020096static int has_dash_c_arg = FALSE;
97
Bram Moolenaara8e691d2016-08-07 15:19:26 +020098/* Various parameters passed between main() and other functions. */
99static mparm_T params;
100
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000101 int
102# ifdef VIMDLL
103_export
104# endif
105# ifdef FEAT_GUI_MSWIN
106# ifdef __BORLANDC__
107_cdecl
108# endif
109VimMain
110# else
111main
112# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100113(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000114{
Bram Moolenaar3f269672009-11-03 11:11:11 +0000115#ifdef STARTUPTIME
116 int i;
117#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000119 /*
120 * Do any system-specific initialisations. These can NOT use IObuff or
121 * NameBuff. Thus emsg2() cannot be called!
122 */
123 mch_early_init();
124
Bram Moolenaar14993322014-09-09 12:25:33 +0200125#if defined(WIN32) && defined(FEAT_MBYTE)
126 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200127 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200128 * convert when 'encoding' changes. Get the unexpanded arguments.
129 */
130 argc = get_cmd_argsW(&argv);
131#endif
132
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000133 /* Many variables are in "params" so that we can pass them to invoked
134 * functions without a lot of arguments. "argc" and "argv" are also
135 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000136 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000137 params.argc = argc;
138 params.argv = argv;
139 params.want_full_screen = TRUE;
140#ifdef FEAT_EVAL
141 params.use_debug_break_level = -1;
142#endif
143#ifdef FEAT_WINDOWS
144 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000145#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000146
Bram Moolenaar99685e62013-05-11 13:56:18 +0200147#ifdef FEAT_RUBY
148 {
149 int ruby_stack_start;
150 vim_ruby_init((void *)&ruby_stack_start);
151 }
152#endif
153
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000154#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000155 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000156#endif
157
158#ifdef MEM_PROFILE
159 atexit(vim_mem_profile_dump);
160#endif
161
162#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200163 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000164 for (i = 1; i < argc; ++i)
165 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000166 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000167 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000168 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000169 TIME_MSG("--- VIM STARTING ---");
170 break;
171 }
172 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000173#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000174 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200176 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000177
178#ifdef FEAT_CLIENTSERVER
179 /*
180 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000181 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000182 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000183 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000184#endif
185
186 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000187 * Figure out the way to work from the command name argv[0].
188 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000189 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000190 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000191
192 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193 * Process the command line arguments. File names are put in the global
194 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000196 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000197 TIME_MSG("parsing arguments");
198
199 /*
200 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000201 * there is no terminal version, and on Windows we can't fork one off with
202 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000203 */
204#ifdef ALWAYS_USE_GUI
205 gui.starting = TRUE;
206#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000207# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000208 /*
209 * Check if the GUI can be started. Reset gui.starting if not.
210 * Don't know about other systems, stay on the safe side and don't check.
211 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000212 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000214 if (gui_init_check() == FAIL)
215 {
216 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000217
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000218 /* When running "evim" or "gvim -y" we need the menus, exit if we
219 * don't have them. */
220 if (params.evim_mode)
221 mch_exit(1);
222 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000223 }
224# endif
225#endif
226
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227 if (GARGCOUNT > 0)
228 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100229#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230 /*
231 * Expand wildcards in file names.
232 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000233 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200235 start_dir = alloc(MAXPATHL);
236 if (start_dir != NULL)
237 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000238 /* Temporarily add '(' and ')' to 'isfname'. These are valid
239 * filename characters but are excluded from 'isfname' to make
240 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
241 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000242 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200244 if (start_dir != NULL)
245 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 }
247#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200248 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000249 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000250
251#if defined(WIN32) && defined(FEAT_MBYTE)
252 {
253 extern void set_alist_count(void);
254
255 /* Remember the number of entries in the argument list. If it changes
256 * we don't react on setting 'encoding'. */
257 set_alist_count();
258 }
259#endif
260
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000261#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000262 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000263 {
264 /*
265 * If there is one filename, fully qualified, we have very probably
266 * been invoked from explorer, so change to the file's directory.
267 * Hint: to avoid this when typing a command use a forward slash.
268 * If the cd fails, it doesn't matter.
269 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200270 (void)vim_chdirfile(params.fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200271 if (start_dir != NULL)
272 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000273 }
274#endif
275 TIME_MSG("expanding arguments");
276
277#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000278 if (params.diff_mode && params.window_count == -1)
279 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280#endif
281
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000282 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283 ++RedrawingDisabled;
284
285 /*
286 * When listing swap file names, don't do cursor positioning et. al.
287 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200288 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000289 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000290
291 /*
292 * When certain to start the GUI, don't check capabilities of terminal.
293 * For GTK we can't be sure, but when started from the desktop it doesn't
294 * make sense to try using a terminal.
295 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000296#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000297 if (gui.starting
298# ifdef FEAT_GUI_GTK
299 && !isatty(2)
300# endif
301 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000302 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000303#endif
304
305#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
306 /* When the GUI is started from Finder, need to display messages in a
307 * message box. isatty(2) returns TRUE anyway, thus we need to check the
308 * name to know we're not started from a terminal. */
309 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000310 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000311 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000312
313 /* Avoid always using "/" as the current directory. Note that when
314 * started from Finder the arglist will be filled later in
315 * HandleODocAE() and "fname" will be NULL. */
316 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
317 && STRCMP(NameBuff, "/") == 0)
318 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200319 if (params.fname != NULL)
320 (void)vim_chdirfile(params.fname);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000321 else
322 {
323 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
324 vim_chdir(NameBuff);
325 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200326 if (start_dir != NULL)
327 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000328 }
329 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000330#endif
331
332 /*
333 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100334 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000335 * Note that we may use mch_exit() before mch_init()!
336 */
337 mch_init();
338 TIME_MSG("shell init");
339
340#ifdef USE_XSMP
341 /*
342 * For want of anywhere else to do it, try to connect to xsmp here.
343 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
344 * Hijacking -X 'no X connection' to also disable XSMP connection as that
345 * has a similar delay upon failure.
346 * Only try if SESSION_MANAGER is set to something non-null.
347 */
348 if (!x_no_connect)
349 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000350 char *p = getenv("SESSION_MANAGER");
351
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000352 if (p != NULL && *p != NUL)
353 {
354 xsmp_init();
355 TIME_MSG("xsmp init");
356 }
357 }
358#endif
359
360 /*
361 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000362 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000363 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000365 /* This message comes before term inits, but after setting "silent_mode"
366 * when the input is not a tty. */
367 if (GARGCOUNT > 1 && !silent_mode)
368 printf(_("%d files to edit\n"), GARGCOUNT);
369
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000370 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000372 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000373 capabilities (will set full_screen) */
374 screen_start(); /* don't know where cursor is now */
375 TIME_MSG("Termcap init");
376 }
377
378 /*
379 * Set the default values for the options that use Rows and Columns.
380 */
381 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000382 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383#ifdef FEAT_DIFF
384 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
385 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000386 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000387 diff_win_options(firstwin, FALSE);
388#endif
389
390 cmdline_row = Rows - p_ch;
391 msg_row = cmdline_row;
392 screenalloc(FALSE); /* allocate screen buffers */
393 set_init_2();
394 TIME_MSG("inits 2");
395
396 msg_scroll = TRUE;
397 no_wait_return = TRUE;
398
399 init_mappings(); /* set up initial mappings */
400
401 init_highlight(TRUE, FALSE); /* set the default highlight groups */
402 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000403
404#ifdef FEAT_EVAL
405 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000406 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407#endif
408
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100409#ifdef FEAT_MZSCHEME
410 /*
411 * Newer version of MzScheme (Racket) require earlier (trampolined)
412 * initialisation via scheme_main_setup.
413 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200414 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100415 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200416 return mzscheme_main();
417#else
418 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100419#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200420}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100421#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200422#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100423
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200424/*
425 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
426 * things simple.
427 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
428 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100429 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200430vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100431{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100432#ifndef NO_VIM_MAIN
Bram Moolenaar66459b72016-08-06 19:01:55 +0200433 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
434 * Allows for setting 'loadplugins' there. */
435 if (params.use_vimrc != NULL && STRCMP(params.use_vimrc, "NONE") == 0)
436 p_lpl = FALSE;
437
Bram Moolenaar58d98232005-07-23 22:25:46 +0000438 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000439 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000440
Bram Moolenaar58d98232005-07-23 22:25:46 +0000441 /* Source startup scripts. */
442 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000443
444#ifdef FEAT_EVAL
445 /*
446 * Read all the plugin files.
447 * Only when compiled with +eval, since most plugins need it.
448 */
449 if (p_lpl)
450 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000451# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar66459b72016-08-06 19:01:55 +0200452 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000453# else
Bram Moolenaar66459b72016-08-06 19:01:55 +0200454 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000455# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000456 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100457
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100458 ex_packloadall(NULL);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100459 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200460
461# ifdef VMS /* Somehow VMS doesn't handle the "**". */
462 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
463# else
464 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
465# endif
466 TIME_MSG("loading after plugins");
467
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000468 }
469#endif
470
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000471#ifdef FEAT_DIFF
472 /* Decide about window layout for diff mode after reading vimrc. */
473 if (params.diff_mode && params.window_layout == 0)
474 {
475 if (diffopt_horizontal())
476 params.window_layout = WIN_HOR; /* use horizontal split */
477 else
478 params.window_layout = WIN_VER; /* use vertical split */
479 }
480#endif
481
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000482 /*
483 * Recovery mode without a file name: List swap files.
484 * This uses the 'dir' option, therefore it must be after the
485 * initializations.
486 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200487 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000488 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200489 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000490 mch_exit(0);
491 }
492
493 /*
494 * Set a few option defaults after reading .vimrc files:
495 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
496 */
497 set_init_3();
498 TIME_MSG("inits 3");
499
500 /*
501 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
502 * Note that this overrides anything from a vimrc file.
503 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000504 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000505 p_uc = 0;
506
507#ifdef FEAT_FKMAP
508 if (curwin->w_p_rl && p_altkeymap)
509 {
510 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
511# ifdef FEAT_ARABIC
512 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
513# endif
514 p_fkmap = TRUE; /* Set the Farsi keymap mode */
515 }
516#endif
517
518#ifdef FEAT_GUI
519 if (gui.starting)
520 {
521#if defined(UNIX) || defined(VMS)
522 /* When something caused a message from a vimrc script, need to output
523 * an extra newline before the shell prompt. */
524 if (did_emsg || msg_didout)
525 putchar('\n');
526#endif
527
528 gui_start(); /* will set full_screen to TRUE */
529 TIME_MSG("starting GUI");
530
531 /* When running "evim" or "gvim -y" we need the menus, exit if we
532 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000533 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000534 mch_exit(1);
535 }
536#endif
537
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538#ifdef FEAT_VIMINFO
539 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000540 * Read in registers, history etc, but not marks, from the viminfo file.
541 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 */
543 if (*p_viminfo != NUL)
544 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000545 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546 TIME_MSG("reading viminfo");
547 }
548#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100549#ifdef FEAT_EVAL
550 /* It's better to make v:oldfiles an empty list than NULL. */
551 if (get_vim_var_list(VV_OLDFILES) == NULL)
552 set_vim_var_list(VV_OLDFILES, list_alloc());
553#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000554
555#ifdef FEAT_QUICKFIX
556 /*
557 * "-q errorfile": Load the error file now.
558 * If the error file can't be read, exit before doing anything else.
559 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000560 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100562 char_u *enc = NULL;
563
564# ifdef FEAT_MBYTE
565 enc = p_menc;
566# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000567 if (params.use_ef != NULL)
568 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000569 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200570 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100571 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000572 {
573 out_char('\n');
574 mch_exit(3);
575 }
576 TIME_MSG("reading errorfile");
577 }
578#endif
579
580 /*
581 * Start putting things on the screen.
582 * Scroll screen down before drawing over it
583 * Clear screen now, so file message will not be cleared.
584 */
585 starting = NO_BUFFERS;
586 no_wait_return = FALSE;
587 if (!exmode_active)
588 msg_scroll = FALSE;
589
590#ifdef FEAT_GUI
591 /*
592 * This seems to be required to make callbacks to be called now, instead
593 * of after things have been put on the screen, which then may be deleted
594 * when getting a resize callback.
595 * For the Mac this handles putting files dropped on the Vim icon to
596 * global_alist.
597 */
598 if (gui.in_use)
599 {
600# ifdef FEAT_SUN_WORKSHOP
601 if (!usingSunWorkShop)
602# endif
603 gui_wait_for_chars(50L);
604 TIME_MSG("GUI delay");
605 }
606#endif
607
608#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
609 qnx_clip_init();
610#endif
611
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200612#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
613 clip_init(TRUE);
614#endif
615
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000616#ifdef FEAT_XCLIPBOARD
617 /* Start using the X clipboard, unless the GUI was started. */
618# ifdef FEAT_GUI
619 if (!gui.in_use)
620# endif
621 {
622 setup_term_clip();
623 TIME_MSG("setup clipboard");
624 }
625#endif
626
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000627#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000628 /* Prepare for being a Vim server. */
629 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000630#endif
631
632 /*
633 * If "-" argument given: Read file from stdin.
634 * Do this before starting Raw mode, because it may change things that the
635 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
636 * are the same terminal: "cat | vim -".
637 * Using autocommands here may cause trouble...
638 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000639 if (params.edit_type == EDIT_STDIN && !recoverymode)
640 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000641
642#if defined(UNIX) || defined(VMS)
643 /* When switching screens and something caused a message from a vimrc
644 * script, need to output an extra newline on exit. */
645 if ((did_emsg || msg_didout) && *T_TI != NUL)
646 newline_on_exit = TRUE;
647#endif
648
649 /*
650 * When done something that is not allowed or error message call
651 * wait_return. This must be done before starttermcap(), because it may
652 * switch to another screen. It must be done after settmode(TMODE_RAW),
653 * because we want to react on a single key stroke.
654 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000655 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000656 */
657 settmode(TMODE_RAW);
658 TIME_MSG("setting raw mode");
659
660 if (need_wait_return || msg_didany)
661 {
662 wait_return(TRUE);
663 TIME_MSG("waiting for return");
664 }
665
666 starttermcap(); /* start termcap if not done by wait_return() */
667 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200668#if defined(FEAT_TERMRESPONSE)
669# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200670 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200671# endif
672 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100673#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674
675#ifdef FEAT_MOUSE
676 setmouse(); /* may start using the mouse */
677#endif
678 if (scroll_region)
679 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000680 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000681
682 /*
683 * Don't clear the screen when starting in Ex mode, unless using the GUI.
684 */
685 if (exmode_active
686#ifdef FEAT_GUI
687 && !gui.in_use
688#endif
689 )
690 must_redraw = CLEAR;
691 else
692 {
693 screenclear(); /* clear screen */
694 TIME_MSG("clearing screen");
695 }
696
697#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000698 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000699 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100700 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200701 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702 TIME_MSG("getting crypt key");
703 }
704#endif
705
706 no_wait_return = TRUE;
707
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000709 * Create the requested number of windows and edit buffers in them.
710 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000712 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 TIME_MSG("opening buffers");
714
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000715#ifdef FEAT_EVAL
716 /* clear v:swapcommand */
717 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
718#endif
719
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000720 /* Ex starts at last line of the file */
721 if (exmode_active)
722 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
723
724#ifdef FEAT_AUTOCMD
725 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
726 TIME_MSG("BufEnter autocommands");
727#endif
728 setpcmark();
729
730#ifdef FEAT_QUICKFIX
731 /*
732 * When started with "-q errorfile" jump to first error now.
733 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000734 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000735 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000736 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000737 TIME_MSG("jump to first error");
738 }
739#endif
740
741#ifdef FEAT_WINDOWS
742 /*
743 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000744 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000745 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200746 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000747#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200748 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749
750#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000751 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000752 {
753 win_T *wp;
754
755 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200756 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000757 diff_win_options(wp, TRUE);
758 }
759#endif
760
761 /*
762 * Shorten any of the filenames, but only when absolute.
763 */
764 shorten_fnames(FALSE);
765
766 /*
767 * Need to jump to the tag before executing the '-c command'.
768 * Makes "vim -c '/return' -t main" work.
769 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000770 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000771 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000772#if defined(HAS_SWAP_EXISTS_ACTION)
773 swap_exists_did_quit = FALSE;
774#endif
775
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000776 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000777 do_cmdline_cmd(IObuff);
778 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000779
780#if defined(HAS_SWAP_EXISTS_ACTION)
781 /* If the user doesn't want to edit the file then we quit here. */
782 if (swap_exists_did_quit)
783 getout(1);
784#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000785 }
786
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000787 /* Execute any "+", "-c" and "-S" arguments. */
788 if (params.n_commands > 0)
789 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000790
791 RedrawingDisabled = 0;
792 redraw_all_later(NOT_VALID);
793 no_wait_return = FALSE;
794 starting = 0;
795
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200796 /* 'autochdir' has been postponed */
797 DO_AUTOCHDIR
798
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000799#ifdef FEAT_TERMRESPONSE
800 /* Requesting the termresponse is postponed until here, so that a "-c q"
801 * argument doesn't make it appear in the shell Vim was started from. */
802 may_req_termresponse();
803#endif
804
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000805 /* start in insert mode */
806 if (p_im)
807 need_start_insertmode = TRUE;
808
Bram Moolenaar14735512016-03-26 21:00:08 +0100809#ifdef FEAT_EVAL
810 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
811#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000812#ifdef FEAT_AUTOCMD
813 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
814 TIME_MSG("VimEnter autocommands");
815#endif
816
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200817#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
818 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
819 * done after the clipboard is available and all initial commands that may
820 * modify the 'clipboard' setting have run; i.e. just before entering the
821 * main loop. */
822 {
823 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100824
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200825 adjust_clip_reg(&default_regname);
826 set_reg_var(default_regname);
827 }
828#endif
829
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000830#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
831 /* When a startup script or session file setup for diff'ing and
832 * scrollbind, sync the scrollbind now. */
833 if (curwin->w_p_diff && curwin->w_p_scb)
834 {
835 update_topline();
836 check_scrollbind((linenr_T)0, 0L);
837 TIME_MSG("diff scrollbinding");
838 }
839#endif
840
841#if defined(WIN3264) && !defined(FEAT_GUI_W32)
842 mch_set_winsize_now(); /* Allow winsize changes from now on */
843#endif
844
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000845#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
846 /* When tab pages were created, may need to update the tab pages line and
847 * scrollbars. This is skipped while creating them. */
848 if (first_tabpage->tp_next != NULL)
849 {
850 out_flush();
851 gui_init_which_components(NULL);
852 gui_update_scrollbars(TRUE);
853 }
854 need_mouse_correct = TRUE;
855#endif
856
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000857 /* If ":startinsert" command used, stuff a dummy command to be able to
858 * call normal_cmd(), which will then start Insert mode. */
859 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000860 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000861
862#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200863 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200864 {
865# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200866# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200867 && !defined(FEAT_GUI_W32)
868 if (gui.in_use)
869 {
870 mch_errmsg(_("netbeans is not supported with this GUI\n"));
871 mch_exit(2);
872 }
873# endif
874# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200876 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200877 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878#endif
879
880 TIME_MSG("before starting main loop");
881
882 /*
883 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100884 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000885 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200887#endif /* NO_VIM_MAIN */
888
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889 return 0;
890}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891
892/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200893 * Initialisation shared by main() and some tests.
894 */
895 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200896common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200897{
898
899#ifdef FEAT_MBYTE
900 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
901#endif
902#ifdef FEAT_EVAL
903 eval_init(); /* init global variables */
904#endif
905
906#ifdef __QNXNTO__
907 qnx_init(); /* PhAttach() for clipboard, (and gui) */
908#endif
909
910#ifdef MAC_OS_CLASSIC
911 /* Prepare for possibly starting GUI sometime */
912 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200913 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200914 TIME_MSG("GUI prepared");
915#endif
916
917 /* Init the table of Normal mode commands. */
918 init_normal_cmds();
919
920#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
921 make_version(); /* Construct the long version string. */
922#endif
923
924 /*
925 * Allocate space for the generic buffers (needed for set_init_1() and
926 * EMSG2()).
927 */
928 if ((IObuff = alloc(IOSIZE)) == NULL
929 || (NameBuff = alloc(MAXPATHL)) == NULL)
930 mch_exit(0);
931 TIME_MSG("Allocated generic buffers");
932
933#ifdef NBDEBUG
934 /* Wait a moment for debugging NetBeans. Must be after allocating
935 * NameBuff. */
936 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
937 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
938 TIME_MSG("NetBeans debug wait");
939#endif
940
941#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
942 /*
943 * Setup to use the current locale (for ctype() and many other things).
944 * NOTE: Translated messages with encodings other than latin1 will not
945 * work until set_init_1() has been called!
946 */
947 init_locale();
948 TIME_MSG("locale set");
949#endif
950
951#ifdef FEAT_GUI
952 gui.dofork = TRUE; /* default is to use fork() */
953#endif
954
955 /*
956 * Do a first scan of the arguments in "argv[]":
957 * -display or --display
958 * --server...
959 * --socketid
960 * --windowid
961 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200962 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200963
964#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200965 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200966#endif
967#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
968 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200969 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200970 TIME_MSG("GUI prepared");
971#endif
972
973#ifdef FEAT_CLIPBOARD
974 clip_init(FALSE); /* Initialise clipboard stuff */
975 TIME_MSG("clipboard setup");
976#endif
977
978 /*
979 * Check if we have an interactive window.
980 * On the Amiga: If there is no window, we open one with a newcli command
981 * (needed for :! to * work). mch_check_win() will also handle the -d or
982 * -dev argument.
983 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100984 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200985 TIME_MSG("window checked");
986
987 /*
988 * Allocate the first window and buffer.
989 * Can't do anything without it, exit when it fails.
990 */
991 if (win_alloc_first() == FAIL)
992 mch_exit(0);
993
994 init_yank(); /* init yank buffers */
995
996 alist_init(&global_alist); /* Init the argument list to empty. */
997 global_alist.id = 0;
998
999 /*
1000 * Set the default values for the options.
1001 * NOTE: Non-latin1 translated messages are working only after this,
1002 * because this is where "has_mbyte" will be set, which is used by
1003 * msg_outtrans_len_attr().
1004 * First find out the home directory, needed to expand "~" in options.
1005 */
1006 init_homedir(); /* find real value of $HOME */
1007 set_init_1();
1008 TIME_MSG("inits 1");
1009
1010#ifdef FEAT_EVAL
1011 set_lang_var(); /* set v:lang and v:ctype */
1012#endif
1013}
1014
1015/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001016 * Return TRUE when the --not-a-term argument was found.
1017 */
1018 int
1019is_not_a_term()
1020{
1021 return params.not_a_term;
1022}
1023
1024/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001025 * Main loop: Execute Normal mode commands until exiting Vim.
1026 * Also used to handle commands in the command-line window, until the window
1027 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001028 * Also used to handle ":visual" command after ":global": execute Normal mode
1029 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001030 */
1031 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001032main_loop(
1033 int cmdwin, /* TRUE when working in the command-line window */
1034 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001035{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001036 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001037 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001038#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001039 /* these are static to avoid a compiler warning */
1040 static linenr_T conceal_old_cursor_line = 0;
1041 static linenr_T conceal_new_cursor_line = 0;
1042 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001043#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001044
1045#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1046 /* Setup to catch a terminating error from the X server. Just ignore
1047 * it, restore the state and continue. This might not always work
1048 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001049 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051 {
1052 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001053 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001054 got_int = TRUE;
1055 need_wait_return = FALSE;
1056 global_busy = FALSE;
1057 exmode_active = 0;
1058 skip_redraw = FALSE;
1059 RedrawingDisabled = 0;
1060 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001061 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001062# ifdef FEAT_EVAL
1063 emsg_skip = 0;
1064# endif
1065 emsg_off = 0;
1066# ifdef FEAT_MOUSE
1067 setmouse();
1068# endif
1069 settmode(TMODE_RAW);
1070 starttermcap();
1071 scroll_start();
1072 redraw_later_clear();
1073 }
1074#endif
1075
1076 clear_oparg(&oa);
1077 while (!cmdwin
1078#ifdef FEAT_CMDWIN
1079 || cmdwin_result == 0
1080#endif
1081 )
1082 {
1083 if (stuff_empty())
1084 {
1085 did_check_timestamps = FALSE;
1086 if (need_check_timestamps)
1087 check_timestamps(FALSE);
1088 if (need_wait_return) /* if wait_return still needed ... */
1089 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001090 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001091 {
1092 need_start_insertmode = FALSE;
1093 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1094 /* skip the fileinfo message now, because it would be shown
1095 * after insert mode finishes! */
1096 need_fileinfo = FALSE;
1097 }
1098 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001099
1100 /* Reset "got_int" now that we got back to the main loop. Except when
1101 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1102 * the ":g" command.
1103 * For ":g/pat/vi" we reset "got_int" when used once. When used
1104 * a second time we go back to Ex mode and abort the ":g" command. */
1105 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001106 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001107 if (noexmode && global_busy && !exmode_active && previous_got_int)
1108 {
1109 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1110 * used and keep "got_int" set, so that it aborts ":g". */
1111 exmode_active = EXMODE_NORMAL;
1112 State = NORMAL;
1113 }
1114 else if (!global_busy || !exmode_active)
1115 {
1116 if (!quit_more)
1117 (void)vgetc(); /* flush all buffers */
1118 got_int = FALSE;
1119 }
1120 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001121 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001122 else
1123 previous_got_int = FALSE;
1124
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001125 if (!exmode_active)
1126 msg_scroll = FALSE;
1127 quit_more = FALSE;
1128
1129 /*
1130 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1131 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1132 * update cursor and redraw.
1133 */
1134 if (skip_redraw || exmode_active)
1135 skip_redraw = FALSE;
1136 else if (do_redraw || stuff_empty())
1137 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001138# ifdef FEAT_GUI
1139 /* If ui_breakcheck() was used a resize may have been postponed. */
1140 gui_may_resize_shell();
1141# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001142#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001143 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001144 if (!finish_op && (
1145# ifdef FEAT_AUTOCMD
1146 has_cursormoved()
1147# endif
1148# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1149 ||
1150# endif
1151# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001152 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001153# endif
1154 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001155# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001156 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001157# endif
1158 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001159 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001160# ifdef FEAT_AUTOCMD
1161 if (has_cursormoved())
1162 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1163 FALSE, curbuf);
1164# endif
1165# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001166 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001168# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001169 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001170# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001171 conceal_new_cursor_line = curwin->w_cursor.lnum;
1172 conceal_update_lines = TRUE;
1173 }
1174# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001175# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001176 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001177# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001178 }
1179#endif
1180
Bram Moolenaar186628f2013-03-19 13:33:23 +01001181#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001182 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001183 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001184 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001185 {
1186 if (last_changedtick_buf == curbuf)
1187 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1188 FALSE, curbuf);
1189 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001190 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001191 }
1192#endif
1193
Bram Moolenaar33aec762006-01-22 23:30:12 +00001194#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1195 /* Scroll-binding for diff mode may have been postponed until
1196 * here. Avoids doing it for every change. */
1197 if (diff_need_scrollbind)
1198 {
1199 check_scrollbind((linenr_T)0, 0L);
1200 diff_need_scrollbind = FALSE;
1201 }
1202#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001203#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001204 /* Include a closed fold completely in the Visual area. */
1205 foldAdjustVisual();
1206#endif
1207#ifdef FEAT_FOLDING
1208 /*
1209 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1210 * contain the cursor.
1211 * When 'foldopen' is "all", open the fold(s) under the cursor.
1212 * This may mark the window for redrawing.
1213 */
1214 if (hasAnyFolding(curwin) && !char_avail())
1215 {
1216 foldCheckClose();
1217 if (fdo_flags & FDO_ALL)
1218 foldOpenCursor();
1219 }
1220#endif
1221
1222 /*
1223 * Before redrawing, make sure w_topline is correct, and w_leftcol
1224 * if lines don't wrap, and w_skipcol if lines wrap.
1225 */
1226 update_topline();
1227 validate_cursor();
1228
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 if (VIsual_active)
1230 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001231 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001232 update_screen(0);
1233 else if (redraw_cmdline || clear_cmdline)
1234 showmode();
1235#ifdef FEAT_WINDOWS
1236 redraw_statuslines();
1237#endif
1238#ifdef FEAT_TITLE
1239 if (need_maketitle)
1240 maketitle();
1241#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001242#ifdef FEAT_VIMINFO
1243 curbuf->b_last_used = vim_time();
1244#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001245 /* display message after redraw */
1246 if (keep_msg != NULL)
1247 {
1248 char_u *p;
1249
1250 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001251 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1252 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 p = keep_msg;
1254 msg_attr(p, keep_msg_attr);
1255 vim_free(p);
1256 }
1257 if (need_fileinfo) /* show file info after redraw */
1258 {
1259 fileinfo(FALSE, TRUE, FALSE);
1260 need_fileinfo = FALSE;
1261 }
1262
1263 emsg_on_display = FALSE; /* can delete error message now */
1264 did_emsg = FALSE;
1265 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001266 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001267 showruler(FALSE);
1268
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001269# if defined(FEAT_CONCEAL)
1270 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001271 && (conceal_old_cursor_line != conceal_new_cursor_line
1272 || conceal_cursor_line(curwin)
1273 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001274 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001275 if (conceal_old_cursor_line != conceal_new_cursor_line
1276 && conceal_old_cursor_line
1277 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001278 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001279 update_single_line(curwin, conceal_new_cursor_line);
1280 curwin->w_valid &= ~VALID_CROW;
1281 }
1282# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001283 setcursor();
1284 cursor_on();
1285
1286 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001287
1288#ifdef STARTUPTIME
1289 /* Now that we have drawn the first screen all the startup stuff
1290 * has been done, close any file for startup messages. */
1291 if (time_fd != NULL)
1292 {
1293 TIME_MSG("first screen update");
1294 TIME_MSG("--- VIM STARTED ---");
1295 fclose(time_fd);
1296 time_fd = NULL;
1297 }
1298#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001299 }
1300#ifdef FEAT_GUI
1301 if (need_mouse_correct)
1302 gui_mouse_correct();
1303#endif
1304
1305 /*
1306 * Update w_curswant if w_set_curswant has been set.
1307 * Postponed until here to avoid computing w_virtcol too often.
1308 */
1309 update_curswant();
1310
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001311#ifdef FEAT_EVAL
1312 /*
1313 * May perform garbage collection when waiting for a character, but
1314 * only at the very toplevel. Otherwise we may be using a List or
1315 * Dict internally somewhere.
1316 * "may_garbage_collect" is reset in vgetc() which is invoked through
1317 * do_exmode() and normal_cmd().
1318 */
1319 may_garbage_collect = (!cmdwin && !noexmode);
1320#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001321 /*
1322 * If we're invoked as ex, do a round of ex commands.
1323 * Otherwise, get and execute a normal mode command.
1324 */
1325 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001326 {
1327 if (noexmode) /* End of ":global/path/visual" commands */
1328 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001329 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001330 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001331 else
1332 normal_cmd(&oa, TRUE);
1333 }
1334}
1335
1336
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001337#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001338/*
1339 * Exit, but leave behind swap files for modified buffers.
1340 */
1341 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001342getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001343{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001344# if defined(SIGHUP) && defined(SIG_IGN)
1345 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1346 * makes Vim exit and then handling SIGHUP causes various reentrance
1347 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001348 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001349# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001351 ml_close_notmod(); /* close all not-modified buffers */
1352 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1353 ml_close_all(FALSE); /* close all memfiles, without deleting */
1354 getout(exitval); /* exit Vim properly */
1355}
1356#endif
1357
1358
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001359/*
1360 * Exit properly.
1361 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001362 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001363getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364{
1365#ifdef FEAT_AUTOCMD
1366 buf_T *buf;
1367 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001368 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001369#endif
1370
1371 exiting = TRUE;
1372
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001373 /* When running in Ex mode an error causes us to exit with a non-zero exit
1374 * code. POSIX requires this, although it's not 100% clear from the
1375 * standard. */
1376 if (exmode_active)
1377 exitval += ex_exitval;
1378
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001379 /* Position the cursor on the last screen line, below all the text */
1380#ifdef FEAT_GUI
1381 if (!gui.in_use)
1382#endif
1383 windgoto((int)Rows - 1, 0);
1384
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001385#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1386 /* Optionally print hashtable efficiency. */
1387 hash_debug_results();
1388#endif
1389
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001390#ifdef FEAT_GUI
1391 msg_didany = FALSE;
1392#endif
1393
1394#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001395 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001397 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1398# if defined FEAT_WINDOWS
1399 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001400 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001401 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001402 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001403 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001404 if (wp->w_buffer == NULL)
1405 /* Autocmd must have close the buffer already, skip. */
1406 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001407 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001408 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001409 {
1410 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1411 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001412 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001413 /* start all over, autocommands may mess up the lists */
1414 next_tp = first_tabpage;
1415 break;
1416 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001417 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001418 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001419# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001420 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1421 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001422# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001423
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001424 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001425 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 if (buf->b_ml.ml_mfp != NULL)
1427 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001428 bufref_T bufref;
1429
1430 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001431 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001432 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001433 if (!bufref_valid(&bufref))
1434 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001435 break;
1436 }
1437 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1438 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001439#endif
1440
1441#ifdef FEAT_VIMINFO
1442 if (*p_viminfo != NUL)
1443 /* Write out the registers, history, marks etc, to the viminfo file */
1444 write_viminfo(NULL, FALSE);
1445#endif
1446
1447#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001448 if (get_vim_var_nr(VV_DYING) <= 1)
1449 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450#endif
1451
Bram Moolenaar05159a02005-02-26 23:04:13 +00001452#ifdef FEAT_PROFILE
1453 profile_dump();
1454#endif
1455
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001456 if (did_emsg
1457#ifdef FEAT_GUI
1458 || (gui.in_use && msg_didany && p_verbose > 0)
1459#endif
1460 )
1461 {
1462 /* give the user a chance to read the (error) message */
1463 no_wait_return = FALSE;
1464 wait_return(FALSE);
1465 }
1466
1467#ifdef FEAT_AUTOCMD
1468 /* Position the cursor again, the autocommands may have moved it */
1469# ifdef FEAT_GUI
1470 if (!gui.in_use)
1471# endif
1472 windgoto((int)Rows - 1, 0);
1473#endif
1474
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001475#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001476 job_stop_on_exit();
1477#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001478#ifdef FEAT_LUA
1479 lua_end();
1480#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001481#ifdef FEAT_MZSCHEME
1482 mzscheme_end();
1483#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001484#ifdef FEAT_TCL
1485 tcl_end();
1486#endif
1487#ifdef FEAT_RUBY
1488 ruby_end();
1489#endif
1490#ifdef FEAT_PYTHON
1491 python_end();
1492#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001493#ifdef FEAT_PYTHON3
1494 python3_end();
1495#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001496#ifdef FEAT_PERL
1497 perl_end();
1498#endif
1499#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1500 iconv_end();
1501#endif
1502#ifdef FEAT_NETBEANS_INTG
1503 netbeans_end();
1504#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001505#ifdef FEAT_CSCOPE
1506 cs_end();
1507#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001508#ifdef FEAT_EVAL
1509 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001510 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001511#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001512#if defined(WIN32) && defined(FEAT_MBYTE)
1513 free_cmd_argsW();
1514#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001515
1516 mch_exit(exitval);
1517}
1518
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001519#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1520/*
1521 * Setup to use the current locale (for ctype() and many other things).
1522 */
1523 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001524init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001525{
1526 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001527
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001528# ifdef FEAT_GUI_GTK
1529 /* Tell Gtk not to change our locale settings. */
1530 gtk_disable_setlocale();
1531# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001532# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1533 /* Make sure strtod() uses a decimal point, not a comma. */
1534 setlocale(LC_NUMERIC, "C");
1535# endif
1536
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001537# ifdef WIN32
1538 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1539 * text while it's expecting text in the current locale. This call avoids
1540 * that. */
1541 setlocale(LC_CTYPE, "C");
1542# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001543
1544# ifdef FEAT_GETTEXT
1545 {
1546 int mustfree = FALSE;
1547 char_u *p;
1548
1549# ifdef DYNAMIC_GETTEXT
1550 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001551 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001552# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001553 /* expand_env() doesn't work yet, because g_chartab[] is not
1554 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001555 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1556 if (p != NULL && *p != NUL)
1557 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001558 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001559 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1560 }
1561 if (mustfree)
1562 vim_free(p);
1563 textdomain(VIMPACKAGE);
1564 }
1565# endif
1566}
1567#endif
1568
1569/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001570 * Get the name of the display, before gui_prepare() removes it from
1571 * argv[]. Used for the xterm-clipboard display.
1572 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001573 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001574 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001575 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001576early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001577{
Bram Moolenaar03005972008-11-20 13:12:36 +00001578#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1579 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001580 int argc = parmp->argc;
1581 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001582 int i;
1583
1584 for (i = 1; i < argc; i++)
1585 {
1586 if (STRCMP(argv[i], "--") == 0)
1587 break;
1588# ifdef FEAT_XCLIPBOARD
1589 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001590# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001591 || STRICMP(argv[i], "--display") == 0
1592# endif
1593 )
1594 {
1595 if (i == argc - 1)
1596 mainerr_arg_missing((char_u *)argv[i]);
1597 xterm_display = argv[++i];
1598 }
1599# endif
1600# ifdef FEAT_CLIENTSERVER
1601 else if (STRICMP(argv[i], "--servername") == 0)
1602 {
1603 if (i == argc - 1)
1604 mainerr_arg_missing((char_u *)argv[i]);
1605 parmp->serverName_arg = (char_u *)argv[++i];
1606 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001607 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001608 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001609 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001610 {
1611 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001612# ifdef FEAT_GUI
1613 if (strstr(argv[i], "-wait") != 0)
1614 /* don't fork() when starting the GUI to edit files ourself */
1615 gui.dofork = FALSE;
1616# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001617 }
1618# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001619
1620# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1621# ifdef FEAT_GUI_W32
1622 else if (STRICMP(argv[i], "--windowid") == 0)
1623# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001624 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001625# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001626 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001627 long_u id;
1628 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001629
1630 if (i == argc - 1)
1631 mainerr_arg_missing((char_u *)argv[i]);
1632 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001633 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001634 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001635 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001636 if (count != 1)
1637 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1638 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001639# ifdef FEAT_GUI_W32
1640 win_socket_id = id;
1641# else
1642 gtk_socket_id = id;
1643# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001644 i++;
1645 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001646# endif
1647# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001648 else if (STRICMP(argv[i], "--echo-wid") == 0)
1649 echo_wid_arg = TRUE;
1650# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001651# ifndef FEAT_NETBEANS_INTG
1652 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001653 {
1654 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1655 mch_exit(2);
1656 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001657# endif
1658
Bram Moolenaar58d98232005-07-23 22:25:46 +00001659 }
1660#endif
1661}
1662
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001663#ifndef NO_VIM_MAIN
1664/*
1665 * Get a (optional) count for a Vim argument.
1666 */
1667 static int
1668get_number_arg(
1669 char_u *p, /* pointer to argument */
1670 int *idx, /* index in argument, is incremented */
1671 int def) /* default value */
1672{
1673 if (vim_isdigit(p[*idx]))
1674 {
1675 def = atoi((char *)&(p[*idx]));
1676 while (vim_isdigit(p[*idx]))
1677 *idx = *idx + 1;
1678 }
1679 return def;
1680}
1681
1682/*
1683 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1684 * If the executable name starts with "r" we disable shell commands.
1685 * If the next character is "e" we run in Easy mode.
1686 * If the next character is "g" we run the GUI version.
1687 * If the next characters are "view" we start in readonly mode.
1688 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1689 * If the next characters are "ex" we start in Ex mode. If it's followed
1690 * by "im" use improved Ex mode.
1691 */
1692 static void
1693parse_command_name(mparm_T *parmp)
1694{
1695 char_u *initstr;
1696
1697 initstr = gettail((char_u *)parmp->argv[0]);
1698
1699#ifdef MACOS_X_UNIX
1700 /* An issue has been seen when launching Vim in such a way that
1701 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1702 * executable or a symbolic link of it. Until this issue is resolved
1703 * we prohibit the GUI from being used.
1704 */
1705 if (STRCMP(initstr, parmp->argv[0]) == 0)
1706 disallow_gui = TRUE;
1707
1708 /* TODO: On MacOS X default to gui if argv[0] ends in:
1709 * /Vim.app/Contents/MacOS/Vim */
1710#endif
1711
1712#ifdef FEAT_EVAL
1713 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001714 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001715#endif
1716
1717 if (TOLOWER_ASC(initstr[0]) == 'r')
1718 {
1719 restricted = TRUE;
1720 ++initstr;
1721 }
1722
1723 /* Use evim mode for "evim" and "egvim", not for "editor". */
1724 if (TOLOWER_ASC(initstr[0]) == 'e'
1725 && (TOLOWER_ASC(initstr[1]) == 'v'
1726 || TOLOWER_ASC(initstr[1]) == 'g'))
1727 {
1728#ifdef FEAT_GUI
1729 gui.starting = TRUE;
1730#endif
1731 parmp->evim_mode = TRUE;
1732 ++initstr;
1733 }
1734
1735 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1736 if (TOLOWER_ASC(initstr[0]) == 'g')
1737 {
1738 main_start_gui();
1739#ifdef FEAT_GUI
1740 ++initstr;
1741#endif
1742 }
1743
1744 if (STRNICMP(initstr, "view", 4) == 0)
1745 {
1746 readonlymode = TRUE;
1747 curbuf->b_p_ro = TRUE;
1748 p_uc = 10000; /* don't update very often */
1749 initstr += 4;
1750 }
1751 else if (STRNICMP(initstr, "vim", 3) == 0)
1752 initstr += 3;
1753
1754 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1755 if (STRICMP(initstr, "diff") == 0)
1756 {
1757#ifdef FEAT_DIFF
1758 parmp->diff_mode = TRUE;
1759#else
1760 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1761 mch_errmsg("\n");
1762 mch_exit(2);
1763#endif
1764 }
1765
1766 if (STRNICMP(initstr, "ex", 2) == 0)
1767 {
1768 if (STRNICMP(initstr + 2, "im", 2) == 0)
1769 exmode_active = EXMODE_VIM;
1770 else
1771 exmode_active = EXMODE_NORMAL;
1772 change_compatible(TRUE); /* set 'compatible' */
1773 }
1774}
1775
Bram Moolenaar58d98232005-07-23 22:25:46 +00001776/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001777 * Scan the command line arguments.
1778 */
1779 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001780command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001781{
1782 int argc = parmp->argc;
1783 char **argv = parmp->argv;
1784 int argv_idx; /* index in argv[n][] */
1785 int had_minmin = FALSE; /* found "--" argument */
1786 int want_argument; /* option argument with argument */
1787 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001788 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001789 long n;
1790
1791 --argc;
1792 ++argv;
1793 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1794 while (argc > 0)
1795 {
1796 /*
1797 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1798 */
1799 if (argv[0][0] == '+' && !had_minmin)
1800 {
1801 if (parmp->n_commands >= MAX_ARG_CMDS)
1802 mainerr(ME_EXTRA_CMD, NULL);
1803 argv_idx = -1; /* skip to next argument */
1804 if (argv[0][1] == NUL)
1805 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1806 else
1807 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1808 }
1809
1810 /*
1811 * Optional argument.
1812 */
1813 else if (argv[0][0] == '-' && !had_minmin)
1814 {
1815 want_argument = FALSE;
1816 c = argv[0][argv_idx++];
1817#ifdef VMS
1818 /*
1819 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1820 * and "-/X" as "-X".
1821 */
1822 if (c == '/')
1823 {
1824 c = argv[0][argv_idx++];
1825 c = TOUPPER_ASC(c);
1826 }
1827 else
1828 c = TOLOWER_ASC(c);
1829#endif
1830 switch (c)
1831 {
1832 case NUL: /* "vim -" read from stdin */
1833 /* "ex -" silent mode */
1834 if (exmode_active)
1835 silent_mode = TRUE;
1836 else
1837 {
1838 if (parmp->edit_type != EDIT_NONE)
1839 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1840 parmp->edit_type = EDIT_STDIN;
1841 read_cmd_fd = 2; /* read from stderr instead of stdin */
1842 }
1843 argv_idx = -1; /* skip to next argument */
1844 break;
1845
1846 case '-': /* "--" don't take any more option arguments */
1847 /* "--help" give help message */
1848 /* "--version" give version message */
1849 /* "--literal" take files literally */
1850 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001851 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001852 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001853 /* "--noplugin[s]" skip plugins */
1854 /* "--cmd <cmd>" execute cmd before vimrc */
1855 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1856 usage();
1857 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1858 {
1859 Columns = 80; /* need to init Columns */
1860 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1861 list_version();
1862 msg_putchar('\n');
1863 msg_didout = FALSE;
1864 mch_exit(0);
1865 }
1866 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1867 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001868#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001869 parmp->literal = TRUE;
1870#endif
1871 }
1872 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1873 {
1874#ifdef FEAT_GUI
1875 gui.dofork = FALSE; /* don't fork() when starting GUI */
1876#endif
1877 }
1878 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1879 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001880 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1881 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001882 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1883 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001884 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1885 {
1886 want_argument = TRUE;
1887 argv_idx += 3;
1888 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001889 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1890 {
1891 want_argument = TRUE;
1892 argv_idx += 11;
1893 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001894#ifdef FEAT_CLIENTSERVER
1895 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1896 ; /* already processed -- no arg */
1897 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1898 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1899 {
1900 /* already processed -- snatch the following arg */
1901 if (argc > 1)
1902 {
1903 --argc;
1904 ++argv;
1905 }
1906 }
1907#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001908#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1909# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001910 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001911# else
1912 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1913# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001914 {
1915 /* already processed -- snatch the following arg */
1916 if (argc > 1)
1917 {
1918 --argc;
1919 ++argv;
1920 }
1921 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001922#endif
1923#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001924 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1925 {
1926 /* already processed, skip */
1927 }
1928#endif
1929 else
1930 {
1931 if (argv[0][argv_idx])
1932 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1933 had_minmin = TRUE;
1934 }
1935 if (!want_argument)
1936 argv_idx = -1; /* skip to next argument */
1937 break;
1938
1939 case 'A': /* "-A" start in Arabic mode */
1940#ifdef FEAT_ARABIC
1941 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1942#else
1943 mch_errmsg(_(e_noarabic));
1944 mch_exit(2);
1945#endif
1946 break;
1947
1948 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001949 /* Needs to be effective before expanding file names, because
1950 * for Win32 this makes us edit a shortcut file itself,
1951 * instead of the file it links to. */
1952 set_options_bin(curbuf->b_p_bin, 1, 0);
1953 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001954 break;
1955
1956 case 'C': /* "-C" Compatible */
1957 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001958 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001959 break;
1960
1961 case 'e': /* "-e" Ex mode */
1962 exmode_active = EXMODE_NORMAL;
1963 break;
1964
1965 case 'E': /* "-E" Improved Ex mode */
1966 exmode_active = EXMODE_VIM;
1967 break;
1968
1969 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1970 window directly, not with newcli */
1971#ifdef FEAT_GUI
1972 gui.dofork = FALSE; /* don't fork() when starting GUI */
1973#endif
1974 break;
1975
1976 case 'g': /* "-g" start GUI */
1977 main_start_gui();
1978 break;
1979
1980 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1981#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001982 p_fkmap = TRUE;
1983 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001984#else
1985 mch_errmsg(_(e_nofarsi));
1986 mch_exit(2);
1987#endif
1988 break;
1989
1990 case 'h': /* "-h" give help message */
1991#ifdef FEAT_GUI_GNOME
1992 /* Tell usage() to exit for "gvim". */
1993 gui.starting = FALSE;
1994#endif
1995 usage();
1996 break;
1997
1998 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1999#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002000 p_hkmap = TRUE;
2001 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002002#else
2003 mch_errmsg(_(e_nohebrew));
2004 mch_exit(2);
2005#endif
2006 break;
2007
2008 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2009#ifdef FEAT_LISP
2010 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2011 p_sm = TRUE;
2012#endif
2013 break;
2014
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002015 case 'M': /* "-M" no changes or writing of files */
2016 reset_modifiable();
2017 /* FALLTHROUGH */
2018
2019 case 'm': /* "-m" no writing of files */
2020 p_write = FALSE;
2021 break;
2022
2023 case 'y': /* "-y" easy mode */
2024#ifdef FEAT_GUI
2025 gui.starting = TRUE; /* start GUI a bit later */
2026#endif
2027 parmp->evim_mode = TRUE;
2028 break;
2029
2030 case 'N': /* "-N" Nocompatible */
2031 change_compatible(FALSE);
2032 break;
2033
2034 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002035#ifdef FEAT_NETBEANS_INTG
2036 /* checking for "-nb", netbeans parameters */
2037 if (argv[0][argv_idx] == 'b')
2038 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002039 netbeansArg = argv[0];
2040 argv_idx = -1; /* skip to next argument */
2041 }
2042 else
2043#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002044 parmp->no_swap_file = TRUE;
2045 break;
2046
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002047 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002048#ifdef TARGET_API_MAC_OSX
2049 /* For some reason on MacOS X, an argument like:
2050 -psn_0_10223617 is passed in when invoke from Finder
2051 or with the 'open' command */
2052 if (argv[0][argv_idx] == 's')
2053 {
2054 argv_idx = -1; /* bypass full -psn */
2055 main_start_gui();
2056 break;
2057 }
2058#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002059#ifdef FEAT_WINDOWS
2060 /* default is 0: open window for each file */
2061 parmp->window_count = get_number_arg((char_u *)argv[0],
2062 &argv_idx, 0);
2063 parmp->window_layout = WIN_TABS;
2064#endif
2065 break;
2066
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002067 case 'o': /* "-o[N]" open N horizontal split windows */
2068#ifdef FEAT_WINDOWS
2069 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002070 parmp->window_count = get_number_arg((char_u *)argv[0],
2071 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002072 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002073#endif
2074 break;
2075
2076 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002077#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002078 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002079 parmp->window_count = get_number_arg((char_u *)argv[0],
2080 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002081 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002082#endif
2083 break;
2084
2085#ifdef FEAT_QUICKFIX
2086 case 'q': /* "-q" QuickFix mode */
2087 if (parmp->edit_type != EDIT_NONE)
2088 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2089 parmp->edit_type = EDIT_QF;
2090 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2091 {
2092 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2093 argv_idx = -1;
2094 }
2095 else if (argc > 1) /* "-q {errorfile}" */
2096 want_argument = TRUE;
2097 break;
2098#endif
2099
2100 case 'R': /* "-R" readonly mode */
2101 readonlymode = TRUE;
2102 curbuf->b_p_ro = TRUE;
2103 p_uc = 10000; /* don't update very often */
2104 break;
2105
2106 case 'r': /* "-r" recovery mode */
2107 case 'L': /* "-L" recovery mode */
2108 recoverymode = 1;
2109 break;
2110
2111 case 's':
2112 if (exmode_active) /* "-s" silent (batch) mode */
2113 silent_mode = TRUE;
2114 else /* "-s {scriptin}" read from script file */
2115 want_argument = TRUE;
2116 break;
2117
2118 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2119 if (parmp->edit_type != EDIT_NONE)
2120 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2121 parmp->edit_type = EDIT_TAG;
2122 if (argv[0][argv_idx]) /* "-t{tag}" */
2123 {
2124 parmp->tagname = (char_u *)argv[0] + argv_idx;
2125 argv_idx = -1;
2126 }
2127 else /* "-t {tag}" */
2128 want_argument = TRUE;
2129 break;
2130
2131#ifdef FEAT_EVAL
2132 case 'D': /* "-D" Debugging */
2133 parmp->use_debug_break_level = 9999;
2134 break;
2135#endif
2136#ifdef FEAT_DIFF
2137 case 'd': /* "-d" 'diff' */
2138# ifdef AMIGA
2139 /* check for "-dev {device}" */
2140 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2141 want_argument = TRUE;
2142 else
2143# endif
2144 parmp->diff_mode = TRUE;
2145 break;
2146#endif
2147 case 'V': /* "-V{N}" Verbose level */
2148 /* default is 10: a little bit verbose */
2149 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2150 if (argv[0][argv_idx] != NUL)
2151 {
2152 set_option_value((char_u *)"verbosefile", 0L,
2153 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002154 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002155 }
2156 break;
2157
2158 case 'v': /* "-v" Vi-mode (as if called "vi") */
2159 exmode_active = 0;
2160#ifdef FEAT_GUI
2161 gui.starting = FALSE; /* don't start GUI */
2162#endif
2163 break;
2164
2165 case 'w': /* "-w{number}" set window height */
2166 /* "-w {scriptout}" write to script */
2167 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2168 {
2169 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2170 set_option_value((char_u *)"window", n, NULL, 0);
2171 break;
2172 }
2173 want_argument = TRUE;
2174 break;
2175
2176#ifdef FEAT_CRYPT
2177 case 'x': /* "-x" encrypted reading/writing of files */
2178 parmp->ask_for_key = TRUE;
2179 break;
2180#endif
2181
2182 case 'X': /* "-X" don't connect to X server */
2183#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2184 x_no_connect = TRUE;
2185#endif
2186 break;
2187
2188 case 'Z': /* "-Z" restricted mode */
2189 restricted = TRUE;
2190 break;
2191
2192 case 'c': /* "-c{command}" or "-c {command}" execute
2193 command */
2194 if (argv[0][argv_idx] != NUL)
2195 {
2196 if (parmp->n_commands >= MAX_ARG_CMDS)
2197 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002198 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2199 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002200 argv_idx = -1;
2201 break;
2202 }
2203 /*FALLTHROUGH*/
2204 case 'S': /* "-S {file}" execute Vim script */
2205 case 'i': /* "-i {viminfo}" use for viminfo */
2206#ifndef FEAT_DIFF
2207 case 'd': /* "-d {device}" device (for Amiga) */
2208#endif
2209 case 'T': /* "-T {terminal}" terminal name */
2210 case 'u': /* "-u {vimrc}" vim inits file */
2211 case 'U': /* "-U {gvimrc}" gvim inits file */
2212 case 'W': /* "-W {scriptout}" overwrite */
2213#ifdef FEAT_GUI_W32
2214 case 'P': /* "-P {parent title}" MDI parent */
2215#endif
2216 want_argument = TRUE;
2217 break;
2218
2219 default:
2220 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2221 }
2222
2223 /*
2224 * Handle option arguments with argument.
2225 */
2226 if (want_argument)
2227 {
2228 /*
2229 * Check for garbage immediately after the option letter.
2230 */
2231 if (argv[0][argv_idx] != NUL)
2232 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2233
2234 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002235 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002236 mainerr_arg_missing((char_u *)argv[0]);
2237 ++argv;
2238 argv_idx = -1;
2239
2240 switch (c)
2241 {
2242 case 'c': /* "-c {command}" execute command */
2243 case 'S': /* "-S {file}" execute Vim script */
2244 if (parmp->n_commands >= MAX_ARG_CMDS)
2245 mainerr(ME_EXTRA_CMD, NULL);
2246 if (c == 'S')
2247 {
2248 char *a;
2249
2250 if (argc < 1)
2251 /* "-S" without argument: use default session file
2252 * name. */
2253 a = SESSION_FILE;
2254 else if (argv[0][0] == '-')
2255 {
2256 /* "-S" followed by another option: use default
2257 * session file name. */
2258 a = SESSION_FILE;
2259 ++argc;
2260 --argv;
2261 }
2262 else
2263 a = argv[0];
2264 p = alloc((unsigned)(STRLEN(a) + 4));
2265 if (p == NULL)
2266 mch_exit(2);
2267 sprintf((char *)p, "so %s", a);
2268 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2269 parmp->commands[parmp->n_commands++] = p;
2270 }
2271 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002272 parmp->commands[parmp->n_commands++] =
2273 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002274 break;
2275
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002276 case '-':
2277 if (argv[-1][2] == 'c')
2278 {
2279 /* "--cmd {command}" execute command */
2280 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2281 mainerr(ME_EXTRA_CMD, NULL);
2282 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002283 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002284 }
2285 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002286 break;
2287
2288 /* case 'd': -d {device} is handled in mch_check_win() for the
2289 * Amiga */
2290
2291#ifdef FEAT_QUICKFIX
2292 case 'q': /* "-q {errorfile}" QuickFix mode */
2293 parmp->use_ef = (char_u *)argv[0];
2294 break;
2295#endif
2296
2297 case 'i': /* "-i {viminfo}" use for viminfo */
2298 use_viminfo = (char_u *)argv[0];
2299 break;
2300
2301 case 's': /* "-s {scriptin}" read from script file */
2302 if (scriptin[0] != NULL)
2303 {
2304scripterror:
2305 mch_errmsg(_("Attempt to open script file again: \""));
2306 mch_errmsg(argv[-1]);
2307 mch_errmsg(" ");
2308 mch_errmsg(argv[0]);
2309 mch_errmsg("\"\n");
2310 mch_exit(2);
2311 }
2312 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2313 {
2314 mch_errmsg(_("Cannot open for reading: \""));
2315 mch_errmsg(argv[0]);
2316 mch_errmsg("\"\n");
2317 mch_exit(2);
2318 }
2319 if (save_typebuf() == FAIL)
2320 mch_exit(2); /* out of memory */
2321 break;
2322
2323 case 't': /* "-t {tag}" */
2324 parmp->tagname = (char_u *)argv[0];
2325 break;
2326
2327 case 'T': /* "-T {terminal}" terminal name */
2328 /*
2329 * The -T term argument is always available and when
2330 * HAVE_TERMLIB is supported it overrides the environment
2331 * variable TERM.
2332 */
2333#ifdef FEAT_GUI
2334 if (term_is_gui((char_u *)argv[0]))
2335 gui.starting = TRUE; /* start GUI a bit later */
2336 else
2337#endif
2338 parmp->term = (char_u *)argv[0];
2339 break;
2340
2341 case 'u': /* "-u {vimrc}" vim inits file */
2342 parmp->use_vimrc = (char_u *)argv[0];
2343 break;
2344
2345 case 'U': /* "-U {gvimrc}" gvim inits file */
2346#ifdef FEAT_GUI
2347 use_gvimrc = (char_u *)argv[0];
2348#endif
2349 break;
2350
2351 case 'w': /* "-w {nr}" 'window' value */
2352 /* "-w {scriptout}" append to script file */
2353 if (vim_isdigit(*((char_u *)argv[0])))
2354 {
2355 argv_idx = 0;
2356 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2357 set_option_value((char_u *)"window", n, NULL, 0);
2358 argv_idx = -1;
2359 break;
2360 }
2361 /*FALLTHROUGH*/
2362 case 'W': /* "-W {scriptout}" overwrite script file */
2363 if (scriptout != NULL)
2364 goto scripterror;
2365 if ((scriptout = mch_fopen(argv[0],
2366 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2367 {
2368 mch_errmsg(_("Cannot open for script output: \""));
2369 mch_errmsg(argv[0]);
2370 mch_errmsg("\"\n");
2371 mch_exit(2);
2372 }
2373 break;
2374
2375#ifdef FEAT_GUI_W32
2376 case 'P': /* "-P {parent title}" MDI parent */
2377 gui_mch_set_parent(argv[0]);
2378 break;
2379#endif
2380 }
2381 }
2382 }
2383
2384 /*
2385 * File name argument.
2386 */
2387 else
2388 {
2389 argv_idx = -1; /* skip to next argument */
2390
2391 /* Check for only one type of editing. */
2392 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2393 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2394 parmp->edit_type = EDIT_FILE;
2395
2396#ifdef MSWIN
2397 /* Remember if the argument was a full path before changing
2398 * slashes to backslashes. */
2399 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2400 parmp->full_path = TRUE;
2401#endif
2402
2403 /* Add the file to the global argument list. */
2404 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2405 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2406 mch_exit(2);
2407#ifdef FEAT_DIFF
2408 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2409 && !mch_isdir(alist_name(&GARGLIST[0])))
2410 {
2411 char_u *r;
2412
2413 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2414 if (r != NULL)
2415 {
2416 vim_free(p);
2417 p = r;
2418 }
2419 }
2420#endif
2421#if defined(__CYGWIN32__) && !defined(WIN32)
2422 /*
2423 * If vim is invoked by non-Cygwin tools, convert away any
2424 * DOS paths, so things like .swp files are created correctly.
2425 * Look for evidence of non-Cygwin paths before we bother.
2426 * This is only for when using the Unix files.
2427 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002428 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002429 {
2430 char posix_path[PATH_MAX];
2431
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002432# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2433 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2434# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002435 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002436# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002438 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439 if (p == NULL)
2440 mch_exit(2);
2441 }
2442#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002443
2444#ifdef USE_FNAME_CASE
2445 /* Make the case of the file name match the actual file. */
2446 fname_case(p, 0);
2447#endif
2448
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002450#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002451 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002452#else
2453 2 /* add buffer number now and use curbuf */
2454#endif
2455 );
2456
2457#if defined(FEAT_MBYTE) && defined(WIN32)
2458 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459 /* Remember this argument has been added to the argument list.
2460 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002461 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002462# ifdef FEAT_DIFF
2463 parmp->diff_mode
2464# else
2465 FALSE
2466# endif
2467 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002468 }
2469#endif
2470 }
2471
2472 /*
2473 * If there are no more letters after the current "-", go to next
2474 * argument. argv_idx is set to -1 when the current argument is to be
2475 * skipped.
2476 */
2477 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2478 {
2479 --argc;
2480 ++argv;
2481 argv_idx = 1;
2482 }
2483 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002484
2485#ifdef FEAT_EVAL
2486 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2487 * one. */
2488 if (parmp->n_commands > 0)
2489 {
2490 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2491 if (p != NULL)
2492 {
2493 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2494 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2495 vim_free(p);
2496 }
2497 }
2498#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002499}
2500
2501/*
2502 * Print a warning if stdout is not a terminal.
2503 * When starting in Ex mode and commands come from a file, set Silent mode.
2504 */
2505 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002506check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002507{
2508 int input_isatty; /* is active input a terminal? */
2509
2510 input_isatty = mch_input_isatty();
2511 if (exmode_active)
2512 {
2513 if (!input_isatty)
2514 silent_mode = TRUE;
2515 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002516 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002517#ifdef FEAT_GUI
2518 /* don't want the delay when started from the desktop */
2519 && !gui.starting
2520#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002521 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002522 {
2523#ifdef NBDEBUG
2524 /*
2525 * This shouldn't be necessary. But if I run netbeans with the log
2526 * output coming to the console and XOpenDisplay fails, I get vim
2527 * trying to start with input/output to my console tty. This fills my
2528 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002529 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002530 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002531 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002532 {
2533 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2534 exit(1);
2535 }
2536#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002537#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2538 if (is_cygpty_used())
2539 {
2540 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2541 exit(1);
2542 }
2543#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002544 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2546 if (!input_isatty)
2547 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2548 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002549 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2550 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551 if (scriptin[0] == NULL)
2552 ui_delay(2000L, TRUE);
2553 TIME_MSG("Warning delay");
2554 }
2555}
2556
2557/*
2558 * Read text from stdin.
2559 */
2560 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002561read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562{
2563 int i;
2564
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002565#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002566 /* When getting the ATTENTION prompt here, use a dialog */
2567 swap_exists_action = SEA_DIALOG;
2568#endif
2569 no_wait_return = TRUE;
2570 i = msg_didany;
2571 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002572 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573 no_wait_return = FALSE;
2574 msg_didany = i;
2575 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002576#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002577 check_swap_exists_action();
2578#endif
2579#if !(defined(AMIGA) || defined(MACOS))
2580 /*
2581 * Close stdin and dup it from stderr. Required for GPM to work
2582 * properly, and for running external commands.
2583 * Is there any other system that cannot do this?
2584 */
2585 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002586 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587#endif
2588}
2589
2590/*
2591 * Create the requested number of windows and edit buffers in them.
2592 * Also does recovery if "recoverymode" set.
2593 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002595create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596{
2597#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002598 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002599 int done = 0;
2600
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002601 /*
2602 * Create the number of windows that was requested.
2603 */
2604 if (parmp->window_count == -1) /* was not set */
2605 parmp->window_count = 1;
2606 if (parmp->window_count == 0)
2607 parmp->window_count = GARGCOUNT;
2608 if (parmp->window_count > 1)
2609 {
2610 /* Don't change the windows if there was a command in .vimrc that
2611 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002612 if (parmp->window_layout == 0)
2613 parmp->window_layout = WIN_HOR;
2614 if (parmp->window_layout == WIN_TABS)
2615 {
2616 parmp->window_count = make_tabpages(parmp->window_count);
2617 TIME_MSG("making tab pages");
2618 }
2619 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002620 {
2621 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002622 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002623 TIME_MSG("making windows");
2624 }
2625 else
2626 parmp->window_count = win_count();
2627 }
2628 else
2629 parmp->window_count = 1;
2630#endif
2631
2632 if (recoverymode) /* do recover */
2633 {
2634 msg_scroll = TRUE; /* scroll message up */
2635 ml_recover();
2636 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2637 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002638 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002639 }
2640 else
2641 {
2642 /*
2643 * Open a buffer for windows that don't have one yet.
2644 * Commands in the .vimrc might have loaded a file or split the window.
2645 * Watch out for autocommands that delete a window.
2646 */
2647#ifdef FEAT_AUTOCMD
2648 /*
2649 * Don't execute Win/Buf Enter/Leave autocommands here
2650 */
2651 ++autocmd_no_enter;
2652 ++autocmd_no_leave;
2653#endif
2654#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002655 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002656 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002657 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002658 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002659 {
2660 if (parmp->window_layout == WIN_TABS)
2661 goto_tabpage(1);
2662 else
2663 curwin = firstwin;
2664 }
2665 else if (parmp->window_layout == WIN_TABS)
2666 {
2667 if (curtab->tp_next == NULL)
2668 break;
2669 goto_tabpage(0);
2670 }
2671 else
2672 {
2673 if (curwin->w_next == NULL)
2674 break;
2675 curwin = curwin->w_next;
2676 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002677 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002678#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002679 curbuf = curwin->w_buffer;
2680 if (curbuf->b_ml.ml_mfp == NULL)
2681 {
2682#ifdef FEAT_FOLDING
2683 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2684 if (p_fdls >= 0)
2685 curwin->w_p_fdl = p_fdls;
2686#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002687#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002688 /* When getting the ATTENTION prompt here, use a dialog */
2689 swap_exists_action = SEA_DIALOG;
2690#endif
2691 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002692
2693 /* create memfile, read file */
2694 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002695
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002696#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002697 if (swap_exists_action == SEA_QUIT)
2698 {
2699 if (got_int || only_one_window())
2700 {
2701 /* abort selected or quit and only one window */
2702 did_emsg = FALSE; /* avoid hit-enter prompt */
2703 getout(1);
2704 }
2705 /* We can't close the window, it would disturb what
2706 * happens next. Clear the file name and set the arg
2707 * index to -1 to delete it later. */
2708 setfname(curbuf, NULL, NULL, FALSE);
2709 curwin->w_arg_idx = -1;
2710 swap_exists_action = SEA_NONE;
2711 }
2712 else
2713 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002714#endif
2715#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002716 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717#endif
2718 }
2719#ifdef FEAT_WINDOWS
2720 ui_breakcheck();
2721 if (got_int)
2722 {
2723 (void)vgetc(); /* only break the file loading, not the rest */
2724 break;
2725 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002727#endif
2728#ifdef FEAT_WINDOWS
2729 if (parmp->window_layout == WIN_TABS)
2730 goto_tabpage(1);
2731 else
2732 curwin = firstwin;
2733 curbuf = curwin->w_buffer;
2734#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002735#ifdef FEAT_AUTOCMD
2736 --autocmd_no_enter;
2737 --autocmd_no_leave;
2738#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739 }
2740}
2741
2742#ifdef FEAT_WINDOWS
2743 /*
2744 * If opened more than one window, start editing files in the other
2745 * windows. make_windows() has already opened the windows.
2746 */
2747 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002748edit_buffers(
2749 mparm_T *parmp,
2750 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002751{
2752 int arg_idx; /* index in argument list */
2753 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002754 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002755 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002756
2757# ifdef FEAT_AUTOCMD
2758 /*
2759 * Don't execute Win/Buf Enter/Leave autocommands here
2760 */
2761 ++autocmd_no_enter;
2762 ++autocmd_no_leave;
2763# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002764
2765 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2766 if (curwin->w_arg_idx == -1)
2767 {
2768 win_close(curwin, TRUE);
2769 advance = FALSE;
2770 }
2771
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002772 arg_idx = 1;
2773 for (i = 1; i < parmp->window_count; ++i)
2774 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002775 if (cwd != NULL)
2776 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002777 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2778 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002779 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002780 ++arg_idx;
2781 win_close(curwin, TRUE);
2782 advance = FALSE;
2783 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002784 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785
Bram Moolenaar84212822006-11-07 21:59:47 +00002786 if (advance)
2787 {
2788 if (parmp->window_layout == WIN_TABS)
2789 {
2790 if (curtab->tp_next == NULL) /* just checking */
2791 break;
2792 goto_tabpage(0);
2793 }
2794 else
2795 {
2796 if (curwin->w_next == NULL) /* just checking */
2797 break;
2798 win_enter(curwin->w_next, FALSE);
2799 }
2800 }
2801 advance = TRUE;
2802
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002803 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002804 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002805 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2806 {
2807 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002808 /* Edit file from arg list, if there is one. When "Quit" selected
2809 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002810# ifdef HAS_SWAP_EXISTS_ACTION
2811 swap_exists_did_quit = FALSE;
2812# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002813 (void)do_ecmd(0, arg_idx < GARGCOUNT
2814 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002815 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002816# ifdef HAS_SWAP_EXISTS_ACTION
2817 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002818 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002819 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002820 if (got_int || only_one_window())
2821 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002822 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002823 did_emsg = FALSE; /* avoid hit-enter prompt */
2824 getout(1);
2825 }
2826 win_close(curwin, TRUE);
2827 advance = FALSE;
2828 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002829# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002830 if (arg_idx == GARGCOUNT - 1)
2831 arg_had_last = TRUE;
2832 ++arg_idx;
2833 }
2834 ui_breakcheck();
2835 if (got_int)
2836 {
2837 (void)vgetc(); /* only break the file loading, not the rest */
2838 break;
2839 }
2840 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002841
2842 if (parmp->window_layout == WIN_TABS)
2843 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002844# ifdef FEAT_AUTOCMD
2845 --autocmd_no_enter;
2846# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002847
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002848 /* make the first window the current window */
2849 win = firstwin;
2850#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2851 /* Avoid making a preview window the current window. */
2852 while (win->w_p_pvw)
2853 {
2854 win = win->w_next;
2855 if (win == NULL)
2856 {
2857 win = firstwin;
2858 break;
2859 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002860 }
2861#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002862 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002863
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002864# ifdef FEAT_AUTOCMD
2865 --autocmd_no_leave;
2866# endif
2867 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002868 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002869 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2870}
2871#endif /* FEAT_WINDOWS */
2872
2873/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002874 * Execute the commands from --cmd arguments "cmds[cnt]".
2875 */
2876 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002877exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002878{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002879 char_u **cmds = parmp->pre_commands;
2880 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002881 int i;
2882
2883 if (cnt > 0)
2884 {
2885 curwin->w_cursor.lnum = 0; /* just in case.. */
2886 sourcing_name = (char_u *)_("pre-vimrc command line");
2887# ifdef FEAT_EVAL
2888 current_SID = SID_CMDARG;
2889# endif
2890 for (i = 0; i < cnt; ++i)
2891 do_cmdline_cmd(cmds[i]);
2892 sourcing_name = NULL;
2893# ifdef FEAT_EVAL
2894 current_SID = 0;
2895# endif
2896 TIME_MSG("--cmd commands");
2897 }
2898}
2899
2900/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 * Execute "+", "-c" and "-S" arguments.
2902 */
2903 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002904exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002905{
2906 int i;
2907
2908 /*
2909 * We start commands on line 0, make "vim +/pat file" match a
2910 * pattern on line 1. But don't move the cursor when an autocommand
2911 * with g`" was used.
2912 */
2913 msg_scroll = TRUE;
2914 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2915 curwin->w_cursor.lnum = 0;
2916 sourcing_name = (char_u *)"command line";
2917#ifdef FEAT_EVAL
2918 current_SID = SID_CARG;
2919#endif
2920 for (i = 0; i < parmp->n_commands; ++i)
2921 {
2922 do_cmdline_cmd(parmp->commands[i]);
2923 if (parmp->cmds_tofree[i])
2924 vim_free(parmp->commands[i]);
2925 }
2926 sourcing_name = NULL;
2927#ifdef FEAT_EVAL
2928 current_SID = 0;
2929#endif
2930 if (curwin->w_cursor.lnum == 0)
2931 curwin->w_cursor.lnum = 1;
2932
2933 if (!exmode_active)
2934 msg_scroll = FALSE;
2935
2936#ifdef FEAT_QUICKFIX
2937 /* When started with "-q errorfile" jump to first error again. */
2938 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002939 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002940#endif
2941 TIME_MSG("executing command arguments");
2942}
2943
2944/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002945 * Source startup scripts.
2946 */
2947 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002948source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002949{
2950 int i;
2951
2952 /*
2953 * For "evim" source evim.vim first of all, so that the user can overrule
2954 * any things he doesn't like.
2955 */
2956 if (parmp->evim_mode)
2957 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002958 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002959 TIME_MSG("source evim file");
2960 }
2961
2962 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002963 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002964 * nothing else.
2965 */
2966 if (parmp->use_vimrc != NULL)
2967 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002968 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2969 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002970 {
2971#ifdef FEAT_GUI
2972 if (use_gvimrc == NULL) /* don't load gvimrc either */
2973 use_gvimrc = parmp->use_vimrc;
2974#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002975 }
2976 else
2977 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002978 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002979 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2980 }
2981 }
2982 else if (!silent_mode)
2983 {
2984#ifdef AMIGA
2985 struct Process *proc = (struct Process *)FindTask(0L);
2986 APTR save_winptr = proc->pr_WindowPtr;
2987
2988 /* Avoid a requester here for a volume that doesn't exist. */
2989 proc->pr_WindowPtr = (APTR)-1L;
2990#endif
2991
2992 /*
2993 * Get system wide defaults, if the file name is defined.
2994 */
2995#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002996 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002997#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002998#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002999 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003000#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001
3002 /*
3003 * Try to read initialization commands from the following places:
3004 * - environment variable VIMINIT
3005 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3006 * - second user vimrc file ($VIM/.vimrc for Dos)
3007 * - environment variable EXINIT
3008 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3009 * - second user exrc file ($VIM/.exrc for Dos)
3010 * The first that exists is used, the rest is ignored.
3011 */
3012 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3013 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003014 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003015#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003016 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3017 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003018#endif
3019#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003020 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3021 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003022#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003023#ifdef USR_VIMRC_FILE4
3024 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3025 DOSO_VIMRC) == FAIL
3026#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003027 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003028 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003030 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003031#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003032 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003033 {
3034 /* When no .vimrc file was found: source defaults.vim. */
3035 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003036 }
3037 }
3038
3039 /*
3040 * Read initialization commands from ".vimrc" or ".exrc" in current
3041 * directory. This is only done if the 'exrc' option is set.
3042 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003043 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003044 * option has been reset in environment of global ".exrc" or ".vimrc".
3045 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3046 * SYS_VIMRC_FILE.
3047 */
3048 if (p_exrc)
3049 {
3050#if defined(UNIX) || defined(VMS)
3051 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3052 if (!file_owned(VIMRC_FILE))
3053#endif
3054 secure = p_secure;
3055
3056 i = FAIL;
3057 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3058 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3059#ifdef USR_VIMRC_FILE2
3060 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3061 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3062#endif
3063#ifdef USR_VIMRC_FILE3
3064 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3065 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3066#endif
3067#ifdef SYS_VIMRC_FILE
3068 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3069 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3070#endif
3071 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003072 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003073
3074 if (i == FAIL)
3075 {
3076#if defined(UNIX) || defined(VMS)
3077 /* if ".exrc" is not owned by user set 'secure' mode */
3078 if (!file_owned(EXRC_FILE))
3079 secure = p_secure;
3080 else
3081 secure = 0;
3082#endif
3083 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3084 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3085#ifdef USR_EXRC_FILE2
3086 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3087 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3088#endif
3089 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003090 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003091 }
3092 }
3093 if (secure == 2)
3094 need_wait_return = TRUE;
3095 secure = 0;
3096#ifdef AMIGA
3097 proc->pr_WindowPtr = save_winptr;
3098#endif
3099 }
3100 TIME_MSG("sourcing vimrc file(s)");
3101}
3102
3103/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003104 * Setup to start using the GUI. Exit with an error when not available.
3105 */
3106 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003107main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003108{
3109#ifdef FEAT_GUI
3110 gui.starting = TRUE; /* start GUI a bit later */
3111#else
3112 mch_errmsg(_(e_nogvim));
3113 mch_errmsg("\n");
3114 mch_exit(2);
3115#endif
3116}
3117
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003118#endif /* NO_VIM_MAIN */
3119
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003120/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003121 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003122 * Returns FAIL if the environment variable was not executed, OK otherwise.
3123 */
3124 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003125process_env(
3126 char_u *env,
3127 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003128{
3129 char_u *initstr;
3130 char_u *save_sourcing_name;
3131 linenr_T save_sourcing_lnum;
3132#ifdef FEAT_EVAL
3133 scid_T save_sid;
3134#endif
3135
3136 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3137 {
3138 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003139 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003140 save_sourcing_name = sourcing_name;
3141 save_sourcing_lnum = sourcing_lnum;
3142 sourcing_name = env;
3143 sourcing_lnum = 0;
3144#ifdef FEAT_EVAL
3145 save_sid = current_SID;
3146 current_SID = SID_ENV;
3147#endif
3148 do_cmdline_cmd(initstr);
3149 sourcing_name = save_sourcing_name;
3150 sourcing_lnum = save_sourcing_lnum;
3151#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003152 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003153#endif
3154 return OK;
3155 }
3156 return FAIL;
3157}
3158
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003159#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003160/*
3161 * Return TRUE if we are certain the user owns the file "fname".
3162 * Used for ".vimrc" and ".exrc".
3163 * Use both stat() and lstat() for extra security.
3164 */
3165 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003166file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003167{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003168 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003169# ifdef UNIX
3170 uid_t uid = getuid();
3171# else /* VMS */
3172 uid_t uid = ((getgid() << 16) | getuid());
3173# endif
3174
3175 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3176# ifdef HAVE_LSTAT
3177 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3178# endif
3179 );
3180}
3181#endif
3182
3183/*
3184 * Give an error message main_errors["n"] and exit.
3185 */
3186 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003187mainerr(
3188 int n, /* one of the ME_ defines */
3189 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003190{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003191#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003192 reset_signals(); /* kill us with CTRL-C here, if you like */
3193#endif
3194
3195 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003196 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003197 mch_errmsg(_(main_errors[n]));
3198 if (str != NULL)
3199 {
3200 mch_errmsg(": \"");
3201 mch_errmsg((char *)str);
3202 mch_errmsg("\"");
3203 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003204 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003205
3206 mch_exit(1);
3207}
3208
3209 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003210mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003211{
3212 mainerr(ME_ARG_MISSING, str);
3213}
3214
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003215#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003216/*
3217 * print a message with three spaces prepended and '\n' appended.
3218 */
3219 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003220main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003221{
3222 mch_msg(" ");
3223 mch_msg(s);
3224 mch_msg("\n");
3225}
3226
3227/*
3228 * Print messages for "vim -h" or "vim --help" and exit.
3229 */
3230 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003231usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003232{
3233 int i;
3234 static char *(use[]) =
3235 {
3236 N_("[file ..] edit specified file(s)"),
3237 N_("- read text from stdin"),
3238 N_("-t tag edit file where tag is defined"),
3239#ifdef FEAT_QUICKFIX
3240 N_("-q [errorfile] edit file with first error")
3241#endif
3242 };
3243
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003244#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003245 reset_signals(); /* kill us with CTRL-C here, if you like */
3246#endif
3247
3248 mch_msg(longVersion);
3249 mch_msg(_("\n\nusage:"));
3250 for (i = 0; ; ++i)
3251 {
3252 mch_msg(_(" vim [arguments] "));
3253 mch_msg(_(use[i]));
3254 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3255 break;
3256 mch_msg(_("\n or:"));
3257 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003258#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003259 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003260#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003261
3262 mch_msg(_("\n\nArguments:\n"));
3263 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003264#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265 main_msg(_("--literal\t\tDon't expand wildcards"));
3266#endif
3267#ifdef FEAT_OLE
3268 main_msg(_("-register\t\tRegister this gvim for OLE"));
3269 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3270#endif
3271#ifdef FEAT_GUI
3272 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3273 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3274#endif
3275 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3276 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003277 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003278 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3279#ifdef FEAT_DIFF
3280 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3281#endif
3282 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3283 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3284 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3285 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3286 main_msg(_("-M\t\t\tModifications in text not allowed"));
3287 main_msg(_("-b\t\t\tBinary mode"));
3288#ifdef FEAT_LISP
3289 main_msg(_("-l\t\t\tLisp mode"));
3290#endif
3291 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3292 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003293 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3294#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003295 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003296#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003297 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3298 main_msg(_("-r\t\t\tList swap files and exit"));
3299 main_msg(_("-r (with file name)\tRecover crashed session"));
3300 main_msg(_("-L\t\t\tSame as -r"));
3301#ifdef AMIGA
3302 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3303 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3304#endif
3305#ifdef FEAT_ARABIC
3306 main_msg(_("-A\t\t\tstart in Arabic mode"));
3307#endif
3308#ifdef FEAT_RIGHTLEFT
3309 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3310#endif
3311#ifdef FEAT_FKMAP
3312 main_msg(_("-F\t\t\tStart in Farsi mode"));
3313#endif
3314 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003315 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003316 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003317 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3318#ifdef FEAT_GUI
3319 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3320#endif
3321 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003322#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003323 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003324 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3325 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003326#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003327 main_msg(_("+\t\t\tStart at end of file"));
3328 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003329 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3331 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3332 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3333 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3334 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3335#ifdef FEAT_CRYPT
3336 main_msg(_("-x\t\t\tEdit encrypted files"));
3337#endif
3338#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3339# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3340 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3341# endif
3342 main_msg(_("-X\t\t\tDo not connect to X server"));
3343#endif
3344#ifdef FEAT_CLIENTSERVER
3345 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3346 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3347 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3348 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003349# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003350 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003351# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3353 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3354 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3355 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3356#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003357#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003358 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003359#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003360#ifdef FEAT_VIMINFO
3361 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3362#endif
3363 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3364 main_msg(_("--version\t\tPrint version information and exit"));
3365
3366#ifdef FEAT_GUI_X11
3367# ifdef FEAT_GUI_MOTIF
3368 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3369# else
3370# ifdef FEAT_GUI_ATHENA
3371# ifdef FEAT_GUI_NEXTAW
3372 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3373# else
3374 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3375# endif
3376# endif
3377# endif
3378 main_msg(_("-display <display>\tRun vim on <display>"));
3379 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003380 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3381 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3382 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3383 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3384 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3385 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3386 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3387 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3388# ifdef FEAT_GUI_ATHENA
3389 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3390# endif
3391 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3392 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3393 main_msg(_("-xrm <resource>\tSet the specified resource"));
3394#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395#ifdef FEAT_GUI_GTK
3396 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3397 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3398 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3399 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3400 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003401 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003403 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405#ifdef FEAT_GUI_W32
3406 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003407 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408#endif
3409
3410#ifdef FEAT_GUI_GNOME
3411 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3412 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003413 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003414 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003415 gui.dofork = FALSE;
3416 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003417 else
3418#endif
3419 mch_exit(0);
3420}
3421
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003422#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003423/*
3424 * Check the result of the ATTENTION dialog:
3425 * When "Quit" selected, exit Vim.
3426 * When "Recover" selected, recover the file.
3427 */
3428 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003429check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430{
3431 if (swap_exists_action == SEA_QUIT)
3432 getout(1);
3433 handle_swap_exists(NULL);
3434}
3435#endif
3436
Bram Moolenaar08cab962017-03-04 14:37:18 +01003437#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003438
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003440static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003441
3442static struct timeval prev_timeval;
3443
Bram Moolenaar3f269672009-11-03 11:11:11 +00003444# ifdef WIN3264
3445/*
3446 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3447 */
3448 static int
3449gettimeofday(struct timeval *tv, char *dummy)
3450{
3451 long t = clock();
3452 tv->tv_sec = t / CLOCKS_PER_SEC;
3453 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3454 return 0;
3455}
3456# endif
3457
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003458/*
3459 * Save the previous time before doing something that could nest.
3460 * set "*tv_rel" to the time elapsed so far.
3461 */
3462 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003463time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464{
3465 *((struct timeval *)tv_rel) = prev_timeval;
3466 gettimeofday(&prev_timeval, NULL);
3467 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3468 - ((struct timeval *)tv_rel)->tv_usec;
3469 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3470 - ((struct timeval *)tv_rel)->tv_sec;
3471 if (((struct timeval *)tv_rel)->tv_usec < 0)
3472 {
3473 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3474 --((struct timeval *)tv_rel)->tv_sec;
3475 }
3476 *(struct timeval *)tv_start = prev_timeval;
3477}
3478
3479/*
3480 * Compute the previous time after doing something that could nest.
3481 * Subtract "*tp" from prev_timeval;
3482 * Note: The arguments are (void *) to avoid trouble with systems that don't
3483 * have struct timeval.
3484 */
3485 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003486time_pop(
3487 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003488{
3489 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3490 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3491 if (prev_timeval.tv_usec < 0)
3492 {
3493 prev_timeval.tv_usec += 1000000;
3494 --prev_timeval.tv_sec;
3495 }
3496}
3497
3498 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003499time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003500{
3501 long usec;
3502 long msec;
3503
3504 usec = now->tv_usec - then->tv_usec;
3505 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3506 usec = usec % 1000L;
3507 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3508}
3509
3510 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003511time_msg(
3512 char *mesg,
3513 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003514 (struct timeval *) */
3515{
3516 static struct timeval start;
3517 struct timeval now;
3518
3519 if (time_fd != NULL)
3520 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003521 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003522 {
3523 gettimeofday(&start, NULL);
3524 prev_timeval = start;
3525 fprintf(time_fd, "\n\ntimes in msec\n");
3526 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3527 fprintf(time_fd, " clock elapsed: other lines\n\n");
3528 }
3529 gettimeofday(&now, NULL);
3530 time_diff(&start, &now);
3531 if (((struct timeval *)tv_start) != NULL)
3532 {
3533 fprintf(time_fd, " ");
3534 time_diff(((struct timeval *)tv_start), &now);
3535 }
3536 fprintf(time_fd, " ");
3537 time_diff(&prev_timeval, &now);
3538 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003539 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003540 }
3541}
3542
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003543#endif
3544
Bram Moolenaar595297d2017-03-04 19:11:12 +01003545#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003546 static void
3547set_progpath(char_u *argv0)
3548{
3549 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003550
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003551# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003552 char buf[PATH_MAX + 1];
3553 ssize_t len;
3554
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003555 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003556 if (len > 0)
3557 {
3558 buf[len] = NUL;
3559 val = (char_u *)buf;
3560 }
3561# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003562 /* A relative path containing a "/" will become invalid when using ":cd",
3563 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003564 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3565 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003566# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003567 char_u *path = NULL;
3568
3569 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3570 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003571# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003572 char_u buf[MAXPATHL];
3573
Bram Moolenaar43663192017-03-05 14:29:12 +01003574 if (!mch_isFullName(argv0))
3575 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003576 if (gettail(argv0) != argv0
3577 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3578 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003579 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003580# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003581# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003582
Bram Moolenaar08cab962017-03-04 14:37:18 +01003583 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003584
Bram Moolenaar066029e2017-03-05 15:19:32 +01003585# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003586 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003587# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003588}
3589
3590#endif /* NO_VIM_MAIN */
3591
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003592#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003593
3594/*
3595 * Common code for the X command server and the Win32 command server.
3596 */
3597
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003598static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003599
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003600/*
3601 * Do the client-server stuff, unless "--servername ''" was used.
3602 */
3603 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003604exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003605{
3606 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3607 {
3608# ifdef WIN32
3609 /* Initialise the client/server messaging infrastructure. */
3610 serverInitMessaging();
3611# endif
3612
3613 /*
3614 * When a command server argument was found, execute it. This may
3615 * exit Vim when it was successful. Otherwise it's executed further
3616 * on. Remember the encoding used here in "serverStrEnc".
3617 */
3618 if (parmp->serverArg)
3619 {
3620 cmdsrv_main(&parmp->argc, parmp->argv,
3621 parmp->serverName_arg, &parmp->serverStr);
3622# ifdef FEAT_MBYTE
3623 parmp->serverStrEnc = vim_strsave(p_enc);
3624# endif
3625 }
3626
3627 /* If we're still running, get the name to register ourselves.
3628 * On Win32 can register right now, for X11 need to setup the
3629 * clipboard first, it's further down. */
3630 parmp->servername = serverMakeName(parmp->serverName_arg,
3631 parmp->argv[0]);
3632# ifdef WIN32
3633 if (parmp->servername != NULL)
3634 {
3635 serverSetName(parmp->servername);
3636 vim_free(parmp->servername);
3637 }
3638# endif
3639 }
3640}
3641
3642/*
3643 * Prepare for running as a Vim server.
3644 */
3645 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003646prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003647{
3648# if defined(FEAT_X11)
3649 /*
3650 * Register for remote command execution with :serversend and --remote
3651 * unless there was a -X or a --servername '' on the command line.
3652 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003653 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003654 */
3655 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3656# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003657 (gui.in_use
3658# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003659 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003660# endif
3661 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003662# endif
3663 parmp->serverName_arg != NULL))
3664 {
3665 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3666 vim_free(parmp->servername);
3667 TIME_MSG("register server name");
3668 }
3669 else
3670 serverDelayedStartName = parmp->servername;
3671# endif
3672
3673 /*
3674 * Execute command ourselves if we're here because the send failed (or
3675 * else we would have exited above).
3676 */
3677 if (parmp->serverStr != NULL)
3678 {
3679 char_u *p;
3680
3681 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3682 parmp->serverStr, &p));
3683 vim_free(p);
3684 }
3685}
3686
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003687 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003688cmdsrv_main(
3689 int *argc,
3690 char **argv,
3691 char_u *serverName_arg,
3692 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003693{
3694 char_u *res;
3695 int i;
3696 char_u *sname;
3697 int ret;
3698 int didone = FALSE;
3699 int exiterr = 0;
3700 char **newArgV = argv + 1;
3701 int newArgC = 1,
3702 Argc = *argc;
3703 int argtype;
3704#define ARGTYPE_OTHER 0
3705#define ARGTYPE_EDIT 1
3706#define ARGTYPE_EDIT_WAIT 2
3707#define ARGTYPE_SEND 3
3708 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003709 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003710# ifndef FEAT_X11
3711 HWND srv;
3712# else
3713 Window srv;
3714
3715 setup_term_clip();
3716# endif
3717
3718 sname = serverMakeName(serverName_arg, argv[0]);
3719 if (sname == NULL)
3720 return;
3721
3722 /*
3723 * Execute the command server related arguments and remove them
3724 * from the argc/argv array; We may have to return into main()
3725 */
3726 for (i = 1; i < Argc; i++)
3727 {
3728 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003729 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003730 {
3731 for (; i < *argc; i++)
3732 {
3733 *newArgV++ = argv[i];
3734 newArgC++;
3735 }
3736 break;
3737 }
3738
Bram Moolenaareb94e552006-03-11 21:35:11 +00003739 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003740 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003741 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3742 {
3743 char *p = argv[i] + 8;
3744
3745 argtype = ARGTYPE_EDIT;
3746 while (*p != NUL)
3747 {
3748 if (STRNICMP(p, "-wait", 5) == 0)
3749 {
3750 argtype = ARGTYPE_EDIT_WAIT;
3751 p += 5;
3752 }
3753 else if (STRNICMP(p, "-silent", 7) == 0)
3754 {
3755 silent = TRUE;
3756 p += 7;
3757 }
3758 else if (STRNICMP(p, "-tab", 4) == 0)
3759 {
3760 tabs = TRUE;
3761 p += 4;
3762 }
3763 else
3764 {
3765 argtype = ARGTYPE_OTHER;
3766 break;
3767 }
3768 }
3769 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003770 else
3771 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003772
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003773 if (argtype != ARGTYPE_OTHER)
3774 {
3775 if (i == *argc - 1)
3776 mainerr_arg_missing((char_u *)argv[i]);
3777 if (argtype == ARGTYPE_SEND)
3778 {
3779 *serverStr = (char_u *)argv[i + 1];
3780 i++;
3781 }
3782 else
3783 {
3784 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003785 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003786 if (*serverStr == NULL)
3787 {
3788 /* Probably out of memory, exit. */
3789 didone = TRUE;
3790 exiterr = 1;
3791 break;
3792 }
3793 Argc = i;
3794 }
3795# ifdef FEAT_X11
3796 if (xterm_dpy == NULL)
3797 {
3798 mch_errmsg(_("No display"));
3799 ret = -1;
3800 }
3801 else
3802 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003803 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003804# else
3805 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003806 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003807# endif
3808 if (ret < 0)
3809 {
3810 if (argtype == ARGTYPE_SEND)
3811 {
3812 /* Failed to send, abort. */
3813 mch_errmsg(_(": Send failed.\n"));
3814 didone = TRUE;
3815 exiterr = 1;
3816 }
3817 else if (!silent)
3818 /* Let vim start normally. */
3819 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3820 break;
3821 }
3822
3823# ifdef FEAT_GUI_W32
3824 /* Guess that when the server name starts with "g" it's a GUI
3825 * server, which we can bring to the foreground here.
3826 * Foreground() in the server doesn't work very well. */
3827 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3828 SetForegroundWindow(srv);
3829# endif
3830
3831 /*
3832 * For --remote-wait: Wait until the server did edit each
3833 * file. Also detect that the server no longer runs.
3834 */
3835 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3836 {
3837 int numFiles = *argc - i - 1;
3838 int j;
3839 char_u *done = alloc(numFiles);
3840 char_u *p;
3841# ifdef FEAT_GUI_W32
3842 NOTIFYICONDATA ni;
3843 int count = 0;
3844 extern HWND message_window;
3845# endif
3846
3847 if (numFiles > 0 && argv[i + 1][0] == '+')
3848 /* Skip "+cmd" argument, don't wait for it to be edited. */
3849 --numFiles;
3850
3851# ifdef FEAT_GUI_W32
3852 ni.cbSize = sizeof(ni);
3853 ni.hWnd = message_window;
3854 ni.uID = 0;
3855 ni.uFlags = NIF_ICON|NIF_TIP;
3856 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3857 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3858 Shell_NotifyIcon(NIM_ADD, &ni);
3859# endif
3860
3861 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003862 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003863 while (memchr(done, 0, numFiles) != NULL)
3864 {
3865# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003866 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003867 if (p == NULL)
3868 break;
3869# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003870 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003871 break;
3872# endif
3873 j = atoi((char *)p);
3874 if (j >= 0 && j < numFiles)
3875 {
3876# ifdef FEAT_GUI_W32
3877 ++count;
3878 sprintf(ni.szTip, _("%d of %d edited"),
3879 count, numFiles);
3880 Shell_NotifyIcon(NIM_MODIFY, &ni);
3881# endif
3882 done[j] = 1;
3883 }
3884 }
3885# ifdef FEAT_GUI_W32
3886 Shell_NotifyIcon(NIM_DELETE, &ni);
3887# endif
3888 }
3889 }
3890 else if (STRICMP(argv[i], "--remote-expr") == 0)
3891 {
3892 if (i == *argc - 1)
3893 mainerr_arg_missing((char_u *)argv[i]);
3894# ifdef WIN32
3895 /* Win32 always works? */
3896 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003897 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003898# else
3899 if (xterm_dpy == NULL)
3900 mch_errmsg(_("No display: Send expression failed.\n"));
3901 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003902 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003903# endif
3904 {
3905 if (res != NULL && *res != NUL)
3906 {
3907 /* Output error from remote */
3908 mch_errmsg((char *)res);
3909 vim_free(res);
3910 res = NULL;
3911 }
3912 mch_errmsg(_(": Send expression failed.\n"));
3913 }
3914 }
3915 else if (STRICMP(argv[i], "--serverlist") == 0)
3916 {
3917# ifdef WIN32
3918 /* Win32 always works? */
3919 res = serverGetVimNames();
3920# else
3921 if (xterm_dpy != NULL)
3922 res = serverGetVimNames(xterm_dpy);
3923# endif
3924 if (called_emsg)
3925 mch_errmsg("\n");
3926 }
3927 else if (STRICMP(argv[i], "--servername") == 0)
3928 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003929 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003930 i++;
3931 continue;
3932 }
3933 else
3934 {
3935 *newArgV++ = argv[i];
3936 newArgC++;
3937 continue;
3938 }
3939 didone = TRUE;
3940 if (res != NULL && *res != NUL)
3941 {
3942 mch_msg((char *)res);
3943 if (res[STRLEN(res) - 1] != '\n')
3944 mch_msg("\n");
3945 }
3946 vim_free(res);
3947 }
3948
3949 if (didone)
3950 {
3951 display_errors(); /* display any collected messages */
3952 exit(exiterr); /* Mission accomplished - get out */
3953 }
3954
3955 /* Return back into main() */
3956 *argc = newArgC;
3957 vim_free(sname);
3958}
3959
3960/*
3961 * Build a ":drop" command to send to a Vim server.
3962 */
3963 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003964build_drop_cmd(
3965 int filec,
3966 char **filev,
3967 int tabs, /* Use ":tab drop" instead of ":drop". */
3968 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003969{
3970 garray_T ga;
3971 int i;
3972 char_u *inicmd = NULL;
3973 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003974 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003975 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003976
3977 if (filec > 0 && filev[0][0] == '+')
3978 {
3979 inicmd = (char_u *)filev[0] + 1;
3980 filev++;
3981 filec--;
3982 }
3983 /* Check if we have at least one argument. */
3984 if (filec <= 0)
3985 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003986
3987 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003988 cwd = alloc(MAXPATHL);
3989 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003990 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003991 if (mch_dirname(cwd, MAXPATHL) != OK)
3992 {
3993 vim_free(cwd);
3994 return NULL;
3995 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003996 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003997#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003998 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003999#else
4000 PATH_ESC_CHARS,
4001#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004002 '\\', TRUE);
4003 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004004 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004005 return NULL;
4006 ga_init2(&ga, 1, 100);
4007 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004008 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004009
4010 /* Call inputsave() so that a prompt for an encryption key works. */
4011 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4012 if (tabs)
4013 ga_concat(&ga, (char_u *)"tab ");
4014 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004015 for (i = 0; i < filec; i++)
4016 {
4017 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004018 * do it again in the Vim server. On MS-Windows only escape
4019 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004020 p = vim_strsave_escaped((char_u *)filev[i],
4021#ifdef UNIX
4022 PATH_ESC_CHARS
4023#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004024 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004025#endif
4026 );
4027 if (p == NULL)
4028 {
4029 vim_free(ga.ga_data);
4030 return NULL;
4031 }
4032 ga_concat(&ga, (char_u *)" ");
4033 ga_concat(&ga, p);
4034 vim_free(p);
4035 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004036 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4037
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004038 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4039 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004040 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4041
4042 /* Switch back to the correct current directory (prior to temporary path
4043 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004044 * correct after the :drop command. With line breaks and spaces:
4045 * if !exists('+acd') || !&acd
4046 * if haslocaldir()
4047 * cd -
4048 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004049 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004050 * cd -
4051 * endif
4052 * endif
4053 */
4054 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004055 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004056 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004057 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004058 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004059
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004060 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004061 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4062 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004063 if (inicmd != NULL)
4064 {
4065 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4066 * the following commands to be inserted as text. Use a "|",
4067 * hopefully "inicmd" does allow this... */
4068 ga_concat(&ga, inicmd);
4069 ga_concat(&ga, (char_u *)"|");
4070 }
4071 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4072 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004073 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004074 ga_append(&ga, NUL);
4075 return ga.ga_data;
4076}
4077
4078/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004079 * Make our basic server name: use the specified "arg" if given, otherwise use
4080 * the tail of the command "cmd" we were started with.
4081 * Return the name in allocated memory. This doesn't include a serial number.
4082 */
4083 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004084serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004085{
4086 char_u *p;
4087
4088 if (arg != NULL && *arg != NUL)
4089 p = vim_strsave_up(arg);
4090 else
4091 {
4092 p = vim_strsave_up(gettail((char_u *)cmd));
4093 /* Remove .exe or .bat from the name. */
4094 if (p != NULL && vim_strchr(p, '.') != NULL)
4095 *vim_strchr(p, '.') = NUL;
4096 }
4097 return p;
4098}
4099#endif /* FEAT_CLIENTSERVER */
4100
4101#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4102/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004103 * Replace termcodes such as <CR> and insert as key presses if there is room.
4104 */
4105 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004106server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004107{
4108 char_u *ptr = NULL;
4109 char_u *cpo_save = p_cpo;
4110
4111 /* Set 'cpoptions' the way we want it.
4112 * B set - backslashes are *not* treated specially
4113 * k set - keycodes are *not* reverse-engineered
4114 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004115 * The last but one parameter of replace_termcodes() is TRUE so that the
4116 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004117 */
4118 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004119 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004120 p_cpo = cpo_save;
4121
4122 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4123 {
4124 /*
4125 * Add the string to the input stream.
4126 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4127 *
4128 * First clear typed characters from the typeahead buffer, there could
4129 * be half a mapping there. Then append to the existing string, so
4130 * that multiple commands from a client are concatenated.
4131 */
4132 if (typebuf.tb_maplen < typebuf.tb_len)
4133 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4134 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4135
4136 /* Let input_available() know we inserted text in the typeahead
4137 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004138 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004139 }
4140 vim_free((char_u *)ptr);
4141}
4142
4143/*
4144 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004145 */
4146 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004147eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004148{
4149 char_u *res;
4150 int save_dbl = debug_break_level;
4151 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004152 void *fc;
4153
4154 /* Evaluate the expression at the toplevel, don't use variables local to
4155 * the calling function. */
4156 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004157
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004158 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4159 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004160 debug_break_level = -1;
4161 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004162 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4163 * to be typed. Do generate errors so that try/catch works. */
4164 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004165
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004166 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004167
4168 debug_break_level = save_dbl;
4169 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004170 --emsg_silent;
4171 if (emsg_silent < 0)
4172 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004173 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004174
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004175 /* A client can tell us to redraw, but not to display the cursor, so do
4176 * that here. */
4177 setcursor();
4178 out_flush();
4179#ifdef FEAT_GUI
4180 if (gui.in_use)
4181 gui_update_cursor(FALSE, FALSE);
4182#endif
4183
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004184 return res;
4185}
4186
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004187/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004188 * Evaluate a command or expression sent to ourselves.
4189 */
4190 int
4191sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4192{
4193 if (asExpr)
4194 {
4195 char_u *ret;
4196
4197 ret = eval_client_expr_to_string(cmd);
4198 if (result != NULL)
4199 {
4200 if (ret == NULL)
4201 {
4202 char *err = _(e_invexprmsg);
4203 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4204 char_u *msg;
4205
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004206 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004207 if (msg != NULL)
4208 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4209 *result = msg;
4210 }
4211 else
4212 *result = ret;
4213 }
4214 else
4215 vim_free(ret);
4216 return ret == NULL ? -1 : 0;
4217 }
4218 server_to_input_buf(cmd);
4219 return 0;
4220}
4221
4222/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004223 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4224 * return an allocated string. Otherwise return "data".
4225 * "*tofree" is set to the result when it needs to be freed later.
4226 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004227 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004228serverConvert(
4229 char_u *client_enc UNUSED,
4230 char_u *data,
4231 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004232{
4233 char_u *res = data;
4234
4235 *tofree = NULL;
4236# ifdef FEAT_MBYTE
4237 if (client_enc != NULL && p_enc != NULL)
4238 {
4239 vimconv_T vimconv;
4240
4241 vimconv.vc_type = CONV_NONE;
4242 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4243 && vimconv.vc_type != CONV_NONE)
4244 {
4245 res = string_convert(&vimconv, data, NULL);
4246 if (res == NULL)
4247 res = data;
4248 else
4249 *tofree = res;
4250 }
4251 convert_setup(&vimconv, NULL, NULL);
4252 }
4253# endif
4254 return res;
4255}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004256#endif