blob: ebd1ec3333e45374b7635d1e21a7ebae354e3aa6 [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 Moolenaarc1cb78c2006-06-20 16:51:47 +0000452# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar66459b72016-08-06 19:01:55 +0200453 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000454# else
Bram Moolenaar66459b72016-08-06 19:01:55 +0200455 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000456# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000457 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100458
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100459 ex_packloadall(NULL);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100460 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200461
462# ifdef VMS /* Somehow VMS doesn't handle the "**". */
463 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
464# else
465 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
466# endif
467 TIME_MSG("loading after plugins");
468
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000469 }
470#endif
471
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000472#ifdef FEAT_DIFF
473 /* Decide about window layout for diff mode after reading vimrc. */
474 if (params.diff_mode && params.window_layout == 0)
475 {
476 if (diffopt_horizontal())
477 params.window_layout = WIN_HOR; /* use horizontal split */
478 else
479 params.window_layout = WIN_VER; /* use vertical split */
480 }
481#endif
482
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000483 /*
484 * Recovery mode without a file name: List swap files.
485 * This uses the 'dir' option, therefore it must be after the
486 * initializations.
487 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200488 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000489 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200490 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000491 mch_exit(0);
492 }
493
494 /*
495 * Set a few option defaults after reading .vimrc files:
496 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
497 */
498 set_init_3();
499 TIME_MSG("inits 3");
500
501 /*
502 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
503 * Note that this overrides anything from a vimrc file.
504 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000505 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000506 p_uc = 0;
507
508#ifdef FEAT_FKMAP
509 if (curwin->w_p_rl && p_altkeymap)
510 {
511 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
512# ifdef FEAT_ARABIC
513 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
514# endif
515 p_fkmap = TRUE; /* Set the Farsi keymap mode */
516 }
517#endif
518
519#ifdef FEAT_GUI
520 if (gui.starting)
521 {
522#if defined(UNIX) || defined(VMS)
523 /* When something caused a message from a vimrc script, need to output
524 * an extra newline before the shell prompt. */
525 if (did_emsg || msg_didout)
526 putchar('\n');
527#endif
528
529 gui_start(); /* will set full_screen to TRUE */
530 TIME_MSG("starting GUI");
531
532 /* When running "evim" or "gvim -y" we need the menus, exit if we
533 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000534 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535 mch_exit(1);
536 }
537#endif
538
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539#ifdef FEAT_VIMINFO
540 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000541 * Read in registers, history etc, but not marks, from the viminfo file.
542 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000543 */
544 if (*p_viminfo != NUL)
545 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000546 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547 TIME_MSG("reading viminfo");
548 }
549#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100550#ifdef FEAT_EVAL
551 /* It's better to make v:oldfiles an empty list than NULL. */
552 if (get_vim_var_list(VV_OLDFILES) == NULL)
553 set_vim_var_list(VV_OLDFILES, list_alloc());
554#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555
556#ifdef FEAT_QUICKFIX
557 /*
558 * "-q errorfile": Load the error file now.
559 * If the error file can't be read, exit before doing anything else.
560 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000561 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100563 char_u *enc = NULL;
564
565# ifdef FEAT_MBYTE
566 enc = p_menc;
567# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000568 if (params.use_ef != NULL)
569 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000570 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200571 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100572 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000573 {
574 out_char('\n');
575 mch_exit(3);
576 }
577 TIME_MSG("reading errorfile");
578 }
579#endif
580
581 /*
582 * Start putting things on the screen.
583 * Scroll screen down before drawing over it
584 * Clear screen now, so file message will not be cleared.
585 */
586 starting = NO_BUFFERS;
587 no_wait_return = FALSE;
588 if (!exmode_active)
589 msg_scroll = FALSE;
590
591#ifdef FEAT_GUI
592 /*
593 * This seems to be required to make callbacks to be called now, instead
594 * of after things have been put on the screen, which then may be deleted
595 * when getting a resize callback.
596 * For the Mac this handles putting files dropped on the Vim icon to
597 * global_alist.
598 */
599 if (gui.in_use)
600 {
601# ifdef FEAT_SUN_WORKSHOP
602 if (!usingSunWorkShop)
603# endif
604 gui_wait_for_chars(50L);
605 TIME_MSG("GUI delay");
606 }
607#endif
608
609#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
610 qnx_clip_init();
611#endif
612
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200613#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
614 clip_init(TRUE);
615#endif
616
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000617#ifdef FEAT_XCLIPBOARD
618 /* Start using the X clipboard, unless the GUI was started. */
619# ifdef FEAT_GUI
620 if (!gui.in_use)
621# endif
622 {
623 setup_term_clip();
624 TIME_MSG("setup clipboard");
625 }
626#endif
627
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000628#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000629 /* Prepare for being a Vim server. */
630 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000631#endif
632
633 /*
634 * If "-" argument given: Read file from stdin.
635 * Do this before starting Raw mode, because it may change things that the
636 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
637 * are the same terminal: "cat | vim -".
638 * Using autocommands here may cause trouble...
639 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000640 if (params.edit_type == EDIT_STDIN && !recoverymode)
641 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000642
643#if defined(UNIX) || defined(VMS)
644 /* When switching screens and something caused a message from a vimrc
645 * script, need to output an extra newline on exit. */
646 if ((did_emsg || msg_didout) && *T_TI != NUL)
647 newline_on_exit = TRUE;
648#endif
649
650 /*
651 * When done something that is not allowed or error message call
652 * wait_return. This must be done before starttermcap(), because it may
653 * switch to another screen. It must be done after settmode(TMODE_RAW),
654 * because we want to react on a single key stroke.
655 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000656 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000657 */
658 settmode(TMODE_RAW);
659 TIME_MSG("setting raw mode");
660
661 if (need_wait_return || msg_didany)
662 {
663 wait_return(TRUE);
664 TIME_MSG("waiting for return");
665 }
666
667 starttermcap(); /* start termcap if not done by wait_return() */
668 TIME_MSG("start termcap");
669
670#ifdef FEAT_MOUSE
671 setmouse(); /* may start using the mouse */
672#endif
673 if (scroll_region)
674 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000675 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676
677 /*
678 * Don't clear the screen when starting in Ex mode, unless using the GUI.
679 */
680 if (exmode_active
681#ifdef FEAT_GUI
682 && !gui.in_use
683#endif
684 )
685 must_redraw = CLEAR;
686 else
687 {
688 screenclear(); /* clear screen */
689 TIME_MSG("clearing screen");
690 }
691
692#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000693 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000694 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100695 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200696 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000697 TIME_MSG("getting crypt key");
698 }
699#endif
700
701 no_wait_return = TRUE;
702
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000704 * Create the requested number of windows and edit buffers in them.
705 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000706 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000707 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708 TIME_MSG("opening buffers");
709
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000710#ifdef FEAT_EVAL
711 /* clear v:swapcommand */
712 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
713#endif
714
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 /* Ex starts at last line of the file */
716 if (exmode_active)
717 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
718
719#ifdef FEAT_AUTOCMD
720 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
721 TIME_MSG("BufEnter autocommands");
722#endif
723 setpcmark();
724
725#ifdef FEAT_QUICKFIX
726 /*
727 * When started with "-q errorfile" jump to first error now.
728 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000729 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000730 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000731 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732 TIME_MSG("jump to first error");
733 }
734#endif
735
736#ifdef FEAT_WINDOWS
737 /*
738 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000739 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200741 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000742#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200743 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000744
745#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000746 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000747 {
748 win_T *wp;
749
750 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200751 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000752 diff_win_options(wp, TRUE);
753 }
754#endif
755
756 /*
757 * Shorten any of the filenames, but only when absolute.
758 */
759 shorten_fnames(FALSE);
760
761 /*
762 * Need to jump to the tag before executing the '-c command'.
763 * Makes "vim -c '/return' -t main" work.
764 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000765 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000766 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000767#if defined(HAS_SWAP_EXISTS_ACTION)
768 swap_exists_did_quit = FALSE;
769#endif
770
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000771 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000772 do_cmdline_cmd(IObuff);
773 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000774
775#if defined(HAS_SWAP_EXISTS_ACTION)
776 /* If the user doesn't want to edit the file then we quit here. */
777 if (swap_exists_did_quit)
778 getout(1);
779#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000780 }
781
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000782 /* Execute any "+", "-c" and "-S" arguments. */
783 if (params.n_commands > 0)
784 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000785
Bram Moolenaar976787d2017-06-04 15:45:50 +0200786#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
787 /* Must be done before redrawing, puts a few characters on the screen. */
788 may_req_ambiguous_char_width();
789#endif
790
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000791 RedrawingDisabled = 0;
792 redraw_all_later(NOT_VALID);
793 no_wait_return = FALSE;
794 starting = 0;
795
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200796 /* 'autochdir' has been postponed */
797 DO_AUTOCHDIR
798
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000799#ifdef FEAT_TERMRESPONSE
800 /* Requesting the termresponse is postponed until here, so that a "-c q"
801 * argument doesn't make it appear in the shell Vim was started from. */
802 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200803
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200804 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000805#endif
806
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000807 /* start in insert mode */
808 if (p_im)
809 need_start_insertmode = TRUE;
810
Bram Moolenaar14735512016-03-26 21:00:08 +0100811#ifdef FEAT_EVAL
812 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
813#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814#ifdef FEAT_AUTOCMD
815 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
816 TIME_MSG("VimEnter autocommands");
817#endif
818
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200819#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
820 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
821 * done after the clipboard is available and all initial commands that may
822 * modify the 'clipboard' setting have run; i.e. just before entering the
823 * main loop. */
824 {
825 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100826
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200827 adjust_clip_reg(&default_regname);
828 set_reg_var(default_regname);
829 }
830#endif
831
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000832#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
833 /* When a startup script or session file setup for diff'ing and
834 * scrollbind, sync the scrollbind now. */
835 if (curwin->w_p_diff && curwin->w_p_scb)
836 {
837 update_topline();
838 check_scrollbind((linenr_T)0, 0L);
839 TIME_MSG("diff scrollbinding");
840 }
841#endif
842
843#if defined(WIN3264) && !defined(FEAT_GUI_W32)
844 mch_set_winsize_now(); /* Allow winsize changes from now on */
845#endif
846
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000847#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
848 /* When tab pages were created, may need to update the tab pages line and
849 * scrollbars. This is skipped while creating them. */
850 if (first_tabpage->tp_next != NULL)
851 {
852 out_flush();
853 gui_init_which_components(NULL);
854 gui_update_scrollbars(TRUE);
855 }
856 need_mouse_correct = TRUE;
857#endif
858
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000859 /* If ":startinsert" command used, stuff a dummy command to be able to
860 * call normal_cmd(), which will then start Insert mode. */
861 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000862 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000863
864#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200865 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200866 {
867# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200868# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200869 && !defined(FEAT_GUI_W32)
870 if (gui.in_use)
871 {
872 mch_errmsg(_("netbeans is not supported with this GUI\n"));
873 mch_exit(2);
874 }
875# endif
876# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200878 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200879 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880#endif
881
882 TIME_MSG("before starting main loop");
883
884 /*
885 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100886 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000887 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000888
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200889#endif /* NO_VIM_MAIN */
890
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891 return 0;
892}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893
894/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200895 * Initialisation shared by main() and some tests.
896 */
897 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200898common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200899{
900
901#ifdef FEAT_MBYTE
902 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
903#endif
904#ifdef FEAT_EVAL
905 eval_init(); /* init global variables */
906#endif
907
908#ifdef __QNXNTO__
909 qnx_init(); /* PhAttach() for clipboard, (and gui) */
910#endif
911
912#ifdef MAC_OS_CLASSIC
913 /* Prepare for possibly starting GUI sometime */
914 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200915 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200916 TIME_MSG("GUI prepared");
917#endif
918
919 /* Init the table of Normal mode commands. */
920 init_normal_cmds();
921
922#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
923 make_version(); /* Construct the long version string. */
924#endif
925
926 /*
927 * Allocate space for the generic buffers (needed for set_init_1() and
928 * EMSG2()).
929 */
930 if ((IObuff = alloc(IOSIZE)) == NULL
931 || (NameBuff = alloc(MAXPATHL)) == NULL)
932 mch_exit(0);
933 TIME_MSG("Allocated generic buffers");
934
935#ifdef NBDEBUG
936 /* Wait a moment for debugging NetBeans. Must be after allocating
937 * NameBuff. */
938 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
939 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
940 TIME_MSG("NetBeans debug wait");
941#endif
942
943#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
944 /*
945 * Setup to use the current locale (for ctype() and many other things).
946 * NOTE: Translated messages with encodings other than latin1 will not
947 * work until set_init_1() has been called!
948 */
949 init_locale();
950 TIME_MSG("locale set");
951#endif
952
953#ifdef FEAT_GUI
954 gui.dofork = TRUE; /* default is to use fork() */
955#endif
956
957 /*
958 * Do a first scan of the arguments in "argv[]":
959 * -display or --display
960 * --server...
961 * --socketid
962 * --windowid
963 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200964 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200965
966#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200967 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200968#endif
969#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
970 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200971 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200972 TIME_MSG("GUI prepared");
973#endif
974
975#ifdef FEAT_CLIPBOARD
976 clip_init(FALSE); /* Initialise clipboard stuff */
977 TIME_MSG("clipboard setup");
978#endif
979
980 /*
981 * Check if we have an interactive window.
982 * On the Amiga: If there is no window, we open one with a newcli command
983 * (needed for :! to * work). mch_check_win() will also handle the -d or
984 * -dev argument.
985 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100986 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200987 TIME_MSG("window checked");
988
989 /*
990 * Allocate the first window and buffer.
991 * Can't do anything without it, exit when it fails.
992 */
993 if (win_alloc_first() == FAIL)
994 mch_exit(0);
995
996 init_yank(); /* init yank buffers */
997
998 alist_init(&global_alist); /* Init the argument list to empty. */
999 global_alist.id = 0;
1000
1001 /*
1002 * Set the default values for the options.
1003 * NOTE: Non-latin1 translated messages are working only after this,
1004 * because this is where "has_mbyte" will be set, which is used by
1005 * msg_outtrans_len_attr().
1006 * First find out the home directory, needed to expand "~" in options.
1007 */
1008 init_homedir(); /* find real value of $HOME */
1009 set_init_1();
1010 TIME_MSG("inits 1");
1011
1012#ifdef FEAT_EVAL
1013 set_lang_var(); /* set v:lang and v:ctype */
1014#endif
1015}
1016
1017/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001018 * Return TRUE when the --not-a-term argument was found.
1019 */
1020 int
1021is_not_a_term()
1022{
1023 return params.not_a_term;
1024}
1025
1026/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001027 * Main loop: Execute Normal mode commands until exiting Vim.
1028 * Also used to handle commands in the command-line window, until the window
1029 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001030 * Also used to handle ":visual" command after ":global": execute Normal mode
1031 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001032 */
1033 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001034main_loop(
1035 int cmdwin, /* TRUE when working in the command-line window */
1036 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001037{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001038 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001039 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001040#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001041 /* these are static to avoid a compiler warning */
1042 static linenr_T conceal_old_cursor_line = 0;
1043 static linenr_T conceal_new_cursor_line = 0;
1044 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001045#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046
1047#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1048 /* Setup to catch a terminating error from the X server. Just ignore
1049 * it, restore the state and continue. This might not always work
1050 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001051 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001053 {
1054 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001055 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001056 got_int = TRUE;
1057 need_wait_return = FALSE;
1058 global_busy = FALSE;
1059 exmode_active = 0;
1060 skip_redraw = FALSE;
1061 RedrawingDisabled = 0;
1062 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001063 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001064# ifdef FEAT_EVAL
1065 emsg_skip = 0;
1066# endif
1067 emsg_off = 0;
1068# ifdef FEAT_MOUSE
1069 setmouse();
1070# endif
1071 settmode(TMODE_RAW);
1072 starttermcap();
1073 scroll_start();
1074 redraw_later_clear();
1075 }
1076#endif
1077
1078 clear_oparg(&oa);
1079 while (!cmdwin
1080#ifdef FEAT_CMDWIN
1081 || cmdwin_result == 0
1082#endif
1083 )
1084 {
1085 if (stuff_empty())
1086 {
1087 did_check_timestamps = FALSE;
1088 if (need_check_timestamps)
1089 check_timestamps(FALSE);
1090 if (need_wait_return) /* if wait_return still needed ... */
1091 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001092 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001093 {
1094 need_start_insertmode = FALSE;
1095 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1096 /* skip the fileinfo message now, because it would be shown
1097 * after insert mode finishes! */
1098 need_fileinfo = FALSE;
1099 }
1100 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001101
1102 /* Reset "got_int" now that we got back to the main loop. Except when
1103 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1104 * the ":g" command.
1105 * For ":g/pat/vi" we reset "got_int" when used once. When used
1106 * a second time we go back to Ex mode and abort the ":g" command. */
1107 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001108 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001109 if (noexmode && global_busy && !exmode_active && previous_got_int)
1110 {
1111 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1112 * used and keep "got_int" set, so that it aborts ":g". */
1113 exmode_active = EXMODE_NORMAL;
1114 State = NORMAL;
1115 }
1116 else if (!global_busy || !exmode_active)
1117 {
1118 if (!quit_more)
1119 (void)vgetc(); /* flush all buffers */
1120 got_int = FALSE;
1121 }
1122 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001123 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001124 else
1125 previous_got_int = FALSE;
1126
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001127 if (!exmode_active)
1128 msg_scroll = FALSE;
1129 quit_more = FALSE;
1130
1131 /*
1132 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1133 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1134 * update cursor and redraw.
1135 */
1136 if (skip_redraw || exmode_active)
1137 skip_redraw = FALSE;
1138 else if (do_redraw || stuff_empty())
1139 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001140# ifdef FEAT_GUI
1141 /* If ui_breakcheck() was used a resize may have been postponed. */
1142 gui_may_resize_shell();
1143# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001144#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001145 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001146 if (!finish_op && (
1147# ifdef FEAT_AUTOCMD
1148 has_cursormoved()
1149# endif
1150# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1151 ||
1152# endif
1153# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001154 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001155# endif
1156 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001157# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001158 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001159# endif
1160 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001161 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001162# ifdef FEAT_AUTOCMD
1163 if (has_cursormoved())
1164 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1165 FALSE, curbuf);
1166# endif
1167# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001168 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001169 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001170# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001171 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001172# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001173 conceal_new_cursor_line = curwin->w_cursor.lnum;
1174 conceal_update_lines = TRUE;
1175 }
1176# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001177# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001178 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001179# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001180 }
1181#endif
1182
Bram Moolenaar186628f2013-03-19 13:33:23 +01001183#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001184 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001185 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001186 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001187 {
1188 if (last_changedtick_buf == curbuf)
1189 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1190 FALSE, curbuf);
1191 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001192 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001193 }
1194#endif
1195
Bram Moolenaar33aec762006-01-22 23:30:12 +00001196#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1197 /* Scroll-binding for diff mode may have been postponed until
1198 * here. Avoids doing it for every change. */
1199 if (diff_need_scrollbind)
1200 {
1201 check_scrollbind((linenr_T)0, 0L);
1202 diff_need_scrollbind = FALSE;
1203 }
1204#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001205#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001206 /* Include a closed fold completely in the Visual area. */
1207 foldAdjustVisual();
1208#endif
1209#ifdef FEAT_FOLDING
1210 /*
1211 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1212 * contain the cursor.
1213 * When 'foldopen' is "all", open the fold(s) under the cursor.
1214 * This may mark the window for redrawing.
1215 */
1216 if (hasAnyFolding(curwin) && !char_avail())
1217 {
1218 foldCheckClose();
1219 if (fdo_flags & FDO_ALL)
1220 foldOpenCursor();
1221 }
1222#endif
1223
1224 /*
1225 * Before redrawing, make sure w_topline is correct, and w_leftcol
1226 * if lines don't wrap, and w_skipcol if lines wrap.
1227 */
1228 update_topline();
1229 validate_cursor();
1230
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001231 if (VIsual_active)
1232 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001233 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001234 update_screen(0);
1235 else if (redraw_cmdline || clear_cmdline)
1236 showmode();
1237#ifdef FEAT_WINDOWS
1238 redraw_statuslines();
1239#endif
1240#ifdef FEAT_TITLE
1241 if (need_maketitle)
1242 maketitle();
1243#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001244#ifdef FEAT_VIMINFO
1245 curbuf->b_last_used = vim_time();
1246#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001247 /* display message after redraw */
1248 if (keep_msg != NULL)
1249 {
1250 char_u *p;
1251
1252 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001253 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1254 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001255 p = keep_msg;
1256 msg_attr(p, keep_msg_attr);
1257 vim_free(p);
1258 }
1259 if (need_fileinfo) /* show file info after redraw */
1260 {
1261 fileinfo(FALSE, TRUE, FALSE);
1262 need_fileinfo = FALSE;
1263 }
1264
1265 emsg_on_display = FALSE; /* can delete error message now */
1266 did_emsg = FALSE;
1267 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001268 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001269 showruler(FALSE);
1270
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001271# if defined(FEAT_CONCEAL)
1272 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001273 && (conceal_old_cursor_line != conceal_new_cursor_line
1274 || conceal_cursor_line(curwin)
1275 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001276 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001277 if (conceal_old_cursor_line != conceal_new_cursor_line
1278 && conceal_old_cursor_line
1279 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001280 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001281 update_single_line(curwin, conceal_new_cursor_line);
1282 curwin->w_valid &= ~VALID_CROW;
1283 }
1284# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001285 setcursor();
1286 cursor_on();
1287
1288 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001289
1290#ifdef STARTUPTIME
1291 /* Now that we have drawn the first screen all the startup stuff
1292 * has been done, close any file for startup messages. */
1293 if (time_fd != NULL)
1294 {
1295 TIME_MSG("first screen update");
1296 TIME_MSG("--- VIM STARTED ---");
1297 fclose(time_fd);
1298 time_fd = NULL;
1299 }
1300#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001301 }
1302#ifdef FEAT_GUI
1303 if (need_mouse_correct)
1304 gui_mouse_correct();
1305#endif
1306
1307 /*
1308 * Update w_curswant if w_set_curswant has been set.
1309 * Postponed until here to avoid computing w_virtcol too often.
1310 */
1311 update_curswant();
1312
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001313#ifdef FEAT_EVAL
1314 /*
1315 * May perform garbage collection when waiting for a character, but
1316 * only at the very toplevel. Otherwise we may be using a List or
1317 * Dict internally somewhere.
1318 * "may_garbage_collect" is reset in vgetc() which is invoked through
1319 * do_exmode() and normal_cmd().
1320 */
1321 may_garbage_collect = (!cmdwin && !noexmode);
1322#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001323 /*
1324 * If we're invoked as ex, do a round of ex commands.
1325 * Otherwise, get and execute a normal mode command.
1326 */
1327 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001328 {
1329 if (noexmode) /* End of ":global/path/visual" commands */
1330 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001331 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001332 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001333 else
1334 normal_cmd(&oa, TRUE);
1335 }
1336}
1337
1338
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001339#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001340/*
1341 * Exit, but leave behind swap files for modified buffers.
1342 */
1343 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001344getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001345{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001346# if defined(SIGHUP) && defined(SIG_IGN)
1347 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1348 * makes Vim exit and then handling SIGHUP causes various reentrance
1349 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001351# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001352
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001353 ml_close_notmod(); /* close all not-modified buffers */
1354 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1355 ml_close_all(FALSE); /* close all memfiles, without deleting */
1356 getout(exitval); /* exit Vim properly */
1357}
1358#endif
1359
1360
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001361/*
1362 * Exit properly.
1363 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001365getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001366{
1367#ifdef FEAT_AUTOCMD
1368 buf_T *buf;
1369 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001370 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001371#endif
1372
1373 exiting = TRUE;
1374
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001375 /* When running in Ex mode an error causes us to exit with a non-zero exit
1376 * code. POSIX requires this, although it's not 100% clear from the
1377 * standard. */
1378 if (exmode_active)
1379 exitval += ex_exitval;
1380
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001381 /* Position the cursor on the last screen line, below all the text */
1382#ifdef FEAT_GUI
1383 if (!gui.in_use)
1384#endif
1385 windgoto((int)Rows - 1, 0);
1386
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001387#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1388 /* Optionally print hashtable efficiency. */
1389 hash_debug_results();
1390#endif
1391
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001392#ifdef FEAT_GUI
1393 msg_didany = FALSE;
1394#endif
1395
1396#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001397 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001398 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001399 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1400# if defined FEAT_WINDOWS
1401 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001402 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001403 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001404 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001405 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001406 if (wp->w_buffer == NULL)
1407 /* Autocmd must have close the buffer already, skip. */
1408 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001409 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001410 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001411 {
1412 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1413 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001414 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001415 /* start all over, autocommands may mess up the lists */
1416 next_tp = first_tabpage;
1417 break;
1418 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001419 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001420 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001421# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1423 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001424# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001425
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001427 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001428 if (buf->b_ml.ml_mfp != NULL)
1429 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001430 bufref_T bufref;
1431
1432 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001434 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001435 if (!bufref_valid(&bufref))
1436 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001437 break;
1438 }
1439 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1440 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001441#endif
1442
1443#ifdef FEAT_VIMINFO
1444 if (*p_viminfo != NUL)
1445 /* Write out the registers, history, marks etc, to the viminfo file */
1446 write_viminfo(NULL, FALSE);
1447#endif
1448
1449#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001450 if (get_vim_var_nr(VV_DYING) <= 1)
1451 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001452#endif
1453
Bram Moolenaar05159a02005-02-26 23:04:13 +00001454#ifdef FEAT_PROFILE
1455 profile_dump();
1456#endif
1457
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001458 if (did_emsg
1459#ifdef FEAT_GUI
1460 || (gui.in_use && msg_didany && p_verbose > 0)
1461#endif
1462 )
1463 {
1464 /* give the user a chance to read the (error) message */
1465 no_wait_return = FALSE;
1466 wait_return(FALSE);
1467 }
1468
1469#ifdef FEAT_AUTOCMD
1470 /* Position the cursor again, the autocommands may have moved it */
1471# ifdef FEAT_GUI
1472 if (!gui.in_use)
1473# endif
1474 windgoto((int)Rows - 1, 0);
1475#endif
1476
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001477#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001478 job_stop_on_exit();
1479#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001480#ifdef FEAT_LUA
1481 lua_end();
1482#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001483#ifdef FEAT_MZSCHEME
1484 mzscheme_end();
1485#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001486#ifdef FEAT_TCL
1487 tcl_end();
1488#endif
1489#ifdef FEAT_RUBY
1490 ruby_end();
1491#endif
1492#ifdef FEAT_PYTHON
1493 python_end();
1494#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001495#ifdef FEAT_PYTHON3
1496 python3_end();
1497#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001498#ifdef FEAT_PERL
1499 perl_end();
1500#endif
1501#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1502 iconv_end();
1503#endif
1504#ifdef FEAT_NETBEANS_INTG
1505 netbeans_end();
1506#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001507#ifdef FEAT_CSCOPE
1508 cs_end();
1509#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001510#ifdef FEAT_EVAL
1511 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001512 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001513#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001514#if defined(WIN32) && defined(FEAT_MBYTE)
1515 free_cmd_argsW();
1516#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001517
1518 mch_exit(exitval);
1519}
1520
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001521#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1522/*
1523 * Setup to use the current locale (for ctype() and many other things).
1524 */
1525 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001526init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001527{
1528 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001529
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001530# ifdef FEAT_GUI_GTK
1531 /* Tell Gtk not to change our locale settings. */
1532 gtk_disable_setlocale();
1533# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001534# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1535 /* Make sure strtod() uses a decimal point, not a comma. */
1536 setlocale(LC_NUMERIC, "C");
1537# endif
1538
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001539# ifdef WIN32
1540 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1541 * text while it's expecting text in the current locale. This call avoids
1542 * that. */
1543 setlocale(LC_CTYPE, "C");
1544# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001545
1546# ifdef FEAT_GETTEXT
1547 {
1548 int mustfree = FALSE;
1549 char_u *p;
1550
1551# ifdef DYNAMIC_GETTEXT
1552 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001553 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001554# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001555 /* expand_env() doesn't work yet, because g_chartab[] is not
1556 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001557 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1558 if (p != NULL && *p != NUL)
1559 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001560 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001561 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1562 }
1563 if (mustfree)
1564 vim_free(p);
1565 textdomain(VIMPACKAGE);
1566 }
1567# endif
1568}
1569#endif
1570
1571/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001572 * Get the name of the display, before gui_prepare() removes it from
1573 * argv[]. Used for the xterm-clipboard display.
1574 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001575 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001576 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001577 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001578early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001579{
Bram Moolenaar03005972008-11-20 13:12:36 +00001580#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1581 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001582 int argc = parmp->argc;
1583 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001584 int i;
1585
1586 for (i = 1; i < argc; i++)
1587 {
1588 if (STRCMP(argv[i], "--") == 0)
1589 break;
1590# ifdef FEAT_XCLIPBOARD
1591 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001592# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001593 || STRICMP(argv[i], "--display") == 0
1594# endif
1595 )
1596 {
1597 if (i == argc - 1)
1598 mainerr_arg_missing((char_u *)argv[i]);
1599 xterm_display = argv[++i];
1600 }
1601# endif
1602# ifdef FEAT_CLIENTSERVER
1603 else if (STRICMP(argv[i], "--servername") == 0)
1604 {
1605 if (i == argc - 1)
1606 mainerr_arg_missing((char_u *)argv[i]);
1607 parmp->serverName_arg = (char_u *)argv[++i];
1608 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001609 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001610 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001611 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001612 {
1613 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001614# ifdef FEAT_GUI
1615 if (strstr(argv[i], "-wait") != 0)
1616 /* don't fork() when starting the GUI to edit files ourself */
1617 gui.dofork = FALSE;
1618# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001619 }
1620# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001621
1622# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1623# ifdef FEAT_GUI_W32
1624 else if (STRICMP(argv[i], "--windowid") == 0)
1625# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001626 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001627# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001628 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001629 long_u id;
1630 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001631
1632 if (i == argc - 1)
1633 mainerr_arg_missing((char_u *)argv[i]);
1634 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001635 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001636 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001637 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001638 if (count != 1)
1639 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1640 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001641# ifdef FEAT_GUI_W32
1642 win_socket_id = id;
1643# else
1644 gtk_socket_id = id;
1645# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001646 i++;
1647 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001648# endif
1649# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001650 else if (STRICMP(argv[i], "--echo-wid") == 0)
1651 echo_wid_arg = TRUE;
1652# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001653# ifndef FEAT_NETBEANS_INTG
1654 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001655 {
1656 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1657 mch_exit(2);
1658 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001659# endif
1660
Bram Moolenaar58d98232005-07-23 22:25:46 +00001661 }
1662#endif
1663}
1664
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001665#ifndef NO_VIM_MAIN
1666/*
1667 * Get a (optional) count for a Vim argument.
1668 */
1669 static int
1670get_number_arg(
1671 char_u *p, /* pointer to argument */
1672 int *idx, /* index in argument, is incremented */
1673 int def) /* default value */
1674{
1675 if (vim_isdigit(p[*idx]))
1676 {
1677 def = atoi((char *)&(p[*idx]));
1678 while (vim_isdigit(p[*idx]))
1679 *idx = *idx + 1;
1680 }
1681 return def;
1682}
1683
1684/*
1685 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1686 * If the executable name starts with "r" we disable shell commands.
1687 * If the next character is "e" we run in Easy mode.
1688 * If the next character is "g" we run the GUI version.
1689 * If the next characters are "view" we start in readonly mode.
1690 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1691 * If the next characters are "ex" we start in Ex mode. If it's followed
1692 * by "im" use improved Ex mode.
1693 */
1694 static void
1695parse_command_name(mparm_T *parmp)
1696{
1697 char_u *initstr;
1698
1699 initstr = gettail((char_u *)parmp->argv[0]);
1700
1701#ifdef MACOS_X_UNIX
1702 /* An issue has been seen when launching Vim in such a way that
1703 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1704 * executable or a symbolic link of it. Until this issue is resolved
1705 * we prohibit the GUI from being used.
1706 */
1707 if (STRCMP(initstr, parmp->argv[0]) == 0)
1708 disallow_gui = TRUE;
1709
1710 /* TODO: On MacOS X default to gui if argv[0] ends in:
1711 * /Vim.app/Contents/MacOS/Vim */
1712#endif
1713
1714#ifdef FEAT_EVAL
1715 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001716 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001717#endif
1718
1719 if (TOLOWER_ASC(initstr[0]) == 'r')
1720 {
1721 restricted = TRUE;
1722 ++initstr;
1723 }
1724
1725 /* Use evim mode for "evim" and "egvim", not for "editor". */
1726 if (TOLOWER_ASC(initstr[0]) == 'e'
1727 && (TOLOWER_ASC(initstr[1]) == 'v'
1728 || TOLOWER_ASC(initstr[1]) == 'g'))
1729 {
1730#ifdef FEAT_GUI
1731 gui.starting = TRUE;
1732#endif
1733 parmp->evim_mode = TRUE;
1734 ++initstr;
1735 }
1736
1737 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1738 if (TOLOWER_ASC(initstr[0]) == 'g')
1739 {
1740 main_start_gui();
1741#ifdef FEAT_GUI
1742 ++initstr;
1743#endif
1744 }
1745
1746 if (STRNICMP(initstr, "view", 4) == 0)
1747 {
1748 readonlymode = TRUE;
1749 curbuf->b_p_ro = TRUE;
1750 p_uc = 10000; /* don't update very often */
1751 initstr += 4;
1752 }
1753 else if (STRNICMP(initstr, "vim", 3) == 0)
1754 initstr += 3;
1755
1756 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1757 if (STRICMP(initstr, "diff") == 0)
1758 {
1759#ifdef FEAT_DIFF
1760 parmp->diff_mode = TRUE;
1761#else
1762 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1763 mch_errmsg("\n");
1764 mch_exit(2);
1765#endif
1766 }
1767
1768 if (STRNICMP(initstr, "ex", 2) == 0)
1769 {
1770 if (STRNICMP(initstr + 2, "im", 2) == 0)
1771 exmode_active = EXMODE_VIM;
1772 else
1773 exmode_active = EXMODE_NORMAL;
1774 change_compatible(TRUE); /* set 'compatible' */
1775 }
1776}
1777
Bram Moolenaar58d98232005-07-23 22:25:46 +00001778/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001779 * Scan the command line arguments.
1780 */
1781 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001782command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001783{
1784 int argc = parmp->argc;
1785 char **argv = parmp->argv;
1786 int argv_idx; /* index in argv[n][] */
1787 int had_minmin = FALSE; /* found "--" argument */
1788 int want_argument; /* option argument with argument */
1789 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001790 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001791 long n;
1792
1793 --argc;
1794 ++argv;
1795 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1796 while (argc > 0)
1797 {
1798 /*
1799 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1800 */
1801 if (argv[0][0] == '+' && !had_minmin)
1802 {
1803 if (parmp->n_commands >= MAX_ARG_CMDS)
1804 mainerr(ME_EXTRA_CMD, NULL);
1805 argv_idx = -1; /* skip to next argument */
1806 if (argv[0][1] == NUL)
1807 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1808 else
1809 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1810 }
1811
1812 /*
1813 * Optional argument.
1814 */
1815 else if (argv[0][0] == '-' && !had_minmin)
1816 {
1817 want_argument = FALSE;
1818 c = argv[0][argv_idx++];
1819#ifdef VMS
1820 /*
1821 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1822 * and "-/X" as "-X".
1823 */
1824 if (c == '/')
1825 {
1826 c = argv[0][argv_idx++];
1827 c = TOUPPER_ASC(c);
1828 }
1829 else
1830 c = TOLOWER_ASC(c);
1831#endif
1832 switch (c)
1833 {
1834 case NUL: /* "vim -" read from stdin */
1835 /* "ex -" silent mode */
1836 if (exmode_active)
1837 silent_mode = TRUE;
1838 else
1839 {
1840 if (parmp->edit_type != EDIT_NONE)
1841 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1842 parmp->edit_type = EDIT_STDIN;
1843 read_cmd_fd = 2; /* read from stderr instead of stdin */
1844 }
1845 argv_idx = -1; /* skip to next argument */
1846 break;
1847
1848 case '-': /* "--" don't take any more option arguments */
1849 /* "--help" give help message */
1850 /* "--version" give version message */
1851 /* "--literal" take files literally */
1852 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001853 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001854 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001855 /* "--noplugin[s]" skip plugins */
1856 /* "--cmd <cmd>" execute cmd before vimrc */
1857 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1858 usage();
1859 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1860 {
1861 Columns = 80; /* need to init Columns */
1862 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1863 list_version();
1864 msg_putchar('\n');
1865 msg_didout = FALSE;
1866 mch_exit(0);
1867 }
1868 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1869 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001870#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001871 parmp->literal = TRUE;
1872#endif
1873 }
1874 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1875 {
1876#ifdef FEAT_GUI
1877 gui.dofork = FALSE; /* don't fork() when starting GUI */
1878#endif
1879 }
1880 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1881 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001882 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1883 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001884 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1885 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001886 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1887 {
1888 want_argument = TRUE;
1889 argv_idx += 3;
1890 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001891 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1892 {
1893 want_argument = TRUE;
1894 argv_idx += 11;
1895 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001896#ifdef FEAT_CLIENTSERVER
1897 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1898 ; /* already processed -- no arg */
1899 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1900 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1901 {
1902 /* already processed -- snatch the following arg */
1903 if (argc > 1)
1904 {
1905 --argc;
1906 ++argv;
1907 }
1908 }
1909#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001910#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1911# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001912 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001913# else
1914 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1915# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001916 {
1917 /* already processed -- snatch the following arg */
1918 if (argc > 1)
1919 {
1920 --argc;
1921 ++argv;
1922 }
1923 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001924#endif
1925#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001926 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1927 {
1928 /* already processed, skip */
1929 }
1930#endif
1931 else
1932 {
1933 if (argv[0][argv_idx])
1934 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1935 had_minmin = TRUE;
1936 }
1937 if (!want_argument)
1938 argv_idx = -1; /* skip to next argument */
1939 break;
1940
1941 case 'A': /* "-A" start in Arabic mode */
1942#ifdef FEAT_ARABIC
1943 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1944#else
1945 mch_errmsg(_(e_noarabic));
1946 mch_exit(2);
1947#endif
1948 break;
1949
1950 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001951 /* Needs to be effective before expanding file names, because
1952 * for Win32 this makes us edit a shortcut file itself,
1953 * instead of the file it links to. */
1954 set_options_bin(curbuf->b_p_bin, 1, 0);
1955 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001956 break;
1957
1958 case 'C': /* "-C" Compatible */
1959 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001960 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001961 break;
1962
1963 case 'e': /* "-e" Ex mode */
1964 exmode_active = EXMODE_NORMAL;
1965 break;
1966
1967 case 'E': /* "-E" Improved Ex mode */
1968 exmode_active = EXMODE_VIM;
1969 break;
1970
1971 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1972 window directly, not with newcli */
1973#ifdef FEAT_GUI
1974 gui.dofork = FALSE; /* don't fork() when starting GUI */
1975#endif
1976 break;
1977
1978 case 'g': /* "-g" start GUI */
1979 main_start_gui();
1980 break;
1981
1982 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1983#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001984 p_fkmap = TRUE;
1985 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001986#else
1987 mch_errmsg(_(e_nofarsi));
1988 mch_exit(2);
1989#endif
1990 break;
1991
1992 case 'h': /* "-h" give help message */
1993#ifdef FEAT_GUI_GNOME
1994 /* Tell usage() to exit for "gvim". */
1995 gui.starting = FALSE;
1996#endif
1997 usage();
1998 break;
1999
2000 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2001#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002002 p_hkmap = TRUE;
2003 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002004#else
2005 mch_errmsg(_(e_nohebrew));
2006 mch_exit(2);
2007#endif
2008 break;
2009
2010 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2011#ifdef FEAT_LISP
2012 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2013 p_sm = TRUE;
2014#endif
2015 break;
2016
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002017 case 'M': /* "-M" no changes or writing of files */
2018 reset_modifiable();
2019 /* FALLTHROUGH */
2020
2021 case 'm': /* "-m" no writing of files */
2022 p_write = FALSE;
2023 break;
2024
2025 case 'y': /* "-y" easy mode */
2026#ifdef FEAT_GUI
2027 gui.starting = TRUE; /* start GUI a bit later */
2028#endif
2029 parmp->evim_mode = TRUE;
2030 break;
2031
2032 case 'N': /* "-N" Nocompatible */
2033 change_compatible(FALSE);
2034 break;
2035
2036 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002037#ifdef FEAT_NETBEANS_INTG
2038 /* checking for "-nb", netbeans parameters */
2039 if (argv[0][argv_idx] == 'b')
2040 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002041 netbeansArg = argv[0];
2042 argv_idx = -1; /* skip to next argument */
2043 }
2044 else
2045#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002046 parmp->no_swap_file = TRUE;
2047 break;
2048
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002049 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002050#ifdef TARGET_API_MAC_OSX
2051 /* For some reason on MacOS X, an argument like:
2052 -psn_0_10223617 is passed in when invoke from Finder
2053 or with the 'open' command */
2054 if (argv[0][argv_idx] == 's')
2055 {
2056 argv_idx = -1; /* bypass full -psn */
2057 main_start_gui();
2058 break;
2059 }
2060#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002061#ifdef FEAT_WINDOWS
2062 /* default is 0: open window for each file */
2063 parmp->window_count = get_number_arg((char_u *)argv[0],
2064 &argv_idx, 0);
2065 parmp->window_layout = WIN_TABS;
2066#endif
2067 break;
2068
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002069 case 'o': /* "-o[N]" open N horizontal split windows */
2070#ifdef FEAT_WINDOWS
2071 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002072 parmp->window_count = get_number_arg((char_u *)argv[0],
2073 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002074 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002075#endif
2076 break;
2077
2078 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002079#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002080 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002081 parmp->window_count = get_number_arg((char_u *)argv[0],
2082 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002083 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002084#endif
2085 break;
2086
2087#ifdef FEAT_QUICKFIX
2088 case 'q': /* "-q" QuickFix mode */
2089 if (parmp->edit_type != EDIT_NONE)
2090 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2091 parmp->edit_type = EDIT_QF;
2092 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2093 {
2094 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2095 argv_idx = -1;
2096 }
2097 else if (argc > 1) /* "-q {errorfile}" */
2098 want_argument = TRUE;
2099 break;
2100#endif
2101
2102 case 'R': /* "-R" readonly mode */
2103 readonlymode = TRUE;
2104 curbuf->b_p_ro = TRUE;
2105 p_uc = 10000; /* don't update very often */
2106 break;
2107
2108 case 'r': /* "-r" recovery mode */
2109 case 'L': /* "-L" recovery mode */
2110 recoverymode = 1;
2111 break;
2112
2113 case 's':
2114 if (exmode_active) /* "-s" silent (batch) mode */
2115 silent_mode = TRUE;
2116 else /* "-s {scriptin}" read from script file */
2117 want_argument = TRUE;
2118 break;
2119
2120 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2121 if (parmp->edit_type != EDIT_NONE)
2122 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2123 parmp->edit_type = EDIT_TAG;
2124 if (argv[0][argv_idx]) /* "-t{tag}" */
2125 {
2126 parmp->tagname = (char_u *)argv[0] + argv_idx;
2127 argv_idx = -1;
2128 }
2129 else /* "-t {tag}" */
2130 want_argument = TRUE;
2131 break;
2132
2133#ifdef FEAT_EVAL
2134 case 'D': /* "-D" Debugging */
2135 parmp->use_debug_break_level = 9999;
2136 break;
2137#endif
2138#ifdef FEAT_DIFF
2139 case 'd': /* "-d" 'diff' */
2140# ifdef AMIGA
2141 /* check for "-dev {device}" */
2142 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2143 want_argument = TRUE;
2144 else
2145# endif
2146 parmp->diff_mode = TRUE;
2147 break;
2148#endif
2149 case 'V': /* "-V{N}" Verbose level */
2150 /* default is 10: a little bit verbose */
2151 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2152 if (argv[0][argv_idx] != NUL)
2153 {
2154 set_option_value((char_u *)"verbosefile", 0L,
2155 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002156 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002157 }
2158 break;
2159
2160 case 'v': /* "-v" Vi-mode (as if called "vi") */
2161 exmode_active = 0;
2162#ifdef FEAT_GUI
2163 gui.starting = FALSE; /* don't start GUI */
2164#endif
2165 break;
2166
2167 case 'w': /* "-w{number}" set window height */
2168 /* "-w {scriptout}" write to script */
2169 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2170 {
2171 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2172 set_option_value((char_u *)"window", n, NULL, 0);
2173 break;
2174 }
2175 want_argument = TRUE;
2176 break;
2177
2178#ifdef FEAT_CRYPT
2179 case 'x': /* "-x" encrypted reading/writing of files */
2180 parmp->ask_for_key = TRUE;
2181 break;
2182#endif
2183
2184 case 'X': /* "-X" don't connect to X server */
2185#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2186 x_no_connect = TRUE;
2187#endif
2188 break;
2189
2190 case 'Z': /* "-Z" restricted mode */
2191 restricted = TRUE;
2192 break;
2193
2194 case 'c': /* "-c{command}" or "-c {command}" execute
2195 command */
2196 if (argv[0][argv_idx] != NUL)
2197 {
2198 if (parmp->n_commands >= MAX_ARG_CMDS)
2199 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002200 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2201 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002202 argv_idx = -1;
2203 break;
2204 }
2205 /*FALLTHROUGH*/
2206 case 'S': /* "-S {file}" execute Vim script */
2207 case 'i': /* "-i {viminfo}" use for viminfo */
2208#ifndef FEAT_DIFF
2209 case 'd': /* "-d {device}" device (for Amiga) */
2210#endif
2211 case 'T': /* "-T {terminal}" terminal name */
2212 case 'u': /* "-u {vimrc}" vim inits file */
2213 case 'U': /* "-U {gvimrc}" gvim inits file */
2214 case 'W': /* "-W {scriptout}" overwrite */
2215#ifdef FEAT_GUI_W32
2216 case 'P': /* "-P {parent title}" MDI parent */
2217#endif
2218 want_argument = TRUE;
2219 break;
2220
2221 default:
2222 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2223 }
2224
2225 /*
2226 * Handle option arguments with argument.
2227 */
2228 if (want_argument)
2229 {
2230 /*
2231 * Check for garbage immediately after the option letter.
2232 */
2233 if (argv[0][argv_idx] != NUL)
2234 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2235
2236 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002237 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002238 mainerr_arg_missing((char_u *)argv[0]);
2239 ++argv;
2240 argv_idx = -1;
2241
2242 switch (c)
2243 {
2244 case 'c': /* "-c {command}" execute command */
2245 case 'S': /* "-S {file}" execute Vim script */
2246 if (parmp->n_commands >= MAX_ARG_CMDS)
2247 mainerr(ME_EXTRA_CMD, NULL);
2248 if (c == 'S')
2249 {
2250 char *a;
2251
2252 if (argc < 1)
2253 /* "-S" without argument: use default session file
2254 * name. */
2255 a = SESSION_FILE;
2256 else if (argv[0][0] == '-')
2257 {
2258 /* "-S" followed by another option: use default
2259 * session file name. */
2260 a = SESSION_FILE;
2261 ++argc;
2262 --argv;
2263 }
2264 else
2265 a = argv[0];
2266 p = alloc((unsigned)(STRLEN(a) + 4));
2267 if (p == NULL)
2268 mch_exit(2);
2269 sprintf((char *)p, "so %s", a);
2270 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2271 parmp->commands[parmp->n_commands++] = p;
2272 }
2273 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002274 parmp->commands[parmp->n_commands++] =
2275 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002276 break;
2277
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002278 case '-':
2279 if (argv[-1][2] == 'c')
2280 {
2281 /* "--cmd {command}" execute command */
2282 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2283 mainerr(ME_EXTRA_CMD, NULL);
2284 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002285 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002286 }
2287 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002288 break;
2289
2290 /* case 'd': -d {device} is handled in mch_check_win() for the
2291 * Amiga */
2292
2293#ifdef FEAT_QUICKFIX
2294 case 'q': /* "-q {errorfile}" QuickFix mode */
2295 parmp->use_ef = (char_u *)argv[0];
2296 break;
2297#endif
2298
2299 case 'i': /* "-i {viminfo}" use for viminfo */
2300 use_viminfo = (char_u *)argv[0];
2301 break;
2302
2303 case 's': /* "-s {scriptin}" read from script file */
2304 if (scriptin[0] != NULL)
2305 {
2306scripterror:
2307 mch_errmsg(_("Attempt to open script file again: \""));
2308 mch_errmsg(argv[-1]);
2309 mch_errmsg(" ");
2310 mch_errmsg(argv[0]);
2311 mch_errmsg("\"\n");
2312 mch_exit(2);
2313 }
2314 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2315 {
2316 mch_errmsg(_("Cannot open for reading: \""));
2317 mch_errmsg(argv[0]);
2318 mch_errmsg("\"\n");
2319 mch_exit(2);
2320 }
2321 if (save_typebuf() == FAIL)
2322 mch_exit(2); /* out of memory */
2323 break;
2324
2325 case 't': /* "-t {tag}" */
2326 parmp->tagname = (char_u *)argv[0];
2327 break;
2328
2329 case 'T': /* "-T {terminal}" terminal name */
2330 /*
2331 * The -T term argument is always available and when
2332 * HAVE_TERMLIB is supported it overrides the environment
2333 * variable TERM.
2334 */
2335#ifdef FEAT_GUI
2336 if (term_is_gui((char_u *)argv[0]))
2337 gui.starting = TRUE; /* start GUI a bit later */
2338 else
2339#endif
2340 parmp->term = (char_u *)argv[0];
2341 break;
2342
2343 case 'u': /* "-u {vimrc}" vim inits file */
2344 parmp->use_vimrc = (char_u *)argv[0];
2345 break;
2346
2347 case 'U': /* "-U {gvimrc}" gvim inits file */
2348#ifdef FEAT_GUI
2349 use_gvimrc = (char_u *)argv[0];
2350#endif
2351 break;
2352
2353 case 'w': /* "-w {nr}" 'window' value */
2354 /* "-w {scriptout}" append to script file */
2355 if (vim_isdigit(*((char_u *)argv[0])))
2356 {
2357 argv_idx = 0;
2358 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2359 set_option_value((char_u *)"window", n, NULL, 0);
2360 argv_idx = -1;
2361 break;
2362 }
2363 /*FALLTHROUGH*/
2364 case 'W': /* "-W {scriptout}" overwrite script file */
2365 if (scriptout != NULL)
2366 goto scripterror;
2367 if ((scriptout = mch_fopen(argv[0],
2368 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2369 {
2370 mch_errmsg(_("Cannot open for script output: \""));
2371 mch_errmsg(argv[0]);
2372 mch_errmsg("\"\n");
2373 mch_exit(2);
2374 }
2375 break;
2376
2377#ifdef FEAT_GUI_W32
2378 case 'P': /* "-P {parent title}" MDI parent */
2379 gui_mch_set_parent(argv[0]);
2380 break;
2381#endif
2382 }
2383 }
2384 }
2385
2386 /*
2387 * File name argument.
2388 */
2389 else
2390 {
2391 argv_idx = -1; /* skip to next argument */
2392
2393 /* Check for only one type of editing. */
2394 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2395 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2396 parmp->edit_type = EDIT_FILE;
2397
2398#ifdef MSWIN
2399 /* Remember if the argument was a full path before changing
2400 * slashes to backslashes. */
2401 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2402 parmp->full_path = TRUE;
2403#endif
2404
2405 /* Add the file to the global argument list. */
2406 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2407 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2408 mch_exit(2);
2409#ifdef FEAT_DIFF
2410 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2411 && !mch_isdir(alist_name(&GARGLIST[0])))
2412 {
2413 char_u *r;
2414
2415 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2416 if (r != NULL)
2417 {
2418 vim_free(p);
2419 p = r;
2420 }
2421 }
2422#endif
2423#if defined(__CYGWIN32__) && !defined(WIN32)
2424 /*
2425 * If vim is invoked by non-Cygwin tools, convert away any
2426 * DOS paths, so things like .swp files are created correctly.
2427 * Look for evidence of non-Cygwin paths before we bother.
2428 * This is only for when using the Unix files.
2429 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002430 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002431 {
2432 char posix_path[PATH_MAX];
2433
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002434# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2435 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2436# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002438# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002440 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002441 if (p == NULL)
2442 mch_exit(2);
2443 }
2444#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002445
2446#ifdef USE_FNAME_CASE
2447 /* Make the case of the file name match the actual file. */
2448 fname_case(p, 0);
2449#endif
2450
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002451 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002452#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002453 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454#else
2455 2 /* add buffer number now and use curbuf */
2456#endif
2457 );
2458
2459#if defined(FEAT_MBYTE) && defined(WIN32)
2460 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002461 /* Remember this argument has been added to the argument list.
2462 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002463 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002464# ifdef FEAT_DIFF
2465 parmp->diff_mode
2466# else
2467 FALSE
2468# endif
2469 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002470 }
2471#endif
2472 }
2473
2474 /*
2475 * If there are no more letters after the current "-", go to next
2476 * argument. argv_idx is set to -1 when the current argument is to be
2477 * skipped.
2478 */
2479 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2480 {
2481 --argc;
2482 ++argv;
2483 argv_idx = 1;
2484 }
2485 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002486
2487#ifdef FEAT_EVAL
2488 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2489 * one. */
2490 if (parmp->n_commands > 0)
2491 {
2492 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2493 if (p != NULL)
2494 {
2495 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2496 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2497 vim_free(p);
2498 }
2499 }
2500#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002501}
2502
2503/*
2504 * Print a warning if stdout is not a terminal.
2505 * When starting in Ex mode and commands come from a file, set Silent mode.
2506 */
2507 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002508check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002509{
2510 int input_isatty; /* is active input a terminal? */
2511
2512 input_isatty = mch_input_isatty();
2513 if (exmode_active)
2514 {
2515 if (!input_isatty)
2516 silent_mode = TRUE;
2517 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002518 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002519#ifdef FEAT_GUI
2520 /* don't want the delay when started from the desktop */
2521 && !gui.starting
2522#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002523 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002524 {
2525#ifdef NBDEBUG
2526 /*
2527 * This shouldn't be necessary. But if I run netbeans with the log
2528 * output coming to the console and XOpenDisplay fails, I get vim
2529 * trying to start with input/output to my console tty. This fills my
2530 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002531 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002532 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002533 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002534 {
2535 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2536 exit(1);
2537 }
2538#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002539#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2540 if (is_cygpty_used())
2541 {
2542 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2543 exit(1);
2544 }
2545#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002546 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002547 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2548 if (!input_isatty)
2549 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2550 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002551 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2552 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002553 if (scriptin[0] == NULL)
2554 ui_delay(2000L, TRUE);
2555 TIME_MSG("Warning delay");
2556 }
2557}
2558
2559/*
2560 * Read text from stdin.
2561 */
2562 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002563read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002564{
2565 int i;
2566
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002567#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568 /* When getting the ATTENTION prompt here, use a dialog */
2569 swap_exists_action = SEA_DIALOG;
2570#endif
2571 no_wait_return = TRUE;
2572 i = msg_didany;
2573 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002574 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002575 no_wait_return = FALSE;
2576 msg_didany = i;
2577 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002578#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002579 check_swap_exists_action();
2580#endif
2581#if !(defined(AMIGA) || defined(MACOS))
2582 /*
2583 * Close stdin and dup it from stderr. Required for GPM to work
2584 * properly, and for running external commands.
2585 * Is there any other system that cannot do this?
2586 */
2587 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002588 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589#endif
2590}
2591
2592/*
2593 * Create the requested number of windows and edit buffers in them.
2594 * Also does recovery if "recoverymode" set.
2595 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002597create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002598{
2599#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002600 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002601 int done = 0;
2602
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002603 /*
2604 * Create the number of windows that was requested.
2605 */
2606 if (parmp->window_count == -1) /* was not set */
2607 parmp->window_count = 1;
2608 if (parmp->window_count == 0)
2609 parmp->window_count = GARGCOUNT;
2610 if (parmp->window_count > 1)
2611 {
2612 /* Don't change the windows if there was a command in .vimrc that
2613 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002614 if (parmp->window_layout == 0)
2615 parmp->window_layout = WIN_HOR;
2616 if (parmp->window_layout == WIN_TABS)
2617 {
2618 parmp->window_count = make_tabpages(parmp->window_count);
2619 TIME_MSG("making tab pages");
2620 }
2621 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002622 {
2623 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002624 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625 TIME_MSG("making windows");
2626 }
2627 else
2628 parmp->window_count = win_count();
2629 }
2630 else
2631 parmp->window_count = 1;
2632#endif
2633
2634 if (recoverymode) /* do recover */
2635 {
2636 msg_scroll = TRUE; /* scroll message up */
2637 ml_recover();
2638 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2639 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002640 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002641 }
2642 else
2643 {
2644 /*
2645 * Open a buffer for windows that don't have one yet.
2646 * Commands in the .vimrc might have loaded a file or split the window.
2647 * Watch out for autocommands that delete a window.
2648 */
2649#ifdef FEAT_AUTOCMD
2650 /*
2651 * Don't execute Win/Buf Enter/Leave autocommands here
2652 */
2653 ++autocmd_no_enter;
2654 ++autocmd_no_leave;
2655#endif
2656#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002657 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002658 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002659 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002660 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002661 {
2662 if (parmp->window_layout == WIN_TABS)
2663 goto_tabpage(1);
2664 else
2665 curwin = firstwin;
2666 }
2667 else if (parmp->window_layout == WIN_TABS)
2668 {
2669 if (curtab->tp_next == NULL)
2670 break;
2671 goto_tabpage(0);
2672 }
2673 else
2674 {
2675 if (curwin->w_next == NULL)
2676 break;
2677 curwin = curwin->w_next;
2678 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002679 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002680#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002681 curbuf = curwin->w_buffer;
2682 if (curbuf->b_ml.ml_mfp == NULL)
2683 {
2684#ifdef FEAT_FOLDING
2685 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2686 if (p_fdls >= 0)
2687 curwin->w_p_fdl = p_fdls;
2688#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002689#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690 /* When getting the ATTENTION prompt here, use a dialog */
2691 swap_exists_action = SEA_DIALOG;
2692#endif
2693 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002694
2695 /* create memfile, read file */
2696 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002697
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002698#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002699 if (swap_exists_action == SEA_QUIT)
2700 {
2701 if (got_int || only_one_window())
2702 {
2703 /* abort selected or quit and only one window */
2704 did_emsg = FALSE; /* avoid hit-enter prompt */
2705 getout(1);
2706 }
2707 /* We can't close the window, it would disturb what
2708 * happens next. Clear the file name and set the arg
2709 * index to -1 to delete it later. */
2710 setfname(curbuf, NULL, NULL, FALSE);
2711 curwin->w_arg_idx = -1;
2712 swap_exists_action = SEA_NONE;
2713 }
2714 else
2715 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002716#endif
2717#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002718 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719#endif
2720 }
2721#ifdef FEAT_WINDOWS
2722 ui_breakcheck();
2723 if (got_int)
2724 {
2725 (void)vgetc(); /* only break the file loading, not the rest */
2726 break;
2727 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002729#endif
2730#ifdef FEAT_WINDOWS
2731 if (parmp->window_layout == WIN_TABS)
2732 goto_tabpage(1);
2733 else
2734 curwin = firstwin;
2735 curbuf = curwin->w_buffer;
2736#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737#ifdef FEAT_AUTOCMD
2738 --autocmd_no_enter;
2739 --autocmd_no_leave;
2740#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002741 }
2742}
2743
2744#ifdef FEAT_WINDOWS
2745 /*
2746 * If opened more than one window, start editing files in the other
2747 * windows. make_windows() has already opened the windows.
2748 */
2749 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002750edit_buffers(
2751 mparm_T *parmp,
2752 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002753{
2754 int arg_idx; /* index in argument list */
2755 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002756 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002757 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758
2759# ifdef FEAT_AUTOCMD
2760 /*
2761 * Don't execute Win/Buf Enter/Leave autocommands here
2762 */
2763 ++autocmd_no_enter;
2764 ++autocmd_no_leave;
2765# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002766
2767 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2768 if (curwin->w_arg_idx == -1)
2769 {
2770 win_close(curwin, TRUE);
2771 advance = FALSE;
2772 }
2773
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774 arg_idx = 1;
2775 for (i = 1; i < parmp->window_count; ++i)
2776 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002777 if (cwd != NULL)
2778 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002779 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2780 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002781 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002782 ++arg_idx;
2783 win_close(curwin, TRUE);
2784 advance = FALSE;
2785 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002786 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002787
Bram Moolenaar84212822006-11-07 21:59:47 +00002788 if (advance)
2789 {
2790 if (parmp->window_layout == WIN_TABS)
2791 {
2792 if (curtab->tp_next == NULL) /* just checking */
2793 break;
2794 goto_tabpage(0);
2795 }
2796 else
2797 {
2798 if (curwin->w_next == NULL) /* just checking */
2799 break;
2800 win_enter(curwin->w_next, FALSE);
2801 }
2802 }
2803 advance = TRUE;
2804
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002805 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002806 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002807 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2808 {
2809 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002810 /* Edit file from arg list, if there is one. When "Quit" selected
2811 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002812# ifdef HAS_SWAP_EXISTS_ACTION
2813 swap_exists_did_quit = FALSE;
2814# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002815 (void)do_ecmd(0, arg_idx < GARGCOUNT
2816 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002817 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002818# ifdef HAS_SWAP_EXISTS_ACTION
2819 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002820 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002821 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002822 if (got_int || only_one_window())
2823 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002824 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002825 did_emsg = FALSE; /* avoid hit-enter prompt */
2826 getout(1);
2827 }
2828 win_close(curwin, TRUE);
2829 advance = FALSE;
2830 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002831# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002832 if (arg_idx == GARGCOUNT - 1)
2833 arg_had_last = TRUE;
2834 ++arg_idx;
2835 }
2836 ui_breakcheck();
2837 if (got_int)
2838 {
2839 (void)vgetc(); /* only break the file loading, not the rest */
2840 break;
2841 }
2842 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002843
2844 if (parmp->window_layout == WIN_TABS)
2845 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002846# ifdef FEAT_AUTOCMD
2847 --autocmd_no_enter;
2848# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002849
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002850 /* make the first window the current window */
2851 win = firstwin;
2852#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2853 /* Avoid making a preview window the current window. */
2854 while (win->w_p_pvw)
2855 {
2856 win = win->w_next;
2857 if (win == NULL)
2858 {
2859 win = firstwin;
2860 break;
2861 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002862 }
2863#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002864 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002865
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002866# ifdef FEAT_AUTOCMD
2867 --autocmd_no_leave;
2868# endif
2869 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002870 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2872}
2873#endif /* FEAT_WINDOWS */
2874
2875/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002876 * Execute the commands from --cmd arguments "cmds[cnt]".
2877 */
2878 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002879exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002880{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002881 char_u **cmds = parmp->pre_commands;
2882 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002883 int i;
2884
2885 if (cnt > 0)
2886 {
2887 curwin->w_cursor.lnum = 0; /* just in case.. */
2888 sourcing_name = (char_u *)_("pre-vimrc command line");
2889# ifdef FEAT_EVAL
2890 current_SID = SID_CMDARG;
2891# endif
2892 for (i = 0; i < cnt; ++i)
2893 do_cmdline_cmd(cmds[i]);
2894 sourcing_name = NULL;
2895# ifdef FEAT_EVAL
2896 current_SID = 0;
2897# endif
2898 TIME_MSG("--cmd commands");
2899 }
2900}
2901
2902/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903 * Execute "+", "-c" and "-S" arguments.
2904 */
2905 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002906exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002907{
2908 int i;
2909
2910 /*
2911 * We start commands on line 0, make "vim +/pat file" match a
2912 * pattern on line 1. But don't move the cursor when an autocommand
2913 * with g`" was used.
2914 */
2915 msg_scroll = TRUE;
2916 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2917 curwin->w_cursor.lnum = 0;
2918 sourcing_name = (char_u *)"command line";
2919#ifdef FEAT_EVAL
2920 current_SID = SID_CARG;
2921#endif
2922 for (i = 0; i < parmp->n_commands; ++i)
2923 {
2924 do_cmdline_cmd(parmp->commands[i]);
2925 if (parmp->cmds_tofree[i])
2926 vim_free(parmp->commands[i]);
2927 }
2928 sourcing_name = NULL;
2929#ifdef FEAT_EVAL
2930 current_SID = 0;
2931#endif
2932 if (curwin->w_cursor.lnum == 0)
2933 curwin->w_cursor.lnum = 1;
2934
2935 if (!exmode_active)
2936 msg_scroll = FALSE;
2937
2938#ifdef FEAT_QUICKFIX
2939 /* When started with "-q errorfile" jump to first error again. */
2940 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002941 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002942#endif
2943 TIME_MSG("executing command arguments");
2944}
2945
2946/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002947 * Source startup scripts.
2948 */
2949 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002950source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002951{
2952 int i;
2953
2954 /*
2955 * For "evim" source evim.vim first of all, so that the user can overrule
2956 * any things he doesn't like.
2957 */
2958 if (parmp->evim_mode)
2959 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002960 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002961 TIME_MSG("source evim file");
2962 }
2963
2964 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002965 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002966 * nothing else.
2967 */
2968 if (parmp->use_vimrc != NULL)
2969 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002970 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2971 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002972 {
2973#ifdef FEAT_GUI
2974 if (use_gvimrc == NULL) /* don't load gvimrc either */
2975 use_gvimrc = parmp->use_vimrc;
2976#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002977 }
2978 else
2979 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002980 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002981 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2982 }
2983 }
2984 else if (!silent_mode)
2985 {
2986#ifdef AMIGA
2987 struct Process *proc = (struct Process *)FindTask(0L);
2988 APTR save_winptr = proc->pr_WindowPtr;
2989
2990 /* Avoid a requester here for a volume that doesn't exist. */
2991 proc->pr_WindowPtr = (APTR)-1L;
2992#endif
2993
2994 /*
2995 * Get system wide defaults, if the file name is defined.
2996 */
2997#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002998 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002999#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003000#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003001 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003002#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003
3004 /*
3005 * Try to read initialization commands from the following places:
3006 * - environment variable VIMINIT
3007 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3008 * - second user vimrc file ($VIM/.vimrc for Dos)
3009 * - environment variable EXINIT
3010 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3011 * - second user exrc file ($VIM/.exrc for Dos)
3012 * The first that exists is used, the rest is ignored.
3013 */
3014 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3015 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003016 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003017#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003018 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3019 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003020#endif
3021#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003022 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3023 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003024#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003025#ifdef USR_VIMRC_FILE4
3026 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3027 DOSO_VIMRC) == FAIL
3028#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003030 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003031#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003032 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003033#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003034 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003035 {
3036 /* When no .vimrc file was found: source defaults.vim. */
3037 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003038 }
3039 }
3040
3041 /*
3042 * Read initialization commands from ".vimrc" or ".exrc" in current
3043 * directory. This is only done if the 'exrc' option is set.
3044 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003045 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003046 * option has been reset in environment of global ".exrc" or ".vimrc".
3047 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3048 * SYS_VIMRC_FILE.
3049 */
3050 if (p_exrc)
3051 {
3052#if defined(UNIX) || defined(VMS)
3053 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3054 if (!file_owned(VIMRC_FILE))
3055#endif
3056 secure = p_secure;
3057
3058 i = FAIL;
3059 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3060 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3061#ifdef USR_VIMRC_FILE2
3062 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3063 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3064#endif
3065#ifdef USR_VIMRC_FILE3
3066 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3067 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3068#endif
3069#ifdef SYS_VIMRC_FILE
3070 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3071 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3072#endif
3073 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003074 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003075
3076 if (i == FAIL)
3077 {
3078#if defined(UNIX) || defined(VMS)
3079 /* if ".exrc" is not owned by user set 'secure' mode */
3080 if (!file_owned(EXRC_FILE))
3081 secure = p_secure;
3082 else
3083 secure = 0;
3084#endif
3085 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3086 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3087#ifdef USR_EXRC_FILE2
3088 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3089 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3090#endif
3091 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003092 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003093 }
3094 }
3095 if (secure == 2)
3096 need_wait_return = TRUE;
3097 secure = 0;
3098#ifdef AMIGA
3099 proc->pr_WindowPtr = save_winptr;
3100#endif
3101 }
3102 TIME_MSG("sourcing vimrc file(s)");
3103}
3104
3105/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003106 * Setup to start using the GUI. Exit with an error when not available.
3107 */
3108 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003109main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003110{
3111#ifdef FEAT_GUI
3112 gui.starting = TRUE; /* start GUI a bit later */
3113#else
3114 mch_errmsg(_(e_nogvim));
3115 mch_errmsg("\n");
3116 mch_exit(2);
3117#endif
3118}
3119
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003120#endif /* NO_VIM_MAIN */
3121
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003122/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003123 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003124 * Returns FAIL if the environment variable was not executed, OK otherwise.
3125 */
3126 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003127process_env(
3128 char_u *env,
3129 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003130{
3131 char_u *initstr;
3132 char_u *save_sourcing_name;
3133 linenr_T save_sourcing_lnum;
3134#ifdef FEAT_EVAL
3135 scid_T save_sid;
3136#endif
3137
3138 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3139 {
3140 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003141 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003142 save_sourcing_name = sourcing_name;
3143 save_sourcing_lnum = sourcing_lnum;
3144 sourcing_name = env;
3145 sourcing_lnum = 0;
3146#ifdef FEAT_EVAL
3147 save_sid = current_SID;
3148 current_SID = SID_ENV;
3149#endif
3150 do_cmdline_cmd(initstr);
3151 sourcing_name = save_sourcing_name;
3152 sourcing_lnum = save_sourcing_lnum;
3153#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003154 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003155#endif
3156 return OK;
3157 }
3158 return FAIL;
3159}
3160
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003161#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003162/*
3163 * Return TRUE if we are certain the user owns the file "fname".
3164 * Used for ".vimrc" and ".exrc".
3165 * Use both stat() and lstat() for extra security.
3166 */
3167 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003168file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003169{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003170 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003171# ifdef UNIX
3172 uid_t uid = getuid();
3173# else /* VMS */
3174 uid_t uid = ((getgid() << 16) | getuid());
3175# endif
3176
3177 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3178# ifdef HAVE_LSTAT
3179 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3180# endif
3181 );
3182}
3183#endif
3184
3185/*
3186 * Give an error message main_errors["n"] and exit.
3187 */
3188 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003189mainerr(
3190 int n, /* one of the ME_ defines */
3191 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003192{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003193#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003194 reset_signals(); /* kill us with CTRL-C here, if you like */
3195#endif
3196
3197 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003198 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003199 mch_errmsg(_(main_errors[n]));
3200 if (str != NULL)
3201 {
3202 mch_errmsg(": \"");
3203 mch_errmsg((char *)str);
3204 mch_errmsg("\"");
3205 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003206 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003207
3208 mch_exit(1);
3209}
3210
3211 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003212mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003213{
3214 mainerr(ME_ARG_MISSING, str);
3215}
3216
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003217#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218/*
3219 * print a message with three spaces prepended and '\n' appended.
3220 */
3221 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003222main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003223{
3224 mch_msg(" ");
3225 mch_msg(s);
3226 mch_msg("\n");
3227}
3228
3229/*
3230 * Print messages for "vim -h" or "vim --help" and exit.
3231 */
3232 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003233usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003234{
3235 int i;
3236 static char *(use[]) =
3237 {
3238 N_("[file ..] edit specified file(s)"),
3239 N_("- read text from stdin"),
3240 N_("-t tag edit file where tag is defined"),
3241#ifdef FEAT_QUICKFIX
3242 N_("-q [errorfile] edit file with first error")
3243#endif
3244 };
3245
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003246#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003247 reset_signals(); /* kill us with CTRL-C here, if you like */
3248#endif
3249
3250 mch_msg(longVersion);
3251 mch_msg(_("\n\nusage:"));
3252 for (i = 0; ; ++i)
3253 {
3254 mch_msg(_(" vim [arguments] "));
3255 mch_msg(_(use[i]));
3256 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3257 break;
3258 mch_msg(_("\n or:"));
3259 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003260#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003261 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003262#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003263
3264 mch_msg(_("\n\nArguments:\n"));
3265 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003266#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003267 main_msg(_("--literal\t\tDon't expand wildcards"));
3268#endif
3269#ifdef FEAT_OLE
3270 main_msg(_("-register\t\tRegister this gvim for OLE"));
3271 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3272#endif
3273#ifdef FEAT_GUI
3274 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3275 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3276#endif
3277 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3278 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003279 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003280 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3281#ifdef FEAT_DIFF
3282 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3283#endif
3284 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3285 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3286 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3287 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3288 main_msg(_("-M\t\t\tModifications in text not allowed"));
3289 main_msg(_("-b\t\t\tBinary mode"));
3290#ifdef FEAT_LISP
3291 main_msg(_("-l\t\t\tLisp mode"));
3292#endif
3293 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3294 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003295 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3296#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003297 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003298#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003299 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3300 main_msg(_("-r\t\t\tList swap files and exit"));
3301 main_msg(_("-r (with file name)\tRecover crashed session"));
3302 main_msg(_("-L\t\t\tSame as -r"));
3303#ifdef AMIGA
3304 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3305 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3306#endif
3307#ifdef FEAT_ARABIC
3308 main_msg(_("-A\t\t\tstart in Arabic mode"));
3309#endif
3310#ifdef FEAT_RIGHTLEFT
3311 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3312#endif
3313#ifdef FEAT_FKMAP
3314 main_msg(_("-F\t\t\tStart in Farsi mode"));
3315#endif
3316 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003317 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003318 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003319 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3320#ifdef FEAT_GUI
3321 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3322#endif
3323 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003324#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003325 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003326 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3327 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003328#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003329 main_msg(_("+\t\t\tStart at end of file"));
3330 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003332 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3333 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3334 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3335 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3336 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3337#ifdef FEAT_CRYPT
3338 main_msg(_("-x\t\t\tEdit encrypted files"));
3339#endif
3340#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3341# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3342 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3343# endif
3344 main_msg(_("-X\t\t\tDo not connect to X server"));
3345#endif
3346#ifdef FEAT_CLIENTSERVER
3347 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3348 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3349 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3350 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003351# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003352 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003353# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003354 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3355 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3356 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3357 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3358#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003359#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003360 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003361#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003362#ifdef FEAT_VIMINFO
3363 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3364#endif
3365 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3366 main_msg(_("--version\t\tPrint version information and exit"));
3367
3368#ifdef FEAT_GUI_X11
3369# ifdef FEAT_GUI_MOTIF
3370 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3371# else
3372# ifdef FEAT_GUI_ATHENA
3373# ifdef FEAT_GUI_NEXTAW
3374 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3375# else
3376 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3377# endif
3378# endif
3379# endif
3380 main_msg(_("-display <display>\tRun vim on <display>"));
3381 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003382 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3383 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3384 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3385 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3386 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3387 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3388 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3389 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3390# ifdef FEAT_GUI_ATHENA
3391 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3392# endif
3393 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3394 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3395 main_msg(_("-xrm <resource>\tSet the specified resource"));
3396#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397#ifdef FEAT_GUI_GTK
3398 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3399 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3400 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3401 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3402 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003405 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003407#ifdef FEAT_GUI_W32
3408 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003409 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003410#endif
3411
3412#ifdef FEAT_GUI_GNOME
3413 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3414 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003415 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003416 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003417 gui.dofork = FALSE;
3418 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003419 else
3420#endif
3421 mch_exit(0);
3422}
3423
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003424#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003425/*
3426 * Check the result of the ATTENTION dialog:
3427 * When "Quit" selected, exit Vim.
3428 * When "Recover" selected, recover the file.
3429 */
3430 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003431check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432{
3433 if (swap_exists_action == SEA_QUIT)
3434 getout(1);
3435 handle_swap_exists(NULL);
3436}
3437#endif
3438
Bram Moolenaar08cab962017-03-04 14:37:18 +01003439#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003440
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003441#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003442static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443
3444static struct timeval prev_timeval;
3445
Bram Moolenaar3f269672009-11-03 11:11:11 +00003446# ifdef WIN3264
3447/*
3448 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3449 */
3450 static int
3451gettimeofday(struct timeval *tv, char *dummy)
3452{
3453 long t = clock();
3454 tv->tv_sec = t / CLOCKS_PER_SEC;
3455 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3456 return 0;
3457}
3458# endif
3459
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003460/*
3461 * Save the previous time before doing something that could nest.
3462 * set "*tv_rel" to the time elapsed so far.
3463 */
3464 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003465time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003466{
3467 *((struct timeval *)tv_rel) = prev_timeval;
3468 gettimeofday(&prev_timeval, NULL);
3469 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3470 - ((struct timeval *)tv_rel)->tv_usec;
3471 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3472 - ((struct timeval *)tv_rel)->tv_sec;
3473 if (((struct timeval *)tv_rel)->tv_usec < 0)
3474 {
3475 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3476 --((struct timeval *)tv_rel)->tv_sec;
3477 }
3478 *(struct timeval *)tv_start = prev_timeval;
3479}
3480
3481/*
3482 * Compute the previous time after doing something that could nest.
3483 * Subtract "*tp" from prev_timeval;
3484 * Note: The arguments are (void *) to avoid trouble with systems that don't
3485 * have struct timeval.
3486 */
3487 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003488time_pop(
3489 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490{
3491 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3492 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3493 if (prev_timeval.tv_usec < 0)
3494 {
3495 prev_timeval.tv_usec += 1000000;
3496 --prev_timeval.tv_sec;
3497 }
3498}
3499
3500 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003501time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003502{
3503 long usec;
3504 long msec;
3505
3506 usec = now->tv_usec - then->tv_usec;
3507 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3508 usec = usec % 1000L;
3509 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3510}
3511
3512 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003513time_msg(
3514 char *mesg,
3515 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003516 (struct timeval *) */
3517{
3518 static struct timeval start;
3519 struct timeval now;
3520
3521 if (time_fd != NULL)
3522 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003523 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003524 {
3525 gettimeofday(&start, NULL);
3526 prev_timeval = start;
3527 fprintf(time_fd, "\n\ntimes in msec\n");
3528 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3529 fprintf(time_fd, " clock elapsed: other lines\n\n");
3530 }
3531 gettimeofday(&now, NULL);
3532 time_diff(&start, &now);
3533 if (((struct timeval *)tv_start) != NULL)
3534 {
3535 fprintf(time_fd, " ");
3536 time_diff(((struct timeval *)tv_start), &now);
3537 }
3538 fprintf(time_fd, " ");
3539 time_diff(&prev_timeval, &now);
3540 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003541 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003542 }
3543}
3544
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545#endif
3546
Bram Moolenaar595297d2017-03-04 19:11:12 +01003547#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003548 static void
3549set_progpath(char_u *argv0)
3550{
3551 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003552
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003553# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003554 char buf[PATH_MAX + 1];
3555 ssize_t len;
3556
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003557 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003558 if (len > 0)
3559 {
3560 buf[len] = NUL;
3561 val = (char_u *)buf;
3562 }
3563# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003564 /* A relative path containing a "/" will become invalid when using ":cd",
3565 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003566 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3567 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003568# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003569 char_u *path = NULL;
3570
3571 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3572 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003573# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003574 char_u buf[MAXPATHL];
3575
Bram Moolenaar43663192017-03-05 14:29:12 +01003576 if (!mch_isFullName(argv0))
3577 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003578 if (gettail(argv0) != argv0
3579 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3580 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003581 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003582# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003583# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003584
Bram Moolenaar08cab962017-03-04 14:37:18 +01003585 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003586
Bram Moolenaar066029e2017-03-05 15:19:32 +01003587# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003588 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003589# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003590}
3591
3592#endif /* NO_VIM_MAIN */
3593
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003594#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003595
3596/*
3597 * Common code for the X command server and the Win32 command server.
3598 */
3599
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003600static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003601
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003602/*
3603 * Do the client-server stuff, unless "--servername ''" was used.
3604 */
3605 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003606exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003607{
3608 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3609 {
3610# ifdef WIN32
3611 /* Initialise the client/server messaging infrastructure. */
3612 serverInitMessaging();
3613# endif
3614
3615 /*
3616 * When a command server argument was found, execute it. This may
3617 * exit Vim when it was successful. Otherwise it's executed further
3618 * on. Remember the encoding used here in "serverStrEnc".
3619 */
3620 if (parmp->serverArg)
3621 {
3622 cmdsrv_main(&parmp->argc, parmp->argv,
3623 parmp->serverName_arg, &parmp->serverStr);
3624# ifdef FEAT_MBYTE
3625 parmp->serverStrEnc = vim_strsave(p_enc);
3626# endif
3627 }
3628
3629 /* If we're still running, get the name to register ourselves.
3630 * On Win32 can register right now, for X11 need to setup the
3631 * clipboard first, it's further down. */
3632 parmp->servername = serverMakeName(parmp->serverName_arg,
3633 parmp->argv[0]);
3634# ifdef WIN32
3635 if (parmp->servername != NULL)
3636 {
3637 serverSetName(parmp->servername);
3638 vim_free(parmp->servername);
3639 }
3640# endif
3641 }
3642}
3643
3644/*
3645 * Prepare for running as a Vim server.
3646 */
3647 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003648prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003649{
3650# if defined(FEAT_X11)
3651 /*
3652 * Register for remote command execution with :serversend and --remote
3653 * unless there was a -X or a --servername '' on the command line.
3654 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003655 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003656 */
3657 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3658# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003659 (gui.in_use
3660# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003661 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003662# endif
3663 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003664# endif
3665 parmp->serverName_arg != NULL))
3666 {
3667 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3668 vim_free(parmp->servername);
3669 TIME_MSG("register server name");
3670 }
3671 else
3672 serverDelayedStartName = parmp->servername;
3673# endif
3674
3675 /*
3676 * Execute command ourselves if we're here because the send failed (or
3677 * else we would have exited above).
3678 */
3679 if (parmp->serverStr != NULL)
3680 {
3681 char_u *p;
3682
3683 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3684 parmp->serverStr, &p));
3685 vim_free(p);
3686 }
3687}
3688
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003689 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003690cmdsrv_main(
3691 int *argc,
3692 char **argv,
3693 char_u *serverName_arg,
3694 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003695{
3696 char_u *res;
3697 int i;
3698 char_u *sname;
3699 int ret;
3700 int didone = FALSE;
3701 int exiterr = 0;
3702 char **newArgV = argv + 1;
3703 int newArgC = 1,
3704 Argc = *argc;
3705 int argtype;
3706#define ARGTYPE_OTHER 0
3707#define ARGTYPE_EDIT 1
3708#define ARGTYPE_EDIT_WAIT 2
3709#define ARGTYPE_SEND 3
3710 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003711 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003712# ifndef FEAT_X11
3713 HWND srv;
3714# else
3715 Window srv;
3716
3717 setup_term_clip();
3718# endif
3719
3720 sname = serverMakeName(serverName_arg, argv[0]);
3721 if (sname == NULL)
3722 return;
3723
3724 /*
3725 * Execute the command server related arguments and remove them
3726 * from the argc/argv array; We may have to return into main()
3727 */
3728 for (i = 1; i < Argc; i++)
3729 {
3730 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003731 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003732 {
3733 for (; i < *argc; i++)
3734 {
3735 *newArgV++ = argv[i];
3736 newArgC++;
3737 }
3738 break;
3739 }
3740
Bram Moolenaareb94e552006-03-11 21:35:11 +00003741 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003742 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003743 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3744 {
3745 char *p = argv[i] + 8;
3746
3747 argtype = ARGTYPE_EDIT;
3748 while (*p != NUL)
3749 {
3750 if (STRNICMP(p, "-wait", 5) == 0)
3751 {
3752 argtype = ARGTYPE_EDIT_WAIT;
3753 p += 5;
3754 }
3755 else if (STRNICMP(p, "-silent", 7) == 0)
3756 {
3757 silent = TRUE;
3758 p += 7;
3759 }
3760 else if (STRNICMP(p, "-tab", 4) == 0)
3761 {
3762 tabs = TRUE;
3763 p += 4;
3764 }
3765 else
3766 {
3767 argtype = ARGTYPE_OTHER;
3768 break;
3769 }
3770 }
3771 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003772 else
3773 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003774
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003775 if (argtype != ARGTYPE_OTHER)
3776 {
3777 if (i == *argc - 1)
3778 mainerr_arg_missing((char_u *)argv[i]);
3779 if (argtype == ARGTYPE_SEND)
3780 {
3781 *serverStr = (char_u *)argv[i + 1];
3782 i++;
3783 }
3784 else
3785 {
3786 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003787 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003788 if (*serverStr == NULL)
3789 {
3790 /* Probably out of memory, exit. */
3791 didone = TRUE;
3792 exiterr = 1;
3793 break;
3794 }
3795 Argc = i;
3796 }
3797# ifdef FEAT_X11
3798 if (xterm_dpy == NULL)
3799 {
3800 mch_errmsg(_("No display"));
3801 ret = -1;
3802 }
3803 else
3804 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003805 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003806# else
3807 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003808 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003809# endif
3810 if (ret < 0)
3811 {
3812 if (argtype == ARGTYPE_SEND)
3813 {
3814 /* Failed to send, abort. */
3815 mch_errmsg(_(": Send failed.\n"));
3816 didone = TRUE;
3817 exiterr = 1;
3818 }
3819 else if (!silent)
3820 /* Let vim start normally. */
3821 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3822 break;
3823 }
3824
3825# ifdef FEAT_GUI_W32
3826 /* Guess that when the server name starts with "g" it's a GUI
3827 * server, which we can bring to the foreground here.
3828 * Foreground() in the server doesn't work very well. */
3829 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3830 SetForegroundWindow(srv);
3831# endif
3832
3833 /*
3834 * For --remote-wait: Wait until the server did edit each
3835 * file. Also detect that the server no longer runs.
3836 */
3837 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3838 {
3839 int numFiles = *argc - i - 1;
3840 int j;
3841 char_u *done = alloc(numFiles);
3842 char_u *p;
3843# ifdef FEAT_GUI_W32
3844 NOTIFYICONDATA ni;
3845 int count = 0;
3846 extern HWND message_window;
3847# endif
3848
3849 if (numFiles > 0 && argv[i + 1][0] == '+')
3850 /* Skip "+cmd" argument, don't wait for it to be edited. */
3851 --numFiles;
3852
3853# ifdef FEAT_GUI_W32
3854 ni.cbSize = sizeof(ni);
3855 ni.hWnd = message_window;
3856 ni.uID = 0;
3857 ni.uFlags = NIF_ICON|NIF_TIP;
3858 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3859 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3860 Shell_NotifyIcon(NIM_ADD, &ni);
3861# endif
3862
3863 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003864 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003865 while (memchr(done, 0, numFiles) != NULL)
3866 {
3867# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003868 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003869 if (p == NULL)
3870 break;
3871# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003872 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003873 break;
3874# endif
3875 j = atoi((char *)p);
3876 if (j >= 0 && j < numFiles)
3877 {
3878# ifdef FEAT_GUI_W32
3879 ++count;
3880 sprintf(ni.szTip, _("%d of %d edited"),
3881 count, numFiles);
3882 Shell_NotifyIcon(NIM_MODIFY, &ni);
3883# endif
3884 done[j] = 1;
3885 }
3886 }
3887# ifdef FEAT_GUI_W32
3888 Shell_NotifyIcon(NIM_DELETE, &ni);
3889# endif
3890 }
3891 }
3892 else if (STRICMP(argv[i], "--remote-expr") == 0)
3893 {
3894 if (i == *argc - 1)
3895 mainerr_arg_missing((char_u *)argv[i]);
3896# ifdef WIN32
3897 /* Win32 always works? */
3898 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003899 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003900# else
3901 if (xterm_dpy == NULL)
3902 mch_errmsg(_("No display: Send expression failed.\n"));
3903 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003904 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003905# endif
3906 {
3907 if (res != NULL && *res != NUL)
3908 {
3909 /* Output error from remote */
3910 mch_errmsg((char *)res);
3911 vim_free(res);
3912 res = NULL;
3913 }
3914 mch_errmsg(_(": Send expression failed.\n"));
3915 }
3916 }
3917 else if (STRICMP(argv[i], "--serverlist") == 0)
3918 {
3919# ifdef WIN32
3920 /* Win32 always works? */
3921 res = serverGetVimNames();
3922# else
3923 if (xterm_dpy != NULL)
3924 res = serverGetVimNames(xterm_dpy);
3925# endif
3926 if (called_emsg)
3927 mch_errmsg("\n");
3928 }
3929 else if (STRICMP(argv[i], "--servername") == 0)
3930 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003931 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003932 i++;
3933 continue;
3934 }
3935 else
3936 {
3937 *newArgV++ = argv[i];
3938 newArgC++;
3939 continue;
3940 }
3941 didone = TRUE;
3942 if (res != NULL && *res != NUL)
3943 {
3944 mch_msg((char *)res);
3945 if (res[STRLEN(res) - 1] != '\n')
3946 mch_msg("\n");
3947 }
3948 vim_free(res);
3949 }
3950
3951 if (didone)
3952 {
3953 display_errors(); /* display any collected messages */
3954 exit(exiterr); /* Mission accomplished - get out */
3955 }
3956
3957 /* Return back into main() */
3958 *argc = newArgC;
3959 vim_free(sname);
3960}
3961
3962/*
3963 * Build a ":drop" command to send to a Vim server.
3964 */
3965 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003966build_drop_cmd(
3967 int filec,
3968 char **filev,
3969 int tabs, /* Use ":tab drop" instead of ":drop". */
3970 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003971{
3972 garray_T ga;
3973 int i;
3974 char_u *inicmd = NULL;
3975 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003976 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003977 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003978
3979 if (filec > 0 && filev[0][0] == '+')
3980 {
3981 inicmd = (char_u *)filev[0] + 1;
3982 filev++;
3983 filec--;
3984 }
3985 /* Check if we have at least one argument. */
3986 if (filec <= 0)
3987 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003988
3989 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003990 cwd = alloc(MAXPATHL);
3991 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003992 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003993 if (mch_dirname(cwd, MAXPATHL) != OK)
3994 {
3995 vim_free(cwd);
3996 return NULL;
3997 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003998 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003999#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004000 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004001#else
4002 PATH_ESC_CHARS,
4003#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004004 '\\', TRUE);
4005 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004006 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004007 return NULL;
4008 ga_init2(&ga, 1, 100);
4009 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004010 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004011
4012 /* Call inputsave() so that a prompt for an encryption key works. */
4013 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4014 if (tabs)
4015 ga_concat(&ga, (char_u *)"tab ");
4016 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004017 for (i = 0; i < filec; i++)
4018 {
4019 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004020 * do it again in the Vim server. On MS-Windows only escape
4021 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004022 p = vim_strsave_escaped((char_u *)filev[i],
4023#ifdef UNIX
4024 PATH_ESC_CHARS
4025#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004026 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004027#endif
4028 );
4029 if (p == NULL)
4030 {
4031 vim_free(ga.ga_data);
4032 return NULL;
4033 }
4034 ga_concat(&ga, (char_u *)" ");
4035 ga_concat(&ga, p);
4036 vim_free(p);
4037 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004038 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4039
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004040 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4041 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004042 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4043
4044 /* Switch back to the correct current directory (prior to temporary path
4045 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004046 * correct after the :drop command. With line breaks and spaces:
4047 * if !exists('+acd') || !&acd
4048 * if haslocaldir()
4049 * cd -
4050 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004051 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004052 * cd -
4053 * endif
4054 * endif
4055 */
4056 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004057 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004058 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004059 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004060 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004061
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004062 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004063 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4064 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004065 if (inicmd != NULL)
4066 {
4067 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4068 * the following commands to be inserted as text. Use a "|",
4069 * hopefully "inicmd" does allow this... */
4070 ga_concat(&ga, inicmd);
4071 ga_concat(&ga, (char_u *)"|");
4072 }
4073 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4074 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004075 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004076 ga_append(&ga, NUL);
4077 return ga.ga_data;
4078}
4079
4080/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004081 * Make our basic server name: use the specified "arg" if given, otherwise use
4082 * the tail of the command "cmd" we were started with.
4083 * Return the name in allocated memory. This doesn't include a serial number.
4084 */
4085 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004086serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004087{
4088 char_u *p;
4089
4090 if (arg != NULL && *arg != NUL)
4091 p = vim_strsave_up(arg);
4092 else
4093 {
4094 p = vim_strsave_up(gettail((char_u *)cmd));
4095 /* Remove .exe or .bat from the name. */
4096 if (p != NULL && vim_strchr(p, '.') != NULL)
4097 *vim_strchr(p, '.') = NUL;
4098 }
4099 return p;
4100}
4101#endif /* FEAT_CLIENTSERVER */
4102
4103#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4104/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004105 * Replace termcodes such as <CR> and insert as key presses if there is room.
4106 */
4107 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004108server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004109{
4110 char_u *ptr = NULL;
4111 char_u *cpo_save = p_cpo;
4112
4113 /* Set 'cpoptions' the way we want it.
4114 * B set - backslashes are *not* treated specially
4115 * k set - keycodes are *not* reverse-engineered
4116 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004117 * The last but one parameter of replace_termcodes() is TRUE so that the
4118 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004119 */
4120 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004121 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004122 p_cpo = cpo_save;
4123
4124 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4125 {
4126 /*
4127 * Add the string to the input stream.
4128 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4129 *
4130 * First clear typed characters from the typeahead buffer, there could
4131 * be half a mapping there. Then append to the existing string, so
4132 * that multiple commands from a client are concatenated.
4133 */
4134 if (typebuf.tb_maplen < typebuf.tb_len)
4135 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4136 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4137
4138 /* Let input_available() know we inserted text in the typeahead
4139 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004140 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004141 }
4142 vim_free((char_u *)ptr);
4143}
4144
4145/*
4146 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004147 */
4148 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004149eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004150{
4151 char_u *res;
4152 int save_dbl = debug_break_level;
4153 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004154 void *fc;
4155
4156 /* Evaluate the expression at the toplevel, don't use variables local to
4157 * the calling function. */
4158 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004159
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004160 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4161 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004162 debug_break_level = -1;
4163 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004164 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4165 * to be typed. Do generate errors so that try/catch works. */
4166 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004167
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004168 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004169
4170 debug_break_level = save_dbl;
4171 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004172 --emsg_silent;
4173 if (emsg_silent < 0)
4174 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004175 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004176
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004177 /* A client can tell us to redraw, but not to display the cursor, so do
4178 * that here. */
4179 setcursor();
4180 out_flush();
4181#ifdef FEAT_GUI
4182 if (gui.in_use)
4183 gui_update_cursor(FALSE, FALSE);
4184#endif
4185
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004186 return res;
4187}
4188
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004189/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004190 * Evaluate a command or expression sent to ourselves.
4191 */
4192 int
4193sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4194{
4195 if (asExpr)
4196 {
4197 char_u *ret;
4198
4199 ret = eval_client_expr_to_string(cmd);
4200 if (result != NULL)
4201 {
4202 if (ret == NULL)
4203 {
4204 char *err = _(e_invexprmsg);
4205 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4206 char_u *msg;
4207
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004208 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004209 if (msg != NULL)
4210 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4211 *result = msg;
4212 }
4213 else
4214 *result = ret;
4215 }
4216 else
4217 vim_free(ret);
4218 return ret == NULL ? -1 : 0;
4219 }
4220 server_to_input_buf(cmd);
4221 return 0;
4222}
4223
4224/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004225 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4226 * return an allocated string. Otherwise return "data".
4227 * "*tofree" is set to the result when it needs to be freed later.
4228 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004229 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004230serverConvert(
4231 char_u *client_enc UNUSED,
4232 char_u *data,
4233 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004234{
4235 char_u *res = data;
4236
4237 *tofree = NULL;
4238# ifdef FEAT_MBYTE
4239 if (client_enc != NULL && p_enc != NULL)
4240 {
4241 vimconv_T vimconv;
4242
4243 vimconv.vc_type = CONV_NONE;
4244 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4245 && vimconv.vc_type != CONV_NONE)
4246 {
4247 res = string_convert(&vimconv, data, NULL);
4248 if (res == NULL)
4249 res = data;
4250 else
4251 *tofree = res;
4252 }
4253 convert_setup(&vimconv, NULL, NULL);
4254 }
4255# endif
4256 return res;
4257}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004258#endif