blob: 52370dc4193a9655583b33eec1bdb53939a589ba [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
14# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
16# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
17 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000018# endif
19# include <limits.h>
20#endif
21
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020022#if defined(WIN3264) && !defined(FEAT_GUI_W32)
23# include "iscygpty.h"
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Values for edit_type. */
27#define EDIT_NONE 0 /* no edit type yet */
28#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
29#define EDIT_STDIN 2 /* read file from stdin */
30#define EDIT_TAG 3 /* tag name argument given, use tagname */
31#define EDIT_QF 4 /* start in quickfix mode */
32
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010033#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000035#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020037# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
38static void init_locale(void);
39# endif
40static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010041#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void main_msg(char *s);
43static void usage(void);
44static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void command_line_scan(mparm_T *parmp);
47static void check_tty(mparm_T *parmp);
48static void read_stdin(void);
49static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010050# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010051static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010052# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010053static void exe_pre_commands(mparm_T *parmp);
54static void exe_commands(mparm_T *parmp);
55static void source_startup_scripts(mparm_T *parmp);
56static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010057# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010058static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010059# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +010060# ifdef FEAT_EVAL
61static void set_progpath(char_u *argv0);
62# endif
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010063# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void exec_on_server(mparm_T *parmp);
65static void prepare_server(mparm_T *parmp);
66static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
67static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010068# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000069#endif
70
71
Bram Moolenaarb4210b32004-06-13 14:51:16 +000072/*
73 * Different types of error messages.
74 */
75static char *(main_errors[]) =
76{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000077 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000078#define ME_UNKNOWN_OPTION 0
79 N_("Too many edit arguments"),
80#define ME_TOO_MANY_ARGS 1
81 N_("Argument missing after"),
82#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000083 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000084#define ME_GARBAGE 3
85 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
86#define ME_EXTRA_CMD 4
87 N_("Invalid argument for"),
88#define ME_INVALID_ARG 5
89};
90
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010091#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +010092#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020093
94static char_u *start_dir = NULL; /* current working dir on startup */
95
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020096static int has_dash_c_arg = FALSE;
97
Bram Moolenaara8e691d2016-08-07 15:19:26 +020098/* Various parameters passed between main() and other functions. */
99static mparm_T params;
100
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000101 int
102# ifdef VIMDLL
103_export
104# endif
105# ifdef FEAT_GUI_MSWIN
106# ifdef __BORLANDC__
107_cdecl
108# endif
109VimMain
110# else
111main
112# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100113(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000114{
Bram Moolenaar3f269672009-11-03 11:11:11 +0000115#ifdef STARTUPTIME
116 int i;
117#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000119 /*
120 * Do any system-specific initialisations. These can NOT use IObuff or
121 * NameBuff. Thus emsg2() cannot be called!
122 */
123 mch_early_init();
124
Bram Moolenaar14993322014-09-09 12:25:33 +0200125#if defined(WIN32) && defined(FEAT_MBYTE)
126 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200127 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200128 * convert when 'encoding' changes. Get the unexpanded arguments.
129 */
130 argc = get_cmd_argsW(&argv);
131#endif
132
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000133 /* Many variables are in "params" so that we can pass them to invoked
134 * functions without a lot of arguments. "argc" and "argv" are also
135 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000136 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000137 params.argc = argc;
138 params.argv = argv;
139 params.want_full_screen = TRUE;
140#ifdef FEAT_EVAL
141 params.use_debug_break_level = -1;
142#endif
143#ifdef FEAT_WINDOWS
144 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000145#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000146
Bram Moolenaar99685e62013-05-11 13:56:18 +0200147#ifdef FEAT_RUBY
148 {
149 int ruby_stack_start;
150 vim_ruby_init((void *)&ruby_stack_start);
151 }
152#endif
153
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000154#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000155 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000156#endif
157
158#ifdef MEM_PROFILE
159 atexit(vim_mem_profile_dump);
160#endif
161
162#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200163 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000164 for (i = 1; i < argc; ++i)
165 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000166 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000167 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000168 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000169 TIME_MSG("--- VIM STARTING ---");
170 break;
171 }
172 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000173#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000174 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200176 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000177
178#ifdef FEAT_CLIENTSERVER
179 /*
180 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000181 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000182 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000183 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000184#endif
185
186 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000187 * Figure out the way to work from the command name argv[0].
188 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000189 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000190 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000191
192 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193 * Process the command line arguments. File names are put in the global
194 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000196 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000197 TIME_MSG("parsing arguments");
198
199 /*
200 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000201 * there is no terminal version, and on Windows we can't fork one off with
202 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000203 */
204#ifdef ALWAYS_USE_GUI
205 gui.starting = TRUE;
206#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000207# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000208 /*
209 * Check if the GUI can be started. Reset gui.starting if not.
210 * Don't know about other systems, stay on the safe side and don't check.
211 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000212 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000214 if (gui_init_check() == FAIL)
215 {
216 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000217
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000218 /* When running "evim" or "gvim -y" we need the menus, exit if we
219 * don't have them. */
220 if (params.evim_mode)
221 mch_exit(1);
222 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000223 }
224# endif
225#endif
226
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227 if (GARGCOUNT > 0)
228 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100229#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230 /*
231 * Expand wildcards in file names.
232 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000233 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200235 start_dir = alloc(MAXPATHL);
236 if (start_dir != NULL)
237 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000238 /* Temporarily add '(' and ')' to 'isfname'. These are valid
239 * filename characters but are excluded from 'isfname' to make
240 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
241 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000242 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200244 if (start_dir != NULL)
245 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 }
247#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200248 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000249 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000250
251#if defined(WIN32) && defined(FEAT_MBYTE)
252 {
253 extern void set_alist_count(void);
254
255 /* Remember the number of entries in the argument list. If it changes
256 * we don't react on setting 'encoding'. */
257 set_alist_count();
258 }
259#endif
260
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000261#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000262 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000263 {
264 /*
265 * If there is one filename, fully qualified, we have very probably
266 * been invoked from explorer, so change to the file's directory.
267 * Hint: to avoid this when typing a command use a forward slash.
268 * If the cd fails, it doesn't matter.
269 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200270 (void)vim_chdirfile(params.fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200271 if (start_dir != NULL)
272 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000273 }
274#endif
275 TIME_MSG("expanding arguments");
276
277#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000278 if (params.diff_mode && params.window_count == -1)
279 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280#endif
281
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000282 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283 ++RedrawingDisabled;
284
285 /*
286 * When listing swap file names, don't do cursor positioning et. al.
287 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200288 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000289 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000290
291 /*
292 * When certain to start the GUI, don't check capabilities of terminal.
293 * For GTK we can't be sure, but when started from the desktop it doesn't
294 * make sense to try using a terminal.
295 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000296#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000297 if (gui.starting
298# ifdef FEAT_GUI_GTK
299 && !isatty(2)
300# endif
301 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000302 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000303#endif
304
305#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
306 /* When the GUI is started from Finder, need to display messages in a
307 * message box. isatty(2) returns TRUE anyway, thus we need to check the
308 * name to know we're not started from a terminal. */
309 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000310 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000311 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000312
313 /* Avoid always using "/" as the current directory. Note that when
314 * started from Finder the arglist will be filled later in
315 * HandleODocAE() and "fname" will be NULL. */
316 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
317 && STRCMP(NameBuff, "/") == 0)
318 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200319 if (params.fname != NULL)
320 (void)vim_chdirfile(params.fname);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000321 else
322 {
323 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
324 vim_chdir(NameBuff);
325 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200326 if (start_dir != NULL)
327 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000328 }
329 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000330#endif
331
332 /*
333 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100334 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000335 * Note that we may use mch_exit() before mch_init()!
336 */
337 mch_init();
338 TIME_MSG("shell init");
339
340#ifdef USE_XSMP
341 /*
342 * For want of anywhere else to do it, try to connect to xsmp here.
343 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
344 * Hijacking -X 'no X connection' to also disable XSMP connection as that
345 * has a similar delay upon failure.
346 * Only try if SESSION_MANAGER is set to something non-null.
347 */
348 if (!x_no_connect)
349 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000350 char *p = getenv("SESSION_MANAGER");
351
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000352 if (p != NULL && *p != NUL)
353 {
354 xsmp_init();
355 TIME_MSG("xsmp init");
356 }
357 }
358#endif
359
360 /*
361 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000362 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000363 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000365 /* This message comes before term inits, but after setting "silent_mode"
366 * when the input is not a tty. */
367 if (GARGCOUNT > 1 && !silent_mode)
368 printf(_("%d files to edit\n"), GARGCOUNT);
369
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000370 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000372 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000373 capabilities (will set full_screen) */
374 screen_start(); /* don't know where cursor is now */
375 TIME_MSG("Termcap init");
376 }
377
378 /*
379 * Set the default values for the options that use Rows and Columns.
380 */
381 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000382 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383#ifdef FEAT_DIFF
384 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
385 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000386 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000387 diff_win_options(firstwin, FALSE);
388#endif
389
390 cmdline_row = Rows - p_ch;
391 msg_row = cmdline_row;
392 screenalloc(FALSE); /* allocate screen buffers */
393 set_init_2();
394 TIME_MSG("inits 2");
395
396 msg_scroll = TRUE;
397 no_wait_return = TRUE;
398
399 init_mappings(); /* set up initial mappings */
400
401 init_highlight(TRUE, FALSE); /* set the default highlight groups */
402 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000403
404#ifdef FEAT_EVAL
405 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000406 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407#endif
408
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100409#ifdef FEAT_MZSCHEME
410 /*
411 * Newer version of MzScheme (Racket) require earlier (trampolined)
412 * initialisation via scheme_main_setup.
413 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200414 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100415 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200416 return mzscheme_main();
417#else
418 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100419#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200420}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100421#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200422#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100423
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200424/*
425 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
426 * things simple.
427 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
428 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100429 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200430vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100431{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100432#ifndef NO_VIM_MAIN
Bram Moolenaar66459b72016-08-06 19:01:55 +0200433 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
434 * Allows for setting 'loadplugins' there. */
435 if (params.use_vimrc != NULL && STRCMP(params.use_vimrc, "NONE") == 0)
436 p_lpl = FALSE;
437
Bram Moolenaar58d98232005-07-23 22:25:46 +0000438 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000439 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000440
Bram Moolenaar58d98232005-07-23 22:25:46 +0000441 /* Source startup scripts. */
442 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000443
444#ifdef FEAT_EVAL
445 /*
446 * Read all the plugin files.
447 * Only when compiled with +eval, since most plugins need it.
448 */
449 if (p_lpl)
450 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000451# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar66459b72016-08-06 19:01:55 +0200452 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000453# else
Bram Moolenaar66459b72016-08-06 19:01:55 +0200454 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000455# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000456 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100457
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100458 ex_packloadall(NULL);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100459 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200460
461# ifdef VMS /* Somehow VMS doesn't handle the "**". */
462 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
463# else
464 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
465# endif
466 TIME_MSG("loading after plugins");
467
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000468 }
469#endif
470
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000471#ifdef FEAT_DIFF
472 /* Decide about window layout for diff mode after reading vimrc. */
473 if (params.diff_mode && params.window_layout == 0)
474 {
475 if (diffopt_horizontal())
476 params.window_layout = WIN_HOR; /* use horizontal split */
477 else
478 params.window_layout = WIN_VER; /* use vertical split */
479 }
480#endif
481
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000482 /*
483 * Recovery mode without a file name: List swap files.
484 * This uses the 'dir' option, therefore it must be after the
485 * initializations.
486 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200487 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000488 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200489 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000490 mch_exit(0);
491 }
492
493 /*
494 * Set a few option defaults after reading .vimrc files:
495 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
496 */
497 set_init_3();
498 TIME_MSG("inits 3");
499
500 /*
501 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
502 * Note that this overrides anything from a vimrc file.
503 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000504 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000505 p_uc = 0;
506
507#ifdef FEAT_FKMAP
508 if (curwin->w_p_rl && p_altkeymap)
509 {
510 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
511# ifdef FEAT_ARABIC
512 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
513# endif
514 p_fkmap = TRUE; /* Set the Farsi keymap mode */
515 }
516#endif
517
518#ifdef FEAT_GUI
519 if (gui.starting)
520 {
521#if defined(UNIX) || defined(VMS)
522 /* When something caused a message from a vimrc script, need to output
523 * an extra newline before the shell prompt. */
524 if (did_emsg || msg_didout)
525 putchar('\n');
526#endif
527
528 gui_start(); /* will set full_screen to TRUE */
529 TIME_MSG("starting GUI");
530
531 /* When running "evim" or "gvim -y" we need the menus, exit if we
532 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000533 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000534 mch_exit(1);
535 }
536#endif
537
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538#ifdef FEAT_VIMINFO
539 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000540 * Read in registers, history etc, but not marks, from the viminfo file.
541 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 */
543 if (*p_viminfo != NUL)
544 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000545 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546 TIME_MSG("reading viminfo");
547 }
548#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100549#ifdef FEAT_EVAL
550 /* It's better to make v:oldfiles an empty list than NULL. */
551 if (get_vim_var_list(VV_OLDFILES) == NULL)
552 set_vim_var_list(VV_OLDFILES, list_alloc());
553#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000554
555#ifdef FEAT_QUICKFIX
556 /*
557 * "-q errorfile": Load the error file now.
558 * If the error file can't be read, exit before doing anything else.
559 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000560 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100562 char_u *enc = NULL;
563
564# ifdef FEAT_MBYTE
565 enc = p_menc;
566# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000567 if (params.use_ef != NULL)
568 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000569 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200570 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100571 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000572 {
573 out_char('\n');
574 mch_exit(3);
575 }
576 TIME_MSG("reading errorfile");
577 }
578#endif
579
580 /*
581 * Start putting things on the screen.
582 * Scroll screen down before drawing over it
583 * Clear screen now, so file message will not be cleared.
584 */
585 starting = NO_BUFFERS;
586 no_wait_return = FALSE;
587 if (!exmode_active)
588 msg_scroll = FALSE;
589
590#ifdef FEAT_GUI
591 /*
592 * This seems to be required to make callbacks to be called now, instead
593 * of after things have been put on the screen, which then may be deleted
594 * when getting a resize callback.
595 * For the Mac this handles putting files dropped on the Vim icon to
596 * global_alist.
597 */
598 if (gui.in_use)
599 {
600# ifdef FEAT_SUN_WORKSHOP
601 if (!usingSunWorkShop)
602# endif
603 gui_wait_for_chars(50L);
604 TIME_MSG("GUI delay");
605 }
606#endif
607
608#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
609 qnx_clip_init();
610#endif
611
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200612#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
613 clip_init(TRUE);
614#endif
615
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000616#ifdef FEAT_XCLIPBOARD
617 /* Start using the X clipboard, unless the GUI was started. */
618# ifdef FEAT_GUI
619 if (!gui.in_use)
620# endif
621 {
622 setup_term_clip();
623 TIME_MSG("setup clipboard");
624 }
625#endif
626
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000627#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000628 /* Prepare for being a Vim server. */
629 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000630#endif
631
632 /*
633 * If "-" argument given: Read file from stdin.
634 * Do this before starting Raw mode, because it may change things that the
635 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
636 * are the same terminal: "cat | vim -".
637 * Using autocommands here may cause trouble...
638 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000639 if (params.edit_type == EDIT_STDIN && !recoverymode)
640 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000641
642#if defined(UNIX) || defined(VMS)
643 /* When switching screens and something caused a message from a vimrc
644 * script, need to output an extra newline on exit. */
645 if ((did_emsg || msg_didout) && *T_TI != NUL)
646 newline_on_exit = TRUE;
647#endif
648
649 /*
650 * When done something that is not allowed or error message call
651 * wait_return. This must be done before starttermcap(), because it may
652 * switch to another screen. It must be done after settmode(TMODE_RAW),
653 * because we want to react on a single key stroke.
654 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000655 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000656 */
657 settmode(TMODE_RAW);
658 TIME_MSG("setting raw mode");
659
660 if (need_wait_return || msg_didany)
661 {
662 wait_return(TRUE);
663 TIME_MSG("waiting for return");
664 }
665
666 starttermcap(); /* start termcap if not done by wait_return() */
667 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200668#if defined(FEAT_TERMRESPONSE)
669# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200670 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200671# endif
672 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100673#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000674
675#ifdef FEAT_MOUSE
676 setmouse(); /* may start using the mouse */
677#endif
678 if (scroll_region)
679 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000680 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000681
682 /*
683 * Don't clear the screen when starting in Ex mode, unless using the GUI.
684 */
685 if (exmode_active
686#ifdef FEAT_GUI
687 && !gui.in_use
688#endif
689 )
690 must_redraw = CLEAR;
691 else
692 {
693 screenclear(); /* clear screen */
694 TIME_MSG("clearing screen");
695 }
696
697#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000698 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000699 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100700 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200701 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702 TIME_MSG("getting crypt key");
703 }
704#endif
705
706 no_wait_return = TRUE;
707
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000709 * Create the requested number of windows and edit buffers in them.
710 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000712 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000713 TIME_MSG("opening buffers");
714
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000715#ifdef FEAT_EVAL
716 /* clear v:swapcommand */
717 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
718#endif
719
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000720 /* Ex starts at last line of the file */
721 if (exmode_active)
722 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
723
724#ifdef FEAT_AUTOCMD
725 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
726 TIME_MSG("BufEnter autocommands");
727#endif
728 setpcmark();
729
730#ifdef FEAT_QUICKFIX
731 /*
732 * When started with "-q errorfile" jump to first error now.
733 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000734 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000735 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000736 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000737 TIME_MSG("jump to first error");
738 }
739#endif
740
741#ifdef FEAT_WINDOWS
742 /*
743 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000744 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000745 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200746 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000747#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200748 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749
750#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000751 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000752 {
753 win_T *wp;
754
755 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200756 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000757 diff_win_options(wp, TRUE);
758 }
759#endif
760
761 /*
762 * Shorten any of the filenames, but only when absolute.
763 */
764 shorten_fnames(FALSE);
765
766 /*
767 * Need to jump to the tag before executing the '-c command'.
768 * Makes "vim -c '/return' -t main" work.
769 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000770 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000771 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000772#if defined(HAS_SWAP_EXISTS_ACTION)
773 swap_exists_did_quit = FALSE;
774#endif
775
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000776 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000777 do_cmdline_cmd(IObuff);
778 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000779
780#if defined(HAS_SWAP_EXISTS_ACTION)
781 /* If the user doesn't want to edit the file then we quit here. */
782 if (swap_exists_did_quit)
783 getout(1);
784#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000785 }
786
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000787 /* Execute any "+", "-c" and "-S" arguments. */
788 if (params.n_commands > 0)
789 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000790
791 RedrawingDisabled = 0;
792 redraw_all_later(NOT_VALID);
793 no_wait_return = FALSE;
794 starting = 0;
795
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200796 /* 'autochdir' has been postponed */
797 DO_AUTOCHDIR
798
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000799#ifdef FEAT_TERMRESPONSE
800 /* Requesting the termresponse is postponed until here, so that a "-c q"
801 * argument doesn't make it appear in the shell Vim was started from. */
802 may_req_termresponse();
803#endif
804
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000805 /* start in insert mode */
806 if (p_im)
807 need_start_insertmode = TRUE;
808
Bram Moolenaar14735512016-03-26 21:00:08 +0100809#ifdef FEAT_EVAL
810 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
811#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000812#ifdef FEAT_AUTOCMD
813 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
814 TIME_MSG("VimEnter autocommands");
815#endif
816
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200817#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
818 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
819 * done after the clipboard is available and all initial commands that may
820 * modify the 'clipboard' setting have run; i.e. just before entering the
821 * main loop. */
822 {
823 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100824
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200825 adjust_clip_reg(&default_regname);
826 set_reg_var(default_regname);
827 }
828#endif
829
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000830#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
831 /* When a startup script or session file setup for diff'ing and
832 * scrollbind, sync the scrollbind now. */
833 if (curwin->w_p_diff && curwin->w_p_scb)
834 {
835 update_topline();
836 check_scrollbind((linenr_T)0, 0L);
837 TIME_MSG("diff scrollbinding");
838 }
839#endif
840
841#if defined(WIN3264) && !defined(FEAT_GUI_W32)
842 mch_set_winsize_now(); /* Allow winsize changes from now on */
843#endif
844
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000845#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
846 /* When tab pages were created, may need to update the tab pages line and
847 * scrollbars. This is skipped while creating them. */
848 if (first_tabpage->tp_next != NULL)
849 {
850 out_flush();
851 gui_init_which_components(NULL);
852 gui_update_scrollbars(TRUE);
853 }
854 need_mouse_correct = TRUE;
855#endif
856
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000857 /* If ":startinsert" command used, stuff a dummy command to be able to
858 * call normal_cmd(), which will then start Insert mode. */
859 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000860 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000861
862#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200863 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200864 {
865# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200866# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200867 && !defined(FEAT_GUI_W32)
868 if (gui.in_use)
869 {
870 mch_errmsg(_("netbeans is not supported with this GUI\n"));
871 mch_exit(2);
872 }
873# endif
874# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200876 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200877 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878#endif
879
880 TIME_MSG("before starting main loop");
881
882 /*
883 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100884 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000885 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200887#endif /* NO_VIM_MAIN */
888
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889 return 0;
890}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891
892/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200893 * Initialisation shared by main() and some tests.
894 */
895 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200896common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200897{
898
899#ifdef FEAT_MBYTE
900 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
901#endif
902#ifdef FEAT_EVAL
903 eval_init(); /* init global variables */
904#endif
905
906#ifdef __QNXNTO__
907 qnx_init(); /* PhAttach() for clipboard, (and gui) */
908#endif
909
910#ifdef MAC_OS_CLASSIC
911 /* Prepare for possibly starting GUI sometime */
912 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200913 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200914 TIME_MSG("GUI prepared");
915#endif
916
917 /* Init the table of Normal mode commands. */
918 init_normal_cmds();
919
920#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
921 make_version(); /* Construct the long version string. */
922#endif
923
924 /*
925 * Allocate space for the generic buffers (needed for set_init_1() and
926 * EMSG2()).
927 */
928 if ((IObuff = alloc(IOSIZE)) == NULL
929 || (NameBuff = alloc(MAXPATHL)) == NULL)
930 mch_exit(0);
931 TIME_MSG("Allocated generic buffers");
932
933#ifdef NBDEBUG
934 /* Wait a moment for debugging NetBeans. Must be after allocating
935 * NameBuff. */
936 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
937 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
938 TIME_MSG("NetBeans debug wait");
939#endif
940
941#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
942 /*
943 * Setup to use the current locale (for ctype() and many other things).
944 * NOTE: Translated messages with encodings other than latin1 will not
945 * work until set_init_1() has been called!
946 */
947 init_locale();
948 TIME_MSG("locale set");
949#endif
950
951#ifdef FEAT_GUI
952 gui.dofork = TRUE; /* default is to use fork() */
953#endif
954
955 /*
956 * Do a first scan of the arguments in "argv[]":
957 * -display or --display
958 * --server...
959 * --socketid
960 * --windowid
961 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200962 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200963
964#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200965 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200966#endif
967#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
968 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200969 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200970 TIME_MSG("GUI prepared");
971#endif
972
973#ifdef FEAT_CLIPBOARD
974 clip_init(FALSE); /* Initialise clipboard stuff */
975 TIME_MSG("clipboard setup");
976#endif
977
978 /*
979 * Check if we have an interactive window.
980 * On the Amiga: If there is no window, we open one with a newcli command
981 * (needed for :! to * work). mch_check_win() will also handle the -d or
982 * -dev argument.
983 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100984 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200985 TIME_MSG("window checked");
986
987 /*
988 * Allocate the first window and buffer.
989 * Can't do anything without it, exit when it fails.
990 */
991 if (win_alloc_first() == FAIL)
992 mch_exit(0);
993
994 init_yank(); /* init yank buffers */
995
996 alist_init(&global_alist); /* Init the argument list to empty. */
997 global_alist.id = 0;
998
999 /*
1000 * Set the default values for the options.
1001 * NOTE: Non-latin1 translated messages are working only after this,
1002 * because this is where "has_mbyte" will be set, which is used by
1003 * msg_outtrans_len_attr().
1004 * First find out the home directory, needed to expand "~" in options.
1005 */
1006 init_homedir(); /* find real value of $HOME */
1007 set_init_1();
1008 TIME_MSG("inits 1");
1009
1010#ifdef FEAT_EVAL
1011 set_lang_var(); /* set v:lang and v:ctype */
1012#endif
1013}
1014
1015/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001016 * Main loop: Execute Normal mode commands until exiting Vim.
1017 * Also used to handle commands in the command-line window, until the window
1018 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019 * Also used to handle ":visual" command after ":global": execute Normal mode
1020 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001021 */
1022 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001023main_loop(
1024 int cmdwin, /* TRUE when working in the command-line window */
1025 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001027 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001028 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001029#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001030 /* these are static to avoid a compiler warning */
1031 static linenr_T conceal_old_cursor_line = 0;
1032 static linenr_T conceal_new_cursor_line = 0;
1033 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001034#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001035
1036#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1037 /* Setup to catch a terminating error from the X server. Just ignore
1038 * it, restore the state and continue. This might not always work
1039 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001040 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001042 {
1043 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001044 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001045 got_int = TRUE;
1046 need_wait_return = FALSE;
1047 global_busy = FALSE;
1048 exmode_active = 0;
1049 skip_redraw = FALSE;
1050 RedrawingDisabled = 0;
1051 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001052 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001053# ifdef FEAT_EVAL
1054 emsg_skip = 0;
1055# endif
1056 emsg_off = 0;
1057# ifdef FEAT_MOUSE
1058 setmouse();
1059# endif
1060 settmode(TMODE_RAW);
1061 starttermcap();
1062 scroll_start();
1063 redraw_later_clear();
1064 }
1065#endif
1066
1067 clear_oparg(&oa);
1068 while (!cmdwin
1069#ifdef FEAT_CMDWIN
1070 || cmdwin_result == 0
1071#endif
1072 )
1073 {
1074 if (stuff_empty())
1075 {
1076 did_check_timestamps = FALSE;
1077 if (need_check_timestamps)
1078 check_timestamps(FALSE);
1079 if (need_wait_return) /* if wait_return still needed ... */
1080 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001081 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001082 {
1083 need_start_insertmode = FALSE;
1084 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1085 /* skip the fileinfo message now, because it would be shown
1086 * after insert mode finishes! */
1087 need_fileinfo = FALSE;
1088 }
1089 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001090
1091 /* Reset "got_int" now that we got back to the main loop. Except when
1092 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1093 * the ":g" command.
1094 * For ":g/pat/vi" we reset "got_int" when used once. When used
1095 * a second time we go back to Ex mode and abort the ":g" command. */
1096 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001097 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001098 if (noexmode && global_busy && !exmode_active && previous_got_int)
1099 {
1100 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1101 * used and keep "got_int" set, so that it aborts ":g". */
1102 exmode_active = EXMODE_NORMAL;
1103 State = NORMAL;
1104 }
1105 else if (!global_busy || !exmode_active)
1106 {
1107 if (!quit_more)
1108 (void)vgetc(); /* flush all buffers */
1109 got_int = FALSE;
1110 }
1111 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001112 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001113 else
1114 previous_got_int = FALSE;
1115
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001116 if (!exmode_active)
1117 msg_scroll = FALSE;
1118 quit_more = FALSE;
1119
1120 /*
1121 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1122 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1123 * update cursor and redraw.
1124 */
1125 if (skip_redraw || exmode_active)
1126 skip_redraw = FALSE;
1127 else if (do_redraw || stuff_empty())
1128 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001129# ifdef FEAT_GUI
1130 /* If ui_breakcheck() was used a resize may have been postponed. */
1131 gui_may_resize_shell();
1132# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001133#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001134 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001135 if (!finish_op && (
1136# ifdef FEAT_AUTOCMD
1137 has_cursormoved()
1138# endif
1139# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1140 ||
1141# endif
1142# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001143 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001144# endif
1145 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001146# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001147 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001148# endif
1149 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001150 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001151# ifdef FEAT_AUTOCMD
1152 if (has_cursormoved())
1153 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1154 FALSE, curbuf);
1155# endif
1156# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001157 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001158 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001159# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001160 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001161# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001162 conceal_new_cursor_line = curwin->w_cursor.lnum;
1163 conceal_update_lines = TRUE;
1164 }
1165# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001166# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001167 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001168# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001169 }
1170#endif
1171
Bram Moolenaar186628f2013-03-19 13:33:23 +01001172#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001173 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001174 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001175 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001176 {
1177 if (last_changedtick_buf == curbuf)
1178 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1179 FALSE, curbuf);
1180 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001181 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001182 }
1183#endif
1184
Bram Moolenaar33aec762006-01-22 23:30:12 +00001185#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1186 /* Scroll-binding for diff mode may have been postponed until
1187 * here. Avoids doing it for every change. */
1188 if (diff_need_scrollbind)
1189 {
1190 check_scrollbind((linenr_T)0, 0L);
1191 diff_need_scrollbind = FALSE;
1192 }
1193#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001194#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001195 /* Include a closed fold completely in the Visual area. */
1196 foldAdjustVisual();
1197#endif
1198#ifdef FEAT_FOLDING
1199 /*
1200 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1201 * contain the cursor.
1202 * When 'foldopen' is "all", open the fold(s) under the cursor.
1203 * This may mark the window for redrawing.
1204 */
1205 if (hasAnyFolding(curwin) && !char_avail())
1206 {
1207 foldCheckClose();
1208 if (fdo_flags & FDO_ALL)
1209 foldOpenCursor();
1210 }
1211#endif
1212
1213 /*
1214 * Before redrawing, make sure w_topline is correct, and w_leftcol
1215 * if lines don't wrap, and w_skipcol if lines wrap.
1216 */
1217 update_topline();
1218 validate_cursor();
1219
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001220 if (VIsual_active)
1221 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001222 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001223 update_screen(0);
1224 else if (redraw_cmdline || clear_cmdline)
1225 showmode();
1226#ifdef FEAT_WINDOWS
1227 redraw_statuslines();
1228#endif
1229#ifdef FEAT_TITLE
1230 if (need_maketitle)
1231 maketitle();
1232#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001233#ifdef FEAT_VIMINFO
1234 curbuf->b_last_used = vim_time();
1235#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001236 /* display message after redraw */
1237 if (keep_msg != NULL)
1238 {
1239 char_u *p;
1240
1241 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001242 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1243 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001244 p = keep_msg;
1245 msg_attr(p, keep_msg_attr);
1246 vim_free(p);
1247 }
1248 if (need_fileinfo) /* show file info after redraw */
1249 {
1250 fileinfo(FALSE, TRUE, FALSE);
1251 need_fileinfo = FALSE;
1252 }
1253
1254 emsg_on_display = FALSE; /* can delete error message now */
1255 did_emsg = FALSE;
1256 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001257 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001258 showruler(FALSE);
1259
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001260# if defined(FEAT_CONCEAL)
1261 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001262 && (conceal_old_cursor_line != conceal_new_cursor_line
1263 || conceal_cursor_line(curwin)
1264 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001265 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001266 if (conceal_old_cursor_line != conceal_new_cursor_line
1267 && conceal_old_cursor_line
1268 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001269 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001270 update_single_line(curwin, conceal_new_cursor_line);
1271 curwin->w_valid &= ~VALID_CROW;
1272 }
1273# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001274 setcursor();
1275 cursor_on();
1276
1277 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001278
1279#ifdef STARTUPTIME
1280 /* Now that we have drawn the first screen all the startup stuff
1281 * has been done, close any file for startup messages. */
1282 if (time_fd != NULL)
1283 {
1284 TIME_MSG("first screen update");
1285 TIME_MSG("--- VIM STARTED ---");
1286 fclose(time_fd);
1287 time_fd = NULL;
1288 }
1289#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001290 }
1291#ifdef FEAT_GUI
1292 if (need_mouse_correct)
1293 gui_mouse_correct();
1294#endif
1295
1296 /*
1297 * Update w_curswant if w_set_curswant has been set.
1298 * Postponed until here to avoid computing w_virtcol too often.
1299 */
1300 update_curswant();
1301
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001302#ifdef FEAT_EVAL
1303 /*
1304 * May perform garbage collection when waiting for a character, but
1305 * only at the very toplevel. Otherwise we may be using a List or
1306 * Dict internally somewhere.
1307 * "may_garbage_collect" is reset in vgetc() which is invoked through
1308 * do_exmode() and normal_cmd().
1309 */
1310 may_garbage_collect = (!cmdwin && !noexmode);
1311#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001312 /*
1313 * If we're invoked as ex, do a round of ex commands.
1314 * Otherwise, get and execute a normal mode command.
1315 */
1316 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001317 {
1318 if (noexmode) /* End of ":global/path/visual" commands */
1319 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001320 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001321 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001322 else
1323 normal_cmd(&oa, TRUE);
1324 }
1325}
1326
1327
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001328#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001329/*
1330 * Exit, but leave behind swap files for modified buffers.
1331 */
1332 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001333getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001334{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001335# if defined(SIGHUP) && defined(SIG_IGN)
1336 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1337 * makes Vim exit and then handling SIGHUP causes various reentrance
1338 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001339 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001340# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001341
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 ml_close_notmod(); /* close all not-modified buffers */
1343 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1344 ml_close_all(FALSE); /* close all memfiles, without deleting */
1345 getout(exitval); /* exit Vim properly */
1346}
1347#endif
1348
1349
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001350/*
1351 * Exit properly.
1352 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001353 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001354getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001355{
1356#ifdef FEAT_AUTOCMD
1357 buf_T *buf;
1358 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001359 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001360#endif
1361
1362 exiting = TRUE;
1363
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001364 /* When running in Ex mode an error causes us to exit with a non-zero exit
1365 * code. POSIX requires this, although it's not 100% clear from the
1366 * standard. */
1367 if (exmode_active)
1368 exitval += ex_exitval;
1369
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370 /* Position the cursor on the last screen line, below all the text */
1371#ifdef FEAT_GUI
1372 if (!gui.in_use)
1373#endif
1374 windgoto((int)Rows - 1, 0);
1375
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001376#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1377 /* Optionally print hashtable efficiency. */
1378 hash_debug_results();
1379#endif
1380
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001381#ifdef FEAT_GUI
1382 msg_didany = FALSE;
1383#endif
1384
1385#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001386 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001387 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001388 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1389# if defined FEAT_WINDOWS
1390 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001391 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001392 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001393 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001394 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001395 if (wp->w_buffer == NULL)
1396 /* Autocmd must have close the buffer already, skip. */
1397 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001398 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001399 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001400 {
1401 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1402 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001403 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001404 /* start all over, autocommands may mess up the lists */
1405 next_tp = first_tabpage;
1406 break;
1407 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001408 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001410# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001411 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1412 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001413# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001414
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001415 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001416 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001417 if (buf->b_ml.ml_mfp != NULL)
1418 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001419 bufref_T bufref;
1420
1421 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001424 if (!bufref_valid(&bufref))
1425 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 break;
1427 }
1428 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1429 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001430#endif
1431
1432#ifdef FEAT_VIMINFO
1433 if (*p_viminfo != NUL)
1434 /* Write out the registers, history, marks etc, to the viminfo file */
1435 write_viminfo(NULL, FALSE);
1436#endif
1437
1438#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 if (get_vim_var_nr(VV_DYING) <= 1)
1440 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001441#endif
1442
Bram Moolenaar05159a02005-02-26 23:04:13 +00001443#ifdef FEAT_PROFILE
1444 profile_dump();
1445#endif
1446
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001447 if (did_emsg
1448#ifdef FEAT_GUI
1449 || (gui.in_use && msg_didany && p_verbose > 0)
1450#endif
1451 )
1452 {
1453 /* give the user a chance to read the (error) message */
1454 no_wait_return = FALSE;
1455 wait_return(FALSE);
1456 }
1457
1458#ifdef FEAT_AUTOCMD
1459 /* Position the cursor again, the autocommands may have moved it */
1460# ifdef FEAT_GUI
1461 if (!gui.in_use)
1462# endif
1463 windgoto((int)Rows - 1, 0);
1464#endif
1465
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001466#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001467 job_stop_on_exit();
1468#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001469#ifdef FEAT_LUA
1470 lua_end();
1471#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001472#ifdef FEAT_MZSCHEME
1473 mzscheme_end();
1474#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001475#ifdef FEAT_TCL
1476 tcl_end();
1477#endif
1478#ifdef FEAT_RUBY
1479 ruby_end();
1480#endif
1481#ifdef FEAT_PYTHON
1482 python_end();
1483#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001484#ifdef FEAT_PYTHON3
1485 python3_end();
1486#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001487#ifdef FEAT_PERL
1488 perl_end();
1489#endif
1490#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1491 iconv_end();
1492#endif
1493#ifdef FEAT_NETBEANS_INTG
1494 netbeans_end();
1495#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001496#ifdef FEAT_CSCOPE
1497 cs_end();
1498#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001499#ifdef FEAT_EVAL
1500 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001501 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001502#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001503#if defined(WIN32) && defined(FEAT_MBYTE)
1504 free_cmd_argsW();
1505#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001506
1507 mch_exit(exitval);
1508}
1509
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001510#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1511/*
1512 * Setup to use the current locale (for ctype() and many other things).
1513 */
1514 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001515init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001516{
1517 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001518
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001519# ifdef FEAT_GUI_GTK
1520 /* Tell Gtk not to change our locale settings. */
1521 gtk_disable_setlocale();
1522# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001523# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1524 /* Make sure strtod() uses a decimal point, not a comma. */
1525 setlocale(LC_NUMERIC, "C");
1526# endif
1527
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001528# ifdef WIN32
1529 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1530 * text while it's expecting text in the current locale. This call avoids
1531 * that. */
1532 setlocale(LC_CTYPE, "C");
1533# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001534
1535# ifdef FEAT_GETTEXT
1536 {
1537 int mustfree = FALSE;
1538 char_u *p;
1539
1540# ifdef DYNAMIC_GETTEXT
1541 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001542 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001543# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001544 /* expand_env() doesn't work yet, because g_chartab[] is not
1545 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001546 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1547 if (p != NULL && *p != NUL)
1548 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001549 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001550 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1551 }
1552 if (mustfree)
1553 vim_free(p);
1554 textdomain(VIMPACKAGE);
1555 }
1556# endif
1557}
1558#endif
1559
1560/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001561 * Get the name of the display, before gui_prepare() removes it from
1562 * argv[]. Used for the xterm-clipboard display.
1563 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001564 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001565 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001566 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001567early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001568{
Bram Moolenaar03005972008-11-20 13:12:36 +00001569#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1570 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001571 int argc = parmp->argc;
1572 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001573 int i;
1574
1575 for (i = 1; i < argc; i++)
1576 {
1577 if (STRCMP(argv[i], "--") == 0)
1578 break;
1579# ifdef FEAT_XCLIPBOARD
1580 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001581# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001582 || STRICMP(argv[i], "--display") == 0
1583# endif
1584 )
1585 {
1586 if (i == argc - 1)
1587 mainerr_arg_missing((char_u *)argv[i]);
1588 xterm_display = argv[++i];
1589 }
1590# endif
1591# ifdef FEAT_CLIENTSERVER
1592 else if (STRICMP(argv[i], "--servername") == 0)
1593 {
1594 if (i == argc - 1)
1595 mainerr_arg_missing((char_u *)argv[i]);
1596 parmp->serverName_arg = (char_u *)argv[++i];
1597 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001598 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001599 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001600 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001601 {
1602 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001603# ifdef FEAT_GUI
1604 if (strstr(argv[i], "-wait") != 0)
1605 /* don't fork() when starting the GUI to edit files ourself */
1606 gui.dofork = FALSE;
1607# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001608 }
1609# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001610
1611# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1612# ifdef FEAT_GUI_W32
1613 else if (STRICMP(argv[i], "--windowid") == 0)
1614# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001615 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001616# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001617 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001618 long_u id;
1619 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001620
1621 if (i == argc - 1)
1622 mainerr_arg_missing((char_u *)argv[i]);
1623 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001624 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001625 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001626 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001627 if (count != 1)
1628 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1629 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001630# ifdef FEAT_GUI_W32
1631 win_socket_id = id;
1632# else
1633 gtk_socket_id = id;
1634# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001635 i++;
1636 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001637# endif
1638# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001639 else if (STRICMP(argv[i], "--echo-wid") == 0)
1640 echo_wid_arg = TRUE;
1641# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001642# ifndef FEAT_NETBEANS_INTG
1643 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001644 {
1645 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1646 mch_exit(2);
1647 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001648# endif
1649
Bram Moolenaar58d98232005-07-23 22:25:46 +00001650 }
1651#endif
1652}
1653
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001654#ifndef NO_VIM_MAIN
1655/*
1656 * Get a (optional) count for a Vim argument.
1657 */
1658 static int
1659get_number_arg(
1660 char_u *p, /* pointer to argument */
1661 int *idx, /* index in argument, is incremented */
1662 int def) /* default value */
1663{
1664 if (vim_isdigit(p[*idx]))
1665 {
1666 def = atoi((char *)&(p[*idx]));
1667 while (vim_isdigit(p[*idx]))
1668 *idx = *idx + 1;
1669 }
1670 return def;
1671}
1672
1673/*
1674 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1675 * If the executable name starts with "r" we disable shell commands.
1676 * If the next character is "e" we run in Easy mode.
1677 * If the next character is "g" we run the GUI version.
1678 * If the next characters are "view" we start in readonly mode.
1679 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1680 * If the next characters are "ex" we start in Ex mode. If it's followed
1681 * by "im" use improved Ex mode.
1682 */
1683 static void
1684parse_command_name(mparm_T *parmp)
1685{
1686 char_u *initstr;
1687
1688 initstr = gettail((char_u *)parmp->argv[0]);
1689
1690#ifdef MACOS_X_UNIX
1691 /* An issue has been seen when launching Vim in such a way that
1692 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1693 * executable or a symbolic link of it. Until this issue is resolved
1694 * we prohibit the GUI from being used.
1695 */
1696 if (STRCMP(initstr, parmp->argv[0]) == 0)
1697 disallow_gui = TRUE;
1698
1699 /* TODO: On MacOS X default to gui if argv[0] ends in:
1700 * /Vim.app/Contents/MacOS/Vim */
1701#endif
1702
1703#ifdef FEAT_EVAL
1704 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001705 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001706#endif
1707
1708 if (TOLOWER_ASC(initstr[0]) == 'r')
1709 {
1710 restricted = TRUE;
1711 ++initstr;
1712 }
1713
1714 /* Use evim mode for "evim" and "egvim", not for "editor". */
1715 if (TOLOWER_ASC(initstr[0]) == 'e'
1716 && (TOLOWER_ASC(initstr[1]) == 'v'
1717 || TOLOWER_ASC(initstr[1]) == 'g'))
1718 {
1719#ifdef FEAT_GUI
1720 gui.starting = TRUE;
1721#endif
1722 parmp->evim_mode = TRUE;
1723 ++initstr;
1724 }
1725
1726 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1727 if (TOLOWER_ASC(initstr[0]) == 'g')
1728 {
1729 main_start_gui();
1730#ifdef FEAT_GUI
1731 ++initstr;
1732#endif
1733 }
1734
1735 if (STRNICMP(initstr, "view", 4) == 0)
1736 {
1737 readonlymode = TRUE;
1738 curbuf->b_p_ro = TRUE;
1739 p_uc = 10000; /* don't update very often */
1740 initstr += 4;
1741 }
1742 else if (STRNICMP(initstr, "vim", 3) == 0)
1743 initstr += 3;
1744
1745 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1746 if (STRICMP(initstr, "diff") == 0)
1747 {
1748#ifdef FEAT_DIFF
1749 parmp->diff_mode = TRUE;
1750#else
1751 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1752 mch_errmsg("\n");
1753 mch_exit(2);
1754#endif
1755 }
1756
1757 if (STRNICMP(initstr, "ex", 2) == 0)
1758 {
1759 if (STRNICMP(initstr + 2, "im", 2) == 0)
1760 exmode_active = EXMODE_VIM;
1761 else
1762 exmode_active = EXMODE_NORMAL;
1763 change_compatible(TRUE); /* set 'compatible' */
1764 }
1765}
1766
Bram Moolenaar58d98232005-07-23 22:25:46 +00001767/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001768 * Scan the command line arguments.
1769 */
1770 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001771command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001772{
1773 int argc = parmp->argc;
1774 char **argv = parmp->argv;
1775 int argv_idx; /* index in argv[n][] */
1776 int had_minmin = FALSE; /* found "--" argument */
1777 int want_argument; /* option argument with argument */
1778 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001779 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001780 long n;
1781
1782 --argc;
1783 ++argv;
1784 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1785 while (argc > 0)
1786 {
1787 /*
1788 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1789 */
1790 if (argv[0][0] == '+' && !had_minmin)
1791 {
1792 if (parmp->n_commands >= MAX_ARG_CMDS)
1793 mainerr(ME_EXTRA_CMD, NULL);
1794 argv_idx = -1; /* skip to next argument */
1795 if (argv[0][1] == NUL)
1796 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1797 else
1798 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1799 }
1800
1801 /*
1802 * Optional argument.
1803 */
1804 else if (argv[0][0] == '-' && !had_minmin)
1805 {
1806 want_argument = FALSE;
1807 c = argv[0][argv_idx++];
1808#ifdef VMS
1809 /*
1810 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1811 * and "-/X" as "-X".
1812 */
1813 if (c == '/')
1814 {
1815 c = argv[0][argv_idx++];
1816 c = TOUPPER_ASC(c);
1817 }
1818 else
1819 c = TOLOWER_ASC(c);
1820#endif
1821 switch (c)
1822 {
1823 case NUL: /* "vim -" read from stdin */
1824 /* "ex -" silent mode */
1825 if (exmode_active)
1826 silent_mode = TRUE;
1827 else
1828 {
1829 if (parmp->edit_type != EDIT_NONE)
1830 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1831 parmp->edit_type = EDIT_STDIN;
1832 read_cmd_fd = 2; /* read from stderr instead of stdin */
1833 }
1834 argv_idx = -1; /* skip to next argument */
1835 break;
1836
1837 case '-': /* "--" don't take any more option arguments */
1838 /* "--help" give help message */
1839 /* "--version" give version message */
1840 /* "--literal" take files literally */
1841 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001842 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001843 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001844 /* "--noplugin[s]" skip plugins */
1845 /* "--cmd <cmd>" execute cmd before vimrc */
1846 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1847 usage();
1848 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1849 {
1850 Columns = 80; /* need to init Columns */
1851 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1852 list_version();
1853 msg_putchar('\n');
1854 msg_didout = FALSE;
1855 mch_exit(0);
1856 }
1857 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1858 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001859#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001860 parmp->literal = TRUE;
1861#endif
1862 }
1863 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1864 {
1865#ifdef FEAT_GUI
1866 gui.dofork = FALSE; /* don't fork() when starting GUI */
1867#endif
1868 }
1869 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1870 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001871 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1872 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001873 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1874 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001875 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1876 {
1877 want_argument = TRUE;
1878 argv_idx += 3;
1879 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001880 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1881 {
1882 want_argument = TRUE;
1883 argv_idx += 11;
1884 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001885#ifdef FEAT_CLIENTSERVER
1886 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1887 ; /* already processed -- no arg */
1888 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1889 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1890 {
1891 /* already processed -- snatch the following arg */
1892 if (argc > 1)
1893 {
1894 --argc;
1895 ++argv;
1896 }
1897 }
1898#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001899#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1900# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001901 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001902# else
1903 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1904# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001905 {
1906 /* already processed -- snatch the following arg */
1907 if (argc > 1)
1908 {
1909 --argc;
1910 ++argv;
1911 }
1912 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001913#endif
1914#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001915 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1916 {
1917 /* already processed, skip */
1918 }
1919#endif
1920 else
1921 {
1922 if (argv[0][argv_idx])
1923 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1924 had_minmin = TRUE;
1925 }
1926 if (!want_argument)
1927 argv_idx = -1; /* skip to next argument */
1928 break;
1929
1930 case 'A': /* "-A" start in Arabic mode */
1931#ifdef FEAT_ARABIC
1932 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1933#else
1934 mch_errmsg(_(e_noarabic));
1935 mch_exit(2);
1936#endif
1937 break;
1938
1939 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001940 /* Needs to be effective before expanding file names, because
1941 * for Win32 this makes us edit a shortcut file itself,
1942 * instead of the file it links to. */
1943 set_options_bin(curbuf->b_p_bin, 1, 0);
1944 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001945 break;
1946
1947 case 'C': /* "-C" Compatible */
1948 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001949 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001950 break;
1951
1952 case 'e': /* "-e" Ex mode */
1953 exmode_active = EXMODE_NORMAL;
1954 break;
1955
1956 case 'E': /* "-E" Improved Ex mode */
1957 exmode_active = EXMODE_VIM;
1958 break;
1959
1960 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1961 window directly, not with newcli */
1962#ifdef FEAT_GUI
1963 gui.dofork = FALSE; /* don't fork() when starting GUI */
1964#endif
1965 break;
1966
1967 case 'g': /* "-g" start GUI */
1968 main_start_gui();
1969 break;
1970
1971 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1972#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001973 p_fkmap = TRUE;
1974 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001975#else
1976 mch_errmsg(_(e_nofarsi));
1977 mch_exit(2);
1978#endif
1979 break;
1980
1981 case 'h': /* "-h" give help message */
1982#ifdef FEAT_GUI_GNOME
1983 /* Tell usage() to exit for "gvim". */
1984 gui.starting = FALSE;
1985#endif
1986 usage();
1987 break;
1988
1989 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1990#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001991 p_hkmap = TRUE;
1992 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001993#else
1994 mch_errmsg(_(e_nohebrew));
1995 mch_exit(2);
1996#endif
1997 break;
1998
1999 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2000#ifdef FEAT_LISP
2001 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2002 p_sm = TRUE;
2003#endif
2004 break;
2005
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002006 case 'M': /* "-M" no changes or writing of files */
2007 reset_modifiable();
2008 /* FALLTHROUGH */
2009
2010 case 'm': /* "-m" no writing of files */
2011 p_write = FALSE;
2012 break;
2013
2014 case 'y': /* "-y" easy mode */
2015#ifdef FEAT_GUI
2016 gui.starting = TRUE; /* start GUI a bit later */
2017#endif
2018 parmp->evim_mode = TRUE;
2019 break;
2020
2021 case 'N': /* "-N" Nocompatible */
2022 change_compatible(FALSE);
2023 break;
2024
2025 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002026#ifdef FEAT_NETBEANS_INTG
2027 /* checking for "-nb", netbeans parameters */
2028 if (argv[0][argv_idx] == 'b')
2029 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002030 netbeansArg = argv[0];
2031 argv_idx = -1; /* skip to next argument */
2032 }
2033 else
2034#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002035 parmp->no_swap_file = TRUE;
2036 break;
2037
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002038 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002039#ifdef TARGET_API_MAC_OSX
2040 /* For some reason on MacOS X, an argument like:
2041 -psn_0_10223617 is passed in when invoke from Finder
2042 or with the 'open' command */
2043 if (argv[0][argv_idx] == 's')
2044 {
2045 argv_idx = -1; /* bypass full -psn */
2046 main_start_gui();
2047 break;
2048 }
2049#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002050#ifdef FEAT_WINDOWS
2051 /* default is 0: open window for each file */
2052 parmp->window_count = get_number_arg((char_u *)argv[0],
2053 &argv_idx, 0);
2054 parmp->window_layout = WIN_TABS;
2055#endif
2056 break;
2057
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002058 case 'o': /* "-o[N]" open N horizontal split windows */
2059#ifdef FEAT_WINDOWS
2060 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002061 parmp->window_count = get_number_arg((char_u *)argv[0],
2062 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002063 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002064#endif
2065 break;
2066
2067 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002068#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002069 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002070 parmp->window_count = get_number_arg((char_u *)argv[0],
2071 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002072 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002073#endif
2074 break;
2075
2076#ifdef FEAT_QUICKFIX
2077 case 'q': /* "-q" QuickFix mode */
2078 if (parmp->edit_type != EDIT_NONE)
2079 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2080 parmp->edit_type = EDIT_QF;
2081 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2082 {
2083 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2084 argv_idx = -1;
2085 }
2086 else if (argc > 1) /* "-q {errorfile}" */
2087 want_argument = TRUE;
2088 break;
2089#endif
2090
2091 case 'R': /* "-R" readonly mode */
2092 readonlymode = TRUE;
2093 curbuf->b_p_ro = TRUE;
2094 p_uc = 10000; /* don't update very often */
2095 break;
2096
2097 case 'r': /* "-r" recovery mode */
2098 case 'L': /* "-L" recovery mode */
2099 recoverymode = 1;
2100 break;
2101
2102 case 's':
2103 if (exmode_active) /* "-s" silent (batch) mode */
2104 silent_mode = TRUE;
2105 else /* "-s {scriptin}" read from script file */
2106 want_argument = TRUE;
2107 break;
2108
2109 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2110 if (parmp->edit_type != EDIT_NONE)
2111 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2112 parmp->edit_type = EDIT_TAG;
2113 if (argv[0][argv_idx]) /* "-t{tag}" */
2114 {
2115 parmp->tagname = (char_u *)argv[0] + argv_idx;
2116 argv_idx = -1;
2117 }
2118 else /* "-t {tag}" */
2119 want_argument = TRUE;
2120 break;
2121
2122#ifdef FEAT_EVAL
2123 case 'D': /* "-D" Debugging */
2124 parmp->use_debug_break_level = 9999;
2125 break;
2126#endif
2127#ifdef FEAT_DIFF
2128 case 'd': /* "-d" 'diff' */
2129# ifdef AMIGA
2130 /* check for "-dev {device}" */
2131 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2132 want_argument = TRUE;
2133 else
2134# endif
2135 parmp->diff_mode = TRUE;
2136 break;
2137#endif
2138 case 'V': /* "-V{N}" Verbose level */
2139 /* default is 10: a little bit verbose */
2140 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2141 if (argv[0][argv_idx] != NUL)
2142 {
2143 set_option_value((char_u *)"verbosefile", 0L,
2144 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002145 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002146 }
2147 break;
2148
2149 case 'v': /* "-v" Vi-mode (as if called "vi") */
2150 exmode_active = 0;
2151#ifdef FEAT_GUI
2152 gui.starting = FALSE; /* don't start GUI */
2153#endif
2154 break;
2155
2156 case 'w': /* "-w{number}" set window height */
2157 /* "-w {scriptout}" write to script */
2158 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2159 {
2160 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2161 set_option_value((char_u *)"window", n, NULL, 0);
2162 break;
2163 }
2164 want_argument = TRUE;
2165 break;
2166
2167#ifdef FEAT_CRYPT
2168 case 'x': /* "-x" encrypted reading/writing of files */
2169 parmp->ask_for_key = TRUE;
2170 break;
2171#endif
2172
2173 case 'X': /* "-X" don't connect to X server */
2174#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2175 x_no_connect = TRUE;
2176#endif
2177 break;
2178
2179 case 'Z': /* "-Z" restricted mode */
2180 restricted = TRUE;
2181 break;
2182
2183 case 'c': /* "-c{command}" or "-c {command}" execute
2184 command */
2185 if (argv[0][argv_idx] != NUL)
2186 {
2187 if (parmp->n_commands >= MAX_ARG_CMDS)
2188 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002189 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2190 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002191 argv_idx = -1;
2192 break;
2193 }
2194 /*FALLTHROUGH*/
2195 case 'S': /* "-S {file}" execute Vim script */
2196 case 'i': /* "-i {viminfo}" use for viminfo */
2197#ifndef FEAT_DIFF
2198 case 'd': /* "-d {device}" device (for Amiga) */
2199#endif
2200 case 'T': /* "-T {terminal}" terminal name */
2201 case 'u': /* "-u {vimrc}" vim inits file */
2202 case 'U': /* "-U {gvimrc}" gvim inits file */
2203 case 'W': /* "-W {scriptout}" overwrite */
2204#ifdef FEAT_GUI_W32
2205 case 'P': /* "-P {parent title}" MDI parent */
2206#endif
2207 want_argument = TRUE;
2208 break;
2209
2210 default:
2211 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2212 }
2213
2214 /*
2215 * Handle option arguments with argument.
2216 */
2217 if (want_argument)
2218 {
2219 /*
2220 * Check for garbage immediately after the option letter.
2221 */
2222 if (argv[0][argv_idx] != NUL)
2223 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2224
2225 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002226 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002227 mainerr_arg_missing((char_u *)argv[0]);
2228 ++argv;
2229 argv_idx = -1;
2230
2231 switch (c)
2232 {
2233 case 'c': /* "-c {command}" execute command */
2234 case 'S': /* "-S {file}" execute Vim script */
2235 if (parmp->n_commands >= MAX_ARG_CMDS)
2236 mainerr(ME_EXTRA_CMD, NULL);
2237 if (c == 'S')
2238 {
2239 char *a;
2240
2241 if (argc < 1)
2242 /* "-S" without argument: use default session file
2243 * name. */
2244 a = SESSION_FILE;
2245 else if (argv[0][0] == '-')
2246 {
2247 /* "-S" followed by another option: use default
2248 * session file name. */
2249 a = SESSION_FILE;
2250 ++argc;
2251 --argv;
2252 }
2253 else
2254 a = argv[0];
2255 p = alloc((unsigned)(STRLEN(a) + 4));
2256 if (p == NULL)
2257 mch_exit(2);
2258 sprintf((char *)p, "so %s", a);
2259 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2260 parmp->commands[parmp->n_commands++] = p;
2261 }
2262 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002263 parmp->commands[parmp->n_commands++] =
2264 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002265 break;
2266
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002267 case '-':
2268 if (argv[-1][2] == 'c')
2269 {
2270 /* "--cmd {command}" execute command */
2271 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2272 mainerr(ME_EXTRA_CMD, NULL);
2273 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002274 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002275 }
2276 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002277 break;
2278
2279 /* case 'd': -d {device} is handled in mch_check_win() for the
2280 * Amiga */
2281
2282#ifdef FEAT_QUICKFIX
2283 case 'q': /* "-q {errorfile}" QuickFix mode */
2284 parmp->use_ef = (char_u *)argv[0];
2285 break;
2286#endif
2287
2288 case 'i': /* "-i {viminfo}" use for viminfo */
2289 use_viminfo = (char_u *)argv[0];
2290 break;
2291
2292 case 's': /* "-s {scriptin}" read from script file */
2293 if (scriptin[0] != NULL)
2294 {
2295scripterror:
2296 mch_errmsg(_("Attempt to open script file again: \""));
2297 mch_errmsg(argv[-1]);
2298 mch_errmsg(" ");
2299 mch_errmsg(argv[0]);
2300 mch_errmsg("\"\n");
2301 mch_exit(2);
2302 }
2303 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2304 {
2305 mch_errmsg(_("Cannot open for reading: \""));
2306 mch_errmsg(argv[0]);
2307 mch_errmsg("\"\n");
2308 mch_exit(2);
2309 }
2310 if (save_typebuf() == FAIL)
2311 mch_exit(2); /* out of memory */
2312 break;
2313
2314 case 't': /* "-t {tag}" */
2315 parmp->tagname = (char_u *)argv[0];
2316 break;
2317
2318 case 'T': /* "-T {terminal}" terminal name */
2319 /*
2320 * The -T term argument is always available and when
2321 * HAVE_TERMLIB is supported it overrides the environment
2322 * variable TERM.
2323 */
2324#ifdef FEAT_GUI
2325 if (term_is_gui((char_u *)argv[0]))
2326 gui.starting = TRUE; /* start GUI a bit later */
2327 else
2328#endif
2329 parmp->term = (char_u *)argv[0];
2330 break;
2331
2332 case 'u': /* "-u {vimrc}" vim inits file */
2333 parmp->use_vimrc = (char_u *)argv[0];
2334 break;
2335
2336 case 'U': /* "-U {gvimrc}" gvim inits file */
2337#ifdef FEAT_GUI
2338 use_gvimrc = (char_u *)argv[0];
2339#endif
2340 break;
2341
2342 case 'w': /* "-w {nr}" 'window' value */
2343 /* "-w {scriptout}" append to script file */
2344 if (vim_isdigit(*((char_u *)argv[0])))
2345 {
2346 argv_idx = 0;
2347 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2348 set_option_value((char_u *)"window", n, NULL, 0);
2349 argv_idx = -1;
2350 break;
2351 }
2352 /*FALLTHROUGH*/
2353 case 'W': /* "-W {scriptout}" overwrite script file */
2354 if (scriptout != NULL)
2355 goto scripterror;
2356 if ((scriptout = mch_fopen(argv[0],
2357 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2358 {
2359 mch_errmsg(_("Cannot open for script output: \""));
2360 mch_errmsg(argv[0]);
2361 mch_errmsg("\"\n");
2362 mch_exit(2);
2363 }
2364 break;
2365
2366#ifdef FEAT_GUI_W32
2367 case 'P': /* "-P {parent title}" MDI parent */
2368 gui_mch_set_parent(argv[0]);
2369 break;
2370#endif
2371 }
2372 }
2373 }
2374
2375 /*
2376 * File name argument.
2377 */
2378 else
2379 {
2380 argv_idx = -1; /* skip to next argument */
2381
2382 /* Check for only one type of editing. */
2383 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2384 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2385 parmp->edit_type = EDIT_FILE;
2386
2387#ifdef MSWIN
2388 /* Remember if the argument was a full path before changing
2389 * slashes to backslashes. */
2390 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2391 parmp->full_path = TRUE;
2392#endif
2393
2394 /* Add the file to the global argument list. */
2395 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2396 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2397 mch_exit(2);
2398#ifdef FEAT_DIFF
2399 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2400 && !mch_isdir(alist_name(&GARGLIST[0])))
2401 {
2402 char_u *r;
2403
2404 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2405 if (r != NULL)
2406 {
2407 vim_free(p);
2408 p = r;
2409 }
2410 }
2411#endif
2412#if defined(__CYGWIN32__) && !defined(WIN32)
2413 /*
2414 * If vim is invoked by non-Cygwin tools, convert away any
2415 * DOS paths, so things like .swp files are created correctly.
2416 * Look for evidence of non-Cygwin paths before we bother.
2417 * This is only for when using the Unix files.
2418 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002419 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002420 {
2421 char posix_path[PATH_MAX];
2422
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002423# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2424 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2425# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002426 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002427# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002428 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002429 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002430 if (p == NULL)
2431 mch_exit(2);
2432 }
2433#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002434
2435#ifdef USE_FNAME_CASE
2436 /* Make the case of the file name match the actual file. */
2437 fname_case(p, 0);
2438#endif
2439
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002441#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002442 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443#else
2444 2 /* add buffer number now and use curbuf */
2445#endif
2446 );
2447
2448#if defined(FEAT_MBYTE) && defined(WIN32)
2449 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002450 /* Remember this argument has been added to the argument list.
2451 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002452 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002453# ifdef FEAT_DIFF
2454 parmp->diff_mode
2455# else
2456 FALSE
2457# endif
2458 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459 }
2460#endif
2461 }
2462
2463 /*
2464 * If there are no more letters after the current "-", go to next
2465 * argument. argv_idx is set to -1 when the current argument is to be
2466 * skipped.
2467 */
2468 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2469 {
2470 --argc;
2471 ++argv;
2472 argv_idx = 1;
2473 }
2474 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002475
2476#ifdef FEAT_EVAL
2477 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2478 * one. */
2479 if (parmp->n_commands > 0)
2480 {
2481 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2482 if (p != NULL)
2483 {
2484 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2485 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2486 vim_free(p);
2487 }
2488 }
2489#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002490}
2491
2492/*
2493 * Print a warning if stdout is not a terminal.
2494 * When starting in Ex mode and commands come from a file, set Silent mode.
2495 */
2496 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002497check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002498{
2499 int input_isatty; /* is active input a terminal? */
2500
2501 input_isatty = mch_input_isatty();
2502 if (exmode_active)
2503 {
2504 if (!input_isatty)
2505 silent_mode = TRUE;
2506 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002507 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508#ifdef FEAT_GUI
2509 /* don't want the delay when started from the desktop */
2510 && !gui.starting
2511#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002512 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002513 {
2514#ifdef NBDEBUG
2515 /*
2516 * This shouldn't be necessary. But if I run netbeans with the log
2517 * output coming to the console and XOpenDisplay fails, I get vim
2518 * trying to start with input/output to my console tty. This fills my
2519 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002520 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002521 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002522 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002523 {
2524 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2525 exit(1);
2526 }
2527#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002528#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2529 if (is_cygpty_used())
2530 {
2531 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2532 exit(1);
2533 }
2534#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002535 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002536 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2537 if (!input_isatty)
2538 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2539 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002540 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2541 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002542 if (scriptin[0] == NULL)
2543 ui_delay(2000L, TRUE);
2544 TIME_MSG("Warning delay");
2545 }
2546}
2547
2548/*
2549 * Read text from stdin.
2550 */
2551 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002552read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002553{
2554 int i;
2555
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002556#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002557 /* When getting the ATTENTION prompt here, use a dialog */
2558 swap_exists_action = SEA_DIALOG;
2559#endif
2560 no_wait_return = TRUE;
2561 i = msg_didany;
2562 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002563 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002564 no_wait_return = FALSE;
2565 msg_didany = i;
2566 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002567#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568 check_swap_exists_action();
2569#endif
2570#if !(defined(AMIGA) || defined(MACOS))
2571 /*
2572 * Close stdin and dup it from stderr. Required for GPM to work
2573 * properly, and for running external commands.
2574 * Is there any other system that cannot do this?
2575 */
2576 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002577 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002578#endif
2579}
2580
2581/*
2582 * Create the requested number of windows and edit buffers in them.
2583 * Also does recovery if "recoverymode" set.
2584 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002585 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002586create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587{
2588#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002589 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002590 int done = 0;
2591
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592 /*
2593 * Create the number of windows that was requested.
2594 */
2595 if (parmp->window_count == -1) /* was not set */
2596 parmp->window_count = 1;
2597 if (parmp->window_count == 0)
2598 parmp->window_count = GARGCOUNT;
2599 if (parmp->window_count > 1)
2600 {
2601 /* Don't change the windows if there was a command in .vimrc that
2602 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002603 if (parmp->window_layout == 0)
2604 parmp->window_layout = WIN_HOR;
2605 if (parmp->window_layout == WIN_TABS)
2606 {
2607 parmp->window_count = make_tabpages(parmp->window_count);
2608 TIME_MSG("making tab pages");
2609 }
2610 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002611 {
2612 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002613 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002614 TIME_MSG("making windows");
2615 }
2616 else
2617 parmp->window_count = win_count();
2618 }
2619 else
2620 parmp->window_count = 1;
2621#endif
2622
2623 if (recoverymode) /* do recover */
2624 {
2625 msg_scroll = TRUE; /* scroll message up */
2626 ml_recover();
2627 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2628 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002629 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002630 }
2631 else
2632 {
2633 /*
2634 * Open a buffer for windows that don't have one yet.
2635 * Commands in the .vimrc might have loaded a file or split the window.
2636 * Watch out for autocommands that delete a window.
2637 */
2638#ifdef FEAT_AUTOCMD
2639 /*
2640 * Don't execute Win/Buf Enter/Leave autocommands here
2641 */
2642 ++autocmd_no_enter;
2643 ++autocmd_no_leave;
2644#endif
2645#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002646 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002647 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002648 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002649 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002650 {
2651 if (parmp->window_layout == WIN_TABS)
2652 goto_tabpage(1);
2653 else
2654 curwin = firstwin;
2655 }
2656 else if (parmp->window_layout == WIN_TABS)
2657 {
2658 if (curtab->tp_next == NULL)
2659 break;
2660 goto_tabpage(0);
2661 }
2662 else
2663 {
2664 if (curwin->w_next == NULL)
2665 break;
2666 curwin = curwin->w_next;
2667 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002668 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002669#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002670 curbuf = curwin->w_buffer;
2671 if (curbuf->b_ml.ml_mfp == NULL)
2672 {
2673#ifdef FEAT_FOLDING
2674 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2675 if (p_fdls >= 0)
2676 curwin->w_p_fdl = p_fdls;
2677#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002678#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002679 /* When getting the ATTENTION prompt here, use a dialog */
2680 swap_exists_action = SEA_DIALOG;
2681#endif
2682 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002683
2684 /* create memfile, read file */
2685 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002686
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002687#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002688 if (swap_exists_action == SEA_QUIT)
2689 {
2690 if (got_int || only_one_window())
2691 {
2692 /* abort selected or quit and only one window */
2693 did_emsg = FALSE; /* avoid hit-enter prompt */
2694 getout(1);
2695 }
2696 /* We can't close the window, it would disturb what
2697 * happens next. Clear the file name and set the arg
2698 * index to -1 to delete it later. */
2699 setfname(curbuf, NULL, NULL, FALSE);
2700 curwin->w_arg_idx = -1;
2701 swap_exists_action = SEA_NONE;
2702 }
2703 else
2704 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002705#endif
2706#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002707 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708#endif
2709 }
2710#ifdef FEAT_WINDOWS
2711 ui_breakcheck();
2712 if (got_int)
2713 {
2714 (void)vgetc(); /* only break the file loading, not the rest */
2715 break;
2716 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002717 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002718#endif
2719#ifdef FEAT_WINDOWS
2720 if (parmp->window_layout == WIN_TABS)
2721 goto_tabpage(1);
2722 else
2723 curwin = firstwin;
2724 curbuf = curwin->w_buffer;
2725#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726#ifdef FEAT_AUTOCMD
2727 --autocmd_no_enter;
2728 --autocmd_no_leave;
2729#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002730 }
2731}
2732
2733#ifdef FEAT_WINDOWS
2734 /*
2735 * If opened more than one window, start editing files in the other
2736 * windows. make_windows() has already opened the windows.
2737 */
2738 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002739edit_buffers(
2740 mparm_T *parmp,
2741 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002742{
2743 int arg_idx; /* index in argument list */
2744 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002745 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002746 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002747
2748# ifdef FEAT_AUTOCMD
2749 /*
2750 * Don't execute Win/Buf Enter/Leave autocommands here
2751 */
2752 ++autocmd_no_enter;
2753 ++autocmd_no_leave;
2754# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002755
2756 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2757 if (curwin->w_arg_idx == -1)
2758 {
2759 win_close(curwin, TRUE);
2760 advance = FALSE;
2761 }
2762
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002763 arg_idx = 1;
2764 for (i = 1; i < parmp->window_count; ++i)
2765 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002766 if (cwd != NULL)
2767 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002768 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2769 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002770 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002771 ++arg_idx;
2772 win_close(curwin, TRUE);
2773 advance = FALSE;
2774 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002775 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002776
Bram Moolenaar84212822006-11-07 21:59:47 +00002777 if (advance)
2778 {
2779 if (parmp->window_layout == WIN_TABS)
2780 {
2781 if (curtab->tp_next == NULL) /* just checking */
2782 break;
2783 goto_tabpage(0);
2784 }
2785 else
2786 {
2787 if (curwin->w_next == NULL) /* just checking */
2788 break;
2789 win_enter(curwin->w_next, FALSE);
2790 }
2791 }
2792 advance = TRUE;
2793
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002795 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2797 {
2798 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002799 /* Edit file from arg list, if there is one. When "Quit" selected
2800 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002801# ifdef HAS_SWAP_EXISTS_ACTION
2802 swap_exists_did_quit = FALSE;
2803# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002804 (void)do_ecmd(0, arg_idx < GARGCOUNT
2805 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002806 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002807# ifdef HAS_SWAP_EXISTS_ACTION
2808 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002809 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002810 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002811 if (got_int || only_one_window())
2812 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002813 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002814 did_emsg = FALSE; /* avoid hit-enter prompt */
2815 getout(1);
2816 }
2817 win_close(curwin, TRUE);
2818 advance = FALSE;
2819 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002820# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002821 if (arg_idx == GARGCOUNT - 1)
2822 arg_had_last = TRUE;
2823 ++arg_idx;
2824 }
2825 ui_breakcheck();
2826 if (got_int)
2827 {
2828 (void)vgetc(); /* only break the file loading, not the rest */
2829 break;
2830 }
2831 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002832
2833 if (parmp->window_layout == WIN_TABS)
2834 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002835# ifdef FEAT_AUTOCMD
2836 --autocmd_no_enter;
2837# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002838
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002839 /* make the first window the current window */
2840 win = firstwin;
2841#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2842 /* Avoid making a preview window the current window. */
2843 while (win->w_p_pvw)
2844 {
2845 win = win->w_next;
2846 if (win == NULL)
2847 {
2848 win = firstwin;
2849 break;
2850 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002851 }
2852#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002853 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002854
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002855# ifdef FEAT_AUTOCMD
2856 --autocmd_no_leave;
2857# endif
2858 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002859 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002860 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2861}
2862#endif /* FEAT_WINDOWS */
2863
2864/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002865 * Execute the commands from --cmd arguments "cmds[cnt]".
2866 */
2867 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002868exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002869{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002870 char_u **cmds = parmp->pre_commands;
2871 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002872 int i;
2873
2874 if (cnt > 0)
2875 {
2876 curwin->w_cursor.lnum = 0; /* just in case.. */
2877 sourcing_name = (char_u *)_("pre-vimrc command line");
2878# ifdef FEAT_EVAL
2879 current_SID = SID_CMDARG;
2880# endif
2881 for (i = 0; i < cnt; ++i)
2882 do_cmdline_cmd(cmds[i]);
2883 sourcing_name = NULL;
2884# ifdef FEAT_EVAL
2885 current_SID = 0;
2886# endif
2887 TIME_MSG("--cmd commands");
2888 }
2889}
2890
2891/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002892 * Execute "+", "-c" and "-S" arguments.
2893 */
2894 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002895exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002896{
2897 int i;
2898
2899 /*
2900 * We start commands on line 0, make "vim +/pat file" match a
2901 * pattern on line 1. But don't move the cursor when an autocommand
2902 * with g`" was used.
2903 */
2904 msg_scroll = TRUE;
2905 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2906 curwin->w_cursor.lnum = 0;
2907 sourcing_name = (char_u *)"command line";
2908#ifdef FEAT_EVAL
2909 current_SID = SID_CARG;
2910#endif
2911 for (i = 0; i < parmp->n_commands; ++i)
2912 {
2913 do_cmdline_cmd(parmp->commands[i]);
2914 if (parmp->cmds_tofree[i])
2915 vim_free(parmp->commands[i]);
2916 }
2917 sourcing_name = NULL;
2918#ifdef FEAT_EVAL
2919 current_SID = 0;
2920#endif
2921 if (curwin->w_cursor.lnum == 0)
2922 curwin->w_cursor.lnum = 1;
2923
2924 if (!exmode_active)
2925 msg_scroll = FALSE;
2926
2927#ifdef FEAT_QUICKFIX
2928 /* When started with "-q errorfile" jump to first error again. */
2929 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002930 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002931#endif
2932 TIME_MSG("executing command arguments");
2933}
2934
2935/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002936 * Source startup scripts.
2937 */
2938 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002939source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002940{
2941 int i;
2942
2943 /*
2944 * For "evim" source evim.vim first of all, so that the user can overrule
2945 * any things he doesn't like.
2946 */
2947 if (parmp->evim_mode)
2948 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002949 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002950 TIME_MSG("source evim file");
2951 }
2952
2953 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002954 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002955 * nothing else.
2956 */
2957 if (parmp->use_vimrc != NULL)
2958 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002959 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2960 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002961 {
2962#ifdef FEAT_GUI
2963 if (use_gvimrc == NULL) /* don't load gvimrc either */
2964 use_gvimrc = parmp->use_vimrc;
2965#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002966 }
2967 else
2968 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002969 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002970 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2971 }
2972 }
2973 else if (!silent_mode)
2974 {
2975#ifdef AMIGA
2976 struct Process *proc = (struct Process *)FindTask(0L);
2977 APTR save_winptr = proc->pr_WindowPtr;
2978
2979 /* Avoid a requester here for a volume that doesn't exist. */
2980 proc->pr_WindowPtr = (APTR)-1L;
2981#endif
2982
2983 /*
2984 * Get system wide defaults, if the file name is defined.
2985 */
2986#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002987 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002988#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002989#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002990 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002991#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002992
2993 /*
2994 * Try to read initialization commands from the following places:
2995 * - environment variable VIMINIT
2996 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2997 * - second user vimrc file ($VIM/.vimrc for Dos)
2998 * - environment variable EXINIT
2999 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3000 * - second user exrc file ($VIM/.exrc for Dos)
3001 * The first that exists is used, the rest is ignored.
3002 */
3003 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3004 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003005 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003006#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003007 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3008 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003009#endif
3010#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003011 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3012 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003013#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003014#ifdef USR_VIMRC_FILE4
3015 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3016 DOSO_VIMRC) == FAIL
3017#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003018 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003019 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003020#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003021 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003022#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003023 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003024 {
3025 /* When no .vimrc file was found: source defaults.vim. */
3026 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003027 }
3028 }
3029
3030 /*
3031 * Read initialization commands from ".vimrc" or ".exrc" in current
3032 * directory. This is only done if the 'exrc' option is set.
3033 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003034 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003035 * option has been reset in environment of global ".exrc" or ".vimrc".
3036 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3037 * SYS_VIMRC_FILE.
3038 */
3039 if (p_exrc)
3040 {
3041#if defined(UNIX) || defined(VMS)
3042 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3043 if (!file_owned(VIMRC_FILE))
3044#endif
3045 secure = p_secure;
3046
3047 i = FAIL;
3048 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3049 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3050#ifdef USR_VIMRC_FILE2
3051 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3052 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3053#endif
3054#ifdef USR_VIMRC_FILE3
3055 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3056 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3057#endif
3058#ifdef SYS_VIMRC_FILE
3059 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3060 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3061#endif
3062 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003063 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003064
3065 if (i == FAIL)
3066 {
3067#if defined(UNIX) || defined(VMS)
3068 /* if ".exrc" is not owned by user set 'secure' mode */
3069 if (!file_owned(EXRC_FILE))
3070 secure = p_secure;
3071 else
3072 secure = 0;
3073#endif
3074 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3075 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3076#ifdef USR_EXRC_FILE2
3077 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3078 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3079#endif
3080 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003081 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003082 }
3083 }
3084 if (secure == 2)
3085 need_wait_return = TRUE;
3086 secure = 0;
3087#ifdef AMIGA
3088 proc->pr_WindowPtr = save_winptr;
3089#endif
3090 }
3091 TIME_MSG("sourcing vimrc file(s)");
3092}
3093
3094/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003095 * Setup to start using the GUI. Exit with an error when not available.
3096 */
3097 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003098main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003099{
3100#ifdef FEAT_GUI
3101 gui.starting = TRUE; /* start GUI a bit later */
3102#else
3103 mch_errmsg(_(e_nogvim));
3104 mch_errmsg("\n");
3105 mch_exit(2);
3106#endif
3107}
3108
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003109#endif /* NO_VIM_MAIN */
3110
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003111/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003112 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003113 * Returns FAIL if the environment variable was not executed, OK otherwise.
3114 */
3115 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003116process_env(
3117 char_u *env,
3118 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003119{
3120 char_u *initstr;
3121 char_u *save_sourcing_name;
3122 linenr_T save_sourcing_lnum;
3123#ifdef FEAT_EVAL
3124 scid_T save_sid;
3125#endif
3126
3127 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3128 {
3129 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003130 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003131 save_sourcing_name = sourcing_name;
3132 save_sourcing_lnum = sourcing_lnum;
3133 sourcing_name = env;
3134 sourcing_lnum = 0;
3135#ifdef FEAT_EVAL
3136 save_sid = current_SID;
3137 current_SID = SID_ENV;
3138#endif
3139 do_cmdline_cmd(initstr);
3140 sourcing_name = save_sourcing_name;
3141 sourcing_lnum = save_sourcing_lnum;
3142#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003143 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003144#endif
3145 return OK;
3146 }
3147 return FAIL;
3148}
3149
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003150#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003151/*
3152 * Return TRUE if we are certain the user owns the file "fname".
3153 * Used for ".vimrc" and ".exrc".
3154 * Use both stat() and lstat() for extra security.
3155 */
3156 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003157file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003158{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003159 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003160# ifdef UNIX
3161 uid_t uid = getuid();
3162# else /* VMS */
3163 uid_t uid = ((getgid() << 16) | getuid());
3164# endif
3165
3166 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3167# ifdef HAVE_LSTAT
3168 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3169# endif
3170 );
3171}
3172#endif
3173
3174/*
3175 * Give an error message main_errors["n"] and exit.
3176 */
3177 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003178mainerr(
3179 int n, /* one of the ME_ defines */
3180 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003181{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003182#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003183 reset_signals(); /* kill us with CTRL-C here, if you like */
3184#endif
3185
3186 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003187 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003188 mch_errmsg(_(main_errors[n]));
3189 if (str != NULL)
3190 {
3191 mch_errmsg(": \"");
3192 mch_errmsg((char *)str);
3193 mch_errmsg("\"");
3194 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003195 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003196
3197 mch_exit(1);
3198}
3199
3200 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003201mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202{
3203 mainerr(ME_ARG_MISSING, str);
3204}
3205
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003206#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003207/*
3208 * print a message with three spaces prepended and '\n' appended.
3209 */
3210 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003211main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003212{
3213 mch_msg(" ");
3214 mch_msg(s);
3215 mch_msg("\n");
3216}
3217
3218/*
3219 * Print messages for "vim -h" or "vim --help" and exit.
3220 */
3221 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003222usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003223{
3224 int i;
3225 static char *(use[]) =
3226 {
3227 N_("[file ..] edit specified file(s)"),
3228 N_("- read text from stdin"),
3229 N_("-t tag edit file where tag is defined"),
3230#ifdef FEAT_QUICKFIX
3231 N_("-q [errorfile] edit file with first error")
3232#endif
3233 };
3234
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003235#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003236 reset_signals(); /* kill us with CTRL-C here, if you like */
3237#endif
3238
3239 mch_msg(longVersion);
3240 mch_msg(_("\n\nusage:"));
3241 for (i = 0; ; ++i)
3242 {
3243 mch_msg(_(" vim [arguments] "));
3244 mch_msg(_(use[i]));
3245 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3246 break;
3247 mch_msg(_("\n or:"));
3248 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003249#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003250 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003251#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003252
3253 mch_msg(_("\n\nArguments:\n"));
3254 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003255#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256 main_msg(_("--literal\t\tDon't expand wildcards"));
3257#endif
3258#ifdef FEAT_OLE
3259 main_msg(_("-register\t\tRegister this gvim for OLE"));
3260 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3261#endif
3262#ifdef FEAT_GUI
3263 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3264 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3265#endif
3266 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3267 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003268 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003269 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3270#ifdef FEAT_DIFF
3271 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3272#endif
3273 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3274 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3275 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3276 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3277 main_msg(_("-M\t\t\tModifications in text not allowed"));
3278 main_msg(_("-b\t\t\tBinary mode"));
3279#ifdef FEAT_LISP
3280 main_msg(_("-l\t\t\tLisp mode"));
3281#endif
3282 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3283 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003284 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3285#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003286 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003287#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003288 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3289 main_msg(_("-r\t\t\tList swap files and exit"));
3290 main_msg(_("-r (with file name)\tRecover crashed session"));
3291 main_msg(_("-L\t\t\tSame as -r"));
3292#ifdef AMIGA
3293 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3294 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3295#endif
3296#ifdef FEAT_ARABIC
3297 main_msg(_("-A\t\t\tstart in Arabic mode"));
3298#endif
3299#ifdef FEAT_RIGHTLEFT
3300 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3301#endif
3302#ifdef FEAT_FKMAP
3303 main_msg(_("-F\t\t\tStart in Farsi mode"));
3304#endif
3305 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003306 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003307 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003308 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3309#ifdef FEAT_GUI
3310 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3311#endif
3312 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003313#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003314 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003315 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3316 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003317#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 main_msg(_("+\t\t\tStart at end of file"));
3319 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003320 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003321 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3322 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3323 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3324 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3325 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3326#ifdef FEAT_CRYPT
3327 main_msg(_("-x\t\t\tEdit encrypted files"));
3328#endif
3329#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3330# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3331 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3332# endif
3333 main_msg(_("-X\t\t\tDo not connect to X server"));
3334#endif
3335#ifdef FEAT_CLIENTSERVER
3336 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3337 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3338 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3339 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003340# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003341 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003342# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003343 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3344 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3345 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3346 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3347#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003348#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003349 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003350#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003351#ifdef FEAT_VIMINFO
3352 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3353#endif
3354 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3355 main_msg(_("--version\t\tPrint version information and exit"));
3356
3357#ifdef FEAT_GUI_X11
3358# ifdef FEAT_GUI_MOTIF
3359 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3360# else
3361# ifdef FEAT_GUI_ATHENA
3362# ifdef FEAT_GUI_NEXTAW
3363 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3364# else
3365 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3366# endif
3367# endif
3368# endif
3369 main_msg(_("-display <display>\tRun vim on <display>"));
3370 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003371 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3372 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3373 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3374 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3375 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3376 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3377 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3378 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3379# ifdef FEAT_GUI_ATHENA
3380 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3381# endif
3382 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3383 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3384 main_msg(_("-xrm <resource>\tSet the specified resource"));
3385#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386#ifdef FEAT_GUI_GTK
3387 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3388 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3389 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3390 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3391 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003392 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003394 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396#ifdef FEAT_GUI_W32
3397 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003398 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399#endif
3400
3401#ifdef FEAT_GUI_GNOME
3402 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3403 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003404 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003406 gui.dofork = FALSE;
3407 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408 else
3409#endif
3410 mch_exit(0);
3411}
3412
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003413#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003414/*
3415 * Check the result of the ATTENTION dialog:
3416 * When "Quit" selected, exit Vim.
3417 * When "Recover" selected, recover the file.
3418 */
3419 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003420check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421{
3422 if (swap_exists_action == SEA_QUIT)
3423 getout(1);
3424 handle_swap_exists(NULL);
3425}
3426#endif
3427
Bram Moolenaar08cab962017-03-04 14:37:18 +01003428#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003429
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003431static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432
3433static struct timeval prev_timeval;
3434
Bram Moolenaar3f269672009-11-03 11:11:11 +00003435# ifdef WIN3264
3436/*
3437 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3438 */
3439 static int
3440gettimeofday(struct timeval *tv, char *dummy)
3441{
3442 long t = clock();
3443 tv->tv_sec = t / CLOCKS_PER_SEC;
3444 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3445 return 0;
3446}
3447# endif
3448
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003449/*
3450 * Save the previous time before doing something that could nest.
3451 * set "*tv_rel" to the time elapsed so far.
3452 */
3453 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003454time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003455{
3456 *((struct timeval *)tv_rel) = prev_timeval;
3457 gettimeofday(&prev_timeval, NULL);
3458 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3459 - ((struct timeval *)tv_rel)->tv_usec;
3460 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3461 - ((struct timeval *)tv_rel)->tv_sec;
3462 if (((struct timeval *)tv_rel)->tv_usec < 0)
3463 {
3464 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3465 --((struct timeval *)tv_rel)->tv_sec;
3466 }
3467 *(struct timeval *)tv_start = prev_timeval;
3468}
3469
3470/*
3471 * Compute the previous time after doing something that could nest.
3472 * Subtract "*tp" from prev_timeval;
3473 * Note: The arguments are (void *) to avoid trouble with systems that don't
3474 * have struct timeval.
3475 */
3476 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003477time_pop(
3478 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003479{
3480 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3481 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3482 if (prev_timeval.tv_usec < 0)
3483 {
3484 prev_timeval.tv_usec += 1000000;
3485 --prev_timeval.tv_sec;
3486 }
3487}
3488
3489 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003490time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003491{
3492 long usec;
3493 long msec;
3494
3495 usec = now->tv_usec - then->tv_usec;
3496 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3497 usec = usec % 1000L;
3498 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3499}
3500
3501 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003502time_msg(
3503 char *mesg,
3504 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003505 (struct timeval *) */
3506{
3507 static struct timeval start;
3508 struct timeval now;
3509
3510 if (time_fd != NULL)
3511 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003512 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003513 {
3514 gettimeofday(&start, NULL);
3515 prev_timeval = start;
3516 fprintf(time_fd, "\n\ntimes in msec\n");
3517 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3518 fprintf(time_fd, " clock elapsed: other lines\n\n");
3519 }
3520 gettimeofday(&now, NULL);
3521 time_diff(&start, &now);
3522 if (((struct timeval *)tv_start) != NULL)
3523 {
3524 fprintf(time_fd, " ");
3525 time_diff(((struct timeval *)tv_start), &now);
3526 }
3527 fprintf(time_fd, " ");
3528 time_diff(&prev_timeval, &now);
3529 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003530 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003531 }
3532}
3533
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003534#endif
3535
Bram Moolenaar595297d2017-03-04 19:11:12 +01003536#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003537 static void
3538set_progpath(char_u *argv0)
3539{
3540 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003541
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003542# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003543 char buf[PATH_MAX + 1];
3544 ssize_t len;
3545
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003546 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003547 if (len > 0)
3548 {
3549 buf[len] = NUL;
3550 val = (char_u *)buf;
3551 }
3552# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003553 /* A relative path containing a "/" will become invalid when using ":cd",
3554 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003555 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3556 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003557# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003558 char_u *path = NULL;
3559
3560 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3561 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003562# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003563 char_u buf[MAXPATHL];
3564
Bram Moolenaar43663192017-03-05 14:29:12 +01003565 if (!mch_isFullName(argv0))
3566 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003567 if (gettail(argv0) != argv0
3568 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3569 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003570 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003571# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003572# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003573
Bram Moolenaar08cab962017-03-04 14:37:18 +01003574 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003575
Bram Moolenaar066029e2017-03-05 15:19:32 +01003576# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003577 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003578# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003579}
3580
3581#endif /* NO_VIM_MAIN */
3582
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003583#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003584
3585/*
3586 * Common code for the X command server and the Win32 command server.
3587 */
3588
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003589static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003590
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003591/*
3592 * Do the client-server stuff, unless "--servername ''" was used.
3593 */
3594 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003595exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003596{
3597 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3598 {
3599# ifdef WIN32
3600 /* Initialise the client/server messaging infrastructure. */
3601 serverInitMessaging();
3602# endif
3603
3604 /*
3605 * When a command server argument was found, execute it. This may
3606 * exit Vim when it was successful. Otherwise it's executed further
3607 * on. Remember the encoding used here in "serverStrEnc".
3608 */
3609 if (parmp->serverArg)
3610 {
3611 cmdsrv_main(&parmp->argc, parmp->argv,
3612 parmp->serverName_arg, &parmp->serverStr);
3613# ifdef FEAT_MBYTE
3614 parmp->serverStrEnc = vim_strsave(p_enc);
3615# endif
3616 }
3617
3618 /* If we're still running, get the name to register ourselves.
3619 * On Win32 can register right now, for X11 need to setup the
3620 * clipboard first, it's further down. */
3621 parmp->servername = serverMakeName(parmp->serverName_arg,
3622 parmp->argv[0]);
3623# ifdef WIN32
3624 if (parmp->servername != NULL)
3625 {
3626 serverSetName(parmp->servername);
3627 vim_free(parmp->servername);
3628 }
3629# endif
3630 }
3631}
3632
3633/*
3634 * Prepare for running as a Vim server.
3635 */
3636 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003637prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003638{
3639# if defined(FEAT_X11)
3640 /*
3641 * Register for remote command execution with :serversend and --remote
3642 * unless there was a -X or a --servername '' on the command line.
3643 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003644 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003645 */
3646 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3647# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003648 (gui.in_use
3649# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003650 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003651# endif
3652 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003653# endif
3654 parmp->serverName_arg != NULL))
3655 {
3656 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3657 vim_free(parmp->servername);
3658 TIME_MSG("register server name");
3659 }
3660 else
3661 serverDelayedStartName = parmp->servername;
3662# endif
3663
3664 /*
3665 * Execute command ourselves if we're here because the send failed (or
3666 * else we would have exited above).
3667 */
3668 if (parmp->serverStr != NULL)
3669 {
3670 char_u *p;
3671
3672 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3673 parmp->serverStr, &p));
3674 vim_free(p);
3675 }
3676}
3677
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003678 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003679cmdsrv_main(
3680 int *argc,
3681 char **argv,
3682 char_u *serverName_arg,
3683 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003684{
3685 char_u *res;
3686 int i;
3687 char_u *sname;
3688 int ret;
3689 int didone = FALSE;
3690 int exiterr = 0;
3691 char **newArgV = argv + 1;
3692 int newArgC = 1,
3693 Argc = *argc;
3694 int argtype;
3695#define ARGTYPE_OTHER 0
3696#define ARGTYPE_EDIT 1
3697#define ARGTYPE_EDIT_WAIT 2
3698#define ARGTYPE_SEND 3
3699 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003700 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003701# ifndef FEAT_X11
3702 HWND srv;
3703# else
3704 Window srv;
3705
3706 setup_term_clip();
3707# endif
3708
3709 sname = serverMakeName(serverName_arg, argv[0]);
3710 if (sname == NULL)
3711 return;
3712
3713 /*
3714 * Execute the command server related arguments and remove them
3715 * from the argc/argv array; We may have to return into main()
3716 */
3717 for (i = 1; i < Argc; i++)
3718 {
3719 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003720 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003721 {
3722 for (; i < *argc; i++)
3723 {
3724 *newArgV++ = argv[i];
3725 newArgC++;
3726 }
3727 break;
3728 }
3729
Bram Moolenaareb94e552006-03-11 21:35:11 +00003730 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003731 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003732 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3733 {
3734 char *p = argv[i] + 8;
3735
3736 argtype = ARGTYPE_EDIT;
3737 while (*p != NUL)
3738 {
3739 if (STRNICMP(p, "-wait", 5) == 0)
3740 {
3741 argtype = ARGTYPE_EDIT_WAIT;
3742 p += 5;
3743 }
3744 else if (STRNICMP(p, "-silent", 7) == 0)
3745 {
3746 silent = TRUE;
3747 p += 7;
3748 }
3749 else if (STRNICMP(p, "-tab", 4) == 0)
3750 {
3751 tabs = TRUE;
3752 p += 4;
3753 }
3754 else
3755 {
3756 argtype = ARGTYPE_OTHER;
3757 break;
3758 }
3759 }
3760 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003761 else
3762 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003763
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003764 if (argtype != ARGTYPE_OTHER)
3765 {
3766 if (i == *argc - 1)
3767 mainerr_arg_missing((char_u *)argv[i]);
3768 if (argtype == ARGTYPE_SEND)
3769 {
3770 *serverStr = (char_u *)argv[i + 1];
3771 i++;
3772 }
3773 else
3774 {
3775 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003776 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003777 if (*serverStr == NULL)
3778 {
3779 /* Probably out of memory, exit. */
3780 didone = TRUE;
3781 exiterr = 1;
3782 break;
3783 }
3784 Argc = i;
3785 }
3786# ifdef FEAT_X11
3787 if (xterm_dpy == NULL)
3788 {
3789 mch_errmsg(_("No display"));
3790 ret = -1;
3791 }
3792 else
3793 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3794 NULL, &srv, 0, 0, silent);
3795# else
3796 /* Win32 always works? */
3797 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3798# endif
3799 if (ret < 0)
3800 {
3801 if (argtype == ARGTYPE_SEND)
3802 {
3803 /* Failed to send, abort. */
3804 mch_errmsg(_(": Send failed.\n"));
3805 didone = TRUE;
3806 exiterr = 1;
3807 }
3808 else if (!silent)
3809 /* Let vim start normally. */
3810 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3811 break;
3812 }
3813
3814# ifdef FEAT_GUI_W32
3815 /* Guess that when the server name starts with "g" it's a GUI
3816 * server, which we can bring to the foreground here.
3817 * Foreground() in the server doesn't work very well. */
3818 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3819 SetForegroundWindow(srv);
3820# endif
3821
3822 /*
3823 * For --remote-wait: Wait until the server did edit each
3824 * file. Also detect that the server no longer runs.
3825 */
3826 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3827 {
3828 int numFiles = *argc - i - 1;
3829 int j;
3830 char_u *done = alloc(numFiles);
3831 char_u *p;
3832# ifdef FEAT_GUI_W32
3833 NOTIFYICONDATA ni;
3834 int count = 0;
3835 extern HWND message_window;
3836# endif
3837
3838 if (numFiles > 0 && argv[i + 1][0] == '+')
3839 /* Skip "+cmd" argument, don't wait for it to be edited. */
3840 --numFiles;
3841
3842# ifdef FEAT_GUI_W32
3843 ni.cbSize = sizeof(ni);
3844 ni.hWnd = message_window;
3845 ni.uID = 0;
3846 ni.uFlags = NIF_ICON|NIF_TIP;
3847 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3848 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3849 Shell_NotifyIcon(NIM_ADD, &ni);
3850# endif
3851
3852 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003853 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003854 while (memchr(done, 0, numFiles) != NULL)
3855 {
3856# ifdef WIN32
3857 p = serverGetReply(srv, NULL, TRUE, TRUE);
3858 if (p == NULL)
3859 break;
3860# else
3861 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3862 break;
3863# endif
3864 j = atoi((char *)p);
3865 if (j >= 0 && j < numFiles)
3866 {
3867# ifdef FEAT_GUI_W32
3868 ++count;
3869 sprintf(ni.szTip, _("%d of %d edited"),
3870 count, numFiles);
3871 Shell_NotifyIcon(NIM_MODIFY, &ni);
3872# endif
3873 done[j] = 1;
3874 }
3875 }
3876# ifdef FEAT_GUI_W32
3877 Shell_NotifyIcon(NIM_DELETE, &ni);
3878# endif
3879 }
3880 }
3881 else if (STRICMP(argv[i], "--remote-expr") == 0)
3882 {
3883 if (i == *argc - 1)
3884 mainerr_arg_missing((char_u *)argv[i]);
3885# ifdef WIN32
3886 /* Win32 always works? */
3887 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3888 &res, NULL, 1, FALSE) < 0)
3889# else
3890 if (xterm_dpy == NULL)
3891 mch_errmsg(_("No display: Send expression failed.\n"));
3892 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3893 &res, NULL, 1, 1, FALSE) < 0)
3894# endif
3895 {
3896 if (res != NULL && *res != NUL)
3897 {
3898 /* Output error from remote */
3899 mch_errmsg((char *)res);
3900 vim_free(res);
3901 res = NULL;
3902 }
3903 mch_errmsg(_(": Send expression failed.\n"));
3904 }
3905 }
3906 else if (STRICMP(argv[i], "--serverlist") == 0)
3907 {
3908# ifdef WIN32
3909 /* Win32 always works? */
3910 res = serverGetVimNames();
3911# else
3912 if (xterm_dpy != NULL)
3913 res = serverGetVimNames(xterm_dpy);
3914# endif
3915 if (called_emsg)
3916 mch_errmsg("\n");
3917 }
3918 else if (STRICMP(argv[i], "--servername") == 0)
3919 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003920 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003921 i++;
3922 continue;
3923 }
3924 else
3925 {
3926 *newArgV++ = argv[i];
3927 newArgC++;
3928 continue;
3929 }
3930 didone = TRUE;
3931 if (res != NULL && *res != NUL)
3932 {
3933 mch_msg((char *)res);
3934 if (res[STRLEN(res) - 1] != '\n')
3935 mch_msg("\n");
3936 }
3937 vim_free(res);
3938 }
3939
3940 if (didone)
3941 {
3942 display_errors(); /* display any collected messages */
3943 exit(exiterr); /* Mission accomplished - get out */
3944 }
3945
3946 /* Return back into main() */
3947 *argc = newArgC;
3948 vim_free(sname);
3949}
3950
3951/*
3952 * Build a ":drop" command to send to a Vim server.
3953 */
3954 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003955build_drop_cmd(
3956 int filec,
3957 char **filev,
3958 int tabs, /* Use ":tab drop" instead of ":drop". */
3959 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003960{
3961 garray_T ga;
3962 int i;
3963 char_u *inicmd = NULL;
3964 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003965 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003966 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003967
3968 if (filec > 0 && filev[0][0] == '+')
3969 {
3970 inicmd = (char_u *)filev[0] + 1;
3971 filev++;
3972 filec--;
3973 }
3974 /* Check if we have at least one argument. */
3975 if (filec <= 0)
3976 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003977
3978 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003979 cwd = alloc(MAXPATHL);
3980 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003981 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003982 if (mch_dirname(cwd, MAXPATHL) != OK)
3983 {
3984 vim_free(cwd);
3985 return NULL;
3986 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003987 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003988#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003989 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003990#else
3991 PATH_ESC_CHARS,
3992#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003993 '\\', TRUE);
3994 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003995 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003996 return NULL;
3997 ga_init2(&ga, 1, 100);
3998 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003999 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004000
4001 /* Call inputsave() so that a prompt for an encryption key works. */
4002 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4003 if (tabs)
4004 ga_concat(&ga, (char_u *)"tab ");
4005 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004006 for (i = 0; i < filec; i++)
4007 {
4008 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004009 * do it again in the Vim server. On MS-Windows only escape
4010 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004011 p = vim_strsave_escaped((char_u *)filev[i],
4012#ifdef UNIX
4013 PATH_ESC_CHARS
4014#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004015 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004016#endif
4017 );
4018 if (p == NULL)
4019 {
4020 vim_free(ga.ga_data);
4021 return NULL;
4022 }
4023 ga_concat(&ga, (char_u *)" ");
4024 ga_concat(&ga, p);
4025 vim_free(p);
4026 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004027 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4028
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004029 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4030 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004031 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4032
4033 /* Switch back to the correct current directory (prior to temporary path
4034 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004035 * correct after the :drop command. With line breaks and spaces:
4036 * if !exists('+acd') || !&acd
4037 * if haslocaldir()
4038 * cd -
4039 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004040 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004041 * cd -
4042 * endif
4043 * endif
4044 */
4045 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004046 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004047 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004048 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004049 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004050
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004051 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004052 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4053 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004054 if (inicmd != NULL)
4055 {
4056 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4057 * the following commands to be inserted as text. Use a "|",
4058 * hopefully "inicmd" does allow this... */
4059 ga_concat(&ga, inicmd);
4060 ga_concat(&ga, (char_u *)"|");
4061 }
4062 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4063 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004064 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004065 ga_append(&ga, NUL);
4066 return ga.ga_data;
4067}
4068
4069/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004070 * Make our basic server name: use the specified "arg" if given, otherwise use
4071 * the tail of the command "cmd" we were started with.
4072 * Return the name in allocated memory. This doesn't include a serial number.
4073 */
4074 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004075serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004076{
4077 char_u *p;
4078
4079 if (arg != NULL && *arg != NUL)
4080 p = vim_strsave_up(arg);
4081 else
4082 {
4083 p = vim_strsave_up(gettail((char_u *)cmd));
4084 /* Remove .exe or .bat from the name. */
4085 if (p != NULL && vim_strchr(p, '.') != NULL)
4086 *vim_strchr(p, '.') = NUL;
4087 }
4088 return p;
4089}
4090#endif /* FEAT_CLIENTSERVER */
4091
4092#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4093/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004094 * Replace termcodes such as <CR> and insert as key presses if there is room.
4095 */
4096 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004097server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004098{
4099 char_u *ptr = NULL;
4100 char_u *cpo_save = p_cpo;
4101
4102 /* Set 'cpoptions' the way we want it.
4103 * B set - backslashes are *not* treated specially
4104 * k set - keycodes are *not* reverse-engineered
4105 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004106 * The last but one parameter of replace_termcodes() is TRUE so that the
4107 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004108 */
4109 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004110 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004111 p_cpo = cpo_save;
4112
4113 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4114 {
4115 /*
4116 * Add the string to the input stream.
4117 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4118 *
4119 * First clear typed characters from the typeahead buffer, there could
4120 * be half a mapping there. Then append to the existing string, so
4121 * that multiple commands from a client are concatenated.
4122 */
4123 if (typebuf.tb_maplen < typebuf.tb_len)
4124 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4125 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4126
4127 /* Let input_available() know we inserted text in the typeahead
4128 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004129 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004130 }
4131 vim_free((char_u *)ptr);
4132}
4133
4134/*
4135 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004136 */
4137 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004138eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004139{
4140 char_u *res;
4141 int save_dbl = debug_break_level;
4142 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004143 void *fc;
4144
4145 /* Evaluate the expression at the toplevel, don't use variables local to
4146 * the calling function. */
4147 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004148
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004149 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4150 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004151 debug_break_level = -1;
4152 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004153 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4154 * to be typed. Do generate errors so that try/catch works. */
4155 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004156
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004157 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004158
4159 debug_break_level = save_dbl;
4160 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004161 --emsg_silent;
4162 if (emsg_silent < 0)
4163 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004164 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004165
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004166 /* A client can tell us to redraw, but not to display the cursor, so do
4167 * that here. */
4168 setcursor();
4169 out_flush();
4170#ifdef FEAT_GUI
4171 if (gui.in_use)
4172 gui_update_cursor(FALSE, FALSE);
4173#endif
4174
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004175 return res;
4176}
4177
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004178/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004179 * Evaluate a command or expression sent to ourselves.
4180 */
4181 int
4182sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4183{
4184 if (asExpr)
4185 {
4186 char_u *ret;
4187
4188 ret = eval_client_expr_to_string(cmd);
4189 if (result != NULL)
4190 {
4191 if (ret == NULL)
4192 {
4193 char *err = _(e_invexprmsg);
4194 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4195 char_u *msg;
4196
4197 msg = alloc(len);
4198 if (msg != NULL)
4199 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4200 *result = msg;
4201 }
4202 else
4203 *result = ret;
4204 }
4205 else
4206 vim_free(ret);
4207 return ret == NULL ? -1 : 0;
4208 }
4209 server_to_input_buf(cmd);
4210 return 0;
4211}
4212
4213/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004214 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4215 * return an allocated string. Otherwise return "data".
4216 * "*tofree" is set to the result when it needs to be freed later.
4217 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004218 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004219serverConvert(
4220 char_u *client_enc UNUSED,
4221 char_u *data,
4222 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004223{
4224 char_u *res = data;
4225
4226 *tofree = NULL;
4227# ifdef FEAT_MBYTE
4228 if (client_enc != NULL && p_enc != NULL)
4229 {
4230 vimconv_T vimconv;
4231
4232 vimconv.vc_type = CONV_NONE;
4233 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4234 && vimconv.vc_type != CONV_NONE)
4235 {
4236 res = string_convert(&vimconv, data, NULL);
4237 if (res == NULL)
4238 res = data;
4239 else
4240 *tofree = res;
4241 }
4242 convert_setup(&vimconv, NULL, NULL);
4243 }
4244# endif
4245 return res;
4246}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004247#endif