blob: 81eb1a6799391309fe08e0432bd3c3661f8d1618 [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. */
Bram Moolenaarc4da1132017-07-15 19:39:43 +0200436 if (params.use_vimrc != NULL
437 && (STRCMP(params.use_vimrc, "NONE") == 0
438 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
Bram Moolenaar66459b72016-08-06 19:01:55 +0200439 p_lpl = FALSE;
440
Bram Moolenaar58d98232005-07-23 22:25:46 +0000441 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000442 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000443
Bram Moolenaar58d98232005-07-23 22:25:46 +0000444 /* Source startup scripts. */
445 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000446
447#ifdef FEAT_EVAL
448 /*
449 * Read all the plugin files.
450 * Only when compiled with +eval, since most plugins need it.
451 */
452 if (p_lpl)
453 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200454 char_u *rtp_copy = NULL;
455
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200456 /* First add all package directories to 'runtimepath', so that their
457 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200458 * :packloadall command.
459 * Make a copy of 'runtimepath', so that source_runtime does not use
460 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200461 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462 {
463 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200464 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200465 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200466
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200467 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000468# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200469 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000470# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200471 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000472# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200473 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000474 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200475 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100476
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200477 /* Only source "start" packages if not done already with a :packloadall
478 * command. */
479 if (!did_source_packages)
480 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100481 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200482
483# ifdef VMS /* Somehow VMS doesn't handle the "**". */
484 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
485# else
486 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
487# endif
488 TIME_MSG("loading after plugins");
489
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000490 }
491#endif
492
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000493#ifdef FEAT_DIFF
494 /* Decide about window layout for diff mode after reading vimrc. */
495 if (params.diff_mode && params.window_layout == 0)
496 {
497 if (diffopt_horizontal())
498 params.window_layout = WIN_HOR; /* use horizontal split */
499 else
500 params.window_layout = WIN_VER; /* use vertical split */
501 }
502#endif
503
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000504 /*
505 * Recovery mode without a file name: List swap files.
506 * This uses the 'dir' option, therefore it must be after the
507 * initializations.
508 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200509 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200511 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000512 mch_exit(0);
513 }
514
515 /*
516 * Set a few option defaults after reading .vimrc files:
517 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
518 */
519 set_init_3();
520 TIME_MSG("inits 3");
521
522 /*
523 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
524 * Note that this overrides anything from a vimrc file.
525 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000526 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527 p_uc = 0;
528
529#ifdef FEAT_FKMAP
530 if (curwin->w_p_rl && p_altkeymap)
531 {
532 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
533# ifdef FEAT_ARABIC
534 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
535# endif
536 p_fkmap = TRUE; /* Set the Farsi keymap mode */
537 }
538#endif
539
540#ifdef FEAT_GUI
541 if (gui.starting)
542 {
543#if defined(UNIX) || defined(VMS)
544 /* When something caused a message from a vimrc script, need to output
545 * an extra newline before the shell prompt. */
546 if (did_emsg || msg_didout)
547 putchar('\n');
548#endif
549
550 gui_start(); /* will set full_screen to TRUE */
551 TIME_MSG("starting GUI");
552
553 /* When running "evim" or "gvim -y" we need the menus, exit if we
554 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000555 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000556 mch_exit(1);
557 }
558#endif
559
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000560#ifdef FEAT_VIMINFO
561 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000562 * Read in registers, history etc, but not marks, from the viminfo file.
563 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564 */
565 if (*p_viminfo != NUL)
566 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000567 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000568 TIME_MSG("reading viminfo");
569 }
570#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100571#ifdef FEAT_EVAL
572 /* It's better to make v:oldfiles an empty list than NULL. */
573 if (get_vim_var_list(VV_OLDFILES) == NULL)
574 set_vim_var_list(VV_OLDFILES, list_alloc());
575#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000576
577#ifdef FEAT_QUICKFIX
578 /*
579 * "-q errorfile": Load the error file now.
580 * If the error file can't be read, exit before doing anything else.
581 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000582 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000583 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100584 char_u *enc = NULL;
585
586# ifdef FEAT_MBYTE
587 enc = p_menc;
588# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000589 if (params.use_ef != NULL)
590 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000591 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200592 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100593 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000594 {
595 out_char('\n');
596 mch_exit(3);
597 }
598 TIME_MSG("reading errorfile");
599 }
600#endif
601
602 /*
603 * Start putting things on the screen.
604 * Scroll screen down before drawing over it
605 * Clear screen now, so file message will not be cleared.
606 */
607 starting = NO_BUFFERS;
608 no_wait_return = FALSE;
609 if (!exmode_active)
610 msg_scroll = FALSE;
611
612#ifdef FEAT_GUI
613 /*
614 * This seems to be required to make callbacks to be called now, instead
615 * of after things have been put on the screen, which then may be deleted
616 * when getting a resize callback.
617 * For the Mac this handles putting files dropped on the Vim icon to
618 * global_alist.
619 */
620 if (gui.in_use)
621 {
622# ifdef FEAT_SUN_WORKSHOP
623 if (!usingSunWorkShop)
624# endif
625 gui_wait_for_chars(50L);
626 TIME_MSG("GUI delay");
627 }
628#endif
629
630#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
631 qnx_clip_init();
632#endif
633
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200634#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
635 clip_init(TRUE);
636#endif
637
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000638#ifdef FEAT_XCLIPBOARD
639 /* Start using the X clipboard, unless the GUI was started. */
640# ifdef FEAT_GUI
641 if (!gui.in_use)
642# endif
643 {
644 setup_term_clip();
645 TIME_MSG("setup clipboard");
646 }
647#endif
648
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000649#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000650 /* Prepare for being a Vim server. */
651 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000652#endif
653
654 /*
655 * If "-" argument given: Read file from stdin.
656 * Do this before starting Raw mode, because it may change things that the
657 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
658 * are the same terminal: "cat | vim -".
659 * Using autocommands here may cause trouble...
660 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000661 if (params.edit_type == EDIT_STDIN && !recoverymode)
662 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000663
664#if defined(UNIX) || defined(VMS)
665 /* When switching screens and something caused a message from a vimrc
666 * script, need to output an extra newline on exit. */
667 if ((did_emsg || msg_didout) && *T_TI != NUL)
668 newline_on_exit = TRUE;
669#endif
670
671 /*
672 * When done something that is not allowed or error message call
673 * wait_return. This must be done before starttermcap(), because it may
674 * switch to another screen. It must be done after settmode(TMODE_RAW),
675 * because we want to react on a single key stroke.
676 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000677 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000678 */
679 settmode(TMODE_RAW);
680 TIME_MSG("setting raw mode");
681
682 if (need_wait_return || msg_didany)
683 {
684 wait_return(TRUE);
685 TIME_MSG("waiting for return");
686 }
687
688 starttermcap(); /* start termcap if not done by wait_return() */
689 TIME_MSG("start termcap");
690
691#ifdef FEAT_MOUSE
692 setmouse(); /* may start using the mouse */
693#endif
694 if (scroll_region)
695 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000696 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000697
698 /*
699 * Don't clear the screen when starting in Ex mode, unless using the GUI.
700 */
701 if (exmode_active
702#ifdef FEAT_GUI
703 && !gui.in_use
704#endif
705 )
706 must_redraw = CLEAR;
707 else
708 {
709 screenclear(); /* clear screen */
710 TIME_MSG("clearing screen");
711 }
712
713#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000714 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100716 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200717 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 TIME_MSG("getting crypt key");
719 }
720#endif
721
722 no_wait_return = TRUE;
723
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000725 * Create the requested number of windows and edit buffers in them.
726 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000727 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000728 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729 TIME_MSG("opening buffers");
730
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000731#ifdef FEAT_EVAL
732 /* clear v:swapcommand */
733 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
734#endif
735
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 /* Ex starts at last line of the file */
737 if (exmode_active)
738 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
739
740#ifdef FEAT_AUTOCMD
741 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
742 TIME_MSG("BufEnter autocommands");
743#endif
744 setpcmark();
745
746#ifdef FEAT_QUICKFIX
747 /*
748 * When started with "-q errorfile" jump to first error now.
749 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000750 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000751 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000752 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753 TIME_MSG("jump to first error");
754 }
755#endif
756
757#ifdef FEAT_WINDOWS
758 /*
759 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000760 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000761 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200762 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000763#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200764 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000765
766#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000767 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 {
769 win_T *wp;
770
771 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200772 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000773 diff_win_options(wp, TRUE);
774 }
775#endif
776
777 /*
778 * Shorten any of the filenames, but only when absolute.
779 */
780 shorten_fnames(FALSE);
781
782 /*
783 * Need to jump to the tag before executing the '-c command'.
784 * Makes "vim -c '/return' -t main" work.
785 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000786 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000787 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000788#if defined(HAS_SWAP_EXISTS_ACTION)
789 swap_exists_did_quit = FALSE;
790#endif
791
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000792 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000793 do_cmdline_cmd(IObuff);
794 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000795
796#if defined(HAS_SWAP_EXISTS_ACTION)
797 /* If the user doesn't want to edit the file then we quit here. */
798 if (swap_exists_did_quit)
799 getout(1);
800#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801 }
802
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000803 /* Execute any "+", "-c" and "-S" arguments. */
804 if (params.n_commands > 0)
805 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200807 /* Must come before the may_req_ calls. */
808 starting = 0;
809
Bram Moolenaar976787d2017-06-04 15:45:50 +0200810#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
811 /* Must be done before redrawing, puts a few characters on the screen. */
812 may_req_ambiguous_char_width();
813#endif
814
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000815 RedrawingDisabled = 0;
816 redraw_all_later(NOT_VALID);
817 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000818
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200819 /* 'autochdir' has been postponed */
820 DO_AUTOCHDIR
821
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000822#ifdef FEAT_TERMRESPONSE
823 /* Requesting the termresponse is postponed until here, so that a "-c q"
824 * argument doesn't make it appear in the shell Vim was started from. */
825 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200826
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200827 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000828#endif
829
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000830 /* start in insert mode */
831 if (p_im)
832 need_start_insertmode = TRUE;
833
Bram Moolenaar14735512016-03-26 21:00:08 +0100834#ifdef FEAT_EVAL
835 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
836#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000837#ifdef FEAT_AUTOCMD
838 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
839 TIME_MSG("VimEnter autocommands");
840#endif
841
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200842#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
843 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
844 * done after the clipboard is available and all initial commands that may
845 * modify the 'clipboard' setting have run; i.e. just before entering the
846 * main loop. */
847 {
848 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100849
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200850 adjust_clip_reg(&default_regname);
851 set_reg_var(default_regname);
852 }
853#endif
854
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000855#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
856 /* When a startup script or session file setup for diff'ing and
857 * scrollbind, sync the scrollbind now. */
858 if (curwin->w_p_diff && curwin->w_p_scb)
859 {
860 update_topline();
861 check_scrollbind((linenr_T)0, 0L);
862 TIME_MSG("diff scrollbinding");
863 }
864#endif
865
866#if defined(WIN3264) && !defined(FEAT_GUI_W32)
867 mch_set_winsize_now(); /* Allow winsize changes from now on */
868#endif
869
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000870#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
871 /* When tab pages were created, may need to update the tab pages line and
872 * scrollbars. This is skipped while creating them. */
873 if (first_tabpage->tp_next != NULL)
874 {
875 out_flush();
876 gui_init_which_components(NULL);
877 gui_update_scrollbars(TRUE);
878 }
879 need_mouse_correct = TRUE;
880#endif
881
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882 /* If ":startinsert" command used, stuff a dummy command to be able to
883 * call normal_cmd(), which will then start Insert mode. */
884 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000885 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886
887#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200888 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200889 {
890# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200891# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200892 && !defined(FEAT_GUI_W32)
893 if (gui.in_use)
894 {
895 mch_errmsg(_("netbeans is not supported with this GUI\n"));
896 mch_exit(2);
897 }
898# endif
899# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000900 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200901 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200902 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000903#endif
904
905 TIME_MSG("before starting main loop");
906
907 /*
908 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100909 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000910 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000911
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200912#endif /* NO_VIM_MAIN */
913
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000914 return 0;
915}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000916
917/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200918 * Initialisation shared by main() and some tests.
919 */
920 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200921common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200922{
923
924#ifdef FEAT_MBYTE
925 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
926#endif
927#ifdef FEAT_EVAL
928 eval_init(); /* init global variables */
929#endif
930
931#ifdef __QNXNTO__
932 qnx_init(); /* PhAttach() for clipboard, (and gui) */
933#endif
934
935#ifdef MAC_OS_CLASSIC
936 /* Prepare for possibly starting GUI sometime */
937 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200938 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200939 TIME_MSG("GUI prepared");
940#endif
941
942 /* Init the table of Normal mode commands. */
943 init_normal_cmds();
944
945#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
946 make_version(); /* Construct the long version string. */
947#endif
948
949 /*
950 * Allocate space for the generic buffers (needed for set_init_1() and
951 * EMSG2()).
952 */
953 if ((IObuff = alloc(IOSIZE)) == NULL
954 || (NameBuff = alloc(MAXPATHL)) == NULL)
955 mch_exit(0);
956 TIME_MSG("Allocated generic buffers");
957
958#ifdef NBDEBUG
959 /* Wait a moment for debugging NetBeans. Must be after allocating
960 * NameBuff. */
961 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
962 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
963 TIME_MSG("NetBeans debug wait");
964#endif
965
966#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
967 /*
968 * Setup to use the current locale (for ctype() and many other things).
969 * NOTE: Translated messages with encodings other than latin1 will not
970 * work until set_init_1() has been called!
971 */
972 init_locale();
973 TIME_MSG("locale set");
974#endif
975
976#ifdef FEAT_GUI
977 gui.dofork = TRUE; /* default is to use fork() */
978#endif
979
980 /*
981 * Do a first scan of the arguments in "argv[]":
982 * -display or --display
983 * --server...
984 * --socketid
985 * --windowid
986 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200987 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200988
989#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200990 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200991#endif
992#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
993 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200994 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200995 TIME_MSG("GUI prepared");
996#endif
997
998#ifdef FEAT_CLIPBOARD
999 clip_init(FALSE); /* Initialise clipboard stuff */
1000 TIME_MSG("clipboard setup");
1001#endif
1002
1003 /*
1004 * Check if we have an interactive window.
1005 * On the Amiga: If there is no window, we open one with a newcli command
1006 * (needed for :! to * work). mch_check_win() will also handle the -d or
1007 * -dev argument.
1008 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001009 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001010 TIME_MSG("window checked");
1011
1012 /*
1013 * Allocate the first window and buffer.
1014 * Can't do anything without it, exit when it fails.
1015 */
1016 if (win_alloc_first() == FAIL)
1017 mch_exit(0);
1018
1019 init_yank(); /* init yank buffers */
1020
1021 alist_init(&global_alist); /* Init the argument list to empty. */
1022 global_alist.id = 0;
1023
1024 /*
1025 * Set the default values for the options.
1026 * NOTE: Non-latin1 translated messages are working only after this,
1027 * because this is where "has_mbyte" will be set, which is used by
1028 * msg_outtrans_len_attr().
1029 * First find out the home directory, needed to expand "~" in options.
1030 */
1031 init_homedir(); /* find real value of $HOME */
1032 set_init_1();
1033 TIME_MSG("inits 1");
1034
1035#ifdef FEAT_EVAL
1036 set_lang_var(); /* set v:lang and v:ctype */
1037#endif
1038}
1039
1040/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001041 * Return TRUE when the --not-a-term argument was found.
1042 */
1043 int
1044is_not_a_term()
1045{
1046 return params.not_a_term;
1047}
1048
1049/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001050 * Main loop: Execute Normal mode commands until exiting Vim.
1051 * Also used to handle commands in the command-line window, until the window
1052 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 * Also used to handle ":visual" command after ":global": execute Normal mode
1054 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001055 */
1056 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001057main_loop(
1058 int cmdwin, /* TRUE when working in the command-line window */
1059 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001060{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001061 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001062 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001063#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001064 /* these are static to avoid a compiler warning */
1065 static linenr_T conceal_old_cursor_line = 0;
1066 static linenr_T conceal_new_cursor_line = 0;
1067 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001068#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001069
1070#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1071 /* Setup to catch a terminating error from the X server. Just ignore
1072 * it, restore the state and continue. This might not always work
1073 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001074 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001075 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001076 {
1077 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001078 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001079 got_int = TRUE;
1080 need_wait_return = FALSE;
1081 global_busy = FALSE;
1082 exmode_active = 0;
1083 skip_redraw = FALSE;
1084 RedrawingDisabled = 0;
1085 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001086 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001087# ifdef FEAT_EVAL
1088 emsg_skip = 0;
1089# endif
1090 emsg_off = 0;
1091# ifdef FEAT_MOUSE
1092 setmouse();
1093# endif
1094 settmode(TMODE_RAW);
1095 starttermcap();
1096 scroll_start();
1097 redraw_later_clear();
1098 }
1099#endif
1100
1101 clear_oparg(&oa);
1102 while (!cmdwin
1103#ifdef FEAT_CMDWIN
1104 || cmdwin_result == 0
1105#endif
1106 )
1107 {
1108 if (stuff_empty())
1109 {
1110 did_check_timestamps = FALSE;
1111 if (need_check_timestamps)
1112 check_timestamps(FALSE);
1113 if (need_wait_return) /* if wait_return still needed ... */
1114 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001115 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001116 {
1117 need_start_insertmode = FALSE;
1118 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1119 /* skip the fileinfo message now, because it would be shown
1120 * after insert mode finishes! */
1121 need_fileinfo = FALSE;
1122 }
1123 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001124
1125 /* Reset "got_int" now that we got back to the main loop. Except when
1126 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1127 * the ":g" command.
1128 * For ":g/pat/vi" we reset "got_int" when used once. When used
1129 * a second time we go back to Ex mode and abort the ":g" command. */
1130 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001131 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001132 if (noexmode && global_busy && !exmode_active && previous_got_int)
1133 {
1134 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1135 * used and keep "got_int" set, so that it aborts ":g". */
1136 exmode_active = EXMODE_NORMAL;
1137 State = NORMAL;
1138 }
1139 else if (!global_busy || !exmode_active)
1140 {
1141 if (!quit_more)
1142 (void)vgetc(); /* flush all buffers */
1143 got_int = FALSE;
1144 }
1145 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001146 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001147 else
1148 previous_got_int = FALSE;
1149
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001150 if (!exmode_active)
1151 msg_scroll = FALSE;
1152 quit_more = FALSE;
1153
1154 /*
1155 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1156 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1157 * update cursor and redraw.
1158 */
1159 if (skip_redraw || exmode_active)
1160 skip_redraw = FALSE;
1161 else if (do_redraw || stuff_empty())
1162 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001163# ifdef FEAT_GUI
1164 /* If ui_breakcheck() was used a resize may have been postponed. */
1165 gui_may_resize_shell();
1166# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001168 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001169 if (!finish_op && (
1170# ifdef FEAT_AUTOCMD
1171 has_cursormoved()
1172# endif
1173# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1174 ||
1175# endif
1176# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001177 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001178# endif
1179 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001180# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001181 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001182# endif
1183 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001184 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001185# ifdef FEAT_AUTOCMD
1186 if (has_cursormoved())
1187 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1188 FALSE, curbuf);
1189# endif
1190# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001191 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001192 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001193# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001194 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001195# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001196 conceal_new_cursor_line = curwin->w_cursor.lnum;
1197 conceal_update_lines = TRUE;
1198 }
1199# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001200# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001201 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001202# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001203 }
1204#endif
1205
Bram Moolenaar186628f2013-03-19 13:33:23 +01001206#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001207 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001208 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001209 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001210 {
1211 if (last_changedtick_buf == curbuf)
1212 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1213 FALSE, curbuf);
1214 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001215 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001216 }
1217#endif
1218
Bram Moolenaar33aec762006-01-22 23:30:12 +00001219#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1220 /* Scroll-binding for diff mode may have been postponed until
1221 * here. Avoids doing it for every change. */
1222 if (diff_need_scrollbind)
1223 {
1224 check_scrollbind((linenr_T)0, 0L);
1225 diff_need_scrollbind = FALSE;
1226 }
1227#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001228#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 /* Include a closed fold completely in the Visual area. */
1230 foldAdjustVisual();
1231#endif
1232#ifdef FEAT_FOLDING
1233 /*
1234 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1235 * contain the cursor.
1236 * When 'foldopen' is "all", open the fold(s) under the cursor.
1237 * This may mark the window for redrawing.
1238 */
1239 if (hasAnyFolding(curwin) && !char_avail())
1240 {
1241 foldCheckClose();
1242 if (fdo_flags & FDO_ALL)
1243 foldOpenCursor();
1244 }
1245#endif
1246
1247 /*
1248 * Before redrawing, make sure w_topline is correct, and w_leftcol
1249 * if lines don't wrap, and w_skipcol if lines wrap.
1250 */
1251 update_topline();
1252 validate_cursor();
1253
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001254 if (VIsual_active)
1255 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001256 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001257 update_screen(0);
1258 else if (redraw_cmdline || clear_cmdline)
1259 showmode();
1260#ifdef FEAT_WINDOWS
1261 redraw_statuslines();
1262#endif
1263#ifdef FEAT_TITLE
1264 if (need_maketitle)
1265 maketitle();
1266#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001267#ifdef FEAT_VIMINFO
1268 curbuf->b_last_used = vim_time();
1269#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001270 /* display message after redraw */
1271 if (keep_msg != NULL)
1272 {
1273 char_u *p;
1274
1275 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001276 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1277 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001278 p = keep_msg;
1279 msg_attr(p, keep_msg_attr);
1280 vim_free(p);
1281 }
1282 if (need_fileinfo) /* show file info after redraw */
1283 {
1284 fileinfo(FALSE, TRUE, FALSE);
1285 need_fileinfo = FALSE;
1286 }
1287
1288 emsg_on_display = FALSE; /* can delete error message now */
1289 did_emsg = FALSE;
1290 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001291 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001292 showruler(FALSE);
1293
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001294# if defined(FEAT_CONCEAL)
1295 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001296 && (conceal_old_cursor_line != conceal_new_cursor_line
1297 || conceal_cursor_line(curwin)
1298 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001299 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001300 if (conceal_old_cursor_line != conceal_new_cursor_line
1301 && conceal_old_cursor_line
1302 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001303 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001304 update_single_line(curwin, conceal_new_cursor_line);
1305 curwin->w_valid &= ~VALID_CROW;
1306 }
1307# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001308 setcursor();
1309 cursor_on();
1310
1311 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001312
1313#ifdef STARTUPTIME
1314 /* Now that we have drawn the first screen all the startup stuff
1315 * has been done, close any file for startup messages. */
1316 if (time_fd != NULL)
1317 {
1318 TIME_MSG("first screen update");
1319 TIME_MSG("--- VIM STARTED ---");
1320 fclose(time_fd);
1321 time_fd = NULL;
1322 }
1323#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001324 }
1325#ifdef FEAT_GUI
1326 if (need_mouse_correct)
1327 gui_mouse_correct();
1328#endif
1329
1330 /*
1331 * Update w_curswant if w_set_curswant has been set.
1332 * Postponed until here to avoid computing w_virtcol too often.
1333 */
1334 update_curswant();
1335
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001336#ifdef FEAT_EVAL
1337 /*
1338 * May perform garbage collection when waiting for a character, but
1339 * only at the very toplevel. Otherwise we may be using a List or
1340 * Dict internally somewhere.
1341 * "may_garbage_collect" is reset in vgetc() which is invoked through
1342 * do_exmode() and normal_cmd().
1343 */
1344 may_garbage_collect = (!cmdwin && !noexmode);
1345#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001346 /*
1347 * If we're invoked as ex, do a round of ex commands.
1348 * Otherwise, get and execute a normal mode command.
1349 */
1350 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001351 {
1352 if (noexmode) /* End of ":global/path/visual" commands */
1353 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001354 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001355 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001356 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001357 {
1358#ifdef FEAT_TERMINAL
Bram Moolenaar423802d2017-07-30 16:52:24 +02001359 if (term_use_loop() && oa.op_type == OP_NOP && oa.regname == NUL)
1360 {
1361 /* If terminal_loop() returns OK we got a key that is handled
1362 * in Normal model. With FAIL the terminal was closed and the
1363 * screen needs to be redrawn. */
1364 if (terminal_loop() == OK)
1365 normal_cmd(&oa, TRUE);
1366 }
1367 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001368#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001369 normal_cmd(&oa, TRUE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001370 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001371 }
1372}
1373
1374
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001375#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001376/*
1377 * Exit, but leave behind swap files for modified buffers.
1378 */
1379 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001380getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001381{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001382# if defined(SIGHUP) && defined(SIG_IGN)
1383 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1384 * makes Vim exit and then handling SIGHUP causes various reentrance
1385 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001386 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001387# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001388
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001389 ml_close_notmod(); /* close all not-modified buffers */
1390 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1391 ml_close_all(FALSE); /* close all memfiles, without deleting */
1392 getout(exitval); /* exit Vim properly */
1393}
1394#endif
1395
1396
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001397/*
1398 * Exit properly.
1399 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001400 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001401getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001402{
1403#ifdef FEAT_AUTOCMD
1404 buf_T *buf;
1405 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001406 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001407#endif
1408
1409 exiting = TRUE;
1410
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001411 /* When running in Ex mode an error causes us to exit with a non-zero exit
1412 * code. POSIX requires this, although it's not 100% clear from the
1413 * standard. */
1414 if (exmode_active)
1415 exitval += ex_exitval;
1416
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001417 /* Position the cursor on the last screen line, below all the text */
1418#ifdef FEAT_GUI
1419 if (!gui.in_use)
1420#endif
1421 windgoto((int)Rows - 1, 0);
1422
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001423#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1424 /* Optionally print hashtable efficiency. */
1425 hash_debug_results();
1426#endif
1427
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001428#ifdef FEAT_GUI
1429 msg_didany = FALSE;
1430#endif
1431
1432#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001434 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001435 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1436# if defined FEAT_WINDOWS
1437 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001438 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001440 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001441 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001442 if (wp->w_buffer == NULL)
1443 /* Autocmd must have close the buffer already, skip. */
1444 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001445 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001446 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001447 {
1448 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1449 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001450 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001451 /* start all over, autocommands may mess up the lists */
1452 next_tp = first_tabpage;
1453 break;
1454 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001455 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001456 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001457# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001458 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1459 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001460# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001461
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001462 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001463 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001464 if (buf->b_ml.ml_mfp != NULL)
1465 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001466 bufref_T bufref;
1467
1468 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001469 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001470 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001471 if (!bufref_valid(&bufref))
1472 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001473 break;
1474 }
1475 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1476 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001477#endif
1478
1479#ifdef FEAT_VIMINFO
1480 if (*p_viminfo != NUL)
1481 /* Write out the registers, history, marks etc, to the viminfo file */
1482 write_viminfo(NULL, FALSE);
1483#endif
1484
1485#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001486 if (get_vim_var_nr(VV_DYING) <= 1)
1487 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001488#endif
1489
Bram Moolenaar05159a02005-02-26 23:04:13 +00001490#ifdef FEAT_PROFILE
1491 profile_dump();
1492#endif
1493
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001494 if (did_emsg
1495#ifdef FEAT_GUI
1496 || (gui.in_use && msg_didany && p_verbose > 0)
1497#endif
1498 )
1499 {
1500 /* give the user a chance to read the (error) message */
1501 no_wait_return = FALSE;
1502 wait_return(FALSE);
1503 }
1504
1505#ifdef FEAT_AUTOCMD
1506 /* Position the cursor again, the autocommands may have moved it */
1507# ifdef FEAT_GUI
1508 if (!gui.in_use)
1509# endif
1510 windgoto((int)Rows - 1, 0);
1511#endif
1512
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001513#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001514 job_stop_on_exit();
1515#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001516#ifdef FEAT_LUA
1517 lua_end();
1518#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001519#ifdef FEAT_MZSCHEME
1520 mzscheme_end();
1521#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001522#ifdef FEAT_TCL
1523 tcl_end();
1524#endif
1525#ifdef FEAT_RUBY
1526 ruby_end();
1527#endif
1528#ifdef FEAT_PYTHON
1529 python_end();
1530#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001531#ifdef FEAT_PYTHON3
1532 python3_end();
1533#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001534#ifdef FEAT_PERL
1535 perl_end();
1536#endif
1537#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1538 iconv_end();
1539#endif
1540#ifdef FEAT_NETBEANS_INTG
1541 netbeans_end();
1542#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001543#ifdef FEAT_CSCOPE
1544 cs_end();
1545#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001546#ifdef FEAT_EVAL
1547 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001548 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001549#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001550#if defined(WIN32) && defined(FEAT_MBYTE)
1551 free_cmd_argsW();
1552#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001553
1554 mch_exit(exitval);
1555}
1556
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001557#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1558/*
1559 * Setup to use the current locale (for ctype() and many other things).
1560 */
1561 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001562init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001563{
1564 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001565
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001566# ifdef FEAT_GUI_GTK
1567 /* Tell Gtk not to change our locale settings. */
1568 gtk_disable_setlocale();
1569# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001570# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1571 /* Make sure strtod() uses a decimal point, not a comma. */
1572 setlocale(LC_NUMERIC, "C");
1573# endif
1574
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001575# ifdef WIN32
1576 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1577 * text while it's expecting text in the current locale. This call avoids
1578 * that. */
1579 setlocale(LC_CTYPE, "C");
1580# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001581
1582# ifdef FEAT_GETTEXT
1583 {
1584 int mustfree = FALSE;
1585 char_u *p;
1586
1587# ifdef DYNAMIC_GETTEXT
1588 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001589 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001590# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001591 /* expand_env() doesn't work yet, because g_chartab[] is not
1592 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001593 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1594 if (p != NULL && *p != NUL)
1595 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001596 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001597 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1598 }
1599 if (mustfree)
1600 vim_free(p);
1601 textdomain(VIMPACKAGE);
1602 }
1603# endif
1604}
1605#endif
1606
1607/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001608 * Get the name of the display, before gui_prepare() removes it from
1609 * argv[]. Used for the xterm-clipboard display.
1610 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001611 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001612 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001613 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001614early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001615{
Bram Moolenaar03005972008-11-20 13:12:36 +00001616#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1617 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001618 int argc = parmp->argc;
1619 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001620 int i;
1621
1622 for (i = 1; i < argc; i++)
1623 {
1624 if (STRCMP(argv[i], "--") == 0)
1625 break;
1626# ifdef FEAT_XCLIPBOARD
1627 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001628# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001629 || STRICMP(argv[i], "--display") == 0
1630# endif
1631 )
1632 {
1633 if (i == argc - 1)
1634 mainerr_arg_missing((char_u *)argv[i]);
1635 xterm_display = argv[++i];
1636 }
1637# endif
1638# ifdef FEAT_CLIENTSERVER
1639 else if (STRICMP(argv[i], "--servername") == 0)
1640 {
1641 if (i == argc - 1)
1642 mainerr_arg_missing((char_u *)argv[i]);
1643 parmp->serverName_arg = (char_u *)argv[++i];
1644 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001645 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001646 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001647 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001648 {
1649 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001650# ifdef FEAT_GUI
1651 if (strstr(argv[i], "-wait") != 0)
1652 /* don't fork() when starting the GUI to edit files ourself */
1653 gui.dofork = FALSE;
1654# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001655 }
1656# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001657
1658# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1659# ifdef FEAT_GUI_W32
1660 else if (STRICMP(argv[i], "--windowid") == 0)
1661# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001662 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001663# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001664 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001665 long_u id;
1666 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001667
1668 if (i == argc - 1)
1669 mainerr_arg_missing((char_u *)argv[i]);
1670 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001671 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001672 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001673 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001674 if (count != 1)
1675 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1676 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001677# ifdef FEAT_GUI_W32
1678 win_socket_id = id;
1679# else
1680 gtk_socket_id = id;
1681# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001682 i++;
1683 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001684# endif
1685# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001686 else if (STRICMP(argv[i], "--echo-wid") == 0)
1687 echo_wid_arg = TRUE;
1688# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001689# ifndef FEAT_NETBEANS_INTG
1690 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001691 {
1692 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1693 mch_exit(2);
1694 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001695# endif
1696
Bram Moolenaar58d98232005-07-23 22:25:46 +00001697 }
1698#endif
1699}
1700
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001701#ifndef NO_VIM_MAIN
1702/*
1703 * Get a (optional) count for a Vim argument.
1704 */
1705 static int
1706get_number_arg(
1707 char_u *p, /* pointer to argument */
1708 int *idx, /* index in argument, is incremented */
1709 int def) /* default value */
1710{
1711 if (vim_isdigit(p[*idx]))
1712 {
1713 def = atoi((char *)&(p[*idx]));
1714 while (vim_isdigit(p[*idx]))
1715 *idx = *idx + 1;
1716 }
1717 return def;
1718}
1719
1720/*
1721 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1722 * If the executable name starts with "r" we disable shell commands.
1723 * If the next character is "e" we run in Easy mode.
1724 * If the next character is "g" we run the GUI version.
1725 * If the next characters are "view" we start in readonly mode.
1726 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1727 * If the next characters are "ex" we start in Ex mode. If it's followed
1728 * by "im" use improved Ex mode.
1729 */
1730 static void
1731parse_command_name(mparm_T *parmp)
1732{
1733 char_u *initstr;
1734
1735 initstr = gettail((char_u *)parmp->argv[0]);
1736
1737#ifdef MACOS_X_UNIX
1738 /* An issue has been seen when launching Vim in such a way that
1739 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1740 * executable or a symbolic link of it. Until this issue is resolved
1741 * we prohibit the GUI from being used.
1742 */
1743 if (STRCMP(initstr, parmp->argv[0]) == 0)
1744 disallow_gui = TRUE;
1745
1746 /* TODO: On MacOS X default to gui if argv[0] ends in:
1747 * /Vim.app/Contents/MacOS/Vim */
1748#endif
1749
1750#ifdef FEAT_EVAL
1751 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001752 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001753#endif
1754
1755 if (TOLOWER_ASC(initstr[0]) == 'r')
1756 {
1757 restricted = TRUE;
1758 ++initstr;
1759 }
1760
1761 /* Use evim mode for "evim" and "egvim", not for "editor". */
1762 if (TOLOWER_ASC(initstr[0]) == 'e'
1763 && (TOLOWER_ASC(initstr[1]) == 'v'
1764 || TOLOWER_ASC(initstr[1]) == 'g'))
1765 {
1766#ifdef FEAT_GUI
1767 gui.starting = TRUE;
1768#endif
1769 parmp->evim_mode = TRUE;
1770 ++initstr;
1771 }
1772
1773 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1774 if (TOLOWER_ASC(initstr[0]) == 'g')
1775 {
1776 main_start_gui();
1777#ifdef FEAT_GUI
1778 ++initstr;
1779#endif
1780 }
1781
1782 if (STRNICMP(initstr, "view", 4) == 0)
1783 {
1784 readonlymode = TRUE;
1785 curbuf->b_p_ro = TRUE;
1786 p_uc = 10000; /* don't update very often */
1787 initstr += 4;
1788 }
1789 else if (STRNICMP(initstr, "vim", 3) == 0)
1790 initstr += 3;
1791
1792 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1793 if (STRICMP(initstr, "diff") == 0)
1794 {
1795#ifdef FEAT_DIFF
1796 parmp->diff_mode = TRUE;
1797#else
1798 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1799 mch_errmsg("\n");
1800 mch_exit(2);
1801#endif
1802 }
1803
1804 if (STRNICMP(initstr, "ex", 2) == 0)
1805 {
1806 if (STRNICMP(initstr + 2, "im", 2) == 0)
1807 exmode_active = EXMODE_VIM;
1808 else
1809 exmode_active = EXMODE_NORMAL;
1810 change_compatible(TRUE); /* set 'compatible' */
1811 }
1812}
1813
Bram Moolenaar58d98232005-07-23 22:25:46 +00001814/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001815 * Scan the command line arguments.
1816 */
1817 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001818command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001819{
1820 int argc = parmp->argc;
1821 char **argv = parmp->argv;
1822 int argv_idx; /* index in argv[n][] */
1823 int had_minmin = FALSE; /* found "--" argument */
1824 int want_argument; /* option argument with argument */
1825 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001826 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001827 long n;
1828
1829 --argc;
1830 ++argv;
1831 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1832 while (argc > 0)
1833 {
1834 /*
1835 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1836 */
1837 if (argv[0][0] == '+' && !had_minmin)
1838 {
1839 if (parmp->n_commands >= MAX_ARG_CMDS)
1840 mainerr(ME_EXTRA_CMD, NULL);
1841 argv_idx = -1; /* skip to next argument */
1842 if (argv[0][1] == NUL)
1843 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1844 else
1845 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1846 }
1847
1848 /*
1849 * Optional argument.
1850 */
1851 else if (argv[0][0] == '-' && !had_minmin)
1852 {
1853 want_argument = FALSE;
1854 c = argv[0][argv_idx++];
1855#ifdef VMS
1856 /*
1857 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1858 * and "-/X" as "-X".
1859 */
1860 if (c == '/')
1861 {
1862 c = argv[0][argv_idx++];
1863 c = TOUPPER_ASC(c);
1864 }
1865 else
1866 c = TOLOWER_ASC(c);
1867#endif
1868 switch (c)
1869 {
1870 case NUL: /* "vim -" read from stdin */
1871 /* "ex -" silent mode */
1872 if (exmode_active)
1873 silent_mode = TRUE;
1874 else
1875 {
1876 if (parmp->edit_type != EDIT_NONE)
1877 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1878 parmp->edit_type = EDIT_STDIN;
1879 read_cmd_fd = 2; /* read from stderr instead of stdin */
1880 }
1881 argv_idx = -1; /* skip to next argument */
1882 break;
1883
1884 case '-': /* "--" don't take any more option arguments */
1885 /* "--help" give help message */
1886 /* "--version" give version message */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001887 /* "--clean" clean context */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001888 /* "--literal" take files literally */
1889 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001890 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001891 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001892 /* "--noplugin[s]" skip plugins */
1893 /* "--cmd <cmd>" execute cmd before vimrc */
1894 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1895 usage();
1896 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1897 {
1898 Columns = 80; /* need to init Columns */
1899 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1900 list_version();
1901 msg_putchar('\n');
1902 msg_didout = FALSE;
1903 mch_exit(0);
1904 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001905 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
1906 {
1907 parmp->use_vimrc = (char_u *)"DEFAULTS";
1908 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
1909 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001910 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1911 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001912#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001913 parmp->literal = TRUE;
1914#endif
1915 }
1916 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1917 {
1918#ifdef FEAT_GUI
1919 gui.dofork = FALSE; /* don't fork() when starting GUI */
1920#endif
1921 }
1922 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1923 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001924 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1925 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001926 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1927 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001928 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1929 {
1930 want_argument = TRUE;
1931 argv_idx += 3;
1932 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001933 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1934 {
1935 want_argument = TRUE;
1936 argv_idx += 11;
1937 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001938#ifdef FEAT_CLIENTSERVER
1939 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1940 ; /* already processed -- no arg */
1941 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1942 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1943 {
1944 /* already processed -- snatch the following arg */
1945 if (argc > 1)
1946 {
1947 --argc;
1948 ++argv;
1949 }
1950 }
1951#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001952#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1953# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001954 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001955# else
1956 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1957# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001958 {
1959 /* already processed -- snatch the following arg */
1960 if (argc > 1)
1961 {
1962 --argc;
1963 ++argv;
1964 }
1965 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001966#endif
1967#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001968 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1969 {
1970 /* already processed, skip */
1971 }
1972#endif
1973 else
1974 {
1975 if (argv[0][argv_idx])
1976 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1977 had_minmin = TRUE;
1978 }
1979 if (!want_argument)
1980 argv_idx = -1; /* skip to next argument */
1981 break;
1982
1983 case 'A': /* "-A" start in Arabic mode */
1984#ifdef FEAT_ARABIC
1985 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1986#else
1987 mch_errmsg(_(e_noarabic));
1988 mch_exit(2);
1989#endif
1990 break;
1991
1992 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001993 /* Needs to be effective before expanding file names, because
1994 * for Win32 this makes us edit a shortcut file itself,
1995 * instead of the file it links to. */
1996 set_options_bin(curbuf->b_p_bin, 1, 0);
1997 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001998 break;
1999
2000 case 'C': /* "-C" Compatible */
2001 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02002002 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002003 break;
2004
2005 case 'e': /* "-e" Ex mode */
2006 exmode_active = EXMODE_NORMAL;
2007 break;
2008
2009 case 'E': /* "-E" Improved Ex mode */
2010 exmode_active = EXMODE_VIM;
2011 break;
2012
2013 case 'f': /* "-f" GUI: run in foreground. Amiga: open
2014 window directly, not with newcli */
2015#ifdef FEAT_GUI
2016 gui.dofork = FALSE; /* don't fork() when starting GUI */
2017#endif
2018 break;
2019
2020 case 'g': /* "-g" start GUI */
2021 main_start_gui();
2022 break;
2023
2024 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2025#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002026 p_fkmap = TRUE;
2027 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002028#else
2029 mch_errmsg(_(e_nofarsi));
2030 mch_exit(2);
2031#endif
2032 break;
2033
2034 case 'h': /* "-h" give help message */
2035#ifdef FEAT_GUI_GNOME
2036 /* Tell usage() to exit for "gvim". */
2037 gui.starting = FALSE;
2038#endif
2039 usage();
2040 break;
2041
2042 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2043#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002044 p_hkmap = TRUE;
2045 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002046#else
2047 mch_errmsg(_(e_nohebrew));
2048 mch_exit(2);
2049#endif
2050 break;
2051
2052 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2053#ifdef FEAT_LISP
2054 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2055 p_sm = TRUE;
2056#endif
2057 break;
2058
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002059 case 'M': /* "-M" no changes or writing of files */
2060 reset_modifiable();
2061 /* FALLTHROUGH */
2062
2063 case 'm': /* "-m" no writing of files */
2064 p_write = FALSE;
2065 break;
2066
2067 case 'y': /* "-y" easy mode */
2068#ifdef FEAT_GUI
2069 gui.starting = TRUE; /* start GUI a bit later */
2070#endif
2071 parmp->evim_mode = TRUE;
2072 break;
2073
2074 case 'N': /* "-N" Nocompatible */
2075 change_compatible(FALSE);
2076 break;
2077
2078 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002079#ifdef FEAT_NETBEANS_INTG
2080 /* checking for "-nb", netbeans parameters */
2081 if (argv[0][argv_idx] == 'b')
2082 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002083 netbeansArg = argv[0];
2084 argv_idx = -1; /* skip to next argument */
2085 }
2086 else
2087#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002088 parmp->no_swap_file = TRUE;
2089 break;
2090
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002091 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002092#ifdef TARGET_API_MAC_OSX
2093 /* For some reason on MacOS X, an argument like:
2094 -psn_0_10223617 is passed in when invoke from Finder
2095 or with the 'open' command */
2096 if (argv[0][argv_idx] == 's')
2097 {
2098 argv_idx = -1; /* bypass full -psn */
2099 main_start_gui();
2100 break;
2101 }
2102#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002103#ifdef FEAT_WINDOWS
2104 /* default is 0: open window for each file */
2105 parmp->window_count = get_number_arg((char_u *)argv[0],
2106 &argv_idx, 0);
2107 parmp->window_layout = WIN_TABS;
2108#endif
2109 break;
2110
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002111 case 'o': /* "-o[N]" open N horizontal split windows */
2112#ifdef FEAT_WINDOWS
2113 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002114 parmp->window_count = get_number_arg((char_u *)argv[0],
2115 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002116 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002117#endif
2118 break;
2119
2120 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002121#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002122 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002123 parmp->window_count = get_number_arg((char_u *)argv[0],
2124 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002125 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002126#endif
2127 break;
2128
2129#ifdef FEAT_QUICKFIX
2130 case 'q': /* "-q" QuickFix mode */
2131 if (parmp->edit_type != EDIT_NONE)
2132 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2133 parmp->edit_type = EDIT_QF;
2134 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2135 {
2136 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2137 argv_idx = -1;
2138 }
2139 else if (argc > 1) /* "-q {errorfile}" */
2140 want_argument = TRUE;
2141 break;
2142#endif
2143
2144 case 'R': /* "-R" readonly mode */
2145 readonlymode = TRUE;
2146 curbuf->b_p_ro = TRUE;
2147 p_uc = 10000; /* don't update very often */
2148 break;
2149
2150 case 'r': /* "-r" recovery mode */
2151 case 'L': /* "-L" recovery mode */
2152 recoverymode = 1;
2153 break;
2154
2155 case 's':
2156 if (exmode_active) /* "-s" silent (batch) mode */
2157 silent_mode = TRUE;
2158 else /* "-s {scriptin}" read from script file */
2159 want_argument = TRUE;
2160 break;
2161
2162 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2163 if (parmp->edit_type != EDIT_NONE)
2164 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2165 parmp->edit_type = EDIT_TAG;
2166 if (argv[0][argv_idx]) /* "-t{tag}" */
2167 {
2168 parmp->tagname = (char_u *)argv[0] + argv_idx;
2169 argv_idx = -1;
2170 }
2171 else /* "-t {tag}" */
2172 want_argument = TRUE;
2173 break;
2174
2175#ifdef FEAT_EVAL
2176 case 'D': /* "-D" Debugging */
2177 parmp->use_debug_break_level = 9999;
2178 break;
2179#endif
2180#ifdef FEAT_DIFF
2181 case 'd': /* "-d" 'diff' */
2182# ifdef AMIGA
2183 /* check for "-dev {device}" */
2184 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2185 want_argument = TRUE;
2186 else
2187# endif
2188 parmp->diff_mode = TRUE;
2189 break;
2190#endif
2191 case 'V': /* "-V{N}" Verbose level */
2192 /* default is 10: a little bit verbose */
2193 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2194 if (argv[0][argv_idx] != NUL)
2195 {
2196 set_option_value((char_u *)"verbosefile", 0L,
2197 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002198 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002199 }
2200 break;
2201
2202 case 'v': /* "-v" Vi-mode (as if called "vi") */
2203 exmode_active = 0;
2204#ifdef FEAT_GUI
2205 gui.starting = FALSE; /* don't start GUI */
2206#endif
2207 break;
2208
2209 case 'w': /* "-w{number}" set window height */
2210 /* "-w {scriptout}" write to script */
2211 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2212 {
2213 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2214 set_option_value((char_u *)"window", n, NULL, 0);
2215 break;
2216 }
2217 want_argument = TRUE;
2218 break;
2219
2220#ifdef FEAT_CRYPT
2221 case 'x': /* "-x" encrypted reading/writing of files */
2222 parmp->ask_for_key = TRUE;
2223 break;
2224#endif
2225
2226 case 'X': /* "-X" don't connect to X server */
2227#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2228 x_no_connect = TRUE;
2229#endif
2230 break;
2231
2232 case 'Z': /* "-Z" restricted mode */
2233 restricted = TRUE;
2234 break;
2235
2236 case 'c': /* "-c{command}" or "-c {command}" execute
2237 command */
2238 if (argv[0][argv_idx] != NUL)
2239 {
2240 if (parmp->n_commands >= MAX_ARG_CMDS)
2241 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002242 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2243 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002244 argv_idx = -1;
2245 break;
2246 }
2247 /*FALLTHROUGH*/
2248 case 'S': /* "-S {file}" execute Vim script */
2249 case 'i': /* "-i {viminfo}" use for viminfo */
2250#ifndef FEAT_DIFF
2251 case 'd': /* "-d {device}" device (for Amiga) */
2252#endif
2253 case 'T': /* "-T {terminal}" terminal name */
2254 case 'u': /* "-u {vimrc}" vim inits file */
2255 case 'U': /* "-U {gvimrc}" gvim inits file */
2256 case 'W': /* "-W {scriptout}" overwrite */
2257#ifdef FEAT_GUI_W32
2258 case 'P': /* "-P {parent title}" MDI parent */
2259#endif
2260 want_argument = TRUE;
2261 break;
2262
2263 default:
2264 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2265 }
2266
2267 /*
2268 * Handle option arguments with argument.
2269 */
2270 if (want_argument)
2271 {
2272 /*
2273 * Check for garbage immediately after the option letter.
2274 */
2275 if (argv[0][argv_idx] != NUL)
2276 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2277
2278 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002279 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002280 mainerr_arg_missing((char_u *)argv[0]);
2281 ++argv;
2282 argv_idx = -1;
2283
2284 switch (c)
2285 {
2286 case 'c': /* "-c {command}" execute command */
2287 case 'S': /* "-S {file}" execute Vim script */
2288 if (parmp->n_commands >= MAX_ARG_CMDS)
2289 mainerr(ME_EXTRA_CMD, NULL);
2290 if (c == 'S')
2291 {
2292 char *a;
2293
2294 if (argc < 1)
2295 /* "-S" without argument: use default session file
2296 * name. */
2297 a = SESSION_FILE;
2298 else if (argv[0][0] == '-')
2299 {
2300 /* "-S" followed by another option: use default
2301 * session file name. */
2302 a = SESSION_FILE;
2303 ++argc;
2304 --argv;
2305 }
2306 else
2307 a = argv[0];
2308 p = alloc((unsigned)(STRLEN(a) + 4));
2309 if (p == NULL)
2310 mch_exit(2);
2311 sprintf((char *)p, "so %s", a);
2312 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2313 parmp->commands[parmp->n_commands++] = p;
2314 }
2315 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002316 parmp->commands[parmp->n_commands++] =
2317 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002318 break;
2319
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002320 case '-':
2321 if (argv[-1][2] == 'c')
2322 {
2323 /* "--cmd {command}" execute command */
2324 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2325 mainerr(ME_EXTRA_CMD, NULL);
2326 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002327 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002328 }
2329 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002330 break;
2331
2332 /* case 'd': -d {device} is handled in mch_check_win() for the
2333 * Amiga */
2334
2335#ifdef FEAT_QUICKFIX
2336 case 'q': /* "-q {errorfile}" QuickFix mode */
2337 parmp->use_ef = (char_u *)argv[0];
2338 break;
2339#endif
2340
2341 case 'i': /* "-i {viminfo}" use for viminfo */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002342 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002343 break;
2344
2345 case 's': /* "-s {scriptin}" read from script file */
2346 if (scriptin[0] != NULL)
2347 {
2348scripterror:
2349 mch_errmsg(_("Attempt to open script file again: \""));
2350 mch_errmsg(argv[-1]);
2351 mch_errmsg(" ");
2352 mch_errmsg(argv[0]);
2353 mch_errmsg("\"\n");
2354 mch_exit(2);
2355 }
2356 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2357 {
2358 mch_errmsg(_("Cannot open for reading: \""));
2359 mch_errmsg(argv[0]);
2360 mch_errmsg("\"\n");
2361 mch_exit(2);
2362 }
2363 if (save_typebuf() == FAIL)
2364 mch_exit(2); /* out of memory */
2365 break;
2366
2367 case 't': /* "-t {tag}" */
2368 parmp->tagname = (char_u *)argv[0];
2369 break;
2370
2371 case 'T': /* "-T {terminal}" terminal name */
2372 /*
2373 * The -T term argument is always available and when
2374 * HAVE_TERMLIB is supported it overrides the environment
2375 * variable TERM.
2376 */
2377#ifdef FEAT_GUI
2378 if (term_is_gui((char_u *)argv[0]))
2379 gui.starting = TRUE; /* start GUI a bit later */
2380 else
2381#endif
2382 parmp->term = (char_u *)argv[0];
2383 break;
2384
2385 case 'u': /* "-u {vimrc}" vim inits file */
2386 parmp->use_vimrc = (char_u *)argv[0];
2387 break;
2388
2389 case 'U': /* "-U {gvimrc}" gvim inits file */
2390#ifdef FEAT_GUI
2391 use_gvimrc = (char_u *)argv[0];
2392#endif
2393 break;
2394
2395 case 'w': /* "-w {nr}" 'window' value */
2396 /* "-w {scriptout}" append to script file */
2397 if (vim_isdigit(*((char_u *)argv[0])))
2398 {
2399 argv_idx = 0;
2400 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2401 set_option_value((char_u *)"window", n, NULL, 0);
2402 argv_idx = -1;
2403 break;
2404 }
2405 /*FALLTHROUGH*/
2406 case 'W': /* "-W {scriptout}" overwrite script file */
2407 if (scriptout != NULL)
2408 goto scripterror;
2409 if ((scriptout = mch_fopen(argv[0],
2410 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2411 {
2412 mch_errmsg(_("Cannot open for script output: \""));
2413 mch_errmsg(argv[0]);
2414 mch_errmsg("\"\n");
2415 mch_exit(2);
2416 }
2417 break;
2418
2419#ifdef FEAT_GUI_W32
2420 case 'P': /* "-P {parent title}" MDI parent */
2421 gui_mch_set_parent(argv[0]);
2422 break;
2423#endif
2424 }
2425 }
2426 }
2427
2428 /*
2429 * File name argument.
2430 */
2431 else
2432 {
2433 argv_idx = -1; /* skip to next argument */
2434
2435 /* Check for only one type of editing. */
2436 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2437 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2438 parmp->edit_type = EDIT_FILE;
2439
2440#ifdef MSWIN
2441 /* Remember if the argument was a full path before changing
2442 * slashes to backslashes. */
2443 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2444 parmp->full_path = TRUE;
2445#endif
2446
2447 /* Add the file to the global argument list. */
2448 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2449 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2450 mch_exit(2);
2451#ifdef FEAT_DIFF
2452 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2453 && !mch_isdir(alist_name(&GARGLIST[0])))
2454 {
2455 char_u *r;
2456
2457 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2458 if (r != NULL)
2459 {
2460 vim_free(p);
2461 p = r;
2462 }
2463 }
2464#endif
2465#if defined(__CYGWIN32__) && !defined(WIN32)
2466 /*
2467 * If vim is invoked by non-Cygwin tools, convert away any
2468 * DOS paths, so things like .swp files are created correctly.
2469 * Look for evidence of non-Cygwin paths before we bother.
2470 * This is only for when using the Unix files.
2471 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002472 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473 {
2474 char posix_path[PATH_MAX];
2475
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002476# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2477 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2478# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002479 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002480# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002481 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002482 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002483 if (p == NULL)
2484 mch_exit(2);
2485 }
2486#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002487
2488#ifdef USE_FNAME_CASE
2489 /* Make the case of the file name match the actual file. */
2490 fname_case(p, 0);
2491#endif
2492
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002493 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002494#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002495 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002496#else
2497 2 /* add buffer number now and use curbuf */
2498#endif
2499 );
2500
2501#if defined(FEAT_MBYTE) && defined(WIN32)
2502 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002503 /* Remember this argument has been added to the argument list.
2504 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002505 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002506# ifdef FEAT_DIFF
2507 parmp->diff_mode
2508# else
2509 FALSE
2510# endif
2511 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002512 }
2513#endif
2514 }
2515
2516 /*
2517 * If there are no more letters after the current "-", go to next
2518 * argument. argv_idx is set to -1 when the current argument is to be
2519 * skipped.
2520 */
2521 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2522 {
2523 --argc;
2524 ++argv;
2525 argv_idx = 1;
2526 }
2527 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002528
2529#ifdef FEAT_EVAL
2530 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2531 * one. */
2532 if (parmp->n_commands > 0)
2533 {
2534 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2535 if (p != NULL)
2536 {
2537 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2538 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2539 vim_free(p);
2540 }
2541 }
2542#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002543}
2544
2545/*
2546 * Print a warning if stdout is not a terminal.
2547 * When starting in Ex mode and commands come from a file, set Silent mode.
2548 */
2549 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002550check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551{
2552 int input_isatty; /* is active input a terminal? */
2553
2554 input_isatty = mch_input_isatty();
2555 if (exmode_active)
2556 {
2557 if (!input_isatty)
2558 silent_mode = TRUE;
2559 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002560 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002561#ifdef FEAT_GUI
2562 /* don't want the delay when started from the desktop */
2563 && !gui.starting
2564#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002565 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002566 {
2567#ifdef NBDEBUG
2568 /*
2569 * This shouldn't be necessary. But if I run netbeans with the log
2570 * output coming to the console and XOpenDisplay fails, I get vim
2571 * trying to start with input/output to my console tty. This fills my
2572 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002573 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002575 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002576 {
2577 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2578 exit(1);
2579 }
2580#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002581#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2582 if (is_cygpty_used())
2583 {
2584 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2585 exit(1);
2586 }
2587#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002588 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2590 if (!input_isatty)
2591 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2592 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002593 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2594 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 if (scriptin[0] == NULL)
2596 ui_delay(2000L, TRUE);
2597 TIME_MSG("Warning delay");
2598 }
2599}
2600
2601/*
2602 * Read text from stdin.
2603 */
2604 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002605read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002606{
2607 int i;
2608
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002609#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002610 /* When getting the ATTENTION prompt here, use a dialog */
2611 swap_exists_action = SEA_DIALOG;
2612#endif
2613 no_wait_return = TRUE;
2614 i = msg_didany;
2615 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002616 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002617 no_wait_return = FALSE;
2618 msg_didany = i;
2619 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002620#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621 check_swap_exists_action();
2622#endif
2623#if !(defined(AMIGA) || defined(MACOS))
2624 /*
2625 * Close stdin and dup it from stderr. Required for GPM to work
2626 * properly, and for running external commands.
2627 * Is there any other system that cannot do this?
2628 */
2629 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002630 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631#endif
2632}
2633
2634/*
2635 * Create the requested number of windows and edit buffers in them.
2636 * Also does recovery if "recoverymode" set.
2637 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002638 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002639create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002640{
2641#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002642 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002643 int done = 0;
2644
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002645 /*
2646 * Create the number of windows that was requested.
2647 */
2648 if (parmp->window_count == -1) /* was not set */
2649 parmp->window_count = 1;
2650 if (parmp->window_count == 0)
2651 parmp->window_count = GARGCOUNT;
2652 if (parmp->window_count > 1)
2653 {
2654 /* Don't change the windows if there was a command in .vimrc that
2655 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002656 if (parmp->window_layout == 0)
2657 parmp->window_layout = WIN_HOR;
2658 if (parmp->window_layout == WIN_TABS)
2659 {
2660 parmp->window_count = make_tabpages(parmp->window_count);
2661 TIME_MSG("making tab pages");
2662 }
2663 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664 {
2665 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002666 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667 TIME_MSG("making windows");
2668 }
2669 else
2670 parmp->window_count = win_count();
2671 }
2672 else
2673 parmp->window_count = 1;
2674#endif
2675
2676 if (recoverymode) /* do recover */
2677 {
2678 msg_scroll = TRUE; /* scroll message up */
2679 ml_recover();
2680 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2681 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002682 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002683 }
2684 else
2685 {
2686 /*
2687 * Open a buffer for windows that don't have one yet.
2688 * Commands in the .vimrc might have loaded a file or split the window.
2689 * Watch out for autocommands that delete a window.
2690 */
2691#ifdef FEAT_AUTOCMD
2692 /*
2693 * Don't execute Win/Buf Enter/Leave autocommands here
2694 */
2695 ++autocmd_no_enter;
2696 ++autocmd_no_leave;
2697#endif
2698#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002699 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002700 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002701 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002703 {
2704 if (parmp->window_layout == WIN_TABS)
2705 goto_tabpage(1);
2706 else
2707 curwin = firstwin;
2708 }
2709 else if (parmp->window_layout == WIN_TABS)
2710 {
2711 if (curtab->tp_next == NULL)
2712 break;
2713 goto_tabpage(0);
2714 }
2715 else
2716 {
2717 if (curwin->w_next == NULL)
2718 break;
2719 curwin = curwin->w_next;
2720 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002721 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002722#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002723 curbuf = curwin->w_buffer;
2724 if (curbuf->b_ml.ml_mfp == NULL)
2725 {
2726#ifdef FEAT_FOLDING
2727 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2728 if (p_fdls >= 0)
2729 curwin->w_p_fdl = p_fdls;
2730#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002731#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002732 /* When getting the ATTENTION prompt here, use a dialog */
2733 swap_exists_action = SEA_DIALOG;
2734#endif
2735 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002736
2737 /* create memfile, read file */
2738 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002740#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002741 if (swap_exists_action == SEA_QUIT)
2742 {
2743 if (got_int || only_one_window())
2744 {
2745 /* abort selected or quit and only one window */
2746 did_emsg = FALSE; /* avoid hit-enter prompt */
2747 getout(1);
2748 }
2749 /* We can't close the window, it would disturb what
2750 * happens next. Clear the file name and set the arg
2751 * index to -1 to delete it later. */
2752 setfname(curbuf, NULL, NULL, FALSE);
2753 curwin->w_arg_idx = -1;
2754 swap_exists_action = SEA_NONE;
2755 }
2756 else
2757 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758#endif
2759#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002760 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002761#endif
2762 }
2763#ifdef FEAT_WINDOWS
2764 ui_breakcheck();
2765 if (got_int)
2766 {
2767 (void)vgetc(); /* only break the file loading, not the rest */
2768 break;
2769 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002771#endif
2772#ifdef FEAT_WINDOWS
2773 if (parmp->window_layout == WIN_TABS)
2774 goto_tabpage(1);
2775 else
2776 curwin = firstwin;
2777 curbuf = curwin->w_buffer;
2778#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002779#ifdef FEAT_AUTOCMD
2780 --autocmd_no_enter;
2781 --autocmd_no_leave;
2782#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 }
2784}
2785
2786#ifdef FEAT_WINDOWS
2787 /*
2788 * If opened more than one window, start editing files in the other
2789 * windows. make_windows() has already opened the windows.
2790 */
2791 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002792edit_buffers(
2793 mparm_T *parmp,
2794 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002795{
2796 int arg_idx; /* index in argument list */
2797 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002798 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002799 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002800
2801# ifdef FEAT_AUTOCMD
2802 /*
2803 * Don't execute Win/Buf Enter/Leave autocommands here
2804 */
2805 ++autocmd_no_enter;
2806 ++autocmd_no_leave;
2807# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002808
2809 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2810 if (curwin->w_arg_idx == -1)
2811 {
2812 win_close(curwin, TRUE);
2813 advance = FALSE;
2814 }
2815
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816 arg_idx = 1;
2817 for (i = 1; i < parmp->window_count; ++i)
2818 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002819 if (cwd != NULL)
2820 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002821 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2822 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002823 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002824 ++arg_idx;
2825 win_close(curwin, TRUE);
2826 advance = FALSE;
2827 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002828 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002829
Bram Moolenaar84212822006-11-07 21:59:47 +00002830 if (advance)
2831 {
2832 if (parmp->window_layout == WIN_TABS)
2833 {
2834 if (curtab->tp_next == NULL) /* just checking */
2835 break;
2836 goto_tabpage(0);
2837 }
2838 else
2839 {
2840 if (curwin->w_next == NULL) /* just checking */
2841 break;
2842 win_enter(curwin->w_next, FALSE);
2843 }
2844 }
2845 advance = TRUE;
2846
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002847 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002848 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002849 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2850 {
2851 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002852 /* Edit file from arg list, if there is one. When "Quit" selected
2853 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002854# ifdef HAS_SWAP_EXISTS_ACTION
2855 swap_exists_did_quit = FALSE;
2856# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002857 (void)do_ecmd(0, arg_idx < GARGCOUNT
2858 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002859 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002860# ifdef HAS_SWAP_EXISTS_ACTION
2861 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002862 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002863 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002864 if (got_int || only_one_window())
2865 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002866 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002867 did_emsg = FALSE; /* avoid hit-enter prompt */
2868 getout(1);
2869 }
2870 win_close(curwin, TRUE);
2871 advance = FALSE;
2872 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002873# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002874 if (arg_idx == GARGCOUNT - 1)
2875 arg_had_last = TRUE;
2876 ++arg_idx;
2877 }
2878 ui_breakcheck();
2879 if (got_int)
2880 {
2881 (void)vgetc(); /* only break the file loading, not the rest */
2882 break;
2883 }
2884 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002885
2886 if (parmp->window_layout == WIN_TABS)
2887 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002888# ifdef FEAT_AUTOCMD
2889 --autocmd_no_enter;
2890# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002891
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002892 /* make the first window the current window */
2893 win = firstwin;
2894#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2895 /* Avoid making a preview window the current window. */
2896 while (win->w_p_pvw)
2897 {
2898 win = win->w_next;
2899 if (win == NULL)
2900 {
2901 win = firstwin;
2902 break;
2903 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002904 }
2905#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002906 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002907
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002908# ifdef FEAT_AUTOCMD
2909 --autocmd_no_leave;
2910# endif
2911 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002912 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002913 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2914}
2915#endif /* FEAT_WINDOWS */
2916
2917/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002918 * Execute the commands from --cmd arguments "cmds[cnt]".
2919 */
2920 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002921exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002922{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002923 char_u **cmds = parmp->pre_commands;
2924 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002925 int i;
2926
2927 if (cnt > 0)
2928 {
2929 curwin->w_cursor.lnum = 0; /* just in case.. */
2930 sourcing_name = (char_u *)_("pre-vimrc command line");
2931# ifdef FEAT_EVAL
2932 current_SID = SID_CMDARG;
2933# endif
2934 for (i = 0; i < cnt; ++i)
2935 do_cmdline_cmd(cmds[i]);
2936 sourcing_name = NULL;
2937# ifdef FEAT_EVAL
2938 current_SID = 0;
2939# endif
2940 TIME_MSG("--cmd commands");
2941 }
2942}
2943
2944/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002945 * Execute "+", "-c" and "-S" arguments.
2946 */
2947 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002948exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002949{
2950 int i;
2951
2952 /*
2953 * We start commands on line 0, make "vim +/pat file" match a
2954 * pattern on line 1. But don't move the cursor when an autocommand
2955 * with g`" was used.
2956 */
2957 msg_scroll = TRUE;
2958 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2959 curwin->w_cursor.lnum = 0;
2960 sourcing_name = (char_u *)"command line";
2961#ifdef FEAT_EVAL
2962 current_SID = SID_CARG;
2963#endif
2964 for (i = 0; i < parmp->n_commands; ++i)
2965 {
2966 do_cmdline_cmd(parmp->commands[i]);
2967 if (parmp->cmds_tofree[i])
2968 vim_free(parmp->commands[i]);
2969 }
2970 sourcing_name = NULL;
2971#ifdef FEAT_EVAL
2972 current_SID = 0;
2973#endif
2974 if (curwin->w_cursor.lnum == 0)
2975 curwin->w_cursor.lnum = 1;
2976
2977 if (!exmode_active)
2978 msg_scroll = FALSE;
2979
2980#ifdef FEAT_QUICKFIX
2981 /* When started with "-q errorfile" jump to first error again. */
2982 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002983 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002984#endif
2985 TIME_MSG("executing command arguments");
2986}
2987
2988/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002989 * Source startup scripts.
2990 */
2991 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002992source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002993{
2994 int i;
2995
2996 /*
2997 * For "evim" source evim.vim first of all, so that the user can overrule
2998 * any things he doesn't like.
2999 */
3000 if (parmp->evim_mode)
3001 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003002 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003 TIME_MSG("source evim file");
3004 }
3005
3006 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003007 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008 * nothing else.
3009 */
3010 if (parmp->use_vimrc != NULL)
3011 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003012 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
3013 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
3014 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003015 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003016 {
3017#ifdef FEAT_GUI
3018 if (use_gvimrc == NULL) /* don't load gvimrc either */
3019 use_gvimrc = parmp->use_vimrc;
3020#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003021 }
3022 else
3023 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003024 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003025 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3026 }
3027 }
3028 else if (!silent_mode)
3029 {
3030#ifdef AMIGA
3031 struct Process *proc = (struct Process *)FindTask(0L);
3032 APTR save_winptr = proc->pr_WindowPtr;
3033
3034 /* Avoid a requester here for a volume that doesn't exist. */
3035 proc->pr_WindowPtr = (APTR)-1L;
3036#endif
3037
3038 /*
3039 * Get system wide defaults, if the file name is defined.
3040 */
3041#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003042 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003043#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003044#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003045 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003046#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003047
3048 /*
3049 * Try to read initialization commands from the following places:
3050 * - environment variable VIMINIT
3051 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3052 * - second user vimrc file ($VIM/.vimrc for Dos)
3053 * - environment variable EXINIT
3054 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3055 * - second user exrc file ($VIM/.exrc for Dos)
3056 * The first that exists is used, the rest is ignored.
3057 */
3058 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3059 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003060 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003061#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003062 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3063 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003064#endif
3065#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003066 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3067 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003068#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003069#ifdef USR_VIMRC_FILE4
3070 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3071 DOSO_VIMRC) == FAIL
3072#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003073 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003074 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003075#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003076 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003077#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003078 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003079 {
3080 /* When no .vimrc file was found: source defaults.vim. */
3081 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003082 }
3083 }
3084
3085 /*
3086 * Read initialization commands from ".vimrc" or ".exrc" in current
3087 * directory. This is only done if the 'exrc' option is set.
3088 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003089 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003090 * option has been reset in environment of global ".exrc" or ".vimrc".
3091 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3092 * SYS_VIMRC_FILE.
3093 */
3094 if (p_exrc)
3095 {
3096#if defined(UNIX) || defined(VMS)
3097 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3098 if (!file_owned(VIMRC_FILE))
3099#endif
3100 secure = p_secure;
3101
3102 i = FAIL;
3103 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3104 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3105#ifdef USR_VIMRC_FILE2
3106 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3107 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3108#endif
3109#ifdef USR_VIMRC_FILE3
3110 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3111 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3112#endif
3113#ifdef SYS_VIMRC_FILE
3114 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3115 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3116#endif
3117 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003118 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003119
3120 if (i == FAIL)
3121 {
3122#if defined(UNIX) || defined(VMS)
3123 /* if ".exrc" is not owned by user set 'secure' mode */
3124 if (!file_owned(EXRC_FILE))
3125 secure = p_secure;
3126 else
3127 secure = 0;
3128#endif
3129 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3130 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3131#ifdef USR_EXRC_FILE2
3132 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3133 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3134#endif
3135 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003136 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003137 }
3138 }
3139 if (secure == 2)
3140 need_wait_return = TRUE;
3141 secure = 0;
3142#ifdef AMIGA
3143 proc->pr_WindowPtr = save_winptr;
3144#endif
3145 }
3146 TIME_MSG("sourcing vimrc file(s)");
3147}
3148
3149/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003150 * Setup to start using the GUI. Exit with an error when not available.
3151 */
3152 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003153main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003154{
3155#ifdef FEAT_GUI
3156 gui.starting = TRUE; /* start GUI a bit later */
3157#else
3158 mch_errmsg(_(e_nogvim));
3159 mch_errmsg("\n");
3160 mch_exit(2);
3161#endif
3162}
3163
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003164#endif /* NO_VIM_MAIN */
3165
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003166/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003167 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003168 * Returns FAIL if the environment variable was not executed, OK otherwise.
3169 */
3170 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003171process_env(
3172 char_u *env,
3173 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003174{
3175 char_u *initstr;
3176 char_u *save_sourcing_name;
3177 linenr_T save_sourcing_lnum;
3178#ifdef FEAT_EVAL
3179 scid_T save_sid;
3180#endif
3181
3182 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3183 {
3184 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003185 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003186 save_sourcing_name = sourcing_name;
3187 save_sourcing_lnum = sourcing_lnum;
3188 sourcing_name = env;
3189 sourcing_lnum = 0;
3190#ifdef FEAT_EVAL
3191 save_sid = current_SID;
3192 current_SID = SID_ENV;
3193#endif
3194 do_cmdline_cmd(initstr);
3195 sourcing_name = save_sourcing_name;
3196 sourcing_lnum = save_sourcing_lnum;
3197#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003198 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003199#endif
3200 return OK;
3201 }
3202 return FAIL;
3203}
3204
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003205#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003206/*
3207 * Return TRUE if we are certain the user owns the file "fname".
3208 * Used for ".vimrc" and ".exrc".
3209 * Use both stat() and lstat() for extra security.
3210 */
3211 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003212file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003213{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003214 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003215# ifdef UNIX
3216 uid_t uid = getuid();
3217# else /* VMS */
3218 uid_t uid = ((getgid() << 16) | getuid());
3219# endif
3220
3221 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3222# ifdef HAVE_LSTAT
3223 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3224# endif
3225 );
3226}
3227#endif
3228
3229/*
3230 * Give an error message main_errors["n"] and exit.
3231 */
3232 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003233mainerr(
3234 int n, /* one of the ME_ defines */
3235 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003236{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003237#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003238 reset_signals(); /* kill us with CTRL-C here, if you like */
3239#endif
3240
3241 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003242 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003243 mch_errmsg(_(main_errors[n]));
3244 if (str != NULL)
3245 {
3246 mch_errmsg(": \"");
3247 mch_errmsg((char *)str);
3248 mch_errmsg("\"");
3249 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003250 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003251
3252 mch_exit(1);
3253}
3254
3255 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003256mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003257{
3258 mainerr(ME_ARG_MISSING, str);
3259}
3260
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003261#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003262/*
3263 * print a message with three spaces prepended and '\n' appended.
3264 */
3265 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003266main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003267{
3268 mch_msg(" ");
3269 mch_msg(s);
3270 mch_msg("\n");
3271}
3272
3273/*
3274 * Print messages for "vim -h" or "vim --help" and exit.
3275 */
3276 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003277usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003278{
3279 int i;
3280 static char *(use[]) =
3281 {
3282 N_("[file ..] edit specified file(s)"),
3283 N_("- read text from stdin"),
3284 N_("-t tag edit file where tag is defined"),
3285#ifdef FEAT_QUICKFIX
3286 N_("-q [errorfile] edit file with first error")
3287#endif
3288 };
3289
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003290#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003291 reset_signals(); /* kill us with CTRL-C here, if you like */
3292#endif
3293
3294 mch_msg(longVersion);
3295 mch_msg(_("\n\nusage:"));
3296 for (i = 0; ; ++i)
3297 {
3298 mch_msg(_(" vim [arguments] "));
3299 mch_msg(_(use[i]));
3300 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3301 break;
3302 mch_msg(_("\n or:"));
3303 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003304#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003305 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003306#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003307
3308 mch_msg(_("\n\nArguments:\n"));
3309 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003310#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003311 main_msg(_("--literal\t\tDon't expand wildcards"));
3312#endif
3313#ifdef FEAT_OLE
3314 main_msg(_("-register\t\tRegister this gvim for OLE"));
3315 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3316#endif
3317#ifdef FEAT_GUI
3318 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3319 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3320#endif
3321 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3322 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003323 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003324 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3325#ifdef FEAT_DIFF
3326 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3327#endif
3328 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3329 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3330 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3331 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3332 main_msg(_("-M\t\t\tModifications in text not allowed"));
3333 main_msg(_("-b\t\t\tBinary mode"));
3334#ifdef FEAT_LISP
3335 main_msg(_("-l\t\t\tLisp mode"));
3336#endif
3337 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3338 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003339 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3340#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003341 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003342#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003343 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3344 main_msg(_("-r\t\t\tList swap files and exit"));
3345 main_msg(_("-r (with file name)\tRecover crashed session"));
3346 main_msg(_("-L\t\t\tSame as -r"));
3347#ifdef AMIGA
3348 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3349 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3350#endif
3351#ifdef FEAT_ARABIC
3352 main_msg(_("-A\t\t\tstart in Arabic mode"));
3353#endif
3354#ifdef FEAT_RIGHTLEFT
3355 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3356#endif
3357#ifdef FEAT_FKMAP
3358 main_msg(_("-F\t\t\tStart in Farsi mode"));
3359#endif
3360 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003361 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003362 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003363 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3364#ifdef FEAT_GUI
3365 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3366#endif
3367 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003368#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003369 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003370 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3371 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003372#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003373 main_msg(_("+\t\t\tStart at end of file"));
3374 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3377 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3378 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3379 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3380 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3381#ifdef FEAT_CRYPT
3382 main_msg(_("-x\t\t\tEdit encrypted files"));
3383#endif
3384#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3385# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3386 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3387# endif
3388 main_msg(_("-X\t\t\tDo not connect to X server"));
3389#endif
3390#ifdef FEAT_CLIENTSERVER
3391 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3392 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3393 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3394 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003395# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003396 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003397# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003398 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3399 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3400 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3401 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3402#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003403#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003404 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003405#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406#ifdef FEAT_VIMINFO
3407 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3408#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003409 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003410 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3411 main_msg(_("--version\t\tPrint version information and exit"));
3412
3413#ifdef FEAT_GUI_X11
3414# ifdef FEAT_GUI_MOTIF
3415 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3416# else
3417# ifdef FEAT_GUI_ATHENA
3418# ifdef FEAT_GUI_NEXTAW
3419 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3420# else
3421 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3422# endif
3423# endif
3424# endif
3425 main_msg(_("-display <display>\tRun vim on <display>"));
3426 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3428 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3429 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3430 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3431 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3432 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3433 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3434 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3435# ifdef FEAT_GUI_ATHENA
3436 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3437# endif
3438 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3439 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3440 main_msg(_("-xrm <resource>\tSet the specified resource"));
3441#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442#ifdef FEAT_GUI_GTK
3443 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3444 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3445 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3446 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3447 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003448 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003449 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003450 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003451#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003452#ifdef FEAT_GUI_W32
3453 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003454 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003455#endif
3456
3457#ifdef FEAT_GUI_GNOME
3458 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3459 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003460 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003461 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003462 gui.dofork = FALSE;
3463 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464 else
3465#endif
3466 mch_exit(0);
3467}
3468
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003469#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003470/*
3471 * Check the result of the ATTENTION dialog:
3472 * When "Quit" selected, exit Vim.
3473 * When "Recover" selected, recover the file.
3474 */
3475 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003476check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003477{
3478 if (swap_exists_action == SEA_QUIT)
3479 getout(1);
3480 handle_swap_exists(NULL);
3481}
3482#endif
3483
Bram Moolenaar08cab962017-03-04 14:37:18 +01003484#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003485
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003486#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003487static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003488
3489static struct timeval prev_timeval;
3490
Bram Moolenaar3f269672009-11-03 11:11:11 +00003491# ifdef WIN3264
3492/*
3493 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3494 */
3495 static int
3496gettimeofday(struct timeval *tv, char *dummy)
3497{
3498 long t = clock();
3499 tv->tv_sec = t / CLOCKS_PER_SEC;
3500 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3501 return 0;
3502}
3503# endif
3504
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003505/*
3506 * Save the previous time before doing something that could nest.
3507 * set "*tv_rel" to the time elapsed so far.
3508 */
3509 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003510time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003511{
3512 *((struct timeval *)tv_rel) = prev_timeval;
3513 gettimeofday(&prev_timeval, NULL);
3514 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3515 - ((struct timeval *)tv_rel)->tv_usec;
3516 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3517 - ((struct timeval *)tv_rel)->tv_sec;
3518 if (((struct timeval *)tv_rel)->tv_usec < 0)
3519 {
3520 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3521 --((struct timeval *)tv_rel)->tv_sec;
3522 }
3523 *(struct timeval *)tv_start = prev_timeval;
3524}
3525
3526/*
3527 * Compute the previous time after doing something that could nest.
3528 * Subtract "*tp" from prev_timeval;
3529 * Note: The arguments are (void *) to avoid trouble with systems that don't
3530 * have struct timeval.
3531 */
3532 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003533time_pop(
3534 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535{
3536 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3537 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3538 if (prev_timeval.tv_usec < 0)
3539 {
3540 prev_timeval.tv_usec += 1000000;
3541 --prev_timeval.tv_sec;
3542 }
3543}
3544
3545 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003546time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003547{
3548 long usec;
3549 long msec;
3550
3551 usec = now->tv_usec - then->tv_usec;
3552 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3553 usec = usec % 1000L;
3554 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3555}
3556
3557 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003558time_msg(
3559 char *mesg,
3560 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003561 (struct timeval *) */
3562{
3563 static struct timeval start;
3564 struct timeval now;
3565
3566 if (time_fd != NULL)
3567 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003568 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003569 {
3570 gettimeofday(&start, NULL);
3571 prev_timeval = start;
3572 fprintf(time_fd, "\n\ntimes in msec\n");
3573 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3574 fprintf(time_fd, " clock elapsed: other lines\n\n");
3575 }
3576 gettimeofday(&now, NULL);
3577 time_diff(&start, &now);
3578 if (((struct timeval *)tv_start) != NULL)
3579 {
3580 fprintf(time_fd, " ");
3581 time_diff(((struct timeval *)tv_start), &now);
3582 }
3583 fprintf(time_fd, " ");
3584 time_diff(&prev_timeval, &now);
3585 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003586 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003587 }
3588}
3589
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003590#endif
3591
Bram Moolenaar595297d2017-03-04 19:11:12 +01003592#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003593 static void
3594set_progpath(char_u *argv0)
3595{
3596 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003597
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003598# ifdef PROC_EXE_LINK
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003599 char buf[PATH_MAX + 1];
3600 ssize_t len;
3601
Bram Moolenaarf3757f02017-03-16 15:13:45 +01003602 len = readlink(PROC_EXE_LINK, buf, PATH_MAX);
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003603 if (len > 0)
3604 {
3605 buf[len] = NUL;
3606 val = (char_u *)buf;
3607 }
3608# else
Bram Moolenaar08cab962017-03-04 14:37:18 +01003609 /* A relative path containing a "/" will become invalid when using ":cd",
3610 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003611 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3612 * this. */
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003613# ifdef WIN32
Bram Moolenaar066029e2017-03-05 15:19:32 +01003614 char_u *path = NULL;
3615
3616 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3617 val = path;
Bram Moolenaar47ffb902017-03-12 18:38:02 +01003618# else
Bram Moolenaar066029e2017-03-05 15:19:32 +01003619 char_u buf[MAXPATHL];
3620
Bram Moolenaar43663192017-03-05 14:29:12 +01003621 if (!mch_isFullName(argv0))
3622 {
Bram Moolenaar43663192017-03-05 14:29:12 +01003623 if (gettail(argv0) != argv0
3624 && vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3625 val = buf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003626 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003627# endif
Bram Moolenaar066029e2017-03-05 15:19:32 +01003628# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003629
Bram Moolenaar08cab962017-03-04 14:37:18 +01003630 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003631
Bram Moolenaar066029e2017-03-05 15:19:32 +01003632# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003633 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003634# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003635}
3636
3637#endif /* NO_VIM_MAIN */
3638
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003639#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003640
3641/*
3642 * Common code for the X command server and the Win32 command server.
3643 */
3644
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003645static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003646
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003647/*
3648 * Do the client-server stuff, unless "--servername ''" was used.
3649 */
3650 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003651exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003652{
3653 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3654 {
3655# ifdef WIN32
3656 /* Initialise the client/server messaging infrastructure. */
3657 serverInitMessaging();
3658# endif
3659
3660 /*
3661 * When a command server argument was found, execute it. This may
3662 * exit Vim when it was successful. Otherwise it's executed further
3663 * on. Remember the encoding used here in "serverStrEnc".
3664 */
3665 if (parmp->serverArg)
3666 {
3667 cmdsrv_main(&parmp->argc, parmp->argv,
3668 parmp->serverName_arg, &parmp->serverStr);
3669# ifdef FEAT_MBYTE
3670 parmp->serverStrEnc = vim_strsave(p_enc);
3671# endif
3672 }
3673
3674 /* If we're still running, get the name to register ourselves.
3675 * On Win32 can register right now, for X11 need to setup the
3676 * clipboard first, it's further down. */
3677 parmp->servername = serverMakeName(parmp->serverName_arg,
3678 parmp->argv[0]);
3679# ifdef WIN32
3680 if (parmp->servername != NULL)
3681 {
3682 serverSetName(parmp->servername);
3683 vim_free(parmp->servername);
3684 }
3685# endif
3686 }
3687}
3688
3689/*
3690 * Prepare for running as a Vim server.
3691 */
3692 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003693prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003694{
3695# if defined(FEAT_X11)
3696 /*
3697 * Register for remote command execution with :serversend and --remote
3698 * unless there was a -X or a --servername '' on the command line.
3699 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003700 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003701 */
3702 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3703# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003704 (gui.in_use
3705# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003706 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003707# endif
3708 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003709# endif
3710 parmp->serverName_arg != NULL))
3711 {
3712 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3713 vim_free(parmp->servername);
3714 TIME_MSG("register server name");
3715 }
3716 else
3717 serverDelayedStartName = parmp->servername;
3718# endif
3719
3720 /*
3721 * Execute command ourselves if we're here because the send failed (or
3722 * else we would have exited above).
3723 */
3724 if (parmp->serverStr != NULL)
3725 {
3726 char_u *p;
3727
3728 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3729 parmp->serverStr, &p));
3730 vim_free(p);
3731 }
3732}
3733
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003734 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003735cmdsrv_main(
3736 int *argc,
3737 char **argv,
3738 char_u *serverName_arg,
3739 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003740{
3741 char_u *res;
3742 int i;
3743 char_u *sname;
3744 int ret;
3745 int didone = FALSE;
3746 int exiterr = 0;
3747 char **newArgV = argv + 1;
3748 int newArgC = 1,
3749 Argc = *argc;
3750 int argtype;
3751#define ARGTYPE_OTHER 0
3752#define ARGTYPE_EDIT 1
3753#define ARGTYPE_EDIT_WAIT 2
3754#define ARGTYPE_SEND 3
3755 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003756 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003757# ifndef FEAT_X11
3758 HWND srv;
3759# else
3760 Window srv;
3761
3762 setup_term_clip();
3763# endif
3764
3765 sname = serverMakeName(serverName_arg, argv[0]);
3766 if (sname == NULL)
3767 return;
3768
3769 /*
3770 * Execute the command server related arguments and remove them
3771 * from the argc/argv array; We may have to return into main()
3772 */
3773 for (i = 1; i < Argc; i++)
3774 {
3775 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003776 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003777 {
3778 for (; i < *argc; i++)
3779 {
3780 *newArgV++ = argv[i];
3781 newArgC++;
3782 }
3783 break;
3784 }
3785
Bram Moolenaareb94e552006-03-11 21:35:11 +00003786 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003787 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003788 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3789 {
3790 char *p = argv[i] + 8;
3791
3792 argtype = ARGTYPE_EDIT;
3793 while (*p != NUL)
3794 {
3795 if (STRNICMP(p, "-wait", 5) == 0)
3796 {
3797 argtype = ARGTYPE_EDIT_WAIT;
3798 p += 5;
3799 }
3800 else if (STRNICMP(p, "-silent", 7) == 0)
3801 {
3802 silent = TRUE;
3803 p += 7;
3804 }
3805 else if (STRNICMP(p, "-tab", 4) == 0)
3806 {
3807 tabs = TRUE;
3808 p += 4;
3809 }
3810 else
3811 {
3812 argtype = ARGTYPE_OTHER;
3813 break;
3814 }
3815 }
3816 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003817 else
3818 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003819
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003820 if (argtype != ARGTYPE_OTHER)
3821 {
3822 if (i == *argc - 1)
3823 mainerr_arg_missing((char_u *)argv[i]);
3824 if (argtype == ARGTYPE_SEND)
3825 {
3826 *serverStr = (char_u *)argv[i + 1];
3827 i++;
3828 }
3829 else
3830 {
3831 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003832 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003833 if (*serverStr == NULL)
3834 {
3835 /* Probably out of memory, exit. */
3836 didone = TRUE;
3837 exiterr = 1;
3838 break;
3839 }
3840 Argc = i;
3841 }
3842# ifdef FEAT_X11
3843 if (xterm_dpy == NULL)
3844 {
3845 mch_errmsg(_("No display"));
3846 ret = -1;
3847 }
3848 else
3849 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003850 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003851# else
3852 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003853 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003854# endif
3855 if (ret < 0)
3856 {
3857 if (argtype == ARGTYPE_SEND)
3858 {
3859 /* Failed to send, abort. */
3860 mch_errmsg(_(": Send failed.\n"));
3861 didone = TRUE;
3862 exiterr = 1;
3863 }
3864 else if (!silent)
3865 /* Let vim start normally. */
3866 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3867 break;
3868 }
3869
3870# ifdef FEAT_GUI_W32
3871 /* Guess that when the server name starts with "g" it's a GUI
3872 * server, which we can bring to the foreground here.
3873 * Foreground() in the server doesn't work very well. */
3874 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3875 SetForegroundWindow(srv);
3876# endif
3877
3878 /*
3879 * For --remote-wait: Wait until the server did edit each
3880 * file. Also detect that the server no longer runs.
3881 */
3882 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3883 {
3884 int numFiles = *argc - i - 1;
3885 int j;
3886 char_u *done = alloc(numFiles);
3887 char_u *p;
3888# ifdef FEAT_GUI_W32
3889 NOTIFYICONDATA ni;
3890 int count = 0;
3891 extern HWND message_window;
3892# endif
3893
3894 if (numFiles > 0 && argv[i + 1][0] == '+')
3895 /* Skip "+cmd" argument, don't wait for it to be edited. */
3896 --numFiles;
3897
3898# ifdef FEAT_GUI_W32
3899 ni.cbSize = sizeof(ni);
3900 ni.hWnd = message_window;
3901 ni.uID = 0;
3902 ni.uFlags = NIF_ICON|NIF_TIP;
3903 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3904 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3905 Shell_NotifyIcon(NIM_ADD, &ni);
3906# endif
3907
3908 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003909 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003910 while (memchr(done, 0, numFiles) != NULL)
3911 {
3912# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003913 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003914 if (p == NULL)
3915 break;
3916# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003917 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003918 break;
3919# endif
3920 j = atoi((char *)p);
3921 if (j >= 0 && j < numFiles)
3922 {
3923# ifdef FEAT_GUI_W32
3924 ++count;
3925 sprintf(ni.szTip, _("%d of %d edited"),
3926 count, numFiles);
3927 Shell_NotifyIcon(NIM_MODIFY, &ni);
3928# endif
3929 done[j] = 1;
3930 }
3931 }
3932# ifdef FEAT_GUI_W32
3933 Shell_NotifyIcon(NIM_DELETE, &ni);
3934# endif
3935 }
3936 }
3937 else if (STRICMP(argv[i], "--remote-expr") == 0)
3938 {
3939 if (i == *argc - 1)
3940 mainerr_arg_missing((char_u *)argv[i]);
3941# ifdef WIN32
3942 /* Win32 always works? */
3943 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003944 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003945# else
3946 if (xterm_dpy == NULL)
3947 mch_errmsg(_("No display: Send expression failed.\n"));
3948 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003949 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003950# endif
3951 {
3952 if (res != NULL && *res != NUL)
3953 {
3954 /* Output error from remote */
3955 mch_errmsg((char *)res);
3956 vim_free(res);
3957 res = NULL;
3958 }
3959 mch_errmsg(_(": Send expression failed.\n"));
3960 }
3961 }
3962 else if (STRICMP(argv[i], "--serverlist") == 0)
3963 {
3964# ifdef WIN32
3965 /* Win32 always works? */
3966 res = serverGetVimNames();
3967# else
3968 if (xterm_dpy != NULL)
3969 res = serverGetVimNames(xterm_dpy);
3970# endif
3971 if (called_emsg)
3972 mch_errmsg("\n");
3973 }
3974 else if (STRICMP(argv[i], "--servername") == 0)
3975 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003976 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003977 i++;
3978 continue;
3979 }
3980 else
3981 {
3982 *newArgV++ = argv[i];
3983 newArgC++;
3984 continue;
3985 }
3986 didone = TRUE;
3987 if (res != NULL && *res != NUL)
3988 {
3989 mch_msg((char *)res);
3990 if (res[STRLEN(res) - 1] != '\n')
3991 mch_msg("\n");
3992 }
3993 vim_free(res);
3994 }
3995
3996 if (didone)
3997 {
3998 display_errors(); /* display any collected messages */
3999 exit(exiterr); /* Mission accomplished - get out */
4000 }
4001
4002 /* Return back into main() */
4003 *argc = newArgC;
4004 vim_free(sname);
4005}
4006
4007/*
4008 * Build a ":drop" command to send to a Vim server.
4009 */
4010 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004011build_drop_cmd(
4012 int filec,
4013 char **filev,
4014 int tabs, /* Use ":tab drop" instead of ":drop". */
4015 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004016{
4017 garray_T ga;
4018 int i;
4019 char_u *inicmd = NULL;
4020 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004021 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004022 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004023
4024 if (filec > 0 && filev[0][0] == '+')
4025 {
4026 inicmd = (char_u *)filev[0] + 1;
4027 filev++;
4028 filec--;
4029 }
4030 /* Check if we have at least one argument. */
4031 if (filec <= 0)
4032 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004033
4034 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004035 cwd = alloc(MAXPATHL);
4036 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004037 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004038 if (mch_dirname(cwd, MAXPATHL) != OK)
4039 {
4040 vim_free(cwd);
4041 return NULL;
4042 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004043 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004044#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004045 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004046#else
4047 PATH_ESC_CHARS,
4048#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004049 '\\', TRUE);
4050 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004051 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004052 return NULL;
4053 ga_init2(&ga, 1, 100);
4054 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004055 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004056
4057 /* Call inputsave() so that a prompt for an encryption key works. */
4058 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4059 if (tabs)
4060 ga_concat(&ga, (char_u *)"tab ");
4061 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004062 for (i = 0; i < filec; i++)
4063 {
4064 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004065 * do it again in the Vim server. On MS-Windows only escape
4066 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004067 p = vim_strsave_escaped((char_u *)filev[i],
4068#ifdef UNIX
4069 PATH_ESC_CHARS
4070#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004071 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004072#endif
4073 );
4074 if (p == NULL)
4075 {
4076 vim_free(ga.ga_data);
4077 return NULL;
4078 }
4079 ga_concat(&ga, (char_u *)" ");
4080 ga_concat(&ga, p);
4081 vim_free(p);
4082 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004083 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4084
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004085 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4086 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004087 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4088
4089 /* Switch back to the correct current directory (prior to temporary path
4090 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004091 * correct after the :drop command. With line breaks and spaces:
4092 * if !exists('+acd') || !&acd
4093 * if haslocaldir()
4094 * cd -
4095 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004096 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004097 * cd -
4098 * endif
4099 * endif
4100 */
4101 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004102 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004103 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004104 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004105 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004106
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004107 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004108 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4109 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004110 if (inicmd != NULL)
4111 {
4112 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4113 * the following commands to be inserted as text. Use a "|",
4114 * hopefully "inicmd" does allow this... */
4115 ga_concat(&ga, inicmd);
4116 ga_concat(&ga, (char_u *)"|");
4117 }
4118 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4119 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004120 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004121 ga_append(&ga, NUL);
4122 return ga.ga_data;
4123}
4124
4125/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004126 * Make our basic server name: use the specified "arg" if given, otherwise use
4127 * the tail of the command "cmd" we were started with.
4128 * Return the name in allocated memory. This doesn't include a serial number.
4129 */
4130 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004131serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004132{
4133 char_u *p;
4134
4135 if (arg != NULL && *arg != NUL)
4136 p = vim_strsave_up(arg);
4137 else
4138 {
4139 p = vim_strsave_up(gettail((char_u *)cmd));
4140 /* Remove .exe or .bat from the name. */
4141 if (p != NULL && vim_strchr(p, '.') != NULL)
4142 *vim_strchr(p, '.') = NUL;
4143 }
4144 return p;
4145}
4146#endif /* FEAT_CLIENTSERVER */
4147
4148#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4149/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004150 * Replace termcodes such as <CR> and insert as key presses if there is room.
4151 */
4152 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004153server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004154{
4155 char_u *ptr = NULL;
4156 char_u *cpo_save = p_cpo;
4157
4158 /* Set 'cpoptions' the way we want it.
4159 * B set - backslashes are *not* treated specially
4160 * k set - keycodes are *not* reverse-engineered
4161 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004162 * The last but one parameter of replace_termcodes() is TRUE so that the
4163 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004164 */
4165 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004166 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004167 p_cpo = cpo_save;
4168
4169 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4170 {
4171 /*
4172 * Add the string to the input stream.
4173 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4174 *
4175 * First clear typed characters from the typeahead buffer, there could
4176 * be half a mapping there. Then append to the existing string, so
4177 * that multiple commands from a client are concatenated.
4178 */
4179 if (typebuf.tb_maplen < typebuf.tb_len)
4180 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4181 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4182
4183 /* Let input_available() know we inserted text in the typeahead
4184 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004185 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004186 }
4187 vim_free((char_u *)ptr);
4188}
4189
4190/*
4191 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004192 */
4193 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004194eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004195{
4196 char_u *res;
4197 int save_dbl = debug_break_level;
4198 int save_ro = redir_off;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004199 void *fc;
4200
4201 /* Evaluate the expression at the toplevel, don't use variables local to
4202 * the calling function. */
4203 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004204
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004205 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4206 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004207 debug_break_level = -1;
4208 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004209 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4210 * to be typed. Do generate errors so that try/catch works. */
4211 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004212
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004213 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004214
4215 debug_break_level = save_dbl;
4216 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004217 --emsg_silent;
4218 if (emsg_silent < 0)
4219 emsg_silent = 0;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004220 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004221
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004222 /* A client can tell us to redraw, but not to display the cursor, so do
4223 * that here. */
4224 setcursor();
4225 out_flush();
4226#ifdef FEAT_GUI
4227 if (gui.in_use)
4228 gui_update_cursor(FALSE, FALSE);
4229#endif
4230
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004231 return res;
4232}
4233
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004234/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004235 * Evaluate a command or expression sent to ourselves.
4236 */
4237 int
4238sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4239{
4240 if (asExpr)
4241 {
4242 char_u *ret;
4243
4244 ret = eval_client_expr_to_string(cmd);
4245 if (result != NULL)
4246 {
4247 if (ret == NULL)
4248 {
4249 char *err = _(e_invexprmsg);
4250 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4251 char_u *msg;
4252
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004253 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004254 if (msg != NULL)
4255 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4256 *result = msg;
4257 }
4258 else
4259 *result = ret;
4260 }
4261 else
4262 vim_free(ret);
4263 return ret == NULL ? -1 : 0;
4264 }
4265 server_to_input_buf(cmd);
4266 return 0;
4267}
4268
4269/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004270 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4271 * return an allocated string. Otherwise return "data".
4272 * "*tofree" is set to the result when it needs to be freed later.
4273 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004274 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004275serverConvert(
4276 char_u *client_enc UNUSED,
4277 char_u *data,
4278 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004279{
4280 char_u *res = data;
4281
4282 *tofree = NULL;
4283# ifdef FEAT_MBYTE
4284 if (client_enc != NULL && p_enc != NULL)
4285 {
4286 vimconv_T vimconv;
4287
4288 vimconv.vc_type = CONV_NONE;
4289 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4290 && vimconv.vc_type != CONV_NONE)
4291 {
4292 res = string_convert(&vimconv, data, NULL);
4293 if (res == NULL)
4294 res = data;
4295 else
4296 *tofree = res;
4297 }
4298 convert_setup(&vimconv, NULL, NULL);
4299 }
4300# endif
4301 return res;
4302}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004303#endif