blob: 0c7aa98462eb8b541af9b438333d32afb176290f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
14# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
16# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
17 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000018# endif
19# include <limits.h>
20#endif
21
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020022#if defined(WIN3264) && !defined(FEAT_GUI_W32)
23# include "iscygpty.h"
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Values for edit_type. */
27#define EDIT_NONE 0 /* no edit type yet */
28#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
29#define EDIT_STDIN 2 /* read file from stdin */
30#define EDIT_TAG 3 /* tag name argument given, use tagname */
31#define EDIT_QF 4 /* start in quickfix mode */
32
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010033#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000035#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020037# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
38static void init_locale(void);
39# endif
40static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010041#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void main_msg(char *s);
43static void usage(void);
44static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void command_line_scan(mparm_T *parmp);
47static void check_tty(mparm_T *parmp);
48static void read_stdin(void);
49static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010050# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010051static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010052# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010053static void exe_pre_commands(mparm_T *parmp);
54static void exe_commands(mparm_T *parmp);
55static void source_startup_scripts(mparm_T *parmp);
56static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010057# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010058static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010059# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +010060# ifdef FEAT_EVAL
61static void set_progpath(char_u *argv0);
62# endif
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010063# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010064static void exec_on_server(mparm_T *parmp);
65static void prepare_server(mparm_T *parmp);
66static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
67static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010068# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000069#endif
70
71
Bram Moolenaarb4210b32004-06-13 14:51:16 +000072/*
73 * Different types of error messages.
74 */
75static char *(main_errors[]) =
76{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000077 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000078#define ME_UNKNOWN_OPTION 0
79 N_("Too many edit arguments"),
80#define ME_TOO_MANY_ARGS 1
81 N_("Argument missing after"),
82#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000083 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000084#define ME_GARBAGE 3
85 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
86#define ME_EXTRA_CMD 4
87 N_("Invalid argument for"),
88#define ME_INVALID_ARG 5
89};
90
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010091#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaara6044292017-04-02 18:19:53 +020092
93/* Various parameters passed between main() and other functions. */
94static mparm_T params;
95
Bram Moolenaar8866d272012-11-28 15:55:42 +010096#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020097
98static char_u *start_dir = NULL; /* current working dir on startup */
99
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +0200100static int has_dash_c_arg = FALSE;
101
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000102 int
103# ifdef VIMDLL
104_export
105# endif
106# ifdef FEAT_GUI_MSWIN
107# ifdef __BORLANDC__
108_cdecl
109# endif
110VimMain
111# else
112main
113# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100114(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000115{
Bram Moolenaar3f269672009-11-03 11:11:11 +0000116#ifdef STARTUPTIME
117 int i;
118#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000119
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120 /*
121 * Do any system-specific initialisations. These can NOT use IObuff or
122 * NameBuff. Thus emsg2() cannot be called!
123 */
124 mch_early_init();
125
Bram Moolenaar14993322014-09-09 12:25:33 +0200126#if defined(WIN32) && defined(FEAT_MBYTE)
127 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200128 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200129 * convert when 'encoding' changes. Get the unexpanded arguments.
130 */
131 argc = get_cmd_argsW(&argv);
132#endif
133
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 /* Many variables are in "params" so that we can pass them to invoked
135 * functions without a lot of arguments. "argc" and "argv" are also
136 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000137 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000138 params.argc = argc;
139 params.argv = argv;
140 params.want_full_screen = TRUE;
141#ifdef FEAT_EVAL
142 params.use_debug_break_level = -1;
143#endif
144#ifdef FEAT_WINDOWS
145 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000146#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000147
Bram Moolenaar99685e62013-05-11 13:56:18 +0200148#ifdef FEAT_RUBY
149 {
150 int ruby_stack_start;
151 vim_ruby_init((void *)&ruby_stack_start);
152 }
153#endif
154
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000155#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000156 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000157#endif
158
159#ifdef MEM_PROFILE
160 atexit(vim_mem_profile_dump);
161#endif
162
163#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200164 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000165 for (i = 1; i < argc; ++i)
166 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000167 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000168 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000169 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000170 TIME_MSG("--- VIM STARTING ---");
171 break;
172 }
173 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000175 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200177 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178
179#ifdef FEAT_CLIENTSERVER
180 /*
181 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000182 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000183 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000184 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000185#endif
186
187 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000188 * Figure out the way to work from the command name argv[0].
189 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000190 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000192
193 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000194 * Process the command line arguments. File names are put in the global
195 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000196 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000198 TIME_MSG("parsing arguments");
199
200 /*
201 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000202 * there is no terminal version, and on Windows we can't fork one off with
203 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000204 */
205#ifdef ALWAYS_USE_GUI
206 gui.starting = TRUE;
207#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000208# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000209 /*
210 * Check if the GUI can be started. Reset gui.starting if not.
211 * Don't know about other systems, stay on the safe side and don't check.
212 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000213 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000215 if (gui_init_check() == FAIL)
216 {
217 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000219 /* When running "evim" or "gvim -y" we need the menus, exit if we
220 * don't have them. */
221 if (params.evim_mode)
222 mch_exit(1);
223 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 }
225# endif
226#endif
227
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000228 if (GARGCOUNT > 0)
229 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100230#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231 /*
232 * Expand wildcards in file names.
233 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000234 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200236 start_dir = alloc(MAXPATHL);
237 if (start_dir != NULL)
238 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 /* Temporarily add '(' and ')' to 'isfname'. These are valid
240 * filename characters but are excluded from 'isfname' to make
241 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
242 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000243 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200245 if (start_dir != NULL)
246 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000247 }
248#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200249 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000251
252#if defined(WIN32) && defined(FEAT_MBYTE)
253 {
254 extern void set_alist_count(void);
255
256 /* Remember the number of entries in the argument list. If it changes
257 * we don't react on setting 'encoding'. */
258 set_alist_count();
259 }
260#endif
261
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000263 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000264 {
265 /*
266 * If there is one filename, fully qualified, we have very probably
267 * been invoked from explorer, so change to the file's directory.
268 * Hint: to avoid this when typing a command use a forward slash.
269 * If the cd fails, it doesn't matter.
270 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200271 (void)vim_chdirfile(params.fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200272 if (start_dir != NULL)
273 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000274 }
275#endif
276 TIME_MSG("expanding arguments");
277
278#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000279 if (params.diff_mode && params.window_count == -1)
280 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000281#endif
282
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000283 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000284 ++RedrawingDisabled;
285
286 /*
287 * When listing swap file names, don't do cursor positioning et. al.
288 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200289 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000290 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291
292 /*
293 * When certain to start the GUI, don't check capabilities of terminal.
294 * For GTK we can't be sure, but when started from the desktop it doesn't
295 * make sense to try using a terminal.
296 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000297#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 if (gui.starting
299# ifdef FEAT_GUI_GTK
300 && !isatty(2)
301# endif
302 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000303 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000304#endif
305
306#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
307 /* When the GUI is started from Finder, need to display messages in a
308 * message box. isatty(2) returns TRUE anyway, thus we need to check the
309 * name to know we're not started from a terminal. */
310 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000311 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000312 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000313
314 /* Avoid always using "/" as the current directory. Note that when
315 * started from Finder the arglist will be filled later in
316 * HandleODocAE() and "fname" will be NULL. */
317 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
318 && STRCMP(NameBuff, "/") == 0)
319 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200320 if (params.fname != NULL)
321 (void)vim_chdirfile(params.fname);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000322 else
323 {
324 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
325 vim_chdir(NameBuff);
326 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200327 if (start_dir != NULL)
328 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000329 }
330 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331#endif
332
333 /*
334 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100335 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336 * Note that we may use mch_exit() before mch_init()!
337 */
338 mch_init();
339 TIME_MSG("shell init");
340
341#ifdef USE_XSMP
342 /*
343 * For want of anywhere else to do it, try to connect to xsmp here.
344 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
345 * Hijacking -X 'no X connection' to also disable XSMP connection as that
346 * has a similar delay upon failure.
347 * Only try if SESSION_MANAGER is set to something non-null.
348 */
349 if (!x_no_connect)
350 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000351 char *p = getenv("SESSION_MANAGER");
352
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353 if (p != NULL && *p != NUL)
354 {
355 xsmp_init();
356 TIME_MSG("xsmp init");
357 }
358 }
359#endif
360
361 /*
362 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000363 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000366 /* This message comes before term inits, but after setting "silent_mode"
367 * when the input is not a tty. */
368 if (GARGCOUNT > 1 && !silent_mode)
369 printf(_("%d files to edit\n"), GARGCOUNT);
370
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000371 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000373 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000374 capabilities (will set full_screen) */
375 screen_start(); /* don't know where cursor is now */
376 TIME_MSG("Termcap init");
377 }
378
379 /*
380 * Set the default values for the options that use Rows and Columns.
381 */
382 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000383 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384#ifdef FEAT_DIFF
385 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
386 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000387 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388 diff_win_options(firstwin, FALSE);
389#endif
390
391 cmdline_row = Rows - p_ch;
392 msg_row = cmdline_row;
393 screenalloc(FALSE); /* allocate screen buffers */
394 set_init_2();
395 TIME_MSG("inits 2");
396
397 msg_scroll = TRUE;
398 no_wait_return = TRUE;
399
400 init_mappings(); /* set up initial mappings */
401
402 init_highlight(TRUE, FALSE); /* set the default highlight groups */
403 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404
405#ifdef FEAT_EVAL
406 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000407 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408#endif
409
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100410#ifdef FEAT_MZSCHEME
411 /*
412 * Newer version of MzScheme (Racket) require earlier (trampolined)
413 * initialisation via scheme_main_setup.
414 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200415 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100416 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200417 return mzscheme_main();
418#else
419 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100420#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200421}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100422#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200423#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100424
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200425/*
426 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
427 * things simple.
428 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
429 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100430 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200431vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100432{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100433#ifndef NO_VIM_MAIN
Bram Moolenaar66459b72016-08-06 19:01:55 +0200434 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
435 * Allows for setting 'loadplugins' there. */
436 if (params.use_vimrc != NULL && STRCMP(params.use_vimrc, "NONE") == 0)
437 p_lpl = FALSE;
438
Bram Moolenaar58d98232005-07-23 22:25:46 +0000439 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000440 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000441
Bram Moolenaar58d98232005-07-23 22:25:46 +0000442 /* Source startup scripts. */
443 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444
445#ifdef FEAT_EVAL
446 /*
447 * Read all the plugin files.
448 * Only when compiled with +eval, since most plugins need it.
449 */
450 if (p_lpl)
451 {
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200452 /* First add all package directories to 'runtimepath', so that their
453 * autoload directories can be found. Only if not done already with a
454 * :packloadall command. */
455 if (!did_source_packages)
456 add_pack_start_dirs();
457
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000458# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar66459b72016-08-06 19:01:55 +0200459 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000460# else
Bram Moolenaar66459b72016-08-06 19:01:55 +0200461 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000462# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000463 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100464
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200465 /* Only source "start" packages if not done already with a :packloadall
466 * command. */
467 if (!did_source_packages)
468 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100469 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200470
471# ifdef VMS /* Somehow VMS doesn't handle the "**". */
472 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
473# else
474 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
475# endif
476 TIME_MSG("loading after plugins");
477
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000478 }
479#endif
480
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000481#ifdef FEAT_DIFF
482 /* Decide about window layout for diff mode after reading vimrc. */
483 if (params.diff_mode && params.window_layout == 0)
484 {
485 if (diffopt_horizontal())
486 params.window_layout = WIN_HOR; /* use horizontal split */
487 else
488 params.window_layout = WIN_VER; /* use vertical split */
489 }
490#endif
491
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000492 /*
493 * Recovery mode without a file name: List swap files.
494 * This uses the 'dir' option, therefore it must be after the
495 * initializations.
496 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200497 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000498 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200499 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000500 mch_exit(0);
501 }
502
503 /*
504 * Set a few option defaults after reading .vimrc files:
505 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
506 */
507 set_init_3();
508 TIME_MSG("inits 3");
509
510 /*
511 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
512 * Note that this overrides anything from a vimrc file.
513 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000514 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000515 p_uc = 0;
516
517#ifdef FEAT_FKMAP
518 if (curwin->w_p_rl && p_altkeymap)
519 {
520 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
521# ifdef FEAT_ARABIC
522 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
523# endif
524 p_fkmap = TRUE; /* Set the Farsi keymap mode */
525 }
526#endif
527
528#ifdef FEAT_GUI
529 if (gui.starting)
530 {
531#if defined(UNIX) || defined(VMS)
532 /* When something caused a message from a vimrc script, need to output
533 * an extra newline before the shell prompt. */
534 if (did_emsg || msg_didout)
535 putchar('\n');
536#endif
537
538 gui_start(); /* will set full_screen to TRUE */
539 TIME_MSG("starting GUI");
540
541 /* When running "evim" or "gvim -y" we need the menus, exit if we
542 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000543 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000544 mch_exit(1);
545 }
546#endif
547
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548#ifdef FEAT_VIMINFO
549 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000550 * Read in registers, history etc, but not marks, from the viminfo file.
551 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000552 */
553 if (*p_viminfo != NUL)
554 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000555 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556 TIME_MSG("reading viminfo");
557 }
558#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100559#ifdef FEAT_EVAL
560 /* It's better to make v:oldfiles an empty list than NULL. */
561 if (get_vim_var_list(VV_OLDFILES) == NULL)
562 set_vim_var_list(VV_OLDFILES, list_alloc());
563#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564
565#ifdef FEAT_QUICKFIX
566 /*
567 * "-q errorfile": Load the error file now.
568 * If the error file can't be read, exit before doing anything else.
569 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000570 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000571 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100572 char_u *enc = NULL;
573
574# ifdef FEAT_MBYTE
575 enc = p_menc;
576# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000577 if (params.use_ef != NULL)
578 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000579 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200580 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100581 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000582 {
583 out_char('\n');
584 mch_exit(3);
585 }
586 TIME_MSG("reading errorfile");
587 }
588#endif
589
590 /*
591 * Start putting things on the screen.
592 * Scroll screen down before drawing over it
593 * Clear screen now, so file message will not be cleared.
594 */
595 starting = NO_BUFFERS;
596 no_wait_return = FALSE;
597 if (!exmode_active)
598 msg_scroll = FALSE;
599
600#ifdef FEAT_GUI
601 /*
602 * This seems to be required to make callbacks to be called now, instead
603 * of after things have been put on the screen, which then may be deleted
604 * when getting a resize callback.
605 * For the Mac this handles putting files dropped on the Vim icon to
606 * global_alist.
607 */
608 if (gui.in_use)
609 {
610# ifdef FEAT_SUN_WORKSHOP
611 if (!usingSunWorkShop)
612# endif
613 gui_wait_for_chars(50L);
614 TIME_MSG("GUI delay");
615 }
616#endif
617
618#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
619 qnx_clip_init();
620#endif
621
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200622#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
623 clip_init(TRUE);
624#endif
625
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000626#ifdef FEAT_XCLIPBOARD
627 /* Start using the X clipboard, unless the GUI was started. */
628# ifdef FEAT_GUI
629 if (!gui.in_use)
630# endif
631 {
632 setup_term_clip();
633 TIME_MSG("setup clipboard");
634 }
635#endif
636
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000637#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000638 /* Prepare for being a Vim server. */
639 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000640#endif
641
642 /*
643 * If "-" argument given: Read file from stdin.
644 * Do this before starting Raw mode, because it may change things that the
645 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
646 * are the same terminal: "cat | vim -".
647 * Using autocommands here may cause trouble...
648 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000649 if (params.edit_type == EDIT_STDIN && !recoverymode)
650 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000651
652#if defined(UNIX) || defined(VMS)
653 /* When switching screens and something caused a message from a vimrc
654 * script, need to output an extra newline on exit. */
655 if ((did_emsg || msg_didout) && *T_TI != NUL)
656 newline_on_exit = TRUE;
657#endif
658
659 /*
660 * When done something that is not allowed or error message call
661 * wait_return. This must be done before starttermcap(), because it may
662 * switch to another screen. It must be done after settmode(TMODE_RAW),
663 * because we want to react on a single key stroke.
664 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000665 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000666 */
667 settmode(TMODE_RAW);
668 TIME_MSG("setting raw mode");
669
670 if (need_wait_return || msg_didany)
671 {
672 wait_return(TRUE);
673 TIME_MSG("waiting for return");
674 }
675
676 starttermcap(); /* start termcap if not done by wait_return() */
677 TIME_MSG("start termcap");
678
679#ifdef FEAT_MOUSE
680 setmouse(); /* may start using the mouse */
681#endif
682 if (scroll_region)
683 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000684 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000685
686 /*
687 * Don't clear the screen when starting in Ex mode, unless using the GUI.
688 */
689 if (exmode_active
690#ifdef FEAT_GUI
691 && !gui.in_use
692#endif
693 )
694 must_redraw = CLEAR;
695 else
696 {
697 screenclear(); /* clear screen */
698 TIME_MSG("clearing screen");
699 }
700
701#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000702 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100704 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200705 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000706 TIME_MSG("getting crypt key");
707 }
708#endif
709
710 no_wait_return = TRUE;
711
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000713 * Create the requested number of windows and edit buffers in them.
714 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000716 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000717 TIME_MSG("opening buffers");
718
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000719#ifdef FEAT_EVAL
720 /* clear v:swapcommand */
721 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
722#endif
723
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 /* Ex starts at last line of the file */
725 if (exmode_active)
726 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
727
728#ifdef FEAT_AUTOCMD
729 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
730 TIME_MSG("BufEnter autocommands");
731#endif
732 setpcmark();
733
734#ifdef FEAT_QUICKFIX
735 /*
736 * When started with "-q errorfile" jump to first error now.
737 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000738 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000739 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000740 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000741 TIME_MSG("jump to first error");
742 }
743#endif
744
745#ifdef FEAT_WINDOWS
746 /*
747 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000748 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200750 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000751#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200752 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753
754#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000755 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000756 {
757 win_T *wp;
758
759 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200760 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000761 diff_win_options(wp, TRUE);
762 }
763#endif
764
765 /*
766 * Shorten any of the filenames, but only when absolute.
767 */
768 shorten_fnames(FALSE);
769
770 /*
771 * Need to jump to the tag before executing the '-c command'.
772 * Makes "vim -c '/return' -t main" work.
773 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000774 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000775 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000776#if defined(HAS_SWAP_EXISTS_ACTION)
777 swap_exists_did_quit = FALSE;
778#endif
779
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000780 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000781 do_cmdline_cmd(IObuff);
782 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000783
784#if defined(HAS_SWAP_EXISTS_ACTION)
785 /* If the user doesn't want to edit the file then we quit here. */
786 if (swap_exists_did_quit)
787 getout(1);
788#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000789 }
790
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000791 /* Execute any "+", "-c" and "-S" arguments. */
792 if (params.n_commands > 0)
793 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794
Bram Moolenaar976787d2017-06-04 15:45:50 +0200795#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
796 /* Must be done before redrawing, puts a few characters on the screen. */
797 may_req_ambiguous_char_width();
798#endif
799
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800 RedrawingDisabled = 0;
801 redraw_all_later(NOT_VALID);
802 no_wait_return = FALSE;
803 starting = 0;
804
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200805 /* 'autochdir' has been postponed */
806 DO_AUTOCHDIR
807
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000808#ifdef FEAT_TERMRESPONSE
809 /* Requesting the termresponse is postponed until here, so that a "-c q"
810 * argument doesn't make it appear in the shell Vim was started from. */
811 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200812
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200813 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000814#endif
815
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000816 /* start in insert mode */
817 if (p_im)
818 need_start_insertmode = TRUE;
819
Bram Moolenaar14735512016-03-26 21:00:08 +0100820#ifdef FEAT_EVAL
821 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
822#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000823#ifdef FEAT_AUTOCMD
824 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
825 TIME_MSG("VimEnter autocommands");
826#endif
827
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200828#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
829 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
830 * done after the clipboard is available and all initial commands that may
831 * modify the 'clipboard' setting have run; i.e. just before entering the
832 * main loop. */
833 {
834 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100835
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200836 adjust_clip_reg(&default_regname);
837 set_reg_var(default_regname);
838 }
839#endif
840
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
842 /* When a startup script or session file setup for diff'ing and
843 * scrollbind, sync the scrollbind now. */
844 if (curwin->w_p_diff && curwin->w_p_scb)
845 {
846 update_topline();
847 check_scrollbind((linenr_T)0, 0L);
848 TIME_MSG("diff scrollbinding");
849 }
850#endif
851
852#if defined(WIN3264) && !defined(FEAT_GUI_W32)
853 mch_set_winsize_now(); /* Allow winsize changes from now on */
854#endif
855
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000856#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
857 /* When tab pages were created, may need to update the tab pages line and
858 * scrollbars. This is skipped while creating them. */
859 if (first_tabpage->tp_next != NULL)
860 {
861 out_flush();
862 gui_init_which_components(NULL);
863 gui_update_scrollbars(TRUE);
864 }
865 need_mouse_correct = TRUE;
866#endif
867
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 /* If ":startinsert" command used, stuff a dummy command to be able to
869 * call normal_cmd(), which will then start Insert mode. */
870 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000871 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000872
873#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200874 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200875 {
876# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200877# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200878 && !defined(FEAT_GUI_W32)
879 if (gui.in_use)
880 {
881 mch_errmsg(_("netbeans is not supported with this GUI\n"));
882 mch_exit(2);
883 }
884# endif
885# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200887 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200888 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889#endif
890
891 TIME_MSG("before starting main loop");
892
893 /*
894 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100895 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000896 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000897
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200898#endif /* NO_VIM_MAIN */
899
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000900 return 0;
901}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000902
903/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200904 * Initialisation shared by main() and some tests.
905 */
906 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200907common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200908{
909
910#ifdef FEAT_MBYTE
911 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
912#endif
913#ifdef FEAT_EVAL
914 eval_init(); /* init global variables */
915#endif
916
917#ifdef __QNXNTO__
918 qnx_init(); /* PhAttach() for clipboard, (and gui) */
919#endif
920
921#ifdef MAC_OS_CLASSIC
922 /* Prepare for possibly starting GUI sometime */
923 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200924 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200925 TIME_MSG("GUI prepared");
926#endif
927
928 /* Init the table of Normal mode commands. */
929 init_normal_cmds();
930
931#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
932 make_version(); /* Construct the long version string. */
933#endif
934
935 /*
936 * Allocate space for the generic buffers (needed for set_init_1() and
937 * EMSG2()).
938 */
939 if ((IObuff = alloc(IOSIZE)) == NULL
940 || (NameBuff = alloc(MAXPATHL)) == NULL)
941 mch_exit(0);
942 TIME_MSG("Allocated generic buffers");
943
944#ifdef NBDEBUG
945 /* Wait a moment for debugging NetBeans. Must be after allocating
946 * NameBuff. */
947 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
948 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
949 TIME_MSG("NetBeans debug wait");
950#endif
951
952#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
953 /*
954 * Setup to use the current locale (for ctype() and many other things).
955 * NOTE: Translated messages with encodings other than latin1 will not
956 * work until set_init_1() has been called!
957 */
958 init_locale();
959 TIME_MSG("locale set");
960#endif
961
962#ifdef FEAT_GUI
963 gui.dofork = TRUE; /* default is to use fork() */
964#endif
965
966 /*
967 * Do a first scan of the arguments in "argv[]":
968 * -display or --display
969 * --server...
970 * --socketid
971 * --windowid
972 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200973 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200974
975#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200976 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200977#endif
978#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
979 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200980 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200981 TIME_MSG("GUI prepared");
982#endif
983
984#ifdef FEAT_CLIPBOARD
985 clip_init(FALSE); /* Initialise clipboard stuff */
986 TIME_MSG("clipboard setup");
987#endif
988
989 /*
990 * Check if we have an interactive window.
991 * On the Amiga: If there is no window, we open one with a newcli command
992 * (needed for :! to * work). mch_check_win() will also handle the -d or
993 * -dev argument.
994 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100995 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200996 TIME_MSG("window checked");
997
998 /*
999 * Allocate the first window and buffer.
1000 * Can't do anything without it, exit when it fails.
1001 */
1002 if (win_alloc_first() == FAIL)
1003 mch_exit(0);
1004
1005 init_yank(); /* init yank buffers */
1006
1007 alist_init(&global_alist); /* Init the argument list to empty. */
1008 global_alist.id = 0;
1009
1010 /*
1011 * Set the default values for the options.
1012 * NOTE: Non-latin1 translated messages are working only after this,
1013 * because this is where "has_mbyte" will be set, which is used by
1014 * msg_outtrans_len_attr().
1015 * First find out the home directory, needed to expand "~" in options.
1016 */
1017 init_homedir(); /* find real value of $HOME */
1018 set_init_1();
1019 TIME_MSG("inits 1");
1020
1021#ifdef FEAT_EVAL
1022 set_lang_var(); /* set v:lang and v:ctype */
1023#endif
1024}
1025
1026/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001027 * Return TRUE when the --not-a-term argument was found.
1028 */
1029 int
1030is_not_a_term()
1031{
1032 return params.not_a_term;
1033}
1034
1035/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001036 * Main loop: Execute Normal mode commands until exiting Vim.
1037 * Also used to handle commands in the command-line window, until the window
1038 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 * Also used to handle ":visual" command after ":global": execute Normal mode
1040 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001041 */
1042 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001043main_loop(
1044 int cmdwin, /* TRUE when working in the command-line window */
1045 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001047 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001048 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001049#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001050 /* these are static to avoid a compiler warning */
1051 static linenr_T conceal_old_cursor_line = 0;
1052 static linenr_T conceal_new_cursor_line = 0;
1053 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001054#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001055
1056#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1057 /* Setup to catch a terminating error from the X server. Just ignore
1058 * it, restore the state and continue. This might not always work
1059 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001060 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001062 {
1063 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001064 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001065 got_int = TRUE;
1066 need_wait_return = FALSE;
1067 global_busy = FALSE;
1068 exmode_active = 0;
1069 skip_redraw = FALSE;
1070 RedrawingDisabled = 0;
1071 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001072 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001073# ifdef FEAT_EVAL
1074 emsg_skip = 0;
1075# endif
1076 emsg_off = 0;
1077# ifdef FEAT_MOUSE
1078 setmouse();
1079# endif
1080 settmode(TMODE_RAW);
1081 starttermcap();
1082 scroll_start();
1083 redraw_later_clear();
1084 }
1085#endif
1086
1087 clear_oparg(&oa);
1088 while (!cmdwin
1089#ifdef FEAT_CMDWIN
1090 || cmdwin_result == 0
1091#endif
1092 )
1093 {
1094 if (stuff_empty())
1095 {
1096 did_check_timestamps = FALSE;
1097 if (need_check_timestamps)
1098 check_timestamps(FALSE);
1099 if (need_wait_return) /* if wait_return still needed ... */
1100 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001101 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001102 {
1103 need_start_insertmode = FALSE;
1104 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1105 /* skip the fileinfo message now, because it would be shown
1106 * after insert mode finishes! */
1107 need_fileinfo = FALSE;
1108 }
1109 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001110
1111 /* Reset "got_int" now that we got back to the main loop. Except when
1112 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1113 * the ":g" command.
1114 * For ":g/pat/vi" we reset "got_int" when used once. When used
1115 * a second time we go back to Ex mode and abort the ":g" command. */
1116 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001117 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001118 if (noexmode && global_busy && !exmode_active && previous_got_int)
1119 {
1120 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1121 * used and keep "got_int" set, so that it aborts ":g". */
1122 exmode_active = EXMODE_NORMAL;
1123 State = NORMAL;
1124 }
1125 else if (!global_busy || !exmode_active)
1126 {
1127 if (!quit_more)
1128 (void)vgetc(); /* flush all buffers */
1129 got_int = FALSE;
1130 }
1131 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001132 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001133 else
1134 previous_got_int = FALSE;
1135
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001136 if (!exmode_active)
1137 msg_scroll = FALSE;
1138 quit_more = FALSE;
1139
1140 /*
1141 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1142 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1143 * update cursor and redraw.
1144 */
1145 if (skip_redraw || exmode_active)
1146 skip_redraw = FALSE;
1147 else if (do_redraw || stuff_empty())
1148 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001149# ifdef FEAT_GUI
1150 /* If ui_breakcheck() was used a resize may have been postponed. */
1151 gui_may_resize_shell();
1152# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001153#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001154 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001155 if (!finish_op && (
1156# ifdef FEAT_AUTOCMD
1157 has_cursormoved()
1158# endif
1159# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1160 ||
1161# endif
1162# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001163 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001164# endif
1165 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001166# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001167 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001168# endif
1169 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001170 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001171# ifdef FEAT_AUTOCMD
1172 if (has_cursormoved())
1173 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1174 FALSE, curbuf);
1175# endif
1176# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001177 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001178 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001179# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001180 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001181# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001182 conceal_new_cursor_line = curwin->w_cursor.lnum;
1183 conceal_update_lines = TRUE;
1184 }
1185# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001186# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001187 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001188# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001189 }
1190#endif
1191
Bram Moolenaar186628f2013-03-19 13:33:23 +01001192#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001193 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001194 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001195 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001196 {
1197 if (last_changedtick_buf == curbuf)
1198 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1199 FALSE, curbuf);
1200 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001201 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001202 }
1203#endif
1204
Bram Moolenaar33aec762006-01-22 23:30:12 +00001205#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1206 /* Scroll-binding for diff mode may have been postponed until
1207 * here. Avoids doing it for every change. */
1208 if (diff_need_scrollbind)
1209 {
1210 check_scrollbind((linenr_T)0, 0L);
1211 diff_need_scrollbind = FALSE;
1212 }
1213#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001214#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001215 /* Include a closed fold completely in the Visual area. */
1216 foldAdjustVisual();
1217#endif
1218#ifdef FEAT_FOLDING
1219 /*
1220 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1221 * contain the cursor.
1222 * When 'foldopen' is "all", open the fold(s) under the cursor.
1223 * This may mark the window for redrawing.
1224 */
1225 if (hasAnyFolding(curwin) && !char_avail())
1226 {
1227 foldCheckClose();
1228 if (fdo_flags & FDO_ALL)
1229 foldOpenCursor();
1230 }
1231#endif
1232
1233 /*
1234 * Before redrawing, make sure w_topline is correct, and w_leftcol
1235 * if lines don't wrap, and w_skipcol if lines wrap.
1236 */
1237 update_topline();
1238 validate_cursor();
1239
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001240 if (VIsual_active)
1241 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001242 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001243 update_screen(0);
1244 else if (redraw_cmdline || clear_cmdline)
1245 showmode();
1246#ifdef FEAT_WINDOWS
1247 redraw_statuslines();
1248#endif
1249#ifdef FEAT_TITLE
1250 if (need_maketitle)
1251 maketitle();
1252#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001253#ifdef FEAT_VIMINFO
1254 curbuf->b_last_used = vim_time();
1255#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001256 /* display message after redraw */
1257 if (keep_msg != NULL)
1258 {
1259 char_u *p;
1260
1261 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001262 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1263 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001264 p = keep_msg;
1265 msg_attr(p, keep_msg_attr);
1266 vim_free(p);
1267 }
1268 if (need_fileinfo) /* show file info after redraw */
1269 {
1270 fileinfo(FALSE, TRUE, FALSE);
1271 need_fileinfo = FALSE;
1272 }
1273
1274 emsg_on_display = FALSE; /* can delete error message now */
1275 did_emsg = FALSE;
1276 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001277 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001278 showruler(FALSE);
1279
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001280# if defined(FEAT_CONCEAL)
1281 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001282 && (conceal_old_cursor_line != conceal_new_cursor_line
1283 || conceal_cursor_line(curwin)
1284 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001285 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001286 if (conceal_old_cursor_line != conceal_new_cursor_line
1287 && conceal_old_cursor_line
1288 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001289 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001290 update_single_line(curwin, conceal_new_cursor_line);
1291 curwin->w_valid &= ~VALID_CROW;
1292 }
1293# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001294 setcursor();
1295 cursor_on();
1296
1297 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001298
1299#ifdef STARTUPTIME
1300 /* Now that we have drawn the first screen all the startup stuff
1301 * has been done, close any file for startup messages. */
1302 if (time_fd != NULL)
1303 {
1304 TIME_MSG("first screen update");
1305 TIME_MSG("--- VIM STARTED ---");
1306 fclose(time_fd);
1307 time_fd = NULL;
1308 }
1309#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001310 }
1311#ifdef FEAT_GUI
1312 if (need_mouse_correct)
1313 gui_mouse_correct();
1314#endif
1315
1316 /*
1317 * Update w_curswant if w_set_curswant has been set.
1318 * Postponed until here to avoid computing w_virtcol too often.
1319 */
1320 update_curswant();
1321
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001322#ifdef FEAT_EVAL
1323 /*
1324 * May perform garbage collection when waiting for a character, but
1325 * only at the very toplevel. Otherwise we may be using a List or
1326 * Dict internally somewhere.
1327 * "may_garbage_collect" is reset in vgetc() which is invoked through
1328 * do_exmode() and normal_cmd().
1329 */
1330 may_garbage_collect = (!cmdwin && !noexmode);
1331#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001332 /*
1333 * If we're invoked as ex, do a round of ex commands.
1334 * Otherwise, get and execute a normal mode command.
1335 */
1336 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001337 {
1338 if (noexmode) /* End of ":global/path/visual" commands */
1339 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001340 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001341 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 else
1343 normal_cmd(&oa, TRUE);
1344 }
1345}
1346
1347
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001348#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001349/*
1350 * Exit, but leave behind swap files for modified buffers.
1351 */
1352 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001353getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001354{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001355# if defined(SIGHUP) && defined(SIG_IGN)
1356 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1357 * makes Vim exit and then handling SIGHUP causes various reentrance
1358 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001359 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001360# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001361
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001362 ml_close_notmod(); /* close all not-modified buffers */
1363 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1364 ml_close_all(FALSE); /* close all memfiles, without deleting */
1365 getout(exitval); /* exit Vim properly */
1366}
1367#endif
1368
1369
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001370/*
1371 * Exit properly.
1372 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001373 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001374getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001375{
1376#ifdef FEAT_AUTOCMD
1377 buf_T *buf;
1378 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001379 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001380#endif
1381
1382 exiting = TRUE;
1383
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001384 /* When running in Ex mode an error causes us to exit with a non-zero exit
1385 * code. POSIX requires this, although it's not 100% clear from the
1386 * standard. */
1387 if (exmode_active)
1388 exitval += ex_exitval;
1389
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001390 /* Position the cursor on the last screen line, below all the text */
1391#ifdef FEAT_GUI
1392 if (!gui.in_use)
1393#endif
1394 windgoto((int)Rows - 1, 0);
1395
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001396#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1397 /* Optionally print hashtable efficiency. */
1398 hash_debug_results();
1399#endif
1400
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401#ifdef FEAT_GUI
1402 msg_didany = FALSE;
1403#endif
1404
1405#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001406 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001407 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001408 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1409# if defined FEAT_WINDOWS
1410 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001411 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001412 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001413 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001414 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001415 if (wp->w_buffer == NULL)
1416 /* Autocmd must have close the buffer already, skip. */
1417 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001418 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001419 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001420 {
1421 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1422 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001423 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001424 /* start all over, autocommands may mess up the lists */
1425 next_tp = first_tabpage;
1426 break;
1427 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001428 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001429 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001430# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001431 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1432 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001433# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001434
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001435 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001436 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001437 if (buf->b_ml.ml_mfp != NULL)
1438 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001439 bufref_T bufref;
1440
1441 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001442 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001443 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001444 if (!bufref_valid(&bufref))
1445 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001446 break;
1447 }
1448 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1449 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450#endif
1451
1452#ifdef FEAT_VIMINFO
1453 if (*p_viminfo != NUL)
1454 /* Write out the registers, history, marks etc, to the viminfo file */
1455 write_viminfo(NULL, FALSE);
1456#endif
1457
1458#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001459 if (get_vim_var_nr(VV_DYING) <= 1)
1460 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001461#endif
1462
Bram Moolenaar05159a02005-02-26 23:04:13 +00001463#ifdef FEAT_PROFILE
1464 profile_dump();
1465#endif
1466
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001467 if (did_emsg
1468#ifdef FEAT_GUI
1469 || (gui.in_use && msg_didany && p_verbose > 0)
1470#endif
1471 )
1472 {
1473 /* give the user a chance to read the (error) message */
1474 no_wait_return = FALSE;
1475 wait_return(FALSE);
1476 }
1477
1478#ifdef FEAT_AUTOCMD
1479 /* Position the cursor again, the autocommands may have moved it */
1480# ifdef FEAT_GUI
1481 if (!gui.in_use)
1482# endif
1483 windgoto((int)Rows - 1, 0);
1484#endif
1485
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001486#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001487 job_stop_on_exit();
1488#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001489#ifdef FEAT_LUA
1490 lua_end();
1491#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001492#ifdef FEAT_MZSCHEME
1493 mzscheme_end();
1494#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001495#ifdef FEAT_TCL
1496 tcl_end();
1497#endif
1498#ifdef FEAT_RUBY
1499 ruby_end();
1500#endif
1501#ifdef FEAT_PYTHON
1502 python_end();
1503#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001504#ifdef FEAT_PYTHON3
1505 python3_end();
1506#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001507#ifdef FEAT_PERL
1508 perl_end();
1509#endif
1510#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1511 iconv_end();
1512#endif
1513#ifdef FEAT_NETBEANS_INTG
1514 netbeans_end();
1515#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001516#ifdef FEAT_CSCOPE
1517 cs_end();
1518#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001519#ifdef FEAT_EVAL
1520 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001521 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001522#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001523#if defined(WIN32) && defined(FEAT_MBYTE)
1524 free_cmd_argsW();
1525#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001526
1527 mch_exit(exitval);
1528}
1529
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001530#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1531/*
1532 * Setup to use the current locale (for ctype() and many other things).
1533 */
1534 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001535init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001536{
1537 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001538
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001539# ifdef FEAT_GUI_GTK
1540 /* Tell Gtk not to change our locale settings. */
1541 gtk_disable_setlocale();
1542# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001543# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1544 /* Make sure strtod() uses a decimal point, not a comma. */
1545 setlocale(LC_NUMERIC, "C");
1546# endif
1547
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001548# ifdef WIN32
1549 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1550 * text while it's expecting text in the current locale. This call avoids
1551 * that. */
1552 setlocale(LC_CTYPE, "C");
1553# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001554
1555# ifdef FEAT_GETTEXT
1556 {
1557 int mustfree = FALSE;
1558 char_u *p;
1559
1560# ifdef DYNAMIC_GETTEXT
1561 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001562 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001563# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001564 /* expand_env() doesn't work yet, because g_chartab[] is not
1565 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001566 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1567 if (p != NULL && *p != NUL)
1568 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001569 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001570 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1571 }
1572 if (mustfree)
1573 vim_free(p);
1574 textdomain(VIMPACKAGE);
1575 }
1576# endif
1577}
1578#endif
1579
1580/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001581 * Get the name of the display, before gui_prepare() removes it from
1582 * argv[]. Used for the xterm-clipboard display.
1583 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001584 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001585 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001586 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001587early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001588{
Bram Moolenaar03005972008-11-20 13:12:36 +00001589#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1590 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001591 int argc = parmp->argc;
1592 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001593 int i;
1594
1595 for (i = 1; i < argc; i++)
1596 {
1597 if (STRCMP(argv[i], "--") == 0)
1598 break;
1599# ifdef FEAT_XCLIPBOARD
1600 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001601# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602 || STRICMP(argv[i], "--display") == 0
1603# endif
1604 )
1605 {
1606 if (i == argc - 1)
1607 mainerr_arg_missing((char_u *)argv[i]);
1608 xterm_display = argv[++i];
1609 }
1610# endif
1611# ifdef FEAT_CLIENTSERVER
1612 else if (STRICMP(argv[i], "--servername") == 0)
1613 {
1614 if (i == argc - 1)
1615 mainerr_arg_missing((char_u *)argv[i]);
1616 parmp->serverName_arg = (char_u *)argv[++i];
1617 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001618 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001619 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001620 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001621 {
1622 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001623# ifdef FEAT_GUI
1624 if (strstr(argv[i], "-wait") != 0)
1625 /* don't fork() when starting the GUI to edit files ourself */
1626 gui.dofork = FALSE;
1627# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001628 }
1629# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001630
1631# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1632# ifdef FEAT_GUI_W32
1633 else if (STRICMP(argv[i], "--windowid") == 0)
1634# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001635 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001636# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001637 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001638 long_u id;
1639 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001640
1641 if (i == argc - 1)
1642 mainerr_arg_missing((char_u *)argv[i]);
1643 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001644 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001645 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001646 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001647 if (count != 1)
1648 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1649 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001650# ifdef FEAT_GUI_W32
1651 win_socket_id = id;
1652# else
1653 gtk_socket_id = id;
1654# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001655 i++;
1656 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001657# endif
1658# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001659 else if (STRICMP(argv[i], "--echo-wid") == 0)
1660 echo_wid_arg = TRUE;
1661# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001662# ifndef FEAT_NETBEANS_INTG
1663 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001664 {
1665 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1666 mch_exit(2);
1667 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001668# endif
1669
Bram Moolenaar58d98232005-07-23 22:25:46 +00001670 }
1671#endif
1672}
1673
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001674#ifndef NO_VIM_MAIN
1675/*
1676 * Get a (optional) count for a Vim argument.
1677 */
1678 static int
1679get_number_arg(
1680 char_u *p, /* pointer to argument */
1681 int *idx, /* index in argument, is incremented */
1682 int def) /* default value */
1683{
1684 if (vim_isdigit(p[*idx]))
1685 {
1686 def = atoi((char *)&(p[*idx]));
1687 while (vim_isdigit(p[*idx]))
1688 *idx = *idx + 1;
1689 }
1690 return def;
1691}
1692
1693/*
1694 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1695 * If the executable name starts with "r" we disable shell commands.
1696 * If the next character is "e" we run in Easy mode.
1697 * If the next character is "g" we run the GUI version.
1698 * If the next characters are "view" we start in readonly mode.
1699 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1700 * If the next characters are "ex" we start in Ex mode. If it's followed
1701 * by "im" use improved Ex mode.
1702 */
1703 static void
1704parse_command_name(mparm_T *parmp)
1705{
1706 char_u *initstr;
1707
1708 initstr = gettail((char_u *)parmp->argv[0]);
1709
1710#ifdef MACOS_X_UNIX
1711 /* An issue has been seen when launching Vim in such a way that
1712 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1713 * executable or a symbolic link of it. Until this issue is resolved
1714 * we prohibit the GUI from being used.
1715 */
1716 if (STRCMP(initstr, parmp->argv[0]) == 0)
1717 disallow_gui = TRUE;
1718
1719 /* TODO: On MacOS X default to gui if argv[0] ends in:
1720 * /Vim.app/Contents/MacOS/Vim */
1721#endif
1722
1723#ifdef FEAT_EVAL
1724 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001725 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001726#endif
1727
1728 if (TOLOWER_ASC(initstr[0]) == 'r')
1729 {
1730 restricted = TRUE;
1731 ++initstr;
1732 }
1733
1734 /* Use evim mode for "evim" and "egvim", not for "editor". */
1735 if (TOLOWER_ASC(initstr[0]) == 'e'
1736 && (TOLOWER_ASC(initstr[1]) == 'v'
1737 || TOLOWER_ASC(initstr[1]) == 'g'))
1738 {
1739#ifdef FEAT_GUI
1740 gui.starting = TRUE;
1741#endif
1742 parmp->evim_mode = TRUE;
1743 ++initstr;
1744 }
1745
1746 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1747 if (TOLOWER_ASC(initstr[0]) == 'g')
1748 {
1749 main_start_gui();
1750#ifdef FEAT_GUI
1751 ++initstr;
1752#endif
1753 }
1754
1755 if (STRNICMP(initstr, "view", 4) == 0)
1756 {
1757 readonlymode = TRUE;
1758 curbuf->b_p_ro = TRUE;
1759 p_uc = 10000; /* don't update very often */
1760 initstr += 4;
1761 }
1762 else if (STRNICMP(initstr, "vim", 3) == 0)
1763 initstr += 3;
1764
1765 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1766 if (STRICMP(initstr, "diff") == 0)
1767 {
1768#ifdef FEAT_DIFF
1769 parmp->diff_mode = TRUE;
1770#else
1771 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1772 mch_errmsg("\n");
1773 mch_exit(2);
1774#endif
1775 }
1776
1777 if (STRNICMP(initstr, "ex", 2) == 0)
1778 {
1779 if (STRNICMP(initstr + 2, "im", 2) == 0)
1780 exmode_active = EXMODE_VIM;
1781 else
1782 exmode_active = EXMODE_NORMAL;
1783 change_compatible(TRUE); /* set 'compatible' */
1784 }
1785}
1786
Bram Moolenaar58d98232005-07-23 22:25:46 +00001787/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001788 * Scan the command line arguments.
1789 */
1790 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001791command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001792{
1793 int argc = parmp->argc;
1794 char **argv = parmp->argv;
1795 int argv_idx; /* index in argv[n][] */
1796 int had_minmin = FALSE; /* found "--" argument */
1797 int want_argument; /* option argument with argument */
1798 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001799 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001800 long n;
1801
1802 --argc;
1803 ++argv;
1804 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1805 while (argc > 0)
1806 {
1807 /*
1808 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1809 */
1810 if (argv[0][0] == '+' && !had_minmin)
1811 {
1812 if (parmp->n_commands >= MAX_ARG_CMDS)
1813 mainerr(ME_EXTRA_CMD, NULL);
1814 argv_idx = -1; /* skip to next argument */
1815 if (argv[0][1] == NUL)
1816 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1817 else
1818 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1819 }
1820
1821 /*
1822 * Optional argument.
1823 */
1824 else if (argv[0][0] == '-' && !had_minmin)
1825 {
1826 want_argument = FALSE;
1827 c = argv[0][argv_idx++];
1828#ifdef VMS
1829 /*
1830 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1831 * and "-/X" as "-X".
1832 */
1833 if (c == '/')
1834 {
1835 c = argv[0][argv_idx++];
1836 c = TOUPPER_ASC(c);
1837 }
1838 else
1839 c = TOLOWER_ASC(c);
1840#endif
1841 switch (c)
1842 {
1843 case NUL: /* "vim -" read from stdin */
1844 /* "ex -" silent mode */
1845 if (exmode_active)
1846 silent_mode = TRUE;
1847 else
1848 {
1849 if (parmp->edit_type != EDIT_NONE)
1850 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1851 parmp->edit_type = EDIT_STDIN;
1852 read_cmd_fd = 2; /* read from stderr instead of stdin */
1853 }
1854 argv_idx = -1; /* skip to next argument */
1855 break;
1856
1857 case '-': /* "--" don't take any more option arguments */
1858 /* "--help" give help message */
1859 /* "--version" give version message */
1860 /* "--literal" take files literally */
1861 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001862 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001863 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001864 /* "--noplugin[s]" skip plugins */
1865 /* "--cmd <cmd>" execute cmd before vimrc */
1866 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1867 usage();
1868 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1869 {
1870 Columns = 80; /* need to init Columns */
1871 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1872 list_version();
1873 msg_putchar('\n');
1874 msg_didout = FALSE;
1875 mch_exit(0);
1876 }
1877 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1878 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001879#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001880 parmp->literal = TRUE;
1881#endif
1882 }
1883 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1884 {
1885#ifdef FEAT_GUI
1886 gui.dofork = FALSE; /* don't fork() when starting GUI */
1887#endif
1888 }
1889 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1890 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001891 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1892 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001893 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1894 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001895 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1896 {
1897 want_argument = TRUE;
1898 argv_idx += 3;
1899 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001900 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1901 {
1902 want_argument = TRUE;
1903 argv_idx += 11;
1904 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001905#ifdef FEAT_CLIENTSERVER
1906 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1907 ; /* already processed -- no arg */
1908 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1909 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1910 {
1911 /* already processed -- snatch the following arg */
1912 if (argc > 1)
1913 {
1914 --argc;
1915 ++argv;
1916 }
1917 }
1918#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001919#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1920# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001921 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001922# else
1923 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1924# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001925 {
1926 /* already processed -- snatch the following arg */
1927 if (argc > 1)
1928 {
1929 --argc;
1930 ++argv;
1931 }
1932 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001933#endif
1934#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001935 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1936 {
1937 /* already processed, skip */
1938 }
1939#endif
1940 else
1941 {
1942 if (argv[0][argv_idx])
1943 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1944 had_minmin = TRUE;
1945 }
1946 if (!want_argument)
1947 argv_idx = -1; /* skip to next argument */
1948 break;
1949
1950 case 'A': /* "-A" start in Arabic mode */
1951#ifdef FEAT_ARABIC
1952 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1953#else
1954 mch_errmsg(_(e_noarabic));
1955 mch_exit(2);
1956#endif
1957 break;
1958
1959 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001960 /* Needs to be effective before expanding file names, because
1961 * for Win32 this makes us edit a shortcut file itself,
1962 * instead of the file it links to. */
1963 set_options_bin(curbuf->b_p_bin, 1, 0);
1964 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001965 break;
1966
1967 case 'C': /* "-C" Compatible */
1968 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001969 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001970 break;
1971
1972 case 'e': /* "-e" Ex mode */
1973 exmode_active = EXMODE_NORMAL;
1974 break;
1975
1976 case 'E': /* "-E" Improved Ex mode */
1977 exmode_active = EXMODE_VIM;
1978 break;
1979
1980 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1981 window directly, not with newcli */
1982#ifdef FEAT_GUI
1983 gui.dofork = FALSE; /* don't fork() when starting GUI */
1984#endif
1985 break;
1986
1987 case 'g': /* "-g" start GUI */
1988 main_start_gui();
1989 break;
1990
1991 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1992#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001993 p_fkmap = TRUE;
1994 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001995#else
1996 mch_errmsg(_(e_nofarsi));
1997 mch_exit(2);
1998#endif
1999 break;
2000
2001 case 'h': /* "-h" give help message */
2002#ifdef FEAT_GUI_GNOME
2003 /* Tell usage() to exit for "gvim". */
2004 gui.starting = FALSE;
2005#endif
2006 usage();
2007 break;
2008
2009 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2010#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002011 p_hkmap = TRUE;
2012 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002013#else
2014 mch_errmsg(_(e_nohebrew));
2015 mch_exit(2);
2016#endif
2017 break;
2018
2019 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2020#ifdef FEAT_LISP
2021 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2022 p_sm = TRUE;
2023#endif
2024 break;
2025
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002026 case 'M': /* "-M" no changes or writing of files */
2027 reset_modifiable();
2028 /* FALLTHROUGH */
2029
2030 case 'm': /* "-m" no writing of files */
2031 p_write = FALSE;
2032 break;
2033
2034 case 'y': /* "-y" easy mode */
2035#ifdef FEAT_GUI
2036 gui.starting = TRUE; /* start GUI a bit later */
2037#endif
2038 parmp->evim_mode = TRUE;
2039 break;
2040
2041 case 'N': /* "-N" Nocompatible */
2042 change_compatible(FALSE);
2043 break;
2044
2045 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002046#ifdef FEAT_NETBEANS_INTG
2047 /* checking for "-nb", netbeans parameters */
2048 if (argv[0][argv_idx] == 'b')
2049 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002050 netbeansArg = argv[0];
2051 argv_idx = -1; /* skip to next argument */
2052 }
2053 else
2054#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002055 parmp->no_swap_file = TRUE;
2056 break;
2057
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002058 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002059#ifdef TARGET_API_MAC_OSX
2060 /* For some reason on MacOS X, an argument like:
2061 -psn_0_10223617 is passed in when invoke from Finder
2062 or with the 'open' command */
2063 if (argv[0][argv_idx] == 's')
2064 {
2065 argv_idx = -1; /* bypass full -psn */
2066 main_start_gui();
2067 break;
2068 }
2069#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002070#ifdef FEAT_WINDOWS
2071 /* default is 0: open window for each file */
2072 parmp->window_count = get_number_arg((char_u *)argv[0],
2073 &argv_idx, 0);
2074 parmp->window_layout = WIN_TABS;
2075#endif
2076 break;
2077
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002078 case 'o': /* "-o[N]" open N horizontal split windows */
2079#ifdef FEAT_WINDOWS
2080 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002081 parmp->window_count = get_number_arg((char_u *)argv[0],
2082 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002083 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002084#endif
2085 break;
2086
2087 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002088#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002089 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002090 parmp->window_count = get_number_arg((char_u *)argv[0],
2091 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002092 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002093#endif
2094 break;
2095
2096#ifdef FEAT_QUICKFIX
2097 case 'q': /* "-q" QuickFix mode */
2098 if (parmp->edit_type != EDIT_NONE)
2099 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2100 parmp->edit_type = EDIT_QF;
2101 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2102 {
2103 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2104 argv_idx = -1;
2105 }
2106 else if (argc > 1) /* "-q {errorfile}" */
2107 want_argument = TRUE;
2108 break;
2109#endif
2110
2111 case 'R': /* "-R" readonly mode */
2112 readonlymode = TRUE;
2113 curbuf->b_p_ro = TRUE;
2114 p_uc = 10000; /* don't update very often */
2115 break;
2116
2117 case 'r': /* "-r" recovery mode */
2118 case 'L': /* "-L" recovery mode */
2119 recoverymode = 1;
2120 break;
2121
2122 case 's':
2123 if (exmode_active) /* "-s" silent (batch) mode */
2124 silent_mode = TRUE;
2125 else /* "-s {scriptin}" read from script file */
2126 want_argument = TRUE;
2127 break;
2128
2129 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2130 if (parmp->edit_type != EDIT_NONE)
2131 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2132 parmp->edit_type = EDIT_TAG;
2133 if (argv[0][argv_idx]) /* "-t{tag}" */
2134 {
2135 parmp->tagname = (char_u *)argv[0] + argv_idx;
2136 argv_idx = -1;
2137 }
2138 else /* "-t {tag}" */
2139 want_argument = TRUE;
2140 break;
2141
2142#ifdef FEAT_EVAL
2143 case 'D': /* "-D" Debugging */
2144 parmp->use_debug_break_level = 9999;
2145 break;
2146#endif
2147#ifdef FEAT_DIFF
2148 case 'd': /* "-d" 'diff' */
2149# ifdef AMIGA
2150 /* check for "-dev {device}" */
2151 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2152 want_argument = TRUE;
2153 else
2154# endif
2155 parmp->diff_mode = TRUE;
2156 break;
2157#endif
2158 case 'V': /* "-V{N}" Verbose level */
2159 /* default is 10: a little bit verbose */
2160 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2161 if (argv[0][argv_idx] != NUL)
2162 {
2163 set_option_value((char_u *)"verbosefile", 0L,
2164 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002165 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002166 }
2167 break;
2168
2169 case 'v': /* "-v" Vi-mode (as if called "vi") */
2170 exmode_active = 0;
2171#ifdef FEAT_GUI
2172 gui.starting = FALSE; /* don't start GUI */
2173#endif
2174 break;
2175
2176 case 'w': /* "-w{number}" set window height */
2177 /* "-w {scriptout}" write to script */
2178 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2179 {
2180 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2181 set_option_value((char_u *)"window", n, NULL, 0);
2182 break;
2183 }
2184 want_argument = TRUE;
2185 break;
2186
2187#ifdef FEAT_CRYPT
2188 case 'x': /* "-x" encrypted reading/writing of files */
2189 parmp->ask_for_key = TRUE;
2190 break;
2191#endif
2192
2193 case 'X': /* "-X" don't connect to X server */
2194#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2195 x_no_connect = TRUE;
2196#endif
2197 break;
2198
2199 case 'Z': /* "-Z" restricted mode */
2200 restricted = TRUE;
2201 break;
2202
2203 case 'c': /* "-c{command}" or "-c {command}" execute
2204 command */
2205 if (argv[0][argv_idx] != NUL)
2206 {
2207 if (parmp->n_commands >= MAX_ARG_CMDS)
2208 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002209 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2210 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002211 argv_idx = -1;
2212 break;
2213 }
2214 /*FALLTHROUGH*/
2215 case 'S': /* "-S {file}" execute Vim script */
2216 case 'i': /* "-i {viminfo}" use for viminfo */
2217#ifndef FEAT_DIFF
2218 case 'd': /* "-d {device}" device (for Amiga) */
2219#endif
2220 case 'T': /* "-T {terminal}" terminal name */
2221 case 'u': /* "-u {vimrc}" vim inits file */
2222 case 'U': /* "-U {gvimrc}" gvim inits file */
2223 case 'W': /* "-W {scriptout}" overwrite */
2224#ifdef FEAT_GUI_W32
2225 case 'P': /* "-P {parent title}" MDI parent */
2226#endif
2227 want_argument = TRUE;
2228 break;
2229
2230 default:
2231 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2232 }
2233
2234 /*
2235 * Handle option arguments with argument.
2236 */
2237 if (want_argument)
2238 {
2239 /*
2240 * Check for garbage immediately after the option letter.
2241 */
2242 if (argv[0][argv_idx] != NUL)
2243 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2244
2245 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002246 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002247 mainerr_arg_missing((char_u *)argv[0]);
2248 ++argv;
2249 argv_idx = -1;
2250
2251 switch (c)
2252 {
2253 case 'c': /* "-c {command}" execute command */
2254 case 'S': /* "-S {file}" execute Vim script */
2255 if (parmp->n_commands >= MAX_ARG_CMDS)
2256 mainerr(ME_EXTRA_CMD, NULL);
2257 if (c == 'S')
2258 {
2259 char *a;
2260
2261 if (argc < 1)
2262 /* "-S" without argument: use default session file
2263 * name. */
2264 a = SESSION_FILE;
2265 else if (argv[0][0] == '-')
2266 {
2267 /* "-S" followed by another option: use default
2268 * session file name. */
2269 a = SESSION_FILE;
2270 ++argc;
2271 --argv;
2272 }
2273 else
2274 a = argv[0];
2275 p = alloc((unsigned)(STRLEN(a) + 4));
2276 if (p == NULL)
2277 mch_exit(2);
2278 sprintf((char *)p, "so %s", a);
2279 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2280 parmp->commands[parmp->n_commands++] = p;
2281 }
2282 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002283 parmp->commands[parmp->n_commands++] =
2284 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002285 break;
2286
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002287 case '-':
2288 if (argv[-1][2] == 'c')
2289 {
2290 /* "--cmd {command}" execute command */
2291 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2292 mainerr(ME_EXTRA_CMD, NULL);
2293 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002294 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002295 }
2296 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002297 break;
2298
2299 /* case 'd': -d {device} is handled in mch_check_win() for the
2300 * Amiga */
2301
2302#ifdef FEAT_QUICKFIX
2303 case 'q': /* "-q {errorfile}" QuickFix mode */
2304 parmp->use_ef = (char_u *)argv[0];
2305 break;
2306#endif
2307
2308 case 'i': /* "-i {viminfo}" use for viminfo */
2309 use_viminfo = (char_u *)argv[0];
2310 break;
2311
2312 case 's': /* "-s {scriptin}" read from script file */
2313 if (scriptin[0] != NULL)
2314 {
2315scripterror:
2316 mch_errmsg(_("Attempt to open script file again: \""));
2317 mch_errmsg(argv[-1]);
2318 mch_errmsg(" ");
2319 mch_errmsg(argv[0]);
2320 mch_errmsg("\"\n");
2321 mch_exit(2);
2322 }
2323 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2324 {
2325 mch_errmsg(_("Cannot open for reading: \""));
2326 mch_errmsg(argv[0]);
2327 mch_errmsg("\"\n");
2328 mch_exit(2);
2329 }
2330 if (save_typebuf() == FAIL)
2331 mch_exit(2); /* out of memory */
2332 break;
2333
2334 case 't': /* "-t {tag}" */
2335 parmp->tagname = (char_u *)argv[0];
2336 break;
2337
2338 case 'T': /* "-T {terminal}" terminal name */
2339 /*
2340 * The -T term argument is always available and when
2341 * HAVE_TERMLIB is supported it overrides the environment
2342 * variable TERM.
2343 */
2344#ifdef FEAT_GUI
2345 if (term_is_gui((char_u *)argv[0]))
2346 gui.starting = TRUE; /* start GUI a bit later */
2347 else
2348#endif
2349 parmp->term = (char_u *)argv[0];
2350 break;
2351
2352 case 'u': /* "-u {vimrc}" vim inits file */
2353 parmp->use_vimrc = (char_u *)argv[0];
2354 break;
2355
2356 case 'U': /* "-U {gvimrc}" gvim inits file */
2357#ifdef FEAT_GUI
2358 use_gvimrc = (char_u *)argv[0];
2359#endif
2360 break;
2361
2362 case 'w': /* "-w {nr}" 'window' value */
2363 /* "-w {scriptout}" append to script file */
2364 if (vim_isdigit(*((char_u *)argv[0])))
2365 {
2366 argv_idx = 0;
2367 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2368 set_option_value((char_u *)"window", n, NULL, 0);
2369 argv_idx = -1;
2370 break;
2371 }
2372 /*FALLTHROUGH*/
2373 case 'W': /* "-W {scriptout}" overwrite script file */
2374 if (scriptout != NULL)
2375 goto scripterror;
2376 if ((scriptout = mch_fopen(argv[0],
2377 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2378 {
2379 mch_errmsg(_("Cannot open for script output: \""));
2380 mch_errmsg(argv[0]);
2381 mch_errmsg("\"\n");
2382 mch_exit(2);
2383 }
2384 break;
2385
2386#ifdef FEAT_GUI_W32
2387 case 'P': /* "-P {parent title}" MDI parent */
2388 gui_mch_set_parent(argv[0]);
2389 break;
2390#endif
2391 }
2392 }
2393 }
2394
2395 /*
2396 * File name argument.
2397 */
2398 else
2399 {
2400 argv_idx = -1; /* skip to next argument */
2401
2402 /* Check for only one type of editing. */
2403 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2404 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2405 parmp->edit_type = EDIT_FILE;
2406
2407#ifdef MSWIN
2408 /* Remember if the argument was a full path before changing
2409 * slashes to backslashes. */
2410 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2411 parmp->full_path = TRUE;
2412#endif
2413
2414 /* Add the file to the global argument list. */
2415 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2416 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2417 mch_exit(2);
2418#ifdef FEAT_DIFF
2419 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2420 && !mch_isdir(alist_name(&GARGLIST[0])))
2421 {
2422 char_u *r;
2423
2424 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2425 if (r != NULL)
2426 {
2427 vim_free(p);
2428 p = r;
2429 }
2430 }
2431#endif
2432#if defined(__CYGWIN32__) && !defined(WIN32)
2433 /*
2434 * If vim is invoked by non-Cygwin tools, convert away any
2435 * DOS paths, so things like .swp files are created correctly.
2436 * Look for evidence of non-Cygwin paths before we bother.
2437 * This is only for when using the Unix files.
2438 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002439 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 {
2441 char posix_path[PATH_MAX];
2442
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002443# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2444 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2445# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002446 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002447# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002448 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002449 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002450 if (p == NULL)
2451 mch_exit(2);
2452 }
2453#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002454
2455#ifdef USE_FNAME_CASE
2456 /* Make the case of the file name match the actual file. */
2457 fname_case(p, 0);
2458#endif
2459
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002461#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002462 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002463#else
2464 2 /* add buffer number now and use curbuf */
2465#endif
2466 );
2467
2468#if defined(FEAT_MBYTE) && defined(WIN32)
2469 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002470 /* Remember this argument has been added to the argument list.
2471 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002472 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002473# ifdef FEAT_DIFF
2474 parmp->diff_mode
2475# else
2476 FALSE
2477# endif
2478 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002479 }
2480#endif
2481 }
2482
2483 /*
2484 * If there are no more letters after the current "-", go to next
2485 * argument. argv_idx is set to -1 when the current argument is to be
2486 * skipped.
2487 */
2488 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2489 {
2490 --argc;
2491 ++argv;
2492 argv_idx = 1;
2493 }
2494 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002495
2496#ifdef FEAT_EVAL
2497 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2498 * one. */
2499 if (parmp->n_commands > 0)
2500 {
2501 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2502 if (p != NULL)
2503 {
2504 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2505 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2506 vim_free(p);
2507 }
2508 }
2509#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002510}
2511
2512/*
2513 * Print a warning if stdout is not a terminal.
2514 * When starting in Ex mode and commands come from a file, set Silent mode.
2515 */
2516 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002517check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002518{
2519 int input_isatty; /* is active input a terminal? */
2520
2521 input_isatty = mch_input_isatty();
2522 if (exmode_active)
2523 {
2524 if (!input_isatty)
2525 silent_mode = TRUE;
2526 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002527 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002528#ifdef FEAT_GUI
2529 /* don't want the delay when started from the desktop */
2530 && !gui.starting
2531#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002532 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002533 {
2534#ifdef NBDEBUG
2535 /*
2536 * This shouldn't be necessary. But if I run netbeans with the log
2537 * output coming to the console and XOpenDisplay fails, I get vim
2538 * trying to start with input/output to my console tty. This fills my
2539 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002540 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002542 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002543 {
2544 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2545 exit(1);
2546 }
2547#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002548#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2549 if (is_cygpty_used())
2550 {
2551 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2552 exit(1);
2553 }
2554#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002555 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002556 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2557 if (!input_isatty)
2558 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2559 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002560 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2561 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562 if (scriptin[0] == NULL)
2563 ui_delay(2000L, TRUE);
2564 TIME_MSG("Warning delay");
2565 }
2566}
2567
2568/*
2569 * Read text from stdin.
2570 */
2571 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002572read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573{
2574 int i;
2575
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002576#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002577 /* When getting the ATTENTION prompt here, use a dialog */
2578 swap_exists_action = SEA_DIALOG;
2579#endif
2580 no_wait_return = TRUE;
2581 i = msg_didany;
2582 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002583 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002584 no_wait_return = FALSE;
2585 msg_didany = i;
2586 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002587#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002588 check_swap_exists_action();
2589#endif
2590#if !(defined(AMIGA) || defined(MACOS))
2591 /*
2592 * Close stdin and dup it from stderr. Required for GPM to work
2593 * properly, and for running external commands.
2594 * Is there any other system that cannot do this?
2595 */
2596 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002597 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002598#endif
2599}
2600
2601/*
2602 * Create the requested number of windows and edit buffers in them.
2603 * Also does recovery if "recoverymode" set.
2604 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002605 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002606create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002607{
2608#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002609 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002610 int done = 0;
2611
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002612 /*
2613 * Create the number of windows that was requested.
2614 */
2615 if (parmp->window_count == -1) /* was not set */
2616 parmp->window_count = 1;
2617 if (parmp->window_count == 0)
2618 parmp->window_count = GARGCOUNT;
2619 if (parmp->window_count > 1)
2620 {
2621 /* Don't change the windows if there was a command in .vimrc that
2622 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002623 if (parmp->window_layout == 0)
2624 parmp->window_layout = WIN_HOR;
2625 if (parmp->window_layout == WIN_TABS)
2626 {
2627 parmp->window_count = make_tabpages(parmp->window_count);
2628 TIME_MSG("making tab pages");
2629 }
2630 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631 {
2632 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002633 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002634 TIME_MSG("making windows");
2635 }
2636 else
2637 parmp->window_count = win_count();
2638 }
2639 else
2640 parmp->window_count = 1;
2641#endif
2642
2643 if (recoverymode) /* do recover */
2644 {
2645 msg_scroll = TRUE; /* scroll message up */
2646 ml_recover();
2647 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2648 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002649 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002650 }
2651 else
2652 {
2653 /*
2654 * Open a buffer for windows that don't have one yet.
2655 * Commands in the .vimrc might have loaded a file or split the window.
2656 * Watch out for autocommands that delete a window.
2657 */
2658#ifdef FEAT_AUTOCMD
2659 /*
2660 * Don't execute Win/Buf Enter/Leave autocommands here
2661 */
2662 ++autocmd_no_enter;
2663 ++autocmd_no_leave;
2664#endif
2665#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002666 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002667 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002668 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002669 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002670 {
2671 if (parmp->window_layout == WIN_TABS)
2672 goto_tabpage(1);
2673 else
2674 curwin = firstwin;
2675 }
2676 else if (parmp->window_layout == WIN_TABS)
2677 {
2678 if (curtab->tp_next == NULL)
2679 break;
2680 goto_tabpage(0);
2681 }
2682 else
2683 {
2684 if (curwin->w_next == NULL)
2685 break;
2686 curwin = curwin->w_next;
2687 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002688 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002689#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690 curbuf = curwin->w_buffer;
2691 if (curbuf->b_ml.ml_mfp == NULL)
2692 {
2693#ifdef FEAT_FOLDING
2694 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2695 if (p_fdls >= 0)
2696 curwin->w_p_fdl = p_fdls;
2697#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002698#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002699 /* When getting the ATTENTION prompt here, use a dialog */
2700 swap_exists_action = SEA_DIALOG;
2701#endif
2702 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002703
2704 /* create memfile, read file */
2705 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002706
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002707#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002708 if (swap_exists_action == SEA_QUIT)
2709 {
2710 if (got_int || only_one_window())
2711 {
2712 /* abort selected or quit and only one window */
2713 did_emsg = FALSE; /* avoid hit-enter prompt */
2714 getout(1);
2715 }
2716 /* We can't close the window, it would disturb what
2717 * happens next. Clear the file name and set the arg
2718 * index to -1 to delete it later. */
2719 setfname(curbuf, NULL, NULL, FALSE);
2720 curwin->w_arg_idx = -1;
2721 swap_exists_action = SEA_NONE;
2722 }
2723 else
2724 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002725#endif
2726#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002727 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728#endif
2729 }
2730#ifdef FEAT_WINDOWS
2731 ui_breakcheck();
2732 if (got_int)
2733 {
2734 (void)vgetc(); /* only break the file loading, not the rest */
2735 break;
2736 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002738#endif
2739#ifdef FEAT_WINDOWS
2740 if (parmp->window_layout == WIN_TABS)
2741 goto_tabpage(1);
2742 else
2743 curwin = firstwin;
2744 curbuf = curwin->w_buffer;
2745#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002746#ifdef FEAT_AUTOCMD
2747 --autocmd_no_enter;
2748 --autocmd_no_leave;
2749#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002750 }
2751}
2752
2753#ifdef FEAT_WINDOWS
2754 /*
2755 * If opened more than one window, start editing files in the other
2756 * windows. make_windows() has already opened the windows.
2757 */
2758 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002759edit_buffers(
2760 mparm_T *parmp,
2761 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002762{
2763 int arg_idx; /* index in argument list */
2764 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002765 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002766 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002767
2768# ifdef FEAT_AUTOCMD
2769 /*
2770 * Don't execute Win/Buf Enter/Leave autocommands here
2771 */
2772 ++autocmd_no_enter;
2773 ++autocmd_no_leave;
2774# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002775
2776 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2777 if (curwin->w_arg_idx == -1)
2778 {
2779 win_close(curwin, TRUE);
2780 advance = FALSE;
2781 }
2782
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 arg_idx = 1;
2784 for (i = 1; i < parmp->window_count; ++i)
2785 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002786 if (cwd != NULL)
2787 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002788 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2789 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002790 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002791 ++arg_idx;
2792 win_close(curwin, TRUE);
2793 advance = FALSE;
2794 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002795 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796
Bram Moolenaar84212822006-11-07 21:59:47 +00002797 if (advance)
2798 {
2799 if (parmp->window_layout == WIN_TABS)
2800 {
2801 if (curtab->tp_next == NULL) /* just checking */
2802 break;
2803 goto_tabpage(0);
2804 }
2805 else
2806 {
2807 if (curwin->w_next == NULL) /* just checking */
2808 break;
2809 win_enter(curwin->w_next, FALSE);
2810 }
2811 }
2812 advance = TRUE;
2813
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002814 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002815 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2817 {
2818 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002819 /* Edit file from arg list, if there is one. When "Quit" selected
2820 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002821# ifdef HAS_SWAP_EXISTS_ACTION
2822 swap_exists_did_quit = FALSE;
2823# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002824 (void)do_ecmd(0, arg_idx < GARGCOUNT
2825 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002826 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002827# ifdef HAS_SWAP_EXISTS_ACTION
2828 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002829 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002830 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002831 if (got_int || only_one_window())
2832 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002833 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002834 did_emsg = FALSE; /* avoid hit-enter prompt */
2835 getout(1);
2836 }
2837 win_close(curwin, TRUE);
2838 advance = FALSE;
2839 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002840# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002841 if (arg_idx == GARGCOUNT - 1)
2842 arg_had_last = TRUE;
2843 ++arg_idx;
2844 }
2845 ui_breakcheck();
2846 if (got_int)
2847 {
2848 (void)vgetc(); /* only break the file loading, not the rest */
2849 break;
2850 }
2851 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002852
2853 if (parmp->window_layout == WIN_TABS)
2854 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002855# ifdef FEAT_AUTOCMD
2856 --autocmd_no_enter;
2857# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002858
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002859 /* make the first window the current window */
2860 win = firstwin;
2861#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2862 /* Avoid making a preview window the current window. */
2863 while (win->w_p_pvw)
2864 {
2865 win = win->w_next;
2866 if (win == NULL)
2867 {
2868 win = firstwin;
2869 break;
2870 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002871 }
2872#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002873 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002874
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875# ifdef FEAT_AUTOCMD
2876 --autocmd_no_leave;
2877# endif
2878 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002879 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002880 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2881}
2882#endif /* FEAT_WINDOWS */
2883
2884/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002885 * Execute the commands from --cmd arguments "cmds[cnt]".
2886 */
2887 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002888exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002889{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002890 char_u **cmds = parmp->pre_commands;
2891 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002892 int i;
2893
2894 if (cnt > 0)
2895 {
2896 curwin->w_cursor.lnum = 0; /* just in case.. */
2897 sourcing_name = (char_u *)_("pre-vimrc command line");
2898# ifdef FEAT_EVAL
2899 current_SID = SID_CMDARG;
2900# endif
2901 for (i = 0; i < cnt; ++i)
2902 do_cmdline_cmd(cmds[i]);
2903 sourcing_name = NULL;
2904# ifdef FEAT_EVAL
2905 current_SID = 0;
2906# endif
2907 TIME_MSG("--cmd commands");
2908 }
2909}
2910
2911/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002912 * Execute "+", "-c" and "-S" arguments.
2913 */
2914 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002915exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002916{
2917 int i;
2918
2919 /*
2920 * We start commands on line 0, make "vim +/pat file" match a
2921 * pattern on line 1. But don't move the cursor when an autocommand
2922 * with g`" was used.
2923 */
2924 msg_scroll = TRUE;
2925 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2926 curwin->w_cursor.lnum = 0;
2927 sourcing_name = (char_u *)"command line";
2928#ifdef FEAT_EVAL
2929 current_SID = SID_CARG;
2930#endif
2931 for (i = 0; i < parmp->n_commands; ++i)
2932 {
2933 do_cmdline_cmd(parmp->commands[i]);
2934 if (parmp->cmds_tofree[i])
2935 vim_free(parmp->commands[i]);
2936 }
2937 sourcing_name = NULL;
2938#ifdef FEAT_EVAL
2939 current_SID = 0;
2940#endif
2941 if (curwin->w_cursor.lnum == 0)
2942 curwin->w_cursor.lnum = 1;
2943
2944 if (!exmode_active)
2945 msg_scroll = FALSE;
2946
2947#ifdef FEAT_QUICKFIX
2948 /* When started with "-q errorfile" jump to first error again. */
2949 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002950 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002951#endif
2952 TIME_MSG("executing command arguments");
2953}
2954
2955/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002956 * Source startup scripts.
2957 */
2958 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002959source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002960{
2961 int i;
2962
2963 /*
2964 * For "evim" source evim.vim first of all, so that the user can overrule
2965 * any things he doesn't like.
2966 */
2967 if (parmp->evim_mode)
2968 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002969 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002970 TIME_MSG("source evim file");
2971 }
2972
2973 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002974 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002975 * nothing else.
2976 */
2977 if (parmp->use_vimrc != NULL)
2978 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002979 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2980 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002981 {
2982#ifdef FEAT_GUI
2983 if (use_gvimrc == NULL) /* don't load gvimrc either */
2984 use_gvimrc = parmp->use_vimrc;
2985#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002986 }
2987 else
2988 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002989 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002990 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2991 }
2992 }
2993 else if (!silent_mode)
2994 {
2995#ifdef AMIGA
2996 struct Process *proc = (struct Process *)FindTask(0L);
2997 APTR save_winptr = proc->pr_WindowPtr;
2998
2999 /* Avoid a requester here for a volume that doesn't exist. */
3000 proc->pr_WindowPtr = (APTR)-1L;
3001#endif
3002
3003 /*
3004 * Get system wide defaults, if the file name is defined.
3005 */
3006#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003007 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003009#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003010 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003011#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003012
3013 /*
3014 * Try to read initialization commands from the following places:
3015 * - environment variable VIMINIT
3016 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3017 * - second user vimrc file ($VIM/.vimrc for Dos)
3018 * - environment variable EXINIT
3019 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3020 * - second user exrc file ($VIM/.exrc for Dos)
3021 * The first that exists is used, the rest is ignored.
3022 */
3023 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3024 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003025 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003026#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003027 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3028 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029#endif
3030#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003031 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3032 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003033#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003034#ifdef USR_VIMRC_FILE4
3035 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3036 DOSO_VIMRC) == FAIL
3037#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003038 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003039 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003040#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003041 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003042#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003043 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003044 {
3045 /* When no .vimrc file was found: source defaults.vim. */
3046 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003047 }
3048 }
3049
3050 /*
3051 * Read initialization commands from ".vimrc" or ".exrc" in current
3052 * directory. This is only done if the 'exrc' option is set.
3053 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003054 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003055 * option has been reset in environment of global ".exrc" or ".vimrc".
3056 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3057 * SYS_VIMRC_FILE.
3058 */
3059 if (p_exrc)
3060 {
3061#if defined(UNIX) || defined(VMS)
3062 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3063 if (!file_owned(VIMRC_FILE))
3064#endif
3065 secure = p_secure;
3066
3067 i = FAIL;
3068 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3069 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3070#ifdef USR_VIMRC_FILE2
3071 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3072 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3073#endif
3074#ifdef USR_VIMRC_FILE3
3075 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3076 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3077#endif
3078#ifdef SYS_VIMRC_FILE
3079 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3080 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3081#endif
3082 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003083 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003084
3085 if (i == FAIL)
3086 {
3087#if defined(UNIX) || defined(VMS)
3088 /* if ".exrc" is not owned by user set 'secure' mode */
3089 if (!file_owned(EXRC_FILE))
3090 secure = p_secure;
3091 else
3092 secure = 0;
3093#endif
3094 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3095 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3096#ifdef USR_EXRC_FILE2
3097 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3098 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3099#endif
3100 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003101 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003102 }
3103 }
3104 if (secure == 2)
3105 need_wait_return = TRUE;
3106 secure = 0;
3107#ifdef AMIGA
3108 proc->pr_WindowPtr = save_winptr;
3109#endif
3110 }
3111 TIME_MSG("sourcing vimrc file(s)");
3112}
3113
3114/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003115 * Setup to start using the GUI. Exit with an error when not available.
3116 */
3117 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003118main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003119{
3120#ifdef FEAT_GUI
3121 gui.starting = TRUE; /* start GUI a bit later */
3122#else
3123 mch_errmsg(_(e_nogvim));
3124 mch_errmsg("\n");
3125 mch_exit(2);
3126#endif
3127}
3128
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003129#endif /* NO_VIM_MAIN */
3130
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003131/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003132 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003133 * Returns FAIL if the environment variable was not executed, OK otherwise.
3134 */
3135 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003136process_env(
3137 char_u *env,
3138 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003139{
3140 char_u *initstr;
3141 char_u *save_sourcing_name;
3142 linenr_T save_sourcing_lnum;
3143#ifdef FEAT_EVAL
3144 scid_T save_sid;
3145#endif
3146
3147 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3148 {
3149 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003150 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003151 save_sourcing_name = sourcing_name;
3152 save_sourcing_lnum = sourcing_lnum;
3153 sourcing_name = env;
3154 sourcing_lnum = 0;
3155#ifdef FEAT_EVAL
3156 save_sid = current_SID;
3157 current_SID = SID_ENV;
3158#endif
3159 do_cmdline_cmd(initstr);
3160 sourcing_name = save_sourcing_name;
3161 sourcing_lnum = save_sourcing_lnum;
3162#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003163 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003164#endif
3165 return OK;
3166 }
3167 return FAIL;
3168}
3169
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003170#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003171/*
3172 * Return TRUE if we are certain the user owns the file "fname".
3173 * Used for ".vimrc" and ".exrc".
3174 * Use both stat() and lstat() for extra security.
3175 */
3176 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003177file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003178{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003179 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003180# ifdef UNIX
3181 uid_t uid = getuid();
3182# else /* VMS */
3183 uid_t uid = ((getgid() << 16) | getuid());
3184# endif
3185
3186 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3187# ifdef HAVE_LSTAT
3188 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3189# endif
3190 );
3191}
3192#endif
3193
3194/*
3195 * Give an error message main_errors["n"] and exit.
3196 */
3197 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003198mainerr(
3199 int n, /* one of the ME_ defines */
3200 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003201{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003202#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003203 reset_signals(); /* kill us with CTRL-C here, if you like */
3204#endif
3205
3206 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003207 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003208 mch_errmsg(_(main_errors[n]));
3209 if (str != NULL)
3210 {
3211 mch_errmsg(": \"");
3212 mch_errmsg((char *)str);
3213 mch_errmsg("\"");
3214 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003215 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003216
3217 mch_exit(1);
3218}
3219
3220 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003221mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003222{
3223 mainerr(ME_ARG_MISSING, str);
3224}
3225
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003226#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003227/*
3228 * print a message with three spaces prepended and '\n' appended.
3229 */
3230 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003231main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003232{
3233 mch_msg(" ");
3234 mch_msg(s);
3235 mch_msg("\n");
3236}
3237
3238/*
3239 * Print messages for "vim -h" or "vim --help" and exit.
3240 */
3241 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003242usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003243{
3244 int i;
3245 static char *(use[]) =
3246 {
3247 N_("[file ..] edit specified file(s)"),
3248 N_("- read text from stdin"),
3249 N_("-t tag edit file where tag is defined"),
3250#ifdef FEAT_QUICKFIX
3251 N_("-q [errorfile] edit file with first error")
3252#endif
3253 };
3254
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003255#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256 reset_signals(); /* kill us with CTRL-C here, if you like */
3257#endif
3258
3259 mch_msg(longVersion);
3260 mch_msg(_("\n\nusage:"));
3261 for (i = 0; ; ++i)
3262 {
3263 mch_msg(_(" vim [arguments] "));
3264 mch_msg(_(use[i]));
3265 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3266 break;
3267 mch_msg(_("\n or:"));
3268 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003269#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003270 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003271#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003272
3273 mch_msg(_("\n\nArguments:\n"));
3274 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003275#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003276 main_msg(_("--literal\t\tDon't expand wildcards"));
3277#endif
3278#ifdef FEAT_OLE
3279 main_msg(_("-register\t\tRegister this gvim for OLE"));
3280 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3281#endif
3282#ifdef FEAT_GUI
3283 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3284 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3285#endif
3286 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3287 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003288 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003289 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3290#ifdef FEAT_DIFF
3291 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3292#endif
3293 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3294 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3295 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3296 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3297 main_msg(_("-M\t\t\tModifications in text not allowed"));
3298 main_msg(_("-b\t\t\tBinary mode"));
3299#ifdef FEAT_LISP
3300 main_msg(_("-l\t\t\tLisp mode"));
3301#endif
3302 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3303 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003304 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3305#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003306 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003307#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003308 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3309 main_msg(_("-r\t\t\tList swap files and exit"));
3310 main_msg(_("-r (with file name)\tRecover crashed session"));
3311 main_msg(_("-L\t\t\tSame as -r"));
3312#ifdef AMIGA
3313 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3314 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3315#endif
3316#ifdef FEAT_ARABIC
3317 main_msg(_("-A\t\t\tstart in Arabic mode"));
3318#endif
3319#ifdef FEAT_RIGHTLEFT
3320 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3321#endif
3322#ifdef FEAT_FKMAP
3323 main_msg(_("-F\t\t\tStart in Farsi mode"));
3324#endif
3325 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003326 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003327 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3329#ifdef FEAT_GUI
3330 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3331#endif
3332 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003333#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003334 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003335 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3336 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003337#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003338 main_msg(_("+\t\t\tStart at end of file"));
3339 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003341 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3342 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3343 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3344 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3345 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3346#ifdef FEAT_CRYPT
3347 main_msg(_("-x\t\t\tEdit encrypted files"));
3348#endif
3349#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3350# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3351 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3352# endif
3353 main_msg(_("-X\t\t\tDo not connect to X server"));
3354#endif
3355#ifdef FEAT_CLIENTSERVER
3356 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3357 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3358 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3359 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003360# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003361 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003362# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003363 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3364 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3365 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3366 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3367#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003368#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003369 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003370#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003371#ifdef FEAT_VIMINFO
3372 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3373#endif
3374 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3375 main_msg(_("--version\t\tPrint version information and exit"));
3376
3377#ifdef FEAT_GUI_X11
3378# ifdef FEAT_GUI_MOTIF
3379 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3380# else
3381# ifdef FEAT_GUI_ATHENA
3382# ifdef FEAT_GUI_NEXTAW
3383 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3384# else
3385 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3386# endif
3387# endif
3388# endif
3389 main_msg(_("-display <display>\tRun vim on <display>"));
3390 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3392 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3393 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3394 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3395 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3396 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3397 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3398 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3399# ifdef FEAT_GUI_ATHENA
3400 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3401# endif
3402 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3403 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3404 main_msg(_("-xrm <resource>\tSet the specified resource"));
3405#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406#ifdef FEAT_GUI_GTK
3407 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3408 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3409 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3410 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3411 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003412 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003414 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003416#ifdef FEAT_GUI_W32
3417 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003418 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003419#endif
3420
3421#ifdef FEAT_GUI_GNOME
3422 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3423 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003424 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003426 gui.dofork = FALSE;
3427 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428 else
3429#endif
3430 mch_exit(0);
3431}
3432
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003433#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003434/*
3435 * Check the result of the ATTENTION dialog:
3436 * When "Quit" selected, exit Vim.
3437 * When "Recover" selected, recover the file.
3438 */
3439 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003440check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003441{
3442 if (swap_exists_action == SEA_QUIT)
3443 getout(1);
3444 handle_swap_exists(NULL);
3445}
3446#endif
3447
Bram Moolenaar08cab962017-03-04 14:37:18 +01003448#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003449
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003451static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003452
3453static struct timeval prev_timeval;
3454
Bram Moolenaar3f269672009-11-03 11:11:11 +00003455# ifdef WIN3264
3456/*
3457 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3458 */
3459 static int
3460gettimeofday(struct timeval *tv, char *dummy)
3461{
3462 long t = clock();
3463 tv->tv_sec = t / CLOCKS_PER_SEC;
3464 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3465 return 0;
3466}
3467# endif
3468
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003469/*
3470 * Save the previous time before doing something that could nest.
3471 * set "*tv_rel" to the time elapsed so far.
3472 */
3473 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003474time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003475{
3476 *((struct timeval *)tv_rel) = prev_timeval;
3477 gettimeofday(&prev_timeval, NULL);
3478 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3479 - ((struct timeval *)tv_rel)->tv_usec;
3480 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3481 - ((struct timeval *)tv_rel)->tv_sec;
3482 if (((struct timeval *)tv_rel)->tv_usec < 0)
3483 {
3484 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3485 --((struct timeval *)tv_rel)->tv_sec;
3486 }
3487 *(struct timeval *)tv_start = prev_timeval;
3488}
3489
3490/*
3491 * Compute the previous time after doing something that could nest.
3492 * Subtract "*tp" from prev_timeval;
3493 * Note: The arguments are (void *) to avoid trouble with systems that don't
3494 * have struct timeval.
3495 */
3496 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003497time_pop(
3498 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499{
3500 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3501 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3502 if (prev_timeval.tv_usec < 0)
3503 {
3504 prev_timeval.tv_usec += 1000000;
3505 --prev_timeval.tv_sec;
3506 }
3507}
3508
3509 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003510time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003511{
3512 long usec;
3513 long msec;
3514
3515 usec = now->tv_usec - then->tv_usec;
3516 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3517 usec = usec % 1000L;
3518 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3519}
3520
3521 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003522time_msg(
3523 char *mesg,
3524 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003525 (struct timeval *) */
3526{
3527 static struct timeval start;
3528 struct timeval now;
3529
3530 if (time_fd != NULL)
3531 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003532 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003533 {
3534 gettimeofday(&start, NULL);
3535 prev_timeval = start;
3536 fprintf(time_fd, "\n\ntimes in msec\n");
3537 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3538 fprintf(time_fd, " clock elapsed: other lines\n\n");
3539 }
3540 gettimeofday(&now, NULL);
3541 time_diff(&start, &now);
3542 if (((struct timeval *)tv_start) != NULL)
3543 {
3544 fprintf(time_fd, " ");
3545 time_diff(((struct timeval *)tv_start), &now);
3546 }
3547 fprintf(time_fd, " ");
3548 time_diff(&prev_timeval, &now);
3549 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003550 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003551 }
3552}
3553
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003554#endif
3555
Bram Moolenaar595297d2017-03-04 19:11:12 +01003556#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003557 static void
3558set_progpath(char_u *argv0)
3559{
3560 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003561
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003562# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003563 char buf[PATH_MAX + 1];
3564 ssize_t len;
3565
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003566 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003567 if (len > 0)
3568 {
3569 buf[len] = NUL;
3570 val = (char_u *)buf;
3571 }
3572# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003573 /* A relative path containing a "/" will become invalid when using ":cd",
3574 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003575 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3576 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003577# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003578 char_u *path = NULL;
3579
3580 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3581 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003582# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003583 char_u buf[MAXPATHL];
3584
Bram Moolenaar43663192017-03-05 14:29:12 +01003585 if (!mch_isFullName(argv0))
3586 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003587 if (gettail(argv0) != argv0
3588 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3589 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003590 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003591# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003592# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003593
Bram Moolenaar08cab962017-03-04 14:37:18 +01003594 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003595
Bram Moolenaar066029e2017-03-05 15:19:32 +01003596# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003597 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003598# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003599}
3600
3601#endif /* NO_VIM_MAIN */
3602
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003603#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003604
3605/*
3606 * Common code for the X command server and the Win32 command server.
3607 */
3608
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003609static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003610
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003611/*
3612 * Do the client-server stuff, unless "--servername ''" was used.
3613 */
3614 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003615exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003616{
3617 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3618 {
3619# ifdef WIN32
3620 /* Initialise the client/server messaging infrastructure. */
3621 serverInitMessaging();
3622# endif
3623
3624 /*
3625 * When a command server argument was found, execute it. This may
3626 * exit Vim when it was successful. Otherwise it's executed further
3627 * on. Remember the encoding used here in "serverStrEnc".
3628 */
3629 if (parmp->serverArg)
3630 {
3631 cmdsrv_main(&parmp->argc, parmp->argv,
3632 parmp->serverName_arg, &parmp->serverStr);
3633# ifdef FEAT_MBYTE
3634 parmp->serverStrEnc = vim_strsave(p_enc);
3635# endif
3636 }
3637
3638 /* If we're still running, get the name to register ourselves.
3639 * On Win32 can register right now, for X11 need to setup the
3640 * clipboard first, it's further down. */
3641 parmp->servername = serverMakeName(parmp->serverName_arg,
3642 parmp->argv[0]);
3643# ifdef WIN32
3644 if (parmp->servername != NULL)
3645 {
3646 serverSetName(parmp->servername);
3647 vim_free(parmp->servername);
3648 }
3649# endif
3650 }
3651}
3652
3653/*
3654 * Prepare for running as a Vim server.
3655 */
3656 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003657prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003658{
3659# if defined(FEAT_X11)
3660 /*
3661 * Register for remote command execution with :serversend and --remote
3662 * unless there was a -X or a --servername '' on the command line.
3663 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003664 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003665 */
3666 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3667# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003668 (gui.in_use
3669# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003670 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003671# endif
3672 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003673# endif
3674 parmp->serverName_arg != NULL))
3675 {
3676 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3677 vim_free(parmp->servername);
3678 TIME_MSG("register server name");
3679 }
3680 else
3681 serverDelayedStartName = parmp->servername;
3682# endif
3683
3684 /*
3685 * Execute command ourselves if we're here because the send failed (or
3686 * else we would have exited above).
3687 */
3688 if (parmp->serverStr != NULL)
3689 {
3690 char_u *p;
3691
3692 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3693 parmp->serverStr, &p));
3694 vim_free(p);
3695 }
3696}
3697
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003698 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003699cmdsrv_main(
3700 int *argc,
3701 char **argv,
3702 char_u *serverName_arg,
3703 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003704{
3705 char_u *res;
3706 int i;
3707 char_u *sname;
3708 int ret;
3709 int didone = FALSE;
3710 int exiterr = 0;
3711 char **newArgV = argv + 1;
3712 int newArgC = 1,
3713 Argc = *argc;
3714 int argtype;
3715#define ARGTYPE_OTHER 0
3716#define ARGTYPE_EDIT 1
3717#define ARGTYPE_EDIT_WAIT 2
3718#define ARGTYPE_SEND 3
3719 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003720 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003721# ifndef FEAT_X11
3722 HWND srv;
3723# else
3724 Window srv;
3725
3726 setup_term_clip();
3727# endif
3728
3729 sname = serverMakeName(serverName_arg, argv[0]);
3730 if (sname == NULL)
3731 return;
3732
3733 /*
3734 * Execute the command server related arguments and remove them
3735 * from the argc/argv array; We may have to return into main()
3736 */
3737 for (i = 1; i < Argc; i++)
3738 {
3739 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003740 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003741 {
3742 for (; i < *argc; i++)
3743 {
3744 *newArgV++ = argv[i];
3745 newArgC++;
3746 }
3747 break;
3748 }
3749
Bram Moolenaareb94e552006-03-11 21:35:11 +00003750 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003751 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003752 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3753 {
3754 char *p = argv[i] + 8;
3755
3756 argtype = ARGTYPE_EDIT;
3757 while (*p != NUL)
3758 {
3759 if (STRNICMP(p, "-wait", 5) == 0)
3760 {
3761 argtype = ARGTYPE_EDIT_WAIT;
3762 p += 5;
3763 }
3764 else if (STRNICMP(p, "-silent", 7) == 0)
3765 {
3766 silent = TRUE;
3767 p += 7;
3768 }
3769 else if (STRNICMP(p, "-tab", 4) == 0)
3770 {
3771 tabs = TRUE;
3772 p += 4;
3773 }
3774 else
3775 {
3776 argtype = ARGTYPE_OTHER;
3777 break;
3778 }
3779 }
3780 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003781 else
3782 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003783
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003784 if (argtype != ARGTYPE_OTHER)
3785 {
3786 if (i == *argc - 1)
3787 mainerr_arg_missing((char_u *)argv[i]);
3788 if (argtype == ARGTYPE_SEND)
3789 {
3790 *serverStr = (char_u *)argv[i + 1];
3791 i++;
3792 }
3793 else
3794 {
3795 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003796 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003797 if (*serverStr == NULL)
3798 {
3799 /* Probably out of memory, exit. */
3800 didone = TRUE;
3801 exiterr = 1;
3802 break;
3803 }
3804 Argc = i;
3805 }
3806# ifdef FEAT_X11
3807 if (xterm_dpy == NULL)
3808 {
3809 mch_errmsg(_("No display"));
3810 ret = -1;
3811 }
3812 else
3813 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003814 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003815# else
3816 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003817 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003818# endif
3819 if (ret < 0)
3820 {
3821 if (argtype == ARGTYPE_SEND)
3822 {
3823 /* Failed to send, abort. */
3824 mch_errmsg(_(": Send failed.\n"));
3825 didone = TRUE;
3826 exiterr = 1;
3827 }
3828 else if (!silent)
3829 /* Let vim start normally. */
3830 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3831 break;
3832 }
3833
3834# ifdef FEAT_GUI_W32
3835 /* Guess that when the server name starts with "g" it's a GUI
3836 * server, which we can bring to the foreground here.
3837 * Foreground() in the server doesn't work very well. */
3838 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3839 SetForegroundWindow(srv);
3840# endif
3841
3842 /*
3843 * For --remote-wait: Wait until the server did edit each
3844 * file. Also detect that the server no longer runs.
3845 */
3846 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3847 {
3848 int numFiles = *argc - i - 1;
3849 int j;
3850 char_u *done = alloc(numFiles);
3851 char_u *p;
3852# ifdef FEAT_GUI_W32
3853 NOTIFYICONDATA ni;
3854 int count = 0;
3855 extern HWND message_window;
3856# endif
3857
3858 if (numFiles > 0 && argv[i + 1][0] == '+')
3859 /* Skip "+cmd" argument, don't wait for it to be edited. */
3860 --numFiles;
3861
3862# ifdef FEAT_GUI_W32
3863 ni.cbSize = sizeof(ni);
3864 ni.hWnd = message_window;
3865 ni.uID = 0;
3866 ni.uFlags = NIF_ICON|NIF_TIP;
3867 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3868 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3869 Shell_NotifyIcon(NIM_ADD, &ni);
3870# endif
3871
3872 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003873 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003874 while (memchr(done, 0, numFiles) != NULL)
3875 {
3876# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003877 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003878 if (p == NULL)
3879 break;
3880# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003881 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003882 break;
3883# endif
3884 j = atoi((char *)p);
3885 if (j >= 0 && j < numFiles)
3886 {
3887# ifdef FEAT_GUI_W32
3888 ++count;
3889 sprintf(ni.szTip, _("%d of %d edited"),
3890 count, numFiles);
3891 Shell_NotifyIcon(NIM_MODIFY, &ni);
3892# endif
3893 done[j] = 1;
3894 }
3895 }
3896# ifdef FEAT_GUI_W32
3897 Shell_NotifyIcon(NIM_DELETE, &ni);
3898# endif
3899 }
3900 }
3901 else if (STRICMP(argv[i], "--remote-expr") == 0)
3902 {
3903 if (i == *argc - 1)
3904 mainerr_arg_missing((char_u *)argv[i]);
3905# ifdef WIN32
3906 /* Win32 always works? */
3907 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003908 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003909# else
3910 if (xterm_dpy == NULL)
3911 mch_errmsg(_("No display: Send expression failed.\n"));
3912 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003913 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003914# endif
3915 {
3916 if (res != NULL && *res != NUL)
3917 {
3918 /* Output error from remote */
3919 mch_errmsg((char *)res);
3920 vim_free(res);
3921 res = NULL;
3922 }
3923 mch_errmsg(_(": Send expression failed.\n"));
3924 }
3925 }
3926 else if (STRICMP(argv[i], "--serverlist") == 0)
3927 {
3928# ifdef WIN32
3929 /* Win32 always works? */
3930 res = serverGetVimNames();
3931# else
3932 if (xterm_dpy != NULL)
3933 res = serverGetVimNames(xterm_dpy);
3934# endif
3935 if (called_emsg)
3936 mch_errmsg("\n");
3937 }
3938 else if (STRICMP(argv[i], "--servername") == 0)
3939 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003940 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003941 i++;
3942 continue;
3943 }
3944 else
3945 {
3946 *newArgV++ = argv[i];
3947 newArgC++;
3948 continue;
3949 }
3950 didone = TRUE;
3951 if (res != NULL && *res != NUL)
3952 {
3953 mch_msg((char *)res);
3954 if (res[STRLEN(res) - 1] != '\n')
3955 mch_msg("\n");
3956 }
3957 vim_free(res);
3958 }
3959
3960 if (didone)
3961 {
3962 display_errors(); /* display any collected messages */
3963 exit(exiterr); /* Mission accomplished - get out */
3964 }
3965
3966 /* Return back into main() */
3967 *argc = newArgC;
3968 vim_free(sname);
3969}
3970
3971/*
3972 * Build a ":drop" command to send to a Vim server.
3973 */
3974 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003975build_drop_cmd(
3976 int filec,
3977 char **filev,
3978 int tabs, /* Use ":tab drop" instead of ":drop". */
3979 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003980{
3981 garray_T ga;
3982 int i;
3983 char_u *inicmd = NULL;
3984 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003985 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003986 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003987
3988 if (filec > 0 && filev[0][0] == '+')
3989 {
3990 inicmd = (char_u *)filev[0] + 1;
3991 filev++;
3992 filec--;
3993 }
3994 /* Check if we have at least one argument. */
3995 if (filec <= 0)
3996 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003997
3998 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003999 cwd = alloc(MAXPATHL);
4000 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004001 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004002 if (mch_dirname(cwd, MAXPATHL) != OK)
4003 {
4004 vim_free(cwd);
4005 return NULL;
4006 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004007 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004008#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004009 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004010#else
4011 PATH_ESC_CHARS,
4012#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004013 '\\', TRUE);
4014 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004015 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004016 return NULL;
4017 ga_init2(&ga, 1, 100);
4018 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004019 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004020
4021 /* Call inputsave() so that a prompt for an encryption key works. */
4022 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4023 if (tabs)
4024 ga_concat(&ga, (char_u *)"tab ");
4025 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004026 for (i = 0; i < filec; i++)
4027 {
4028 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004029 * do it again in the Vim server. On MS-Windows only escape
4030 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004031 p = vim_strsave_escaped((char_u *)filev[i],
4032#ifdef UNIX
4033 PATH_ESC_CHARS
4034#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004035 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004036#endif
4037 );
4038 if (p == NULL)
4039 {
4040 vim_free(ga.ga_data);
4041 return NULL;
4042 }
4043 ga_concat(&ga, (char_u *)" ");
4044 ga_concat(&ga, p);
4045 vim_free(p);
4046 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004047 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4048
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004049 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4050 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004051 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4052
4053 /* Switch back to the correct current directory (prior to temporary path
4054 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004055 * correct after the :drop command. With line breaks and spaces:
4056 * if !exists('+acd') || !&acd
4057 * if haslocaldir()
4058 * cd -
4059 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004060 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004061 * cd -
4062 * endif
4063 * endif
4064 */
4065 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004066 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004067 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004068 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004069 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004070
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004071 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004072 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4073 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004074 if (inicmd != NULL)
4075 {
4076 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4077 * the following commands to be inserted as text. Use a "|",
4078 * hopefully "inicmd" does allow this... */
4079 ga_concat(&ga, inicmd);
4080 ga_concat(&ga, (char_u *)"|");
4081 }
4082 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4083 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004084 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004085 ga_append(&ga, NUL);
4086 return ga.ga_data;
4087}
4088
4089/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004090 * Make our basic server name: use the specified "arg" if given, otherwise use
4091 * the tail of the command "cmd" we were started with.
4092 * Return the name in allocated memory. This doesn't include a serial number.
4093 */
4094 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004095serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004096{
4097 char_u *p;
4098
4099 if (arg != NULL && *arg != NUL)
4100 p = vim_strsave_up(arg);
4101 else
4102 {
4103 p = vim_strsave_up(gettail((char_u *)cmd));
4104 /* Remove .exe or .bat from the name. */
4105 if (p != NULL && vim_strchr(p, '.') != NULL)
4106 *vim_strchr(p, '.') = NUL;
4107 }
4108 return p;
4109}
4110#endif /* FEAT_CLIENTSERVER */
4111
4112#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4113/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004114 * Replace termcodes such as <CR> and insert as key presses if there is room.
4115 */
4116 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004117server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004118{
4119 char_u *ptr = NULL;
4120 char_u *cpo_save = p_cpo;
4121
4122 /* Set 'cpoptions' the way we want it.
4123 * B set - backslashes are *not* treated specially
4124 * k set - keycodes are *not* reverse-engineered
4125 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004126 * The last but one parameter of replace_termcodes() is TRUE so that the
4127 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004128 */
4129 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004130 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004131 p_cpo = cpo_save;
4132
4133 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4134 {
4135 /*
4136 * Add the string to the input stream.
4137 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4138 *
4139 * First clear typed characters from the typeahead buffer, there could
4140 * be half a mapping there. Then append to the existing string, so
4141 * that multiple commands from a client are concatenated.
4142 */
4143 if (typebuf.tb_maplen < typebuf.tb_len)
4144 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4145 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4146
4147 /* Let input_available() know we inserted text in the typeahead
4148 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004149 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004150 }
4151 vim_free((char_u *)ptr);
4152}
4153
4154/*
4155 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004156 */
4157 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004158eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004159{
4160 char_u *res;
4161 int save_dbl = debug_break_level;
4162 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004163 void *fc;
4164
4165 /* Evaluate the expression at the toplevel, don't use variables local to
4166 * the calling function. */
4167 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004168
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004169 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4170 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004171 debug_break_level = -1;
4172 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004173 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4174 * to be typed. Do generate errors so that try/catch works. */
4175 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004176
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004177 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004178
4179 debug_break_level = save_dbl;
4180 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004181 --emsg_silent;
4182 if (emsg_silent < 0)
4183 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004184 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004185
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004186 /* A client can tell us to redraw, but not to display the cursor, so do
4187 * that here. */
4188 setcursor();
4189 out_flush();
4190#ifdef FEAT_GUI
4191 if (gui.in_use)
4192 gui_update_cursor(FALSE, FALSE);
4193#endif
4194
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004195 return res;
4196}
4197
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004198/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004199 * Evaluate a command or expression sent to ourselves.
4200 */
4201 int
4202sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4203{
4204 if (asExpr)
4205 {
4206 char_u *ret;
4207
4208 ret = eval_client_expr_to_string(cmd);
4209 if (result != NULL)
4210 {
4211 if (ret == NULL)
4212 {
4213 char *err = _(e_invexprmsg);
4214 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4215 char_u *msg;
4216
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004217 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004218 if (msg != NULL)
4219 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4220 *result = msg;
4221 }
4222 else
4223 *result = ret;
4224 }
4225 else
4226 vim_free(ret);
4227 return ret == NULL ? -1 : 0;
4228 }
4229 server_to_input_buf(cmd);
4230 return 0;
4231}
4232
4233/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004234 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4235 * return an allocated string. Otherwise return "data".
4236 * "*tofree" is set to the result when it needs to be freed later.
4237 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004238 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004239serverConvert(
4240 char_u *client_enc UNUSED,
4241 char_u *data,
4242 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004243{
4244 char_u *res = data;
4245
4246 *tofree = NULL;
4247# ifdef FEAT_MBYTE
4248 if (client_enc != NULL && p_enc != NULL)
4249 {
4250 vimconv_T vimconv;
4251
4252 vimconv.vc_type = CONV_NONE;
4253 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4254 && vimconv.vc_type != CONV_NONE)
4255 {
4256 res = string_convert(&vimconv, data, NULL);
4257 if (res == NULL)
4258 res = data;
4259 else
4260 *tofree = res;
4261 }
4262 convert_setup(&vimconv, NULL, NULL);
4263 }
4264# endif
4265 return res;
4266}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004267#endif