blob: e30701c6729367578e4c0aea783454e553d712a8 [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 Moolenaar6b1da332017-06-09 21:35:47 +0200795 /* Must come before the may_req_ calls. */
796 starting = 0;
797
Bram Moolenaar976787d2017-06-04 15:45:50 +0200798#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
799 /* Must be done before redrawing, puts a few characters on the screen. */
800 may_req_ambiguous_char_width();
801#endif
802
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000803 RedrawingDisabled = 0;
804 redraw_all_later(NOT_VALID);
805 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200807 /* 'autochdir' has been postponed */
808 DO_AUTOCHDIR
809
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000810#ifdef FEAT_TERMRESPONSE
811 /* Requesting the termresponse is postponed until here, so that a "-c q"
812 * argument doesn't make it appear in the shell Vim was started from. */
813 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200814
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200815 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000816#endif
817
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000818 /* start in insert mode */
819 if (p_im)
820 need_start_insertmode = TRUE;
821
Bram Moolenaar14735512016-03-26 21:00:08 +0100822#ifdef FEAT_EVAL
823 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
824#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825#ifdef FEAT_AUTOCMD
826 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
827 TIME_MSG("VimEnter autocommands");
828#endif
829
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200830#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
831 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
832 * done after the clipboard is available and all initial commands that may
833 * modify the 'clipboard' setting have run; i.e. just before entering the
834 * main loop. */
835 {
836 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100837
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200838 adjust_clip_reg(&default_regname);
839 set_reg_var(default_regname);
840 }
841#endif
842
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000843#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
844 /* When a startup script or session file setup for diff'ing and
845 * scrollbind, sync the scrollbind now. */
846 if (curwin->w_p_diff && curwin->w_p_scb)
847 {
848 update_topline();
849 check_scrollbind((linenr_T)0, 0L);
850 TIME_MSG("diff scrollbinding");
851 }
852#endif
853
854#if defined(WIN3264) && !defined(FEAT_GUI_W32)
855 mch_set_winsize_now(); /* Allow winsize changes from now on */
856#endif
857
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000858#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
859 /* When tab pages were created, may need to update the tab pages line and
860 * scrollbars. This is skipped while creating them. */
861 if (first_tabpage->tp_next != NULL)
862 {
863 out_flush();
864 gui_init_which_components(NULL);
865 gui_update_scrollbars(TRUE);
866 }
867 need_mouse_correct = TRUE;
868#endif
869
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000870 /* If ":startinsert" command used, stuff a dummy command to be able to
871 * call normal_cmd(), which will then start Insert mode. */
872 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000873 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000874
875#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200876 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200877 {
878# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200879# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200880 && !defined(FEAT_GUI_W32)
881 if (gui.in_use)
882 {
883 mch_errmsg(_("netbeans is not supported with this GUI\n"));
884 mch_exit(2);
885 }
886# endif
887# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000888 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200889 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200890 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891#endif
892
893 TIME_MSG("before starting main loop");
894
895 /*
896 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100897 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000898 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000899
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200900#endif /* NO_VIM_MAIN */
901
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000902 return 0;
903}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000904
905/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200906 * Initialisation shared by main() and some tests.
907 */
908 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200909common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200910{
911
912#ifdef FEAT_MBYTE
913 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
914#endif
915#ifdef FEAT_EVAL
916 eval_init(); /* init global variables */
917#endif
918
919#ifdef __QNXNTO__
920 qnx_init(); /* PhAttach() for clipboard, (and gui) */
921#endif
922
923#ifdef MAC_OS_CLASSIC
924 /* Prepare for possibly starting GUI sometime */
925 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200926 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200927 TIME_MSG("GUI prepared");
928#endif
929
930 /* Init the table of Normal mode commands. */
931 init_normal_cmds();
932
933#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
934 make_version(); /* Construct the long version string. */
935#endif
936
937 /*
938 * Allocate space for the generic buffers (needed for set_init_1() and
939 * EMSG2()).
940 */
941 if ((IObuff = alloc(IOSIZE)) == NULL
942 || (NameBuff = alloc(MAXPATHL)) == NULL)
943 mch_exit(0);
944 TIME_MSG("Allocated generic buffers");
945
946#ifdef NBDEBUG
947 /* Wait a moment for debugging NetBeans. Must be after allocating
948 * NameBuff. */
949 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
950 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
951 TIME_MSG("NetBeans debug wait");
952#endif
953
954#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
955 /*
956 * Setup to use the current locale (for ctype() and many other things).
957 * NOTE: Translated messages with encodings other than latin1 will not
958 * work until set_init_1() has been called!
959 */
960 init_locale();
961 TIME_MSG("locale set");
962#endif
963
964#ifdef FEAT_GUI
965 gui.dofork = TRUE; /* default is to use fork() */
966#endif
967
968 /*
969 * Do a first scan of the arguments in "argv[]":
970 * -display or --display
971 * --server...
972 * --socketid
973 * --windowid
974 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200975 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200976
977#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200978 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200979#endif
980#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
981 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200982 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200983 TIME_MSG("GUI prepared");
984#endif
985
986#ifdef FEAT_CLIPBOARD
987 clip_init(FALSE); /* Initialise clipboard stuff */
988 TIME_MSG("clipboard setup");
989#endif
990
991 /*
992 * Check if we have an interactive window.
993 * On the Amiga: If there is no window, we open one with a newcli command
994 * (needed for :! to * work). mch_check_win() will also handle the -d or
995 * -dev argument.
996 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100997 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200998 TIME_MSG("window checked");
999
1000 /*
1001 * Allocate the first window and buffer.
1002 * Can't do anything without it, exit when it fails.
1003 */
1004 if (win_alloc_first() == FAIL)
1005 mch_exit(0);
1006
1007 init_yank(); /* init yank buffers */
1008
1009 alist_init(&global_alist); /* Init the argument list to empty. */
1010 global_alist.id = 0;
1011
1012 /*
1013 * Set the default values for the options.
1014 * NOTE: Non-latin1 translated messages are working only after this,
1015 * because this is where "has_mbyte" will be set, which is used by
1016 * msg_outtrans_len_attr().
1017 * First find out the home directory, needed to expand "~" in options.
1018 */
1019 init_homedir(); /* find real value of $HOME */
1020 set_init_1();
1021 TIME_MSG("inits 1");
1022
1023#ifdef FEAT_EVAL
1024 set_lang_var(); /* set v:lang and v:ctype */
1025#endif
1026}
1027
1028/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001029 * Return TRUE when the --not-a-term argument was found.
1030 */
1031 int
1032is_not_a_term()
1033{
1034 return params.not_a_term;
1035}
1036
1037/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038 * Main loop: Execute Normal mode commands until exiting Vim.
1039 * Also used to handle commands in the command-line window, until the window
1040 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 * Also used to handle ":visual" command after ":global": execute Normal mode
1042 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043 */
1044 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001045main_loop(
1046 int cmdwin, /* TRUE when working in the command-line window */
1047 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001048{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001049 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001050 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001051#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001052 /* these are static to avoid a compiler warning */
1053 static linenr_T conceal_old_cursor_line = 0;
1054 static linenr_T conceal_new_cursor_line = 0;
1055 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001056#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001057
1058#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1059 /* Setup to catch a terminating error from the X server. Just ignore
1060 * it, restore the state and continue. This might not always work
1061 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001062 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001064 {
1065 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001066 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001067 got_int = TRUE;
1068 need_wait_return = FALSE;
1069 global_busy = FALSE;
1070 exmode_active = 0;
1071 skip_redraw = FALSE;
1072 RedrawingDisabled = 0;
1073 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001074 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001075# ifdef FEAT_EVAL
1076 emsg_skip = 0;
1077# endif
1078 emsg_off = 0;
1079# ifdef FEAT_MOUSE
1080 setmouse();
1081# endif
1082 settmode(TMODE_RAW);
1083 starttermcap();
1084 scroll_start();
1085 redraw_later_clear();
1086 }
1087#endif
1088
1089 clear_oparg(&oa);
1090 while (!cmdwin
1091#ifdef FEAT_CMDWIN
1092 || cmdwin_result == 0
1093#endif
1094 )
1095 {
1096 if (stuff_empty())
1097 {
1098 did_check_timestamps = FALSE;
1099 if (need_check_timestamps)
1100 check_timestamps(FALSE);
1101 if (need_wait_return) /* if wait_return still needed ... */
1102 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001103 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001104 {
1105 need_start_insertmode = FALSE;
1106 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1107 /* skip the fileinfo message now, because it would be shown
1108 * after insert mode finishes! */
1109 need_fileinfo = FALSE;
1110 }
1111 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001112
1113 /* Reset "got_int" now that we got back to the main loop. Except when
1114 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1115 * the ":g" command.
1116 * For ":g/pat/vi" we reset "got_int" when used once. When used
1117 * a second time we go back to Ex mode and abort the ":g" command. */
1118 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001119 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001120 if (noexmode && global_busy && !exmode_active && previous_got_int)
1121 {
1122 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1123 * used and keep "got_int" set, so that it aborts ":g". */
1124 exmode_active = EXMODE_NORMAL;
1125 State = NORMAL;
1126 }
1127 else if (!global_busy || !exmode_active)
1128 {
1129 if (!quit_more)
1130 (void)vgetc(); /* flush all buffers */
1131 got_int = FALSE;
1132 }
1133 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001134 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001135 else
1136 previous_got_int = FALSE;
1137
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001138 if (!exmode_active)
1139 msg_scroll = FALSE;
1140 quit_more = FALSE;
1141
1142 /*
1143 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1144 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1145 * update cursor and redraw.
1146 */
1147 if (skip_redraw || exmode_active)
1148 skip_redraw = FALSE;
1149 else if (do_redraw || stuff_empty())
1150 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001151# ifdef FEAT_GUI
1152 /* If ui_breakcheck() was used a resize may have been postponed. */
1153 gui_may_resize_shell();
1154# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001155#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001156 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001157 if (!finish_op && (
1158# ifdef FEAT_AUTOCMD
1159 has_cursormoved()
1160# endif
1161# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1162 ||
1163# endif
1164# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001165 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001166# endif
1167 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001168# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001169 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001170# endif
1171 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001172 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001173# ifdef FEAT_AUTOCMD
1174 if (has_cursormoved())
1175 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1176 FALSE, curbuf);
1177# endif
1178# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001179 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001180 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001181# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001182 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001183# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001184 conceal_new_cursor_line = curwin->w_cursor.lnum;
1185 conceal_update_lines = TRUE;
1186 }
1187# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001188# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001189 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001190# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001191 }
1192#endif
1193
Bram Moolenaar186628f2013-03-19 13:33:23 +01001194#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001195 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001196 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001197 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001198 {
1199 if (last_changedtick_buf == curbuf)
1200 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1201 FALSE, curbuf);
1202 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001203 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001204 }
1205#endif
1206
Bram Moolenaar33aec762006-01-22 23:30:12 +00001207#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1208 /* Scroll-binding for diff mode may have been postponed until
1209 * here. Avoids doing it for every change. */
1210 if (diff_need_scrollbind)
1211 {
1212 check_scrollbind((linenr_T)0, 0L);
1213 diff_need_scrollbind = FALSE;
1214 }
1215#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001216#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001217 /* Include a closed fold completely in the Visual area. */
1218 foldAdjustVisual();
1219#endif
1220#ifdef FEAT_FOLDING
1221 /*
1222 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1223 * contain the cursor.
1224 * When 'foldopen' is "all", open the fold(s) under the cursor.
1225 * This may mark the window for redrawing.
1226 */
1227 if (hasAnyFolding(curwin) && !char_avail())
1228 {
1229 foldCheckClose();
1230 if (fdo_flags & FDO_ALL)
1231 foldOpenCursor();
1232 }
1233#endif
1234
1235 /*
1236 * Before redrawing, make sure w_topline is correct, and w_leftcol
1237 * if lines don't wrap, and w_skipcol if lines wrap.
1238 */
1239 update_topline();
1240 validate_cursor();
1241
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001242 if (VIsual_active)
1243 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001244 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001245 update_screen(0);
1246 else if (redraw_cmdline || clear_cmdline)
1247 showmode();
1248#ifdef FEAT_WINDOWS
1249 redraw_statuslines();
1250#endif
1251#ifdef FEAT_TITLE
1252 if (need_maketitle)
1253 maketitle();
1254#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001255#ifdef FEAT_VIMINFO
1256 curbuf->b_last_used = vim_time();
1257#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001258 /* display message after redraw */
1259 if (keep_msg != NULL)
1260 {
1261 char_u *p;
1262
1263 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001264 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1265 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001266 p = keep_msg;
1267 msg_attr(p, keep_msg_attr);
1268 vim_free(p);
1269 }
1270 if (need_fileinfo) /* show file info after redraw */
1271 {
1272 fileinfo(FALSE, TRUE, FALSE);
1273 need_fileinfo = FALSE;
1274 }
1275
1276 emsg_on_display = FALSE; /* can delete error message now */
1277 did_emsg = FALSE;
1278 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001279 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 showruler(FALSE);
1281
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001282# if defined(FEAT_CONCEAL)
1283 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001284 && (conceal_old_cursor_line != conceal_new_cursor_line
1285 || conceal_cursor_line(curwin)
1286 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001287 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001288 if (conceal_old_cursor_line != conceal_new_cursor_line
1289 && conceal_old_cursor_line
1290 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001291 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001292 update_single_line(curwin, conceal_new_cursor_line);
1293 curwin->w_valid &= ~VALID_CROW;
1294 }
1295# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001296 setcursor();
1297 cursor_on();
1298
1299 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001300
1301#ifdef STARTUPTIME
1302 /* Now that we have drawn the first screen all the startup stuff
1303 * has been done, close any file for startup messages. */
1304 if (time_fd != NULL)
1305 {
1306 TIME_MSG("first screen update");
1307 TIME_MSG("--- VIM STARTED ---");
1308 fclose(time_fd);
1309 time_fd = NULL;
1310 }
1311#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001312 }
1313#ifdef FEAT_GUI
1314 if (need_mouse_correct)
1315 gui_mouse_correct();
1316#endif
1317
1318 /*
1319 * Update w_curswant if w_set_curswant has been set.
1320 * Postponed until here to avoid computing w_virtcol too often.
1321 */
1322 update_curswant();
1323
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001324#ifdef FEAT_EVAL
1325 /*
1326 * May perform garbage collection when waiting for a character, but
1327 * only at the very toplevel. Otherwise we may be using a List or
1328 * Dict internally somewhere.
1329 * "may_garbage_collect" is reset in vgetc() which is invoked through
1330 * do_exmode() and normal_cmd().
1331 */
1332 may_garbage_collect = (!cmdwin && !noexmode);
1333#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001334 /*
1335 * If we're invoked as ex, do a round of ex commands.
1336 * Otherwise, get and execute a normal mode command.
1337 */
1338 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001339 {
1340 if (noexmode) /* End of ":global/path/visual" commands */
1341 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001343 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001344 else
1345 normal_cmd(&oa, TRUE);
1346 }
1347}
1348
1349
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001350#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001351/*
1352 * Exit, but leave behind swap files for modified buffers.
1353 */
1354 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001355getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001356{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001357# if defined(SIGHUP) && defined(SIG_IGN)
1358 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1359 * makes Vim exit and then handling SIGHUP causes various reentrance
1360 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001361 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001362# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001363
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364 ml_close_notmod(); /* close all not-modified buffers */
1365 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1366 ml_close_all(FALSE); /* close all memfiles, without deleting */
1367 getout(exitval); /* exit Vim properly */
1368}
1369#endif
1370
1371
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001372/*
1373 * Exit properly.
1374 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001375 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001376getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001377{
1378#ifdef FEAT_AUTOCMD
1379 buf_T *buf;
1380 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001381 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001382#endif
1383
1384 exiting = TRUE;
1385
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001386 /* When running in Ex mode an error causes us to exit with a non-zero exit
1387 * code. POSIX requires this, although it's not 100% clear from the
1388 * standard. */
1389 if (exmode_active)
1390 exitval += ex_exitval;
1391
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001392 /* Position the cursor on the last screen line, below all the text */
1393#ifdef FEAT_GUI
1394 if (!gui.in_use)
1395#endif
1396 windgoto((int)Rows - 1, 0);
1397
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001398#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1399 /* Optionally print hashtable efficiency. */
1400 hash_debug_results();
1401#endif
1402
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001403#ifdef FEAT_GUI
1404 msg_didany = FALSE;
1405#endif
1406
1407#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001408 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001410 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1411# if defined FEAT_WINDOWS
1412 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001414 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001415 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001416 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001417 if (wp->w_buffer == NULL)
1418 /* Autocmd must have close the buffer already, skip. */
1419 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001420 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001421 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 {
1423 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1424 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001425 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 /* start all over, autocommands may mess up the lists */
1427 next_tp = first_tabpage;
1428 break;
1429 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001430 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001432# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1434 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001436
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001437 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001438 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 if (buf->b_ml.ml_mfp != NULL)
1440 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001441 bufref_T bufref;
1442
1443 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001444 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001445 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001446 if (!bufref_valid(&bufref))
1447 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001448 break;
1449 }
1450 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1451 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001452#endif
1453
1454#ifdef FEAT_VIMINFO
1455 if (*p_viminfo != NUL)
1456 /* Write out the registers, history, marks etc, to the viminfo file */
1457 write_viminfo(NULL, FALSE);
1458#endif
1459
1460#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001461 if (get_vim_var_nr(VV_DYING) <= 1)
1462 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001463#endif
1464
Bram Moolenaar05159a02005-02-26 23:04:13 +00001465#ifdef FEAT_PROFILE
1466 profile_dump();
1467#endif
1468
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001469 if (did_emsg
1470#ifdef FEAT_GUI
1471 || (gui.in_use && msg_didany && p_verbose > 0)
1472#endif
1473 )
1474 {
1475 /* give the user a chance to read the (error) message */
1476 no_wait_return = FALSE;
1477 wait_return(FALSE);
1478 }
1479
1480#ifdef FEAT_AUTOCMD
1481 /* Position the cursor again, the autocommands may have moved it */
1482# ifdef FEAT_GUI
1483 if (!gui.in_use)
1484# endif
1485 windgoto((int)Rows - 1, 0);
1486#endif
1487
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001488#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001489 job_stop_on_exit();
1490#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001491#ifdef FEAT_LUA
1492 lua_end();
1493#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001494#ifdef FEAT_MZSCHEME
1495 mzscheme_end();
1496#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001497#ifdef FEAT_TCL
1498 tcl_end();
1499#endif
1500#ifdef FEAT_RUBY
1501 ruby_end();
1502#endif
1503#ifdef FEAT_PYTHON
1504 python_end();
1505#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001506#ifdef FEAT_PYTHON3
1507 python3_end();
1508#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001509#ifdef FEAT_PERL
1510 perl_end();
1511#endif
1512#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1513 iconv_end();
1514#endif
1515#ifdef FEAT_NETBEANS_INTG
1516 netbeans_end();
1517#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001518#ifdef FEAT_CSCOPE
1519 cs_end();
1520#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001521#ifdef FEAT_EVAL
1522 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001523 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001524#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001525#if defined(WIN32) && defined(FEAT_MBYTE)
1526 free_cmd_argsW();
1527#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001528
1529 mch_exit(exitval);
1530}
1531
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001532#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1533/*
1534 * Setup to use the current locale (for ctype() and many other things).
1535 */
1536 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001537init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001538{
1539 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001540
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001541# ifdef FEAT_GUI_GTK
1542 /* Tell Gtk not to change our locale settings. */
1543 gtk_disable_setlocale();
1544# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001545# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1546 /* Make sure strtod() uses a decimal point, not a comma. */
1547 setlocale(LC_NUMERIC, "C");
1548# endif
1549
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001550# ifdef WIN32
1551 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1552 * text while it's expecting text in the current locale. This call avoids
1553 * that. */
1554 setlocale(LC_CTYPE, "C");
1555# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001556
1557# ifdef FEAT_GETTEXT
1558 {
1559 int mustfree = FALSE;
1560 char_u *p;
1561
1562# ifdef DYNAMIC_GETTEXT
1563 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001564 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001565# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001566 /* expand_env() doesn't work yet, because g_chartab[] is not
1567 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001568 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1569 if (p != NULL && *p != NUL)
1570 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001571 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001572 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1573 }
1574 if (mustfree)
1575 vim_free(p);
1576 textdomain(VIMPACKAGE);
1577 }
1578# endif
1579}
1580#endif
1581
1582/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001583 * Get the name of the display, before gui_prepare() removes it from
1584 * argv[]. Used for the xterm-clipboard display.
1585 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001586 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001587 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001588 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001589early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001590{
Bram Moolenaar03005972008-11-20 13:12:36 +00001591#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1592 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001593 int argc = parmp->argc;
1594 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001595 int i;
1596
1597 for (i = 1; i < argc; i++)
1598 {
1599 if (STRCMP(argv[i], "--") == 0)
1600 break;
1601# ifdef FEAT_XCLIPBOARD
1602 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001603# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001604 || STRICMP(argv[i], "--display") == 0
1605# endif
1606 )
1607 {
1608 if (i == argc - 1)
1609 mainerr_arg_missing((char_u *)argv[i]);
1610 xterm_display = argv[++i];
1611 }
1612# endif
1613# ifdef FEAT_CLIENTSERVER
1614 else if (STRICMP(argv[i], "--servername") == 0)
1615 {
1616 if (i == argc - 1)
1617 mainerr_arg_missing((char_u *)argv[i]);
1618 parmp->serverName_arg = (char_u *)argv[++i];
1619 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001620 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001621 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001622 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001623 {
1624 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001625# ifdef FEAT_GUI
1626 if (strstr(argv[i], "-wait") != 0)
1627 /* don't fork() when starting the GUI to edit files ourself */
1628 gui.dofork = FALSE;
1629# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001630 }
1631# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001632
1633# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1634# ifdef FEAT_GUI_W32
1635 else if (STRICMP(argv[i], "--windowid") == 0)
1636# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001637 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001638# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001639 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001640 long_u id;
1641 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001642
1643 if (i == argc - 1)
1644 mainerr_arg_missing((char_u *)argv[i]);
1645 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001646 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001647 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001648 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001649 if (count != 1)
1650 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1651 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001652# ifdef FEAT_GUI_W32
1653 win_socket_id = id;
1654# else
1655 gtk_socket_id = id;
1656# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001657 i++;
1658 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001659# endif
1660# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001661 else if (STRICMP(argv[i], "--echo-wid") == 0)
1662 echo_wid_arg = TRUE;
1663# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001664# ifndef FEAT_NETBEANS_INTG
1665 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001666 {
1667 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1668 mch_exit(2);
1669 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001670# endif
1671
Bram Moolenaar58d98232005-07-23 22:25:46 +00001672 }
1673#endif
1674}
1675
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001676#ifndef NO_VIM_MAIN
1677/*
1678 * Get a (optional) count for a Vim argument.
1679 */
1680 static int
1681get_number_arg(
1682 char_u *p, /* pointer to argument */
1683 int *idx, /* index in argument, is incremented */
1684 int def) /* default value */
1685{
1686 if (vim_isdigit(p[*idx]))
1687 {
1688 def = atoi((char *)&(p[*idx]));
1689 while (vim_isdigit(p[*idx]))
1690 *idx = *idx + 1;
1691 }
1692 return def;
1693}
1694
1695/*
1696 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1697 * If the executable name starts with "r" we disable shell commands.
1698 * If the next character is "e" we run in Easy mode.
1699 * If the next character is "g" we run the GUI version.
1700 * If the next characters are "view" we start in readonly mode.
1701 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1702 * If the next characters are "ex" we start in Ex mode. If it's followed
1703 * by "im" use improved Ex mode.
1704 */
1705 static void
1706parse_command_name(mparm_T *parmp)
1707{
1708 char_u *initstr;
1709
1710 initstr = gettail((char_u *)parmp->argv[0]);
1711
1712#ifdef MACOS_X_UNIX
1713 /* An issue has been seen when launching Vim in such a way that
1714 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1715 * executable or a symbolic link of it. Until this issue is resolved
1716 * we prohibit the GUI from being used.
1717 */
1718 if (STRCMP(initstr, parmp->argv[0]) == 0)
1719 disallow_gui = TRUE;
1720
1721 /* TODO: On MacOS X default to gui if argv[0] ends in:
1722 * /Vim.app/Contents/MacOS/Vim */
1723#endif
1724
1725#ifdef FEAT_EVAL
1726 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001727 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001728#endif
1729
1730 if (TOLOWER_ASC(initstr[0]) == 'r')
1731 {
1732 restricted = TRUE;
1733 ++initstr;
1734 }
1735
1736 /* Use evim mode for "evim" and "egvim", not for "editor". */
1737 if (TOLOWER_ASC(initstr[0]) == 'e'
1738 && (TOLOWER_ASC(initstr[1]) == 'v'
1739 || TOLOWER_ASC(initstr[1]) == 'g'))
1740 {
1741#ifdef FEAT_GUI
1742 gui.starting = TRUE;
1743#endif
1744 parmp->evim_mode = TRUE;
1745 ++initstr;
1746 }
1747
1748 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1749 if (TOLOWER_ASC(initstr[0]) == 'g')
1750 {
1751 main_start_gui();
1752#ifdef FEAT_GUI
1753 ++initstr;
1754#endif
1755 }
1756
1757 if (STRNICMP(initstr, "view", 4) == 0)
1758 {
1759 readonlymode = TRUE;
1760 curbuf->b_p_ro = TRUE;
1761 p_uc = 10000; /* don't update very often */
1762 initstr += 4;
1763 }
1764 else if (STRNICMP(initstr, "vim", 3) == 0)
1765 initstr += 3;
1766
1767 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1768 if (STRICMP(initstr, "diff") == 0)
1769 {
1770#ifdef FEAT_DIFF
1771 parmp->diff_mode = TRUE;
1772#else
1773 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1774 mch_errmsg("\n");
1775 mch_exit(2);
1776#endif
1777 }
1778
1779 if (STRNICMP(initstr, "ex", 2) == 0)
1780 {
1781 if (STRNICMP(initstr + 2, "im", 2) == 0)
1782 exmode_active = EXMODE_VIM;
1783 else
1784 exmode_active = EXMODE_NORMAL;
1785 change_compatible(TRUE); /* set 'compatible' */
1786 }
1787}
1788
Bram Moolenaar58d98232005-07-23 22:25:46 +00001789/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001790 * Scan the command line arguments.
1791 */
1792 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001793command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001794{
1795 int argc = parmp->argc;
1796 char **argv = parmp->argv;
1797 int argv_idx; /* index in argv[n][] */
1798 int had_minmin = FALSE; /* found "--" argument */
1799 int want_argument; /* option argument with argument */
1800 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001801 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001802 long n;
1803
1804 --argc;
1805 ++argv;
1806 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1807 while (argc > 0)
1808 {
1809 /*
1810 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1811 */
1812 if (argv[0][0] == '+' && !had_minmin)
1813 {
1814 if (parmp->n_commands >= MAX_ARG_CMDS)
1815 mainerr(ME_EXTRA_CMD, NULL);
1816 argv_idx = -1; /* skip to next argument */
1817 if (argv[0][1] == NUL)
1818 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1819 else
1820 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1821 }
1822
1823 /*
1824 * Optional argument.
1825 */
1826 else if (argv[0][0] == '-' && !had_minmin)
1827 {
1828 want_argument = FALSE;
1829 c = argv[0][argv_idx++];
1830#ifdef VMS
1831 /*
1832 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1833 * and "-/X" as "-X".
1834 */
1835 if (c == '/')
1836 {
1837 c = argv[0][argv_idx++];
1838 c = TOUPPER_ASC(c);
1839 }
1840 else
1841 c = TOLOWER_ASC(c);
1842#endif
1843 switch (c)
1844 {
1845 case NUL: /* "vim -" read from stdin */
1846 /* "ex -" silent mode */
1847 if (exmode_active)
1848 silent_mode = TRUE;
1849 else
1850 {
1851 if (parmp->edit_type != EDIT_NONE)
1852 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1853 parmp->edit_type = EDIT_STDIN;
1854 read_cmd_fd = 2; /* read from stderr instead of stdin */
1855 }
1856 argv_idx = -1; /* skip to next argument */
1857 break;
1858
1859 case '-': /* "--" don't take any more option arguments */
1860 /* "--help" give help message */
1861 /* "--version" give version message */
1862 /* "--literal" take files literally */
1863 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001864 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001865 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001866 /* "--noplugin[s]" skip plugins */
1867 /* "--cmd <cmd>" execute cmd before vimrc */
1868 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1869 usage();
1870 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1871 {
1872 Columns = 80; /* need to init Columns */
1873 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1874 list_version();
1875 msg_putchar('\n');
1876 msg_didout = FALSE;
1877 mch_exit(0);
1878 }
1879 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1880 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001881#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001882 parmp->literal = TRUE;
1883#endif
1884 }
1885 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1886 {
1887#ifdef FEAT_GUI
1888 gui.dofork = FALSE; /* don't fork() when starting GUI */
1889#endif
1890 }
1891 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1892 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001893 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1894 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001895 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1896 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001897 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1898 {
1899 want_argument = TRUE;
1900 argv_idx += 3;
1901 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001902 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1903 {
1904 want_argument = TRUE;
1905 argv_idx += 11;
1906 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001907#ifdef FEAT_CLIENTSERVER
1908 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1909 ; /* already processed -- no arg */
1910 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1911 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1912 {
1913 /* already processed -- snatch the following arg */
1914 if (argc > 1)
1915 {
1916 --argc;
1917 ++argv;
1918 }
1919 }
1920#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001921#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1922# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001923 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001924# else
1925 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1926# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001927 {
1928 /* already processed -- snatch the following arg */
1929 if (argc > 1)
1930 {
1931 --argc;
1932 ++argv;
1933 }
1934 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001935#endif
1936#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001937 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1938 {
1939 /* already processed, skip */
1940 }
1941#endif
1942 else
1943 {
1944 if (argv[0][argv_idx])
1945 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1946 had_minmin = TRUE;
1947 }
1948 if (!want_argument)
1949 argv_idx = -1; /* skip to next argument */
1950 break;
1951
1952 case 'A': /* "-A" start in Arabic mode */
1953#ifdef FEAT_ARABIC
1954 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1955#else
1956 mch_errmsg(_(e_noarabic));
1957 mch_exit(2);
1958#endif
1959 break;
1960
1961 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001962 /* Needs to be effective before expanding file names, because
1963 * for Win32 this makes us edit a shortcut file itself,
1964 * instead of the file it links to. */
1965 set_options_bin(curbuf->b_p_bin, 1, 0);
1966 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001967 break;
1968
1969 case 'C': /* "-C" Compatible */
1970 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001971 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001972 break;
1973
1974 case 'e': /* "-e" Ex mode */
1975 exmode_active = EXMODE_NORMAL;
1976 break;
1977
1978 case 'E': /* "-E" Improved Ex mode */
1979 exmode_active = EXMODE_VIM;
1980 break;
1981
1982 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1983 window directly, not with newcli */
1984#ifdef FEAT_GUI
1985 gui.dofork = FALSE; /* don't fork() when starting GUI */
1986#endif
1987 break;
1988
1989 case 'g': /* "-g" start GUI */
1990 main_start_gui();
1991 break;
1992
1993 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1994#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001995 p_fkmap = TRUE;
1996 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001997#else
1998 mch_errmsg(_(e_nofarsi));
1999 mch_exit(2);
2000#endif
2001 break;
2002
2003 case 'h': /* "-h" give help message */
2004#ifdef FEAT_GUI_GNOME
2005 /* Tell usage() to exit for "gvim". */
2006 gui.starting = FALSE;
2007#endif
2008 usage();
2009 break;
2010
2011 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2012#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002013 p_hkmap = TRUE;
2014 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002015#else
2016 mch_errmsg(_(e_nohebrew));
2017 mch_exit(2);
2018#endif
2019 break;
2020
2021 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2022#ifdef FEAT_LISP
2023 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2024 p_sm = TRUE;
2025#endif
2026 break;
2027
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002028 case 'M': /* "-M" no changes or writing of files */
2029 reset_modifiable();
2030 /* FALLTHROUGH */
2031
2032 case 'm': /* "-m" no writing of files */
2033 p_write = FALSE;
2034 break;
2035
2036 case 'y': /* "-y" easy mode */
2037#ifdef FEAT_GUI
2038 gui.starting = TRUE; /* start GUI a bit later */
2039#endif
2040 parmp->evim_mode = TRUE;
2041 break;
2042
2043 case 'N': /* "-N" Nocompatible */
2044 change_compatible(FALSE);
2045 break;
2046
2047 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002048#ifdef FEAT_NETBEANS_INTG
2049 /* checking for "-nb", netbeans parameters */
2050 if (argv[0][argv_idx] == 'b')
2051 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002052 netbeansArg = argv[0];
2053 argv_idx = -1; /* skip to next argument */
2054 }
2055 else
2056#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002057 parmp->no_swap_file = TRUE;
2058 break;
2059
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002060 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002061#ifdef TARGET_API_MAC_OSX
2062 /* For some reason on MacOS X, an argument like:
2063 -psn_0_10223617 is passed in when invoke from Finder
2064 or with the 'open' command */
2065 if (argv[0][argv_idx] == 's')
2066 {
2067 argv_idx = -1; /* bypass full -psn */
2068 main_start_gui();
2069 break;
2070 }
2071#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002072#ifdef FEAT_WINDOWS
2073 /* default is 0: open window for each file */
2074 parmp->window_count = get_number_arg((char_u *)argv[0],
2075 &argv_idx, 0);
2076 parmp->window_layout = WIN_TABS;
2077#endif
2078 break;
2079
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002080 case 'o': /* "-o[N]" open N horizontal split windows */
2081#ifdef FEAT_WINDOWS
2082 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002083 parmp->window_count = get_number_arg((char_u *)argv[0],
2084 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002085 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002086#endif
2087 break;
2088
2089 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002090#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002091 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002092 parmp->window_count = get_number_arg((char_u *)argv[0],
2093 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002094 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002095#endif
2096 break;
2097
2098#ifdef FEAT_QUICKFIX
2099 case 'q': /* "-q" QuickFix mode */
2100 if (parmp->edit_type != EDIT_NONE)
2101 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2102 parmp->edit_type = EDIT_QF;
2103 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2104 {
2105 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2106 argv_idx = -1;
2107 }
2108 else if (argc > 1) /* "-q {errorfile}" */
2109 want_argument = TRUE;
2110 break;
2111#endif
2112
2113 case 'R': /* "-R" readonly mode */
2114 readonlymode = TRUE;
2115 curbuf->b_p_ro = TRUE;
2116 p_uc = 10000; /* don't update very often */
2117 break;
2118
2119 case 'r': /* "-r" recovery mode */
2120 case 'L': /* "-L" recovery mode */
2121 recoverymode = 1;
2122 break;
2123
2124 case 's':
2125 if (exmode_active) /* "-s" silent (batch) mode */
2126 silent_mode = TRUE;
2127 else /* "-s {scriptin}" read from script file */
2128 want_argument = TRUE;
2129 break;
2130
2131 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2132 if (parmp->edit_type != EDIT_NONE)
2133 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2134 parmp->edit_type = EDIT_TAG;
2135 if (argv[0][argv_idx]) /* "-t{tag}" */
2136 {
2137 parmp->tagname = (char_u *)argv[0] + argv_idx;
2138 argv_idx = -1;
2139 }
2140 else /* "-t {tag}" */
2141 want_argument = TRUE;
2142 break;
2143
2144#ifdef FEAT_EVAL
2145 case 'D': /* "-D" Debugging */
2146 parmp->use_debug_break_level = 9999;
2147 break;
2148#endif
2149#ifdef FEAT_DIFF
2150 case 'd': /* "-d" 'diff' */
2151# ifdef AMIGA
2152 /* check for "-dev {device}" */
2153 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2154 want_argument = TRUE;
2155 else
2156# endif
2157 parmp->diff_mode = TRUE;
2158 break;
2159#endif
2160 case 'V': /* "-V{N}" Verbose level */
2161 /* default is 10: a little bit verbose */
2162 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2163 if (argv[0][argv_idx] != NUL)
2164 {
2165 set_option_value((char_u *)"verbosefile", 0L,
2166 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002167 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002168 }
2169 break;
2170
2171 case 'v': /* "-v" Vi-mode (as if called "vi") */
2172 exmode_active = 0;
2173#ifdef FEAT_GUI
2174 gui.starting = FALSE; /* don't start GUI */
2175#endif
2176 break;
2177
2178 case 'w': /* "-w{number}" set window height */
2179 /* "-w {scriptout}" write to script */
2180 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2181 {
2182 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2183 set_option_value((char_u *)"window", n, NULL, 0);
2184 break;
2185 }
2186 want_argument = TRUE;
2187 break;
2188
2189#ifdef FEAT_CRYPT
2190 case 'x': /* "-x" encrypted reading/writing of files */
2191 parmp->ask_for_key = TRUE;
2192 break;
2193#endif
2194
2195 case 'X': /* "-X" don't connect to X server */
2196#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2197 x_no_connect = TRUE;
2198#endif
2199 break;
2200
2201 case 'Z': /* "-Z" restricted mode */
2202 restricted = TRUE;
2203 break;
2204
2205 case 'c': /* "-c{command}" or "-c {command}" execute
2206 command */
2207 if (argv[0][argv_idx] != NUL)
2208 {
2209 if (parmp->n_commands >= MAX_ARG_CMDS)
2210 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002211 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2212 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002213 argv_idx = -1;
2214 break;
2215 }
2216 /*FALLTHROUGH*/
2217 case 'S': /* "-S {file}" execute Vim script */
2218 case 'i': /* "-i {viminfo}" use for viminfo */
2219#ifndef FEAT_DIFF
2220 case 'd': /* "-d {device}" device (for Amiga) */
2221#endif
2222 case 'T': /* "-T {terminal}" terminal name */
2223 case 'u': /* "-u {vimrc}" vim inits file */
2224 case 'U': /* "-U {gvimrc}" gvim inits file */
2225 case 'W': /* "-W {scriptout}" overwrite */
2226#ifdef FEAT_GUI_W32
2227 case 'P': /* "-P {parent title}" MDI parent */
2228#endif
2229 want_argument = TRUE;
2230 break;
2231
2232 default:
2233 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2234 }
2235
2236 /*
2237 * Handle option arguments with argument.
2238 */
2239 if (want_argument)
2240 {
2241 /*
2242 * Check for garbage immediately after the option letter.
2243 */
2244 if (argv[0][argv_idx] != NUL)
2245 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2246
2247 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002248 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002249 mainerr_arg_missing((char_u *)argv[0]);
2250 ++argv;
2251 argv_idx = -1;
2252
2253 switch (c)
2254 {
2255 case 'c': /* "-c {command}" execute command */
2256 case 'S': /* "-S {file}" execute Vim script */
2257 if (parmp->n_commands >= MAX_ARG_CMDS)
2258 mainerr(ME_EXTRA_CMD, NULL);
2259 if (c == 'S')
2260 {
2261 char *a;
2262
2263 if (argc < 1)
2264 /* "-S" without argument: use default session file
2265 * name. */
2266 a = SESSION_FILE;
2267 else if (argv[0][0] == '-')
2268 {
2269 /* "-S" followed by another option: use default
2270 * session file name. */
2271 a = SESSION_FILE;
2272 ++argc;
2273 --argv;
2274 }
2275 else
2276 a = argv[0];
2277 p = alloc((unsigned)(STRLEN(a) + 4));
2278 if (p == NULL)
2279 mch_exit(2);
2280 sprintf((char *)p, "so %s", a);
2281 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2282 parmp->commands[parmp->n_commands++] = p;
2283 }
2284 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002285 parmp->commands[parmp->n_commands++] =
2286 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002287 break;
2288
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002289 case '-':
2290 if (argv[-1][2] == 'c')
2291 {
2292 /* "--cmd {command}" execute command */
2293 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2294 mainerr(ME_EXTRA_CMD, NULL);
2295 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002296 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002297 }
2298 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002299 break;
2300
2301 /* case 'd': -d {device} is handled in mch_check_win() for the
2302 * Amiga */
2303
2304#ifdef FEAT_QUICKFIX
2305 case 'q': /* "-q {errorfile}" QuickFix mode */
2306 parmp->use_ef = (char_u *)argv[0];
2307 break;
2308#endif
2309
2310 case 'i': /* "-i {viminfo}" use for viminfo */
2311 use_viminfo = (char_u *)argv[0];
2312 break;
2313
2314 case 's': /* "-s {scriptin}" read from script file */
2315 if (scriptin[0] != NULL)
2316 {
2317scripterror:
2318 mch_errmsg(_("Attempt to open script file again: \""));
2319 mch_errmsg(argv[-1]);
2320 mch_errmsg(" ");
2321 mch_errmsg(argv[0]);
2322 mch_errmsg("\"\n");
2323 mch_exit(2);
2324 }
2325 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2326 {
2327 mch_errmsg(_("Cannot open for reading: \""));
2328 mch_errmsg(argv[0]);
2329 mch_errmsg("\"\n");
2330 mch_exit(2);
2331 }
2332 if (save_typebuf() == FAIL)
2333 mch_exit(2); /* out of memory */
2334 break;
2335
2336 case 't': /* "-t {tag}" */
2337 parmp->tagname = (char_u *)argv[0];
2338 break;
2339
2340 case 'T': /* "-T {terminal}" terminal name */
2341 /*
2342 * The -T term argument is always available and when
2343 * HAVE_TERMLIB is supported it overrides the environment
2344 * variable TERM.
2345 */
2346#ifdef FEAT_GUI
2347 if (term_is_gui((char_u *)argv[0]))
2348 gui.starting = TRUE; /* start GUI a bit later */
2349 else
2350#endif
2351 parmp->term = (char_u *)argv[0];
2352 break;
2353
2354 case 'u': /* "-u {vimrc}" vim inits file */
2355 parmp->use_vimrc = (char_u *)argv[0];
2356 break;
2357
2358 case 'U': /* "-U {gvimrc}" gvim inits file */
2359#ifdef FEAT_GUI
2360 use_gvimrc = (char_u *)argv[0];
2361#endif
2362 break;
2363
2364 case 'w': /* "-w {nr}" 'window' value */
2365 /* "-w {scriptout}" append to script file */
2366 if (vim_isdigit(*((char_u *)argv[0])))
2367 {
2368 argv_idx = 0;
2369 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2370 set_option_value((char_u *)"window", n, NULL, 0);
2371 argv_idx = -1;
2372 break;
2373 }
2374 /*FALLTHROUGH*/
2375 case 'W': /* "-W {scriptout}" overwrite script file */
2376 if (scriptout != NULL)
2377 goto scripterror;
2378 if ((scriptout = mch_fopen(argv[0],
2379 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2380 {
2381 mch_errmsg(_("Cannot open for script output: \""));
2382 mch_errmsg(argv[0]);
2383 mch_errmsg("\"\n");
2384 mch_exit(2);
2385 }
2386 break;
2387
2388#ifdef FEAT_GUI_W32
2389 case 'P': /* "-P {parent title}" MDI parent */
2390 gui_mch_set_parent(argv[0]);
2391 break;
2392#endif
2393 }
2394 }
2395 }
2396
2397 /*
2398 * File name argument.
2399 */
2400 else
2401 {
2402 argv_idx = -1; /* skip to next argument */
2403
2404 /* Check for only one type of editing. */
2405 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2406 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2407 parmp->edit_type = EDIT_FILE;
2408
2409#ifdef MSWIN
2410 /* Remember if the argument was a full path before changing
2411 * slashes to backslashes. */
2412 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2413 parmp->full_path = TRUE;
2414#endif
2415
2416 /* Add the file to the global argument list. */
2417 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2418 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2419 mch_exit(2);
2420#ifdef FEAT_DIFF
2421 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2422 && !mch_isdir(alist_name(&GARGLIST[0])))
2423 {
2424 char_u *r;
2425
2426 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2427 if (r != NULL)
2428 {
2429 vim_free(p);
2430 p = r;
2431 }
2432 }
2433#endif
2434#if defined(__CYGWIN32__) && !defined(WIN32)
2435 /*
2436 * If vim is invoked by non-Cygwin tools, convert away any
2437 * DOS paths, so things like .swp files are created correctly.
2438 * Look for evidence of non-Cygwin paths before we bother.
2439 * This is only for when using the Unix files.
2440 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002441 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002442 {
2443 char posix_path[PATH_MAX];
2444
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002445# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2446 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2447# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002448 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002449# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002450 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002451 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002452 if (p == NULL)
2453 mch_exit(2);
2454 }
2455#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002456
2457#ifdef USE_FNAME_CASE
2458 /* Make the case of the file name match the actual file. */
2459 fname_case(p, 0);
2460#endif
2461
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002463#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002464 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002465#else
2466 2 /* add buffer number now and use curbuf */
2467#endif
2468 );
2469
2470#if defined(FEAT_MBYTE) && defined(WIN32)
2471 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472 /* Remember this argument has been added to the argument list.
2473 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002474 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002475# ifdef FEAT_DIFF
2476 parmp->diff_mode
2477# else
2478 FALSE
2479# endif
2480 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002481 }
2482#endif
2483 }
2484
2485 /*
2486 * If there are no more letters after the current "-", go to next
2487 * argument. argv_idx is set to -1 when the current argument is to be
2488 * skipped.
2489 */
2490 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2491 {
2492 --argc;
2493 ++argv;
2494 argv_idx = 1;
2495 }
2496 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002497
2498#ifdef FEAT_EVAL
2499 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2500 * one. */
2501 if (parmp->n_commands > 0)
2502 {
2503 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2504 if (p != NULL)
2505 {
2506 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2507 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2508 vim_free(p);
2509 }
2510 }
2511#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002512}
2513
2514/*
2515 * Print a warning if stdout is not a terminal.
2516 * When starting in Ex mode and commands come from a file, set Silent mode.
2517 */
2518 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002519check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002520{
2521 int input_isatty; /* is active input a terminal? */
2522
2523 input_isatty = mch_input_isatty();
2524 if (exmode_active)
2525 {
2526 if (!input_isatty)
2527 silent_mode = TRUE;
2528 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002529 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002530#ifdef FEAT_GUI
2531 /* don't want the delay when started from the desktop */
2532 && !gui.starting
2533#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002534 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002535 {
2536#ifdef NBDEBUG
2537 /*
2538 * This shouldn't be necessary. But if I run netbeans with the log
2539 * output coming to the console and XOpenDisplay fails, I get vim
2540 * trying to start with input/output to my console tty. This fills my
2541 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002542 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002543 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002544 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545 {
2546 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2547 exit(1);
2548 }
2549#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002550#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2551 if (is_cygpty_used())
2552 {
2553 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2554 exit(1);
2555 }
2556#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002557 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2559 if (!input_isatty)
2560 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2561 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002562 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2563 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002564 if (scriptin[0] == NULL)
2565 ui_delay(2000L, TRUE);
2566 TIME_MSG("Warning delay");
2567 }
2568}
2569
2570/*
2571 * Read text from stdin.
2572 */
2573 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002574read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002575{
2576 int i;
2577
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002578#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002579 /* When getting the ATTENTION prompt here, use a dialog */
2580 swap_exists_action = SEA_DIALOG;
2581#endif
2582 no_wait_return = TRUE;
2583 i = msg_didany;
2584 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002585 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002586 no_wait_return = FALSE;
2587 msg_didany = i;
2588 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002589#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590 check_swap_exists_action();
2591#endif
2592#if !(defined(AMIGA) || defined(MACOS))
2593 /*
2594 * Close stdin and dup it from stderr. Required for GPM to work
2595 * properly, and for running external commands.
2596 * Is there any other system that cannot do this?
2597 */
2598 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002599 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002600#endif
2601}
2602
2603/*
2604 * Create the requested number of windows and edit buffers in them.
2605 * Also does recovery if "recoverymode" set.
2606 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002607 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002608create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002609{
2610#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002611 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002612 int done = 0;
2613
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002614 /*
2615 * Create the number of windows that was requested.
2616 */
2617 if (parmp->window_count == -1) /* was not set */
2618 parmp->window_count = 1;
2619 if (parmp->window_count == 0)
2620 parmp->window_count = GARGCOUNT;
2621 if (parmp->window_count > 1)
2622 {
2623 /* Don't change the windows if there was a command in .vimrc that
2624 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002625 if (parmp->window_layout == 0)
2626 parmp->window_layout = WIN_HOR;
2627 if (parmp->window_layout == WIN_TABS)
2628 {
2629 parmp->window_count = make_tabpages(parmp->window_count);
2630 TIME_MSG("making tab pages");
2631 }
2632 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002633 {
2634 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002635 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002636 TIME_MSG("making windows");
2637 }
2638 else
2639 parmp->window_count = win_count();
2640 }
2641 else
2642 parmp->window_count = 1;
2643#endif
2644
2645 if (recoverymode) /* do recover */
2646 {
2647 msg_scroll = TRUE; /* scroll message up */
2648 ml_recover();
2649 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2650 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002651 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652 }
2653 else
2654 {
2655 /*
2656 * Open a buffer for windows that don't have one yet.
2657 * Commands in the .vimrc might have loaded a file or split the window.
2658 * Watch out for autocommands that delete a window.
2659 */
2660#ifdef FEAT_AUTOCMD
2661 /*
2662 * Don't execute Win/Buf Enter/Leave autocommands here
2663 */
2664 ++autocmd_no_enter;
2665 ++autocmd_no_leave;
2666#endif
2667#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002668 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002669 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002670 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002671 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002672 {
2673 if (parmp->window_layout == WIN_TABS)
2674 goto_tabpage(1);
2675 else
2676 curwin = firstwin;
2677 }
2678 else if (parmp->window_layout == WIN_TABS)
2679 {
2680 if (curtab->tp_next == NULL)
2681 break;
2682 goto_tabpage(0);
2683 }
2684 else
2685 {
2686 if (curwin->w_next == NULL)
2687 break;
2688 curwin = curwin->w_next;
2689 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002690 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002691#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002692 curbuf = curwin->w_buffer;
2693 if (curbuf->b_ml.ml_mfp == NULL)
2694 {
2695#ifdef FEAT_FOLDING
2696 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2697 if (p_fdls >= 0)
2698 curwin->w_p_fdl = p_fdls;
2699#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002700#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002701 /* When getting the ATTENTION prompt here, use a dialog */
2702 swap_exists_action = SEA_DIALOG;
2703#endif
2704 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002705
2706 /* create memfile, read file */
2707 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002709#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002710 if (swap_exists_action == SEA_QUIT)
2711 {
2712 if (got_int || only_one_window())
2713 {
2714 /* abort selected or quit and only one window */
2715 did_emsg = FALSE; /* avoid hit-enter prompt */
2716 getout(1);
2717 }
2718 /* We can't close the window, it would disturb what
2719 * happens next. Clear the file name and set the arg
2720 * index to -1 to delete it later. */
2721 setfname(curbuf, NULL, NULL, FALSE);
2722 curwin->w_arg_idx = -1;
2723 swap_exists_action = SEA_NONE;
2724 }
2725 else
2726 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727#endif
2728#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002729 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002730#endif
2731 }
2732#ifdef FEAT_WINDOWS
2733 ui_breakcheck();
2734 if (got_int)
2735 {
2736 (void)vgetc(); /* only break the file loading, not the rest */
2737 break;
2738 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002740#endif
2741#ifdef FEAT_WINDOWS
2742 if (parmp->window_layout == WIN_TABS)
2743 goto_tabpage(1);
2744 else
2745 curwin = firstwin;
2746 curbuf = curwin->w_buffer;
2747#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002748#ifdef FEAT_AUTOCMD
2749 --autocmd_no_enter;
2750 --autocmd_no_leave;
2751#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752 }
2753}
2754
2755#ifdef FEAT_WINDOWS
2756 /*
2757 * If opened more than one window, start editing files in the other
2758 * windows. make_windows() has already opened the windows.
2759 */
2760 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002761edit_buffers(
2762 mparm_T *parmp,
2763 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764{
2765 int arg_idx; /* index in argument list */
2766 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002767 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002768 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002769
2770# ifdef FEAT_AUTOCMD
2771 /*
2772 * Don't execute Win/Buf Enter/Leave autocommands here
2773 */
2774 ++autocmd_no_enter;
2775 ++autocmd_no_leave;
2776# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002777
2778 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2779 if (curwin->w_arg_idx == -1)
2780 {
2781 win_close(curwin, TRUE);
2782 advance = FALSE;
2783 }
2784
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785 arg_idx = 1;
2786 for (i = 1; i < parmp->window_count; ++i)
2787 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002788 if (cwd != NULL)
2789 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002790 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2791 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002792 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002793 ++arg_idx;
2794 win_close(curwin, TRUE);
2795 advance = FALSE;
2796 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002797 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002798
Bram Moolenaar84212822006-11-07 21:59:47 +00002799 if (advance)
2800 {
2801 if (parmp->window_layout == WIN_TABS)
2802 {
2803 if (curtab->tp_next == NULL) /* just checking */
2804 break;
2805 goto_tabpage(0);
2806 }
2807 else
2808 {
2809 if (curwin->w_next == NULL) /* just checking */
2810 break;
2811 win_enter(curwin->w_next, FALSE);
2812 }
2813 }
2814 advance = TRUE;
2815
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002817 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002818 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2819 {
2820 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002821 /* Edit file from arg list, if there is one. When "Quit" selected
2822 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002823# ifdef HAS_SWAP_EXISTS_ACTION
2824 swap_exists_did_quit = FALSE;
2825# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002826 (void)do_ecmd(0, arg_idx < GARGCOUNT
2827 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002828 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002829# ifdef HAS_SWAP_EXISTS_ACTION
2830 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002831 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002832 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002833 if (got_int || only_one_window())
2834 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002835 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002836 did_emsg = FALSE; /* avoid hit-enter prompt */
2837 getout(1);
2838 }
2839 win_close(curwin, TRUE);
2840 advance = FALSE;
2841 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002842# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002843 if (arg_idx == GARGCOUNT - 1)
2844 arg_had_last = TRUE;
2845 ++arg_idx;
2846 }
2847 ui_breakcheck();
2848 if (got_int)
2849 {
2850 (void)vgetc(); /* only break the file loading, not the rest */
2851 break;
2852 }
2853 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002854
2855 if (parmp->window_layout == WIN_TABS)
2856 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002857# ifdef FEAT_AUTOCMD
2858 --autocmd_no_enter;
2859# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002860
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002861 /* make the first window the current window */
2862 win = firstwin;
2863#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2864 /* Avoid making a preview window the current window. */
2865 while (win->w_p_pvw)
2866 {
2867 win = win->w_next;
2868 if (win == NULL)
2869 {
2870 win = firstwin;
2871 break;
2872 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002873 }
2874#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002875 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002876
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002877# ifdef FEAT_AUTOCMD
2878 --autocmd_no_leave;
2879# endif
2880 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002881 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002882 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2883}
2884#endif /* FEAT_WINDOWS */
2885
2886/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002887 * Execute the commands from --cmd arguments "cmds[cnt]".
2888 */
2889 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002890exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002891{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002892 char_u **cmds = parmp->pre_commands;
2893 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002894 int i;
2895
2896 if (cnt > 0)
2897 {
2898 curwin->w_cursor.lnum = 0; /* just in case.. */
2899 sourcing_name = (char_u *)_("pre-vimrc command line");
2900# ifdef FEAT_EVAL
2901 current_SID = SID_CMDARG;
2902# endif
2903 for (i = 0; i < cnt; ++i)
2904 do_cmdline_cmd(cmds[i]);
2905 sourcing_name = NULL;
2906# ifdef FEAT_EVAL
2907 current_SID = 0;
2908# endif
2909 TIME_MSG("--cmd commands");
2910 }
2911}
2912
2913/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002914 * Execute "+", "-c" and "-S" arguments.
2915 */
2916 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002917exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002918{
2919 int i;
2920
2921 /*
2922 * We start commands on line 0, make "vim +/pat file" match a
2923 * pattern on line 1. But don't move the cursor when an autocommand
2924 * with g`" was used.
2925 */
2926 msg_scroll = TRUE;
2927 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2928 curwin->w_cursor.lnum = 0;
2929 sourcing_name = (char_u *)"command line";
2930#ifdef FEAT_EVAL
2931 current_SID = SID_CARG;
2932#endif
2933 for (i = 0; i < parmp->n_commands; ++i)
2934 {
2935 do_cmdline_cmd(parmp->commands[i]);
2936 if (parmp->cmds_tofree[i])
2937 vim_free(parmp->commands[i]);
2938 }
2939 sourcing_name = NULL;
2940#ifdef FEAT_EVAL
2941 current_SID = 0;
2942#endif
2943 if (curwin->w_cursor.lnum == 0)
2944 curwin->w_cursor.lnum = 1;
2945
2946 if (!exmode_active)
2947 msg_scroll = FALSE;
2948
2949#ifdef FEAT_QUICKFIX
2950 /* When started with "-q errorfile" jump to first error again. */
2951 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002952 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002953#endif
2954 TIME_MSG("executing command arguments");
2955}
2956
2957/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002958 * Source startup scripts.
2959 */
2960 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002961source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002962{
2963 int i;
2964
2965 /*
2966 * For "evim" source evim.vim first of all, so that the user can overrule
2967 * any things he doesn't like.
2968 */
2969 if (parmp->evim_mode)
2970 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002971 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002972 TIME_MSG("source evim file");
2973 }
2974
2975 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002976 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002977 * nothing else.
2978 */
2979 if (parmp->use_vimrc != NULL)
2980 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002981 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2982 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002983 {
2984#ifdef FEAT_GUI
2985 if (use_gvimrc == NULL) /* don't load gvimrc either */
2986 use_gvimrc = parmp->use_vimrc;
2987#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002988 }
2989 else
2990 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002991 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002992 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2993 }
2994 }
2995 else if (!silent_mode)
2996 {
2997#ifdef AMIGA
2998 struct Process *proc = (struct Process *)FindTask(0L);
2999 APTR save_winptr = proc->pr_WindowPtr;
3000
3001 /* Avoid a requester here for a volume that doesn't exist. */
3002 proc->pr_WindowPtr = (APTR)-1L;
3003#endif
3004
3005 /*
3006 * Get system wide defaults, if the file name is defined.
3007 */
3008#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003009 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003010#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003011#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003012 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003013#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003014
3015 /*
3016 * Try to read initialization commands from the following places:
3017 * - environment variable VIMINIT
3018 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3019 * - second user vimrc file ($VIM/.vimrc for Dos)
3020 * - environment variable EXINIT
3021 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3022 * - second user exrc file ($VIM/.exrc for Dos)
3023 * The first that exists is used, the rest is ignored.
3024 */
3025 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3026 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003027 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003028#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003029 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3030 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003031#endif
3032#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003033 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3034 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003035#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003036#ifdef USR_VIMRC_FILE4
3037 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3038 DOSO_VIMRC) == FAIL
3039#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003040 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003041 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003042#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003043 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003044#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003045 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003046 {
3047 /* When no .vimrc file was found: source defaults.vim. */
3048 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003049 }
3050 }
3051
3052 /*
3053 * Read initialization commands from ".vimrc" or ".exrc" in current
3054 * directory. This is only done if the 'exrc' option is set.
3055 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003056 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003057 * option has been reset in environment of global ".exrc" or ".vimrc".
3058 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3059 * SYS_VIMRC_FILE.
3060 */
3061 if (p_exrc)
3062 {
3063#if defined(UNIX) || defined(VMS)
3064 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3065 if (!file_owned(VIMRC_FILE))
3066#endif
3067 secure = p_secure;
3068
3069 i = FAIL;
3070 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3071 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3072#ifdef USR_VIMRC_FILE2
3073 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3074 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3075#endif
3076#ifdef USR_VIMRC_FILE3
3077 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3078 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3079#endif
3080#ifdef SYS_VIMRC_FILE
3081 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3082 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3083#endif
3084 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003085 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003086
3087 if (i == FAIL)
3088 {
3089#if defined(UNIX) || defined(VMS)
3090 /* if ".exrc" is not owned by user set 'secure' mode */
3091 if (!file_owned(EXRC_FILE))
3092 secure = p_secure;
3093 else
3094 secure = 0;
3095#endif
3096 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3097 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3098#ifdef USR_EXRC_FILE2
3099 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3100 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3101#endif
3102 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003103 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003104 }
3105 }
3106 if (secure == 2)
3107 need_wait_return = TRUE;
3108 secure = 0;
3109#ifdef AMIGA
3110 proc->pr_WindowPtr = save_winptr;
3111#endif
3112 }
3113 TIME_MSG("sourcing vimrc file(s)");
3114}
3115
3116/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003117 * Setup to start using the GUI. Exit with an error when not available.
3118 */
3119 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003120main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003121{
3122#ifdef FEAT_GUI
3123 gui.starting = TRUE; /* start GUI a bit later */
3124#else
3125 mch_errmsg(_(e_nogvim));
3126 mch_errmsg("\n");
3127 mch_exit(2);
3128#endif
3129}
3130
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003131#endif /* NO_VIM_MAIN */
3132
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003133/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003134 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003135 * Returns FAIL if the environment variable was not executed, OK otherwise.
3136 */
3137 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003138process_env(
3139 char_u *env,
3140 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003141{
3142 char_u *initstr;
3143 char_u *save_sourcing_name;
3144 linenr_T save_sourcing_lnum;
3145#ifdef FEAT_EVAL
3146 scid_T save_sid;
3147#endif
3148
3149 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3150 {
3151 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003152 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003153 save_sourcing_name = sourcing_name;
3154 save_sourcing_lnum = sourcing_lnum;
3155 sourcing_name = env;
3156 sourcing_lnum = 0;
3157#ifdef FEAT_EVAL
3158 save_sid = current_SID;
3159 current_SID = SID_ENV;
3160#endif
3161 do_cmdline_cmd(initstr);
3162 sourcing_name = save_sourcing_name;
3163 sourcing_lnum = save_sourcing_lnum;
3164#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003165 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003166#endif
3167 return OK;
3168 }
3169 return FAIL;
3170}
3171
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003172#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003173/*
3174 * Return TRUE if we are certain the user owns the file "fname".
3175 * Used for ".vimrc" and ".exrc".
3176 * Use both stat() and lstat() for extra security.
3177 */
3178 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003179file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003180{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003181 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003182# ifdef UNIX
3183 uid_t uid = getuid();
3184# else /* VMS */
3185 uid_t uid = ((getgid() << 16) | getuid());
3186# endif
3187
3188 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3189# ifdef HAVE_LSTAT
3190 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3191# endif
3192 );
3193}
3194#endif
3195
3196/*
3197 * Give an error message main_errors["n"] and exit.
3198 */
3199 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003200mainerr(
3201 int n, /* one of the ME_ defines */
3202 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003203{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003204#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003205 reset_signals(); /* kill us with CTRL-C here, if you like */
3206#endif
3207
3208 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003209 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003210 mch_errmsg(_(main_errors[n]));
3211 if (str != NULL)
3212 {
3213 mch_errmsg(": \"");
3214 mch_errmsg((char *)str);
3215 mch_errmsg("\"");
3216 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003217 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218
3219 mch_exit(1);
3220}
3221
3222 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003223mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003224{
3225 mainerr(ME_ARG_MISSING, str);
3226}
3227
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003228#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003229/*
3230 * print a message with three spaces prepended and '\n' appended.
3231 */
3232 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003233main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003234{
3235 mch_msg(" ");
3236 mch_msg(s);
3237 mch_msg("\n");
3238}
3239
3240/*
3241 * Print messages for "vim -h" or "vim --help" and exit.
3242 */
3243 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003244usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003245{
3246 int i;
3247 static char *(use[]) =
3248 {
3249 N_("[file ..] edit specified file(s)"),
3250 N_("- read text from stdin"),
3251 N_("-t tag edit file where tag is defined"),
3252#ifdef FEAT_QUICKFIX
3253 N_("-q [errorfile] edit file with first error")
3254#endif
3255 };
3256
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003257#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003258 reset_signals(); /* kill us with CTRL-C here, if you like */
3259#endif
3260
3261 mch_msg(longVersion);
3262 mch_msg(_("\n\nusage:"));
3263 for (i = 0; ; ++i)
3264 {
3265 mch_msg(_(" vim [arguments] "));
3266 mch_msg(_(use[i]));
3267 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3268 break;
3269 mch_msg(_("\n or:"));
3270 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003271#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003272 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003273#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003274
3275 mch_msg(_("\n\nArguments:\n"));
3276 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003277#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003278 main_msg(_("--literal\t\tDon't expand wildcards"));
3279#endif
3280#ifdef FEAT_OLE
3281 main_msg(_("-register\t\tRegister this gvim for OLE"));
3282 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3283#endif
3284#ifdef FEAT_GUI
3285 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3286 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3287#endif
3288 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3289 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003290 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003291 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3292#ifdef FEAT_DIFF
3293 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3294#endif
3295 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3296 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3297 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3298 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3299 main_msg(_("-M\t\t\tModifications in text not allowed"));
3300 main_msg(_("-b\t\t\tBinary mode"));
3301#ifdef FEAT_LISP
3302 main_msg(_("-l\t\t\tLisp mode"));
3303#endif
3304 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3305 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003306 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3307#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003308 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003309#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003310 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3311 main_msg(_("-r\t\t\tList swap files and exit"));
3312 main_msg(_("-r (with file name)\tRecover crashed session"));
3313 main_msg(_("-L\t\t\tSame as -r"));
3314#ifdef AMIGA
3315 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3316 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3317#endif
3318#ifdef FEAT_ARABIC
3319 main_msg(_("-A\t\t\tstart in Arabic mode"));
3320#endif
3321#ifdef FEAT_RIGHTLEFT
3322 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3323#endif
3324#ifdef FEAT_FKMAP
3325 main_msg(_("-F\t\t\tStart in Farsi mode"));
3326#endif
3327 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003328 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003329 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3331#ifdef FEAT_GUI
3332 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3333#endif
3334 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003335#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003336 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003337 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3338 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003339#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340 main_msg(_("+\t\t\tStart at end of file"));
3341 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003342 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003343 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3344 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3345 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3346 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3347 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3348#ifdef FEAT_CRYPT
3349 main_msg(_("-x\t\t\tEdit encrypted files"));
3350#endif
3351#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3352# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3353 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3354# endif
3355 main_msg(_("-X\t\t\tDo not connect to X server"));
3356#endif
3357#ifdef FEAT_CLIENTSERVER
3358 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3359 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3360 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3361 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003362# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003363 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003364# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003365 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3366 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3367 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3368 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3369#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003370#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003371 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003372#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003373#ifdef FEAT_VIMINFO
3374 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3375#endif
3376 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3377 main_msg(_("--version\t\tPrint version information and exit"));
3378
3379#ifdef FEAT_GUI_X11
3380# ifdef FEAT_GUI_MOTIF
3381 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3382# else
3383# ifdef FEAT_GUI_ATHENA
3384# ifdef FEAT_GUI_NEXTAW
3385 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3386# else
3387 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3388# endif
3389# endif
3390# endif
3391 main_msg(_("-display <display>\tRun vim on <display>"));
3392 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3394 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3395 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3396 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3397 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3398 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3399 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3400 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3401# ifdef FEAT_GUI_ATHENA
3402 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3403# endif
3404 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3405 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3406 main_msg(_("-xrm <resource>\tSet the specified resource"));
3407#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408#ifdef FEAT_GUI_GTK
3409 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3410 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3411 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3412 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3413 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003414 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003416 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003417#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418#ifdef FEAT_GUI_W32
3419 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003420 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421#endif
3422
3423#ifdef FEAT_GUI_GNOME
3424 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3425 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003426 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003428 gui.dofork = FALSE;
3429 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430 else
3431#endif
3432 mch_exit(0);
3433}
3434
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003435#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436/*
3437 * Check the result of the ATTENTION dialog:
3438 * When "Quit" selected, exit Vim.
3439 * When "Recover" selected, recover the file.
3440 */
3441 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003442check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443{
3444 if (swap_exists_action == SEA_QUIT)
3445 getout(1);
3446 handle_swap_exists(NULL);
3447}
3448#endif
3449
Bram Moolenaar08cab962017-03-04 14:37:18 +01003450#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003451
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003452#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003453static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003454
3455static struct timeval prev_timeval;
3456
Bram Moolenaar3f269672009-11-03 11:11:11 +00003457# ifdef WIN3264
3458/*
3459 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3460 */
3461 static int
3462gettimeofday(struct timeval *tv, char *dummy)
3463{
3464 long t = clock();
3465 tv->tv_sec = t / CLOCKS_PER_SEC;
3466 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3467 return 0;
3468}
3469# endif
3470
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003471/*
3472 * Save the previous time before doing something that could nest.
3473 * set "*tv_rel" to the time elapsed so far.
3474 */
3475 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003476time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003477{
3478 *((struct timeval *)tv_rel) = prev_timeval;
3479 gettimeofday(&prev_timeval, NULL);
3480 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3481 - ((struct timeval *)tv_rel)->tv_usec;
3482 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3483 - ((struct timeval *)tv_rel)->tv_sec;
3484 if (((struct timeval *)tv_rel)->tv_usec < 0)
3485 {
3486 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3487 --((struct timeval *)tv_rel)->tv_sec;
3488 }
3489 *(struct timeval *)tv_start = prev_timeval;
3490}
3491
3492/*
3493 * Compute the previous time after doing something that could nest.
3494 * Subtract "*tp" from prev_timeval;
3495 * Note: The arguments are (void *) to avoid trouble with systems that don't
3496 * have struct timeval.
3497 */
3498 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003499time_pop(
3500 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003501{
3502 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3503 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3504 if (prev_timeval.tv_usec < 0)
3505 {
3506 prev_timeval.tv_usec += 1000000;
3507 --prev_timeval.tv_sec;
3508 }
3509}
3510
3511 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003512time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003513{
3514 long usec;
3515 long msec;
3516
3517 usec = now->tv_usec - then->tv_usec;
3518 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3519 usec = usec % 1000L;
3520 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3521}
3522
3523 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003524time_msg(
3525 char *mesg,
3526 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527 (struct timeval *) */
3528{
3529 static struct timeval start;
3530 struct timeval now;
3531
3532 if (time_fd != NULL)
3533 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003534 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535 {
3536 gettimeofday(&start, NULL);
3537 prev_timeval = start;
3538 fprintf(time_fd, "\n\ntimes in msec\n");
3539 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3540 fprintf(time_fd, " clock elapsed: other lines\n\n");
3541 }
3542 gettimeofday(&now, NULL);
3543 time_diff(&start, &now);
3544 if (((struct timeval *)tv_start) != NULL)
3545 {
3546 fprintf(time_fd, " ");
3547 time_diff(((struct timeval *)tv_start), &now);
3548 }
3549 fprintf(time_fd, " ");
3550 time_diff(&prev_timeval, &now);
3551 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003552 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003553 }
3554}
3555
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003556#endif
3557
Bram Moolenaar595297d2017-03-04 19:11:12 +01003558#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003559 static void
3560set_progpath(char_u *argv0)
3561{
3562 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003563
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003564# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003565 char buf[PATH_MAX + 1];
3566 ssize_t len;
3567
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003568 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003569 if (len > 0)
3570 {
3571 buf[len] = NUL;
3572 val = (char_u *)buf;
3573 }
3574# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003575 /* A relative path containing a "/" will become invalid when using ":cd",
3576 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003577 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3578 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003579# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003580 char_u *path = NULL;
3581
3582 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3583 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003584# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003585 char_u buf[MAXPATHL];
3586
Bram Moolenaar43663192017-03-05 14:29:12 +01003587 if (!mch_isFullName(argv0))
3588 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003589 if (gettail(argv0) != argv0
3590 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3591 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003592 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003593# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003594# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003595
Bram Moolenaar08cab962017-03-04 14:37:18 +01003596 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003597
Bram Moolenaar066029e2017-03-05 15:19:32 +01003598# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003599 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003600# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003601}
3602
3603#endif /* NO_VIM_MAIN */
3604
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003605#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003606
3607/*
3608 * Common code for the X command server and the Win32 command server.
3609 */
3610
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003611static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003612
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003613/*
3614 * Do the client-server stuff, unless "--servername ''" was used.
3615 */
3616 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003617exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003618{
3619 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3620 {
3621# ifdef WIN32
3622 /* Initialise the client/server messaging infrastructure. */
3623 serverInitMessaging();
3624# endif
3625
3626 /*
3627 * When a command server argument was found, execute it. This may
3628 * exit Vim when it was successful. Otherwise it's executed further
3629 * on. Remember the encoding used here in "serverStrEnc".
3630 */
3631 if (parmp->serverArg)
3632 {
3633 cmdsrv_main(&parmp->argc, parmp->argv,
3634 parmp->serverName_arg, &parmp->serverStr);
3635# ifdef FEAT_MBYTE
3636 parmp->serverStrEnc = vim_strsave(p_enc);
3637# endif
3638 }
3639
3640 /* If we're still running, get the name to register ourselves.
3641 * On Win32 can register right now, for X11 need to setup the
3642 * clipboard first, it's further down. */
3643 parmp->servername = serverMakeName(parmp->serverName_arg,
3644 parmp->argv[0]);
3645# ifdef WIN32
3646 if (parmp->servername != NULL)
3647 {
3648 serverSetName(parmp->servername);
3649 vim_free(parmp->servername);
3650 }
3651# endif
3652 }
3653}
3654
3655/*
3656 * Prepare for running as a Vim server.
3657 */
3658 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003659prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003660{
3661# if defined(FEAT_X11)
3662 /*
3663 * Register for remote command execution with :serversend and --remote
3664 * unless there was a -X or a --servername '' on the command line.
3665 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003666 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003667 */
3668 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3669# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003670 (gui.in_use
3671# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003672 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003673# endif
3674 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003675# endif
3676 parmp->serverName_arg != NULL))
3677 {
3678 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3679 vim_free(parmp->servername);
3680 TIME_MSG("register server name");
3681 }
3682 else
3683 serverDelayedStartName = parmp->servername;
3684# endif
3685
3686 /*
3687 * Execute command ourselves if we're here because the send failed (or
3688 * else we would have exited above).
3689 */
3690 if (parmp->serverStr != NULL)
3691 {
3692 char_u *p;
3693
3694 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3695 parmp->serverStr, &p));
3696 vim_free(p);
3697 }
3698}
3699
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003700 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003701cmdsrv_main(
3702 int *argc,
3703 char **argv,
3704 char_u *serverName_arg,
3705 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003706{
3707 char_u *res;
3708 int i;
3709 char_u *sname;
3710 int ret;
3711 int didone = FALSE;
3712 int exiterr = 0;
3713 char **newArgV = argv + 1;
3714 int newArgC = 1,
3715 Argc = *argc;
3716 int argtype;
3717#define ARGTYPE_OTHER 0
3718#define ARGTYPE_EDIT 1
3719#define ARGTYPE_EDIT_WAIT 2
3720#define ARGTYPE_SEND 3
3721 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003722 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003723# ifndef FEAT_X11
3724 HWND srv;
3725# else
3726 Window srv;
3727
3728 setup_term_clip();
3729# endif
3730
3731 sname = serverMakeName(serverName_arg, argv[0]);
3732 if (sname == NULL)
3733 return;
3734
3735 /*
3736 * Execute the command server related arguments and remove them
3737 * from the argc/argv array; We may have to return into main()
3738 */
3739 for (i = 1; i < Argc; i++)
3740 {
3741 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003742 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003743 {
3744 for (; i < *argc; i++)
3745 {
3746 *newArgV++ = argv[i];
3747 newArgC++;
3748 }
3749 break;
3750 }
3751
Bram Moolenaareb94e552006-03-11 21:35:11 +00003752 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003753 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003754 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3755 {
3756 char *p = argv[i] + 8;
3757
3758 argtype = ARGTYPE_EDIT;
3759 while (*p != NUL)
3760 {
3761 if (STRNICMP(p, "-wait", 5) == 0)
3762 {
3763 argtype = ARGTYPE_EDIT_WAIT;
3764 p += 5;
3765 }
3766 else if (STRNICMP(p, "-silent", 7) == 0)
3767 {
3768 silent = TRUE;
3769 p += 7;
3770 }
3771 else if (STRNICMP(p, "-tab", 4) == 0)
3772 {
3773 tabs = TRUE;
3774 p += 4;
3775 }
3776 else
3777 {
3778 argtype = ARGTYPE_OTHER;
3779 break;
3780 }
3781 }
3782 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003783 else
3784 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003785
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003786 if (argtype != ARGTYPE_OTHER)
3787 {
3788 if (i == *argc - 1)
3789 mainerr_arg_missing((char_u *)argv[i]);
3790 if (argtype == ARGTYPE_SEND)
3791 {
3792 *serverStr = (char_u *)argv[i + 1];
3793 i++;
3794 }
3795 else
3796 {
3797 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003798 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003799 if (*serverStr == NULL)
3800 {
3801 /* Probably out of memory, exit. */
3802 didone = TRUE;
3803 exiterr = 1;
3804 break;
3805 }
3806 Argc = i;
3807 }
3808# ifdef FEAT_X11
3809 if (xterm_dpy == NULL)
3810 {
3811 mch_errmsg(_("No display"));
3812 ret = -1;
3813 }
3814 else
3815 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003816 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003817# else
3818 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003819 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003820# endif
3821 if (ret < 0)
3822 {
3823 if (argtype == ARGTYPE_SEND)
3824 {
3825 /* Failed to send, abort. */
3826 mch_errmsg(_(": Send failed.\n"));
3827 didone = TRUE;
3828 exiterr = 1;
3829 }
3830 else if (!silent)
3831 /* Let vim start normally. */
3832 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3833 break;
3834 }
3835
3836# ifdef FEAT_GUI_W32
3837 /* Guess that when the server name starts with "g" it's a GUI
3838 * server, which we can bring to the foreground here.
3839 * Foreground() in the server doesn't work very well. */
3840 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3841 SetForegroundWindow(srv);
3842# endif
3843
3844 /*
3845 * For --remote-wait: Wait until the server did edit each
3846 * file. Also detect that the server no longer runs.
3847 */
3848 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3849 {
3850 int numFiles = *argc - i - 1;
3851 int j;
3852 char_u *done = alloc(numFiles);
3853 char_u *p;
3854# ifdef FEAT_GUI_W32
3855 NOTIFYICONDATA ni;
3856 int count = 0;
3857 extern HWND message_window;
3858# endif
3859
3860 if (numFiles > 0 && argv[i + 1][0] == '+')
3861 /* Skip "+cmd" argument, don't wait for it to be edited. */
3862 --numFiles;
3863
3864# ifdef FEAT_GUI_W32
3865 ni.cbSize = sizeof(ni);
3866 ni.hWnd = message_window;
3867 ni.uID = 0;
3868 ni.uFlags = NIF_ICON|NIF_TIP;
3869 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3870 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3871 Shell_NotifyIcon(NIM_ADD, &ni);
3872# endif
3873
3874 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003875 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003876 while (memchr(done, 0, numFiles) != NULL)
3877 {
3878# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003879 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003880 if (p == NULL)
3881 break;
3882# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003883 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003884 break;
3885# endif
3886 j = atoi((char *)p);
3887 if (j >= 0 && j < numFiles)
3888 {
3889# ifdef FEAT_GUI_W32
3890 ++count;
3891 sprintf(ni.szTip, _("%d of %d edited"),
3892 count, numFiles);
3893 Shell_NotifyIcon(NIM_MODIFY, &ni);
3894# endif
3895 done[j] = 1;
3896 }
3897 }
3898# ifdef FEAT_GUI_W32
3899 Shell_NotifyIcon(NIM_DELETE, &ni);
3900# endif
3901 }
3902 }
3903 else if (STRICMP(argv[i], "--remote-expr") == 0)
3904 {
3905 if (i == *argc - 1)
3906 mainerr_arg_missing((char_u *)argv[i]);
3907# ifdef WIN32
3908 /* Win32 always works? */
3909 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003910 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003911# else
3912 if (xterm_dpy == NULL)
3913 mch_errmsg(_("No display: Send expression failed.\n"));
3914 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003915 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003916# endif
3917 {
3918 if (res != NULL && *res != NUL)
3919 {
3920 /* Output error from remote */
3921 mch_errmsg((char *)res);
3922 vim_free(res);
3923 res = NULL;
3924 }
3925 mch_errmsg(_(": Send expression failed.\n"));
3926 }
3927 }
3928 else if (STRICMP(argv[i], "--serverlist") == 0)
3929 {
3930# ifdef WIN32
3931 /* Win32 always works? */
3932 res = serverGetVimNames();
3933# else
3934 if (xterm_dpy != NULL)
3935 res = serverGetVimNames(xterm_dpy);
3936# endif
3937 if (called_emsg)
3938 mch_errmsg("\n");
3939 }
3940 else if (STRICMP(argv[i], "--servername") == 0)
3941 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003942 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003943 i++;
3944 continue;
3945 }
3946 else
3947 {
3948 *newArgV++ = argv[i];
3949 newArgC++;
3950 continue;
3951 }
3952 didone = TRUE;
3953 if (res != NULL && *res != NUL)
3954 {
3955 mch_msg((char *)res);
3956 if (res[STRLEN(res) - 1] != '\n')
3957 mch_msg("\n");
3958 }
3959 vim_free(res);
3960 }
3961
3962 if (didone)
3963 {
3964 display_errors(); /* display any collected messages */
3965 exit(exiterr); /* Mission accomplished - get out */
3966 }
3967
3968 /* Return back into main() */
3969 *argc = newArgC;
3970 vim_free(sname);
3971}
3972
3973/*
3974 * Build a ":drop" command to send to a Vim server.
3975 */
3976 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003977build_drop_cmd(
3978 int filec,
3979 char **filev,
3980 int tabs, /* Use ":tab drop" instead of ":drop". */
3981 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003982{
3983 garray_T ga;
3984 int i;
3985 char_u *inicmd = NULL;
3986 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003987 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003988 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003989
3990 if (filec > 0 && filev[0][0] == '+')
3991 {
3992 inicmd = (char_u *)filev[0] + 1;
3993 filev++;
3994 filec--;
3995 }
3996 /* Check if we have at least one argument. */
3997 if (filec <= 0)
3998 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003999
4000 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004001 cwd = alloc(MAXPATHL);
4002 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004003 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004004 if (mch_dirname(cwd, MAXPATHL) != OK)
4005 {
4006 vim_free(cwd);
4007 return NULL;
4008 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004009 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004010#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004011 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004012#else
4013 PATH_ESC_CHARS,
4014#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004015 '\\', TRUE);
4016 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004017 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004018 return NULL;
4019 ga_init2(&ga, 1, 100);
4020 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004021 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004022
4023 /* Call inputsave() so that a prompt for an encryption key works. */
4024 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4025 if (tabs)
4026 ga_concat(&ga, (char_u *)"tab ");
4027 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004028 for (i = 0; i < filec; i++)
4029 {
4030 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004031 * do it again in the Vim server. On MS-Windows only escape
4032 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004033 p = vim_strsave_escaped((char_u *)filev[i],
4034#ifdef UNIX
4035 PATH_ESC_CHARS
4036#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004037 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004038#endif
4039 );
4040 if (p == NULL)
4041 {
4042 vim_free(ga.ga_data);
4043 return NULL;
4044 }
4045 ga_concat(&ga, (char_u *)" ");
4046 ga_concat(&ga, p);
4047 vim_free(p);
4048 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004049 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4050
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004051 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4052 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004053 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4054
4055 /* Switch back to the correct current directory (prior to temporary path
4056 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004057 * correct after the :drop command. With line breaks and spaces:
4058 * if !exists('+acd') || !&acd
4059 * if haslocaldir()
4060 * cd -
4061 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004062 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004063 * cd -
4064 * endif
4065 * endif
4066 */
4067 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004068 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004069 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004070 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004071 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004072
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004073 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004074 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4075 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004076 if (inicmd != NULL)
4077 {
4078 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4079 * the following commands to be inserted as text. Use a "|",
4080 * hopefully "inicmd" does allow this... */
4081 ga_concat(&ga, inicmd);
4082 ga_concat(&ga, (char_u *)"|");
4083 }
4084 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4085 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004086 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004087 ga_append(&ga, NUL);
4088 return ga.ga_data;
4089}
4090
4091/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004092 * Make our basic server name: use the specified "arg" if given, otherwise use
4093 * the tail of the command "cmd" we were started with.
4094 * Return the name in allocated memory. This doesn't include a serial number.
4095 */
4096 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004097serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004098{
4099 char_u *p;
4100
4101 if (arg != NULL && *arg != NUL)
4102 p = vim_strsave_up(arg);
4103 else
4104 {
4105 p = vim_strsave_up(gettail((char_u *)cmd));
4106 /* Remove .exe or .bat from the name. */
4107 if (p != NULL && vim_strchr(p, '.') != NULL)
4108 *vim_strchr(p, '.') = NUL;
4109 }
4110 return p;
4111}
4112#endif /* FEAT_CLIENTSERVER */
4113
4114#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4115/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004116 * Replace termcodes such as <CR> and insert as key presses if there is room.
4117 */
4118 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004119server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004120{
4121 char_u *ptr = NULL;
4122 char_u *cpo_save = p_cpo;
4123
4124 /* Set 'cpoptions' the way we want it.
4125 * B set - backslashes are *not* treated specially
4126 * k set - keycodes are *not* reverse-engineered
4127 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004128 * The last but one parameter of replace_termcodes() is TRUE so that the
4129 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004130 */
4131 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004132 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004133 p_cpo = cpo_save;
4134
4135 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4136 {
4137 /*
4138 * Add the string to the input stream.
4139 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4140 *
4141 * First clear typed characters from the typeahead buffer, there could
4142 * be half a mapping there. Then append to the existing string, so
4143 * that multiple commands from a client are concatenated.
4144 */
4145 if (typebuf.tb_maplen < typebuf.tb_len)
4146 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4147 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4148
4149 /* Let input_available() know we inserted text in the typeahead
4150 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004151 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004152 }
4153 vim_free((char_u *)ptr);
4154}
4155
4156/*
4157 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004158 */
4159 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004160eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004161{
4162 char_u *res;
4163 int save_dbl = debug_break_level;
4164 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004165 void *fc;
4166
4167 /* Evaluate the expression at the toplevel, don't use variables local to
4168 * the calling function. */
4169 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004170
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004171 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4172 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004173 debug_break_level = -1;
4174 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004175 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4176 * to be typed. Do generate errors so that try/catch works. */
4177 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004178
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004179 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004180
4181 debug_break_level = save_dbl;
4182 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004183 --emsg_silent;
4184 if (emsg_silent < 0)
4185 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004186 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004187
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004188 /* A client can tell us to redraw, but not to display the cursor, so do
4189 * that here. */
4190 setcursor();
4191 out_flush();
4192#ifdef FEAT_GUI
4193 if (gui.in_use)
4194 gui_update_cursor(FALSE, FALSE);
4195#endif
4196
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004197 return res;
4198}
4199
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004200/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004201 * Evaluate a command or expression sent to ourselves.
4202 */
4203 int
4204sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4205{
4206 if (asExpr)
4207 {
4208 char_u *ret;
4209
4210 ret = eval_client_expr_to_string(cmd);
4211 if (result != NULL)
4212 {
4213 if (ret == NULL)
4214 {
4215 char *err = _(e_invexprmsg);
4216 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4217 char_u *msg;
4218
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004219 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004220 if (msg != NULL)
4221 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4222 *result = msg;
4223 }
4224 else
4225 *result = ret;
4226 }
4227 else
4228 vim_free(ret);
4229 return ret == NULL ? -1 : 0;
4230 }
4231 server_to_input_buf(cmd);
4232 return 0;
4233}
4234
4235/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004236 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4237 * return an allocated string. Otherwise return "data".
4238 * "*tofree" is set to the result when it needs to be freed later.
4239 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004240 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004241serverConvert(
4242 char_u *client_enc UNUSED,
4243 char_u *data,
4244 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004245{
4246 char_u *res = data;
4247
4248 *tofree = NULL;
4249# ifdef FEAT_MBYTE
4250 if (client_enc != NULL && p_enc != NULL)
4251 {
4252 vimconv_T vimconv;
4253
4254 vimconv.vc_type = CONV_NONE;
4255 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4256 && vimconv.vc_type != CONV_NONE)
4257 {
4258 res = string_convert(&vimconv, data, NULL);
4259 if (res == NULL)
4260 res = data;
4261 else
4262 *tofree = res;
4263 }
4264 convert_setup(&vimconv, NULL, NULL);
4265 }
4266# endif
4267 return res;
4268}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004269#endif