blob: 1da2b84c5b28105e8906f32aba578177a93f2958 [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 Moolenaarc013cb62005-07-24 21:18:31 +0000562 if (params.use_ef != NULL)
563 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000564 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200565 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
566 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000567 {
568 out_char('\n');
569 mch_exit(3);
570 }
571 TIME_MSG("reading errorfile");
572 }
573#endif
574
575 /*
576 * Start putting things on the screen.
577 * Scroll screen down before drawing over it
578 * Clear screen now, so file message will not be cleared.
579 */
580 starting = NO_BUFFERS;
581 no_wait_return = FALSE;
582 if (!exmode_active)
583 msg_scroll = FALSE;
584
585#ifdef FEAT_GUI
586 /*
587 * This seems to be required to make callbacks to be called now, instead
588 * of after things have been put on the screen, which then may be deleted
589 * when getting a resize callback.
590 * For the Mac this handles putting files dropped on the Vim icon to
591 * global_alist.
592 */
593 if (gui.in_use)
594 {
595# ifdef FEAT_SUN_WORKSHOP
596 if (!usingSunWorkShop)
597# endif
598 gui_wait_for_chars(50L);
599 TIME_MSG("GUI delay");
600 }
601#endif
602
603#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
604 qnx_clip_init();
605#endif
606
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200607#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
608 clip_init(TRUE);
609#endif
610
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000611#ifdef FEAT_XCLIPBOARD
612 /* Start using the X clipboard, unless the GUI was started. */
613# ifdef FEAT_GUI
614 if (!gui.in_use)
615# endif
616 {
617 setup_term_clip();
618 TIME_MSG("setup clipboard");
619 }
620#endif
621
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000623 /* Prepare for being a Vim server. */
624 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000625#endif
626
627 /*
628 * If "-" argument given: Read file from stdin.
629 * Do this before starting Raw mode, because it may change things that the
630 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
631 * are the same terminal: "cat | vim -".
632 * Using autocommands here may cause trouble...
633 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000634 if (params.edit_type == EDIT_STDIN && !recoverymode)
635 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000636
637#if defined(UNIX) || defined(VMS)
638 /* When switching screens and something caused a message from a vimrc
639 * script, need to output an extra newline on exit. */
640 if ((did_emsg || msg_didout) && *T_TI != NUL)
641 newline_on_exit = TRUE;
642#endif
643
644 /*
645 * When done something that is not allowed or error message call
646 * wait_return. This must be done before starttermcap(), because it may
647 * switch to another screen. It must be done after settmode(TMODE_RAW),
648 * because we want to react on a single key stroke.
649 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000650 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000651 */
652 settmode(TMODE_RAW);
653 TIME_MSG("setting raw mode");
654
655 if (need_wait_return || msg_didany)
656 {
657 wait_return(TRUE);
658 TIME_MSG("waiting for return");
659 }
660
661 starttermcap(); /* start termcap if not done by wait_return() */
662 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200663#if defined(FEAT_TERMRESPONSE)
664# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200665 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200666# endif
667 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100668#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000669
670#ifdef FEAT_MOUSE
671 setmouse(); /* may start using the mouse */
672#endif
673 if (scroll_region)
674 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000675 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676
677 /*
678 * Don't clear the screen when starting in Ex mode, unless using the GUI.
679 */
680 if (exmode_active
681#ifdef FEAT_GUI
682 && !gui.in_use
683#endif
684 )
685 must_redraw = CLEAR;
686 else
687 {
688 screenclear(); /* clear screen */
689 TIME_MSG("clearing screen");
690 }
691
692#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000693 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000694 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100695 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200696 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000697 TIME_MSG("getting crypt key");
698 }
699#endif
700
701 no_wait_return = TRUE;
702
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000704 * Create the requested number of windows and edit buffers in them.
705 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000706 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000707 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708 TIME_MSG("opening buffers");
709
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000710#ifdef FEAT_EVAL
711 /* clear v:swapcommand */
712 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
713#endif
714
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 /* Ex starts at last line of the file */
716 if (exmode_active)
717 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
718
719#ifdef FEAT_AUTOCMD
720 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
721 TIME_MSG("BufEnter autocommands");
722#endif
723 setpcmark();
724
725#ifdef FEAT_QUICKFIX
726 /*
727 * When started with "-q errorfile" jump to first error now.
728 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000729 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000730 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000731 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732 TIME_MSG("jump to first error");
733 }
734#endif
735
736#ifdef FEAT_WINDOWS
737 /*
738 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000739 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200741 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000742#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200743 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000744
745#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000746 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000747 {
748 win_T *wp;
749
750 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200751 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000752 diff_win_options(wp, TRUE);
753 }
754#endif
755
756 /*
757 * Shorten any of the filenames, but only when absolute.
758 */
759 shorten_fnames(FALSE);
760
761 /*
762 * Need to jump to the tag before executing the '-c command'.
763 * Makes "vim -c '/return' -t main" work.
764 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000765 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000766 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000767#if defined(HAS_SWAP_EXISTS_ACTION)
768 swap_exists_did_quit = FALSE;
769#endif
770
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000771 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000772 do_cmdline_cmd(IObuff);
773 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000774
775#if defined(HAS_SWAP_EXISTS_ACTION)
776 /* If the user doesn't want to edit the file then we quit here. */
777 if (swap_exists_did_quit)
778 getout(1);
779#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000780 }
781
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000782 /* Execute any "+", "-c" and "-S" arguments. */
783 if (params.n_commands > 0)
784 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000785
786 RedrawingDisabled = 0;
787 redraw_all_later(NOT_VALID);
788 no_wait_return = FALSE;
789 starting = 0;
790
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200791 /* 'autochdir' has been postponed */
792 DO_AUTOCHDIR
793
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000794#ifdef FEAT_TERMRESPONSE
795 /* Requesting the termresponse is postponed until here, so that a "-c q"
796 * argument doesn't make it appear in the shell Vim was started from. */
797 may_req_termresponse();
798#endif
799
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800 /* start in insert mode */
801 if (p_im)
802 need_start_insertmode = TRUE;
803
Bram Moolenaar14735512016-03-26 21:00:08 +0100804#ifdef FEAT_EVAL
805 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
806#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807#ifdef FEAT_AUTOCMD
808 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
809 TIME_MSG("VimEnter autocommands");
810#endif
811
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200812#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
813 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
814 * done after the clipboard is available and all initial commands that may
815 * modify the 'clipboard' setting have run; i.e. just before entering the
816 * main loop. */
817 {
818 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100819
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200820 adjust_clip_reg(&default_regname);
821 set_reg_var(default_regname);
822 }
823#endif
824
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
826 /* When a startup script or session file setup for diff'ing and
827 * scrollbind, sync the scrollbind now. */
828 if (curwin->w_p_diff && curwin->w_p_scb)
829 {
830 update_topline();
831 check_scrollbind((linenr_T)0, 0L);
832 TIME_MSG("diff scrollbinding");
833 }
834#endif
835
836#if defined(WIN3264) && !defined(FEAT_GUI_W32)
837 mch_set_winsize_now(); /* Allow winsize changes from now on */
838#endif
839
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000840#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
841 /* When tab pages were created, may need to update the tab pages line and
842 * scrollbars. This is skipped while creating them. */
843 if (first_tabpage->tp_next != NULL)
844 {
845 out_flush();
846 gui_init_which_components(NULL);
847 gui_update_scrollbars(TRUE);
848 }
849 need_mouse_correct = TRUE;
850#endif
851
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000852 /* If ":startinsert" command used, stuff a dummy command to be able to
853 * call normal_cmd(), which will then start Insert mode. */
854 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000855 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856
857#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200858 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200859 {
860# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200861# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200862 && !defined(FEAT_GUI_W32)
863 if (gui.in_use)
864 {
865 mch_errmsg(_("netbeans is not supported with this GUI\n"));
866 mch_exit(2);
867 }
868# endif
869# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000870 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200871 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200872 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000873#endif
874
875 TIME_MSG("before starting main loop");
876
877 /*
878 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100879 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000880 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200882#endif /* NO_VIM_MAIN */
883
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884 return 0;
885}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886
887/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200888 * Initialisation shared by main() and some tests.
889 */
890 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200891common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200892{
893
894#ifdef FEAT_MBYTE
895 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
896#endif
897#ifdef FEAT_EVAL
898 eval_init(); /* init global variables */
899#endif
900
901#ifdef __QNXNTO__
902 qnx_init(); /* PhAttach() for clipboard, (and gui) */
903#endif
904
905#ifdef MAC_OS_CLASSIC
906 /* Prepare for possibly starting GUI sometime */
907 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200908 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200909 TIME_MSG("GUI prepared");
910#endif
911
912 /* Init the table of Normal mode commands. */
913 init_normal_cmds();
914
915#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
916 make_version(); /* Construct the long version string. */
917#endif
918
919 /*
920 * Allocate space for the generic buffers (needed for set_init_1() and
921 * EMSG2()).
922 */
923 if ((IObuff = alloc(IOSIZE)) == NULL
924 || (NameBuff = alloc(MAXPATHL)) == NULL)
925 mch_exit(0);
926 TIME_MSG("Allocated generic buffers");
927
928#ifdef NBDEBUG
929 /* Wait a moment for debugging NetBeans. Must be after allocating
930 * NameBuff. */
931 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
932 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
933 TIME_MSG("NetBeans debug wait");
934#endif
935
936#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
937 /*
938 * Setup to use the current locale (for ctype() and many other things).
939 * NOTE: Translated messages with encodings other than latin1 will not
940 * work until set_init_1() has been called!
941 */
942 init_locale();
943 TIME_MSG("locale set");
944#endif
945
946#ifdef FEAT_GUI
947 gui.dofork = TRUE; /* default is to use fork() */
948#endif
949
950 /*
951 * Do a first scan of the arguments in "argv[]":
952 * -display or --display
953 * --server...
954 * --socketid
955 * --windowid
956 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200957 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200958
959#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200960 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200961#endif
962#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
963 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200964 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200965 TIME_MSG("GUI prepared");
966#endif
967
968#ifdef FEAT_CLIPBOARD
969 clip_init(FALSE); /* Initialise clipboard stuff */
970 TIME_MSG("clipboard setup");
971#endif
972
973 /*
974 * Check if we have an interactive window.
975 * On the Amiga: If there is no window, we open one with a newcli command
976 * (needed for :! to * work). mch_check_win() will also handle the -d or
977 * -dev argument.
978 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100979 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200980 TIME_MSG("window checked");
981
982 /*
983 * Allocate the first window and buffer.
984 * Can't do anything without it, exit when it fails.
985 */
986 if (win_alloc_first() == FAIL)
987 mch_exit(0);
988
989 init_yank(); /* init yank buffers */
990
991 alist_init(&global_alist); /* Init the argument list to empty. */
992 global_alist.id = 0;
993
994 /*
995 * Set the default values for the options.
996 * NOTE: Non-latin1 translated messages are working only after this,
997 * because this is where "has_mbyte" will be set, which is used by
998 * msg_outtrans_len_attr().
999 * First find out the home directory, needed to expand "~" in options.
1000 */
1001 init_homedir(); /* find real value of $HOME */
1002 set_init_1();
1003 TIME_MSG("inits 1");
1004
1005#ifdef FEAT_EVAL
1006 set_lang_var(); /* set v:lang and v:ctype */
1007#endif
1008}
1009
1010/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001011 * Main loop: Execute Normal mode commands until exiting Vim.
1012 * Also used to handle commands in the command-line window, until the window
1013 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001014 * Also used to handle ":visual" command after ":global": execute Normal mode
1015 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001016 */
1017 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001018main_loop(
1019 int cmdwin, /* TRUE when working in the command-line window */
1020 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001021{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001022 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001023 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001024#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001025 /* these are static to avoid a compiler warning */
1026 static linenr_T conceal_old_cursor_line = 0;
1027 static linenr_T conceal_new_cursor_line = 0;
1028 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001029#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001030
1031#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1032 /* Setup to catch a terminating error from the X server. Just ignore
1033 * it, restore the state and continue. This might not always work
1034 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001035 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001037 {
1038 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001039 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001040 got_int = TRUE;
1041 need_wait_return = FALSE;
1042 global_busy = FALSE;
1043 exmode_active = 0;
1044 skip_redraw = FALSE;
1045 RedrawingDisabled = 0;
1046 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001047 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001048# ifdef FEAT_EVAL
1049 emsg_skip = 0;
1050# endif
1051 emsg_off = 0;
1052# ifdef FEAT_MOUSE
1053 setmouse();
1054# endif
1055 settmode(TMODE_RAW);
1056 starttermcap();
1057 scroll_start();
1058 redraw_later_clear();
1059 }
1060#endif
1061
1062 clear_oparg(&oa);
1063 while (!cmdwin
1064#ifdef FEAT_CMDWIN
1065 || cmdwin_result == 0
1066#endif
1067 )
1068 {
1069 if (stuff_empty())
1070 {
1071 did_check_timestamps = FALSE;
1072 if (need_check_timestamps)
1073 check_timestamps(FALSE);
1074 if (need_wait_return) /* if wait_return still needed ... */
1075 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001076 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001077 {
1078 need_start_insertmode = FALSE;
1079 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1080 /* skip the fileinfo message now, because it would be shown
1081 * after insert mode finishes! */
1082 need_fileinfo = FALSE;
1083 }
1084 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001085
1086 /* Reset "got_int" now that we got back to the main loop. Except when
1087 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1088 * the ":g" command.
1089 * For ":g/pat/vi" we reset "got_int" when used once. When used
1090 * a second time we go back to Ex mode and abort the ":g" command. */
1091 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001092 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001093 if (noexmode && global_busy && !exmode_active && previous_got_int)
1094 {
1095 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1096 * used and keep "got_int" set, so that it aborts ":g". */
1097 exmode_active = EXMODE_NORMAL;
1098 State = NORMAL;
1099 }
1100 else if (!global_busy || !exmode_active)
1101 {
1102 if (!quit_more)
1103 (void)vgetc(); /* flush all buffers */
1104 got_int = FALSE;
1105 }
1106 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001107 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001108 else
1109 previous_got_int = FALSE;
1110
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001111 if (!exmode_active)
1112 msg_scroll = FALSE;
1113 quit_more = FALSE;
1114
1115 /*
1116 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1117 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1118 * update cursor and redraw.
1119 */
1120 if (skip_redraw || exmode_active)
1121 skip_redraw = FALSE;
1122 else if (do_redraw || stuff_empty())
1123 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001124# ifdef FEAT_GUI
1125 /* If ui_breakcheck() was used a resize may have been postponed. */
1126 gui_may_resize_shell();
1127# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001128#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001129 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001130 if (!finish_op && (
1131# ifdef FEAT_AUTOCMD
1132 has_cursormoved()
1133# endif
1134# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1135 ||
1136# endif
1137# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001138 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001139# endif
1140 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001141# ifdef FEAT_AUTOCMD
1142 && !equalpos(last_cursormoved, curwin->w_cursor)
1143# endif
1144 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001145 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001146# ifdef FEAT_AUTOCMD
1147 if (has_cursormoved())
1148 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1149 FALSE, curbuf);
1150# endif
1151# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001152 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001153 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001154# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001155 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001156# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001157 conceal_new_cursor_line = curwin->w_cursor.lnum;
1158 conceal_update_lines = TRUE;
1159 }
1160# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001161# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001162 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001163# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001164 }
1165#endif
1166
Bram Moolenaar186628f2013-03-19 13:33:23 +01001167#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001168 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001169 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001170 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001171 {
1172 if (last_changedtick_buf == curbuf)
1173 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1174 FALSE, curbuf);
1175 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001176 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001177 }
1178#endif
1179
Bram Moolenaar33aec762006-01-22 23:30:12 +00001180#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1181 /* Scroll-binding for diff mode may have been postponed until
1182 * here. Avoids doing it for every change. */
1183 if (diff_need_scrollbind)
1184 {
1185 check_scrollbind((linenr_T)0, 0L);
1186 diff_need_scrollbind = FALSE;
1187 }
1188#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001189#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001190 /* Include a closed fold completely in the Visual area. */
1191 foldAdjustVisual();
1192#endif
1193#ifdef FEAT_FOLDING
1194 /*
1195 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1196 * contain the cursor.
1197 * When 'foldopen' is "all", open the fold(s) under the cursor.
1198 * This may mark the window for redrawing.
1199 */
1200 if (hasAnyFolding(curwin) && !char_avail())
1201 {
1202 foldCheckClose();
1203 if (fdo_flags & FDO_ALL)
1204 foldOpenCursor();
1205 }
1206#endif
1207
1208 /*
1209 * Before redrawing, make sure w_topline is correct, and w_leftcol
1210 * if lines don't wrap, and w_skipcol if lines wrap.
1211 */
1212 update_topline();
1213 validate_cursor();
1214
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001215 if (VIsual_active)
1216 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001217 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001218 update_screen(0);
1219 else if (redraw_cmdline || clear_cmdline)
1220 showmode();
1221#ifdef FEAT_WINDOWS
1222 redraw_statuslines();
1223#endif
1224#ifdef FEAT_TITLE
1225 if (need_maketitle)
1226 maketitle();
1227#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001228#ifdef FEAT_VIMINFO
1229 curbuf->b_last_used = vim_time();
1230#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001231 /* display message after redraw */
1232 if (keep_msg != NULL)
1233 {
1234 char_u *p;
1235
1236 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001237 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1238 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001239 p = keep_msg;
1240 msg_attr(p, keep_msg_attr);
1241 vim_free(p);
1242 }
1243 if (need_fileinfo) /* show file info after redraw */
1244 {
1245 fileinfo(FALSE, TRUE, FALSE);
1246 need_fileinfo = FALSE;
1247 }
1248
1249 emsg_on_display = FALSE; /* can delete error message now */
1250 did_emsg = FALSE;
1251 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001252 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 showruler(FALSE);
1254
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001255# if defined(FEAT_CONCEAL)
1256 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001257 && (conceal_old_cursor_line != conceal_new_cursor_line
1258 || conceal_cursor_line(curwin)
1259 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001260 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001261 if (conceal_old_cursor_line != conceal_new_cursor_line
1262 && conceal_old_cursor_line
1263 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001264 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001265 update_single_line(curwin, conceal_new_cursor_line);
1266 curwin->w_valid &= ~VALID_CROW;
1267 }
1268# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001269 setcursor();
1270 cursor_on();
1271
1272 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001273
1274#ifdef STARTUPTIME
1275 /* Now that we have drawn the first screen all the startup stuff
1276 * has been done, close any file for startup messages. */
1277 if (time_fd != NULL)
1278 {
1279 TIME_MSG("first screen update");
1280 TIME_MSG("--- VIM STARTED ---");
1281 fclose(time_fd);
1282 time_fd = NULL;
1283 }
1284#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001285 }
1286#ifdef FEAT_GUI
1287 if (need_mouse_correct)
1288 gui_mouse_correct();
1289#endif
1290
1291 /*
1292 * Update w_curswant if w_set_curswant has been set.
1293 * Postponed until here to avoid computing w_virtcol too often.
1294 */
1295 update_curswant();
1296
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001297#ifdef FEAT_EVAL
1298 /*
1299 * May perform garbage collection when waiting for a character, but
1300 * only at the very toplevel. Otherwise we may be using a List or
1301 * Dict internally somewhere.
1302 * "may_garbage_collect" is reset in vgetc() which is invoked through
1303 * do_exmode() and normal_cmd().
1304 */
1305 may_garbage_collect = (!cmdwin && !noexmode);
1306#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001307 /*
1308 * If we're invoked as ex, do a round of ex commands.
1309 * Otherwise, get and execute a normal mode command.
1310 */
1311 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001312 {
1313 if (noexmode) /* End of ":global/path/visual" commands */
1314 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001315 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001316 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001317 else
1318 normal_cmd(&oa, TRUE);
1319 }
1320}
1321
1322
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001323#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001324/*
1325 * Exit, but leave behind swap files for modified buffers.
1326 */
1327 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001328getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001329{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001330# if defined(SIGHUP) && defined(SIG_IGN)
1331 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1332 * makes Vim exit and then handling SIGHUP causes various reentrance
1333 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001334 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001335# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001336
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001337 ml_close_notmod(); /* close all not-modified buffers */
1338 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1339 ml_close_all(FALSE); /* close all memfiles, without deleting */
1340 getout(exitval); /* exit Vim properly */
1341}
1342#endif
1343
1344
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001345/*
1346 * Exit properly.
1347 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001348 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001349getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001350{
1351#ifdef FEAT_AUTOCMD
1352 buf_T *buf;
1353 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001354 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001355#endif
1356
1357 exiting = TRUE;
1358
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001359 /* When running in Ex mode an error causes us to exit with a non-zero exit
1360 * code. POSIX requires this, although it's not 100% clear from the
1361 * standard. */
1362 if (exmode_active)
1363 exitval += ex_exitval;
1364
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365 /* Position the cursor on the last screen line, below all the text */
1366#ifdef FEAT_GUI
1367 if (!gui.in_use)
1368#endif
1369 windgoto((int)Rows - 1, 0);
1370
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001371#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1372 /* Optionally print hashtable efficiency. */
1373 hash_debug_results();
1374#endif
1375
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001376#ifdef FEAT_GUI
1377 msg_didany = FALSE;
1378#endif
1379
1380#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001381 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001382 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001383 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1384# if defined FEAT_WINDOWS
1385 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001386 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001387 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001388 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001389 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001390 if (wp->w_buffer == NULL)
1391 /* Autocmd must have close the buffer already, skip. */
1392 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001393 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001394 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001395 {
1396 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1397 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001398 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001399 /* start all over, autocommands may mess up the lists */
1400 next_tp = first_tabpage;
1401 break;
1402 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001403 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001404 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001405# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001406 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1407 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001408# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001409
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001410 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001411 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001412 if (buf->b_ml.ml_mfp != NULL)
1413 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001414 bufref_T bufref;
1415
1416 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001417 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001418 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001419 if (!bufref_valid(&bufref))
1420 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001421 break;
1422 }
1423 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1424 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001425#endif
1426
1427#ifdef FEAT_VIMINFO
1428 if (*p_viminfo != NUL)
1429 /* Write out the registers, history, marks etc, to the viminfo file */
1430 write_viminfo(NULL, FALSE);
1431#endif
1432
1433#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001434 if (get_vim_var_nr(VV_DYING) <= 1)
1435 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001436#endif
1437
Bram Moolenaar05159a02005-02-26 23:04:13 +00001438#ifdef FEAT_PROFILE
1439 profile_dump();
1440#endif
1441
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001442 if (did_emsg
1443#ifdef FEAT_GUI
1444 || (gui.in_use && msg_didany && p_verbose > 0)
1445#endif
1446 )
1447 {
1448 /* give the user a chance to read the (error) message */
1449 no_wait_return = FALSE;
1450 wait_return(FALSE);
1451 }
1452
1453#ifdef FEAT_AUTOCMD
1454 /* Position the cursor again, the autocommands may have moved it */
1455# ifdef FEAT_GUI
1456 if (!gui.in_use)
1457# endif
1458 windgoto((int)Rows - 1, 0);
1459#endif
1460
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001461#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001462 job_stop_on_exit();
1463#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001464#ifdef FEAT_LUA
1465 lua_end();
1466#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001467#ifdef FEAT_MZSCHEME
1468 mzscheme_end();
1469#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001470#ifdef FEAT_TCL
1471 tcl_end();
1472#endif
1473#ifdef FEAT_RUBY
1474 ruby_end();
1475#endif
1476#ifdef FEAT_PYTHON
1477 python_end();
1478#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001479#ifdef FEAT_PYTHON3
1480 python3_end();
1481#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001482#ifdef FEAT_PERL
1483 perl_end();
1484#endif
1485#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1486 iconv_end();
1487#endif
1488#ifdef FEAT_NETBEANS_INTG
1489 netbeans_end();
1490#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001491#ifdef FEAT_CSCOPE
1492 cs_end();
1493#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001494#ifdef FEAT_EVAL
1495 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001496 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001497#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001498#if defined(WIN32) && defined(FEAT_MBYTE)
1499 free_cmd_argsW();
1500#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001501
1502 mch_exit(exitval);
1503}
1504
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001505#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1506/*
1507 * Setup to use the current locale (for ctype() and many other things).
1508 */
1509 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001510init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001511{
1512 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001513
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001514# ifdef FEAT_GUI_GTK
1515 /* Tell Gtk not to change our locale settings. */
1516 gtk_disable_setlocale();
1517# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001518# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1519 /* Make sure strtod() uses a decimal point, not a comma. */
1520 setlocale(LC_NUMERIC, "C");
1521# endif
1522
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001523# ifdef WIN32
1524 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1525 * text while it's expecting text in the current locale. This call avoids
1526 * that. */
1527 setlocale(LC_CTYPE, "C");
1528# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001529
1530# ifdef FEAT_GETTEXT
1531 {
1532 int mustfree = FALSE;
1533 char_u *p;
1534
1535# ifdef DYNAMIC_GETTEXT
1536 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001537 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001538# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001539 /* expand_env() doesn't work yet, because g_chartab[] is not
1540 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001541 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1542 if (p != NULL && *p != NUL)
1543 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001544 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001545 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1546 }
1547 if (mustfree)
1548 vim_free(p);
1549 textdomain(VIMPACKAGE);
1550 }
1551# endif
1552}
1553#endif
1554
1555/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001556 * Get the name of the display, before gui_prepare() removes it from
1557 * argv[]. Used for the xterm-clipboard display.
1558 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001559 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001560 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001561 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001562early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001563{
Bram Moolenaar03005972008-11-20 13:12:36 +00001564#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1565 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001566 int argc = parmp->argc;
1567 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001568 int i;
1569
1570 for (i = 1; i < argc; i++)
1571 {
1572 if (STRCMP(argv[i], "--") == 0)
1573 break;
1574# ifdef FEAT_XCLIPBOARD
1575 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001576# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001577 || STRICMP(argv[i], "--display") == 0
1578# endif
1579 )
1580 {
1581 if (i == argc - 1)
1582 mainerr_arg_missing((char_u *)argv[i]);
1583 xterm_display = argv[++i];
1584 }
1585# endif
1586# ifdef FEAT_CLIENTSERVER
1587 else if (STRICMP(argv[i], "--servername") == 0)
1588 {
1589 if (i == argc - 1)
1590 mainerr_arg_missing((char_u *)argv[i]);
1591 parmp->serverName_arg = (char_u *)argv[++i];
1592 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001593 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001594 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001595 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001596 {
1597 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001598# ifdef FEAT_GUI
1599 if (strstr(argv[i], "-wait") != 0)
1600 /* don't fork() when starting the GUI to edit files ourself */
1601 gui.dofork = FALSE;
1602# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001603 }
1604# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001605
1606# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1607# ifdef FEAT_GUI_W32
1608 else if (STRICMP(argv[i], "--windowid") == 0)
1609# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001610 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001611# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001612 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001613 long_u id;
1614 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001615
1616 if (i == argc - 1)
1617 mainerr_arg_missing((char_u *)argv[i]);
1618 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001619 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001620 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001621 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001622 if (count != 1)
1623 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1624 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001625# ifdef FEAT_GUI_W32
1626 win_socket_id = id;
1627# else
1628 gtk_socket_id = id;
1629# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001630 i++;
1631 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001632# endif
1633# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001634 else if (STRICMP(argv[i], "--echo-wid") == 0)
1635 echo_wid_arg = TRUE;
1636# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001637# ifndef FEAT_NETBEANS_INTG
1638 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001639 {
1640 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1641 mch_exit(2);
1642 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001643# endif
1644
Bram Moolenaar58d98232005-07-23 22:25:46 +00001645 }
1646#endif
1647}
1648
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001649#ifndef NO_VIM_MAIN
1650/*
1651 * Get a (optional) count for a Vim argument.
1652 */
1653 static int
1654get_number_arg(
1655 char_u *p, /* pointer to argument */
1656 int *idx, /* index in argument, is incremented */
1657 int def) /* default value */
1658{
1659 if (vim_isdigit(p[*idx]))
1660 {
1661 def = atoi((char *)&(p[*idx]));
1662 while (vim_isdigit(p[*idx]))
1663 *idx = *idx + 1;
1664 }
1665 return def;
1666}
1667
1668/*
1669 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1670 * If the executable name starts with "r" we disable shell commands.
1671 * If the next character is "e" we run in Easy mode.
1672 * If the next character is "g" we run the GUI version.
1673 * If the next characters are "view" we start in readonly mode.
1674 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1675 * If the next characters are "ex" we start in Ex mode. If it's followed
1676 * by "im" use improved Ex mode.
1677 */
1678 static void
1679parse_command_name(mparm_T *parmp)
1680{
1681 char_u *initstr;
1682
1683 initstr = gettail((char_u *)parmp->argv[0]);
1684
1685#ifdef MACOS_X_UNIX
1686 /* An issue has been seen when launching Vim in such a way that
1687 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1688 * executable or a symbolic link of it. Until this issue is resolved
1689 * we prohibit the GUI from being used.
1690 */
1691 if (STRCMP(initstr, parmp->argv[0]) == 0)
1692 disallow_gui = TRUE;
1693
1694 /* TODO: On MacOS X default to gui if argv[0] ends in:
1695 * /Vim.app/Contents/MacOS/Vim */
1696#endif
1697
1698#ifdef FEAT_EVAL
1699 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001700 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001701#endif
1702
1703 if (TOLOWER_ASC(initstr[0]) == 'r')
1704 {
1705 restricted = TRUE;
1706 ++initstr;
1707 }
1708
1709 /* Use evim mode for "evim" and "egvim", not for "editor". */
1710 if (TOLOWER_ASC(initstr[0]) == 'e'
1711 && (TOLOWER_ASC(initstr[1]) == 'v'
1712 || TOLOWER_ASC(initstr[1]) == 'g'))
1713 {
1714#ifdef FEAT_GUI
1715 gui.starting = TRUE;
1716#endif
1717 parmp->evim_mode = TRUE;
1718 ++initstr;
1719 }
1720
1721 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1722 if (TOLOWER_ASC(initstr[0]) == 'g')
1723 {
1724 main_start_gui();
1725#ifdef FEAT_GUI
1726 ++initstr;
1727#endif
1728 }
1729
1730 if (STRNICMP(initstr, "view", 4) == 0)
1731 {
1732 readonlymode = TRUE;
1733 curbuf->b_p_ro = TRUE;
1734 p_uc = 10000; /* don't update very often */
1735 initstr += 4;
1736 }
1737 else if (STRNICMP(initstr, "vim", 3) == 0)
1738 initstr += 3;
1739
1740 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1741 if (STRICMP(initstr, "diff") == 0)
1742 {
1743#ifdef FEAT_DIFF
1744 parmp->diff_mode = TRUE;
1745#else
1746 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1747 mch_errmsg("\n");
1748 mch_exit(2);
1749#endif
1750 }
1751
1752 if (STRNICMP(initstr, "ex", 2) == 0)
1753 {
1754 if (STRNICMP(initstr + 2, "im", 2) == 0)
1755 exmode_active = EXMODE_VIM;
1756 else
1757 exmode_active = EXMODE_NORMAL;
1758 change_compatible(TRUE); /* set 'compatible' */
1759 }
1760}
1761
Bram Moolenaar58d98232005-07-23 22:25:46 +00001762/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001763 * Scan the command line arguments.
1764 */
1765 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001766command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001767{
1768 int argc = parmp->argc;
1769 char **argv = parmp->argv;
1770 int argv_idx; /* index in argv[n][] */
1771 int had_minmin = FALSE; /* found "--" argument */
1772 int want_argument; /* option argument with argument */
1773 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001774 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001775 long n;
1776
1777 --argc;
1778 ++argv;
1779 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1780 while (argc > 0)
1781 {
1782 /*
1783 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1784 */
1785 if (argv[0][0] == '+' && !had_minmin)
1786 {
1787 if (parmp->n_commands >= MAX_ARG_CMDS)
1788 mainerr(ME_EXTRA_CMD, NULL);
1789 argv_idx = -1; /* skip to next argument */
1790 if (argv[0][1] == NUL)
1791 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1792 else
1793 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1794 }
1795
1796 /*
1797 * Optional argument.
1798 */
1799 else if (argv[0][0] == '-' && !had_minmin)
1800 {
1801 want_argument = FALSE;
1802 c = argv[0][argv_idx++];
1803#ifdef VMS
1804 /*
1805 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1806 * and "-/X" as "-X".
1807 */
1808 if (c == '/')
1809 {
1810 c = argv[0][argv_idx++];
1811 c = TOUPPER_ASC(c);
1812 }
1813 else
1814 c = TOLOWER_ASC(c);
1815#endif
1816 switch (c)
1817 {
1818 case NUL: /* "vim -" read from stdin */
1819 /* "ex -" silent mode */
1820 if (exmode_active)
1821 silent_mode = TRUE;
1822 else
1823 {
1824 if (parmp->edit_type != EDIT_NONE)
1825 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1826 parmp->edit_type = EDIT_STDIN;
1827 read_cmd_fd = 2; /* read from stderr instead of stdin */
1828 }
1829 argv_idx = -1; /* skip to next argument */
1830 break;
1831
1832 case '-': /* "--" don't take any more option arguments */
1833 /* "--help" give help message */
1834 /* "--version" give version message */
1835 /* "--literal" take files literally */
1836 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001837 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001838 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001839 /* "--noplugin[s]" skip plugins */
1840 /* "--cmd <cmd>" execute cmd before vimrc */
1841 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1842 usage();
1843 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1844 {
1845 Columns = 80; /* need to init Columns */
1846 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1847 list_version();
1848 msg_putchar('\n');
1849 msg_didout = FALSE;
1850 mch_exit(0);
1851 }
1852 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1853 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001854#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001855 parmp->literal = TRUE;
1856#endif
1857 }
1858 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1859 {
1860#ifdef FEAT_GUI
1861 gui.dofork = FALSE; /* don't fork() when starting GUI */
1862#endif
1863 }
1864 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1865 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001866 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1867 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001868 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1869 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001870 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1871 {
1872 want_argument = TRUE;
1873 argv_idx += 3;
1874 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001875 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1876 {
1877 want_argument = TRUE;
1878 argv_idx += 11;
1879 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001880#ifdef FEAT_CLIENTSERVER
1881 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1882 ; /* already processed -- no arg */
1883 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1884 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1885 {
1886 /* already processed -- snatch the following arg */
1887 if (argc > 1)
1888 {
1889 --argc;
1890 ++argv;
1891 }
1892 }
1893#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001894#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1895# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001896 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001897# else
1898 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1899# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001900 {
1901 /* already processed -- snatch the following arg */
1902 if (argc > 1)
1903 {
1904 --argc;
1905 ++argv;
1906 }
1907 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001908#endif
1909#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001910 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1911 {
1912 /* already processed, skip */
1913 }
1914#endif
1915 else
1916 {
1917 if (argv[0][argv_idx])
1918 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1919 had_minmin = TRUE;
1920 }
1921 if (!want_argument)
1922 argv_idx = -1; /* skip to next argument */
1923 break;
1924
1925 case 'A': /* "-A" start in Arabic mode */
1926#ifdef FEAT_ARABIC
1927 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1928#else
1929 mch_errmsg(_(e_noarabic));
1930 mch_exit(2);
1931#endif
1932 break;
1933
1934 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001935 /* Needs to be effective before expanding file names, because
1936 * for Win32 this makes us edit a shortcut file itself,
1937 * instead of the file it links to. */
1938 set_options_bin(curbuf->b_p_bin, 1, 0);
1939 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001940 break;
1941
1942 case 'C': /* "-C" Compatible */
1943 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001944 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001945 break;
1946
1947 case 'e': /* "-e" Ex mode */
1948 exmode_active = EXMODE_NORMAL;
1949 break;
1950
1951 case 'E': /* "-E" Improved Ex mode */
1952 exmode_active = EXMODE_VIM;
1953 break;
1954
1955 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1956 window directly, not with newcli */
1957#ifdef FEAT_GUI
1958 gui.dofork = FALSE; /* don't fork() when starting GUI */
1959#endif
1960 break;
1961
1962 case 'g': /* "-g" start GUI */
1963 main_start_gui();
1964 break;
1965
1966 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1967#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001968 p_fkmap = TRUE;
1969 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001970#else
1971 mch_errmsg(_(e_nofarsi));
1972 mch_exit(2);
1973#endif
1974 break;
1975
1976 case 'h': /* "-h" give help message */
1977#ifdef FEAT_GUI_GNOME
1978 /* Tell usage() to exit for "gvim". */
1979 gui.starting = FALSE;
1980#endif
1981 usage();
1982 break;
1983
1984 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1985#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001986 p_hkmap = TRUE;
1987 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001988#else
1989 mch_errmsg(_(e_nohebrew));
1990 mch_exit(2);
1991#endif
1992 break;
1993
1994 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1995#ifdef FEAT_LISP
1996 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1997 p_sm = TRUE;
1998#endif
1999 break;
2000
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002001 case 'M': /* "-M" no changes or writing of files */
2002 reset_modifiable();
2003 /* FALLTHROUGH */
2004
2005 case 'm': /* "-m" no writing of files */
2006 p_write = FALSE;
2007 break;
2008
2009 case 'y': /* "-y" easy mode */
2010#ifdef FEAT_GUI
2011 gui.starting = TRUE; /* start GUI a bit later */
2012#endif
2013 parmp->evim_mode = TRUE;
2014 break;
2015
2016 case 'N': /* "-N" Nocompatible */
2017 change_compatible(FALSE);
2018 break;
2019
2020 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002021#ifdef FEAT_NETBEANS_INTG
2022 /* checking for "-nb", netbeans parameters */
2023 if (argv[0][argv_idx] == 'b')
2024 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002025 netbeansArg = argv[0];
2026 argv_idx = -1; /* skip to next argument */
2027 }
2028 else
2029#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002030 parmp->no_swap_file = TRUE;
2031 break;
2032
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002033 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002034#ifdef TARGET_API_MAC_OSX
2035 /* For some reason on MacOS X, an argument like:
2036 -psn_0_10223617 is passed in when invoke from Finder
2037 or with the 'open' command */
2038 if (argv[0][argv_idx] == 's')
2039 {
2040 argv_idx = -1; /* bypass full -psn */
2041 main_start_gui();
2042 break;
2043 }
2044#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002045#ifdef FEAT_WINDOWS
2046 /* default is 0: open window for each file */
2047 parmp->window_count = get_number_arg((char_u *)argv[0],
2048 &argv_idx, 0);
2049 parmp->window_layout = WIN_TABS;
2050#endif
2051 break;
2052
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002053 case 'o': /* "-o[N]" open N horizontal split windows */
2054#ifdef FEAT_WINDOWS
2055 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002056 parmp->window_count = get_number_arg((char_u *)argv[0],
2057 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002058 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002059#endif
2060 break;
2061
2062 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002063#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002064 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002065 parmp->window_count = get_number_arg((char_u *)argv[0],
2066 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002067 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002068#endif
2069 break;
2070
2071#ifdef FEAT_QUICKFIX
2072 case 'q': /* "-q" QuickFix mode */
2073 if (parmp->edit_type != EDIT_NONE)
2074 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2075 parmp->edit_type = EDIT_QF;
2076 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2077 {
2078 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2079 argv_idx = -1;
2080 }
2081 else if (argc > 1) /* "-q {errorfile}" */
2082 want_argument = TRUE;
2083 break;
2084#endif
2085
2086 case 'R': /* "-R" readonly mode */
2087 readonlymode = TRUE;
2088 curbuf->b_p_ro = TRUE;
2089 p_uc = 10000; /* don't update very often */
2090 break;
2091
2092 case 'r': /* "-r" recovery mode */
2093 case 'L': /* "-L" recovery mode */
2094 recoverymode = 1;
2095 break;
2096
2097 case 's':
2098 if (exmode_active) /* "-s" silent (batch) mode */
2099 silent_mode = TRUE;
2100 else /* "-s {scriptin}" read from script file */
2101 want_argument = TRUE;
2102 break;
2103
2104 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2105 if (parmp->edit_type != EDIT_NONE)
2106 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2107 parmp->edit_type = EDIT_TAG;
2108 if (argv[0][argv_idx]) /* "-t{tag}" */
2109 {
2110 parmp->tagname = (char_u *)argv[0] + argv_idx;
2111 argv_idx = -1;
2112 }
2113 else /* "-t {tag}" */
2114 want_argument = TRUE;
2115 break;
2116
2117#ifdef FEAT_EVAL
2118 case 'D': /* "-D" Debugging */
2119 parmp->use_debug_break_level = 9999;
2120 break;
2121#endif
2122#ifdef FEAT_DIFF
2123 case 'd': /* "-d" 'diff' */
2124# ifdef AMIGA
2125 /* check for "-dev {device}" */
2126 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2127 want_argument = TRUE;
2128 else
2129# endif
2130 parmp->diff_mode = TRUE;
2131 break;
2132#endif
2133 case 'V': /* "-V{N}" Verbose level */
2134 /* default is 10: a little bit verbose */
2135 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2136 if (argv[0][argv_idx] != NUL)
2137 {
2138 set_option_value((char_u *)"verbosefile", 0L,
2139 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002140 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002141 }
2142 break;
2143
2144 case 'v': /* "-v" Vi-mode (as if called "vi") */
2145 exmode_active = 0;
2146#ifdef FEAT_GUI
2147 gui.starting = FALSE; /* don't start GUI */
2148#endif
2149 break;
2150
2151 case 'w': /* "-w{number}" set window height */
2152 /* "-w {scriptout}" write to script */
2153 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2154 {
2155 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2156 set_option_value((char_u *)"window", n, NULL, 0);
2157 break;
2158 }
2159 want_argument = TRUE;
2160 break;
2161
2162#ifdef FEAT_CRYPT
2163 case 'x': /* "-x" encrypted reading/writing of files */
2164 parmp->ask_for_key = TRUE;
2165 break;
2166#endif
2167
2168 case 'X': /* "-X" don't connect to X server */
2169#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2170 x_no_connect = TRUE;
2171#endif
2172 break;
2173
2174 case 'Z': /* "-Z" restricted mode */
2175 restricted = TRUE;
2176 break;
2177
2178 case 'c': /* "-c{command}" or "-c {command}" execute
2179 command */
2180 if (argv[0][argv_idx] != NUL)
2181 {
2182 if (parmp->n_commands >= MAX_ARG_CMDS)
2183 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002184 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2185 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002186 argv_idx = -1;
2187 break;
2188 }
2189 /*FALLTHROUGH*/
2190 case 'S': /* "-S {file}" execute Vim script */
2191 case 'i': /* "-i {viminfo}" use for viminfo */
2192#ifndef FEAT_DIFF
2193 case 'd': /* "-d {device}" device (for Amiga) */
2194#endif
2195 case 'T': /* "-T {terminal}" terminal name */
2196 case 'u': /* "-u {vimrc}" vim inits file */
2197 case 'U': /* "-U {gvimrc}" gvim inits file */
2198 case 'W': /* "-W {scriptout}" overwrite */
2199#ifdef FEAT_GUI_W32
2200 case 'P': /* "-P {parent title}" MDI parent */
2201#endif
2202 want_argument = TRUE;
2203 break;
2204
2205 default:
2206 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2207 }
2208
2209 /*
2210 * Handle option arguments with argument.
2211 */
2212 if (want_argument)
2213 {
2214 /*
2215 * Check for garbage immediately after the option letter.
2216 */
2217 if (argv[0][argv_idx] != NUL)
2218 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2219
2220 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002221 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002222 mainerr_arg_missing((char_u *)argv[0]);
2223 ++argv;
2224 argv_idx = -1;
2225
2226 switch (c)
2227 {
2228 case 'c': /* "-c {command}" execute command */
2229 case 'S': /* "-S {file}" execute Vim script */
2230 if (parmp->n_commands >= MAX_ARG_CMDS)
2231 mainerr(ME_EXTRA_CMD, NULL);
2232 if (c == 'S')
2233 {
2234 char *a;
2235
2236 if (argc < 1)
2237 /* "-S" without argument: use default session file
2238 * name. */
2239 a = SESSION_FILE;
2240 else if (argv[0][0] == '-')
2241 {
2242 /* "-S" followed by another option: use default
2243 * session file name. */
2244 a = SESSION_FILE;
2245 ++argc;
2246 --argv;
2247 }
2248 else
2249 a = argv[0];
2250 p = alloc((unsigned)(STRLEN(a) + 4));
2251 if (p == NULL)
2252 mch_exit(2);
2253 sprintf((char *)p, "so %s", a);
2254 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2255 parmp->commands[parmp->n_commands++] = p;
2256 }
2257 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002258 parmp->commands[parmp->n_commands++] =
2259 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002260 break;
2261
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002262 case '-':
2263 if (argv[-1][2] == 'c')
2264 {
2265 /* "--cmd {command}" execute command */
2266 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2267 mainerr(ME_EXTRA_CMD, NULL);
2268 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002269 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002270 }
2271 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002272 break;
2273
2274 /* case 'd': -d {device} is handled in mch_check_win() for the
2275 * Amiga */
2276
2277#ifdef FEAT_QUICKFIX
2278 case 'q': /* "-q {errorfile}" QuickFix mode */
2279 parmp->use_ef = (char_u *)argv[0];
2280 break;
2281#endif
2282
2283 case 'i': /* "-i {viminfo}" use for viminfo */
2284 use_viminfo = (char_u *)argv[0];
2285 break;
2286
2287 case 's': /* "-s {scriptin}" read from script file */
2288 if (scriptin[0] != NULL)
2289 {
2290scripterror:
2291 mch_errmsg(_("Attempt to open script file again: \""));
2292 mch_errmsg(argv[-1]);
2293 mch_errmsg(" ");
2294 mch_errmsg(argv[0]);
2295 mch_errmsg("\"\n");
2296 mch_exit(2);
2297 }
2298 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2299 {
2300 mch_errmsg(_("Cannot open for reading: \""));
2301 mch_errmsg(argv[0]);
2302 mch_errmsg("\"\n");
2303 mch_exit(2);
2304 }
2305 if (save_typebuf() == FAIL)
2306 mch_exit(2); /* out of memory */
2307 break;
2308
2309 case 't': /* "-t {tag}" */
2310 parmp->tagname = (char_u *)argv[0];
2311 break;
2312
2313 case 'T': /* "-T {terminal}" terminal name */
2314 /*
2315 * The -T term argument is always available and when
2316 * HAVE_TERMLIB is supported it overrides the environment
2317 * variable TERM.
2318 */
2319#ifdef FEAT_GUI
2320 if (term_is_gui((char_u *)argv[0]))
2321 gui.starting = TRUE; /* start GUI a bit later */
2322 else
2323#endif
2324 parmp->term = (char_u *)argv[0];
2325 break;
2326
2327 case 'u': /* "-u {vimrc}" vim inits file */
2328 parmp->use_vimrc = (char_u *)argv[0];
2329 break;
2330
2331 case 'U': /* "-U {gvimrc}" gvim inits file */
2332#ifdef FEAT_GUI
2333 use_gvimrc = (char_u *)argv[0];
2334#endif
2335 break;
2336
2337 case 'w': /* "-w {nr}" 'window' value */
2338 /* "-w {scriptout}" append to script file */
2339 if (vim_isdigit(*((char_u *)argv[0])))
2340 {
2341 argv_idx = 0;
2342 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2343 set_option_value((char_u *)"window", n, NULL, 0);
2344 argv_idx = -1;
2345 break;
2346 }
2347 /*FALLTHROUGH*/
2348 case 'W': /* "-W {scriptout}" overwrite script file */
2349 if (scriptout != NULL)
2350 goto scripterror;
2351 if ((scriptout = mch_fopen(argv[0],
2352 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2353 {
2354 mch_errmsg(_("Cannot open for script output: \""));
2355 mch_errmsg(argv[0]);
2356 mch_errmsg("\"\n");
2357 mch_exit(2);
2358 }
2359 break;
2360
2361#ifdef FEAT_GUI_W32
2362 case 'P': /* "-P {parent title}" MDI parent */
2363 gui_mch_set_parent(argv[0]);
2364 break;
2365#endif
2366 }
2367 }
2368 }
2369
2370 /*
2371 * File name argument.
2372 */
2373 else
2374 {
2375 argv_idx = -1; /* skip to next argument */
2376
2377 /* Check for only one type of editing. */
2378 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2379 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2380 parmp->edit_type = EDIT_FILE;
2381
2382#ifdef MSWIN
2383 /* Remember if the argument was a full path before changing
2384 * slashes to backslashes. */
2385 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2386 parmp->full_path = TRUE;
2387#endif
2388
2389 /* Add the file to the global argument list. */
2390 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2391 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2392 mch_exit(2);
2393#ifdef FEAT_DIFF
2394 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2395 && !mch_isdir(alist_name(&GARGLIST[0])))
2396 {
2397 char_u *r;
2398
2399 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2400 if (r != NULL)
2401 {
2402 vim_free(p);
2403 p = r;
2404 }
2405 }
2406#endif
2407#if defined(__CYGWIN32__) && !defined(WIN32)
2408 /*
2409 * If vim is invoked by non-Cygwin tools, convert away any
2410 * DOS paths, so things like .swp files are created correctly.
2411 * Look for evidence of non-Cygwin paths before we bother.
2412 * This is only for when using the Unix files.
2413 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002414 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002415 {
2416 char posix_path[PATH_MAX];
2417
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002418# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2419 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2420# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002421 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002422# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002423 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002424 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002425 if (p == NULL)
2426 mch_exit(2);
2427 }
2428#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002429
2430#ifdef USE_FNAME_CASE
2431 /* Make the case of the file name match the actual file. */
2432 fname_case(p, 0);
2433#endif
2434
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002435 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002436#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002437 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002438#else
2439 2 /* add buffer number now and use curbuf */
2440#endif
2441 );
2442
2443#if defined(FEAT_MBYTE) && defined(WIN32)
2444 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002445 /* Remember this argument has been added to the argument list.
2446 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002447 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002448# ifdef FEAT_DIFF
2449 parmp->diff_mode
2450# else
2451 FALSE
2452# endif
2453 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454 }
2455#endif
2456 }
2457
2458 /*
2459 * If there are no more letters after the current "-", go to next
2460 * argument. argv_idx is set to -1 when the current argument is to be
2461 * skipped.
2462 */
2463 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2464 {
2465 --argc;
2466 ++argv;
2467 argv_idx = 1;
2468 }
2469 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002470
2471#ifdef FEAT_EVAL
2472 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2473 * one. */
2474 if (parmp->n_commands > 0)
2475 {
2476 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2477 if (p != NULL)
2478 {
2479 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2480 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2481 vim_free(p);
2482 }
2483 }
2484#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002485}
2486
2487/*
2488 * Print a warning if stdout is not a terminal.
2489 * When starting in Ex mode and commands come from a file, set Silent mode.
2490 */
2491 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002492check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002493{
2494 int input_isatty; /* is active input a terminal? */
2495
2496 input_isatty = mch_input_isatty();
2497 if (exmode_active)
2498 {
2499 if (!input_isatty)
2500 silent_mode = TRUE;
2501 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002502 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002503#ifdef FEAT_GUI
2504 /* don't want the delay when started from the desktop */
2505 && !gui.starting
2506#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002507 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508 {
2509#ifdef NBDEBUG
2510 /*
2511 * This shouldn't be necessary. But if I run netbeans with the log
2512 * output coming to the console and XOpenDisplay fails, I get vim
2513 * trying to start with input/output to my console tty. This fills my
2514 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002515 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002516 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002517 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002518 {
2519 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2520 exit(1);
2521 }
2522#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002523#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2524 if (is_cygpty_used())
2525 {
2526 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2527 exit(1);
2528 }
2529#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002530 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002531 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2532 if (!input_isatty)
2533 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2534 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002535 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2536 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 if (scriptin[0] == NULL)
2538 ui_delay(2000L, TRUE);
2539 TIME_MSG("Warning delay");
2540 }
2541}
2542
2543/*
2544 * Read text from stdin.
2545 */
2546 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002547read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002548{
2549 int i;
2550
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002551#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002552 /* When getting the ATTENTION prompt here, use a dialog */
2553 swap_exists_action = SEA_DIALOG;
2554#endif
2555 no_wait_return = TRUE;
2556 i = msg_didany;
2557 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002558 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002559 no_wait_return = FALSE;
2560 msg_didany = i;
2561 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002562#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002563 check_swap_exists_action();
2564#endif
2565#if !(defined(AMIGA) || defined(MACOS))
2566 /*
2567 * Close stdin and dup it from stderr. Required for GPM to work
2568 * properly, and for running external commands.
2569 * Is there any other system that cannot do this?
2570 */
2571 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002572 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573#endif
2574}
2575
2576/*
2577 * Create the requested number of windows and edit buffers in them.
2578 * Also does recovery if "recoverymode" set.
2579 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002580 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002581create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002582{
2583#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002584 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002585 int done = 0;
2586
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587 /*
2588 * Create the number of windows that was requested.
2589 */
2590 if (parmp->window_count == -1) /* was not set */
2591 parmp->window_count = 1;
2592 if (parmp->window_count == 0)
2593 parmp->window_count = GARGCOUNT;
2594 if (parmp->window_count > 1)
2595 {
2596 /* Don't change the windows if there was a command in .vimrc that
2597 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002598 if (parmp->window_layout == 0)
2599 parmp->window_layout = WIN_HOR;
2600 if (parmp->window_layout == WIN_TABS)
2601 {
2602 parmp->window_count = make_tabpages(parmp->window_count);
2603 TIME_MSG("making tab pages");
2604 }
2605 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002606 {
2607 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002608 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002609 TIME_MSG("making windows");
2610 }
2611 else
2612 parmp->window_count = win_count();
2613 }
2614 else
2615 parmp->window_count = 1;
2616#endif
2617
2618 if (recoverymode) /* do recover */
2619 {
2620 msg_scroll = TRUE; /* scroll message up */
2621 ml_recover();
2622 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2623 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002624 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625 }
2626 else
2627 {
2628 /*
2629 * Open a buffer for windows that don't have one yet.
2630 * Commands in the .vimrc might have loaded a file or split the window.
2631 * Watch out for autocommands that delete a window.
2632 */
2633#ifdef FEAT_AUTOCMD
2634 /*
2635 * Don't execute Win/Buf Enter/Leave autocommands here
2636 */
2637 ++autocmd_no_enter;
2638 ++autocmd_no_leave;
2639#endif
2640#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002641 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002642 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002643 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002644 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002645 {
2646 if (parmp->window_layout == WIN_TABS)
2647 goto_tabpage(1);
2648 else
2649 curwin = firstwin;
2650 }
2651 else if (parmp->window_layout == WIN_TABS)
2652 {
2653 if (curtab->tp_next == NULL)
2654 break;
2655 goto_tabpage(0);
2656 }
2657 else
2658 {
2659 if (curwin->w_next == NULL)
2660 break;
2661 curwin = curwin->w_next;
2662 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002663 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002664#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002665 curbuf = curwin->w_buffer;
2666 if (curbuf->b_ml.ml_mfp == NULL)
2667 {
2668#ifdef FEAT_FOLDING
2669 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2670 if (p_fdls >= 0)
2671 curwin->w_p_fdl = p_fdls;
2672#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002673#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002674 /* When getting the ATTENTION prompt here, use a dialog */
2675 swap_exists_action = SEA_DIALOG;
2676#endif
2677 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002678
2679 /* create memfile, read file */
2680 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002681
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002682#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002683 if (swap_exists_action == SEA_QUIT)
2684 {
2685 if (got_int || only_one_window())
2686 {
2687 /* abort selected or quit and only one window */
2688 did_emsg = FALSE; /* avoid hit-enter prompt */
2689 getout(1);
2690 }
2691 /* We can't close the window, it would disturb what
2692 * happens next. Clear the file name and set the arg
2693 * index to -1 to delete it later. */
2694 setfname(curbuf, NULL, NULL, FALSE);
2695 curwin->w_arg_idx = -1;
2696 swap_exists_action = SEA_NONE;
2697 }
2698 else
2699 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002700#endif
2701#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002703#endif
2704 }
2705#ifdef FEAT_WINDOWS
2706 ui_breakcheck();
2707 if (got_int)
2708 {
2709 (void)vgetc(); /* only break the file loading, not the rest */
2710 break;
2711 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002712 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002713#endif
2714#ifdef FEAT_WINDOWS
2715 if (parmp->window_layout == WIN_TABS)
2716 goto_tabpage(1);
2717 else
2718 curwin = firstwin;
2719 curbuf = curwin->w_buffer;
2720#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002721#ifdef FEAT_AUTOCMD
2722 --autocmd_no_enter;
2723 --autocmd_no_leave;
2724#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002725 }
2726}
2727
2728#ifdef FEAT_WINDOWS
2729 /*
2730 * If opened more than one window, start editing files in the other
2731 * windows. make_windows() has already opened the windows.
2732 */
2733 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002734edit_buffers(
2735 mparm_T *parmp,
2736 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737{
2738 int arg_idx; /* index in argument list */
2739 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002740 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002741 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002742
2743# ifdef FEAT_AUTOCMD
2744 /*
2745 * Don't execute Win/Buf Enter/Leave autocommands here
2746 */
2747 ++autocmd_no_enter;
2748 ++autocmd_no_leave;
2749# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002750
2751 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2752 if (curwin->w_arg_idx == -1)
2753 {
2754 win_close(curwin, TRUE);
2755 advance = FALSE;
2756 }
2757
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758 arg_idx = 1;
2759 for (i = 1; i < parmp->window_count; ++i)
2760 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002761 if (cwd != NULL)
2762 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002763 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2764 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002765 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002766 ++arg_idx;
2767 win_close(curwin, TRUE);
2768 advance = FALSE;
2769 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002770 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002771
Bram Moolenaar84212822006-11-07 21:59:47 +00002772 if (advance)
2773 {
2774 if (parmp->window_layout == WIN_TABS)
2775 {
2776 if (curtab->tp_next == NULL) /* just checking */
2777 break;
2778 goto_tabpage(0);
2779 }
2780 else
2781 {
2782 if (curwin->w_next == NULL) /* just checking */
2783 break;
2784 win_enter(curwin->w_next, FALSE);
2785 }
2786 }
2787 advance = TRUE;
2788
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002789 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002790 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002791 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2792 {
2793 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002794 /* Edit file from arg list, if there is one. When "Quit" selected
2795 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002796# ifdef HAS_SWAP_EXISTS_ACTION
2797 swap_exists_did_quit = FALSE;
2798# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002799 (void)do_ecmd(0, arg_idx < GARGCOUNT
2800 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002801 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002802# ifdef HAS_SWAP_EXISTS_ACTION
2803 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002804 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002805 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002806 if (got_int || only_one_window())
2807 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002808 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002809 did_emsg = FALSE; /* avoid hit-enter prompt */
2810 getout(1);
2811 }
2812 win_close(curwin, TRUE);
2813 advance = FALSE;
2814 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002815# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816 if (arg_idx == GARGCOUNT - 1)
2817 arg_had_last = TRUE;
2818 ++arg_idx;
2819 }
2820 ui_breakcheck();
2821 if (got_int)
2822 {
2823 (void)vgetc(); /* only break the file loading, not the rest */
2824 break;
2825 }
2826 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002827
2828 if (parmp->window_layout == WIN_TABS)
2829 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002830# ifdef FEAT_AUTOCMD
2831 --autocmd_no_enter;
2832# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002833
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002834 /* make the first window the current window */
2835 win = firstwin;
2836#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2837 /* Avoid making a preview window the current window. */
2838 while (win->w_p_pvw)
2839 {
2840 win = win->w_next;
2841 if (win == NULL)
2842 {
2843 win = firstwin;
2844 break;
2845 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002846 }
2847#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002848 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002849
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850# ifdef FEAT_AUTOCMD
2851 --autocmd_no_leave;
2852# endif
2853 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002854 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002855 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2856}
2857#endif /* FEAT_WINDOWS */
2858
2859/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002860 * Execute the commands from --cmd arguments "cmds[cnt]".
2861 */
2862 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002863exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002864{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002865 char_u **cmds = parmp->pre_commands;
2866 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002867 int i;
2868
2869 if (cnt > 0)
2870 {
2871 curwin->w_cursor.lnum = 0; /* just in case.. */
2872 sourcing_name = (char_u *)_("pre-vimrc command line");
2873# ifdef FEAT_EVAL
2874 current_SID = SID_CMDARG;
2875# endif
2876 for (i = 0; i < cnt; ++i)
2877 do_cmdline_cmd(cmds[i]);
2878 sourcing_name = NULL;
2879# ifdef FEAT_EVAL
2880 current_SID = 0;
2881# endif
2882 TIME_MSG("--cmd commands");
2883 }
2884}
2885
2886/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002887 * Execute "+", "-c" and "-S" arguments.
2888 */
2889 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002890exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002891{
2892 int i;
2893
2894 /*
2895 * We start commands on line 0, make "vim +/pat file" match a
2896 * pattern on line 1. But don't move the cursor when an autocommand
2897 * with g`" was used.
2898 */
2899 msg_scroll = TRUE;
2900 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2901 curwin->w_cursor.lnum = 0;
2902 sourcing_name = (char_u *)"command line";
2903#ifdef FEAT_EVAL
2904 current_SID = SID_CARG;
2905#endif
2906 for (i = 0; i < parmp->n_commands; ++i)
2907 {
2908 do_cmdline_cmd(parmp->commands[i]);
2909 if (parmp->cmds_tofree[i])
2910 vim_free(parmp->commands[i]);
2911 }
2912 sourcing_name = NULL;
2913#ifdef FEAT_EVAL
2914 current_SID = 0;
2915#endif
2916 if (curwin->w_cursor.lnum == 0)
2917 curwin->w_cursor.lnum = 1;
2918
2919 if (!exmode_active)
2920 msg_scroll = FALSE;
2921
2922#ifdef FEAT_QUICKFIX
2923 /* When started with "-q errorfile" jump to first error again. */
2924 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002926#endif
2927 TIME_MSG("executing command arguments");
2928}
2929
2930/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002931 * Source startup scripts.
2932 */
2933 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002934source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002935{
2936 int i;
2937
2938 /*
2939 * For "evim" source evim.vim first of all, so that the user can overrule
2940 * any things he doesn't like.
2941 */
2942 if (parmp->evim_mode)
2943 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002944 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002945 TIME_MSG("source evim file");
2946 }
2947
2948 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002949 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002950 * nothing else.
2951 */
2952 if (parmp->use_vimrc != NULL)
2953 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002954 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2955 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002956 {
2957#ifdef FEAT_GUI
2958 if (use_gvimrc == NULL) /* don't load gvimrc either */
2959 use_gvimrc = parmp->use_vimrc;
2960#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002961 }
2962 else
2963 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002964 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002965 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2966 }
2967 }
2968 else if (!silent_mode)
2969 {
2970#ifdef AMIGA
2971 struct Process *proc = (struct Process *)FindTask(0L);
2972 APTR save_winptr = proc->pr_WindowPtr;
2973
2974 /* Avoid a requester here for a volume that doesn't exist. */
2975 proc->pr_WindowPtr = (APTR)-1L;
2976#endif
2977
2978 /*
2979 * Get system wide defaults, if the file name is defined.
2980 */
2981#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002982 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002983#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002984#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002985 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002986#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002987
2988 /*
2989 * Try to read initialization commands from the following places:
2990 * - environment variable VIMINIT
2991 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2992 * - second user vimrc file ($VIM/.vimrc for Dos)
2993 * - environment variable EXINIT
2994 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2995 * - second user exrc file ($VIM/.exrc for Dos)
2996 * The first that exists is used, the rest is ignored.
2997 */
2998 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2999 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003000 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003002 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3003 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004#endif
3005#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003006 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3007 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003009#ifdef USR_VIMRC_FILE4
3010 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3011 DOSO_VIMRC) == FAIL
3012#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003013 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003014 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003015#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003016 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003017#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003018 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003019 {
3020 /* When no .vimrc file was found: source defaults.vim. */
3021 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003022 }
3023 }
3024
3025 /*
3026 * Read initialization commands from ".vimrc" or ".exrc" in current
3027 * directory. This is only done if the 'exrc' option is set.
3028 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003029 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003030 * option has been reset in environment of global ".exrc" or ".vimrc".
3031 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3032 * SYS_VIMRC_FILE.
3033 */
3034 if (p_exrc)
3035 {
3036#if defined(UNIX) || defined(VMS)
3037 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3038 if (!file_owned(VIMRC_FILE))
3039#endif
3040 secure = p_secure;
3041
3042 i = FAIL;
3043 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3044 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3045#ifdef USR_VIMRC_FILE2
3046 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3047 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3048#endif
3049#ifdef USR_VIMRC_FILE3
3050 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3051 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3052#endif
3053#ifdef SYS_VIMRC_FILE
3054 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3055 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3056#endif
3057 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003058 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003059
3060 if (i == FAIL)
3061 {
3062#if defined(UNIX) || defined(VMS)
3063 /* if ".exrc" is not owned by user set 'secure' mode */
3064 if (!file_owned(EXRC_FILE))
3065 secure = p_secure;
3066 else
3067 secure = 0;
3068#endif
3069 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3070 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3071#ifdef USR_EXRC_FILE2
3072 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3073 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3074#endif
3075 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003076 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003077 }
3078 }
3079 if (secure == 2)
3080 need_wait_return = TRUE;
3081 secure = 0;
3082#ifdef AMIGA
3083 proc->pr_WindowPtr = save_winptr;
3084#endif
3085 }
3086 TIME_MSG("sourcing vimrc file(s)");
3087}
3088
3089/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003090 * Setup to start using the GUI. Exit with an error when not available.
3091 */
3092 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003093main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003094{
3095#ifdef FEAT_GUI
3096 gui.starting = TRUE; /* start GUI a bit later */
3097#else
3098 mch_errmsg(_(e_nogvim));
3099 mch_errmsg("\n");
3100 mch_exit(2);
3101#endif
3102}
3103
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003104#endif /* NO_VIM_MAIN */
3105
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003106/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003107 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003108 * Returns FAIL if the environment variable was not executed, OK otherwise.
3109 */
3110 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003111process_env(
3112 char_u *env,
3113 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003114{
3115 char_u *initstr;
3116 char_u *save_sourcing_name;
3117 linenr_T save_sourcing_lnum;
3118#ifdef FEAT_EVAL
3119 scid_T save_sid;
3120#endif
3121
3122 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3123 {
3124 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003125 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003126 save_sourcing_name = sourcing_name;
3127 save_sourcing_lnum = sourcing_lnum;
3128 sourcing_name = env;
3129 sourcing_lnum = 0;
3130#ifdef FEAT_EVAL
3131 save_sid = current_SID;
3132 current_SID = SID_ENV;
3133#endif
3134 do_cmdline_cmd(initstr);
3135 sourcing_name = save_sourcing_name;
3136 sourcing_lnum = save_sourcing_lnum;
3137#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003138 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003139#endif
3140 return OK;
3141 }
3142 return FAIL;
3143}
3144
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003145#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003146/*
3147 * Return TRUE if we are certain the user owns the file "fname".
3148 * Used for ".vimrc" and ".exrc".
3149 * Use both stat() and lstat() for extra security.
3150 */
3151 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003152file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003153{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003154 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003155# ifdef UNIX
3156 uid_t uid = getuid();
3157# else /* VMS */
3158 uid_t uid = ((getgid() << 16) | getuid());
3159# endif
3160
3161 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3162# ifdef HAVE_LSTAT
3163 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3164# endif
3165 );
3166}
3167#endif
3168
3169/*
3170 * Give an error message main_errors["n"] and exit.
3171 */
3172 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003173mainerr(
3174 int n, /* one of the ME_ defines */
3175 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003176{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003177#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003178 reset_signals(); /* kill us with CTRL-C here, if you like */
3179#endif
3180
3181 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003182 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003183 mch_errmsg(_(main_errors[n]));
3184 if (str != NULL)
3185 {
3186 mch_errmsg(": \"");
3187 mch_errmsg((char *)str);
3188 mch_errmsg("\"");
3189 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003190 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003191
3192 mch_exit(1);
3193}
3194
3195 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003196mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003197{
3198 mainerr(ME_ARG_MISSING, str);
3199}
3200
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003201#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202/*
3203 * print a message with three spaces prepended and '\n' appended.
3204 */
3205 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003206main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003207{
3208 mch_msg(" ");
3209 mch_msg(s);
3210 mch_msg("\n");
3211}
3212
3213/*
3214 * Print messages for "vim -h" or "vim --help" and exit.
3215 */
3216 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003217usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218{
3219 int i;
3220 static char *(use[]) =
3221 {
3222 N_("[file ..] edit specified file(s)"),
3223 N_("- read text from stdin"),
3224 N_("-t tag edit file where tag is defined"),
3225#ifdef FEAT_QUICKFIX
3226 N_("-q [errorfile] edit file with first error")
3227#endif
3228 };
3229
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003230#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003231 reset_signals(); /* kill us with CTRL-C here, if you like */
3232#endif
3233
3234 mch_msg(longVersion);
3235 mch_msg(_("\n\nusage:"));
3236 for (i = 0; ; ++i)
3237 {
3238 mch_msg(_(" vim [arguments] "));
3239 mch_msg(_(use[i]));
3240 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3241 break;
3242 mch_msg(_("\n or:"));
3243 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003244#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003245 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003246#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003247
3248 mch_msg(_("\n\nArguments:\n"));
3249 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003250#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003251 main_msg(_("--literal\t\tDon't expand wildcards"));
3252#endif
3253#ifdef FEAT_OLE
3254 main_msg(_("-register\t\tRegister this gvim for OLE"));
3255 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3256#endif
3257#ifdef FEAT_GUI
3258 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3259 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3260#endif
3261 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3262 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003263 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003264 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3265#ifdef FEAT_DIFF
3266 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3267#endif
3268 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3269 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3270 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3271 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3272 main_msg(_("-M\t\t\tModifications in text not allowed"));
3273 main_msg(_("-b\t\t\tBinary mode"));
3274#ifdef FEAT_LISP
3275 main_msg(_("-l\t\t\tLisp mode"));
3276#endif
3277 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3278 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003279 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3280#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003281 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003282#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003283 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3284 main_msg(_("-r\t\t\tList swap files and exit"));
3285 main_msg(_("-r (with file name)\tRecover crashed session"));
3286 main_msg(_("-L\t\t\tSame as -r"));
3287#ifdef AMIGA
3288 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3289 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3290#endif
3291#ifdef FEAT_ARABIC
3292 main_msg(_("-A\t\t\tstart in Arabic mode"));
3293#endif
3294#ifdef FEAT_RIGHTLEFT
3295 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3296#endif
3297#ifdef FEAT_FKMAP
3298 main_msg(_("-F\t\t\tStart in Farsi mode"));
3299#endif
3300 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003301 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003302 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003303 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3304#ifdef FEAT_GUI
3305 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3306#endif
3307 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003308#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003309 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003310 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3311 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003312#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003313 main_msg(_("+\t\t\tStart at end of file"));
3314 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003315 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003316 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3317 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3318 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3319 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3320 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3321#ifdef FEAT_CRYPT
3322 main_msg(_("-x\t\t\tEdit encrypted files"));
3323#endif
3324#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3325# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3326 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3327# endif
3328 main_msg(_("-X\t\t\tDo not connect to X server"));
3329#endif
3330#ifdef FEAT_CLIENTSERVER
3331 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3332 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3333 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3334 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003335# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003336 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003337# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003338 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3339 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3340 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3341 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3342#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003343#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003344 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003345#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003346#ifdef FEAT_VIMINFO
3347 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3348#endif
3349 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3350 main_msg(_("--version\t\tPrint version information and exit"));
3351
3352#ifdef FEAT_GUI_X11
3353# ifdef FEAT_GUI_MOTIF
3354 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3355# else
3356# ifdef FEAT_GUI_ATHENA
3357# ifdef FEAT_GUI_NEXTAW
3358 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3359# else
3360 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3361# endif
3362# endif
3363# endif
3364 main_msg(_("-display <display>\tRun vim on <display>"));
3365 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003366 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3367 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3368 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3369 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3370 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3371 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3372 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3373 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3374# ifdef FEAT_GUI_ATHENA
3375 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3376# endif
3377 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3378 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3379 main_msg(_("-xrm <resource>\tSet the specified resource"));
3380#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003381#ifdef FEAT_GUI_GTK
3382 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3383 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3384 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3385 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3386 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003389 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003390#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391#ifdef FEAT_GUI_W32
3392 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003393 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003394#endif
3395
3396#ifdef FEAT_GUI_GNOME
3397 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3398 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003399 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003401 gui.dofork = FALSE;
3402 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403 else
3404#endif
3405 mch_exit(0);
3406}
3407
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003408#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409/*
3410 * Check the result of the ATTENTION dialog:
3411 * When "Quit" selected, exit Vim.
3412 * When "Recover" selected, recover the file.
3413 */
3414 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003415check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003416{
3417 if (swap_exists_action == SEA_QUIT)
3418 getout(1);
3419 handle_swap_exists(NULL);
3420}
3421#endif
3422
Bram Moolenaar08cab962017-03-04 14:37:18 +01003423#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003424
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003426static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427
3428static struct timeval prev_timeval;
3429
Bram Moolenaar3f269672009-11-03 11:11:11 +00003430# ifdef WIN3264
3431/*
3432 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3433 */
3434 static int
3435gettimeofday(struct timeval *tv, char *dummy)
3436{
3437 long t = clock();
3438 tv->tv_sec = t / CLOCKS_PER_SEC;
3439 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3440 return 0;
3441}
3442# endif
3443
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003444/*
3445 * Save the previous time before doing something that could nest.
3446 * set "*tv_rel" to the time elapsed so far.
3447 */
3448 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003449time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450{
3451 *((struct timeval *)tv_rel) = prev_timeval;
3452 gettimeofday(&prev_timeval, NULL);
3453 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3454 - ((struct timeval *)tv_rel)->tv_usec;
3455 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3456 - ((struct timeval *)tv_rel)->tv_sec;
3457 if (((struct timeval *)tv_rel)->tv_usec < 0)
3458 {
3459 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3460 --((struct timeval *)tv_rel)->tv_sec;
3461 }
3462 *(struct timeval *)tv_start = prev_timeval;
3463}
3464
3465/*
3466 * Compute the previous time after doing something that could nest.
3467 * Subtract "*tp" from prev_timeval;
3468 * Note: The arguments are (void *) to avoid trouble with systems that don't
3469 * have struct timeval.
3470 */
3471 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003472time_pop(
3473 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003474{
3475 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3476 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3477 if (prev_timeval.tv_usec < 0)
3478 {
3479 prev_timeval.tv_usec += 1000000;
3480 --prev_timeval.tv_sec;
3481 }
3482}
3483
3484 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003485time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003486{
3487 long usec;
3488 long msec;
3489
3490 usec = now->tv_usec - then->tv_usec;
3491 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3492 usec = usec % 1000L;
3493 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3494}
3495
3496 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003497time_msg(
3498 char *mesg,
3499 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003500 (struct timeval *) */
3501{
3502 static struct timeval start;
3503 struct timeval now;
3504
3505 if (time_fd != NULL)
3506 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003507 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003508 {
3509 gettimeofday(&start, NULL);
3510 prev_timeval = start;
3511 fprintf(time_fd, "\n\ntimes in msec\n");
3512 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3513 fprintf(time_fd, " clock elapsed: other lines\n\n");
3514 }
3515 gettimeofday(&now, NULL);
3516 time_diff(&start, &now);
3517 if (((struct timeval *)tv_start) != NULL)
3518 {
3519 fprintf(time_fd, " ");
3520 time_diff(((struct timeval *)tv_start), &now);
3521 }
3522 fprintf(time_fd, " ");
3523 time_diff(&prev_timeval, &now);
3524 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003525 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003526 }
3527}
3528
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529#endif
3530
Bram Moolenaar08cab962017-03-04 14:37:18 +01003531#ifndef NO_VIM_MAIN
3532 static void
3533set_progpath(char_u *argv0)
3534{
3535 char_u *val = argv0;
3536 char_u buf[MAXPATHL];
3537
3538 /* A relative path containing a "/" will become invalid when using ":cd",
3539 * turn it into a full path.
3540 * On MS-Windows "vim.exe" is found in the current directory, thus also do
3541 * it when there is no path and the file exists. */
3542 if ( !mch_isFullName(argv0)
3543# ifdef WIN32
3544 && mch_can_exe(argv0, NULL, TRUE)
3545# else
3546 && gettail(argv0) != argv0
3547# endif
3548 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3549 val = buf;
3550 set_vim_var_string(VV_PROGPATH, val, -1);
3551}
3552
3553#endif /* NO_VIM_MAIN */
3554
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003555#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003556
3557/*
3558 * Common code for the X command server and the Win32 command server.
3559 */
3560
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003561static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003562
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003563/*
3564 * Do the client-server stuff, unless "--servername ''" was used.
3565 */
3566 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003567exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003568{
3569 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3570 {
3571# ifdef WIN32
3572 /* Initialise the client/server messaging infrastructure. */
3573 serverInitMessaging();
3574# endif
3575
3576 /*
3577 * When a command server argument was found, execute it. This may
3578 * exit Vim when it was successful. Otherwise it's executed further
3579 * on. Remember the encoding used here in "serverStrEnc".
3580 */
3581 if (parmp->serverArg)
3582 {
3583 cmdsrv_main(&parmp->argc, parmp->argv,
3584 parmp->serverName_arg, &parmp->serverStr);
3585# ifdef FEAT_MBYTE
3586 parmp->serverStrEnc = vim_strsave(p_enc);
3587# endif
3588 }
3589
3590 /* If we're still running, get the name to register ourselves.
3591 * On Win32 can register right now, for X11 need to setup the
3592 * clipboard first, it's further down. */
3593 parmp->servername = serverMakeName(parmp->serverName_arg,
3594 parmp->argv[0]);
3595# ifdef WIN32
3596 if (parmp->servername != NULL)
3597 {
3598 serverSetName(parmp->servername);
3599 vim_free(parmp->servername);
3600 }
3601# endif
3602 }
3603}
3604
3605/*
3606 * Prepare for running as a Vim server.
3607 */
3608 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003609prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003610{
3611# if defined(FEAT_X11)
3612 /*
3613 * Register for remote command execution with :serversend and --remote
3614 * unless there was a -X or a --servername '' on the command line.
3615 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003616 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003617 */
3618 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3619# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003620 (gui.in_use
3621# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003622 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003623# endif
3624 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003625# endif
3626 parmp->serverName_arg != NULL))
3627 {
3628 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3629 vim_free(parmp->servername);
3630 TIME_MSG("register server name");
3631 }
3632 else
3633 serverDelayedStartName = parmp->servername;
3634# endif
3635
3636 /*
3637 * Execute command ourselves if we're here because the send failed (or
3638 * else we would have exited above).
3639 */
3640 if (parmp->serverStr != NULL)
3641 {
3642 char_u *p;
3643
3644 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3645 parmp->serverStr, &p));
3646 vim_free(p);
3647 }
3648}
3649
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003650 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003651cmdsrv_main(
3652 int *argc,
3653 char **argv,
3654 char_u *serverName_arg,
3655 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003656{
3657 char_u *res;
3658 int i;
3659 char_u *sname;
3660 int ret;
3661 int didone = FALSE;
3662 int exiterr = 0;
3663 char **newArgV = argv + 1;
3664 int newArgC = 1,
3665 Argc = *argc;
3666 int argtype;
3667#define ARGTYPE_OTHER 0
3668#define ARGTYPE_EDIT 1
3669#define ARGTYPE_EDIT_WAIT 2
3670#define ARGTYPE_SEND 3
3671 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003672 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003673# ifndef FEAT_X11
3674 HWND srv;
3675# else
3676 Window srv;
3677
3678 setup_term_clip();
3679# endif
3680
3681 sname = serverMakeName(serverName_arg, argv[0]);
3682 if (sname == NULL)
3683 return;
3684
3685 /*
3686 * Execute the command server related arguments and remove them
3687 * from the argc/argv array; We may have to return into main()
3688 */
3689 for (i = 1; i < Argc; i++)
3690 {
3691 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003692 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003693 {
3694 for (; i < *argc; i++)
3695 {
3696 *newArgV++ = argv[i];
3697 newArgC++;
3698 }
3699 break;
3700 }
3701
Bram Moolenaareb94e552006-03-11 21:35:11 +00003702 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003703 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003704 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3705 {
3706 char *p = argv[i] + 8;
3707
3708 argtype = ARGTYPE_EDIT;
3709 while (*p != NUL)
3710 {
3711 if (STRNICMP(p, "-wait", 5) == 0)
3712 {
3713 argtype = ARGTYPE_EDIT_WAIT;
3714 p += 5;
3715 }
3716 else if (STRNICMP(p, "-silent", 7) == 0)
3717 {
3718 silent = TRUE;
3719 p += 7;
3720 }
3721 else if (STRNICMP(p, "-tab", 4) == 0)
3722 {
3723 tabs = TRUE;
3724 p += 4;
3725 }
3726 else
3727 {
3728 argtype = ARGTYPE_OTHER;
3729 break;
3730 }
3731 }
3732 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003733 else
3734 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003735
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003736 if (argtype != ARGTYPE_OTHER)
3737 {
3738 if (i == *argc - 1)
3739 mainerr_arg_missing((char_u *)argv[i]);
3740 if (argtype == ARGTYPE_SEND)
3741 {
3742 *serverStr = (char_u *)argv[i + 1];
3743 i++;
3744 }
3745 else
3746 {
3747 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003748 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003749 if (*serverStr == NULL)
3750 {
3751 /* Probably out of memory, exit. */
3752 didone = TRUE;
3753 exiterr = 1;
3754 break;
3755 }
3756 Argc = i;
3757 }
3758# ifdef FEAT_X11
3759 if (xterm_dpy == NULL)
3760 {
3761 mch_errmsg(_("No display"));
3762 ret = -1;
3763 }
3764 else
3765 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3766 NULL, &srv, 0, 0, silent);
3767# else
3768 /* Win32 always works? */
3769 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3770# endif
3771 if (ret < 0)
3772 {
3773 if (argtype == ARGTYPE_SEND)
3774 {
3775 /* Failed to send, abort. */
3776 mch_errmsg(_(": Send failed.\n"));
3777 didone = TRUE;
3778 exiterr = 1;
3779 }
3780 else if (!silent)
3781 /* Let vim start normally. */
3782 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3783 break;
3784 }
3785
3786# ifdef FEAT_GUI_W32
3787 /* Guess that when the server name starts with "g" it's a GUI
3788 * server, which we can bring to the foreground here.
3789 * Foreground() in the server doesn't work very well. */
3790 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3791 SetForegroundWindow(srv);
3792# endif
3793
3794 /*
3795 * For --remote-wait: Wait until the server did edit each
3796 * file. Also detect that the server no longer runs.
3797 */
3798 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3799 {
3800 int numFiles = *argc - i - 1;
3801 int j;
3802 char_u *done = alloc(numFiles);
3803 char_u *p;
3804# ifdef FEAT_GUI_W32
3805 NOTIFYICONDATA ni;
3806 int count = 0;
3807 extern HWND message_window;
3808# endif
3809
3810 if (numFiles > 0 && argv[i + 1][0] == '+')
3811 /* Skip "+cmd" argument, don't wait for it to be edited. */
3812 --numFiles;
3813
3814# ifdef FEAT_GUI_W32
3815 ni.cbSize = sizeof(ni);
3816 ni.hWnd = message_window;
3817 ni.uID = 0;
3818 ni.uFlags = NIF_ICON|NIF_TIP;
3819 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3820 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3821 Shell_NotifyIcon(NIM_ADD, &ni);
3822# endif
3823
3824 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003825 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003826 while (memchr(done, 0, numFiles) != NULL)
3827 {
3828# ifdef WIN32
3829 p = serverGetReply(srv, NULL, TRUE, TRUE);
3830 if (p == NULL)
3831 break;
3832# else
3833 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3834 break;
3835# endif
3836 j = atoi((char *)p);
3837 if (j >= 0 && j < numFiles)
3838 {
3839# ifdef FEAT_GUI_W32
3840 ++count;
3841 sprintf(ni.szTip, _("%d of %d edited"),
3842 count, numFiles);
3843 Shell_NotifyIcon(NIM_MODIFY, &ni);
3844# endif
3845 done[j] = 1;
3846 }
3847 }
3848# ifdef FEAT_GUI_W32
3849 Shell_NotifyIcon(NIM_DELETE, &ni);
3850# endif
3851 }
3852 }
3853 else if (STRICMP(argv[i], "--remote-expr") == 0)
3854 {
3855 if (i == *argc - 1)
3856 mainerr_arg_missing((char_u *)argv[i]);
3857# ifdef WIN32
3858 /* Win32 always works? */
3859 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3860 &res, NULL, 1, FALSE) < 0)
3861# else
3862 if (xterm_dpy == NULL)
3863 mch_errmsg(_("No display: Send expression failed.\n"));
3864 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3865 &res, NULL, 1, 1, FALSE) < 0)
3866# endif
3867 {
3868 if (res != NULL && *res != NUL)
3869 {
3870 /* Output error from remote */
3871 mch_errmsg((char *)res);
3872 vim_free(res);
3873 res = NULL;
3874 }
3875 mch_errmsg(_(": Send expression failed.\n"));
3876 }
3877 }
3878 else if (STRICMP(argv[i], "--serverlist") == 0)
3879 {
3880# ifdef WIN32
3881 /* Win32 always works? */
3882 res = serverGetVimNames();
3883# else
3884 if (xterm_dpy != NULL)
3885 res = serverGetVimNames(xterm_dpy);
3886# endif
3887 if (called_emsg)
3888 mch_errmsg("\n");
3889 }
3890 else if (STRICMP(argv[i], "--servername") == 0)
3891 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003892 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003893 i++;
3894 continue;
3895 }
3896 else
3897 {
3898 *newArgV++ = argv[i];
3899 newArgC++;
3900 continue;
3901 }
3902 didone = TRUE;
3903 if (res != NULL && *res != NUL)
3904 {
3905 mch_msg((char *)res);
3906 if (res[STRLEN(res) - 1] != '\n')
3907 mch_msg("\n");
3908 }
3909 vim_free(res);
3910 }
3911
3912 if (didone)
3913 {
3914 display_errors(); /* display any collected messages */
3915 exit(exiterr); /* Mission accomplished - get out */
3916 }
3917
3918 /* Return back into main() */
3919 *argc = newArgC;
3920 vim_free(sname);
3921}
3922
3923/*
3924 * Build a ":drop" command to send to a Vim server.
3925 */
3926 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003927build_drop_cmd(
3928 int filec,
3929 char **filev,
3930 int tabs, /* Use ":tab drop" instead of ":drop". */
3931 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003932{
3933 garray_T ga;
3934 int i;
3935 char_u *inicmd = NULL;
3936 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003937 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003938 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003939
3940 if (filec > 0 && filev[0][0] == '+')
3941 {
3942 inicmd = (char_u *)filev[0] + 1;
3943 filev++;
3944 filec--;
3945 }
3946 /* Check if we have at least one argument. */
3947 if (filec <= 0)
3948 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003949
3950 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003951 cwd = alloc(MAXPATHL);
3952 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003953 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003954 if (mch_dirname(cwd, MAXPATHL) != OK)
3955 {
3956 vim_free(cwd);
3957 return NULL;
3958 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003959 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003960#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003961 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003962#else
3963 PATH_ESC_CHARS,
3964#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003965 '\\', TRUE);
3966 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003967 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003968 return NULL;
3969 ga_init2(&ga, 1, 100);
3970 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003971 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003972
3973 /* Call inputsave() so that a prompt for an encryption key works. */
3974 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3975 if (tabs)
3976 ga_concat(&ga, (char_u *)"tab ");
3977 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003978 for (i = 0; i < filec; i++)
3979 {
3980 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003981 * do it again in the Vim server. On MS-Windows only escape
3982 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003983 p = vim_strsave_escaped((char_u *)filev[i],
3984#ifdef UNIX
3985 PATH_ESC_CHARS
3986#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003987 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003988#endif
3989 );
3990 if (p == NULL)
3991 {
3992 vim_free(ga.ga_data);
3993 return NULL;
3994 }
3995 ga_concat(&ga, (char_u *)" ");
3996 ga_concat(&ga, p);
3997 vim_free(p);
3998 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003999 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4000
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004001 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4002 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004003 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4004
4005 /* Switch back to the correct current directory (prior to temporary path
4006 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004007 * correct after the :drop command. With line breaks and spaces:
4008 * if !exists('+acd') || !&acd
4009 * if haslocaldir()
4010 * cd -
4011 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004012 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004013 * cd -
4014 * endif
4015 * endif
4016 */
4017 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004018 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004019 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004020 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004021 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004022
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004023 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004024 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4025 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004026 if (inicmd != NULL)
4027 {
4028 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4029 * the following commands to be inserted as text. Use a "|",
4030 * hopefully "inicmd" does allow this... */
4031 ga_concat(&ga, inicmd);
4032 ga_concat(&ga, (char_u *)"|");
4033 }
4034 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4035 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004036 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004037 ga_append(&ga, NUL);
4038 return ga.ga_data;
4039}
4040
4041/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004042 * Make our basic server name: use the specified "arg" if given, otherwise use
4043 * the tail of the command "cmd" we were started with.
4044 * Return the name in allocated memory. This doesn't include a serial number.
4045 */
4046 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004047serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004048{
4049 char_u *p;
4050
4051 if (arg != NULL && *arg != NUL)
4052 p = vim_strsave_up(arg);
4053 else
4054 {
4055 p = vim_strsave_up(gettail((char_u *)cmd));
4056 /* Remove .exe or .bat from the name. */
4057 if (p != NULL && vim_strchr(p, '.') != NULL)
4058 *vim_strchr(p, '.') = NUL;
4059 }
4060 return p;
4061}
4062#endif /* FEAT_CLIENTSERVER */
4063
4064#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4065/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004066 * Replace termcodes such as <CR> and insert as key presses if there is room.
4067 */
4068 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004069server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004070{
4071 char_u *ptr = NULL;
4072 char_u *cpo_save = p_cpo;
4073
4074 /* Set 'cpoptions' the way we want it.
4075 * B set - backslashes are *not* treated specially
4076 * k set - keycodes are *not* reverse-engineered
4077 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004078 * The last but one parameter of replace_termcodes() is TRUE so that the
4079 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004080 */
4081 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004082 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004083 p_cpo = cpo_save;
4084
4085 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4086 {
4087 /*
4088 * Add the string to the input stream.
4089 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4090 *
4091 * First clear typed characters from the typeahead buffer, there could
4092 * be half a mapping there. Then append to the existing string, so
4093 * that multiple commands from a client are concatenated.
4094 */
4095 if (typebuf.tb_maplen < typebuf.tb_len)
4096 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4097 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4098
4099 /* Let input_available() know we inserted text in the typeahead
4100 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004101 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004102 }
4103 vim_free((char_u *)ptr);
4104}
4105
4106/*
4107 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004108 */
4109 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004110eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004111{
4112 char_u *res;
4113 int save_dbl = debug_break_level;
4114 int save_ro = redir_off;
4115
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004116 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4117 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004118 debug_break_level = -1;
4119 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004120 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4121 * to be typed. Do generate errors so that try/catch works. */
4122 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004123
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004124 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004125
4126 debug_break_level = save_dbl;
4127 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004128 --emsg_silent;
4129 if (emsg_silent < 0)
4130 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004131
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004132 /* A client can tell us to redraw, but not to display the cursor, so do
4133 * that here. */
4134 setcursor();
4135 out_flush();
4136#ifdef FEAT_GUI
4137 if (gui.in_use)
4138 gui_update_cursor(FALSE, FALSE);
4139#endif
4140
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004141 return res;
4142}
4143
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004144/*
4145 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4146 * return an allocated string. Otherwise return "data".
4147 * "*tofree" is set to the result when it needs to be freed later.
4148 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004149 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004150serverConvert(
4151 char_u *client_enc UNUSED,
4152 char_u *data,
4153 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004154{
4155 char_u *res = data;
4156
4157 *tofree = NULL;
4158# ifdef FEAT_MBYTE
4159 if (client_enc != NULL && p_enc != NULL)
4160 {
4161 vimconv_T vimconv;
4162
4163 vimconv.vc_type = CONV_NONE;
4164 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4165 && vimconv.vc_type != CONV_NONE)
4166 {
4167 res = string_convert(&vimconv, data, NULL);
4168 if (res == NULL)
4169 res = data;
4170 else
4171 *tofree = res;
4172 }
4173 convert_setup(&vimconv, NULL, NULL);
4174 }
4175# endif
4176 return res;
4177}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004178#endif