blob: 51e4483e3e7f243e9ac86605476b468316a0add4 [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
14# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
16# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
17 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000018# endif
19# include <limits.h>
20#endif
21
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020022#if defined(WIN3264) && !defined(FEAT_GUI_W32)
23# include "iscygpty.h"
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Values for edit_type. */
27#define EDIT_NONE 0 /* no edit type yet */
28#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
29#define EDIT_STDIN 2 /* read file from stdin */
30#define EDIT_TAG 3 /* tag name argument given, use tagname */
31#define EDIT_QF 4 /* start in quickfix mode */
32
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010033#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000035#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020037# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
38static void init_locale(void);
39# endif
40static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010041#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void main_msg(char *s);
43static void usage(void);
44static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void command_line_scan(mparm_T *parmp);
47static void check_tty(mparm_T *parmp);
48static void read_stdin(void);
49static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010050# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010051static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010052# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010053static void exe_pre_commands(mparm_T *parmp);
54static void exe_commands(mparm_T *parmp);
55static void source_startup_scripts(mparm_T *parmp);
56static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010057# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010058static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010059# endif
60# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010061static void exec_on_server(mparm_T *parmp);
62static void prepare_server(mparm_T *parmp);
63static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
64static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010065# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000066#endif
67
68
Bram Moolenaarb4210b32004-06-13 14:51:16 +000069/*
70 * Different types of error messages.
71 */
72static char *(main_errors[]) =
73{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000074 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000075#define ME_UNKNOWN_OPTION 0
76 N_("Too many edit arguments"),
77#define ME_TOO_MANY_ARGS 1
78 N_("Argument missing after"),
79#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000080 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000081#define ME_GARBAGE 3
82 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
83#define ME_EXTRA_CMD 4
84 N_("Invalid argument for"),
85#define ME_INVALID_ARG 5
86};
87
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010088#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +010089#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020090
91static char_u *start_dir = NULL; /* current working dir on startup */
92
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020093static int has_dash_c_arg = FALSE;
94
Bram Moolenaarb4210b32004-06-13 14:51:16 +000095 int
96# ifdef VIMDLL
97_export
98# endif
99# ifdef FEAT_GUI_MSWIN
100# ifdef __BORLANDC__
101_cdecl
102# endif
103VimMain
104# else
105main
106# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100107(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000108{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000109 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000110 mparm_T params; /* various parameters passed between
111 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000112#ifdef STARTUPTIME
113 int i;
114#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000115
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000116 /*
117 * Do any system-specific initialisations. These can NOT use IObuff or
118 * NameBuff. Thus emsg2() cannot be called!
119 */
120 mch_early_init();
121
Bram Moolenaar14993322014-09-09 12:25:33 +0200122#if defined(WIN32) && defined(FEAT_MBYTE)
123 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200124 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200125 * convert when 'encoding' changes. Get the unexpanded arguments.
126 */
127 argc = get_cmd_argsW(&argv);
128#endif
129
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000130 /* Many variables are in "params" so that we can pass them to invoked
131 * functions without a lot of arguments. "argc" and "argv" are also
132 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000133 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000134 params.argc = argc;
135 params.argv = argv;
136 params.want_full_screen = TRUE;
137#ifdef FEAT_EVAL
138 params.use_debug_break_level = -1;
139#endif
140#ifdef FEAT_WINDOWS
141 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142#endif
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 Moolenaar3f269672009-11-03 11:11:11 +0000160 for (i = 1; i < argc; ++i)
161 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000162 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000163 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000164 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000165 TIME_MSG("--- VIM STARTING ---");
166 break;
167 }
168 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000169#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000170 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000171
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200172 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000173
174#ifdef FEAT_CLIENTSERVER
175 /*
176 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000177 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000179 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000180#endif
181
182 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000183 * Figure out the way to work from the command name argv[0].
184 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000185 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000186 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000187
188 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000189 * Process the command line arguments. File names are put in the global
190 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000191 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000192 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000193 TIME_MSG("parsing arguments");
194
195 /*
196 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 * there is no terminal version, and on Windows we can't fork one off with
198 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000199 */
200#ifdef ALWAYS_USE_GUI
201 gui.starting = TRUE;
202#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000203# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000204 /*
205 * Check if the GUI can be started. Reset gui.starting if not.
206 * Don't know about other systems, stay on the safe side and don't check.
207 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000208 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000209 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000210 if (gui_init_check() == FAIL)
211 {
212 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000214 /* When running "evim" or "gvim -y" we need the menus, exit if we
215 * don't have them. */
216 if (params.evim_mode)
217 mch_exit(1);
218 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000219 }
220# endif
221#endif
222
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000223 if (GARGCOUNT > 0)
224 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100225#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000226 /*
227 * Expand wildcards in file names.
228 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000229 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200231 start_dir = alloc(MAXPATHL);
232 if (start_dir != NULL)
233 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234 /* Temporarily add '(' and ')' to 'isfname'. These are valid
235 * filename characters but are excluded from 'isfname' to make
236 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
237 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000238 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200240 if (start_dir != NULL)
241 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000242 }
243#endif
244 fname = alist_name(&GARGLIST[0]);
245 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000246
247#if defined(WIN32) && defined(FEAT_MBYTE)
248 {
249 extern void set_alist_count(void);
250
251 /* Remember the number of entries in the argument list. If it changes
252 * we don't react on setting 'encoding'. */
253 set_alist_count();
254 }
255#endif
256
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000257#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000258 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259 {
260 /*
261 * If there is one filename, fully qualified, we have very probably
262 * been invoked from explorer, so change to the file's directory.
263 * Hint: to avoid this when typing a command use a forward slash.
264 * If the cd fails, it doesn't matter.
265 */
266 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200267 if (start_dir != NULL)
268 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269 }
270#endif
271 TIME_MSG("expanding arguments");
272
273#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000274 if (params.diff_mode && params.window_count == -1)
275 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000276#endif
277
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000278 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000279 ++RedrawingDisabled;
280
281 /*
282 * When listing swap file names, don't do cursor positioning et. al.
283 */
284 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000285 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000286
287 /*
288 * When certain to start the GUI, don't check capabilities of terminal.
289 * For GTK we can't be sure, but when started from the desktop it doesn't
290 * make sense to try using a terminal.
291 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000292#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000293 if (gui.starting
294# ifdef FEAT_GUI_GTK
295 && !isatty(2)
296# endif
297 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000298 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000299#endif
300
301#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
302 /* When the GUI is started from Finder, need to display messages in a
303 * message box. isatty(2) returns TRUE anyway, thus we need to check the
304 * name to know we're not started from a terminal. */
305 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000306 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000307 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000308
309 /* Avoid always using "/" as the current directory. Note that when
310 * started from Finder the arglist will be filled later in
311 * HandleODocAE() and "fname" will be NULL. */
312 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
313 && STRCMP(NameBuff, "/") == 0)
314 {
315 if (fname != NULL)
316 (void)vim_chdirfile(fname);
317 else
318 {
319 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
320 vim_chdir(NameBuff);
321 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200322 if (start_dir != NULL)
323 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000324 }
325 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000326#endif
327
328 /*
329 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100330 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331 * Note that we may use mch_exit() before mch_init()!
332 */
333 mch_init();
334 TIME_MSG("shell init");
335
336#ifdef USE_XSMP
337 /*
338 * For want of anywhere else to do it, try to connect to xsmp here.
339 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
340 * Hijacking -X 'no X connection' to also disable XSMP connection as that
341 * has a similar delay upon failure.
342 * Only try if SESSION_MANAGER is set to something non-null.
343 */
344 if (!x_no_connect)
345 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000346 char *p = getenv("SESSION_MANAGER");
347
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000348 if (p != NULL && *p != NUL)
349 {
350 xsmp_init();
351 TIME_MSG("xsmp init");
352 }
353 }
354#endif
355
356 /*
357 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000358 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000359 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000361 /* This message comes before term inits, but after setting "silent_mode"
362 * when the input is not a tty. */
363 if (GARGCOUNT > 1 && !silent_mode)
364 printf(_("%d files to edit\n"), GARGCOUNT);
365
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000366 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000368 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000369 capabilities (will set full_screen) */
370 screen_start(); /* don't know where cursor is now */
371 TIME_MSG("Termcap init");
372 }
373
374 /*
375 * Set the default values for the options that use Rows and Columns.
376 */
377 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000378 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379#ifdef FEAT_DIFF
380 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
381 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000382 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 diff_win_options(firstwin, FALSE);
384#endif
385
386 cmdline_row = Rows - p_ch;
387 msg_row = cmdline_row;
388 screenalloc(FALSE); /* allocate screen buffers */
389 set_init_2();
390 TIME_MSG("inits 2");
391
392 msg_scroll = TRUE;
393 no_wait_return = TRUE;
394
395 init_mappings(); /* set up initial mappings */
396
397 init_highlight(TRUE, FALSE); /* set the default highlight groups */
398 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399
400#ifdef FEAT_EVAL
401 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000402 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000403#endif
404
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100405#ifdef FEAT_MZSCHEME
406 /*
407 * Newer version of MzScheme (Racket) require earlier (trampolined)
408 * initialisation via scheme_main_setup.
409 * Implement this by initialising it as early as possible
410 * and splitting off remaining Vim main into vim_main2
411 */
412 {
413 /* Pack up preprocessed command line arguments.
414 * It is safe because Scheme does not access argc/argv. */
415 char *args[2];
416 args[0] = (char *)fname;
417 args[1] = (char *)&params;
418 return mzscheme_main(2, args);
419 }
420}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100421#endif
422#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100423
Bram Moolenaar8866d272012-11-28 15:55:42 +0100424/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
425 * NO_VIM_MAIN is defined. */
426#ifdef FEAT_MZSCHEME
427 int
428vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100429{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100430# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100431 char_u *fname = (char_u *)argv[0];
432 mparm_T params;
433
434 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100435# else
436 return 0;
437}
438# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100439#endif
440
Bram Moolenaar8866d272012-11-28 15:55:42 +0100441#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000442 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000443 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444
Bram Moolenaar58d98232005-07-23 22:25:46 +0000445 /* Source startup scripts. */
446 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447
448#ifdef FEAT_EVAL
449 /*
450 * Read all the plugin files.
451 * Only when compiled with +eval, since most plugins need it.
452 */
453 if (p_lpl)
454 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000455# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100456 source_runtime((char_u *)"plugin/*.vim", DIP_ALL);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000457# else
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100458 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000459# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000460 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100461
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100462 ex_packloadall(NULL);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100463 TIME_MSG("loading packages");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000464 }
465#endif
466
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000467#ifdef FEAT_DIFF
468 /* Decide about window layout for diff mode after reading vimrc. */
469 if (params.diff_mode && params.window_layout == 0)
470 {
471 if (diffopt_horizontal())
472 params.window_layout = WIN_HOR; /* use horizontal split */
473 else
474 params.window_layout = WIN_VER; /* use vertical split */
475 }
476#endif
477
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000478 /*
479 * Recovery mode without a file name: List swap files.
480 * This uses the 'dir' option, therefore it must be after the
481 * initializations.
482 */
483 if (recoverymode && fname == NULL)
484 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200485 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000486 mch_exit(0);
487 }
488
489 /*
490 * Set a few option defaults after reading .vimrc files:
491 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
492 */
493 set_init_3();
494 TIME_MSG("inits 3");
495
496 /*
497 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
498 * Note that this overrides anything from a vimrc file.
499 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000500 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 p_uc = 0;
502
503#ifdef FEAT_FKMAP
504 if (curwin->w_p_rl && p_altkeymap)
505 {
506 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
507# ifdef FEAT_ARABIC
508 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
509# endif
510 p_fkmap = TRUE; /* Set the Farsi keymap mode */
511 }
512#endif
513
514#ifdef FEAT_GUI
515 if (gui.starting)
516 {
517#if defined(UNIX) || defined(VMS)
518 /* When something caused a message from a vimrc script, need to output
519 * an extra newline before the shell prompt. */
520 if (did_emsg || msg_didout)
521 putchar('\n');
522#endif
523
524 gui_start(); /* will set full_screen to TRUE */
525 TIME_MSG("starting GUI");
526
527 /* When running "evim" or "gvim -y" we need the menus, exit if we
528 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000529 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000530 mch_exit(1);
531 }
532#endif
533
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000534#ifdef FEAT_VIMINFO
535 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000536 * Read in registers, history etc, but not marks, from the viminfo file.
537 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538 */
539 if (*p_viminfo != NUL)
540 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000541 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 TIME_MSG("reading viminfo");
543 }
544#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100545#ifdef FEAT_EVAL
546 /* It's better to make v:oldfiles an empty list than NULL. */
547 if (get_vim_var_list(VV_OLDFILES) == NULL)
548 set_vim_var_list(VV_OLDFILES, list_alloc());
549#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000550
551#ifdef FEAT_QUICKFIX
552 /*
553 * "-q errorfile": Load the error file now.
554 * If the error file can't be read, exit before doing anything else.
555 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000556 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000558 if (params.use_ef != NULL)
559 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000560 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200561 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
562 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000563 {
564 out_char('\n');
565 mch_exit(3);
566 }
567 TIME_MSG("reading errorfile");
568 }
569#endif
570
571 /*
572 * Start putting things on the screen.
573 * Scroll screen down before drawing over it
574 * Clear screen now, so file message will not be cleared.
575 */
576 starting = NO_BUFFERS;
577 no_wait_return = FALSE;
578 if (!exmode_active)
579 msg_scroll = FALSE;
580
581#ifdef FEAT_GUI
582 /*
583 * This seems to be required to make callbacks to be called now, instead
584 * of after things have been put on the screen, which then may be deleted
585 * when getting a resize callback.
586 * For the Mac this handles putting files dropped on the Vim icon to
587 * global_alist.
588 */
589 if (gui.in_use)
590 {
591# ifdef FEAT_SUN_WORKSHOP
592 if (!usingSunWorkShop)
593# endif
594 gui_wait_for_chars(50L);
595 TIME_MSG("GUI delay");
596 }
597#endif
598
599#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
600 qnx_clip_init();
601#endif
602
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200603#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
604 clip_init(TRUE);
605#endif
606
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000607#ifdef FEAT_XCLIPBOARD
608 /* Start using the X clipboard, unless the GUI was started. */
609# ifdef FEAT_GUI
610 if (!gui.in_use)
611# endif
612 {
613 setup_term_clip();
614 TIME_MSG("setup clipboard");
615 }
616#endif
617
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000618#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000619 /* Prepare for being a Vim server. */
620 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000621#endif
622
623 /*
624 * If "-" argument given: Read file from stdin.
625 * Do this before starting Raw mode, because it may change things that the
626 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
627 * are the same terminal: "cat | vim -".
628 * Using autocommands here may cause trouble...
629 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000630 if (params.edit_type == EDIT_STDIN && !recoverymode)
631 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000632
633#if defined(UNIX) || defined(VMS)
634 /* When switching screens and something caused a message from a vimrc
635 * script, need to output an extra newline on exit. */
636 if ((did_emsg || msg_didout) && *T_TI != NUL)
637 newline_on_exit = TRUE;
638#endif
639
640 /*
641 * When done something that is not allowed or error message call
642 * wait_return. This must be done before starttermcap(), because it may
643 * switch to another screen. It must be done after settmode(TMODE_RAW),
644 * because we want to react on a single key stroke.
645 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000646 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000647 */
648 settmode(TMODE_RAW);
649 TIME_MSG("setting raw mode");
650
651 if (need_wait_return || msg_didany)
652 {
653 wait_return(TRUE);
654 TIME_MSG("waiting for return");
655 }
656
657 starttermcap(); /* start termcap if not done by wait_return() */
658 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200659#if defined(FEAT_TERMRESPONSE)
660# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200661 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200662# endif
663 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100664#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000665
666#ifdef FEAT_MOUSE
667 setmouse(); /* may start using the mouse */
668#endif
669 if (scroll_region)
670 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000671 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000672
673 /*
674 * Don't clear the screen when starting in Ex mode, unless using the GUI.
675 */
676 if (exmode_active
677#ifdef FEAT_GUI
678 && !gui.in_use
679#endif
680 )
681 must_redraw = CLEAR;
682 else
683 {
684 screenclear(); /* clear screen */
685 TIME_MSG("clearing screen");
686 }
687
688#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000689 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000690 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100691 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200692 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000693 TIME_MSG("getting crypt key");
694 }
695#endif
696
697 no_wait_return = TRUE;
698
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000699 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000700 * Create the requested number of windows and edit buffers in them.
701 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000703 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000704 TIME_MSG("opening buffers");
705
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000706#ifdef FEAT_EVAL
707 /* clear v:swapcommand */
708 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
709#endif
710
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 /* Ex starts at last line of the file */
712 if (exmode_active)
713 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
714
715#ifdef FEAT_AUTOCMD
716 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
717 TIME_MSG("BufEnter autocommands");
718#endif
719 setpcmark();
720
721#ifdef FEAT_QUICKFIX
722 /*
723 * When started with "-q errorfile" jump to first error now.
724 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000725 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000727 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000728 TIME_MSG("jump to first error");
729 }
730#endif
731
732#ifdef FEAT_WINDOWS
733 /*
734 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000735 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200737 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000738#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200739 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740
741#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000742 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000743 {
744 win_T *wp;
745
746 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200747 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000748 diff_win_options(wp, TRUE);
749 }
750#endif
751
752 /*
753 * Shorten any of the filenames, but only when absolute.
754 */
755 shorten_fnames(FALSE);
756
757 /*
758 * Need to jump to the tag before executing the '-c command'.
759 * Makes "vim -c '/return' -t main" work.
760 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000761 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000762 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000763#if defined(HAS_SWAP_EXISTS_ACTION)
764 swap_exists_did_quit = FALSE;
765#endif
766
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000767 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 do_cmdline_cmd(IObuff);
769 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000770
771#if defined(HAS_SWAP_EXISTS_ACTION)
772 /* If the user doesn't want to edit the file then we quit here. */
773 if (swap_exists_did_quit)
774 getout(1);
775#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000776 }
777
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000778 /* Execute any "+", "-c" and "-S" arguments. */
779 if (params.n_commands > 0)
780 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000781
782 RedrawingDisabled = 0;
783 redraw_all_later(NOT_VALID);
784 no_wait_return = FALSE;
785 starting = 0;
786
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200787 /* 'autochdir' has been postponed */
788 DO_AUTOCHDIR
789
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000790#ifdef FEAT_TERMRESPONSE
791 /* Requesting the termresponse is postponed until here, so that a "-c q"
792 * argument doesn't make it appear in the shell Vim was started from. */
793 may_req_termresponse();
794#endif
795
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796 /* start in insert mode */
797 if (p_im)
798 need_start_insertmode = TRUE;
799
Bram Moolenaar14735512016-03-26 21:00:08 +0100800#ifdef FEAT_EVAL
801 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
802#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000803#ifdef FEAT_AUTOCMD
804 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
805 TIME_MSG("VimEnter autocommands");
806#endif
807
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200808#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
809 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
810 * done after the clipboard is available and all initial commands that may
811 * modify the 'clipboard' setting have run; i.e. just before entering the
812 * main loop. */
813 {
814 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100815
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200816 adjust_clip_reg(&default_regname);
817 set_reg_var(default_regname);
818 }
819#endif
820
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000821#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
822 /* When a startup script or session file setup for diff'ing and
823 * scrollbind, sync the scrollbind now. */
824 if (curwin->w_p_diff && curwin->w_p_scb)
825 {
826 update_topline();
827 check_scrollbind((linenr_T)0, 0L);
828 TIME_MSG("diff scrollbinding");
829 }
830#endif
831
832#if defined(WIN3264) && !defined(FEAT_GUI_W32)
833 mch_set_winsize_now(); /* Allow winsize changes from now on */
834#endif
835
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000836#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
837 /* When tab pages were created, may need to update the tab pages line and
838 * scrollbars. This is skipped while creating them. */
839 if (first_tabpage->tp_next != NULL)
840 {
841 out_flush();
842 gui_init_which_components(NULL);
843 gui_update_scrollbars(TRUE);
844 }
845 need_mouse_correct = TRUE;
846#endif
847
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000848 /* If ":startinsert" command used, stuff a dummy command to be able to
849 * call normal_cmd(), which will then start Insert mode. */
850 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000851 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000852
853#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200854 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200855 {
856# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200857# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200858 && !defined(FEAT_GUI_W32)
859 if (gui.in_use)
860 {
861 mch_errmsg(_("netbeans is not supported with this GUI\n"));
862 mch_exit(2);
863 }
864# endif
865# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000866 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200867 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200868 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869#endif
870
871 TIME_MSG("before starting main loop");
872
873 /*
874 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100875 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000876 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877
878 return 0;
879}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100880#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100881#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882
883/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200884 * Initialisation shared by main() and some tests.
885 */
886 void
887common_init(mparm_T *params)
888{
889
890#ifdef FEAT_MBYTE
891 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
892#endif
893#ifdef FEAT_EVAL
894 eval_init(); /* init global variables */
895#endif
896
897#ifdef __QNXNTO__
898 qnx_init(); /* PhAttach() for clipboard, (and gui) */
899#endif
900
901#ifdef MAC_OS_CLASSIC
902 /* Prepare for possibly starting GUI sometime */
903 /* Macintosh needs this before any memory is allocated. */
904 gui_prepare(&params->argc, params->argv);
905 TIME_MSG("GUI prepared");
906#endif
907
908 /* Init the table of Normal mode commands. */
909 init_normal_cmds();
910
911#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
912 make_version(); /* Construct the long version string. */
913#endif
914
915 /*
916 * Allocate space for the generic buffers (needed for set_init_1() and
917 * EMSG2()).
918 */
919 if ((IObuff = alloc(IOSIZE)) == NULL
920 || (NameBuff = alloc(MAXPATHL)) == NULL)
921 mch_exit(0);
922 TIME_MSG("Allocated generic buffers");
923
924#ifdef NBDEBUG
925 /* Wait a moment for debugging NetBeans. Must be after allocating
926 * NameBuff. */
927 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
928 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
929 TIME_MSG("NetBeans debug wait");
930#endif
931
932#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
933 /*
934 * Setup to use the current locale (for ctype() and many other things).
935 * NOTE: Translated messages with encodings other than latin1 will not
936 * work until set_init_1() has been called!
937 */
938 init_locale();
939 TIME_MSG("locale set");
940#endif
941
942#ifdef FEAT_GUI
943 gui.dofork = TRUE; /* default is to use fork() */
944#endif
945
946 /*
947 * Do a first scan of the arguments in "argv[]":
948 * -display or --display
949 * --server...
950 * --socketid
951 * --windowid
952 */
953 early_arg_scan(params);
954
955#ifdef FEAT_SUN_WORKSHOP
956 findYourself(params->argv[0]);
957#endif
958#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
959 /* Prepare for possibly starting GUI sometime */
960 gui_prepare(&params->argc, params->argv);
961 TIME_MSG("GUI prepared");
962#endif
963
964#ifdef FEAT_CLIPBOARD
965 clip_init(FALSE); /* Initialise clipboard stuff */
966 TIME_MSG("clipboard setup");
967#endif
968
969 /*
970 * Check if we have an interactive window.
971 * On the Amiga: If there is no window, we open one with a newcli command
972 * (needed for :! to * work). mch_check_win() will also handle the -d or
973 * -dev argument.
974 */
975 params->stdout_isatty = (mch_check_win(params->argc, params->argv) != FAIL);
976 TIME_MSG("window checked");
977
978 /*
979 * Allocate the first window and buffer.
980 * Can't do anything without it, exit when it fails.
981 */
982 if (win_alloc_first() == FAIL)
983 mch_exit(0);
984
985 init_yank(); /* init yank buffers */
986
987 alist_init(&global_alist); /* Init the argument list to empty. */
988 global_alist.id = 0;
989
990 /*
991 * Set the default values for the options.
992 * NOTE: Non-latin1 translated messages are working only after this,
993 * because this is where "has_mbyte" will be set, which is used by
994 * msg_outtrans_len_attr().
995 * First find out the home directory, needed to expand "~" in options.
996 */
997 init_homedir(); /* find real value of $HOME */
998 set_init_1();
999 TIME_MSG("inits 1");
1000
1001#ifdef FEAT_EVAL
1002 set_lang_var(); /* set v:lang and v:ctype */
1003#endif
1004}
1005
1006/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001007 * Main loop: Execute Normal mode commands until exiting Vim.
1008 * Also used to handle commands in the command-line window, until the window
1009 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010 * Also used to handle ":visual" command after ":global": execute Normal mode
1011 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001012 */
1013 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001014main_loop(
1015 int cmdwin, /* TRUE when working in the command-line window */
1016 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001017{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001018 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001019 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001020#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001021 /* these are static to avoid a compiler warning */
1022 static linenr_T conceal_old_cursor_line = 0;
1023 static linenr_T conceal_new_cursor_line = 0;
1024 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001025#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026
1027#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1028 /* Setup to catch a terminating error from the X server. Just ignore
1029 * it, restore the state and continue. This might not always work
1030 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001031 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001033 {
1034 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001035 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001036 got_int = TRUE;
1037 need_wait_return = FALSE;
1038 global_busy = FALSE;
1039 exmode_active = 0;
1040 skip_redraw = FALSE;
1041 RedrawingDisabled = 0;
1042 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001043 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001044# ifdef FEAT_EVAL
1045 emsg_skip = 0;
1046# endif
1047 emsg_off = 0;
1048# ifdef FEAT_MOUSE
1049 setmouse();
1050# endif
1051 settmode(TMODE_RAW);
1052 starttermcap();
1053 scroll_start();
1054 redraw_later_clear();
1055 }
1056#endif
1057
1058 clear_oparg(&oa);
1059 while (!cmdwin
1060#ifdef FEAT_CMDWIN
1061 || cmdwin_result == 0
1062#endif
1063 )
1064 {
1065 if (stuff_empty())
1066 {
1067 did_check_timestamps = FALSE;
1068 if (need_check_timestamps)
1069 check_timestamps(FALSE);
1070 if (need_wait_return) /* if wait_return still needed ... */
1071 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001072 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001073 {
1074 need_start_insertmode = FALSE;
1075 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1076 /* skip the fileinfo message now, because it would be shown
1077 * after insert mode finishes! */
1078 need_fileinfo = FALSE;
1079 }
1080 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001081
1082 /* Reset "got_int" now that we got back to the main loop. Except when
1083 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1084 * the ":g" command.
1085 * For ":g/pat/vi" we reset "got_int" when used once. When used
1086 * a second time we go back to Ex mode and abort the ":g" command. */
1087 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001088 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001089 if (noexmode && global_busy && !exmode_active && previous_got_int)
1090 {
1091 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1092 * used and keep "got_int" set, so that it aborts ":g". */
1093 exmode_active = EXMODE_NORMAL;
1094 State = NORMAL;
1095 }
1096 else if (!global_busy || !exmode_active)
1097 {
1098 if (!quit_more)
1099 (void)vgetc(); /* flush all buffers */
1100 got_int = FALSE;
1101 }
1102 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001103 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001104 else
1105 previous_got_int = FALSE;
1106
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001107 if (!exmode_active)
1108 msg_scroll = FALSE;
1109 quit_more = FALSE;
1110
1111 /*
1112 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1113 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1114 * update cursor and redraw.
1115 */
1116 if (skip_redraw || exmode_active)
1117 skip_redraw = FALSE;
1118 else if (do_redraw || stuff_empty())
1119 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001120#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001121 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001122 if (!finish_op && (
1123# ifdef FEAT_AUTOCMD
1124 has_cursormoved()
1125# endif
1126# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1127 ||
1128# endif
1129# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001130 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001131# endif
1132 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001133# ifdef FEAT_AUTOCMD
1134 && !equalpos(last_cursormoved, curwin->w_cursor)
1135# endif
1136 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001137 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001138# ifdef FEAT_AUTOCMD
1139 if (has_cursormoved())
1140 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1141 FALSE, curbuf);
1142# endif
1143# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001144 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001145 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001146# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001147 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001148# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001149 conceal_new_cursor_line = curwin->w_cursor.lnum;
1150 conceal_update_lines = TRUE;
1151 }
1152# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001153# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001154 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001155# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001156 }
1157#endif
1158
Bram Moolenaar186628f2013-03-19 13:33:23 +01001159#ifdef FEAT_AUTOCMD
1160 /* Trigger TextChanged if b_changedtick differs. */
1161 if (!finish_op && has_textchanged()
1162 && last_changedtick != curbuf->b_changedtick)
1163 {
1164 if (last_changedtick_buf == curbuf)
1165 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1166 FALSE, curbuf);
1167 last_changedtick_buf = curbuf;
1168 last_changedtick = curbuf->b_changedtick;
1169 }
1170#endif
1171
Bram Moolenaar33aec762006-01-22 23:30:12 +00001172#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1173 /* Scroll-binding for diff mode may have been postponed until
1174 * here. Avoids doing it for every change. */
1175 if (diff_need_scrollbind)
1176 {
1177 check_scrollbind((linenr_T)0, 0L);
1178 diff_need_scrollbind = FALSE;
1179 }
1180#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001181#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001182 /* Include a closed fold completely in the Visual area. */
1183 foldAdjustVisual();
1184#endif
1185#ifdef FEAT_FOLDING
1186 /*
1187 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1188 * contain the cursor.
1189 * When 'foldopen' is "all", open the fold(s) under the cursor.
1190 * This may mark the window for redrawing.
1191 */
1192 if (hasAnyFolding(curwin) && !char_avail())
1193 {
1194 foldCheckClose();
1195 if (fdo_flags & FDO_ALL)
1196 foldOpenCursor();
1197 }
1198#endif
1199
1200 /*
1201 * Before redrawing, make sure w_topline is correct, and w_leftcol
1202 * if lines don't wrap, and w_skipcol if lines wrap.
1203 */
1204 update_topline();
1205 validate_cursor();
1206
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001207 if (VIsual_active)
1208 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001209 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001210 update_screen(0);
1211 else if (redraw_cmdline || clear_cmdline)
1212 showmode();
1213#ifdef FEAT_WINDOWS
1214 redraw_statuslines();
1215#endif
1216#ifdef FEAT_TITLE
1217 if (need_maketitle)
1218 maketitle();
1219#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001220#ifdef FEAT_VIMINFO
1221 curbuf->b_last_used = vim_time();
1222#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001223 /* display message after redraw */
1224 if (keep_msg != NULL)
1225 {
1226 char_u *p;
1227
1228 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001229 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1230 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001231 p = keep_msg;
1232 msg_attr(p, keep_msg_attr);
1233 vim_free(p);
1234 }
1235 if (need_fileinfo) /* show file info after redraw */
1236 {
1237 fileinfo(FALSE, TRUE, FALSE);
1238 need_fileinfo = FALSE;
1239 }
1240
1241 emsg_on_display = FALSE; /* can delete error message now */
1242 did_emsg = FALSE;
1243 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001244 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001245 showruler(FALSE);
1246
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001247# if defined(FEAT_CONCEAL)
1248 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001249 && (conceal_old_cursor_line != conceal_new_cursor_line
1250 || conceal_cursor_line(curwin)
1251 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001252 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001253 if (conceal_old_cursor_line != conceal_new_cursor_line
1254 && conceal_old_cursor_line
1255 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001256 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001257 update_single_line(curwin, conceal_new_cursor_line);
1258 curwin->w_valid &= ~VALID_CROW;
1259 }
1260# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001261 setcursor();
1262 cursor_on();
1263
1264 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001265
1266#ifdef STARTUPTIME
1267 /* Now that we have drawn the first screen all the startup stuff
1268 * has been done, close any file for startup messages. */
1269 if (time_fd != NULL)
1270 {
1271 TIME_MSG("first screen update");
1272 TIME_MSG("--- VIM STARTED ---");
1273 fclose(time_fd);
1274 time_fd = NULL;
1275 }
1276#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001277 }
1278#ifdef FEAT_GUI
1279 if (need_mouse_correct)
1280 gui_mouse_correct();
1281#endif
1282
1283 /*
1284 * Update w_curswant if w_set_curswant has been set.
1285 * Postponed until here to avoid computing w_virtcol too often.
1286 */
1287 update_curswant();
1288
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001289#ifdef FEAT_EVAL
1290 /*
1291 * May perform garbage collection when waiting for a character, but
1292 * only at the very toplevel. Otherwise we may be using a List or
1293 * Dict internally somewhere.
1294 * "may_garbage_collect" is reset in vgetc() which is invoked through
1295 * do_exmode() and normal_cmd().
1296 */
1297 may_garbage_collect = (!cmdwin && !noexmode);
1298#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001299 /*
1300 * If we're invoked as ex, do a round of ex commands.
1301 * Otherwise, get and execute a normal mode command.
1302 */
1303 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001304 {
1305 if (noexmode) /* End of ":global/path/visual" commands */
1306 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001307 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001308 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001309 else
1310 normal_cmd(&oa, TRUE);
1311 }
1312}
1313
1314
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001315#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001316/*
1317 * Exit, but leave behind swap files for modified buffers.
1318 */
1319 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001320getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001321{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001322# if defined(SIGHUP) && defined(SIG_IGN)
1323 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1324 * makes Vim exit and then handling SIGHUP causes various reentrance
1325 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001326 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001327# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001328
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001329 ml_close_notmod(); /* close all not-modified buffers */
1330 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1331 ml_close_all(FALSE); /* close all memfiles, without deleting */
1332 getout(exitval); /* exit Vim properly */
1333}
1334#endif
1335
1336
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001337/*
1338 * Exit properly.
1339 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001340 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001341getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342{
1343#ifdef FEAT_AUTOCMD
1344 buf_T *buf;
1345 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001346 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001347#endif
1348
1349 exiting = TRUE;
1350
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001351 /* When running in Ex mode an error causes us to exit with a non-zero exit
1352 * code. POSIX requires this, although it's not 100% clear from the
1353 * standard. */
1354 if (exmode_active)
1355 exitval += ex_exitval;
1356
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001357 /* Position the cursor on the last screen line, below all the text */
1358#ifdef FEAT_GUI
1359 if (!gui.in_use)
1360#endif
1361 windgoto((int)Rows - 1, 0);
1362
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001363#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1364 /* Optionally print hashtable efficiency. */
1365 hash_debug_results();
1366#endif
1367
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001368#ifdef FEAT_GUI
1369 msg_didany = FALSE;
1370#endif
1371
1372#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001373 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001374 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001375 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1376# if defined FEAT_WINDOWS
1377 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001378 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001379 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001380 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001381 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001382 if (wp->w_buffer == NULL)
1383 /* Autocmd must have close the buffer already, skip. */
1384 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001385 buf = wp->w_buffer;
1386 if (buf->b_changedtick != -1)
1387 {
1388 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1389 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001390 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001391 /* start all over, autocommands may mess up the lists */
1392 next_tp = first_tabpage;
1393 break;
1394 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001395 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001397# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001398 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1399 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001400# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001401
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001402 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001403 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001404 if (buf->b_ml.ml_mfp != NULL)
1405 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001406 bufref_T bufref;
1407
1408 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001409 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001410 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001411 if (!bufref_valid(&bufref))
1412 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001413 break;
1414 }
1415 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1416 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001417#endif
1418
1419#ifdef FEAT_VIMINFO
1420 if (*p_viminfo != NUL)
1421 /* Write out the registers, history, marks etc, to the viminfo file */
1422 write_viminfo(NULL, FALSE);
1423#endif
1424
1425#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 if (get_vim_var_nr(VV_DYING) <= 1)
1427 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001428#endif
1429
Bram Moolenaar05159a02005-02-26 23:04:13 +00001430#ifdef FEAT_PROFILE
1431 profile_dump();
1432#endif
1433
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001434 if (did_emsg
1435#ifdef FEAT_GUI
1436 || (gui.in_use && msg_didany && p_verbose > 0)
1437#endif
1438 )
1439 {
1440 /* give the user a chance to read the (error) message */
1441 no_wait_return = FALSE;
1442 wait_return(FALSE);
1443 }
1444
1445#ifdef FEAT_AUTOCMD
1446 /* Position the cursor again, the autocommands may have moved it */
1447# ifdef FEAT_GUI
1448 if (!gui.in_use)
1449# endif
1450 windgoto((int)Rows - 1, 0);
1451#endif
1452
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001453#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001454 job_stop_on_exit();
1455#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001456#ifdef FEAT_LUA
1457 lua_end();
1458#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001459#ifdef FEAT_MZSCHEME
1460 mzscheme_end();
1461#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001462#ifdef FEAT_TCL
1463 tcl_end();
1464#endif
1465#ifdef FEAT_RUBY
1466 ruby_end();
1467#endif
1468#ifdef FEAT_PYTHON
1469 python_end();
1470#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001471#ifdef FEAT_PYTHON3
1472 python3_end();
1473#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001474#ifdef FEAT_PERL
1475 perl_end();
1476#endif
1477#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1478 iconv_end();
1479#endif
1480#ifdef FEAT_NETBEANS_INTG
1481 netbeans_end();
1482#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001483#ifdef FEAT_CSCOPE
1484 cs_end();
1485#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001486#ifdef FEAT_EVAL
1487 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001488 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001489#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001490#if defined(WIN32) && defined(FEAT_MBYTE)
1491 free_cmd_argsW();
1492#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001493
1494 mch_exit(exitval);
1495}
1496
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001497#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1498/*
1499 * Setup to use the current locale (for ctype() and many other things).
1500 */
1501 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001502init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001503{
1504 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001505
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001506# ifdef FEAT_GUI_GTK
1507 /* Tell Gtk not to change our locale settings. */
1508 gtk_disable_setlocale();
1509# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001510# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1511 /* Make sure strtod() uses a decimal point, not a comma. */
1512 setlocale(LC_NUMERIC, "C");
1513# endif
1514
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001515# ifdef WIN32
1516 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1517 * text while it's expecting text in the current locale. This call avoids
1518 * that. */
1519 setlocale(LC_CTYPE, "C");
1520# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001521
1522# ifdef FEAT_GETTEXT
1523 {
1524 int mustfree = FALSE;
1525 char_u *p;
1526
1527# ifdef DYNAMIC_GETTEXT
1528 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001529 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001530# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001531 /* expand_env() doesn't work yet, because g_chartab[] is not
1532 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001533 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1534 if (p != NULL && *p != NUL)
1535 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001536 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001537 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1538 }
1539 if (mustfree)
1540 vim_free(p);
1541 textdomain(VIMPACKAGE);
1542 }
1543# endif
1544}
1545#endif
1546
1547/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001548 * Get the name of the display, before gui_prepare() removes it from
1549 * argv[]. Used for the xterm-clipboard display.
1550 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001551 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001552 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001553 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001554early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001555{
Bram Moolenaar03005972008-11-20 13:12:36 +00001556#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1557 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001558 int argc = parmp->argc;
1559 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001560 int i;
1561
1562 for (i = 1; i < argc; i++)
1563 {
1564 if (STRCMP(argv[i], "--") == 0)
1565 break;
1566# ifdef FEAT_XCLIPBOARD
1567 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001568# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001569 || STRICMP(argv[i], "--display") == 0
1570# endif
1571 )
1572 {
1573 if (i == argc - 1)
1574 mainerr_arg_missing((char_u *)argv[i]);
1575 xterm_display = argv[++i];
1576 }
1577# endif
1578# ifdef FEAT_CLIENTSERVER
1579 else if (STRICMP(argv[i], "--servername") == 0)
1580 {
1581 if (i == argc - 1)
1582 mainerr_arg_missing((char_u *)argv[i]);
1583 parmp->serverName_arg = (char_u *)argv[++i];
1584 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001585 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001586 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001587 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001588 {
1589 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001590# ifdef FEAT_GUI
1591 if (strstr(argv[i], "-wait") != 0)
1592 /* don't fork() when starting the GUI to edit files ourself */
1593 gui.dofork = FALSE;
1594# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001595 }
1596# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001597
1598# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1599# ifdef FEAT_GUI_W32
1600 else if (STRICMP(argv[i], "--windowid") == 0)
1601# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001603# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001604 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001605 long_u id;
1606 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001607
1608 if (i == argc - 1)
1609 mainerr_arg_missing((char_u *)argv[i]);
1610 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001611 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001612 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001613 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001614 if (count != 1)
1615 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1616 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001617# ifdef FEAT_GUI_W32
1618 win_socket_id = id;
1619# else
1620 gtk_socket_id = id;
1621# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001622 i++;
1623 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001624# endif
1625# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001626 else if (STRICMP(argv[i], "--echo-wid") == 0)
1627 echo_wid_arg = TRUE;
1628# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001629# ifndef FEAT_NETBEANS_INTG
1630 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001631 {
1632 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1633 mch_exit(2);
1634 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001635# endif
1636
Bram Moolenaar58d98232005-07-23 22:25:46 +00001637 }
1638#endif
1639}
1640
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001641#ifndef NO_VIM_MAIN
1642/*
1643 * Get a (optional) count for a Vim argument.
1644 */
1645 static int
1646get_number_arg(
1647 char_u *p, /* pointer to argument */
1648 int *idx, /* index in argument, is incremented */
1649 int def) /* default value */
1650{
1651 if (vim_isdigit(p[*idx]))
1652 {
1653 def = atoi((char *)&(p[*idx]));
1654 while (vim_isdigit(p[*idx]))
1655 *idx = *idx + 1;
1656 }
1657 return def;
1658}
1659
1660/*
1661 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1662 * If the executable name starts with "r" we disable shell commands.
1663 * If the next character is "e" we run in Easy mode.
1664 * If the next character is "g" we run the GUI version.
1665 * If the next characters are "view" we start in readonly mode.
1666 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1667 * If the next characters are "ex" we start in Ex mode. If it's followed
1668 * by "im" use improved Ex mode.
1669 */
1670 static void
1671parse_command_name(mparm_T *parmp)
1672{
1673 char_u *initstr;
1674
1675 initstr = gettail((char_u *)parmp->argv[0]);
1676
1677#ifdef MACOS_X_UNIX
1678 /* An issue has been seen when launching Vim in such a way that
1679 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1680 * executable or a symbolic link of it. Until this issue is resolved
1681 * we prohibit the GUI from being used.
1682 */
1683 if (STRCMP(initstr, parmp->argv[0]) == 0)
1684 disallow_gui = TRUE;
1685
1686 /* TODO: On MacOS X default to gui if argv[0] ends in:
1687 * /Vim.app/Contents/MacOS/Vim */
1688#endif
1689
1690#ifdef FEAT_EVAL
1691 set_vim_var_string(VV_PROGNAME, initstr, -1);
1692 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
1693#endif
1694
1695 if (TOLOWER_ASC(initstr[0]) == 'r')
1696 {
1697 restricted = TRUE;
1698 ++initstr;
1699 }
1700
1701 /* Use evim mode for "evim" and "egvim", not for "editor". */
1702 if (TOLOWER_ASC(initstr[0]) == 'e'
1703 && (TOLOWER_ASC(initstr[1]) == 'v'
1704 || TOLOWER_ASC(initstr[1]) == 'g'))
1705 {
1706#ifdef FEAT_GUI
1707 gui.starting = TRUE;
1708#endif
1709 parmp->evim_mode = TRUE;
1710 ++initstr;
1711 }
1712
1713 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1714 if (TOLOWER_ASC(initstr[0]) == 'g')
1715 {
1716 main_start_gui();
1717#ifdef FEAT_GUI
1718 ++initstr;
1719#endif
1720 }
1721
1722 if (STRNICMP(initstr, "view", 4) == 0)
1723 {
1724 readonlymode = TRUE;
1725 curbuf->b_p_ro = TRUE;
1726 p_uc = 10000; /* don't update very often */
1727 initstr += 4;
1728 }
1729 else if (STRNICMP(initstr, "vim", 3) == 0)
1730 initstr += 3;
1731
1732 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1733 if (STRICMP(initstr, "diff") == 0)
1734 {
1735#ifdef FEAT_DIFF
1736 parmp->diff_mode = TRUE;
1737#else
1738 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1739 mch_errmsg("\n");
1740 mch_exit(2);
1741#endif
1742 }
1743
1744 if (STRNICMP(initstr, "ex", 2) == 0)
1745 {
1746 if (STRNICMP(initstr + 2, "im", 2) == 0)
1747 exmode_active = EXMODE_VIM;
1748 else
1749 exmode_active = EXMODE_NORMAL;
1750 change_compatible(TRUE); /* set 'compatible' */
1751 }
1752}
1753
Bram Moolenaar58d98232005-07-23 22:25:46 +00001754/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001755 * Scan the command line arguments.
1756 */
1757 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001758command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001759{
1760 int argc = parmp->argc;
1761 char **argv = parmp->argv;
1762 int argv_idx; /* index in argv[n][] */
1763 int had_minmin = FALSE; /* found "--" argument */
1764 int want_argument; /* option argument with argument */
1765 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001766 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001767 long n;
1768
1769 --argc;
1770 ++argv;
1771 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1772 while (argc > 0)
1773 {
1774 /*
1775 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1776 */
1777 if (argv[0][0] == '+' && !had_minmin)
1778 {
1779 if (parmp->n_commands >= MAX_ARG_CMDS)
1780 mainerr(ME_EXTRA_CMD, NULL);
1781 argv_idx = -1; /* skip to next argument */
1782 if (argv[0][1] == NUL)
1783 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1784 else
1785 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1786 }
1787
1788 /*
1789 * Optional argument.
1790 */
1791 else if (argv[0][0] == '-' && !had_minmin)
1792 {
1793 want_argument = FALSE;
1794 c = argv[0][argv_idx++];
1795#ifdef VMS
1796 /*
1797 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1798 * and "-/X" as "-X".
1799 */
1800 if (c == '/')
1801 {
1802 c = argv[0][argv_idx++];
1803 c = TOUPPER_ASC(c);
1804 }
1805 else
1806 c = TOLOWER_ASC(c);
1807#endif
1808 switch (c)
1809 {
1810 case NUL: /* "vim -" read from stdin */
1811 /* "ex -" silent mode */
1812 if (exmode_active)
1813 silent_mode = TRUE;
1814 else
1815 {
1816 if (parmp->edit_type != EDIT_NONE)
1817 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1818 parmp->edit_type = EDIT_STDIN;
1819 read_cmd_fd = 2; /* read from stderr instead of stdin */
1820 }
1821 argv_idx = -1; /* skip to next argument */
1822 break;
1823
1824 case '-': /* "--" don't take any more option arguments */
1825 /* "--help" give help message */
1826 /* "--version" give version message */
1827 /* "--literal" take files literally */
1828 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001829 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001830 /* "--noplugin[s]" skip plugins */
1831 /* "--cmd <cmd>" execute cmd before vimrc */
1832 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1833 usage();
1834 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1835 {
1836 Columns = 80; /* need to init Columns */
1837 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1838 list_version();
1839 msg_putchar('\n');
1840 msg_didout = FALSE;
1841 mch_exit(0);
1842 }
1843 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1844 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001845#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001846 parmp->literal = TRUE;
1847#endif
1848 }
1849 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1850 {
1851#ifdef FEAT_GUI
1852 gui.dofork = FALSE; /* don't fork() when starting GUI */
1853#endif
1854 }
1855 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1856 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001857 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1858 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001859 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1860 {
1861 want_argument = TRUE;
1862 argv_idx += 3;
1863 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001864 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1865 {
1866 want_argument = TRUE;
1867 argv_idx += 11;
1868 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001869#ifdef FEAT_CLIENTSERVER
1870 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1871 ; /* already processed -- no arg */
1872 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1873 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1874 {
1875 /* already processed -- snatch the following arg */
1876 if (argc > 1)
1877 {
1878 --argc;
1879 ++argv;
1880 }
1881 }
1882#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001883#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1884# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001885 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001886# else
1887 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1888# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001889 {
1890 /* already processed -- snatch the following arg */
1891 if (argc > 1)
1892 {
1893 --argc;
1894 ++argv;
1895 }
1896 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001897#endif
1898#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001899 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1900 {
1901 /* already processed, skip */
1902 }
1903#endif
1904 else
1905 {
1906 if (argv[0][argv_idx])
1907 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1908 had_minmin = TRUE;
1909 }
1910 if (!want_argument)
1911 argv_idx = -1; /* skip to next argument */
1912 break;
1913
1914 case 'A': /* "-A" start in Arabic mode */
1915#ifdef FEAT_ARABIC
1916 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1917#else
1918 mch_errmsg(_(e_noarabic));
1919 mch_exit(2);
1920#endif
1921 break;
1922
1923 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001924 /* Needs to be effective before expanding file names, because
1925 * for Win32 this makes us edit a shortcut file itself,
1926 * instead of the file it links to. */
1927 set_options_bin(curbuf->b_p_bin, 1, 0);
1928 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001929 break;
1930
1931 case 'C': /* "-C" Compatible */
1932 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001933 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001934 break;
1935
1936 case 'e': /* "-e" Ex mode */
1937 exmode_active = EXMODE_NORMAL;
1938 break;
1939
1940 case 'E': /* "-E" Improved Ex mode */
1941 exmode_active = EXMODE_VIM;
1942 break;
1943
1944 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1945 window directly, not with newcli */
1946#ifdef FEAT_GUI
1947 gui.dofork = FALSE; /* don't fork() when starting GUI */
1948#endif
1949 break;
1950
1951 case 'g': /* "-g" start GUI */
1952 main_start_gui();
1953 break;
1954
1955 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1956#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001957 p_fkmap = TRUE;
1958 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001959#else
1960 mch_errmsg(_(e_nofarsi));
1961 mch_exit(2);
1962#endif
1963 break;
1964
1965 case 'h': /* "-h" give help message */
1966#ifdef FEAT_GUI_GNOME
1967 /* Tell usage() to exit for "gvim". */
1968 gui.starting = FALSE;
1969#endif
1970 usage();
1971 break;
1972
1973 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1974#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001975 p_hkmap = TRUE;
1976 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001977#else
1978 mch_errmsg(_(e_nohebrew));
1979 mch_exit(2);
1980#endif
1981 break;
1982
1983 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1984#ifdef FEAT_LISP
1985 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1986 p_sm = TRUE;
1987#endif
1988 break;
1989
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001990 case 'M': /* "-M" no changes or writing of files */
1991 reset_modifiable();
1992 /* FALLTHROUGH */
1993
1994 case 'm': /* "-m" no writing of files */
1995 p_write = FALSE;
1996 break;
1997
1998 case 'y': /* "-y" easy mode */
1999#ifdef FEAT_GUI
2000 gui.starting = TRUE; /* start GUI a bit later */
2001#endif
2002 parmp->evim_mode = TRUE;
2003 break;
2004
2005 case 'N': /* "-N" Nocompatible */
2006 change_compatible(FALSE);
2007 break;
2008
2009 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002010#ifdef FEAT_NETBEANS_INTG
2011 /* checking for "-nb", netbeans parameters */
2012 if (argv[0][argv_idx] == 'b')
2013 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002014 netbeansArg = argv[0];
2015 argv_idx = -1; /* skip to next argument */
2016 }
2017 else
2018#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002019 parmp->no_swap_file = TRUE;
2020 break;
2021
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002022 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002023#ifdef TARGET_API_MAC_OSX
2024 /* For some reason on MacOS X, an argument like:
2025 -psn_0_10223617 is passed in when invoke from Finder
2026 or with the 'open' command */
2027 if (argv[0][argv_idx] == 's')
2028 {
2029 argv_idx = -1; /* bypass full -psn */
2030 main_start_gui();
2031 break;
2032 }
2033#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002034#ifdef FEAT_WINDOWS
2035 /* default is 0: open window for each file */
2036 parmp->window_count = get_number_arg((char_u *)argv[0],
2037 &argv_idx, 0);
2038 parmp->window_layout = WIN_TABS;
2039#endif
2040 break;
2041
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002042 case 'o': /* "-o[N]" open N horizontal split windows */
2043#ifdef FEAT_WINDOWS
2044 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002045 parmp->window_count = get_number_arg((char_u *)argv[0],
2046 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002047 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002048#endif
2049 break;
2050
2051 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002052#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002053 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002054 parmp->window_count = get_number_arg((char_u *)argv[0],
2055 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002056 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002057#endif
2058 break;
2059
2060#ifdef FEAT_QUICKFIX
2061 case 'q': /* "-q" QuickFix mode */
2062 if (parmp->edit_type != EDIT_NONE)
2063 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2064 parmp->edit_type = EDIT_QF;
2065 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2066 {
2067 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2068 argv_idx = -1;
2069 }
2070 else if (argc > 1) /* "-q {errorfile}" */
2071 want_argument = TRUE;
2072 break;
2073#endif
2074
2075 case 'R': /* "-R" readonly mode */
2076 readonlymode = TRUE;
2077 curbuf->b_p_ro = TRUE;
2078 p_uc = 10000; /* don't update very often */
2079 break;
2080
2081 case 'r': /* "-r" recovery mode */
2082 case 'L': /* "-L" recovery mode */
2083 recoverymode = 1;
2084 break;
2085
2086 case 's':
2087 if (exmode_active) /* "-s" silent (batch) mode */
2088 silent_mode = TRUE;
2089 else /* "-s {scriptin}" read from script file */
2090 want_argument = TRUE;
2091 break;
2092
2093 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2094 if (parmp->edit_type != EDIT_NONE)
2095 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2096 parmp->edit_type = EDIT_TAG;
2097 if (argv[0][argv_idx]) /* "-t{tag}" */
2098 {
2099 parmp->tagname = (char_u *)argv[0] + argv_idx;
2100 argv_idx = -1;
2101 }
2102 else /* "-t {tag}" */
2103 want_argument = TRUE;
2104 break;
2105
2106#ifdef FEAT_EVAL
2107 case 'D': /* "-D" Debugging */
2108 parmp->use_debug_break_level = 9999;
2109 break;
2110#endif
2111#ifdef FEAT_DIFF
2112 case 'd': /* "-d" 'diff' */
2113# ifdef AMIGA
2114 /* check for "-dev {device}" */
2115 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2116 want_argument = TRUE;
2117 else
2118# endif
2119 parmp->diff_mode = TRUE;
2120 break;
2121#endif
2122 case 'V': /* "-V{N}" Verbose level */
2123 /* default is 10: a little bit verbose */
2124 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2125 if (argv[0][argv_idx] != NUL)
2126 {
2127 set_option_value((char_u *)"verbosefile", 0L,
2128 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002129 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002130 }
2131 break;
2132
2133 case 'v': /* "-v" Vi-mode (as if called "vi") */
2134 exmode_active = 0;
2135#ifdef FEAT_GUI
2136 gui.starting = FALSE; /* don't start GUI */
2137#endif
2138 break;
2139
2140 case 'w': /* "-w{number}" set window height */
2141 /* "-w {scriptout}" write to script */
2142 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2143 {
2144 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2145 set_option_value((char_u *)"window", n, NULL, 0);
2146 break;
2147 }
2148 want_argument = TRUE;
2149 break;
2150
2151#ifdef FEAT_CRYPT
2152 case 'x': /* "-x" encrypted reading/writing of files */
2153 parmp->ask_for_key = TRUE;
2154 break;
2155#endif
2156
2157 case 'X': /* "-X" don't connect to X server */
2158#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2159 x_no_connect = TRUE;
2160#endif
2161 break;
2162
2163 case 'Z': /* "-Z" restricted mode */
2164 restricted = TRUE;
2165 break;
2166
2167 case 'c': /* "-c{command}" or "-c {command}" execute
2168 command */
2169 if (argv[0][argv_idx] != NUL)
2170 {
2171 if (parmp->n_commands >= MAX_ARG_CMDS)
2172 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002173 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2174 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002175 argv_idx = -1;
2176 break;
2177 }
2178 /*FALLTHROUGH*/
2179 case 'S': /* "-S {file}" execute Vim script */
2180 case 'i': /* "-i {viminfo}" use for viminfo */
2181#ifndef FEAT_DIFF
2182 case 'd': /* "-d {device}" device (for Amiga) */
2183#endif
2184 case 'T': /* "-T {terminal}" terminal name */
2185 case 'u': /* "-u {vimrc}" vim inits file */
2186 case 'U': /* "-U {gvimrc}" gvim inits file */
2187 case 'W': /* "-W {scriptout}" overwrite */
2188#ifdef FEAT_GUI_W32
2189 case 'P': /* "-P {parent title}" MDI parent */
2190#endif
2191 want_argument = TRUE;
2192 break;
2193
2194 default:
2195 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2196 }
2197
2198 /*
2199 * Handle option arguments with argument.
2200 */
2201 if (want_argument)
2202 {
2203 /*
2204 * Check for garbage immediately after the option letter.
2205 */
2206 if (argv[0][argv_idx] != NUL)
2207 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2208
2209 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002210 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002211 mainerr_arg_missing((char_u *)argv[0]);
2212 ++argv;
2213 argv_idx = -1;
2214
2215 switch (c)
2216 {
2217 case 'c': /* "-c {command}" execute command */
2218 case 'S': /* "-S {file}" execute Vim script */
2219 if (parmp->n_commands >= MAX_ARG_CMDS)
2220 mainerr(ME_EXTRA_CMD, NULL);
2221 if (c == 'S')
2222 {
2223 char *a;
2224
2225 if (argc < 1)
2226 /* "-S" without argument: use default session file
2227 * name. */
2228 a = SESSION_FILE;
2229 else if (argv[0][0] == '-')
2230 {
2231 /* "-S" followed by another option: use default
2232 * session file name. */
2233 a = SESSION_FILE;
2234 ++argc;
2235 --argv;
2236 }
2237 else
2238 a = argv[0];
2239 p = alloc((unsigned)(STRLEN(a) + 4));
2240 if (p == NULL)
2241 mch_exit(2);
2242 sprintf((char *)p, "so %s", a);
2243 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2244 parmp->commands[parmp->n_commands++] = p;
2245 }
2246 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002247 parmp->commands[parmp->n_commands++] =
2248 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002249 break;
2250
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002251 case '-':
2252 if (argv[-1][2] == 'c')
2253 {
2254 /* "--cmd {command}" execute command */
2255 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2256 mainerr(ME_EXTRA_CMD, NULL);
2257 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002258 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002259 }
2260 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002261 break;
2262
2263 /* case 'd': -d {device} is handled in mch_check_win() for the
2264 * Amiga */
2265
2266#ifdef FEAT_QUICKFIX
2267 case 'q': /* "-q {errorfile}" QuickFix mode */
2268 parmp->use_ef = (char_u *)argv[0];
2269 break;
2270#endif
2271
2272 case 'i': /* "-i {viminfo}" use for viminfo */
2273 use_viminfo = (char_u *)argv[0];
2274 break;
2275
2276 case 's': /* "-s {scriptin}" read from script file */
2277 if (scriptin[0] != NULL)
2278 {
2279scripterror:
2280 mch_errmsg(_("Attempt to open script file again: \""));
2281 mch_errmsg(argv[-1]);
2282 mch_errmsg(" ");
2283 mch_errmsg(argv[0]);
2284 mch_errmsg("\"\n");
2285 mch_exit(2);
2286 }
2287 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2288 {
2289 mch_errmsg(_("Cannot open for reading: \""));
2290 mch_errmsg(argv[0]);
2291 mch_errmsg("\"\n");
2292 mch_exit(2);
2293 }
2294 if (save_typebuf() == FAIL)
2295 mch_exit(2); /* out of memory */
2296 break;
2297
2298 case 't': /* "-t {tag}" */
2299 parmp->tagname = (char_u *)argv[0];
2300 break;
2301
2302 case 'T': /* "-T {terminal}" terminal name */
2303 /*
2304 * The -T term argument is always available and when
2305 * HAVE_TERMLIB is supported it overrides the environment
2306 * variable TERM.
2307 */
2308#ifdef FEAT_GUI
2309 if (term_is_gui((char_u *)argv[0]))
2310 gui.starting = TRUE; /* start GUI a bit later */
2311 else
2312#endif
2313 parmp->term = (char_u *)argv[0];
2314 break;
2315
2316 case 'u': /* "-u {vimrc}" vim inits file */
2317 parmp->use_vimrc = (char_u *)argv[0];
2318 break;
2319
2320 case 'U': /* "-U {gvimrc}" gvim inits file */
2321#ifdef FEAT_GUI
2322 use_gvimrc = (char_u *)argv[0];
2323#endif
2324 break;
2325
2326 case 'w': /* "-w {nr}" 'window' value */
2327 /* "-w {scriptout}" append to script file */
2328 if (vim_isdigit(*((char_u *)argv[0])))
2329 {
2330 argv_idx = 0;
2331 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2332 set_option_value((char_u *)"window", n, NULL, 0);
2333 argv_idx = -1;
2334 break;
2335 }
2336 /*FALLTHROUGH*/
2337 case 'W': /* "-W {scriptout}" overwrite script file */
2338 if (scriptout != NULL)
2339 goto scripterror;
2340 if ((scriptout = mch_fopen(argv[0],
2341 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2342 {
2343 mch_errmsg(_("Cannot open for script output: \""));
2344 mch_errmsg(argv[0]);
2345 mch_errmsg("\"\n");
2346 mch_exit(2);
2347 }
2348 break;
2349
2350#ifdef FEAT_GUI_W32
2351 case 'P': /* "-P {parent title}" MDI parent */
2352 gui_mch_set_parent(argv[0]);
2353 break;
2354#endif
2355 }
2356 }
2357 }
2358
2359 /*
2360 * File name argument.
2361 */
2362 else
2363 {
2364 argv_idx = -1; /* skip to next argument */
2365
2366 /* Check for only one type of editing. */
2367 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2368 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2369 parmp->edit_type = EDIT_FILE;
2370
2371#ifdef MSWIN
2372 /* Remember if the argument was a full path before changing
2373 * slashes to backslashes. */
2374 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2375 parmp->full_path = TRUE;
2376#endif
2377
2378 /* Add the file to the global argument list. */
2379 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2380 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2381 mch_exit(2);
2382#ifdef FEAT_DIFF
2383 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2384 && !mch_isdir(alist_name(&GARGLIST[0])))
2385 {
2386 char_u *r;
2387
2388 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2389 if (r != NULL)
2390 {
2391 vim_free(p);
2392 p = r;
2393 }
2394 }
2395#endif
2396#if defined(__CYGWIN32__) && !defined(WIN32)
2397 /*
2398 * If vim is invoked by non-Cygwin tools, convert away any
2399 * DOS paths, so things like .swp files are created correctly.
2400 * Look for evidence of non-Cygwin paths before we bother.
2401 * This is only for when using the Unix files.
2402 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002403 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002404 {
2405 char posix_path[PATH_MAX];
2406
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002407# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2408 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2409# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002410 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002411# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002412 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002413 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002414 if (p == NULL)
2415 mch_exit(2);
2416 }
2417#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002418
2419#ifdef USE_FNAME_CASE
2420 /* Make the case of the file name match the actual file. */
2421 fname_case(p, 0);
2422#endif
2423
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002424 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002425#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002426 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002427#else
2428 2 /* add buffer number now and use curbuf */
2429#endif
2430 );
2431
2432#if defined(FEAT_MBYTE) && defined(WIN32)
2433 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002434 /* Remember this argument has been added to the argument list.
2435 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002436 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002437# ifdef FEAT_DIFF
2438 parmp->diff_mode
2439# else
2440 FALSE
2441# endif
2442 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443 }
2444#endif
2445 }
2446
2447 /*
2448 * If there are no more letters after the current "-", go to next
2449 * argument. argv_idx is set to -1 when the current argument is to be
2450 * skipped.
2451 */
2452 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2453 {
2454 --argc;
2455 ++argv;
2456 argv_idx = 1;
2457 }
2458 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002459
2460#ifdef FEAT_EVAL
2461 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2462 * one. */
2463 if (parmp->n_commands > 0)
2464 {
2465 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2466 if (p != NULL)
2467 {
2468 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2469 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2470 vim_free(p);
2471 }
2472 }
2473#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002474}
2475
2476/*
2477 * Print a warning if stdout is not a terminal.
2478 * When starting in Ex mode and commands come from a file, set Silent mode.
2479 */
2480 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002481check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002482{
2483 int input_isatty; /* is active input a terminal? */
2484
2485 input_isatty = mch_input_isatty();
2486 if (exmode_active)
2487 {
2488 if (!input_isatty)
2489 silent_mode = TRUE;
2490 }
2491 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2492#ifdef FEAT_GUI
2493 /* don't want the delay when started from the desktop */
2494 && !gui.starting
2495#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002496 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002497 {
2498#ifdef NBDEBUG
2499 /*
2500 * This shouldn't be necessary. But if I run netbeans with the log
2501 * output coming to the console and XOpenDisplay fails, I get vim
2502 * trying to start with input/output to my console tty. This fills my
2503 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002504 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002505 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002506 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002507 {
2508 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2509 exit(1);
2510 }
2511#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002512#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2513 if (is_cygpty_used())
2514 {
2515 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2516 exit(1);
2517 }
2518#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002519 if (!parmp->stdout_isatty)
2520 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2521 if (!input_isatty)
2522 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2523 out_flush();
2524 if (scriptin[0] == NULL)
2525 ui_delay(2000L, TRUE);
2526 TIME_MSG("Warning delay");
2527 }
2528}
2529
2530/*
2531 * Read text from stdin.
2532 */
2533 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002534read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002535{
2536 int i;
2537
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002538#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002539 /* When getting the ATTENTION prompt here, use a dialog */
2540 swap_exists_action = SEA_DIALOG;
2541#endif
2542 no_wait_return = TRUE;
2543 i = msg_didany;
2544 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002545 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546 no_wait_return = FALSE;
2547 msg_didany = i;
2548 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002549#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002550 check_swap_exists_action();
2551#endif
2552#if !(defined(AMIGA) || defined(MACOS))
2553 /*
2554 * Close stdin and dup it from stderr. Required for GPM to work
2555 * properly, and for running external commands.
2556 * Is there any other system that cannot do this?
2557 */
2558 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002559 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560#endif
2561}
2562
2563/*
2564 * Create the requested number of windows and edit buffers in them.
2565 * Also does recovery if "recoverymode" set.
2566 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002568create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002569{
2570#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002571 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002572 int done = 0;
2573
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 /*
2575 * Create the number of windows that was requested.
2576 */
2577 if (parmp->window_count == -1) /* was not set */
2578 parmp->window_count = 1;
2579 if (parmp->window_count == 0)
2580 parmp->window_count = GARGCOUNT;
2581 if (parmp->window_count > 1)
2582 {
2583 /* Don't change the windows if there was a command in .vimrc that
2584 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002585 if (parmp->window_layout == 0)
2586 parmp->window_layout = WIN_HOR;
2587 if (parmp->window_layout == WIN_TABS)
2588 {
2589 parmp->window_count = make_tabpages(parmp->window_count);
2590 TIME_MSG("making tab pages");
2591 }
2592 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002593 {
2594 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002595 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596 TIME_MSG("making windows");
2597 }
2598 else
2599 parmp->window_count = win_count();
2600 }
2601 else
2602 parmp->window_count = 1;
2603#endif
2604
2605 if (recoverymode) /* do recover */
2606 {
2607 msg_scroll = TRUE; /* scroll message up */
2608 ml_recover();
2609 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2610 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002611 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002612 }
2613 else
2614 {
2615 /*
2616 * Open a buffer for windows that don't have one yet.
2617 * Commands in the .vimrc might have loaded a file or split the window.
2618 * Watch out for autocommands that delete a window.
2619 */
2620#ifdef FEAT_AUTOCMD
2621 /*
2622 * Don't execute Win/Buf Enter/Leave autocommands here
2623 */
2624 ++autocmd_no_enter;
2625 ++autocmd_no_leave;
2626#endif
2627#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002628 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002629 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002630 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002631 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002632 {
2633 if (parmp->window_layout == WIN_TABS)
2634 goto_tabpage(1);
2635 else
2636 curwin = firstwin;
2637 }
2638 else if (parmp->window_layout == WIN_TABS)
2639 {
2640 if (curtab->tp_next == NULL)
2641 break;
2642 goto_tabpage(0);
2643 }
2644 else
2645 {
2646 if (curwin->w_next == NULL)
2647 break;
2648 curwin = curwin->w_next;
2649 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002650 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002651#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002652 curbuf = curwin->w_buffer;
2653 if (curbuf->b_ml.ml_mfp == NULL)
2654 {
2655#ifdef FEAT_FOLDING
2656 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2657 if (p_fdls >= 0)
2658 curwin->w_p_fdl = p_fdls;
2659#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002660#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002661 /* When getting the ATTENTION prompt here, use a dialog */
2662 swap_exists_action = SEA_DIALOG;
2663#endif
2664 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002665
2666 /* create memfile, read file */
2667 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002668
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002669#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002670 if (swap_exists_action == SEA_QUIT)
2671 {
2672 if (got_int || only_one_window())
2673 {
2674 /* abort selected or quit and only one window */
2675 did_emsg = FALSE; /* avoid hit-enter prompt */
2676 getout(1);
2677 }
2678 /* We can't close the window, it would disturb what
2679 * happens next. Clear the file name and set the arg
2680 * index to -1 to delete it later. */
2681 setfname(curbuf, NULL, NULL, FALSE);
2682 curwin->w_arg_idx = -1;
2683 swap_exists_action = SEA_NONE;
2684 }
2685 else
2686 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002687#endif
2688#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002689 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690#endif
2691 }
2692#ifdef FEAT_WINDOWS
2693 ui_breakcheck();
2694 if (got_int)
2695 {
2696 (void)vgetc(); /* only break the file loading, not the rest */
2697 break;
2698 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002699 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002700#endif
2701#ifdef FEAT_WINDOWS
2702 if (parmp->window_layout == WIN_TABS)
2703 goto_tabpage(1);
2704 else
2705 curwin = firstwin;
2706 curbuf = curwin->w_buffer;
2707#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708#ifdef FEAT_AUTOCMD
2709 --autocmd_no_enter;
2710 --autocmd_no_leave;
2711#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002712 }
2713}
2714
2715#ifdef FEAT_WINDOWS
2716 /*
2717 * If opened more than one window, start editing files in the other
2718 * windows. make_windows() has already opened the windows.
2719 */
2720 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002721edit_buffers(
2722 mparm_T *parmp,
2723 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002724{
2725 int arg_idx; /* index in argument list */
2726 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002727 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002728 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002729
2730# ifdef FEAT_AUTOCMD
2731 /*
2732 * Don't execute Win/Buf Enter/Leave autocommands here
2733 */
2734 ++autocmd_no_enter;
2735 ++autocmd_no_leave;
2736# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002737
2738 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2739 if (curwin->w_arg_idx == -1)
2740 {
2741 win_close(curwin, TRUE);
2742 advance = FALSE;
2743 }
2744
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002745 arg_idx = 1;
2746 for (i = 1; i < parmp->window_count; ++i)
2747 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002748 if (cwd != NULL)
2749 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002750 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2751 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002752 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002753 ++arg_idx;
2754 win_close(curwin, TRUE);
2755 advance = FALSE;
2756 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002757 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758
Bram Moolenaar84212822006-11-07 21:59:47 +00002759 if (advance)
2760 {
2761 if (parmp->window_layout == WIN_TABS)
2762 {
2763 if (curtab->tp_next == NULL) /* just checking */
2764 break;
2765 goto_tabpage(0);
2766 }
2767 else
2768 {
2769 if (curwin->w_next == NULL) /* just checking */
2770 break;
2771 win_enter(curwin->w_next, FALSE);
2772 }
2773 }
2774 advance = TRUE;
2775
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002776 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002777 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002778 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2779 {
2780 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002781 /* Edit file from arg list, if there is one. When "Quit" selected
2782 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002783# ifdef HAS_SWAP_EXISTS_ACTION
2784 swap_exists_did_quit = FALSE;
2785# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002786 (void)do_ecmd(0, arg_idx < GARGCOUNT
2787 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002788 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002789# ifdef HAS_SWAP_EXISTS_ACTION
2790 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002791 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002792 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002793 if (got_int || only_one_window())
2794 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002795 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002796 did_emsg = FALSE; /* avoid hit-enter prompt */
2797 getout(1);
2798 }
2799 win_close(curwin, TRUE);
2800 advance = FALSE;
2801 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002802# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002803 if (arg_idx == GARGCOUNT - 1)
2804 arg_had_last = TRUE;
2805 ++arg_idx;
2806 }
2807 ui_breakcheck();
2808 if (got_int)
2809 {
2810 (void)vgetc(); /* only break the file loading, not the rest */
2811 break;
2812 }
2813 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002814
2815 if (parmp->window_layout == WIN_TABS)
2816 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002817# ifdef FEAT_AUTOCMD
2818 --autocmd_no_enter;
2819# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002820
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002821 /* make the first window the current window */
2822 win = firstwin;
2823#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2824 /* Avoid making a preview window the current window. */
2825 while (win->w_p_pvw)
2826 {
2827 win = win->w_next;
2828 if (win == NULL)
2829 {
2830 win = firstwin;
2831 break;
2832 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002833 }
2834#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002835 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002836
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002837# ifdef FEAT_AUTOCMD
2838 --autocmd_no_leave;
2839# endif
2840 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002841 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002842 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2843}
2844#endif /* FEAT_WINDOWS */
2845
2846/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002847 * Execute the commands from --cmd arguments "cmds[cnt]".
2848 */
2849 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002850exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002851{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002852 char_u **cmds = parmp->pre_commands;
2853 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002854 int i;
2855
2856 if (cnt > 0)
2857 {
2858 curwin->w_cursor.lnum = 0; /* just in case.. */
2859 sourcing_name = (char_u *)_("pre-vimrc command line");
2860# ifdef FEAT_EVAL
2861 current_SID = SID_CMDARG;
2862# endif
2863 for (i = 0; i < cnt; ++i)
2864 do_cmdline_cmd(cmds[i]);
2865 sourcing_name = NULL;
2866# ifdef FEAT_EVAL
2867 current_SID = 0;
2868# endif
2869 TIME_MSG("--cmd commands");
2870 }
2871}
2872
2873/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002874 * Execute "+", "-c" and "-S" arguments.
2875 */
2876 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002877exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002878{
2879 int i;
2880
2881 /*
2882 * We start commands on line 0, make "vim +/pat file" match a
2883 * pattern on line 1. But don't move the cursor when an autocommand
2884 * with g`" was used.
2885 */
2886 msg_scroll = TRUE;
2887 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2888 curwin->w_cursor.lnum = 0;
2889 sourcing_name = (char_u *)"command line";
2890#ifdef FEAT_EVAL
2891 current_SID = SID_CARG;
2892#endif
2893 for (i = 0; i < parmp->n_commands; ++i)
2894 {
2895 do_cmdline_cmd(parmp->commands[i]);
2896 if (parmp->cmds_tofree[i])
2897 vim_free(parmp->commands[i]);
2898 }
2899 sourcing_name = NULL;
2900#ifdef FEAT_EVAL
2901 current_SID = 0;
2902#endif
2903 if (curwin->w_cursor.lnum == 0)
2904 curwin->w_cursor.lnum = 1;
2905
2906 if (!exmode_active)
2907 msg_scroll = FALSE;
2908
2909#ifdef FEAT_QUICKFIX
2910 /* When started with "-q errorfile" jump to first error again. */
2911 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002912 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002913#endif
2914 TIME_MSG("executing command arguments");
2915}
2916
2917/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002918 * Source startup scripts.
2919 */
2920 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002921source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002922{
2923 int i;
2924
2925 /*
2926 * For "evim" source evim.vim first of all, so that the user can overrule
2927 * any things he doesn't like.
2928 */
2929 if (parmp->evim_mode)
2930 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002931 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002932 TIME_MSG("source evim file");
2933 }
2934
2935 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002936 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002937 * nothing else.
2938 */
2939 if (parmp->use_vimrc != NULL)
2940 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002941 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2942 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002943 {
2944#ifdef FEAT_GUI
2945 if (use_gvimrc == NULL) /* don't load gvimrc either */
2946 use_gvimrc = parmp->use_vimrc;
2947#endif
2948 if (parmp->use_vimrc[2] == 'N')
2949 p_lpl = FALSE; /* don't load plugins either */
2950 }
2951 else
2952 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002953 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002954 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2955 }
2956 }
2957 else if (!silent_mode)
2958 {
2959#ifdef AMIGA
2960 struct Process *proc = (struct Process *)FindTask(0L);
2961 APTR save_winptr = proc->pr_WindowPtr;
2962
2963 /* Avoid a requester here for a volume that doesn't exist. */
2964 proc->pr_WindowPtr = (APTR)-1L;
2965#endif
2966
2967 /*
2968 * Get system wide defaults, if the file name is defined.
2969 */
2970#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002971 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002972#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002973#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002974 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002975#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002976
2977 /*
2978 * Try to read initialization commands from the following places:
2979 * - environment variable VIMINIT
2980 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2981 * - second user vimrc file ($VIM/.vimrc for Dos)
2982 * - environment variable EXINIT
2983 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2984 * - second user exrc file ($VIM/.exrc for Dos)
2985 * The first that exists is used, the rest is ignored.
2986 */
2987 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2988 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002989 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002990#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002991 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2992 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002993#endif
2994#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002995 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2996 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002997#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02002998#ifdef USR_VIMRC_FILE4
2999 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3000 DOSO_VIMRC) == FAIL
3001#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003002 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003003 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003005 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003006#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003007 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003008 {
3009 /* When no .vimrc file was found: source defaults.vim. */
3010 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003011 }
3012 }
3013
3014 /*
3015 * Read initialization commands from ".vimrc" or ".exrc" in current
3016 * directory. This is only done if the 'exrc' option is set.
3017 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003018 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003019 * option has been reset in environment of global ".exrc" or ".vimrc".
3020 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3021 * SYS_VIMRC_FILE.
3022 */
3023 if (p_exrc)
3024 {
3025#if defined(UNIX) || defined(VMS)
3026 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3027 if (!file_owned(VIMRC_FILE))
3028#endif
3029 secure = p_secure;
3030
3031 i = FAIL;
3032 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3033 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3034#ifdef USR_VIMRC_FILE2
3035 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3036 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3037#endif
3038#ifdef USR_VIMRC_FILE3
3039 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3040 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3041#endif
3042#ifdef SYS_VIMRC_FILE
3043 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3044 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3045#endif
3046 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003047 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003048
3049 if (i == FAIL)
3050 {
3051#if defined(UNIX) || defined(VMS)
3052 /* if ".exrc" is not owned by user set 'secure' mode */
3053 if (!file_owned(EXRC_FILE))
3054 secure = p_secure;
3055 else
3056 secure = 0;
3057#endif
3058 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3059 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3060#ifdef USR_EXRC_FILE2
3061 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3062 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3063#endif
3064 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003065 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003066 }
3067 }
3068 if (secure == 2)
3069 need_wait_return = TRUE;
3070 secure = 0;
3071#ifdef AMIGA
3072 proc->pr_WindowPtr = save_winptr;
3073#endif
3074 }
3075 TIME_MSG("sourcing vimrc file(s)");
3076}
3077
3078/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003079 * Setup to start using the GUI. Exit with an error when not available.
3080 */
3081 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003082main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003083{
3084#ifdef FEAT_GUI
3085 gui.starting = TRUE; /* start GUI a bit later */
3086#else
3087 mch_errmsg(_(e_nogvim));
3088 mch_errmsg("\n");
3089 mch_exit(2);
3090#endif
3091}
3092
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003093#endif /* NO_VIM_MAIN */
3094
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003095/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003096 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003097 * Returns FAIL if the environment variable was not executed, OK otherwise.
3098 */
3099 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003100process_env(
3101 char_u *env,
3102 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003103{
3104 char_u *initstr;
3105 char_u *save_sourcing_name;
3106 linenr_T save_sourcing_lnum;
3107#ifdef FEAT_EVAL
3108 scid_T save_sid;
3109#endif
3110
3111 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3112 {
3113 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003114 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003115 save_sourcing_name = sourcing_name;
3116 save_sourcing_lnum = sourcing_lnum;
3117 sourcing_name = env;
3118 sourcing_lnum = 0;
3119#ifdef FEAT_EVAL
3120 save_sid = current_SID;
3121 current_SID = SID_ENV;
3122#endif
3123 do_cmdline_cmd(initstr);
3124 sourcing_name = save_sourcing_name;
3125 sourcing_lnum = save_sourcing_lnum;
3126#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003127 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003128#endif
3129 return OK;
3130 }
3131 return FAIL;
3132}
3133
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003134#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003135/*
3136 * Return TRUE if we are certain the user owns the file "fname".
3137 * Used for ".vimrc" and ".exrc".
3138 * Use both stat() and lstat() for extra security.
3139 */
3140 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003141file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003142{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003143 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003144# ifdef UNIX
3145 uid_t uid = getuid();
3146# else /* VMS */
3147 uid_t uid = ((getgid() << 16) | getuid());
3148# endif
3149
3150 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3151# ifdef HAVE_LSTAT
3152 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3153# endif
3154 );
3155}
3156#endif
3157
3158/*
3159 * Give an error message main_errors["n"] and exit.
3160 */
3161 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003162mainerr(
3163 int n, /* one of the ME_ defines */
3164 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003165{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003166#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003167 reset_signals(); /* kill us with CTRL-C here, if you like */
3168#endif
3169
3170 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003171 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003172 mch_errmsg(_(main_errors[n]));
3173 if (str != NULL)
3174 {
3175 mch_errmsg(": \"");
3176 mch_errmsg((char *)str);
3177 mch_errmsg("\"");
3178 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003179 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003180
3181 mch_exit(1);
3182}
3183
3184 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003185mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003186{
3187 mainerr(ME_ARG_MISSING, str);
3188}
3189
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003190#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003191/*
3192 * print a message with three spaces prepended and '\n' appended.
3193 */
3194 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003195main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003196{
3197 mch_msg(" ");
3198 mch_msg(s);
3199 mch_msg("\n");
3200}
3201
3202/*
3203 * Print messages for "vim -h" or "vim --help" and exit.
3204 */
3205 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003206usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003207{
3208 int i;
3209 static char *(use[]) =
3210 {
3211 N_("[file ..] edit specified file(s)"),
3212 N_("- read text from stdin"),
3213 N_("-t tag edit file where tag is defined"),
3214#ifdef FEAT_QUICKFIX
3215 N_("-q [errorfile] edit file with first error")
3216#endif
3217 };
3218
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003219#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003220 reset_signals(); /* kill us with CTRL-C here, if you like */
3221#endif
3222
3223 mch_msg(longVersion);
3224 mch_msg(_("\n\nusage:"));
3225 for (i = 0; ; ++i)
3226 {
3227 mch_msg(_(" vim [arguments] "));
3228 mch_msg(_(use[i]));
3229 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3230 break;
3231 mch_msg(_("\n or:"));
3232 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003233#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003234 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003235#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003236
3237 mch_msg(_("\n\nArguments:\n"));
3238 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003239#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003240 main_msg(_("--literal\t\tDon't expand wildcards"));
3241#endif
3242#ifdef FEAT_OLE
3243 main_msg(_("-register\t\tRegister this gvim for OLE"));
3244 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3245#endif
3246#ifdef FEAT_GUI
3247 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3248 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3249#endif
3250 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3251 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003252 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003253 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3254#ifdef FEAT_DIFF
3255 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3256#endif
3257 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3258 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3259 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3260 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3261 main_msg(_("-M\t\t\tModifications in text not allowed"));
3262 main_msg(_("-b\t\t\tBinary mode"));
3263#ifdef FEAT_LISP
3264 main_msg(_("-l\t\t\tLisp mode"));
3265#endif
3266 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3267 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003268 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3269#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003270 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003271#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003272 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3273 main_msg(_("-r\t\t\tList swap files and exit"));
3274 main_msg(_("-r (with file name)\tRecover crashed session"));
3275 main_msg(_("-L\t\t\tSame as -r"));
3276#ifdef AMIGA
3277 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3278 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3279#endif
3280#ifdef FEAT_ARABIC
3281 main_msg(_("-A\t\t\tstart in Arabic mode"));
3282#endif
3283#ifdef FEAT_RIGHTLEFT
3284 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3285#endif
3286#ifdef FEAT_FKMAP
3287 main_msg(_("-F\t\t\tStart in Farsi mode"));
3288#endif
3289 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003290 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003291 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3292#ifdef FEAT_GUI
3293 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3294#endif
3295 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003296#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003297 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3299 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003300#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 main_msg(_("+\t\t\tStart at end of file"));
3302 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003303 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003304 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3305 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3306 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3307 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3308 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3309#ifdef FEAT_CRYPT
3310 main_msg(_("-x\t\t\tEdit encrypted files"));
3311#endif
3312#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3313# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3314 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3315# endif
3316 main_msg(_("-X\t\t\tDo not connect to X server"));
3317#endif
3318#ifdef FEAT_CLIENTSERVER
3319 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3320 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3321 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3322 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003323# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003324 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003325# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003326 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3327 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3328 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3329 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3330#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003331#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003332 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003333#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003334#ifdef FEAT_VIMINFO
3335 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3336#endif
3337 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3338 main_msg(_("--version\t\tPrint version information and exit"));
3339
3340#ifdef FEAT_GUI_X11
3341# ifdef FEAT_GUI_MOTIF
3342 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3343# else
3344# ifdef FEAT_GUI_ATHENA
3345# ifdef FEAT_GUI_NEXTAW
3346 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3347# else
3348 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3349# endif
3350# endif
3351# endif
3352 main_msg(_("-display <display>\tRun vim on <display>"));
3353 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003354 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3355 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3356 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3357 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3358 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3359 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3360 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3361 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3362# ifdef FEAT_GUI_ATHENA
3363 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3364# endif
3365 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3366 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3367 main_msg(_("-xrm <resource>\tSet the specified resource"));
3368#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003369#ifdef FEAT_GUI_GTK
3370 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3371 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3372 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3373 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3374 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003377 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003378#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003379#ifdef FEAT_GUI_W32
3380 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003381 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003382#endif
3383
3384#ifdef FEAT_GUI_GNOME
3385 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3386 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003387 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003389 gui.dofork = FALSE;
3390 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391 else
3392#endif
3393 mch_exit(0);
3394}
3395
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003396#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397/*
3398 * Check the result of the ATTENTION dialog:
3399 * When "Quit" selected, exit Vim.
3400 * When "Recover" selected, recover the file.
3401 */
3402 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003403check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404{
3405 if (swap_exists_action == SEA_QUIT)
3406 getout(1);
3407 handle_swap_exists(NULL);
3408}
3409#endif
3410
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003411#endif
3412
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003414static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415
3416static struct timeval prev_timeval;
3417
Bram Moolenaar3f269672009-11-03 11:11:11 +00003418# ifdef WIN3264
3419/*
3420 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3421 */
3422 static int
3423gettimeofday(struct timeval *tv, char *dummy)
3424{
3425 long t = clock();
3426 tv->tv_sec = t / CLOCKS_PER_SEC;
3427 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3428 return 0;
3429}
3430# endif
3431
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003432/*
3433 * Save the previous time before doing something that could nest.
3434 * set "*tv_rel" to the time elapsed so far.
3435 */
3436 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003437time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003438{
3439 *((struct timeval *)tv_rel) = prev_timeval;
3440 gettimeofday(&prev_timeval, NULL);
3441 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3442 - ((struct timeval *)tv_rel)->tv_usec;
3443 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3444 - ((struct timeval *)tv_rel)->tv_sec;
3445 if (((struct timeval *)tv_rel)->tv_usec < 0)
3446 {
3447 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3448 --((struct timeval *)tv_rel)->tv_sec;
3449 }
3450 *(struct timeval *)tv_start = prev_timeval;
3451}
3452
3453/*
3454 * Compute the previous time after doing something that could nest.
3455 * Subtract "*tp" from prev_timeval;
3456 * Note: The arguments are (void *) to avoid trouble with systems that don't
3457 * have struct timeval.
3458 */
3459 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003460time_pop(
3461 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003462{
3463 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3464 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3465 if (prev_timeval.tv_usec < 0)
3466 {
3467 prev_timeval.tv_usec += 1000000;
3468 --prev_timeval.tv_sec;
3469 }
3470}
3471
3472 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003473time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003474{
3475 long usec;
3476 long msec;
3477
3478 usec = now->tv_usec - then->tv_usec;
3479 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3480 usec = usec % 1000L;
3481 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3482}
3483
3484 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003485time_msg(
3486 char *mesg,
3487 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003488 (struct timeval *) */
3489{
3490 static struct timeval start;
3491 struct timeval now;
3492
3493 if (time_fd != NULL)
3494 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003495 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003496 {
3497 gettimeofday(&start, NULL);
3498 prev_timeval = start;
3499 fprintf(time_fd, "\n\ntimes in msec\n");
3500 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3501 fprintf(time_fd, " clock elapsed: other lines\n\n");
3502 }
3503 gettimeofday(&now, NULL);
3504 time_diff(&start, &now);
3505 if (((struct timeval *)tv_start) != NULL)
3506 {
3507 fprintf(time_fd, " ");
3508 time_diff(((struct timeval *)tv_start), &now);
3509 }
3510 fprintf(time_fd, " ");
3511 time_diff(&prev_timeval, &now);
3512 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003513 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003514 }
3515}
3516
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003517#endif
3518
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003519#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520
3521/*
3522 * Common code for the X command server and the Win32 command server.
3523 */
3524
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003525static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003526
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003527/*
3528 * Do the client-server stuff, unless "--servername ''" was used.
3529 */
3530 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003531exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003532{
3533 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3534 {
3535# ifdef WIN32
3536 /* Initialise the client/server messaging infrastructure. */
3537 serverInitMessaging();
3538# endif
3539
3540 /*
3541 * When a command server argument was found, execute it. This may
3542 * exit Vim when it was successful. Otherwise it's executed further
3543 * on. Remember the encoding used here in "serverStrEnc".
3544 */
3545 if (parmp->serverArg)
3546 {
3547 cmdsrv_main(&parmp->argc, parmp->argv,
3548 parmp->serverName_arg, &parmp->serverStr);
3549# ifdef FEAT_MBYTE
3550 parmp->serverStrEnc = vim_strsave(p_enc);
3551# endif
3552 }
3553
3554 /* If we're still running, get the name to register ourselves.
3555 * On Win32 can register right now, for X11 need to setup the
3556 * clipboard first, it's further down. */
3557 parmp->servername = serverMakeName(parmp->serverName_arg,
3558 parmp->argv[0]);
3559# ifdef WIN32
3560 if (parmp->servername != NULL)
3561 {
3562 serverSetName(parmp->servername);
3563 vim_free(parmp->servername);
3564 }
3565# endif
3566 }
3567}
3568
3569/*
3570 * Prepare for running as a Vim server.
3571 */
3572 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003573prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003574{
3575# if defined(FEAT_X11)
3576 /*
3577 * Register for remote command execution with :serversend and --remote
3578 * unless there was a -X or a --servername '' on the command line.
3579 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003580 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003581 */
3582 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3583# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003584 (gui.in_use
3585# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003586 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003587# endif
3588 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003589# endif
3590 parmp->serverName_arg != NULL))
3591 {
3592 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3593 vim_free(parmp->servername);
3594 TIME_MSG("register server name");
3595 }
3596 else
3597 serverDelayedStartName = parmp->servername;
3598# endif
3599
3600 /*
3601 * Execute command ourselves if we're here because the send failed (or
3602 * else we would have exited above).
3603 */
3604 if (parmp->serverStr != NULL)
3605 {
3606 char_u *p;
3607
3608 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3609 parmp->serverStr, &p));
3610 vim_free(p);
3611 }
3612}
3613
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003614 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003615cmdsrv_main(
3616 int *argc,
3617 char **argv,
3618 char_u *serverName_arg,
3619 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003620{
3621 char_u *res;
3622 int i;
3623 char_u *sname;
3624 int ret;
3625 int didone = FALSE;
3626 int exiterr = 0;
3627 char **newArgV = argv + 1;
3628 int newArgC = 1,
3629 Argc = *argc;
3630 int argtype;
3631#define ARGTYPE_OTHER 0
3632#define ARGTYPE_EDIT 1
3633#define ARGTYPE_EDIT_WAIT 2
3634#define ARGTYPE_SEND 3
3635 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003636 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003637# ifndef FEAT_X11
3638 HWND srv;
3639# else
3640 Window srv;
3641
3642 setup_term_clip();
3643# endif
3644
3645 sname = serverMakeName(serverName_arg, argv[0]);
3646 if (sname == NULL)
3647 return;
3648
3649 /*
3650 * Execute the command server related arguments and remove them
3651 * from the argc/argv array; We may have to return into main()
3652 */
3653 for (i = 1; i < Argc; i++)
3654 {
3655 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003656 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003657 {
3658 for (; i < *argc; i++)
3659 {
3660 *newArgV++ = argv[i];
3661 newArgC++;
3662 }
3663 break;
3664 }
3665
Bram Moolenaareb94e552006-03-11 21:35:11 +00003666 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003667 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003668 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3669 {
3670 char *p = argv[i] + 8;
3671
3672 argtype = ARGTYPE_EDIT;
3673 while (*p != NUL)
3674 {
3675 if (STRNICMP(p, "-wait", 5) == 0)
3676 {
3677 argtype = ARGTYPE_EDIT_WAIT;
3678 p += 5;
3679 }
3680 else if (STRNICMP(p, "-silent", 7) == 0)
3681 {
3682 silent = TRUE;
3683 p += 7;
3684 }
3685 else if (STRNICMP(p, "-tab", 4) == 0)
3686 {
3687 tabs = TRUE;
3688 p += 4;
3689 }
3690 else
3691 {
3692 argtype = ARGTYPE_OTHER;
3693 break;
3694 }
3695 }
3696 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003697 else
3698 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003699
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003700 if (argtype != ARGTYPE_OTHER)
3701 {
3702 if (i == *argc - 1)
3703 mainerr_arg_missing((char_u *)argv[i]);
3704 if (argtype == ARGTYPE_SEND)
3705 {
3706 *serverStr = (char_u *)argv[i + 1];
3707 i++;
3708 }
3709 else
3710 {
3711 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003712 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003713 if (*serverStr == NULL)
3714 {
3715 /* Probably out of memory, exit. */
3716 didone = TRUE;
3717 exiterr = 1;
3718 break;
3719 }
3720 Argc = i;
3721 }
3722# ifdef FEAT_X11
3723 if (xterm_dpy == NULL)
3724 {
3725 mch_errmsg(_("No display"));
3726 ret = -1;
3727 }
3728 else
3729 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3730 NULL, &srv, 0, 0, silent);
3731# else
3732 /* Win32 always works? */
3733 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3734# endif
3735 if (ret < 0)
3736 {
3737 if (argtype == ARGTYPE_SEND)
3738 {
3739 /* Failed to send, abort. */
3740 mch_errmsg(_(": Send failed.\n"));
3741 didone = TRUE;
3742 exiterr = 1;
3743 }
3744 else if (!silent)
3745 /* Let vim start normally. */
3746 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3747 break;
3748 }
3749
3750# ifdef FEAT_GUI_W32
3751 /* Guess that when the server name starts with "g" it's a GUI
3752 * server, which we can bring to the foreground here.
3753 * Foreground() in the server doesn't work very well. */
3754 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3755 SetForegroundWindow(srv);
3756# endif
3757
3758 /*
3759 * For --remote-wait: Wait until the server did edit each
3760 * file. Also detect that the server no longer runs.
3761 */
3762 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3763 {
3764 int numFiles = *argc - i - 1;
3765 int j;
3766 char_u *done = alloc(numFiles);
3767 char_u *p;
3768# ifdef FEAT_GUI_W32
3769 NOTIFYICONDATA ni;
3770 int count = 0;
3771 extern HWND message_window;
3772# endif
3773
3774 if (numFiles > 0 && argv[i + 1][0] == '+')
3775 /* Skip "+cmd" argument, don't wait for it to be edited. */
3776 --numFiles;
3777
3778# ifdef FEAT_GUI_W32
3779 ni.cbSize = sizeof(ni);
3780 ni.hWnd = message_window;
3781 ni.uID = 0;
3782 ni.uFlags = NIF_ICON|NIF_TIP;
3783 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3784 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3785 Shell_NotifyIcon(NIM_ADD, &ni);
3786# endif
3787
3788 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003789 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003790 while (memchr(done, 0, numFiles) != NULL)
3791 {
3792# ifdef WIN32
3793 p = serverGetReply(srv, NULL, TRUE, TRUE);
3794 if (p == NULL)
3795 break;
3796# else
3797 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3798 break;
3799# endif
3800 j = atoi((char *)p);
3801 if (j >= 0 && j < numFiles)
3802 {
3803# ifdef FEAT_GUI_W32
3804 ++count;
3805 sprintf(ni.szTip, _("%d of %d edited"),
3806 count, numFiles);
3807 Shell_NotifyIcon(NIM_MODIFY, &ni);
3808# endif
3809 done[j] = 1;
3810 }
3811 }
3812# ifdef FEAT_GUI_W32
3813 Shell_NotifyIcon(NIM_DELETE, &ni);
3814# endif
3815 }
3816 }
3817 else if (STRICMP(argv[i], "--remote-expr") == 0)
3818 {
3819 if (i == *argc - 1)
3820 mainerr_arg_missing((char_u *)argv[i]);
3821# ifdef WIN32
3822 /* Win32 always works? */
3823 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3824 &res, NULL, 1, FALSE) < 0)
3825# else
3826 if (xterm_dpy == NULL)
3827 mch_errmsg(_("No display: Send expression failed.\n"));
3828 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3829 &res, NULL, 1, 1, FALSE) < 0)
3830# endif
3831 {
3832 if (res != NULL && *res != NUL)
3833 {
3834 /* Output error from remote */
3835 mch_errmsg((char *)res);
3836 vim_free(res);
3837 res = NULL;
3838 }
3839 mch_errmsg(_(": Send expression failed.\n"));
3840 }
3841 }
3842 else if (STRICMP(argv[i], "--serverlist") == 0)
3843 {
3844# ifdef WIN32
3845 /* Win32 always works? */
3846 res = serverGetVimNames();
3847# else
3848 if (xterm_dpy != NULL)
3849 res = serverGetVimNames(xterm_dpy);
3850# endif
3851 if (called_emsg)
3852 mch_errmsg("\n");
3853 }
3854 else if (STRICMP(argv[i], "--servername") == 0)
3855 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003856 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003857 i++;
3858 continue;
3859 }
3860 else
3861 {
3862 *newArgV++ = argv[i];
3863 newArgC++;
3864 continue;
3865 }
3866 didone = TRUE;
3867 if (res != NULL && *res != NUL)
3868 {
3869 mch_msg((char *)res);
3870 if (res[STRLEN(res) - 1] != '\n')
3871 mch_msg("\n");
3872 }
3873 vim_free(res);
3874 }
3875
3876 if (didone)
3877 {
3878 display_errors(); /* display any collected messages */
3879 exit(exiterr); /* Mission accomplished - get out */
3880 }
3881
3882 /* Return back into main() */
3883 *argc = newArgC;
3884 vim_free(sname);
3885}
3886
3887/*
3888 * Build a ":drop" command to send to a Vim server.
3889 */
3890 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003891build_drop_cmd(
3892 int filec,
3893 char **filev,
3894 int tabs, /* Use ":tab drop" instead of ":drop". */
3895 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003896{
3897 garray_T ga;
3898 int i;
3899 char_u *inicmd = NULL;
3900 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003901 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003902 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003903
3904 if (filec > 0 && filev[0][0] == '+')
3905 {
3906 inicmd = (char_u *)filev[0] + 1;
3907 filev++;
3908 filec--;
3909 }
3910 /* Check if we have at least one argument. */
3911 if (filec <= 0)
3912 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003913
3914 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003915 cwd = alloc(MAXPATHL);
3916 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003917 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003918 if (mch_dirname(cwd, MAXPATHL) != OK)
3919 {
3920 vim_free(cwd);
3921 return NULL;
3922 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003923 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003924#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003925 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003926#else
3927 PATH_ESC_CHARS,
3928#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003929 '\\', TRUE);
3930 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003931 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003932 return NULL;
3933 ga_init2(&ga, 1, 100);
3934 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003935 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003936
3937 /* Call inputsave() so that a prompt for an encryption key works. */
3938 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3939 if (tabs)
3940 ga_concat(&ga, (char_u *)"tab ");
3941 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003942 for (i = 0; i < filec; i++)
3943 {
3944 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003945 * do it again in the Vim server. On MS-Windows only escape
3946 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003947 p = vim_strsave_escaped((char_u *)filev[i],
3948#ifdef UNIX
3949 PATH_ESC_CHARS
3950#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003951 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003952#endif
3953 );
3954 if (p == NULL)
3955 {
3956 vim_free(ga.ga_data);
3957 return NULL;
3958 }
3959 ga_concat(&ga, (char_u *)" ");
3960 ga_concat(&ga, p);
3961 vim_free(p);
3962 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003963 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3964
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003965 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3966 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003967 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3968
3969 /* Switch back to the correct current directory (prior to temporary path
3970 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003971 * correct after the :drop command. With line breaks and spaces:
3972 * if !exists('+acd') || !&acd
3973 * if haslocaldir()
3974 * cd -
3975 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003976 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003977 * cd -
3978 * endif
3979 * endif
3980 */
3981 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003982 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003983 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003984 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003985 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003986
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003987 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003988 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3989 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003990 if (inicmd != NULL)
3991 {
3992 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3993 * the following commands to be inserted as text. Use a "|",
3994 * hopefully "inicmd" does allow this... */
3995 ga_concat(&ga, inicmd);
3996 ga_concat(&ga, (char_u *)"|");
3997 }
3998 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3999 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004000 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004001 ga_append(&ga, NUL);
4002 return ga.ga_data;
4003}
4004
4005/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004006 * Make our basic server name: use the specified "arg" if given, otherwise use
4007 * the tail of the command "cmd" we were started with.
4008 * Return the name in allocated memory. This doesn't include a serial number.
4009 */
4010 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004011serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004012{
4013 char_u *p;
4014
4015 if (arg != NULL && *arg != NUL)
4016 p = vim_strsave_up(arg);
4017 else
4018 {
4019 p = vim_strsave_up(gettail((char_u *)cmd));
4020 /* Remove .exe or .bat from the name. */
4021 if (p != NULL && vim_strchr(p, '.') != NULL)
4022 *vim_strchr(p, '.') = NUL;
4023 }
4024 return p;
4025}
4026#endif /* FEAT_CLIENTSERVER */
4027
4028#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4029/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004030 * Replace termcodes such as <CR> and insert as key presses if there is room.
4031 */
4032 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004033server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004034{
4035 char_u *ptr = NULL;
4036 char_u *cpo_save = p_cpo;
4037
4038 /* Set 'cpoptions' the way we want it.
4039 * B set - backslashes are *not* treated specially
4040 * k set - keycodes are *not* reverse-engineered
4041 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004042 * The last but one parameter of replace_termcodes() is TRUE so that the
4043 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004044 */
4045 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004046 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004047 p_cpo = cpo_save;
4048
4049 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4050 {
4051 /*
4052 * Add the string to the input stream.
4053 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4054 *
4055 * First clear typed characters from the typeahead buffer, there could
4056 * be half a mapping there. Then append to the existing string, so
4057 * that multiple commands from a client are concatenated.
4058 */
4059 if (typebuf.tb_maplen < typebuf.tb_len)
4060 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4061 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4062
4063 /* Let input_available() know we inserted text in the typeahead
4064 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004065 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004066 }
4067 vim_free((char_u *)ptr);
4068}
4069
4070/*
4071 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004072 */
4073 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004074eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004075{
4076 char_u *res;
4077 int save_dbl = debug_break_level;
4078 int save_ro = redir_off;
4079
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004080 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4081 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004082 debug_break_level = -1;
4083 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004084 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4085 * to be typed. Do generate errors so that try/catch works. */
4086 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004087
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004088 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004089
4090 debug_break_level = save_dbl;
4091 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004092 --emsg_silent;
4093 if (emsg_silent < 0)
4094 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004095
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004096 /* A client can tell us to redraw, but not to display the cursor, so do
4097 * that here. */
4098 setcursor();
4099 out_flush();
4100#ifdef FEAT_GUI
4101 if (gui.in_use)
4102 gui_update_cursor(FALSE, FALSE);
4103#endif
4104
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004105 return res;
4106}
4107
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004108/*
4109 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4110 * return an allocated string. Otherwise return "data".
4111 * "*tofree" is set to the result when it needs to be freed later.
4112 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004113 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004114serverConvert(
4115 char_u *client_enc UNUSED,
4116 char_u *data,
4117 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004118{
4119 char_u *res = data;
4120
4121 *tofree = NULL;
4122# ifdef FEAT_MBYTE
4123 if (client_enc != NULL && p_enc != NULL)
4124 {
4125 vimconv_T vimconv;
4126
4127 vimconv.vc_type = CONV_NONE;
4128 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4129 && vimconv.vc_type != CONV_NONE)
4130 {
4131 res = string_convert(&vimconv, data, NULL);
4132 if (res == NULL)
4133 res = data;
4134 else
4135 *tofree = res;
4136 }
4137 convert_setup(&vimconv, NULL, NULL);
4138 }
4139# endif
4140 return res;
4141}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004142#endif