blob: 0283231b04cc4b232d321aec221dd41f31cb31a6 [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 Moolenaar92b8b2d2016-01-29 22:36:45 +010050static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010051static void exe_pre_commands(mparm_T *parmp);
52static void exe_commands(mparm_T *parmp);
53static void source_startup_scripts(mparm_T *parmp);
54static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010055# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010056static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010057# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +010058# ifdef FEAT_EVAL
59static void set_progpath(char_u *argv0);
60# endif
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010061# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010062static void exec_on_server(mparm_T *parmp);
63static void prepare_server(mparm_T *parmp);
64static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
65static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010066# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000067#endif
68
69
Bram Moolenaarb4210b32004-06-13 14:51:16 +000070/*
71 * Different types of error messages.
72 */
73static char *(main_errors[]) =
74{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000075 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000076#define ME_UNKNOWN_OPTION 0
77 N_("Too many edit arguments"),
78#define ME_TOO_MANY_ARGS 1
79 N_("Argument missing after"),
80#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000081 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000082#define ME_GARBAGE 3
83 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
84#define ME_EXTRA_CMD 4
85 N_("Invalid argument for"),
86#define ME_INVALID_ARG 5
87};
88
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010089#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaara6044292017-04-02 18:19:53 +020090
91/* Various parameters passed between main() and other functions. */
92static mparm_T params;
93
Bram Moolenaar8866d272012-11-28 15:55:42 +010094#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020095
96static char_u *start_dir = NULL; /* current working dir on startup */
97
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020098static int has_dash_c_arg = FALSE;
99
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100 int
101# ifdef VIMDLL
102_export
103# endif
104# ifdef FEAT_GUI_MSWIN
105# ifdef __BORLANDC__
106_cdecl
107# endif
108VimMain
109# else
110main
111# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100112(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000113{
Bram Moolenaar3f269672009-11-03 11:11:11 +0000114#ifdef STARTUPTIME
115 int i;
116#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000117
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118 /*
119 * Do any system-specific initialisations. These can NOT use IObuff or
120 * NameBuff. Thus emsg2() cannot be called!
121 */
122 mch_early_init();
123
Bram Moolenaar14993322014-09-09 12:25:33 +0200124#if defined(WIN32) && defined(FEAT_MBYTE)
125 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200126 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200127 * convert when 'encoding' changes. Get the unexpanded arguments.
128 */
129 argc = get_cmd_argsW(&argv);
130#endif
131
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000132 /* Many variables are in "params" so that we can pass them to invoked
133 * functions without a lot of arguments. "argc" and "argv" are also
134 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000135 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 params.argc = argc;
137 params.argv = argv;
138 params.want_full_screen = TRUE;
139#ifdef FEAT_EVAL
140 params.use_debug_break_level = -1;
141#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000143
Bram Moolenaar99685e62013-05-11 13:56:18 +0200144#ifdef FEAT_RUBY
145 {
146 int ruby_stack_start;
147 vim_ruby_init((void *)&ruby_stack_start);
148 }
149#endif
150
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000151#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000152 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000153#endif
154
155#ifdef MEM_PROFILE
156 atexit(vim_mem_profile_dump);
157#endif
158
159#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200160 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000161 for (i = 1; i < argc; ++i)
162 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000163 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000164 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000165 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000166 TIME_MSG("--- VIM STARTING ---");
167 break;
168 }
169 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000170#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000171 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000172
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200173 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174
175#ifdef FEAT_CLIENTSERVER
176 /*
177 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000178 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000179 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000180 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000181#endif
182
183 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000184 * Figure out the way to work from the command name argv[0].
185 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000186 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000187 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000188
189 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000190 * Process the command line arguments. File names are put in the global
191 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000192 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000194 TIME_MSG("parsing arguments");
195
196 /*
197 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000198 * there is no terminal version, and on Windows we can't fork one off with
199 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000200 */
201#ifdef ALWAYS_USE_GUI
202 gui.starting = TRUE;
203#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000204# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 /*
206 * Check if the GUI can be started. Reset gui.starting if not.
207 * Don't know about other systems, stay on the safe side and don't check.
208 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000209 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000211 if (gui_init_check() == FAIL)
212 {
213 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000215 /* When running "evim" or "gvim -y" we need the menus, exit if we
216 * don't have them. */
217 if (params.evim_mode)
218 mch_exit(1);
219 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000220 }
221# endif
222#endif
223
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 if (GARGCOUNT > 0)
225 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100226#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227 /*
228 * Expand wildcards in file names.
229 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000230 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200232 start_dir = alloc(MAXPATHL);
233 if (start_dir != NULL)
234 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235 /* Temporarily add '(' and ')' to 'isfname'. These are valid
236 * filename characters but are excluded from 'isfname' to make
237 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
238 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000239 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200241 if (start_dir != NULL)
242 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243 }
244#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200245 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000247
248#if defined(WIN32) && defined(FEAT_MBYTE)
249 {
250 extern void set_alist_count(void);
251
252 /* Remember the number of entries in the argument list. If it changes
253 * we don't react on setting 'encoding'. */
254 set_alist_count();
255 }
256#endif
257
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000258#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000259 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000260 {
261 /*
262 * If there is one filename, fully qualified, we have very probably
263 * been invoked from explorer, so change to the file's directory.
264 * Hint: to avoid this when typing a command use a forward slash.
265 * If the cd fails, it doesn't matter.
266 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200267 (void)vim_chdirfile(params.fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200268 if (start_dir != NULL)
269 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 }
271#endif
272 TIME_MSG("expanding arguments");
273
274#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000275 if (params.diff_mode && params.window_count == -1)
276 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277#endif
278
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000279 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280 ++RedrawingDisabled;
281
282 /*
283 * When listing swap file names, don't do cursor positioning et. al.
284 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200285 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287
288 /*
289 * When certain to start the GUI, don't check capabilities of terminal.
290 * For GTK we can't be sure, but when started from the desktop it doesn't
291 * make sense to try using a terminal.
292 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000293#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000294 if (gui.starting
295# ifdef FEAT_GUI_GTK
296 && !isatty(2)
297# endif
298 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000299 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300#endif
301
302#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
303 /* When the GUI is started from Finder, need to display messages in a
304 * message box. isatty(2) returns TRUE anyway, thus we need to check the
305 * name to know we're not started from a terminal. */
306 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000307 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000308 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000309
310 /* Avoid always using "/" as the current directory. Note that when
311 * started from Finder the arglist will be filled later in
312 * HandleODocAE() and "fname" will be NULL. */
313 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
314 && STRCMP(NameBuff, "/") == 0)
315 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200316 if (params.fname != NULL)
317 (void)vim_chdirfile(params.fname);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000318 else
319 {
320 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
321 vim_chdir(NameBuff);
322 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200323 if (start_dir != NULL)
324 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000325 }
326 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000327#endif
328
329 /*
330 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100331 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000332 * Note that we may use mch_exit() before mch_init()!
333 */
334 mch_init();
335 TIME_MSG("shell init");
336
337#ifdef USE_XSMP
338 /*
339 * For want of anywhere else to do it, try to connect to xsmp here.
340 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
341 * Hijacking -X 'no X connection' to also disable XSMP connection as that
342 * has a similar delay upon failure.
343 * Only try if SESSION_MANAGER is set to something non-null.
344 */
345 if (!x_no_connect)
346 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000347 char *p = getenv("SESSION_MANAGER");
348
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000349 if (p != NULL && *p != NUL)
350 {
351 xsmp_init();
352 TIME_MSG("xsmp init");
353 }
354 }
355#endif
356
357 /*
358 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000360 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000361
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000362 /* This message comes before term inits, but after setting "silent_mode"
363 * when the input is not a tty. */
364 if (GARGCOUNT > 1 && !silent_mode)
365 printf(_("%d files to edit\n"), GARGCOUNT);
366
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000368 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000369 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370 capabilities (will set full_screen) */
371 screen_start(); /* don't know where cursor is now */
372 TIME_MSG("Termcap init");
373 }
374
375 /*
376 * Set the default values for the options that use Rows and Columns.
377 */
378 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000379 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380#ifdef FEAT_DIFF
381 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
382 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000383 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 diff_win_options(firstwin, FALSE);
385#endif
386
387 cmdline_row = Rows - p_ch;
388 msg_row = cmdline_row;
389 screenalloc(FALSE); /* allocate screen buffers */
390 set_init_2();
391 TIME_MSG("inits 2");
392
393 msg_scroll = TRUE;
394 no_wait_return = TRUE;
395
396 init_mappings(); /* set up initial mappings */
397
398 init_highlight(TRUE, FALSE); /* set the default highlight groups */
399 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400
401#ifdef FEAT_EVAL
402 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000403 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404#endif
405
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200406 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
407 * Allows for setting 'loadplugins' there. */
408 if (params.use_vimrc != NULL
409 && (STRCMP(params.use_vimrc, "NONE") == 0
410 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
411 p_lpl = FALSE;
412
413 /* Execute --cmd arguments. */
414 exe_pre_commands(&params);
415
416 /* Source startup scripts. */
417 source_startup_scripts(&params);
418
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100419#ifdef FEAT_MZSCHEME
420 /*
421 * Newer version of MzScheme (Racket) require earlier (trampolined)
422 * initialisation via scheme_main_setup.
423 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200424 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200425 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100426 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200427 return mzscheme_main();
428#else
429 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100430#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200431}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100432#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200433#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100434
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200435/*
436 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
437 * things simple.
438 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
439 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100440 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200441vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100442{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100443#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444#ifdef FEAT_EVAL
445 /*
446 * Read all the plugin files.
447 * Only when compiled with +eval, since most plugins need it.
448 */
449 if (p_lpl)
450 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200451 char_u *rtp_copy = NULL;
452
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200453 /* First add all package directories to 'runtimepath', so that their
454 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200455 * :packloadall command.
456 * Make a copy of 'runtimepath', so that source_runtime does not use
457 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200458 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200459 {
460 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200461 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200463
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200464 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000465# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200466 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000467# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200468 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000469# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200470 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000471 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200472 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100473
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200474 /* Only source "start" packages if not done already with a :packloadall
475 * command. */
476 if (!did_source_packages)
477 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100478 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200479
480# ifdef VMS /* Somehow VMS doesn't handle the "**". */
481 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
482# else
483 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
484# endif
485 TIME_MSG("loading after plugins");
486
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000487 }
488#endif
489
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000490#ifdef FEAT_DIFF
491 /* Decide about window layout for diff mode after reading vimrc. */
492 if (params.diff_mode && params.window_layout == 0)
493 {
494 if (diffopt_horizontal())
495 params.window_layout = WIN_HOR; /* use horizontal split */
496 else
497 params.window_layout = WIN_VER; /* use vertical split */
498 }
499#endif
500
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 /*
502 * Recovery mode without a file name: List swap files.
503 * This uses the 'dir' option, therefore it must be after the
504 * initializations.
505 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200506 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200508 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000509 mch_exit(0);
510 }
511
512 /*
513 * Set a few option defaults after reading .vimrc files:
514 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
515 */
516 set_init_3();
517 TIME_MSG("inits 3");
518
519 /*
520 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
521 * Note that this overrides anything from a vimrc file.
522 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000523 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000524 p_uc = 0;
525
526#ifdef FEAT_FKMAP
527 if (curwin->w_p_rl && p_altkeymap)
528 {
529 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
530# ifdef FEAT_ARABIC
531 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
532# endif
533 p_fkmap = TRUE; /* Set the Farsi keymap mode */
534 }
535#endif
536
537#ifdef FEAT_GUI
538 if (gui.starting)
539 {
540#if defined(UNIX) || defined(VMS)
541 /* When something caused a message from a vimrc script, need to output
542 * an extra newline before the shell prompt. */
543 if (did_emsg || msg_didout)
544 putchar('\n');
545#endif
546
547 gui_start(); /* will set full_screen to TRUE */
548 TIME_MSG("starting GUI");
549
550 /* When running "evim" or "gvim -y" we need the menus, exit if we
551 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000552 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000553 mch_exit(1);
554 }
555#endif
556
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557#ifdef FEAT_VIMINFO
558 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000559 * Read in registers, history etc, but not marks, from the viminfo file.
560 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561 */
562 if (*p_viminfo != NUL)
563 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000564 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000565 TIME_MSG("reading viminfo");
566 }
567#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100568#ifdef FEAT_EVAL
569 /* It's better to make v:oldfiles an empty list than NULL. */
570 if (get_vim_var_list(VV_OLDFILES) == NULL)
571 set_vim_var_list(VV_OLDFILES, list_alloc());
572#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000573
574#ifdef FEAT_QUICKFIX
575 /*
576 * "-q errorfile": Load the error file now.
577 * If the error file can't be read, exit before doing anything else.
578 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000579 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000580 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100581 char_u *enc = NULL;
582
583# ifdef FEAT_MBYTE
584 enc = p_menc;
585# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000586 if (params.use_ef != NULL)
587 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000588 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200589 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100590 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000591 {
592 out_char('\n');
593 mch_exit(3);
594 }
595 TIME_MSG("reading errorfile");
596 }
597#endif
598
599 /*
600 * Start putting things on the screen.
601 * Scroll screen down before drawing over it
602 * Clear screen now, so file message will not be cleared.
603 */
604 starting = NO_BUFFERS;
605 no_wait_return = FALSE;
606 if (!exmode_active)
607 msg_scroll = FALSE;
608
609#ifdef FEAT_GUI
610 /*
611 * This seems to be required to make callbacks to be called now, instead
612 * of after things have been put on the screen, which then may be deleted
613 * when getting a resize callback.
614 * For the Mac this handles putting files dropped on the Vim icon to
615 * global_alist.
616 */
617 if (gui.in_use)
618 {
619# ifdef FEAT_SUN_WORKSHOP
620 if (!usingSunWorkShop)
621# endif
622 gui_wait_for_chars(50L);
623 TIME_MSG("GUI delay");
624 }
625#endif
626
627#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
628 qnx_clip_init();
629#endif
630
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200631#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
632 clip_init(TRUE);
633#endif
634
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635#ifdef FEAT_XCLIPBOARD
636 /* Start using the X clipboard, unless the GUI was started. */
637# ifdef FEAT_GUI
638 if (!gui.in_use)
639# endif
640 {
641 setup_term_clip();
642 TIME_MSG("setup clipboard");
643 }
644#endif
645
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000646#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000647 /* Prepare for being a Vim server. */
648 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000649#endif
650
651 /*
652 * If "-" argument given: Read file from stdin.
653 * Do this before starting Raw mode, because it may change things that the
654 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
655 * are the same terminal: "cat | vim -".
656 * Using autocommands here may cause trouble...
657 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000658 if (params.edit_type == EDIT_STDIN && !recoverymode)
659 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000660
661#if defined(UNIX) || defined(VMS)
662 /* When switching screens and something caused a message from a vimrc
663 * script, need to output an extra newline on exit. */
664 if ((did_emsg || msg_didout) && *T_TI != NUL)
665 newline_on_exit = TRUE;
666#endif
667
668 /*
669 * When done something that is not allowed or error message call
670 * wait_return. This must be done before starttermcap(), because it may
671 * switch to another screen. It must be done after settmode(TMODE_RAW),
672 * because we want to react on a single key stroke.
673 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000674 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000675 */
676 settmode(TMODE_RAW);
677 TIME_MSG("setting raw mode");
678
679 if (need_wait_return || msg_didany)
680 {
681 wait_return(TRUE);
682 TIME_MSG("waiting for return");
683 }
684
685 starttermcap(); /* start termcap if not done by wait_return() */
686 TIME_MSG("start termcap");
687
688#ifdef FEAT_MOUSE
689 setmouse(); /* may start using the mouse */
690#endif
691 if (scroll_region)
692 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000693 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000694
695 /*
696 * Don't clear the screen when starting in Ex mode, unless using the GUI.
697 */
698 if (exmode_active
699#ifdef FEAT_GUI
700 && !gui.in_use
701#endif
702 )
703 must_redraw = CLEAR;
704 else
705 {
706 screenclear(); /* clear screen */
707 TIME_MSG("clearing screen");
708 }
709
710#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000711 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100713 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200714 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 TIME_MSG("getting crypt key");
716 }
717#endif
718
719 no_wait_return = TRUE;
720
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000722 * Create the requested number of windows and edit buffers in them.
723 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000725 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726 TIME_MSG("opening buffers");
727
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000728#ifdef FEAT_EVAL
729 /* clear v:swapcommand */
730 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
731#endif
732
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000733 /* Ex starts at last line of the file */
734 if (exmode_active)
735 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
736
737#ifdef FEAT_AUTOCMD
738 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
739 TIME_MSG("BufEnter autocommands");
740#endif
741 setpcmark();
742
743#ifdef FEAT_QUICKFIX
744 /*
745 * When started with "-q errorfile" jump to first error now.
746 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000747 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000748 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000749 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000750 TIME_MSG("jump to first error");
751 }
752#endif
753
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 /*
755 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000756 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000757 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200758 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200759 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000760
761#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000762 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000763 {
764 win_T *wp;
765
766 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200767 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 diff_win_options(wp, TRUE);
769 }
770#endif
771
772 /*
773 * Shorten any of the filenames, but only when absolute.
774 */
775 shorten_fnames(FALSE);
776
777 /*
778 * Need to jump to the tag before executing the '-c command'.
779 * Makes "vim -c '/return' -t main" work.
780 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000781 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000783#if defined(HAS_SWAP_EXISTS_ACTION)
784 swap_exists_did_quit = FALSE;
785#endif
786
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000787 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000788 do_cmdline_cmd(IObuff);
789 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000790
791#if defined(HAS_SWAP_EXISTS_ACTION)
792 /* If the user doesn't want to edit the file then we quit here. */
793 if (swap_exists_did_quit)
794 getout(1);
795#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796 }
797
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000798 /* Execute any "+", "-c" and "-S" arguments. */
799 if (params.n_commands > 0)
800 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200802 /* Must come before the may_req_ calls. */
803 starting = 0;
804
Bram Moolenaar976787d2017-06-04 15:45:50 +0200805#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
806 /* Must be done before redrawing, puts a few characters on the screen. */
807 may_req_ambiguous_char_width();
808#endif
809
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000810 RedrawingDisabled = 0;
811 redraw_all_later(NOT_VALID);
812 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200814 /* 'autochdir' has been postponed */
815 DO_AUTOCHDIR
816
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000817#ifdef FEAT_TERMRESPONSE
818 /* Requesting the termresponse is postponed until here, so that a "-c q"
819 * argument doesn't make it appear in the shell Vim was started from. */
820 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200821
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200822 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000823#endif
824
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825 /* start in insert mode */
826 if (p_im)
827 need_start_insertmode = TRUE;
828
Bram Moolenaar14735512016-03-26 21:00:08 +0100829#ifdef FEAT_EVAL
830 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
831#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000832#ifdef FEAT_AUTOCMD
833 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
834 TIME_MSG("VimEnter autocommands");
835#endif
836
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200837#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
838 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
839 * done after the clipboard is available and all initial commands that may
840 * modify the 'clipboard' setting have run; i.e. just before entering the
841 * main loop. */
842 {
843 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100844
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200845 adjust_clip_reg(&default_regname);
846 set_reg_var(default_regname);
847 }
848#endif
849
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
851 /* When a startup script or session file setup for diff'ing and
852 * scrollbind, sync the scrollbind now. */
853 if (curwin->w_p_diff && curwin->w_p_scb)
854 {
855 update_topline();
856 check_scrollbind((linenr_T)0, 0L);
857 TIME_MSG("diff scrollbinding");
858 }
859#endif
860
861#if defined(WIN3264) && !defined(FEAT_GUI_W32)
862 mch_set_winsize_now(); /* Allow winsize changes from now on */
863#endif
864
Bram Moolenaar4033c552017-09-16 20:54:51 +0200865#if defined(FEAT_GUI)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000866 /* When tab pages were created, may need to update the tab pages line and
867 * scrollbars. This is skipped while creating them. */
868 if (first_tabpage->tp_next != NULL)
869 {
870 out_flush();
871 gui_init_which_components(NULL);
872 gui_update_scrollbars(TRUE);
873 }
874 need_mouse_correct = TRUE;
875#endif
876
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877 /* If ":startinsert" command used, stuff a dummy command to be able to
878 * call normal_cmd(), which will then start Insert mode. */
879 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000880 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881
882#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200883 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200884 {
885# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200886# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200887 && !defined(FEAT_GUI_W32)
888 if (gui.in_use)
889 {
890 mch_errmsg(_("netbeans is not supported with this GUI\n"));
891 mch_exit(2);
892 }
893# endif
894# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000895 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200896 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200897 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000898#endif
899
900 TIME_MSG("before starting main loop");
901
902 /*
903 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100904 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000905 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000906
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200907#endif /* NO_VIM_MAIN */
908
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000909 return 0;
910}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000911
912/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200913 * Initialisation shared by main() and some tests.
914 */
915 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200916common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200917{
918
919#ifdef FEAT_MBYTE
920 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
921#endif
922#ifdef FEAT_EVAL
923 eval_init(); /* init global variables */
924#endif
925
926#ifdef __QNXNTO__
927 qnx_init(); /* PhAttach() for clipboard, (and gui) */
928#endif
929
930#ifdef MAC_OS_CLASSIC
931 /* Prepare for possibly starting GUI sometime */
932 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200933 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200934 TIME_MSG("GUI prepared");
935#endif
936
937 /* Init the table of Normal mode commands. */
938 init_normal_cmds();
939
940#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
941 make_version(); /* Construct the long version string. */
942#endif
943
944 /*
945 * Allocate space for the generic buffers (needed for set_init_1() and
946 * EMSG2()).
947 */
948 if ((IObuff = alloc(IOSIZE)) == NULL
949 || (NameBuff = alloc(MAXPATHL)) == NULL)
950 mch_exit(0);
951 TIME_MSG("Allocated generic buffers");
952
953#ifdef NBDEBUG
954 /* Wait a moment for debugging NetBeans. Must be after allocating
955 * NameBuff. */
956 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
957 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
958 TIME_MSG("NetBeans debug wait");
959#endif
960
961#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
962 /*
963 * Setup to use the current locale (for ctype() and many other things).
964 * NOTE: Translated messages with encodings other than latin1 will not
965 * work until set_init_1() has been called!
966 */
967 init_locale();
968 TIME_MSG("locale set");
969#endif
970
971#ifdef FEAT_GUI
972 gui.dofork = TRUE; /* default is to use fork() */
973#endif
974
975 /*
976 * Do a first scan of the arguments in "argv[]":
977 * -display or --display
978 * --server...
979 * --socketid
980 * --windowid
981 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200982 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200983
984#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200985 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200986#endif
987#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
988 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200989 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200990 TIME_MSG("GUI prepared");
991#endif
992
993#ifdef FEAT_CLIPBOARD
994 clip_init(FALSE); /* Initialise clipboard stuff */
995 TIME_MSG("clipboard setup");
996#endif
997
998 /*
999 * Check if we have an interactive window.
1000 * On the Amiga: If there is no window, we open one with a newcli command
1001 * (needed for :! to * work). mch_check_win() will also handle the -d or
1002 * -dev argument.
1003 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001004 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001005 TIME_MSG("window checked");
1006
1007 /*
1008 * Allocate the first window and buffer.
1009 * Can't do anything without it, exit when it fails.
1010 */
1011 if (win_alloc_first() == FAIL)
1012 mch_exit(0);
1013
1014 init_yank(); /* init yank buffers */
1015
1016 alist_init(&global_alist); /* Init the argument list to empty. */
1017 global_alist.id = 0;
1018
1019 /*
1020 * Set the default values for the options.
1021 * NOTE: Non-latin1 translated messages are working only after this,
1022 * because this is where "has_mbyte" will be set, which is used by
1023 * msg_outtrans_len_attr().
1024 * First find out the home directory, needed to expand "~" in options.
1025 */
1026 init_homedir(); /* find real value of $HOME */
1027 set_init_1();
1028 TIME_MSG("inits 1");
1029
1030#ifdef FEAT_EVAL
1031 set_lang_var(); /* set v:lang and v:ctype */
1032#endif
1033}
1034
1035/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001036 * Return TRUE when the --not-a-term argument was found.
1037 */
1038 int
1039is_not_a_term()
1040{
1041 return params.not_a_term;
1042}
1043
1044/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001045 * Main loop: Execute Normal mode commands until exiting Vim.
1046 * Also used to handle commands in the command-line window, until the window
1047 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001048 * Also used to handle ":visual" command after ":global": execute Normal mode
1049 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001050 */
1051 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001052main_loop(
1053 int cmdwin, /* TRUE when working in the command-line window */
1054 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001055{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001056 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001057 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001058#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001059 /* these are static to avoid a compiler warning */
1060 static linenr_T conceal_old_cursor_line = 0;
1061 static linenr_T conceal_new_cursor_line = 0;
1062 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001063#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001064
1065#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1066 /* Setup to catch a terminating error from the X server. Just ignore
1067 * it, restore the state and continue. This might not always work
1068 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001069 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001070 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001071 {
1072 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001073 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001074 got_int = TRUE;
1075 need_wait_return = FALSE;
1076 global_busy = FALSE;
1077 exmode_active = 0;
1078 skip_redraw = FALSE;
1079 RedrawingDisabled = 0;
1080 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001081 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001082# ifdef FEAT_EVAL
1083 emsg_skip = 0;
1084# endif
1085 emsg_off = 0;
1086# ifdef FEAT_MOUSE
1087 setmouse();
1088# endif
1089 settmode(TMODE_RAW);
1090 starttermcap();
1091 scroll_start();
1092 redraw_later_clear();
1093 }
1094#endif
1095
1096 clear_oparg(&oa);
1097 while (!cmdwin
1098#ifdef FEAT_CMDWIN
1099 || cmdwin_result == 0
1100#endif
1101 )
1102 {
1103 if (stuff_empty())
1104 {
1105 did_check_timestamps = FALSE;
1106 if (need_check_timestamps)
1107 check_timestamps(FALSE);
1108 if (need_wait_return) /* if wait_return still needed ... */
1109 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001110 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001111 {
1112 need_start_insertmode = FALSE;
1113 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1114 /* skip the fileinfo message now, because it would be shown
1115 * after insert mode finishes! */
1116 need_fileinfo = FALSE;
1117 }
1118 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001119
1120 /* Reset "got_int" now that we got back to the main loop. Except when
1121 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1122 * the ":g" command.
1123 * For ":g/pat/vi" we reset "got_int" when used once. When used
1124 * a second time we go back to Ex mode and abort the ":g" command. */
1125 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001126 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001127 if (noexmode && global_busy && !exmode_active && previous_got_int)
1128 {
1129 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1130 * used and keep "got_int" set, so that it aborts ":g". */
1131 exmode_active = EXMODE_NORMAL;
1132 State = NORMAL;
1133 }
1134 else if (!global_busy || !exmode_active)
1135 {
1136 if (!quit_more)
1137 (void)vgetc(); /* flush all buffers */
1138 got_int = FALSE;
1139 }
1140 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001141 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001142 else
1143 previous_got_int = FALSE;
1144
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001145 if (!exmode_active)
1146 msg_scroll = FALSE;
1147 quit_more = FALSE;
1148
1149 /*
1150 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1151 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1152 * update cursor and redraw.
1153 */
1154 if (skip_redraw || exmode_active)
1155 skip_redraw = FALSE;
1156 else if (do_redraw || stuff_empty())
1157 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001158# ifdef FEAT_GUI
1159 /* If ui_breakcheck() was used a resize may have been postponed. */
1160 gui_may_resize_shell();
1161# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001162#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001163 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001164 if (!finish_op && (
1165# ifdef FEAT_AUTOCMD
1166 has_cursormoved()
1167# endif
1168# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1169 ||
1170# endif
1171# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001172 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001173# endif
1174 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001175# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001176 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001177# endif
1178 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001179 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001180# ifdef FEAT_AUTOCMD
1181 if (has_cursormoved())
1182 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1183 FALSE, curbuf);
1184# endif
1185# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001186 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001187 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001188# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001189 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001190# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001191 conceal_new_cursor_line = curwin->w_cursor.lnum;
1192 conceal_update_lines = TRUE;
1193 }
1194# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001195# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001196 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001197# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001198 }
1199#endif
1200
Bram Moolenaar186628f2013-03-19 13:33:23 +01001201#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001202 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001203 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001204 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001205 {
1206 if (last_changedtick_buf == curbuf)
1207 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1208 FALSE, curbuf);
1209 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001210 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001211 }
1212#endif
1213
Bram Moolenaar33aec762006-01-22 23:30:12 +00001214#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1215 /* Scroll-binding for diff mode may have been postponed until
1216 * here. Avoids doing it for every change. */
1217 if (diff_need_scrollbind)
1218 {
1219 check_scrollbind((linenr_T)0, 0L);
1220 diff_need_scrollbind = FALSE;
1221 }
1222#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001223#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001224 /* Include a closed fold completely in the Visual area. */
1225 foldAdjustVisual();
1226#endif
1227#ifdef FEAT_FOLDING
1228 /*
1229 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1230 * contain the cursor.
1231 * When 'foldopen' is "all", open the fold(s) under the cursor.
1232 * This may mark the window for redrawing.
1233 */
1234 if (hasAnyFolding(curwin) && !char_avail())
1235 {
1236 foldCheckClose();
1237 if (fdo_flags & FDO_ALL)
1238 foldOpenCursor();
1239 }
1240#endif
1241
1242 /*
1243 * Before redrawing, make sure w_topline is correct, and w_leftcol
1244 * if lines don't wrap, and w_skipcol if lines wrap.
1245 */
1246 update_topline();
1247 validate_cursor();
1248
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001249 if (VIsual_active)
1250 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001251 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001252 update_screen(0);
1253 else if (redraw_cmdline || clear_cmdline)
1254 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001255 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001256#ifdef FEAT_TITLE
1257 if (need_maketitle)
1258 maketitle();
1259#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001260#ifdef FEAT_VIMINFO
1261 curbuf->b_last_used = vim_time();
1262#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001263 /* display message after redraw */
1264 if (keep_msg != NULL)
1265 {
1266 char_u *p;
1267
1268 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001269 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1270 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001271 p = keep_msg;
1272 msg_attr(p, keep_msg_attr);
1273 vim_free(p);
1274 }
1275 if (need_fileinfo) /* show file info after redraw */
1276 {
1277 fileinfo(FALSE, TRUE, FALSE);
1278 need_fileinfo = FALSE;
1279 }
1280
1281 emsg_on_display = FALSE; /* can delete error message now */
1282 did_emsg = FALSE;
1283 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001284 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001285 showruler(FALSE);
1286
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001287# if defined(FEAT_CONCEAL)
1288 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001289 && (conceal_old_cursor_line != conceal_new_cursor_line
1290 || conceal_cursor_line(curwin)
1291 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001292 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001293 if (conceal_old_cursor_line != conceal_new_cursor_line
1294 && conceal_old_cursor_line
1295 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001296 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001297 update_single_line(curwin, conceal_new_cursor_line);
1298 curwin->w_valid &= ~VALID_CROW;
1299 }
1300# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001301 setcursor();
1302 cursor_on();
1303
1304 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001305
1306#ifdef STARTUPTIME
1307 /* Now that we have drawn the first screen all the startup stuff
1308 * has been done, close any file for startup messages. */
1309 if (time_fd != NULL)
1310 {
1311 TIME_MSG("first screen update");
1312 TIME_MSG("--- VIM STARTED ---");
1313 fclose(time_fd);
1314 time_fd = NULL;
1315 }
1316#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001317 }
1318#ifdef FEAT_GUI
1319 if (need_mouse_correct)
1320 gui_mouse_correct();
1321#endif
1322
1323 /*
1324 * Update w_curswant if w_set_curswant has been set.
1325 * Postponed until here to avoid computing w_virtcol too often.
1326 */
1327 update_curswant();
1328
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001329#ifdef FEAT_EVAL
1330 /*
1331 * May perform garbage collection when waiting for a character, but
1332 * only at the very toplevel. Otherwise we may be using a List or
1333 * Dict internally somewhere.
1334 * "may_garbage_collect" is reset in vgetc() which is invoked through
1335 * do_exmode() and normal_cmd().
1336 */
1337 may_garbage_collect = (!cmdwin && !noexmode);
1338#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001339 /*
1340 * If we're invoked as ex, do a round of ex commands.
1341 * Otherwise, get and execute a normal mode command.
1342 */
1343 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001344 {
1345 if (noexmode) /* End of ":global/path/visual" commands */
1346 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001347 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001348 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001349 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001350 {
1351#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001352 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001353 && oa.op_type == OP_NOP && oa.regname == NUL
1354 && !VIsual_active)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001355 {
1356 /* If terminal_loop() returns OK we got a key that is handled
Bram Moolenaar6d819742017-08-06 14:57:49 +02001357 * in Normal model. With FAIL we first need to position the
1358 * cursor and the screen needs to be redrawn. */
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001359 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001360 normal_cmd(&oa, TRUE);
1361 }
1362 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001363#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001364 normal_cmd(&oa, TRUE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001365 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001366 }
1367}
1368
1369
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001370#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001371/*
1372 * Exit, but leave behind swap files for modified buffers.
1373 */
1374 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001375getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001376{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001377# if defined(SIGHUP) && defined(SIG_IGN)
1378 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1379 * makes Vim exit and then handling SIGHUP causes various reentrance
1380 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001381 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001382# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001383
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001384 ml_close_notmod(); /* close all not-modified buffers */
1385 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1386 ml_close_all(FALSE); /* close all memfiles, without deleting */
1387 getout(exitval); /* exit Vim properly */
1388}
1389#endif
1390
1391
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001392/*
1393 * Exit properly.
1394 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001395 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001396getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001397{
1398#ifdef FEAT_AUTOCMD
1399 buf_T *buf;
1400 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001401 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001402#endif
1403
1404 exiting = TRUE;
1405
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001406 /* When running in Ex mode an error causes us to exit with a non-zero exit
1407 * code. POSIX requires this, although it's not 100% clear from the
1408 * standard. */
1409 if (exmode_active)
1410 exitval += ex_exitval;
1411
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001412 /* Position the cursor on the last screen line, below all the text */
1413#ifdef FEAT_GUI
1414 if (!gui.in_use)
1415#endif
1416 windgoto((int)Rows - 1, 0);
1417
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001418#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1419 /* Optionally print hashtable efficiency. */
1420 hash_debug_results();
1421#endif
1422
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423#ifdef FEAT_GUI
1424 msg_didany = FALSE;
1425#endif
1426
1427#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001428 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001429 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001430 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001431 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001432 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001434 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001436 if (wp->w_buffer == NULL)
1437 /* Autocmd must have close the buffer already, skip. */
1438 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001440 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001441 {
1442 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1443 buf->b_fname, FALSE, buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001444 CHANGEDTICK(buf) = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001445 /* start all over, autocommands may mess up the lists */
1446 next_tp = first_tabpage;
1447 break;
1448 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001449 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001451
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001452 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001453 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001454 if (buf->b_ml.ml_mfp != NULL)
1455 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001456 bufref_T bufref;
1457
1458 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001459 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001460 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001461 if (!bufref_valid(&bufref))
1462 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001463 break;
1464 }
1465 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1466 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001467#endif
1468
1469#ifdef FEAT_VIMINFO
1470 if (*p_viminfo != NUL)
1471 /* Write out the registers, history, marks etc, to the viminfo file */
1472 write_viminfo(NULL, FALSE);
1473#endif
1474
1475#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001476 if (get_vim_var_nr(VV_DYING) <= 1)
1477 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001478#endif
1479
Bram Moolenaar05159a02005-02-26 23:04:13 +00001480#ifdef FEAT_PROFILE
1481 profile_dump();
1482#endif
1483
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001484 if (did_emsg
1485#ifdef FEAT_GUI
1486 || (gui.in_use && msg_didany && p_verbose > 0)
1487#endif
1488 )
1489 {
1490 /* give the user a chance to read the (error) message */
1491 no_wait_return = FALSE;
1492 wait_return(FALSE);
1493 }
1494
1495#ifdef FEAT_AUTOCMD
1496 /* Position the cursor again, the autocommands may have moved it */
1497# ifdef FEAT_GUI
1498 if (!gui.in_use)
1499# endif
1500 windgoto((int)Rows - 1, 0);
1501#endif
1502
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001504 job_stop_on_exit();
1505#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001506#ifdef FEAT_LUA
1507 lua_end();
1508#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001509#ifdef FEAT_MZSCHEME
1510 mzscheme_end();
1511#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001512#ifdef FEAT_TCL
1513 tcl_end();
1514#endif
1515#ifdef FEAT_RUBY
1516 ruby_end();
1517#endif
1518#ifdef FEAT_PYTHON
1519 python_end();
1520#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001521#ifdef FEAT_PYTHON3
1522 python3_end();
1523#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001524#ifdef FEAT_PERL
1525 perl_end();
1526#endif
1527#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1528 iconv_end();
1529#endif
1530#ifdef FEAT_NETBEANS_INTG
1531 netbeans_end();
1532#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001533#ifdef FEAT_CSCOPE
1534 cs_end();
1535#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001536#ifdef FEAT_EVAL
1537 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001538 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001539#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001540#if defined(WIN32) && defined(FEAT_MBYTE)
1541 free_cmd_argsW();
1542#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001543
1544 mch_exit(exitval);
1545}
1546
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001547#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1548/*
1549 * Setup to use the current locale (for ctype() and many other things).
1550 */
1551 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001552init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001553{
1554 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001556# ifdef FEAT_GUI_GTK
1557 /* Tell Gtk not to change our locale settings. */
1558 gtk_disable_setlocale();
1559# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001560# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1561 /* Make sure strtod() uses a decimal point, not a comma. */
1562 setlocale(LC_NUMERIC, "C");
1563# endif
1564
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001565# ifdef WIN32
1566 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1567 * text while it's expecting text in the current locale. This call avoids
1568 * that. */
1569 setlocale(LC_CTYPE, "C");
1570# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001571
1572# ifdef FEAT_GETTEXT
1573 {
1574 int mustfree = FALSE;
1575 char_u *p;
1576
1577# ifdef DYNAMIC_GETTEXT
1578 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001579 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001580# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001581 /* expand_env() doesn't work yet, because g_chartab[] is not
1582 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001583 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1584 if (p != NULL && *p != NUL)
1585 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001586 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001587 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1588 }
1589 if (mustfree)
1590 vim_free(p);
1591 textdomain(VIMPACKAGE);
1592 }
1593# endif
1594}
1595#endif
1596
1597/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001598 * Get the name of the display, before gui_prepare() removes it from
1599 * argv[]. Used for the xterm-clipboard display.
1600 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001601 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001603 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001604early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001605{
Bram Moolenaar03005972008-11-20 13:12:36 +00001606#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1607 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001608 int argc = parmp->argc;
1609 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001610 int i;
1611
1612 for (i = 1; i < argc; i++)
1613 {
1614 if (STRCMP(argv[i], "--") == 0)
1615 break;
1616# ifdef FEAT_XCLIPBOARD
1617 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001618# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001619 || STRICMP(argv[i], "--display") == 0
1620# endif
1621 )
1622 {
1623 if (i == argc - 1)
1624 mainerr_arg_missing((char_u *)argv[i]);
1625 xterm_display = argv[++i];
1626 }
1627# endif
1628# ifdef FEAT_CLIENTSERVER
1629 else if (STRICMP(argv[i], "--servername") == 0)
1630 {
1631 if (i == argc - 1)
1632 mainerr_arg_missing((char_u *)argv[i]);
1633 parmp->serverName_arg = (char_u *)argv[++i];
1634 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001635 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001636 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001637 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001638 {
1639 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001640# ifdef FEAT_GUI
1641 if (strstr(argv[i], "-wait") != 0)
1642 /* don't fork() when starting the GUI to edit files ourself */
1643 gui.dofork = FALSE;
1644# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001645 }
1646# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001647
1648# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1649# ifdef FEAT_GUI_W32
1650 else if (STRICMP(argv[i], "--windowid") == 0)
1651# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001652 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001653# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001654 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001655 long_u id;
1656 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001657
1658 if (i == argc - 1)
1659 mainerr_arg_missing((char_u *)argv[i]);
1660 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001661 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001662 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001663 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001664 if (count != 1)
1665 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1666 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001667# ifdef FEAT_GUI_W32
1668 win_socket_id = id;
1669# else
1670 gtk_socket_id = id;
1671# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001672 i++;
1673 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001674# endif
1675# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001676 else if (STRICMP(argv[i], "--echo-wid") == 0)
1677 echo_wid_arg = TRUE;
1678# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001679# ifndef FEAT_NETBEANS_INTG
1680 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001681 {
1682 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1683 mch_exit(2);
1684 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001685# endif
1686
Bram Moolenaar58d98232005-07-23 22:25:46 +00001687 }
1688#endif
1689}
1690
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001691#ifndef NO_VIM_MAIN
1692/*
1693 * Get a (optional) count for a Vim argument.
1694 */
1695 static int
1696get_number_arg(
1697 char_u *p, /* pointer to argument */
1698 int *idx, /* index in argument, is incremented */
1699 int def) /* default value */
1700{
1701 if (vim_isdigit(p[*idx]))
1702 {
1703 def = atoi((char *)&(p[*idx]));
1704 while (vim_isdigit(p[*idx]))
1705 *idx = *idx + 1;
1706 }
1707 return def;
1708}
1709
1710/*
1711 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1712 * If the executable name starts with "r" we disable shell commands.
1713 * If the next character is "e" we run in Easy mode.
1714 * If the next character is "g" we run the GUI version.
1715 * If the next characters are "view" we start in readonly mode.
1716 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1717 * If the next characters are "ex" we start in Ex mode. If it's followed
1718 * by "im" use improved Ex mode.
1719 */
1720 static void
1721parse_command_name(mparm_T *parmp)
1722{
1723 char_u *initstr;
1724
1725 initstr = gettail((char_u *)parmp->argv[0]);
1726
1727#ifdef MACOS_X_UNIX
1728 /* An issue has been seen when launching Vim in such a way that
1729 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1730 * executable or a symbolic link of it. Until this issue is resolved
1731 * we prohibit the GUI from being used.
1732 */
1733 if (STRCMP(initstr, parmp->argv[0]) == 0)
1734 disallow_gui = TRUE;
1735
1736 /* TODO: On MacOS X default to gui if argv[0] ends in:
1737 * /Vim.app/Contents/MacOS/Vim */
1738#endif
1739
1740#ifdef FEAT_EVAL
1741 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001742 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001743#endif
1744
1745 if (TOLOWER_ASC(initstr[0]) == 'r')
1746 {
1747 restricted = TRUE;
1748 ++initstr;
1749 }
1750
1751 /* Use evim mode for "evim" and "egvim", not for "editor". */
1752 if (TOLOWER_ASC(initstr[0]) == 'e'
1753 && (TOLOWER_ASC(initstr[1]) == 'v'
1754 || TOLOWER_ASC(initstr[1]) == 'g'))
1755 {
1756#ifdef FEAT_GUI
1757 gui.starting = TRUE;
1758#endif
1759 parmp->evim_mode = TRUE;
1760 ++initstr;
1761 }
1762
1763 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1764 if (TOLOWER_ASC(initstr[0]) == 'g')
1765 {
1766 main_start_gui();
1767#ifdef FEAT_GUI
1768 ++initstr;
1769#endif
1770 }
1771
1772 if (STRNICMP(initstr, "view", 4) == 0)
1773 {
1774 readonlymode = TRUE;
1775 curbuf->b_p_ro = TRUE;
1776 p_uc = 10000; /* don't update very often */
1777 initstr += 4;
1778 }
1779 else if (STRNICMP(initstr, "vim", 3) == 0)
1780 initstr += 3;
1781
1782 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1783 if (STRICMP(initstr, "diff") == 0)
1784 {
1785#ifdef FEAT_DIFF
1786 parmp->diff_mode = TRUE;
1787#else
1788 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1789 mch_errmsg("\n");
1790 mch_exit(2);
1791#endif
1792 }
1793
1794 if (STRNICMP(initstr, "ex", 2) == 0)
1795 {
1796 if (STRNICMP(initstr + 2, "im", 2) == 0)
1797 exmode_active = EXMODE_VIM;
1798 else
1799 exmode_active = EXMODE_NORMAL;
1800 change_compatible(TRUE); /* set 'compatible' */
1801 }
1802}
1803
Bram Moolenaar58d98232005-07-23 22:25:46 +00001804/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001805 * Scan the command line arguments.
1806 */
1807 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001808command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001809{
1810 int argc = parmp->argc;
1811 char **argv = parmp->argv;
1812 int argv_idx; /* index in argv[n][] */
1813 int had_minmin = FALSE; /* found "--" argument */
1814 int want_argument; /* option argument with argument */
1815 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001816 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001817 long n;
1818
1819 --argc;
1820 ++argv;
1821 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1822 while (argc > 0)
1823 {
1824 /*
1825 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1826 */
1827 if (argv[0][0] == '+' && !had_minmin)
1828 {
1829 if (parmp->n_commands >= MAX_ARG_CMDS)
1830 mainerr(ME_EXTRA_CMD, NULL);
1831 argv_idx = -1; /* skip to next argument */
1832 if (argv[0][1] == NUL)
1833 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1834 else
1835 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1836 }
1837
1838 /*
1839 * Optional argument.
1840 */
1841 else if (argv[0][0] == '-' && !had_minmin)
1842 {
1843 want_argument = FALSE;
1844 c = argv[0][argv_idx++];
1845#ifdef VMS
1846 /*
1847 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1848 * and "-/X" as "-X".
1849 */
1850 if (c == '/')
1851 {
1852 c = argv[0][argv_idx++];
1853 c = TOUPPER_ASC(c);
1854 }
1855 else
1856 c = TOLOWER_ASC(c);
1857#endif
1858 switch (c)
1859 {
1860 case NUL: /* "vim -" read from stdin */
1861 /* "ex -" silent mode */
1862 if (exmode_active)
1863 silent_mode = TRUE;
1864 else
1865 {
1866 if (parmp->edit_type != EDIT_NONE)
1867 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1868 parmp->edit_type = EDIT_STDIN;
1869 read_cmd_fd = 2; /* read from stderr instead of stdin */
1870 }
1871 argv_idx = -1; /* skip to next argument */
1872 break;
1873
1874 case '-': /* "--" don't take any more option arguments */
1875 /* "--help" give help message */
1876 /* "--version" give version message */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001877 /* "--clean" clean context */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001878 /* "--literal" take files literally */
1879 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001880 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001881 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001882 /* "--noplugin[s]" skip plugins */
1883 /* "--cmd <cmd>" execute cmd before vimrc */
1884 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1885 usage();
1886 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1887 {
1888 Columns = 80; /* need to init Columns */
1889 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1890 list_version();
1891 msg_putchar('\n');
1892 msg_didout = FALSE;
1893 mch_exit(0);
1894 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001895 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
1896 {
1897 parmp->use_vimrc = (char_u *)"DEFAULTS";
1898 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
1899 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001900 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1901 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001902#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001903 parmp->literal = TRUE;
1904#endif
1905 }
1906 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1907 {
1908#ifdef FEAT_GUI
1909 gui.dofork = FALSE; /* don't fork() when starting GUI */
1910#endif
1911 }
1912 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1913 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001914 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1915 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001916 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1917 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001918 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1919 {
1920 want_argument = TRUE;
1921 argv_idx += 3;
1922 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001923 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1924 {
1925 want_argument = TRUE;
1926 argv_idx += 11;
1927 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001928#ifdef FEAT_CLIENTSERVER
1929 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1930 ; /* already processed -- no arg */
1931 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1932 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1933 {
1934 /* already processed -- snatch the following arg */
1935 if (argc > 1)
1936 {
1937 --argc;
1938 ++argv;
1939 }
1940 }
1941#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001942#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1943# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001944 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001945# else
1946 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1947# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001948 {
1949 /* already processed -- snatch the following arg */
1950 if (argc > 1)
1951 {
1952 --argc;
1953 ++argv;
1954 }
1955 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001956#endif
1957#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001958 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1959 {
1960 /* already processed, skip */
1961 }
1962#endif
1963 else
1964 {
1965 if (argv[0][argv_idx])
1966 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1967 had_minmin = TRUE;
1968 }
1969 if (!want_argument)
1970 argv_idx = -1; /* skip to next argument */
1971 break;
1972
1973 case 'A': /* "-A" start in Arabic mode */
1974#ifdef FEAT_ARABIC
1975 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1976#else
1977 mch_errmsg(_(e_noarabic));
1978 mch_exit(2);
1979#endif
1980 break;
1981
1982 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001983 /* Needs to be effective before expanding file names, because
1984 * for Win32 this makes us edit a shortcut file itself,
1985 * instead of the file it links to. */
1986 set_options_bin(curbuf->b_p_bin, 1, 0);
1987 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001988 break;
1989
1990 case 'C': /* "-C" Compatible */
1991 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001992 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001993 break;
1994
1995 case 'e': /* "-e" Ex mode */
1996 exmode_active = EXMODE_NORMAL;
1997 break;
1998
1999 case 'E': /* "-E" Improved Ex mode */
2000 exmode_active = EXMODE_VIM;
2001 break;
2002
2003 case 'f': /* "-f" GUI: run in foreground. Amiga: open
2004 window directly, not with newcli */
2005#ifdef FEAT_GUI
2006 gui.dofork = FALSE; /* don't fork() when starting GUI */
2007#endif
2008 break;
2009
2010 case 'g': /* "-g" start GUI */
2011 main_start_gui();
2012 break;
2013
2014 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2015#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002016 p_fkmap = TRUE;
2017 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002018#else
2019 mch_errmsg(_(e_nofarsi));
2020 mch_exit(2);
2021#endif
2022 break;
2023
2024 case 'h': /* "-h" give help message */
2025#ifdef FEAT_GUI_GNOME
2026 /* Tell usage() to exit for "gvim". */
2027 gui.starting = FALSE;
2028#endif
2029 usage();
2030 break;
2031
2032 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2033#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002034 p_hkmap = TRUE;
2035 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002036#else
2037 mch_errmsg(_(e_nohebrew));
2038 mch_exit(2);
2039#endif
2040 break;
2041
2042 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2043#ifdef FEAT_LISP
2044 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2045 p_sm = TRUE;
2046#endif
2047 break;
2048
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002049 case 'M': /* "-M" no changes or writing of files */
2050 reset_modifiable();
2051 /* FALLTHROUGH */
2052
2053 case 'm': /* "-m" no writing of files */
2054 p_write = FALSE;
2055 break;
2056
2057 case 'y': /* "-y" easy mode */
2058#ifdef FEAT_GUI
2059 gui.starting = TRUE; /* start GUI a bit later */
2060#endif
2061 parmp->evim_mode = TRUE;
2062 break;
2063
2064 case 'N': /* "-N" Nocompatible */
2065 change_compatible(FALSE);
2066 break;
2067
2068 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002069#ifdef FEAT_NETBEANS_INTG
2070 /* checking for "-nb", netbeans parameters */
2071 if (argv[0][argv_idx] == 'b')
2072 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002073 netbeansArg = argv[0];
2074 argv_idx = -1; /* skip to next argument */
2075 }
2076 else
2077#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002078 parmp->no_swap_file = TRUE;
2079 break;
2080
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002081 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002082#ifdef TARGET_API_MAC_OSX
2083 /* For some reason on MacOS X, an argument like:
2084 -psn_0_10223617 is passed in when invoke from Finder
2085 or with the 'open' command */
2086 if (argv[0][argv_idx] == 's')
2087 {
2088 argv_idx = -1; /* bypass full -psn */
2089 main_start_gui();
2090 break;
2091 }
2092#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002093 /* default is 0: open window for each file */
2094 parmp->window_count = get_number_arg((char_u *)argv[0],
2095 &argv_idx, 0);
2096 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002097 break;
2098
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002099 case 'o': /* "-o[N]" open N horizontal split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002100 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002101 parmp->window_count = get_number_arg((char_u *)argv[0],
2102 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002103 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002104 break;
2105
2106 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002107 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002108 parmp->window_count = get_number_arg((char_u *)argv[0],
2109 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002110 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002111 break;
2112
2113#ifdef FEAT_QUICKFIX
2114 case 'q': /* "-q" QuickFix mode */
2115 if (parmp->edit_type != EDIT_NONE)
2116 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2117 parmp->edit_type = EDIT_QF;
2118 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2119 {
2120 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2121 argv_idx = -1;
2122 }
2123 else if (argc > 1) /* "-q {errorfile}" */
2124 want_argument = TRUE;
2125 break;
2126#endif
2127
2128 case 'R': /* "-R" readonly mode */
2129 readonlymode = TRUE;
2130 curbuf->b_p_ro = TRUE;
2131 p_uc = 10000; /* don't update very often */
2132 break;
2133
2134 case 'r': /* "-r" recovery mode */
2135 case 'L': /* "-L" recovery mode */
2136 recoverymode = 1;
2137 break;
2138
2139 case 's':
2140 if (exmode_active) /* "-s" silent (batch) mode */
2141 silent_mode = TRUE;
2142 else /* "-s {scriptin}" read from script file */
2143 want_argument = TRUE;
2144 break;
2145
2146 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2147 if (parmp->edit_type != EDIT_NONE)
2148 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2149 parmp->edit_type = EDIT_TAG;
2150 if (argv[0][argv_idx]) /* "-t{tag}" */
2151 {
2152 parmp->tagname = (char_u *)argv[0] + argv_idx;
2153 argv_idx = -1;
2154 }
2155 else /* "-t {tag}" */
2156 want_argument = TRUE;
2157 break;
2158
2159#ifdef FEAT_EVAL
2160 case 'D': /* "-D" Debugging */
2161 parmp->use_debug_break_level = 9999;
2162 break;
2163#endif
2164#ifdef FEAT_DIFF
2165 case 'd': /* "-d" 'diff' */
2166# ifdef AMIGA
2167 /* check for "-dev {device}" */
2168 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2169 want_argument = TRUE;
2170 else
2171# endif
2172 parmp->diff_mode = TRUE;
2173 break;
2174#endif
2175 case 'V': /* "-V{N}" Verbose level */
2176 /* default is 10: a little bit verbose */
2177 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2178 if (argv[0][argv_idx] != NUL)
2179 {
2180 set_option_value((char_u *)"verbosefile", 0L,
2181 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002182 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002183 }
2184 break;
2185
2186 case 'v': /* "-v" Vi-mode (as if called "vi") */
2187 exmode_active = 0;
2188#ifdef FEAT_GUI
2189 gui.starting = FALSE; /* don't start GUI */
2190#endif
2191 break;
2192
2193 case 'w': /* "-w{number}" set window height */
2194 /* "-w {scriptout}" write to script */
2195 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2196 {
2197 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2198 set_option_value((char_u *)"window", n, NULL, 0);
2199 break;
2200 }
2201 want_argument = TRUE;
2202 break;
2203
2204#ifdef FEAT_CRYPT
2205 case 'x': /* "-x" encrypted reading/writing of files */
2206 parmp->ask_for_key = TRUE;
2207 break;
2208#endif
2209
2210 case 'X': /* "-X" don't connect to X server */
2211#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2212 x_no_connect = TRUE;
2213#endif
2214 break;
2215
2216 case 'Z': /* "-Z" restricted mode */
2217 restricted = TRUE;
2218 break;
2219
2220 case 'c': /* "-c{command}" or "-c {command}" execute
2221 command */
2222 if (argv[0][argv_idx] != NUL)
2223 {
2224 if (parmp->n_commands >= MAX_ARG_CMDS)
2225 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002226 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2227 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002228 argv_idx = -1;
2229 break;
2230 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002231 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002232 case 'S': /* "-S {file}" execute Vim script */
2233 case 'i': /* "-i {viminfo}" use for viminfo */
2234#ifndef FEAT_DIFF
2235 case 'd': /* "-d {device}" device (for Amiga) */
2236#endif
2237 case 'T': /* "-T {terminal}" terminal name */
2238 case 'u': /* "-u {vimrc}" vim inits file */
2239 case 'U': /* "-U {gvimrc}" gvim inits file */
2240 case 'W': /* "-W {scriptout}" overwrite */
2241#ifdef FEAT_GUI_W32
2242 case 'P': /* "-P {parent title}" MDI parent */
2243#endif
2244 want_argument = TRUE;
2245 break;
2246
2247 default:
2248 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2249 }
2250
2251 /*
2252 * Handle option arguments with argument.
2253 */
2254 if (want_argument)
2255 {
2256 /*
2257 * Check for garbage immediately after the option letter.
2258 */
2259 if (argv[0][argv_idx] != NUL)
2260 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2261
2262 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002263 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002264 mainerr_arg_missing((char_u *)argv[0]);
2265 ++argv;
2266 argv_idx = -1;
2267
2268 switch (c)
2269 {
2270 case 'c': /* "-c {command}" execute command */
2271 case 'S': /* "-S {file}" execute Vim script */
2272 if (parmp->n_commands >= MAX_ARG_CMDS)
2273 mainerr(ME_EXTRA_CMD, NULL);
2274 if (c == 'S')
2275 {
2276 char *a;
2277
2278 if (argc < 1)
2279 /* "-S" without argument: use default session file
2280 * name. */
2281 a = SESSION_FILE;
2282 else if (argv[0][0] == '-')
2283 {
2284 /* "-S" followed by another option: use default
2285 * session file name. */
2286 a = SESSION_FILE;
2287 ++argc;
2288 --argv;
2289 }
2290 else
2291 a = argv[0];
2292 p = alloc((unsigned)(STRLEN(a) + 4));
2293 if (p == NULL)
2294 mch_exit(2);
2295 sprintf((char *)p, "so %s", a);
2296 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2297 parmp->commands[parmp->n_commands++] = p;
2298 }
2299 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002300 parmp->commands[parmp->n_commands++] =
2301 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002302 break;
2303
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002304 case '-':
2305 if (argv[-1][2] == 'c')
2306 {
2307 /* "--cmd {command}" execute command */
2308 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2309 mainerr(ME_EXTRA_CMD, NULL);
2310 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002311 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002312 }
2313 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002314 break;
2315
2316 /* case 'd': -d {device} is handled in mch_check_win() for the
2317 * Amiga */
2318
2319#ifdef FEAT_QUICKFIX
2320 case 'q': /* "-q {errorfile}" QuickFix mode */
2321 parmp->use_ef = (char_u *)argv[0];
2322 break;
2323#endif
2324
2325 case 'i': /* "-i {viminfo}" use for viminfo */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002326 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002327 break;
2328
2329 case 's': /* "-s {scriptin}" read from script file */
2330 if (scriptin[0] != NULL)
2331 {
2332scripterror:
2333 mch_errmsg(_("Attempt to open script file again: \""));
2334 mch_errmsg(argv[-1]);
2335 mch_errmsg(" ");
2336 mch_errmsg(argv[0]);
2337 mch_errmsg("\"\n");
2338 mch_exit(2);
2339 }
2340 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2341 {
2342 mch_errmsg(_("Cannot open for reading: \""));
2343 mch_errmsg(argv[0]);
2344 mch_errmsg("\"\n");
2345 mch_exit(2);
2346 }
2347 if (save_typebuf() == FAIL)
2348 mch_exit(2); /* out of memory */
2349 break;
2350
2351 case 't': /* "-t {tag}" */
2352 parmp->tagname = (char_u *)argv[0];
2353 break;
2354
2355 case 'T': /* "-T {terminal}" terminal name */
2356 /*
2357 * The -T term argument is always available and when
2358 * HAVE_TERMLIB is supported it overrides the environment
2359 * variable TERM.
2360 */
2361#ifdef FEAT_GUI
2362 if (term_is_gui((char_u *)argv[0]))
2363 gui.starting = TRUE; /* start GUI a bit later */
2364 else
2365#endif
2366 parmp->term = (char_u *)argv[0];
2367 break;
2368
2369 case 'u': /* "-u {vimrc}" vim inits file */
2370 parmp->use_vimrc = (char_u *)argv[0];
2371 break;
2372
2373 case 'U': /* "-U {gvimrc}" gvim inits file */
2374#ifdef FEAT_GUI
2375 use_gvimrc = (char_u *)argv[0];
2376#endif
2377 break;
2378
2379 case 'w': /* "-w {nr}" 'window' value */
2380 /* "-w {scriptout}" append to script file */
2381 if (vim_isdigit(*((char_u *)argv[0])))
2382 {
2383 argv_idx = 0;
2384 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2385 set_option_value((char_u *)"window", n, NULL, 0);
2386 argv_idx = -1;
2387 break;
2388 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002389 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002390 case 'W': /* "-W {scriptout}" overwrite script file */
2391 if (scriptout != NULL)
2392 goto scripterror;
2393 if ((scriptout = mch_fopen(argv[0],
2394 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2395 {
2396 mch_errmsg(_("Cannot open for script output: \""));
2397 mch_errmsg(argv[0]);
2398 mch_errmsg("\"\n");
2399 mch_exit(2);
2400 }
2401 break;
2402
2403#ifdef FEAT_GUI_W32
2404 case 'P': /* "-P {parent title}" MDI parent */
2405 gui_mch_set_parent(argv[0]);
2406 break;
2407#endif
2408 }
2409 }
2410 }
2411
2412 /*
2413 * File name argument.
2414 */
2415 else
2416 {
2417 argv_idx = -1; /* skip to next argument */
2418
2419 /* Check for only one type of editing. */
2420 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2421 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2422 parmp->edit_type = EDIT_FILE;
2423
2424#ifdef MSWIN
2425 /* Remember if the argument was a full path before changing
2426 * slashes to backslashes. */
2427 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2428 parmp->full_path = TRUE;
2429#endif
2430
2431 /* Add the file to the global argument list. */
2432 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2433 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2434 mch_exit(2);
2435#ifdef FEAT_DIFF
2436 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2437 && !mch_isdir(alist_name(&GARGLIST[0])))
2438 {
2439 char_u *r;
2440
2441 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2442 if (r != NULL)
2443 {
2444 vim_free(p);
2445 p = r;
2446 }
2447 }
2448#endif
2449#if defined(__CYGWIN32__) && !defined(WIN32)
2450 /*
2451 * If vim is invoked by non-Cygwin tools, convert away any
2452 * DOS paths, so things like .swp files are created correctly.
2453 * Look for evidence of non-Cygwin paths before we bother.
2454 * This is only for when using the Unix files.
2455 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002456 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002457 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002458 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002460# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002461 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002462# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002463 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002464# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002465 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002466 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002467 if (p == NULL)
2468 mch_exit(2);
2469 }
2470#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002471
2472#ifdef USE_FNAME_CASE
2473 /* Make the case of the file name match the actual file. */
2474 fname_case(p, 0);
2475#endif
2476
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002477 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002478#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002479 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002480#else
2481 2 /* add buffer number now and use curbuf */
2482#endif
2483 );
2484
2485#if defined(FEAT_MBYTE) && defined(WIN32)
2486 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002487 /* Remember this argument has been added to the argument list.
2488 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002489 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002490# ifdef FEAT_DIFF
2491 parmp->diff_mode
2492# else
2493 FALSE
2494# endif
2495 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002496 }
2497#endif
2498 }
2499
2500 /*
2501 * If there are no more letters after the current "-", go to next
2502 * argument. argv_idx is set to -1 when the current argument is to be
2503 * skipped.
2504 */
2505 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2506 {
2507 --argc;
2508 ++argv;
2509 argv_idx = 1;
2510 }
2511 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002512
2513#ifdef FEAT_EVAL
2514 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2515 * one. */
2516 if (parmp->n_commands > 0)
2517 {
2518 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2519 if (p != NULL)
2520 {
2521 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2522 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2523 vim_free(p);
2524 }
2525 }
2526#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002527}
2528
2529/*
2530 * Print a warning if stdout is not a terminal.
2531 * When starting in Ex mode and commands come from a file, set Silent mode.
2532 */
2533 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002534check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002535{
2536 int input_isatty; /* is active input a terminal? */
2537
2538 input_isatty = mch_input_isatty();
2539 if (exmode_active)
2540 {
2541 if (!input_isatty)
2542 silent_mode = TRUE;
2543 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002544 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545#ifdef FEAT_GUI
2546 /* don't want the delay when started from the desktop */
2547 && !gui.starting
2548#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002549 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002550 {
2551#ifdef NBDEBUG
2552 /*
2553 * This shouldn't be necessary. But if I run netbeans with the log
2554 * output coming to the console and XOpenDisplay fails, I get vim
2555 * trying to start with input/output to my console tty. This fills my
2556 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002557 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002559 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560 {
2561 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2562 exit(1);
2563 }
2564#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002565#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2566 if (is_cygpty_used())
2567 {
Bram Moolenaar2a027452017-09-26 19:10:37 +02002568# if defined(FEAT_MBYTE) && defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
2569 && defined(FEAT_GETTEXT)
2570 char *s, *tofree = NULL;
2571
2572 /* Set the encoding of the error message based on $LC_ALL or
2573 * other environment variables instead of 'encoding'.
2574 * Note that the message is shown on a Cygwin terminal (e.g.
2575 * mintty) which encoding is based on $LC_ALL or etc., not the
2576 * current codepage used by normal Win32 console programs. */
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002577 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002578 if (s == NULL)
2579 s = "utf-8"; /* Use "utf-8" by default. */
2580 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2581 vim_free(tofree);
2582# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002583 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2584 exit(1);
2585 }
2586#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002587 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002588 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2589 if (!input_isatty)
2590 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2591 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002592 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2593 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594 if (scriptin[0] == NULL)
2595 ui_delay(2000L, TRUE);
2596 TIME_MSG("Warning delay");
2597 }
2598}
2599
2600/*
2601 * Read text from stdin.
2602 */
2603 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002604read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002605{
2606 int i;
2607
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002608#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002609 /* When getting the ATTENTION prompt here, use a dialog */
2610 swap_exists_action = SEA_DIALOG;
2611#endif
2612 no_wait_return = TRUE;
2613 i = msg_didany;
2614 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002615 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002616 no_wait_return = FALSE;
2617 msg_didany = i;
2618 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002619#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002620 check_swap_exists_action();
2621#endif
2622#if !(defined(AMIGA) || defined(MACOS))
2623 /*
2624 * Close stdin and dup it from stderr. Required for GPM to work
2625 * properly, and for running external commands.
2626 * Is there any other system that cannot do this?
2627 */
2628 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002629 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002630#endif
2631}
2632
2633/*
2634 * Create the requested number of windows and edit buffers in them.
2635 * Also does recovery if "recoverymode" set.
2636 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002637 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002638create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002639{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002640 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002641 int done = 0;
2642
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002643 /*
2644 * Create the number of windows that was requested.
2645 */
2646 if (parmp->window_count == -1) /* was not set */
2647 parmp->window_count = 1;
2648 if (parmp->window_count == 0)
2649 parmp->window_count = GARGCOUNT;
2650 if (parmp->window_count > 1)
2651 {
2652 /* Don't change the windows if there was a command in .vimrc that
2653 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002654 if (parmp->window_layout == 0)
2655 parmp->window_layout = WIN_HOR;
2656 if (parmp->window_layout == WIN_TABS)
2657 {
2658 parmp->window_count = make_tabpages(parmp->window_count);
2659 TIME_MSG("making tab pages");
2660 }
2661 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002662 {
2663 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002664 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002665 TIME_MSG("making windows");
2666 }
2667 else
2668 parmp->window_count = win_count();
2669 }
2670 else
2671 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002672
2673 if (recoverymode) /* do recover */
2674 {
2675 msg_scroll = TRUE; /* scroll message up */
2676 ml_recover();
2677 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2678 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002679 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002680 }
2681 else
2682 {
2683 /*
2684 * Open a buffer for windows that don't have one yet.
2685 * Commands in the .vimrc might have loaded a file or split the window.
2686 * Watch out for autocommands that delete a window.
2687 */
2688#ifdef FEAT_AUTOCMD
2689 /*
2690 * Don't execute Win/Buf Enter/Leave autocommands here
2691 */
2692 ++autocmd_no_enter;
2693 ++autocmd_no_leave;
2694#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002695 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002696 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002697 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002698 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002699 {
2700 if (parmp->window_layout == WIN_TABS)
2701 goto_tabpage(1);
2702 else
2703 curwin = firstwin;
2704 }
2705 else if (parmp->window_layout == WIN_TABS)
2706 {
2707 if (curtab->tp_next == NULL)
2708 break;
2709 goto_tabpage(0);
2710 }
2711 else
2712 {
2713 if (curwin->w_next == NULL)
2714 break;
2715 curwin = curwin->w_next;
2716 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002717 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718 curbuf = curwin->w_buffer;
2719 if (curbuf->b_ml.ml_mfp == NULL)
2720 {
2721#ifdef FEAT_FOLDING
2722 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2723 if (p_fdls >= 0)
2724 curwin->w_p_fdl = p_fdls;
2725#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002726#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727 /* When getting the ATTENTION prompt here, use a dialog */
2728 swap_exists_action = SEA_DIALOG;
2729#endif
2730 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002731
2732 /* create memfile, read file */
2733 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002734
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002735#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002736 if (swap_exists_action == SEA_QUIT)
2737 {
2738 if (got_int || only_one_window())
2739 {
2740 /* abort selected or quit and only one window */
2741 did_emsg = FALSE; /* avoid hit-enter prompt */
2742 getout(1);
2743 }
2744 /* We can't close the window, it would disturb what
2745 * happens next. Clear the file name and set the arg
2746 * index to -1 to delete it later. */
2747 setfname(curbuf, NULL, NULL, FALSE);
2748 curwin->w_arg_idx = -1;
2749 swap_exists_action = SEA_NONE;
2750 }
2751 else
2752 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002753#endif
2754#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002755 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002756#endif
2757 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758 ui_breakcheck();
2759 if (got_int)
2760 {
2761 (void)vgetc(); /* only break the file loading, not the rest */
2762 break;
2763 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002765 if (parmp->window_layout == WIN_TABS)
2766 goto_tabpage(1);
2767 else
2768 curwin = firstwin;
2769 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770#ifdef FEAT_AUTOCMD
2771 --autocmd_no_enter;
2772 --autocmd_no_leave;
2773#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774 }
2775}
2776
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002777 /*
2778 * If opened more than one window, start editing files in the other
2779 * windows. make_windows() has already opened the windows.
2780 */
2781 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002782edit_buffers(
2783 mparm_T *parmp,
2784 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785{
2786 int arg_idx; /* index in argument list */
2787 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002788 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002789 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002790
2791# ifdef FEAT_AUTOCMD
2792 /*
2793 * Don't execute Win/Buf Enter/Leave autocommands here
2794 */
2795 ++autocmd_no_enter;
2796 ++autocmd_no_leave;
2797# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002798
2799 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2800 if (curwin->w_arg_idx == -1)
2801 {
2802 win_close(curwin, TRUE);
2803 advance = FALSE;
2804 }
2805
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002806 arg_idx = 1;
2807 for (i = 1; i < parmp->window_count; ++i)
2808 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002809 if (cwd != NULL)
2810 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002811 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2812 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002813 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002814 ++arg_idx;
2815 win_close(curwin, TRUE);
2816 advance = FALSE;
2817 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002818 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002819
Bram Moolenaar84212822006-11-07 21:59:47 +00002820 if (advance)
2821 {
2822 if (parmp->window_layout == WIN_TABS)
2823 {
2824 if (curtab->tp_next == NULL) /* just checking */
2825 break;
2826 goto_tabpage(0);
2827 }
2828 else
2829 {
2830 if (curwin->w_next == NULL) /* just checking */
2831 break;
2832 win_enter(curwin->w_next, FALSE);
2833 }
2834 }
2835 advance = TRUE;
2836
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002837 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002838 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002839 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2840 {
2841 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002842 /* Edit file from arg list, if there is one. When "Quit" selected
2843 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002844# ifdef HAS_SWAP_EXISTS_ACTION
2845 swap_exists_did_quit = FALSE;
2846# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002847 (void)do_ecmd(0, arg_idx < GARGCOUNT
2848 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002849 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002850# ifdef HAS_SWAP_EXISTS_ACTION
2851 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002852 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002853 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002854 if (got_int || only_one_window())
2855 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002856 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002857 did_emsg = FALSE; /* avoid hit-enter prompt */
2858 getout(1);
2859 }
2860 win_close(curwin, TRUE);
2861 advance = FALSE;
2862 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002863# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002864 if (arg_idx == GARGCOUNT - 1)
2865 arg_had_last = TRUE;
2866 ++arg_idx;
2867 }
2868 ui_breakcheck();
2869 if (got_int)
2870 {
2871 (void)vgetc(); /* only break the file loading, not the rest */
2872 break;
2873 }
2874 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002875
2876 if (parmp->window_layout == WIN_TABS)
2877 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002878# ifdef FEAT_AUTOCMD
2879 --autocmd_no_enter;
2880# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002881
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002882 /* make the first window the current window */
2883 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02002884#if defined(FEAT_QUICKFIX)
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002885 /* Avoid making a preview window the current window. */
2886 while (win->w_p_pvw)
2887 {
2888 win = win->w_next;
2889 if (win == NULL)
2890 {
2891 win = firstwin;
2892 break;
2893 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002894 }
2895#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002896 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002897
Bram Moolenaar4033c552017-09-16 20:54:51 +02002898#ifdef FEAT_AUTOCMD
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 --autocmd_no_leave;
Bram Moolenaar4033c552017-09-16 20:54:51 +02002900#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002901 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002902 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2904}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002905
2906/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002907 * Execute the commands from --cmd arguments "cmds[cnt]".
2908 */
2909 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002910exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002911{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002912 char_u **cmds = parmp->pre_commands;
2913 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002914 int i;
2915
2916 if (cnt > 0)
2917 {
2918 curwin->w_cursor.lnum = 0; /* just in case.. */
2919 sourcing_name = (char_u *)_("pre-vimrc command line");
2920# ifdef FEAT_EVAL
2921 current_SID = SID_CMDARG;
2922# endif
2923 for (i = 0; i < cnt; ++i)
2924 do_cmdline_cmd(cmds[i]);
2925 sourcing_name = NULL;
2926# ifdef FEAT_EVAL
2927 current_SID = 0;
2928# endif
2929 TIME_MSG("--cmd commands");
2930 }
2931}
2932
2933/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002934 * Execute "+", "-c" and "-S" arguments.
2935 */
2936 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002937exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002938{
2939 int i;
2940
2941 /*
2942 * We start commands on line 0, make "vim +/pat file" match a
2943 * pattern on line 1. But don't move the cursor when an autocommand
2944 * with g`" was used.
2945 */
2946 msg_scroll = TRUE;
2947 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2948 curwin->w_cursor.lnum = 0;
2949 sourcing_name = (char_u *)"command line";
2950#ifdef FEAT_EVAL
2951 current_SID = SID_CARG;
2952#endif
2953 for (i = 0; i < parmp->n_commands; ++i)
2954 {
2955 do_cmdline_cmd(parmp->commands[i]);
2956 if (parmp->cmds_tofree[i])
2957 vim_free(parmp->commands[i]);
2958 }
2959 sourcing_name = NULL;
2960#ifdef FEAT_EVAL
2961 current_SID = 0;
2962#endif
2963 if (curwin->w_cursor.lnum == 0)
2964 curwin->w_cursor.lnum = 1;
2965
2966 if (!exmode_active)
2967 msg_scroll = FALSE;
2968
2969#ifdef FEAT_QUICKFIX
2970 /* When started with "-q errorfile" jump to first error again. */
2971 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002972 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002973#endif
2974 TIME_MSG("executing command arguments");
2975}
2976
2977/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002978 * Source startup scripts.
2979 */
2980 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002981source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002982{
2983 int i;
2984
2985 /*
2986 * For "evim" source evim.vim first of all, so that the user can overrule
2987 * any things he doesn't like.
2988 */
2989 if (parmp->evim_mode)
2990 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002991 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002992 TIME_MSG("source evim file");
2993 }
2994
2995 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002996 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002997 * nothing else.
2998 */
2999 if (parmp->use_vimrc != NULL)
3000 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003001 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
3002 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
3003 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003004 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003005 {
3006#ifdef FEAT_GUI
3007 if (use_gvimrc == NULL) /* don't load gvimrc either */
3008 use_gvimrc = parmp->use_vimrc;
3009#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003010 }
3011 else
3012 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003013 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003014 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3015 }
3016 }
3017 else if (!silent_mode)
3018 {
3019#ifdef AMIGA
3020 struct Process *proc = (struct Process *)FindTask(0L);
3021 APTR save_winptr = proc->pr_WindowPtr;
3022
3023 /* Avoid a requester here for a volume that doesn't exist. */
3024 proc->pr_WindowPtr = (APTR)-1L;
3025#endif
3026
3027 /*
3028 * Get system wide defaults, if the file name is defined.
3029 */
3030#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003031 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003032#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003033#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003034 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003035#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003036
3037 /*
3038 * Try to read initialization commands from the following places:
3039 * - environment variable VIMINIT
3040 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3041 * - second user vimrc file ($VIM/.vimrc for Dos)
3042 * - environment variable EXINIT
3043 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3044 * - second user exrc file ($VIM/.exrc for Dos)
3045 * The first that exists is used, the rest is ignored.
3046 */
3047 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3048 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003049 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003050#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003051 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3052 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003053#endif
3054#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003055 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3056 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003057#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003058#ifdef USR_VIMRC_FILE4
3059 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3060 DOSO_VIMRC) == FAIL
3061#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003062 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003063 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003064#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003065 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003066#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003067 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003068 {
3069 /* When no .vimrc file was found: source defaults.vim. */
3070 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003071 }
3072 }
3073
3074 /*
3075 * Read initialization commands from ".vimrc" or ".exrc" in current
3076 * directory. This is only done if the 'exrc' option is set.
3077 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003078 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003079 * option has been reset in environment of global ".exrc" or ".vimrc".
3080 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3081 * SYS_VIMRC_FILE.
3082 */
3083 if (p_exrc)
3084 {
3085#if defined(UNIX) || defined(VMS)
3086 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3087 if (!file_owned(VIMRC_FILE))
3088#endif
3089 secure = p_secure;
3090
3091 i = FAIL;
3092 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3093 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3094#ifdef USR_VIMRC_FILE2
3095 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3096 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3097#endif
3098#ifdef USR_VIMRC_FILE3
3099 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3100 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3101#endif
3102#ifdef SYS_VIMRC_FILE
3103 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3104 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3105#endif
3106 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003107 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003108
3109 if (i == FAIL)
3110 {
3111#if defined(UNIX) || defined(VMS)
3112 /* if ".exrc" is not owned by user set 'secure' mode */
3113 if (!file_owned(EXRC_FILE))
3114 secure = p_secure;
3115 else
3116 secure = 0;
3117#endif
3118 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3119 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3120#ifdef USR_EXRC_FILE2
3121 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3122 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3123#endif
3124 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003125 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003126 }
3127 }
3128 if (secure == 2)
3129 need_wait_return = TRUE;
3130 secure = 0;
3131#ifdef AMIGA
3132 proc->pr_WindowPtr = save_winptr;
3133#endif
3134 }
3135 TIME_MSG("sourcing vimrc file(s)");
3136}
3137
3138/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003139 * Setup to start using the GUI. Exit with an error when not available.
3140 */
3141 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003142main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003143{
3144#ifdef FEAT_GUI
3145 gui.starting = TRUE; /* start GUI a bit later */
3146#else
3147 mch_errmsg(_(e_nogvim));
3148 mch_errmsg("\n");
3149 mch_exit(2);
3150#endif
3151}
3152
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003153#endif /* NO_VIM_MAIN */
3154
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003155/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003156 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003157 * Returns FAIL if the environment variable was not executed, OK otherwise.
3158 */
3159 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003160process_env(
3161 char_u *env,
3162 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003163{
3164 char_u *initstr;
3165 char_u *save_sourcing_name;
3166 linenr_T save_sourcing_lnum;
3167#ifdef FEAT_EVAL
3168 scid_T save_sid;
3169#endif
3170
3171 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3172 {
3173 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003174 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003175 save_sourcing_name = sourcing_name;
3176 save_sourcing_lnum = sourcing_lnum;
3177 sourcing_name = env;
3178 sourcing_lnum = 0;
3179#ifdef FEAT_EVAL
3180 save_sid = current_SID;
3181 current_SID = SID_ENV;
3182#endif
3183 do_cmdline_cmd(initstr);
3184 sourcing_name = save_sourcing_name;
3185 sourcing_lnum = save_sourcing_lnum;
3186#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003187 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003188#endif
3189 return OK;
3190 }
3191 return FAIL;
3192}
3193
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003194#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003195/*
3196 * Return TRUE if we are certain the user owns the file "fname".
3197 * Used for ".vimrc" and ".exrc".
3198 * Use both stat() and lstat() for extra security.
3199 */
3200 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003201file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003203 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003204# ifdef UNIX
3205 uid_t uid = getuid();
3206# else /* VMS */
3207 uid_t uid = ((getgid() << 16) | getuid());
3208# endif
3209
3210 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3211# ifdef HAVE_LSTAT
3212 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3213# endif
3214 );
3215}
3216#endif
3217
3218/*
3219 * Give an error message main_errors["n"] and exit.
3220 */
3221 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003222mainerr(
3223 int n, /* one of the ME_ defines */
3224 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003225{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003226#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003227 reset_signals(); /* kill us with CTRL-C here, if you like */
3228#endif
3229
3230 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003231 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003232 mch_errmsg(_(main_errors[n]));
3233 if (str != NULL)
3234 {
3235 mch_errmsg(": \"");
3236 mch_errmsg((char *)str);
3237 mch_errmsg("\"");
3238 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003239 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003240
3241 mch_exit(1);
3242}
3243
3244 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003245mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003246{
3247 mainerr(ME_ARG_MISSING, str);
3248}
3249
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003250#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003251/*
3252 * print a message with three spaces prepended and '\n' appended.
3253 */
3254 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003255main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256{
3257 mch_msg(" ");
3258 mch_msg(s);
3259 mch_msg("\n");
3260}
3261
3262/*
3263 * Print messages for "vim -h" or "vim --help" and exit.
3264 */
3265 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003266usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003267{
3268 int i;
3269 static char *(use[]) =
3270 {
3271 N_("[file ..] edit specified file(s)"),
3272 N_("- read text from stdin"),
3273 N_("-t tag edit file where tag is defined"),
3274#ifdef FEAT_QUICKFIX
3275 N_("-q [errorfile] edit file with first error")
3276#endif
3277 };
3278
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003279#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003280 reset_signals(); /* kill us with CTRL-C here, if you like */
3281#endif
3282
3283 mch_msg(longVersion);
3284 mch_msg(_("\n\nusage:"));
3285 for (i = 0; ; ++i)
3286 {
3287 mch_msg(_(" vim [arguments] "));
3288 mch_msg(_(use[i]));
3289 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3290 break;
3291 mch_msg(_("\n or:"));
3292 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003293#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003294 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003295#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003296
3297 mch_msg(_("\n\nArguments:\n"));
3298 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003299#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003300 main_msg(_("--literal\t\tDon't expand wildcards"));
3301#endif
3302#ifdef FEAT_OLE
3303 main_msg(_("-register\t\tRegister this gvim for OLE"));
3304 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3305#endif
3306#ifdef FEAT_GUI
3307 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3308 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3309#endif
3310 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3311 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003312 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003313 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3314#ifdef FEAT_DIFF
3315 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3316#endif
3317 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3318 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3319 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3320 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3321 main_msg(_("-M\t\t\tModifications in text not allowed"));
3322 main_msg(_("-b\t\t\tBinary mode"));
3323#ifdef FEAT_LISP
3324 main_msg(_("-l\t\t\tLisp mode"));
3325#endif
3326 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3327 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003328 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3329#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003331#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003332 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3333 main_msg(_("-r\t\t\tList swap files and exit"));
3334 main_msg(_("-r (with file name)\tRecover crashed session"));
3335 main_msg(_("-L\t\t\tSame as -r"));
3336#ifdef AMIGA
3337 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3338 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3339#endif
3340#ifdef FEAT_ARABIC
3341 main_msg(_("-A\t\t\tstart in Arabic mode"));
3342#endif
3343#ifdef FEAT_RIGHTLEFT
3344 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3345#endif
3346#ifdef FEAT_FKMAP
3347 main_msg(_("-F\t\t\tStart in Farsi mode"));
3348#endif
3349 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003350 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003351 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3353#ifdef FEAT_GUI
3354 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3355#endif
3356 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003357 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003358 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3359 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3360 main_msg(_("+\t\t\tStart at end of file"));
3361 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003362 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003363 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3364 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3365 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3366 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3367 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3368#ifdef FEAT_CRYPT
3369 main_msg(_("-x\t\t\tEdit encrypted files"));
3370#endif
3371#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3372# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3373 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3374# endif
3375 main_msg(_("-X\t\t\tDo not connect to X server"));
3376#endif
3377#ifdef FEAT_CLIENTSERVER
3378 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3379 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3380 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3381 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003382 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003383 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3384 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3385 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3386 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3387#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003388#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003389 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003390#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391#ifdef FEAT_VIMINFO
3392 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3393#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003394 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3396 main_msg(_("--version\t\tPrint version information and exit"));
3397
3398#ifdef FEAT_GUI_X11
3399# ifdef FEAT_GUI_MOTIF
3400 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3401# else
3402# ifdef FEAT_GUI_ATHENA
3403# ifdef FEAT_GUI_NEXTAW
3404 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3405# else
3406 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3407# endif
3408# endif
3409# endif
3410 main_msg(_("-display <display>\tRun vim on <display>"));
3411 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003412 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3413 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3414 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3415 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3416 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3417 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3418 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3419 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3420# ifdef FEAT_GUI_ATHENA
3421 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3422# endif
3423 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3424 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3425 main_msg(_("-xrm <resource>\tSet the specified resource"));
3426#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427#ifdef FEAT_GUI_GTK
3428 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3429 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3430 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3431 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3432 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003433 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003434 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003435 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003436#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003437#ifdef FEAT_GUI_W32
3438 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003439 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003440#endif
3441
3442#ifdef FEAT_GUI_GNOME
3443 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3444 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003445 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003446 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003447 gui.dofork = FALSE;
3448 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003449 else
3450#endif
3451 mch_exit(0);
3452}
3453
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003454#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003455/*
3456 * Check the result of the ATTENTION dialog:
3457 * When "Quit" selected, exit Vim.
3458 * When "Recover" selected, recover the file.
3459 */
3460 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003461check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003462{
3463 if (swap_exists_action == SEA_QUIT)
3464 getout(1);
3465 handle_swap_exists(NULL);
3466}
3467#endif
3468
Bram Moolenaar08cab962017-03-04 14:37:18 +01003469#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003470
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003471#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003472static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003473
3474static struct timeval prev_timeval;
3475
Bram Moolenaar3f269672009-11-03 11:11:11 +00003476# ifdef WIN3264
3477/*
3478 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3479 */
3480 static int
3481gettimeofday(struct timeval *tv, char *dummy)
3482{
3483 long t = clock();
3484 tv->tv_sec = t / CLOCKS_PER_SEC;
3485 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3486 return 0;
3487}
3488# endif
3489
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490/*
3491 * Save the previous time before doing something that could nest.
3492 * set "*tv_rel" to the time elapsed so far.
3493 */
3494 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003495time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003496{
3497 *((struct timeval *)tv_rel) = prev_timeval;
3498 gettimeofday(&prev_timeval, NULL);
3499 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3500 - ((struct timeval *)tv_rel)->tv_usec;
3501 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3502 - ((struct timeval *)tv_rel)->tv_sec;
3503 if (((struct timeval *)tv_rel)->tv_usec < 0)
3504 {
3505 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3506 --((struct timeval *)tv_rel)->tv_sec;
3507 }
3508 *(struct timeval *)tv_start = prev_timeval;
3509}
3510
3511/*
3512 * Compute the previous time after doing something that could nest.
3513 * Subtract "*tp" from prev_timeval;
3514 * Note: The arguments are (void *) to avoid trouble with systems that don't
3515 * have struct timeval.
3516 */
3517 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003518time_pop(
3519 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520{
3521 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3522 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3523 if (prev_timeval.tv_usec < 0)
3524 {
3525 prev_timeval.tv_usec += 1000000;
3526 --prev_timeval.tv_sec;
3527 }
3528}
3529
3530 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003531time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003532{
3533 long usec;
3534 long msec;
3535
3536 usec = now->tv_usec - then->tv_usec;
3537 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3538 usec = usec % 1000L;
3539 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3540}
3541
3542 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003543time_msg(
3544 char *mesg,
3545 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003546 (struct timeval *) */
3547{
3548 static struct timeval start;
3549 struct timeval now;
3550
3551 if (time_fd != NULL)
3552 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003553 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003554 {
3555 gettimeofday(&start, NULL);
3556 prev_timeval = start;
3557 fprintf(time_fd, "\n\ntimes in msec\n");
3558 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3559 fprintf(time_fd, " clock elapsed: other lines\n\n");
3560 }
3561 gettimeofday(&now, NULL);
3562 time_diff(&start, &now);
3563 if (((struct timeval *)tv_start) != NULL)
3564 {
3565 fprintf(time_fd, " ");
3566 time_diff(((struct timeval *)tv_start), &now);
3567 }
3568 fprintf(time_fd, " ");
3569 time_diff(&prev_timeval, &now);
3570 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003571 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003572 }
3573}
3574
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003575#endif
3576
Bram Moolenaar595297d2017-03-04 19:11:12 +01003577#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003578 static void
3579set_progpath(char_u *argv0)
3580{
3581 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003582
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003583# if defined(WIN32)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003584 /* A relative path containing a "/" will become invalid when using ":cd",
3585 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003586 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3587 * this. */
Bram Moolenaar066029e2017-03-05 15:19:32 +01003588 char_u *path = NULL;
3589
3590 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3591 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003592# else
3593 char_u buf[MAXPATHL + 1];
3594# ifdef PROC_EXE_LINK
3595 char linkbuf[MAXPATHL + 1];
3596 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003597
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003598 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3599 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003600 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003601 linkbuf[len] = NUL;
3602 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003603 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003604# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003605
3606 if (!mch_isFullName(val))
3607 {
3608 if (gettail(val) != val
3609 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3610 val = buf;
3611 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003612# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003613
Bram Moolenaar08cab962017-03-04 14:37:18 +01003614 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003615
Bram Moolenaar066029e2017-03-05 15:19:32 +01003616# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003617 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003618# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003619}
3620
3621#endif /* NO_VIM_MAIN */
3622
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003623#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003624
3625/*
3626 * Common code for the X command server and the Win32 command server.
3627 */
3628
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003629static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003630
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003631/*
3632 * Do the client-server stuff, unless "--servername ''" was used.
3633 */
3634 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003635exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003636{
3637 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3638 {
3639# ifdef WIN32
3640 /* Initialise the client/server messaging infrastructure. */
3641 serverInitMessaging();
3642# endif
3643
3644 /*
3645 * When a command server argument was found, execute it. This may
3646 * exit Vim when it was successful. Otherwise it's executed further
3647 * on. Remember the encoding used here in "serverStrEnc".
3648 */
3649 if (parmp->serverArg)
3650 {
3651 cmdsrv_main(&parmp->argc, parmp->argv,
3652 parmp->serverName_arg, &parmp->serverStr);
3653# ifdef FEAT_MBYTE
3654 parmp->serverStrEnc = vim_strsave(p_enc);
3655# endif
3656 }
3657
3658 /* If we're still running, get the name to register ourselves.
3659 * On Win32 can register right now, for X11 need to setup the
3660 * clipboard first, it's further down. */
3661 parmp->servername = serverMakeName(parmp->serverName_arg,
3662 parmp->argv[0]);
3663# ifdef WIN32
3664 if (parmp->servername != NULL)
3665 {
3666 serverSetName(parmp->servername);
3667 vim_free(parmp->servername);
3668 }
3669# endif
3670 }
3671}
3672
3673/*
3674 * Prepare for running as a Vim server.
3675 */
3676 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003677prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003678{
3679# if defined(FEAT_X11)
3680 /*
3681 * Register for remote command execution with :serversend and --remote
3682 * unless there was a -X or a --servername '' on the command line.
3683 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003684 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003685 */
3686 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3687# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003688 (gui.in_use
3689# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003690 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003691# endif
3692 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003693# endif
3694 parmp->serverName_arg != NULL))
3695 {
3696 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3697 vim_free(parmp->servername);
3698 TIME_MSG("register server name");
3699 }
3700 else
3701 serverDelayedStartName = parmp->servername;
3702# endif
3703
3704 /*
3705 * Execute command ourselves if we're here because the send failed (or
3706 * else we would have exited above).
3707 */
3708 if (parmp->serverStr != NULL)
3709 {
3710 char_u *p;
3711
3712 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3713 parmp->serverStr, &p));
3714 vim_free(p);
3715 }
3716}
3717
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003718 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003719cmdsrv_main(
3720 int *argc,
3721 char **argv,
3722 char_u *serverName_arg,
3723 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003724{
3725 char_u *res;
3726 int i;
3727 char_u *sname;
3728 int ret;
3729 int didone = FALSE;
3730 int exiterr = 0;
3731 char **newArgV = argv + 1;
3732 int newArgC = 1,
3733 Argc = *argc;
3734 int argtype;
3735#define ARGTYPE_OTHER 0
3736#define ARGTYPE_EDIT 1
3737#define ARGTYPE_EDIT_WAIT 2
3738#define ARGTYPE_SEND 3
3739 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003740 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003741# ifndef FEAT_X11
3742 HWND srv;
3743# else
3744 Window srv;
3745
3746 setup_term_clip();
3747# endif
3748
3749 sname = serverMakeName(serverName_arg, argv[0]);
3750 if (sname == NULL)
3751 return;
3752
3753 /*
3754 * Execute the command server related arguments and remove them
3755 * from the argc/argv array; We may have to return into main()
3756 */
3757 for (i = 1; i < Argc; i++)
3758 {
3759 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003760 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003761 {
3762 for (; i < *argc; i++)
3763 {
3764 *newArgV++ = argv[i];
3765 newArgC++;
3766 }
3767 break;
3768 }
3769
Bram Moolenaareb94e552006-03-11 21:35:11 +00003770 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003771 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003772 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3773 {
3774 char *p = argv[i] + 8;
3775
3776 argtype = ARGTYPE_EDIT;
3777 while (*p != NUL)
3778 {
3779 if (STRNICMP(p, "-wait", 5) == 0)
3780 {
3781 argtype = ARGTYPE_EDIT_WAIT;
3782 p += 5;
3783 }
3784 else if (STRNICMP(p, "-silent", 7) == 0)
3785 {
3786 silent = TRUE;
3787 p += 7;
3788 }
3789 else if (STRNICMP(p, "-tab", 4) == 0)
3790 {
3791 tabs = TRUE;
3792 p += 4;
3793 }
3794 else
3795 {
3796 argtype = ARGTYPE_OTHER;
3797 break;
3798 }
3799 }
3800 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003801 else
3802 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003803
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003804 if (argtype != ARGTYPE_OTHER)
3805 {
3806 if (i == *argc - 1)
3807 mainerr_arg_missing((char_u *)argv[i]);
3808 if (argtype == ARGTYPE_SEND)
3809 {
3810 *serverStr = (char_u *)argv[i + 1];
3811 i++;
3812 }
3813 else
3814 {
3815 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003816 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003817 if (*serverStr == NULL)
3818 {
3819 /* Probably out of memory, exit. */
3820 didone = TRUE;
3821 exiterr = 1;
3822 break;
3823 }
3824 Argc = i;
3825 }
3826# ifdef FEAT_X11
3827 if (xterm_dpy == NULL)
3828 {
3829 mch_errmsg(_("No display"));
3830 ret = -1;
3831 }
3832 else
3833 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003834 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003835# else
3836 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003837 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003838# endif
3839 if (ret < 0)
3840 {
3841 if (argtype == ARGTYPE_SEND)
3842 {
3843 /* Failed to send, abort. */
3844 mch_errmsg(_(": Send failed.\n"));
3845 didone = TRUE;
3846 exiterr = 1;
3847 }
3848 else if (!silent)
3849 /* Let vim start normally. */
3850 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3851 break;
3852 }
3853
3854# ifdef FEAT_GUI_W32
3855 /* Guess that when the server name starts with "g" it's a GUI
3856 * server, which we can bring to the foreground here.
3857 * Foreground() in the server doesn't work very well. */
3858 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3859 SetForegroundWindow(srv);
3860# endif
3861
3862 /*
3863 * For --remote-wait: Wait until the server did edit each
3864 * file. Also detect that the server no longer runs.
3865 */
3866 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3867 {
3868 int numFiles = *argc - i - 1;
3869 int j;
3870 char_u *done = alloc(numFiles);
3871 char_u *p;
3872# ifdef FEAT_GUI_W32
3873 NOTIFYICONDATA ni;
3874 int count = 0;
3875 extern HWND message_window;
3876# endif
3877
3878 if (numFiles > 0 && argv[i + 1][0] == '+')
3879 /* Skip "+cmd" argument, don't wait for it to be edited. */
3880 --numFiles;
3881
3882# ifdef FEAT_GUI_W32
3883 ni.cbSize = sizeof(ni);
3884 ni.hWnd = message_window;
3885 ni.uID = 0;
3886 ni.uFlags = NIF_ICON|NIF_TIP;
3887 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3888 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3889 Shell_NotifyIcon(NIM_ADD, &ni);
3890# endif
3891
3892 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003893 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003894 while (memchr(done, 0, numFiles) != NULL)
3895 {
3896# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003897 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003898 if (p == NULL)
3899 break;
3900# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003901 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003902 break;
3903# endif
3904 j = atoi((char *)p);
3905 if (j >= 0 && j < numFiles)
3906 {
3907# ifdef FEAT_GUI_W32
3908 ++count;
3909 sprintf(ni.szTip, _("%d of %d edited"),
3910 count, numFiles);
3911 Shell_NotifyIcon(NIM_MODIFY, &ni);
3912# endif
3913 done[j] = 1;
3914 }
3915 }
3916# ifdef FEAT_GUI_W32
3917 Shell_NotifyIcon(NIM_DELETE, &ni);
3918# endif
3919 }
3920 }
3921 else if (STRICMP(argv[i], "--remote-expr") == 0)
3922 {
3923 if (i == *argc - 1)
3924 mainerr_arg_missing((char_u *)argv[i]);
3925# ifdef WIN32
3926 /* Win32 always works? */
3927 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003928 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003929# else
3930 if (xterm_dpy == NULL)
3931 mch_errmsg(_("No display: Send expression failed.\n"));
3932 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003933 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003934# endif
3935 {
3936 if (res != NULL && *res != NUL)
3937 {
3938 /* Output error from remote */
3939 mch_errmsg((char *)res);
3940 vim_free(res);
3941 res = NULL;
3942 }
3943 mch_errmsg(_(": Send expression failed.\n"));
3944 }
3945 }
3946 else if (STRICMP(argv[i], "--serverlist") == 0)
3947 {
3948# ifdef WIN32
3949 /* Win32 always works? */
3950 res = serverGetVimNames();
3951# else
3952 if (xterm_dpy != NULL)
3953 res = serverGetVimNames(xterm_dpy);
3954# endif
3955 if (called_emsg)
3956 mch_errmsg("\n");
3957 }
3958 else if (STRICMP(argv[i], "--servername") == 0)
3959 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003960 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003961 i++;
3962 continue;
3963 }
3964 else
3965 {
3966 *newArgV++ = argv[i];
3967 newArgC++;
3968 continue;
3969 }
3970 didone = TRUE;
3971 if (res != NULL && *res != NUL)
3972 {
3973 mch_msg((char *)res);
3974 if (res[STRLEN(res) - 1] != '\n')
3975 mch_msg("\n");
3976 }
3977 vim_free(res);
3978 }
3979
3980 if (didone)
3981 {
3982 display_errors(); /* display any collected messages */
3983 exit(exiterr); /* Mission accomplished - get out */
3984 }
3985
3986 /* Return back into main() */
3987 *argc = newArgC;
3988 vim_free(sname);
3989}
3990
3991/*
3992 * Build a ":drop" command to send to a Vim server.
3993 */
3994 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003995build_drop_cmd(
3996 int filec,
3997 char **filev,
3998 int tabs, /* Use ":tab drop" instead of ":drop". */
3999 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004000{
4001 garray_T ga;
4002 int i;
4003 char_u *inicmd = NULL;
4004 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004005 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004006 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004007
4008 if (filec > 0 && filev[0][0] == '+')
4009 {
4010 inicmd = (char_u *)filev[0] + 1;
4011 filev++;
4012 filec--;
4013 }
4014 /* Check if we have at least one argument. */
4015 if (filec <= 0)
4016 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004017
4018 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004019 cwd = alloc(MAXPATHL);
4020 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004021 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004022 if (mch_dirname(cwd, MAXPATHL) != OK)
4023 {
4024 vim_free(cwd);
4025 return NULL;
4026 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004027 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004028#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004029 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004030#else
4031 PATH_ESC_CHARS,
4032#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004033 '\\', TRUE);
4034 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004035 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004036 return NULL;
4037 ga_init2(&ga, 1, 100);
4038 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004039 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004040
4041 /* Call inputsave() so that a prompt for an encryption key works. */
4042 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4043 if (tabs)
4044 ga_concat(&ga, (char_u *)"tab ");
4045 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004046 for (i = 0; i < filec; i++)
4047 {
4048 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004049 * do it again in the Vim server. On MS-Windows only escape
4050 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004051 p = vim_strsave_escaped((char_u *)filev[i],
4052#ifdef UNIX
4053 PATH_ESC_CHARS
4054#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004055 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004056#endif
4057 );
4058 if (p == NULL)
4059 {
4060 vim_free(ga.ga_data);
4061 return NULL;
4062 }
4063 ga_concat(&ga, (char_u *)" ");
4064 ga_concat(&ga, p);
4065 vim_free(p);
4066 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004067 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4068
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004069 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4070 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004071 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4072
4073 /* Switch back to the correct current directory (prior to temporary path
4074 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004075 * correct after the :drop command. With line breaks and spaces:
4076 * if !exists('+acd') || !&acd
4077 * if haslocaldir()
4078 * cd -
4079 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004080 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004081 * cd -
4082 * endif
4083 * endif
4084 */
4085 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004086 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004087 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004088 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004089 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004090
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004091 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004092 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4093 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004094 if (inicmd != NULL)
4095 {
4096 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4097 * the following commands to be inserted as text. Use a "|",
4098 * hopefully "inicmd" does allow this... */
4099 ga_concat(&ga, inicmd);
4100 ga_concat(&ga, (char_u *)"|");
4101 }
4102 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4103 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004104 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004105 ga_append(&ga, NUL);
4106 return ga.ga_data;
4107}
4108
4109/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004110 * Make our basic server name: use the specified "arg" if given, otherwise use
4111 * the tail of the command "cmd" we were started with.
4112 * Return the name in allocated memory. This doesn't include a serial number.
4113 */
4114 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004115serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004116{
4117 char_u *p;
4118
4119 if (arg != NULL && *arg != NUL)
4120 p = vim_strsave_up(arg);
4121 else
4122 {
4123 p = vim_strsave_up(gettail((char_u *)cmd));
4124 /* Remove .exe or .bat from the name. */
4125 if (p != NULL && vim_strchr(p, '.') != NULL)
4126 *vim_strchr(p, '.') = NUL;
4127 }
4128 return p;
4129}
4130#endif /* FEAT_CLIENTSERVER */
4131
4132#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4133/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004134 * Replace termcodes such as <CR> and insert as key presses if there is room.
4135 */
4136 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004137server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004138{
4139 char_u *ptr = NULL;
4140 char_u *cpo_save = p_cpo;
4141
4142 /* Set 'cpoptions' the way we want it.
4143 * B set - backslashes are *not* treated specially
4144 * k set - keycodes are *not* reverse-engineered
4145 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004146 * The last but one parameter of replace_termcodes() is TRUE so that the
4147 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004148 */
4149 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004150 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004151 p_cpo = cpo_save;
4152
4153 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4154 {
4155 /*
4156 * Add the string to the input stream.
4157 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4158 *
4159 * First clear typed characters from the typeahead buffer, there could
4160 * be half a mapping there. Then append to the existing string, so
4161 * that multiple commands from a client are concatenated.
4162 */
4163 if (typebuf.tb_maplen < typebuf.tb_len)
4164 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4165 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4166
4167 /* Let input_available() know we inserted text in the typeahead
4168 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004169 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004170 }
4171 vim_free((char_u *)ptr);
4172}
4173
4174/*
4175 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004176 */
4177 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004178eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004179{
4180 char_u *res;
4181 int save_dbl = debug_break_level;
4182 int save_ro = redir_off;
Bram Moolenaard99388b2017-10-26 14:28:32 +02004183 void *fc = NULL;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004184
4185 /* Evaluate the expression at the toplevel, don't use variables local to
Bram Moolenaard99388b2017-10-26 14:28:32 +02004186 * the calling function. Except when in debug mode. */
4187 if (!debug_mode)
4188 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004189
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004190 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4191 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004192 debug_break_level = -1;
4193 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004194 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4195 * to be typed. Do generate errors so that try/catch works. */
4196 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004197
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004198 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004199
4200 debug_break_level = save_dbl;
4201 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004202 --emsg_silent;
4203 if (emsg_silent < 0)
4204 emsg_silent = 0;
Bram Moolenaard99388b2017-10-26 14:28:32 +02004205 if (fc != NULL)
4206 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004207
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004208 /* A client can tell us to redraw, but not to display the cursor, so do
4209 * that here. */
4210 setcursor();
4211 out_flush();
4212#ifdef FEAT_GUI
4213 if (gui.in_use)
4214 gui_update_cursor(FALSE, FALSE);
4215#endif
4216
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004217 return res;
4218}
4219
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004220/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004221 * Evaluate a command or expression sent to ourselves.
4222 */
4223 int
4224sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4225{
4226 if (asExpr)
4227 {
4228 char_u *ret;
4229
4230 ret = eval_client_expr_to_string(cmd);
4231 if (result != NULL)
4232 {
4233 if (ret == NULL)
4234 {
4235 char *err = _(e_invexprmsg);
4236 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4237 char_u *msg;
4238
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004239 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004240 if (msg != NULL)
4241 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4242 *result = msg;
4243 }
4244 else
4245 *result = ret;
4246 }
4247 else
4248 vim_free(ret);
4249 return ret == NULL ? -1 : 0;
4250 }
4251 server_to_input_buf(cmd);
4252 return 0;
4253}
4254
4255/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004256 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4257 * return an allocated string. Otherwise return "data".
4258 * "*tofree" is set to the result when it needs to be freed later.
4259 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004260 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004261serverConvert(
4262 char_u *client_enc UNUSED,
4263 char_u *data,
4264 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004265{
4266 char_u *res = data;
4267
4268 *tofree = NULL;
4269# ifdef FEAT_MBYTE
4270 if (client_enc != NULL && p_enc != NULL)
4271 {
4272 vimconv_T vimconv;
4273
4274 vimconv.vc_type = CONV_NONE;
4275 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4276 && vimconv.vc_type != CONV_NONE)
4277 {
4278 res = string_convert(&vimconv, data, NULL);
4279 if (res == NULL)
4280 res = data;
4281 else
4282 *tofree = res;
4283 }
4284 convert_setup(&vimconv, NULL, NULL);
4285 }
4286# endif
4287 return res;
4288}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004289#endif