blob: 48e90c27ffe05590143a4599d81ce7aedaef789a [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 Moolenaar66459b72016-08-06 19:01:55 +0200442 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
443 * Allows for setting 'loadplugins' there. */
444 if (params.use_vimrc != NULL && STRCMP(params.use_vimrc, "NONE") == 0)
445 p_lpl = FALSE;
446
Bram Moolenaar58d98232005-07-23 22:25:46 +0000447 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000448 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000449
Bram Moolenaar58d98232005-07-23 22:25:46 +0000450 /* Source startup scripts. */
451 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000452
453#ifdef FEAT_EVAL
454 /*
455 * Read all the plugin files.
456 * Only when compiled with +eval, since most plugins need it.
457 */
458 if (p_lpl)
459 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000460# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar66459b72016-08-06 19:01:55 +0200461 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000462# else
Bram Moolenaar66459b72016-08-06 19:01:55 +0200463 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_NOAFTER);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000464# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000465 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100466
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100467 ex_packloadall(NULL);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100468 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200469
470# ifdef VMS /* Somehow VMS doesn't handle the "**". */
471 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
472# else
473 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
474# endif
475 TIME_MSG("loading after plugins");
476
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000477 }
478#endif
479
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000480#ifdef FEAT_DIFF
481 /* Decide about window layout for diff mode after reading vimrc. */
482 if (params.diff_mode && params.window_layout == 0)
483 {
484 if (diffopt_horizontal())
485 params.window_layout = WIN_HOR; /* use horizontal split */
486 else
487 params.window_layout = WIN_VER; /* use vertical split */
488 }
489#endif
490
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000491 /*
492 * Recovery mode without a file name: List swap files.
493 * This uses the 'dir' option, therefore it must be after the
494 * initializations.
495 */
496 if (recoverymode && fname == NULL)
497 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200498 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000499 mch_exit(0);
500 }
501
502 /*
503 * Set a few option defaults after reading .vimrc files:
504 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
505 */
506 set_init_3();
507 TIME_MSG("inits 3");
508
509 /*
510 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
511 * Note that this overrides anything from a vimrc file.
512 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000513 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000514 p_uc = 0;
515
516#ifdef FEAT_FKMAP
517 if (curwin->w_p_rl && p_altkeymap)
518 {
519 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
520# ifdef FEAT_ARABIC
521 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
522# endif
523 p_fkmap = TRUE; /* Set the Farsi keymap mode */
524 }
525#endif
526
527#ifdef FEAT_GUI
528 if (gui.starting)
529 {
530#if defined(UNIX) || defined(VMS)
531 /* When something caused a message from a vimrc script, need to output
532 * an extra newline before the shell prompt. */
533 if (did_emsg || msg_didout)
534 putchar('\n');
535#endif
536
537 gui_start(); /* will set full_screen to TRUE */
538 TIME_MSG("starting GUI");
539
540 /* When running "evim" or "gvim -y" we need the menus, exit if we
541 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000542 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000543 mch_exit(1);
544 }
545#endif
546
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547#ifdef FEAT_VIMINFO
548 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000549 * Read in registers, history etc, but not marks, from the viminfo file.
550 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551 */
552 if (*p_viminfo != NUL)
553 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000554 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555 TIME_MSG("reading viminfo");
556 }
557#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100558#ifdef FEAT_EVAL
559 /* It's better to make v:oldfiles an empty list than NULL. */
560 if (get_vim_var_list(VV_OLDFILES) == NULL)
561 set_vim_var_list(VV_OLDFILES, list_alloc());
562#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000563
564#ifdef FEAT_QUICKFIX
565 /*
566 * "-q errorfile": Load the error file now.
567 * If the error file can't be read, exit before doing anything else.
568 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000569 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000570 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000571 if (params.use_ef != NULL)
572 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000573 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200574 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
575 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000576 {
577 out_char('\n');
578 mch_exit(3);
579 }
580 TIME_MSG("reading errorfile");
581 }
582#endif
583
584 /*
585 * Start putting things on the screen.
586 * Scroll screen down before drawing over it
587 * Clear screen now, so file message will not be cleared.
588 */
589 starting = NO_BUFFERS;
590 no_wait_return = FALSE;
591 if (!exmode_active)
592 msg_scroll = FALSE;
593
594#ifdef FEAT_GUI
595 /*
596 * This seems to be required to make callbacks to be called now, instead
597 * of after things have been put on the screen, which then may be deleted
598 * when getting a resize callback.
599 * For the Mac this handles putting files dropped on the Vim icon to
600 * global_alist.
601 */
602 if (gui.in_use)
603 {
604# ifdef FEAT_SUN_WORKSHOP
605 if (!usingSunWorkShop)
606# endif
607 gui_wait_for_chars(50L);
608 TIME_MSG("GUI delay");
609 }
610#endif
611
612#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
613 qnx_clip_init();
614#endif
615
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200616#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
617 clip_init(TRUE);
618#endif
619
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000620#ifdef FEAT_XCLIPBOARD
621 /* Start using the X clipboard, unless the GUI was started. */
622# ifdef FEAT_GUI
623 if (!gui.in_use)
624# endif
625 {
626 setup_term_clip();
627 TIME_MSG("setup clipboard");
628 }
629#endif
630
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000631#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000632 /* Prepare for being a Vim server. */
633 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000634#endif
635
636 /*
637 * If "-" argument given: Read file from stdin.
638 * Do this before starting Raw mode, because it may change things that the
639 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
640 * are the same terminal: "cat | vim -".
641 * Using autocommands here may cause trouble...
642 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000643 if (params.edit_type == EDIT_STDIN && !recoverymode)
644 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000645
646#if defined(UNIX) || defined(VMS)
647 /* When switching screens and something caused a message from a vimrc
648 * script, need to output an extra newline on exit. */
649 if ((did_emsg || msg_didout) && *T_TI != NUL)
650 newline_on_exit = TRUE;
651#endif
652
653 /*
654 * When done something that is not allowed or error message call
655 * wait_return. This must be done before starttermcap(), because it may
656 * switch to another screen. It must be done after settmode(TMODE_RAW),
657 * because we want to react on a single key stroke.
658 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000659 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000660 */
661 settmode(TMODE_RAW);
662 TIME_MSG("setting raw mode");
663
664 if (need_wait_return || msg_didany)
665 {
666 wait_return(TRUE);
667 TIME_MSG("waiting for return");
668 }
669
670 starttermcap(); /* start termcap if not done by wait_return() */
671 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200672#if defined(FEAT_TERMRESPONSE)
673# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200674 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200675# endif
676 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100677#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000678
679#ifdef FEAT_MOUSE
680 setmouse(); /* may start using the mouse */
681#endif
682 if (scroll_region)
683 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000684 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000685
686 /*
687 * Don't clear the screen when starting in Ex mode, unless using the GUI.
688 */
689 if (exmode_active
690#ifdef FEAT_GUI
691 && !gui.in_use
692#endif
693 )
694 must_redraw = CLEAR;
695 else
696 {
697 screenclear(); /* clear screen */
698 TIME_MSG("clearing screen");
699 }
700
701#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000702 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000703 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100704 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200705 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000706 TIME_MSG("getting crypt key");
707 }
708#endif
709
710 no_wait_return = TRUE;
711
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000713 * Create the requested number of windows and edit buffers in them.
714 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000716 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000717 TIME_MSG("opening buffers");
718
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000719#ifdef FEAT_EVAL
720 /* clear v:swapcommand */
721 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
722#endif
723
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 /* Ex starts at last line of the file */
725 if (exmode_active)
726 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
727
728#ifdef FEAT_AUTOCMD
729 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
730 TIME_MSG("BufEnter autocommands");
731#endif
732 setpcmark();
733
734#ifdef FEAT_QUICKFIX
735 /*
736 * When started with "-q errorfile" jump to first error now.
737 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000738 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000739 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000740 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000741 TIME_MSG("jump to first error");
742 }
743#endif
744
745#ifdef FEAT_WINDOWS
746 /*
747 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000748 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000749 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200750 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000751#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200752 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000753
754#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000755 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000756 {
757 win_T *wp;
758
759 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200760 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000761 diff_win_options(wp, TRUE);
762 }
763#endif
764
765 /*
766 * Shorten any of the filenames, but only when absolute.
767 */
768 shorten_fnames(FALSE);
769
770 /*
771 * Need to jump to the tag before executing the '-c command'.
772 * Makes "vim -c '/return' -t main" work.
773 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000774 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000775 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000776#if defined(HAS_SWAP_EXISTS_ACTION)
777 swap_exists_did_quit = FALSE;
778#endif
779
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000780 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000781 do_cmdline_cmd(IObuff);
782 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000783
784#if defined(HAS_SWAP_EXISTS_ACTION)
785 /* If the user doesn't want to edit the file then we quit here. */
786 if (swap_exists_did_quit)
787 getout(1);
788#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000789 }
790
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000791 /* Execute any "+", "-c" and "-S" arguments. */
792 if (params.n_commands > 0)
793 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794
795 RedrawingDisabled = 0;
796 redraw_all_later(NOT_VALID);
797 no_wait_return = FALSE;
798 starting = 0;
799
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200800 /* 'autochdir' has been postponed */
801 DO_AUTOCHDIR
802
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000803#ifdef FEAT_TERMRESPONSE
804 /* Requesting the termresponse is postponed until here, so that a "-c q"
805 * argument doesn't make it appear in the shell Vim was started from. */
806 may_req_termresponse();
807#endif
808
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000809 /* start in insert mode */
810 if (p_im)
811 need_start_insertmode = TRUE;
812
Bram Moolenaar14735512016-03-26 21:00:08 +0100813#ifdef FEAT_EVAL
814 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
815#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000816#ifdef FEAT_AUTOCMD
817 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
818 TIME_MSG("VimEnter autocommands");
819#endif
820
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200821#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
822 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
823 * done after the clipboard is available and all initial commands that may
824 * modify the 'clipboard' setting have run; i.e. just before entering the
825 * main loop. */
826 {
827 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100828
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200829 adjust_clip_reg(&default_regname);
830 set_reg_var(default_regname);
831 }
832#endif
833
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000834#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
835 /* When a startup script or session file setup for diff'ing and
836 * scrollbind, sync the scrollbind now. */
837 if (curwin->w_p_diff && curwin->w_p_scb)
838 {
839 update_topline();
840 check_scrollbind((linenr_T)0, 0L);
841 TIME_MSG("diff scrollbinding");
842 }
843#endif
844
845#if defined(WIN3264) && !defined(FEAT_GUI_W32)
846 mch_set_winsize_now(); /* Allow winsize changes from now on */
847#endif
848
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000849#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
850 /* When tab pages were created, may need to update the tab pages line and
851 * scrollbars. This is skipped while creating them. */
852 if (first_tabpage->tp_next != NULL)
853 {
854 out_flush();
855 gui_init_which_components(NULL);
856 gui_update_scrollbars(TRUE);
857 }
858 need_mouse_correct = TRUE;
859#endif
860
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000861 /* If ":startinsert" command used, stuff a dummy command to be able to
862 * call normal_cmd(), which will then start Insert mode. */
863 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000864 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000865
866#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200867 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200868 {
869# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200870# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200871 && !defined(FEAT_GUI_W32)
872 if (gui.in_use)
873 {
874 mch_errmsg(_("netbeans is not supported with this GUI\n"));
875 mch_exit(2);
876 }
877# endif
878# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000879 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200880 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200881 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882#endif
883
884 TIME_MSG("before starting main loop");
885
886 /*
887 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100888 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000889 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000890
891 return 0;
892}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100893#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100894#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000895
896/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200897 * Initialisation shared by main() and some tests.
898 */
899 void
900common_init(mparm_T *params)
901{
902
903#ifdef FEAT_MBYTE
904 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
905#endif
906#ifdef FEAT_EVAL
907 eval_init(); /* init global variables */
908#endif
909
910#ifdef __QNXNTO__
911 qnx_init(); /* PhAttach() for clipboard, (and gui) */
912#endif
913
914#ifdef MAC_OS_CLASSIC
915 /* Prepare for possibly starting GUI sometime */
916 /* Macintosh needs this before any memory is allocated. */
917 gui_prepare(&params->argc, params->argv);
918 TIME_MSG("GUI prepared");
919#endif
920
921 /* Init the table of Normal mode commands. */
922 init_normal_cmds();
923
924#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
925 make_version(); /* Construct the long version string. */
926#endif
927
928 /*
929 * Allocate space for the generic buffers (needed for set_init_1() and
930 * EMSG2()).
931 */
932 if ((IObuff = alloc(IOSIZE)) == NULL
933 || (NameBuff = alloc(MAXPATHL)) == NULL)
934 mch_exit(0);
935 TIME_MSG("Allocated generic buffers");
936
937#ifdef NBDEBUG
938 /* Wait a moment for debugging NetBeans. Must be after allocating
939 * NameBuff. */
940 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
941 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
942 TIME_MSG("NetBeans debug wait");
943#endif
944
945#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
946 /*
947 * Setup to use the current locale (for ctype() and many other things).
948 * NOTE: Translated messages with encodings other than latin1 will not
949 * work until set_init_1() has been called!
950 */
951 init_locale();
952 TIME_MSG("locale set");
953#endif
954
955#ifdef FEAT_GUI
956 gui.dofork = TRUE; /* default is to use fork() */
957#endif
958
959 /*
960 * Do a first scan of the arguments in "argv[]":
961 * -display or --display
962 * --server...
963 * --socketid
964 * --windowid
965 */
966 early_arg_scan(params);
967
968#ifdef FEAT_SUN_WORKSHOP
969 findYourself(params->argv[0]);
970#endif
971#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
972 /* Prepare for possibly starting GUI sometime */
973 gui_prepare(&params->argc, params->argv);
974 TIME_MSG("GUI prepared");
975#endif
976
977#ifdef FEAT_CLIPBOARD
978 clip_init(FALSE); /* Initialise clipboard stuff */
979 TIME_MSG("clipboard setup");
980#endif
981
982 /*
983 * Check if we have an interactive window.
984 * On the Amiga: If there is no window, we open one with a newcli command
985 * (needed for :! to * work). mch_check_win() will also handle the -d or
986 * -dev argument.
987 */
988 params->stdout_isatty = (mch_check_win(params->argc, params->argv) != FAIL);
989 TIME_MSG("window checked");
990
991 /*
992 * Allocate the first window and buffer.
993 * Can't do anything without it, exit when it fails.
994 */
995 if (win_alloc_first() == FAIL)
996 mch_exit(0);
997
998 init_yank(); /* init yank buffers */
999
1000 alist_init(&global_alist); /* Init the argument list to empty. */
1001 global_alist.id = 0;
1002
1003 /*
1004 * Set the default values for the options.
1005 * NOTE: Non-latin1 translated messages are working only after this,
1006 * because this is where "has_mbyte" will be set, which is used by
1007 * msg_outtrans_len_attr().
1008 * First find out the home directory, needed to expand "~" in options.
1009 */
1010 init_homedir(); /* find real value of $HOME */
1011 set_init_1();
1012 TIME_MSG("inits 1");
1013
1014#ifdef FEAT_EVAL
1015 set_lang_var(); /* set v:lang and v:ctype */
1016#endif
1017}
1018
1019/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001020 * Main loop: Execute Normal mode commands until exiting Vim.
1021 * Also used to handle commands in the command-line window, until the window
1022 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001023 * Also used to handle ":visual" command after ":global": execute Normal mode
1024 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001025 */
1026 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001027main_loop(
1028 int cmdwin, /* TRUE when working in the command-line window */
1029 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001030{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001031 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001032 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001033#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001034 /* these are static to avoid a compiler warning */
1035 static linenr_T conceal_old_cursor_line = 0;
1036 static linenr_T conceal_new_cursor_line = 0;
1037 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001038#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001039
1040#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1041 /* Setup to catch a terminating error from the X server. Just ignore
1042 * it, restore the state and continue. This might not always work
1043 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001044 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046 {
1047 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001048 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001049 got_int = TRUE;
1050 need_wait_return = FALSE;
1051 global_busy = FALSE;
1052 exmode_active = 0;
1053 skip_redraw = FALSE;
1054 RedrawingDisabled = 0;
1055 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001056 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001057# ifdef FEAT_EVAL
1058 emsg_skip = 0;
1059# endif
1060 emsg_off = 0;
1061# ifdef FEAT_MOUSE
1062 setmouse();
1063# endif
1064 settmode(TMODE_RAW);
1065 starttermcap();
1066 scroll_start();
1067 redraw_later_clear();
1068 }
1069#endif
1070
1071 clear_oparg(&oa);
1072 while (!cmdwin
1073#ifdef FEAT_CMDWIN
1074 || cmdwin_result == 0
1075#endif
1076 )
1077 {
1078 if (stuff_empty())
1079 {
1080 did_check_timestamps = FALSE;
1081 if (need_check_timestamps)
1082 check_timestamps(FALSE);
1083 if (need_wait_return) /* if wait_return still needed ... */
1084 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001085 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001086 {
1087 need_start_insertmode = FALSE;
1088 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1089 /* skip the fileinfo message now, because it would be shown
1090 * after insert mode finishes! */
1091 need_fileinfo = FALSE;
1092 }
1093 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001094
1095 /* Reset "got_int" now that we got back to the main loop. Except when
1096 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1097 * the ":g" command.
1098 * For ":g/pat/vi" we reset "got_int" when used once. When used
1099 * a second time we go back to Ex mode and abort the ":g" command. */
1100 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001101 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001102 if (noexmode && global_busy && !exmode_active && previous_got_int)
1103 {
1104 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1105 * used and keep "got_int" set, so that it aborts ":g". */
1106 exmode_active = EXMODE_NORMAL;
1107 State = NORMAL;
1108 }
1109 else if (!global_busy || !exmode_active)
1110 {
1111 if (!quit_more)
1112 (void)vgetc(); /* flush all buffers */
1113 got_int = FALSE;
1114 }
1115 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001116 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001117 else
1118 previous_got_int = FALSE;
1119
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001120 if (!exmode_active)
1121 msg_scroll = FALSE;
1122 quit_more = FALSE;
1123
1124 /*
1125 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1126 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1127 * update cursor and redraw.
1128 */
1129 if (skip_redraw || exmode_active)
1130 skip_redraw = FALSE;
1131 else if (do_redraw || stuff_empty())
1132 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001133#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001134 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001135 if (!finish_op && (
1136# ifdef FEAT_AUTOCMD
1137 has_cursormoved()
1138# endif
1139# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1140 ||
1141# endif
1142# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001143 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001144# endif
1145 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001146# ifdef FEAT_AUTOCMD
1147 && !equalpos(last_cursormoved, curwin->w_cursor)
1148# endif
1149 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001150 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001151# ifdef FEAT_AUTOCMD
1152 if (has_cursormoved())
1153 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1154 FALSE, curbuf);
1155# endif
1156# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001157 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001158 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001159# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001160 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001161# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001162 conceal_new_cursor_line = curwin->w_cursor.lnum;
1163 conceal_update_lines = TRUE;
1164 }
1165# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001166# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001167 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001168# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001169 }
1170#endif
1171
Bram Moolenaar186628f2013-03-19 13:33:23 +01001172#ifdef FEAT_AUTOCMD
1173 /* Trigger TextChanged if b_changedtick differs. */
1174 if (!finish_op && has_textchanged()
1175 && last_changedtick != curbuf->b_changedtick)
1176 {
1177 if (last_changedtick_buf == curbuf)
1178 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1179 FALSE, curbuf);
1180 last_changedtick_buf = curbuf;
1181 last_changedtick = curbuf->b_changedtick;
1182 }
1183#endif
1184
Bram Moolenaar33aec762006-01-22 23:30:12 +00001185#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1186 /* Scroll-binding for diff mode may have been postponed until
1187 * here. Avoids doing it for every change. */
1188 if (diff_need_scrollbind)
1189 {
1190 check_scrollbind((linenr_T)0, 0L);
1191 diff_need_scrollbind = FALSE;
1192 }
1193#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001194#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001195 /* Include a closed fold completely in the Visual area. */
1196 foldAdjustVisual();
1197#endif
1198#ifdef FEAT_FOLDING
1199 /*
1200 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1201 * contain the cursor.
1202 * When 'foldopen' is "all", open the fold(s) under the cursor.
1203 * This may mark the window for redrawing.
1204 */
1205 if (hasAnyFolding(curwin) && !char_avail())
1206 {
1207 foldCheckClose();
1208 if (fdo_flags & FDO_ALL)
1209 foldOpenCursor();
1210 }
1211#endif
1212
1213 /*
1214 * Before redrawing, make sure w_topline is correct, and w_leftcol
1215 * if lines don't wrap, and w_skipcol if lines wrap.
1216 */
1217 update_topline();
1218 validate_cursor();
1219
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001220 if (VIsual_active)
1221 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001222 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001223 update_screen(0);
1224 else if (redraw_cmdline || clear_cmdline)
1225 showmode();
1226#ifdef FEAT_WINDOWS
1227 redraw_statuslines();
1228#endif
1229#ifdef FEAT_TITLE
1230 if (need_maketitle)
1231 maketitle();
1232#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001233#ifdef FEAT_VIMINFO
1234 curbuf->b_last_used = vim_time();
1235#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001236 /* display message after redraw */
1237 if (keep_msg != NULL)
1238 {
1239 char_u *p;
1240
1241 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001242 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1243 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001244 p = keep_msg;
1245 msg_attr(p, keep_msg_attr);
1246 vim_free(p);
1247 }
1248 if (need_fileinfo) /* show file info after redraw */
1249 {
1250 fileinfo(FALSE, TRUE, FALSE);
1251 need_fileinfo = FALSE;
1252 }
1253
1254 emsg_on_display = FALSE; /* can delete error message now */
1255 did_emsg = FALSE;
1256 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001257 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001258 showruler(FALSE);
1259
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001260# if defined(FEAT_CONCEAL)
1261 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001262 && (conceal_old_cursor_line != conceal_new_cursor_line
1263 || conceal_cursor_line(curwin)
1264 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001265 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001266 if (conceal_old_cursor_line != conceal_new_cursor_line
1267 && conceal_old_cursor_line
1268 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001269 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001270 update_single_line(curwin, conceal_new_cursor_line);
1271 curwin->w_valid &= ~VALID_CROW;
1272 }
1273# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001274 setcursor();
1275 cursor_on();
1276
1277 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001278
1279#ifdef STARTUPTIME
1280 /* Now that we have drawn the first screen all the startup stuff
1281 * has been done, close any file for startup messages. */
1282 if (time_fd != NULL)
1283 {
1284 TIME_MSG("first screen update");
1285 TIME_MSG("--- VIM STARTED ---");
1286 fclose(time_fd);
1287 time_fd = NULL;
1288 }
1289#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001290 }
1291#ifdef FEAT_GUI
1292 if (need_mouse_correct)
1293 gui_mouse_correct();
1294#endif
1295
1296 /*
1297 * Update w_curswant if w_set_curswant has been set.
1298 * Postponed until here to avoid computing w_virtcol too often.
1299 */
1300 update_curswant();
1301
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001302#ifdef FEAT_EVAL
1303 /*
1304 * May perform garbage collection when waiting for a character, but
1305 * only at the very toplevel. Otherwise we may be using a List or
1306 * Dict internally somewhere.
1307 * "may_garbage_collect" is reset in vgetc() which is invoked through
1308 * do_exmode() and normal_cmd().
1309 */
1310 may_garbage_collect = (!cmdwin && !noexmode);
1311#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001312 /*
1313 * If we're invoked as ex, do a round of ex commands.
1314 * Otherwise, get and execute a normal mode command.
1315 */
1316 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001317 {
1318 if (noexmode) /* End of ":global/path/visual" commands */
1319 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001320 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001321 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001322 else
1323 normal_cmd(&oa, TRUE);
1324 }
1325}
1326
1327
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001328#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001329/*
1330 * Exit, but leave behind swap files for modified buffers.
1331 */
1332 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001333getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001334{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001335# if defined(SIGHUP) && defined(SIG_IGN)
1336 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1337 * makes Vim exit and then handling SIGHUP causes various reentrance
1338 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001339 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001340# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001341
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 ml_close_notmod(); /* close all not-modified buffers */
1343 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1344 ml_close_all(FALSE); /* close all memfiles, without deleting */
1345 getout(exitval); /* exit Vim properly */
1346}
1347#endif
1348
1349
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001350/*
1351 * Exit properly.
1352 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001353 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001354getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001355{
1356#ifdef FEAT_AUTOCMD
1357 buf_T *buf;
1358 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001359 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001360#endif
1361
1362 exiting = TRUE;
1363
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001364 /* When running in Ex mode an error causes us to exit with a non-zero exit
1365 * code. POSIX requires this, although it's not 100% clear from the
1366 * standard. */
1367 if (exmode_active)
1368 exitval += ex_exitval;
1369
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370 /* Position the cursor on the last screen line, below all the text */
1371#ifdef FEAT_GUI
1372 if (!gui.in_use)
1373#endif
1374 windgoto((int)Rows - 1, 0);
1375
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001376#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1377 /* Optionally print hashtable efficiency. */
1378 hash_debug_results();
1379#endif
1380
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001381#ifdef FEAT_GUI
1382 msg_didany = FALSE;
1383#endif
1384
1385#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001386 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001387 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001388 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1389# if defined FEAT_WINDOWS
1390 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001391 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001392 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001393 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001394 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001395 if (wp->w_buffer == NULL)
1396 /* Autocmd must have close the buffer already, skip. */
1397 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001398 buf = wp->w_buffer;
1399 if (buf->b_changedtick != -1)
1400 {
1401 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1402 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001403 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001404 /* start all over, autocommands may mess up the lists */
1405 next_tp = first_tabpage;
1406 break;
1407 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001408 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001410# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001411 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1412 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001413# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001414
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001415 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001416 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001417 if (buf->b_ml.ml_mfp != NULL)
1418 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001419 bufref_T bufref;
1420
1421 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001423 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001424 if (!bufref_valid(&bufref))
1425 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 break;
1427 }
1428 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1429 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001430#endif
1431
1432#ifdef FEAT_VIMINFO
1433 if (*p_viminfo != NUL)
1434 /* Write out the registers, history, marks etc, to the viminfo file */
1435 write_viminfo(NULL, FALSE);
1436#endif
1437
1438#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 if (get_vim_var_nr(VV_DYING) <= 1)
1440 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001441#endif
1442
Bram Moolenaar05159a02005-02-26 23:04:13 +00001443#ifdef FEAT_PROFILE
1444 profile_dump();
1445#endif
1446
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001447 if (did_emsg
1448#ifdef FEAT_GUI
1449 || (gui.in_use && msg_didany && p_verbose > 0)
1450#endif
1451 )
1452 {
1453 /* give the user a chance to read the (error) message */
1454 no_wait_return = FALSE;
1455 wait_return(FALSE);
1456 }
1457
1458#ifdef FEAT_AUTOCMD
1459 /* Position the cursor again, the autocommands may have moved it */
1460# ifdef FEAT_GUI
1461 if (!gui.in_use)
1462# endif
1463 windgoto((int)Rows - 1, 0);
1464#endif
1465
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001466#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001467 job_stop_on_exit();
1468#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001469#ifdef FEAT_LUA
1470 lua_end();
1471#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001472#ifdef FEAT_MZSCHEME
1473 mzscheme_end();
1474#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001475#ifdef FEAT_TCL
1476 tcl_end();
1477#endif
1478#ifdef FEAT_RUBY
1479 ruby_end();
1480#endif
1481#ifdef FEAT_PYTHON
1482 python_end();
1483#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001484#ifdef FEAT_PYTHON3
1485 python3_end();
1486#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001487#ifdef FEAT_PERL
1488 perl_end();
1489#endif
1490#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1491 iconv_end();
1492#endif
1493#ifdef FEAT_NETBEANS_INTG
1494 netbeans_end();
1495#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001496#ifdef FEAT_CSCOPE
1497 cs_end();
1498#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001499#ifdef FEAT_EVAL
1500 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001501 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001502#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001503#if defined(WIN32) && defined(FEAT_MBYTE)
1504 free_cmd_argsW();
1505#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001506
1507 mch_exit(exitval);
1508}
1509
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001510#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1511/*
1512 * Setup to use the current locale (for ctype() and many other things).
1513 */
1514 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001515init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001516{
1517 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001518
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001519# ifdef FEAT_GUI_GTK
1520 /* Tell Gtk not to change our locale settings. */
1521 gtk_disable_setlocale();
1522# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001523# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1524 /* Make sure strtod() uses a decimal point, not a comma. */
1525 setlocale(LC_NUMERIC, "C");
1526# endif
1527
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001528# ifdef WIN32
1529 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1530 * text while it's expecting text in the current locale. This call avoids
1531 * that. */
1532 setlocale(LC_CTYPE, "C");
1533# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001534
1535# ifdef FEAT_GETTEXT
1536 {
1537 int mustfree = FALSE;
1538 char_u *p;
1539
1540# ifdef DYNAMIC_GETTEXT
1541 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001542 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001543# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001544 /* expand_env() doesn't work yet, because g_chartab[] is not
1545 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001546 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1547 if (p != NULL && *p != NUL)
1548 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001549 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001550 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1551 }
1552 if (mustfree)
1553 vim_free(p);
1554 textdomain(VIMPACKAGE);
1555 }
1556# endif
1557}
1558#endif
1559
1560/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001561 * Get the name of the display, before gui_prepare() removes it from
1562 * argv[]. Used for the xterm-clipboard display.
1563 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001564 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001565 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001566 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001567early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001568{
Bram Moolenaar03005972008-11-20 13:12:36 +00001569#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1570 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001571 int argc = parmp->argc;
1572 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001573 int i;
1574
1575 for (i = 1; i < argc; i++)
1576 {
1577 if (STRCMP(argv[i], "--") == 0)
1578 break;
1579# ifdef FEAT_XCLIPBOARD
1580 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001581# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001582 || STRICMP(argv[i], "--display") == 0
1583# endif
1584 )
1585 {
1586 if (i == argc - 1)
1587 mainerr_arg_missing((char_u *)argv[i]);
1588 xterm_display = argv[++i];
1589 }
1590# endif
1591# ifdef FEAT_CLIENTSERVER
1592 else if (STRICMP(argv[i], "--servername") == 0)
1593 {
1594 if (i == argc - 1)
1595 mainerr_arg_missing((char_u *)argv[i]);
1596 parmp->serverName_arg = (char_u *)argv[++i];
1597 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001598 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001599 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001600 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001601 {
1602 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001603# ifdef FEAT_GUI
1604 if (strstr(argv[i], "-wait") != 0)
1605 /* don't fork() when starting the GUI to edit files ourself */
1606 gui.dofork = FALSE;
1607# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001608 }
1609# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001610
1611# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1612# ifdef FEAT_GUI_W32
1613 else if (STRICMP(argv[i], "--windowid") == 0)
1614# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001615 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001616# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001617 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001618 long_u id;
1619 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001620
1621 if (i == argc - 1)
1622 mainerr_arg_missing((char_u *)argv[i]);
1623 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001624 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001625 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001626 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001627 if (count != 1)
1628 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1629 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001630# ifdef FEAT_GUI_W32
1631 win_socket_id = id;
1632# else
1633 gtk_socket_id = id;
1634# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001635 i++;
1636 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001637# endif
1638# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001639 else if (STRICMP(argv[i], "--echo-wid") == 0)
1640 echo_wid_arg = TRUE;
1641# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001642# ifndef FEAT_NETBEANS_INTG
1643 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001644 {
1645 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1646 mch_exit(2);
1647 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001648# endif
1649
Bram Moolenaar58d98232005-07-23 22:25:46 +00001650 }
1651#endif
1652}
1653
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001654#ifndef NO_VIM_MAIN
1655/*
1656 * Get a (optional) count for a Vim argument.
1657 */
1658 static int
1659get_number_arg(
1660 char_u *p, /* pointer to argument */
1661 int *idx, /* index in argument, is incremented */
1662 int def) /* default value */
1663{
1664 if (vim_isdigit(p[*idx]))
1665 {
1666 def = atoi((char *)&(p[*idx]));
1667 while (vim_isdigit(p[*idx]))
1668 *idx = *idx + 1;
1669 }
1670 return def;
1671}
1672
1673/*
1674 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1675 * If the executable name starts with "r" we disable shell commands.
1676 * If the next character is "e" we run in Easy mode.
1677 * If the next character is "g" we run the GUI version.
1678 * If the next characters are "view" we start in readonly mode.
1679 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1680 * If the next characters are "ex" we start in Ex mode. If it's followed
1681 * by "im" use improved Ex mode.
1682 */
1683 static void
1684parse_command_name(mparm_T *parmp)
1685{
1686 char_u *initstr;
1687
1688 initstr = gettail((char_u *)parmp->argv[0]);
1689
1690#ifdef MACOS_X_UNIX
1691 /* An issue has been seen when launching Vim in such a way that
1692 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1693 * executable or a symbolic link of it. Until this issue is resolved
1694 * we prohibit the GUI from being used.
1695 */
1696 if (STRCMP(initstr, parmp->argv[0]) == 0)
1697 disallow_gui = TRUE;
1698
1699 /* TODO: On MacOS X default to gui if argv[0] ends in:
1700 * /Vim.app/Contents/MacOS/Vim */
1701#endif
1702
1703#ifdef FEAT_EVAL
1704 set_vim_var_string(VV_PROGNAME, initstr, -1);
1705 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
1706#endif
1707
1708 if (TOLOWER_ASC(initstr[0]) == 'r')
1709 {
1710 restricted = TRUE;
1711 ++initstr;
1712 }
1713
1714 /* Use evim mode for "evim" and "egvim", not for "editor". */
1715 if (TOLOWER_ASC(initstr[0]) == 'e'
1716 && (TOLOWER_ASC(initstr[1]) == 'v'
1717 || TOLOWER_ASC(initstr[1]) == 'g'))
1718 {
1719#ifdef FEAT_GUI
1720 gui.starting = TRUE;
1721#endif
1722 parmp->evim_mode = TRUE;
1723 ++initstr;
1724 }
1725
1726 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1727 if (TOLOWER_ASC(initstr[0]) == 'g')
1728 {
1729 main_start_gui();
1730#ifdef FEAT_GUI
1731 ++initstr;
1732#endif
1733 }
1734
1735 if (STRNICMP(initstr, "view", 4) == 0)
1736 {
1737 readonlymode = TRUE;
1738 curbuf->b_p_ro = TRUE;
1739 p_uc = 10000; /* don't update very often */
1740 initstr += 4;
1741 }
1742 else if (STRNICMP(initstr, "vim", 3) == 0)
1743 initstr += 3;
1744
1745 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1746 if (STRICMP(initstr, "diff") == 0)
1747 {
1748#ifdef FEAT_DIFF
1749 parmp->diff_mode = TRUE;
1750#else
1751 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1752 mch_errmsg("\n");
1753 mch_exit(2);
1754#endif
1755 }
1756
1757 if (STRNICMP(initstr, "ex", 2) == 0)
1758 {
1759 if (STRNICMP(initstr + 2, "im", 2) == 0)
1760 exmode_active = EXMODE_VIM;
1761 else
1762 exmode_active = EXMODE_NORMAL;
1763 change_compatible(TRUE); /* set 'compatible' */
1764 }
1765}
1766
Bram Moolenaar58d98232005-07-23 22:25:46 +00001767/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001768 * Scan the command line arguments.
1769 */
1770 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001771command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001772{
1773 int argc = parmp->argc;
1774 char **argv = parmp->argv;
1775 int argv_idx; /* index in argv[n][] */
1776 int had_minmin = FALSE; /* found "--" argument */
1777 int want_argument; /* option argument with argument */
1778 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001779 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001780 long n;
1781
1782 --argc;
1783 ++argv;
1784 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1785 while (argc > 0)
1786 {
1787 /*
1788 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1789 */
1790 if (argv[0][0] == '+' && !had_minmin)
1791 {
1792 if (parmp->n_commands >= MAX_ARG_CMDS)
1793 mainerr(ME_EXTRA_CMD, NULL);
1794 argv_idx = -1; /* skip to next argument */
1795 if (argv[0][1] == NUL)
1796 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1797 else
1798 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1799 }
1800
1801 /*
1802 * Optional argument.
1803 */
1804 else if (argv[0][0] == '-' && !had_minmin)
1805 {
1806 want_argument = FALSE;
1807 c = argv[0][argv_idx++];
1808#ifdef VMS
1809 /*
1810 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1811 * and "-/X" as "-X".
1812 */
1813 if (c == '/')
1814 {
1815 c = argv[0][argv_idx++];
1816 c = TOUPPER_ASC(c);
1817 }
1818 else
1819 c = TOLOWER_ASC(c);
1820#endif
1821 switch (c)
1822 {
1823 case NUL: /* "vim -" read from stdin */
1824 /* "ex -" silent mode */
1825 if (exmode_active)
1826 silent_mode = TRUE;
1827 else
1828 {
1829 if (parmp->edit_type != EDIT_NONE)
1830 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1831 parmp->edit_type = EDIT_STDIN;
1832 read_cmd_fd = 2; /* read from stderr instead of stdin */
1833 }
1834 argv_idx = -1; /* skip to next argument */
1835 break;
1836
1837 case '-': /* "--" don't take any more option arguments */
1838 /* "--help" give help message */
1839 /* "--version" give version message */
1840 /* "--literal" take files literally */
1841 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001842 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001843 /* "--noplugin[s]" skip plugins */
1844 /* "--cmd <cmd>" execute cmd before vimrc */
1845 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1846 usage();
1847 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1848 {
1849 Columns = 80; /* need to init Columns */
1850 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1851 list_version();
1852 msg_putchar('\n');
1853 msg_didout = FALSE;
1854 mch_exit(0);
1855 }
1856 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1857 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001858#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001859 parmp->literal = TRUE;
1860#endif
1861 }
1862 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1863 {
1864#ifdef FEAT_GUI
1865 gui.dofork = FALSE; /* don't fork() when starting GUI */
1866#endif
1867 }
1868 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1869 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001870 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1871 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001872 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1873 {
1874 want_argument = TRUE;
1875 argv_idx += 3;
1876 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001877 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1878 {
1879 want_argument = TRUE;
1880 argv_idx += 11;
1881 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001882#ifdef FEAT_CLIENTSERVER
1883 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1884 ; /* already processed -- no arg */
1885 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1886 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1887 {
1888 /* already processed -- snatch the following arg */
1889 if (argc > 1)
1890 {
1891 --argc;
1892 ++argv;
1893 }
1894 }
1895#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001896#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1897# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001898 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001899# else
1900 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1901# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001902 {
1903 /* already processed -- snatch the following arg */
1904 if (argc > 1)
1905 {
1906 --argc;
1907 ++argv;
1908 }
1909 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001910#endif
1911#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001912 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1913 {
1914 /* already processed, skip */
1915 }
1916#endif
1917 else
1918 {
1919 if (argv[0][argv_idx])
1920 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1921 had_minmin = TRUE;
1922 }
1923 if (!want_argument)
1924 argv_idx = -1; /* skip to next argument */
1925 break;
1926
1927 case 'A': /* "-A" start in Arabic mode */
1928#ifdef FEAT_ARABIC
1929 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1930#else
1931 mch_errmsg(_(e_noarabic));
1932 mch_exit(2);
1933#endif
1934 break;
1935
1936 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001937 /* Needs to be effective before expanding file names, because
1938 * for Win32 this makes us edit a shortcut file itself,
1939 * instead of the file it links to. */
1940 set_options_bin(curbuf->b_p_bin, 1, 0);
1941 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001942 break;
1943
1944 case 'C': /* "-C" Compatible */
1945 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001946 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001947 break;
1948
1949 case 'e': /* "-e" Ex mode */
1950 exmode_active = EXMODE_NORMAL;
1951 break;
1952
1953 case 'E': /* "-E" Improved Ex mode */
1954 exmode_active = EXMODE_VIM;
1955 break;
1956
1957 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1958 window directly, not with newcli */
1959#ifdef FEAT_GUI
1960 gui.dofork = FALSE; /* don't fork() when starting GUI */
1961#endif
1962 break;
1963
1964 case 'g': /* "-g" start GUI */
1965 main_start_gui();
1966 break;
1967
1968 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1969#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001970 p_fkmap = TRUE;
1971 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001972#else
1973 mch_errmsg(_(e_nofarsi));
1974 mch_exit(2);
1975#endif
1976 break;
1977
1978 case 'h': /* "-h" give help message */
1979#ifdef FEAT_GUI_GNOME
1980 /* Tell usage() to exit for "gvim". */
1981 gui.starting = FALSE;
1982#endif
1983 usage();
1984 break;
1985
1986 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1987#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001988 p_hkmap = TRUE;
1989 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001990#else
1991 mch_errmsg(_(e_nohebrew));
1992 mch_exit(2);
1993#endif
1994 break;
1995
1996 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1997#ifdef FEAT_LISP
1998 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1999 p_sm = TRUE;
2000#endif
2001 break;
2002
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002003 case 'M': /* "-M" no changes or writing of files */
2004 reset_modifiable();
2005 /* FALLTHROUGH */
2006
2007 case 'm': /* "-m" no writing of files */
2008 p_write = FALSE;
2009 break;
2010
2011 case 'y': /* "-y" easy mode */
2012#ifdef FEAT_GUI
2013 gui.starting = TRUE; /* start GUI a bit later */
2014#endif
2015 parmp->evim_mode = TRUE;
2016 break;
2017
2018 case 'N': /* "-N" Nocompatible */
2019 change_compatible(FALSE);
2020 break;
2021
2022 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002023#ifdef FEAT_NETBEANS_INTG
2024 /* checking for "-nb", netbeans parameters */
2025 if (argv[0][argv_idx] == 'b')
2026 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002027 netbeansArg = argv[0];
2028 argv_idx = -1; /* skip to next argument */
2029 }
2030 else
2031#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002032 parmp->no_swap_file = TRUE;
2033 break;
2034
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002035 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002036#ifdef TARGET_API_MAC_OSX
2037 /* For some reason on MacOS X, an argument like:
2038 -psn_0_10223617 is passed in when invoke from Finder
2039 or with the 'open' command */
2040 if (argv[0][argv_idx] == 's')
2041 {
2042 argv_idx = -1; /* bypass full -psn */
2043 main_start_gui();
2044 break;
2045 }
2046#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002047#ifdef FEAT_WINDOWS
2048 /* default is 0: open window for each file */
2049 parmp->window_count = get_number_arg((char_u *)argv[0],
2050 &argv_idx, 0);
2051 parmp->window_layout = WIN_TABS;
2052#endif
2053 break;
2054
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002055 case 'o': /* "-o[N]" open N horizontal split windows */
2056#ifdef FEAT_WINDOWS
2057 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002058 parmp->window_count = get_number_arg((char_u *)argv[0],
2059 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002060 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002061#endif
2062 break;
2063
2064 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002065#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002066 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002067 parmp->window_count = get_number_arg((char_u *)argv[0],
2068 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002069 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002070#endif
2071 break;
2072
2073#ifdef FEAT_QUICKFIX
2074 case 'q': /* "-q" QuickFix mode */
2075 if (parmp->edit_type != EDIT_NONE)
2076 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2077 parmp->edit_type = EDIT_QF;
2078 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2079 {
2080 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2081 argv_idx = -1;
2082 }
2083 else if (argc > 1) /* "-q {errorfile}" */
2084 want_argument = TRUE;
2085 break;
2086#endif
2087
2088 case 'R': /* "-R" readonly mode */
2089 readonlymode = TRUE;
2090 curbuf->b_p_ro = TRUE;
2091 p_uc = 10000; /* don't update very often */
2092 break;
2093
2094 case 'r': /* "-r" recovery mode */
2095 case 'L': /* "-L" recovery mode */
2096 recoverymode = 1;
2097 break;
2098
2099 case 's':
2100 if (exmode_active) /* "-s" silent (batch) mode */
2101 silent_mode = TRUE;
2102 else /* "-s {scriptin}" read from script file */
2103 want_argument = TRUE;
2104 break;
2105
2106 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2107 if (parmp->edit_type != EDIT_NONE)
2108 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2109 parmp->edit_type = EDIT_TAG;
2110 if (argv[0][argv_idx]) /* "-t{tag}" */
2111 {
2112 parmp->tagname = (char_u *)argv[0] + argv_idx;
2113 argv_idx = -1;
2114 }
2115 else /* "-t {tag}" */
2116 want_argument = TRUE;
2117 break;
2118
2119#ifdef FEAT_EVAL
2120 case 'D': /* "-D" Debugging */
2121 parmp->use_debug_break_level = 9999;
2122 break;
2123#endif
2124#ifdef FEAT_DIFF
2125 case 'd': /* "-d" 'diff' */
2126# ifdef AMIGA
2127 /* check for "-dev {device}" */
2128 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2129 want_argument = TRUE;
2130 else
2131# endif
2132 parmp->diff_mode = TRUE;
2133 break;
2134#endif
2135 case 'V': /* "-V{N}" Verbose level */
2136 /* default is 10: a little bit verbose */
2137 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2138 if (argv[0][argv_idx] != NUL)
2139 {
2140 set_option_value((char_u *)"verbosefile", 0L,
2141 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002142 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002143 }
2144 break;
2145
2146 case 'v': /* "-v" Vi-mode (as if called "vi") */
2147 exmode_active = 0;
2148#ifdef FEAT_GUI
2149 gui.starting = FALSE; /* don't start GUI */
2150#endif
2151 break;
2152
2153 case 'w': /* "-w{number}" set window height */
2154 /* "-w {scriptout}" write to script */
2155 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2156 {
2157 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2158 set_option_value((char_u *)"window", n, NULL, 0);
2159 break;
2160 }
2161 want_argument = TRUE;
2162 break;
2163
2164#ifdef FEAT_CRYPT
2165 case 'x': /* "-x" encrypted reading/writing of files */
2166 parmp->ask_for_key = TRUE;
2167 break;
2168#endif
2169
2170 case 'X': /* "-X" don't connect to X server */
2171#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2172 x_no_connect = TRUE;
2173#endif
2174 break;
2175
2176 case 'Z': /* "-Z" restricted mode */
2177 restricted = TRUE;
2178 break;
2179
2180 case 'c': /* "-c{command}" or "-c {command}" execute
2181 command */
2182 if (argv[0][argv_idx] != NUL)
2183 {
2184 if (parmp->n_commands >= MAX_ARG_CMDS)
2185 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002186 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2187 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002188 argv_idx = -1;
2189 break;
2190 }
2191 /*FALLTHROUGH*/
2192 case 'S': /* "-S {file}" execute Vim script */
2193 case 'i': /* "-i {viminfo}" use for viminfo */
2194#ifndef FEAT_DIFF
2195 case 'd': /* "-d {device}" device (for Amiga) */
2196#endif
2197 case 'T': /* "-T {terminal}" terminal name */
2198 case 'u': /* "-u {vimrc}" vim inits file */
2199 case 'U': /* "-U {gvimrc}" gvim inits file */
2200 case 'W': /* "-W {scriptout}" overwrite */
2201#ifdef FEAT_GUI_W32
2202 case 'P': /* "-P {parent title}" MDI parent */
2203#endif
2204 want_argument = TRUE;
2205 break;
2206
2207 default:
2208 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2209 }
2210
2211 /*
2212 * Handle option arguments with argument.
2213 */
2214 if (want_argument)
2215 {
2216 /*
2217 * Check for garbage immediately after the option letter.
2218 */
2219 if (argv[0][argv_idx] != NUL)
2220 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2221
2222 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002223 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002224 mainerr_arg_missing((char_u *)argv[0]);
2225 ++argv;
2226 argv_idx = -1;
2227
2228 switch (c)
2229 {
2230 case 'c': /* "-c {command}" execute command */
2231 case 'S': /* "-S {file}" execute Vim script */
2232 if (parmp->n_commands >= MAX_ARG_CMDS)
2233 mainerr(ME_EXTRA_CMD, NULL);
2234 if (c == 'S')
2235 {
2236 char *a;
2237
2238 if (argc < 1)
2239 /* "-S" without argument: use default session file
2240 * name. */
2241 a = SESSION_FILE;
2242 else if (argv[0][0] == '-')
2243 {
2244 /* "-S" followed by another option: use default
2245 * session file name. */
2246 a = SESSION_FILE;
2247 ++argc;
2248 --argv;
2249 }
2250 else
2251 a = argv[0];
2252 p = alloc((unsigned)(STRLEN(a) + 4));
2253 if (p == NULL)
2254 mch_exit(2);
2255 sprintf((char *)p, "so %s", a);
2256 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2257 parmp->commands[parmp->n_commands++] = p;
2258 }
2259 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002260 parmp->commands[parmp->n_commands++] =
2261 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002262 break;
2263
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002264 case '-':
2265 if (argv[-1][2] == 'c')
2266 {
2267 /* "--cmd {command}" execute command */
2268 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2269 mainerr(ME_EXTRA_CMD, NULL);
2270 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002271 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002272 }
2273 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002274 break;
2275
2276 /* case 'd': -d {device} is handled in mch_check_win() for the
2277 * Amiga */
2278
2279#ifdef FEAT_QUICKFIX
2280 case 'q': /* "-q {errorfile}" QuickFix mode */
2281 parmp->use_ef = (char_u *)argv[0];
2282 break;
2283#endif
2284
2285 case 'i': /* "-i {viminfo}" use for viminfo */
2286 use_viminfo = (char_u *)argv[0];
2287 break;
2288
2289 case 's': /* "-s {scriptin}" read from script file */
2290 if (scriptin[0] != NULL)
2291 {
2292scripterror:
2293 mch_errmsg(_("Attempt to open script file again: \""));
2294 mch_errmsg(argv[-1]);
2295 mch_errmsg(" ");
2296 mch_errmsg(argv[0]);
2297 mch_errmsg("\"\n");
2298 mch_exit(2);
2299 }
2300 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2301 {
2302 mch_errmsg(_("Cannot open for reading: \""));
2303 mch_errmsg(argv[0]);
2304 mch_errmsg("\"\n");
2305 mch_exit(2);
2306 }
2307 if (save_typebuf() == FAIL)
2308 mch_exit(2); /* out of memory */
2309 break;
2310
2311 case 't': /* "-t {tag}" */
2312 parmp->tagname = (char_u *)argv[0];
2313 break;
2314
2315 case 'T': /* "-T {terminal}" terminal name */
2316 /*
2317 * The -T term argument is always available and when
2318 * HAVE_TERMLIB is supported it overrides the environment
2319 * variable TERM.
2320 */
2321#ifdef FEAT_GUI
2322 if (term_is_gui((char_u *)argv[0]))
2323 gui.starting = TRUE; /* start GUI a bit later */
2324 else
2325#endif
2326 parmp->term = (char_u *)argv[0];
2327 break;
2328
2329 case 'u': /* "-u {vimrc}" vim inits file */
2330 parmp->use_vimrc = (char_u *)argv[0];
2331 break;
2332
2333 case 'U': /* "-U {gvimrc}" gvim inits file */
2334#ifdef FEAT_GUI
2335 use_gvimrc = (char_u *)argv[0];
2336#endif
2337 break;
2338
2339 case 'w': /* "-w {nr}" 'window' value */
2340 /* "-w {scriptout}" append to script file */
2341 if (vim_isdigit(*((char_u *)argv[0])))
2342 {
2343 argv_idx = 0;
2344 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2345 set_option_value((char_u *)"window", n, NULL, 0);
2346 argv_idx = -1;
2347 break;
2348 }
2349 /*FALLTHROUGH*/
2350 case 'W': /* "-W {scriptout}" overwrite script file */
2351 if (scriptout != NULL)
2352 goto scripterror;
2353 if ((scriptout = mch_fopen(argv[0],
2354 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2355 {
2356 mch_errmsg(_("Cannot open for script output: \""));
2357 mch_errmsg(argv[0]);
2358 mch_errmsg("\"\n");
2359 mch_exit(2);
2360 }
2361 break;
2362
2363#ifdef FEAT_GUI_W32
2364 case 'P': /* "-P {parent title}" MDI parent */
2365 gui_mch_set_parent(argv[0]);
2366 break;
2367#endif
2368 }
2369 }
2370 }
2371
2372 /*
2373 * File name argument.
2374 */
2375 else
2376 {
2377 argv_idx = -1; /* skip to next argument */
2378
2379 /* Check for only one type of editing. */
2380 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2381 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2382 parmp->edit_type = EDIT_FILE;
2383
2384#ifdef MSWIN
2385 /* Remember if the argument was a full path before changing
2386 * slashes to backslashes. */
2387 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2388 parmp->full_path = TRUE;
2389#endif
2390
2391 /* Add the file to the global argument list. */
2392 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2393 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2394 mch_exit(2);
2395#ifdef FEAT_DIFF
2396 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2397 && !mch_isdir(alist_name(&GARGLIST[0])))
2398 {
2399 char_u *r;
2400
2401 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2402 if (r != NULL)
2403 {
2404 vim_free(p);
2405 p = r;
2406 }
2407 }
2408#endif
2409#if defined(__CYGWIN32__) && !defined(WIN32)
2410 /*
2411 * If vim is invoked by non-Cygwin tools, convert away any
2412 * DOS paths, so things like .swp files are created correctly.
2413 * Look for evidence of non-Cygwin paths before we bother.
2414 * This is only for when using the Unix files.
2415 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002416 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002417 {
2418 char posix_path[PATH_MAX];
2419
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002420# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2421 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2422# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002423 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002424# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002425 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002426 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002427 if (p == NULL)
2428 mch_exit(2);
2429 }
2430#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002431
2432#ifdef USE_FNAME_CASE
2433 /* Make the case of the file name match the actual file. */
2434 fname_case(p, 0);
2435#endif
2436
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002438#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002439 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440#else
2441 2 /* add buffer number now and use curbuf */
2442#endif
2443 );
2444
2445#if defined(FEAT_MBYTE) && defined(WIN32)
2446 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002447 /* Remember this argument has been added to the argument list.
2448 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002449 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002450# ifdef FEAT_DIFF
2451 parmp->diff_mode
2452# else
2453 FALSE
2454# endif
2455 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456 }
2457#endif
2458 }
2459
2460 /*
2461 * If there are no more letters after the current "-", go to next
2462 * argument. argv_idx is set to -1 when the current argument is to be
2463 * skipped.
2464 */
2465 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2466 {
2467 --argc;
2468 ++argv;
2469 argv_idx = 1;
2470 }
2471 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002472
2473#ifdef FEAT_EVAL
2474 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2475 * one. */
2476 if (parmp->n_commands > 0)
2477 {
2478 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2479 if (p != NULL)
2480 {
2481 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2482 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2483 vim_free(p);
2484 }
2485 }
2486#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002487}
2488
2489/*
2490 * Print a warning if stdout is not a terminal.
2491 * When starting in Ex mode and commands come from a file, set Silent mode.
2492 */
2493 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002494check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002495{
2496 int input_isatty; /* is active input a terminal? */
2497
2498 input_isatty = mch_input_isatty();
2499 if (exmode_active)
2500 {
2501 if (!input_isatty)
2502 silent_mode = TRUE;
2503 }
2504 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2505#ifdef FEAT_GUI
2506 /* don't want the delay when started from the desktop */
2507 && !gui.starting
2508#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002509 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002510 {
2511#ifdef NBDEBUG
2512 /*
2513 * This shouldn't be necessary. But if I run netbeans with the log
2514 * output coming to the console and XOpenDisplay fails, I get vim
2515 * trying to start with input/output to my console tty. This fills my
2516 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002517 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002518 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002519 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002520 {
2521 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2522 exit(1);
2523 }
2524#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002525#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2526 if (is_cygpty_used())
2527 {
2528 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2529 exit(1);
2530 }
2531#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002532 if (!parmp->stdout_isatty)
2533 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2534 if (!input_isatty)
2535 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2536 out_flush();
2537 if (scriptin[0] == NULL)
2538 ui_delay(2000L, TRUE);
2539 TIME_MSG("Warning delay");
2540 }
2541}
2542
2543/*
2544 * Read text from stdin.
2545 */
2546 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002547read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002548{
2549 int i;
2550
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002551#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002552 /* When getting the ATTENTION prompt here, use a dialog */
2553 swap_exists_action = SEA_DIALOG;
2554#endif
2555 no_wait_return = TRUE;
2556 i = msg_didany;
2557 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002558 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002559 no_wait_return = FALSE;
2560 msg_didany = i;
2561 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002562#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002563 check_swap_exists_action();
2564#endif
2565#if !(defined(AMIGA) || defined(MACOS))
2566 /*
2567 * Close stdin and dup it from stderr. Required for GPM to work
2568 * properly, and for running external commands.
2569 * Is there any other system that cannot do this?
2570 */
2571 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002572 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573#endif
2574}
2575
2576/*
2577 * Create the requested number of windows and edit buffers in them.
2578 * Also does recovery if "recoverymode" set.
2579 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002580 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002581create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002582{
2583#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002584 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002585 int done = 0;
2586
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587 /*
2588 * Create the number of windows that was requested.
2589 */
2590 if (parmp->window_count == -1) /* was not set */
2591 parmp->window_count = 1;
2592 if (parmp->window_count == 0)
2593 parmp->window_count = GARGCOUNT;
2594 if (parmp->window_count > 1)
2595 {
2596 /* Don't change the windows if there was a command in .vimrc that
2597 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002598 if (parmp->window_layout == 0)
2599 parmp->window_layout = WIN_HOR;
2600 if (parmp->window_layout == WIN_TABS)
2601 {
2602 parmp->window_count = make_tabpages(parmp->window_count);
2603 TIME_MSG("making tab pages");
2604 }
2605 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002606 {
2607 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002608 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002609 TIME_MSG("making windows");
2610 }
2611 else
2612 parmp->window_count = win_count();
2613 }
2614 else
2615 parmp->window_count = 1;
2616#endif
2617
2618 if (recoverymode) /* do recover */
2619 {
2620 msg_scroll = TRUE; /* scroll message up */
2621 ml_recover();
2622 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2623 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002624 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625 }
2626 else
2627 {
2628 /*
2629 * Open a buffer for windows that don't have one yet.
2630 * Commands in the .vimrc might have loaded a file or split the window.
2631 * Watch out for autocommands that delete a window.
2632 */
2633#ifdef FEAT_AUTOCMD
2634 /*
2635 * Don't execute Win/Buf Enter/Leave autocommands here
2636 */
2637 ++autocmd_no_enter;
2638 ++autocmd_no_leave;
2639#endif
2640#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002641 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002642 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002643 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002644 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002645 {
2646 if (parmp->window_layout == WIN_TABS)
2647 goto_tabpage(1);
2648 else
2649 curwin = firstwin;
2650 }
2651 else if (parmp->window_layout == WIN_TABS)
2652 {
2653 if (curtab->tp_next == NULL)
2654 break;
2655 goto_tabpage(0);
2656 }
2657 else
2658 {
2659 if (curwin->w_next == NULL)
2660 break;
2661 curwin = curwin->w_next;
2662 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002663 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002664#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002665 curbuf = curwin->w_buffer;
2666 if (curbuf->b_ml.ml_mfp == NULL)
2667 {
2668#ifdef FEAT_FOLDING
2669 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2670 if (p_fdls >= 0)
2671 curwin->w_p_fdl = p_fdls;
2672#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002673#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002674 /* When getting the ATTENTION prompt here, use a dialog */
2675 swap_exists_action = SEA_DIALOG;
2676#endif
2677 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002678
2679 /* create memfile, read file */
2680 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002681
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002682#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002683 if (swap_exists_action == SEA_QUIT)
2684 {
2685 if (got_int || only_one_window())
2686 {
2687 /* abort selected or quit and only one window */
2688 did_emsg = FALSE; /* avoid hit-enter prompt */
2689 getout(1);
2690 }
2691 /* We can't close the window, it would disturb what
2692 * happens next. Clear the file name and set the arg
2693 * index to -1 to delete it later. */
2694 setfname(curbuf, NULL, NULL, FALSE);
2695 curwin->w_arg_idx = -1;
2696 swap_exists_action = SEA_NONE;
2697 }
2698 else
2699 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002700#endif
2701#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002703#endif
2704 }
2705#ifdef FEAT_WINDOWS
2706 ui_breakcheck();
2707 if (got_int)
2708 {
2709 (void)vgetc(); /* only break the file loading, not the rest */
2710 break;
2711 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002712 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002713#endif
2714#ifdef FEAT_WINDOWS
2715 if (parmp->window_layout == WIN_TABS)
2716 goto_tabpage(1);
2717 else
2718 curwin = firstwin;
2719 curbuf = curwin->w_buffer;
2720#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002721#ifdef FEAT_AUTOCMD
2722 --autocmd_no_enter;
2723 --autocmd_no_leave;
2724#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002725 }
2726}
2727
2728#ifdef FEAT_WINDOWS
2729 /*
2730 * If opened more than one window, start editing files in the other
2731 * windows. make_windows() has already opened the windows.
2732 */
2733 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002734edit_buffers(
2735 mparm_T *parmp,
2736 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737{
2738 int arg_idx; /* index in argument list */
2739 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002740 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002741 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002742
2743# ifdef FEAT_AUTOCMD
2744 /*
2745 * Don't execute Win/Buf Enter/Leave autocommands here
2746 */
2747 ++autocmd_no_enter;
2748 ++autocmd_no_leave;
2749# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002750
2751 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2752 if (curwin->w_arg_idx == -1)
2753 {
2754 win_close(curwin, TRUE);
2755 advance = FALSE;
2756 }
2757
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758 arg_idx = 1;
2759 for (i = 1; i < parmp->window_count; ++i)
2760 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002761 if (cwd != NULL)
2762 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002763 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2764 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002765 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002766 ++arg_idx;
2767 win_close(curwin, TRUE);
2768 advance = FALSE;
2769 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002770 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002771
Bram Moolenaar84212822006-11-07 21:59:47 +00002772 if (advance)
2773 {
2774 if (parmp->window_layout == WIN_TABS)
2775 {
2776 if (curtab->tp_next == NULL) /* just checking */
2777 break;
2778 goto_tabpage(0);
2779 }
2780 else
2781 {
2782 if (curwin->w_next == NULL) /* just checking */
2783 break;
2784 win_enter(curwin->w_next, FALSE);
2785 }
2786 }
2787 advance = TRUE;
2788
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002789 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002790 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002791 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2792 {
2793 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002794 /* Edit file from arg list, if there is one. When "Quit" selected
2795 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002796# ifdef HAS_SWAP_EXISTS_ACTION
2797 swap_exists_did_quit = FALSE;
2798# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002799 (void)do_ecmd(0, arg_idx < GARGCOUNT
2800 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002801 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002802# ifdef HAS_SWAP_EXISTS_ACTION
2803 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002804 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002805 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002806 if (got_int || only_one_window())
2807 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002808 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002809 did_emsg = FALSE; /* avoid hit-enter prompt */
2810 getout(1);
2811 }
2812 win_close(curwin, TRUE);
2813 advance = FALSE;
2814 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002815# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816 if (arg_idx == GARGCOUNT - 1)
2817 arg_had_last = TRUE;
2818 ++arg_idx;
2819 }
2820 ui_breakcheck();
2821 if (got_int)
2822 {
2823 (void)vgetc(); /* only break the file loading, not the rest */
2824 break;
2825 }
2826 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002827
2828 if (parmp->window_layout == WIN_TABS)
2829 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002830# ifdef FEAT_AUTOCMD
2831 --autocmd_no_enter;
2832# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002833
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002834 /* make the first window the current window */
2835 win = firstwin;
2836#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2837 /* Avoid making a preview window the current window. */
2838 while (win->w_p_pvw)
2839 {
2840 win = win->w_next;
2841 if (win == NULL)
2842 {
2843 win = firstwin;
2844 break;
2845 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002846 }
2847#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002848 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002849
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850# ifdef FEAT_AUTOCMD
2851 --autocmd_no_leave;
2852# endif
2853 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002854 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002855 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2856}
2857#endif /* FEAT_WINDOWS */
2858
2859/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002860 * Execute the commands from --cmd arguments "cmds[cnt]".
2861 */
2862 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002863exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002864{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002865 char_u **cmds = parmp->pre_commands;
2866 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002867 int i;
2868
2869 if (cnt > 0)
2870 {
2871 curwin->w_cursor.lnum = 0; /* just in case.. */
2872 sourcing_name = (char_u *)_("pre-vimrc command line");
2873# ifdef FEAT_EVAL
2874 current_SID = SID_CMDARG;
2875# endif
2876 for (i = 0; i < cnt; ++i)
2877 do_cmdline_cmd(cmds[i]);
2878 sourcing_name = NULL;
2879# ifdef FEAT_EVAL
2880 current_SID = 0;
2881# endif
2882 TIME_MSG("--cmd commands");
2883 }
2884}
2885
2886/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002887 * Execute "+", "-c" and "-S" arguments.
2888 */
2889 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002890exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002891{
2892 int i;
2893
2894 /*
2895 * We start commands on line 0, make "vim +/pat file" match a
2896 * pattern on line 1. But don't move the cursor when an autocommand
2897 * with g`" was used.
2898 */
2899 msg_scroll = TRUE;
2900 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2901 curwin->w_cursor.lnum = 0;
2902 sourcing_name = (char_u *)"command line";
2903#ifdef FEAT_EVAL
2904 current_SID = SID_CARG;
2905#endif
2906 for (i = 0; i < parmp->n_commands; ++i)
2907 {
2908 do_cmdline_cmd(parmp->commands[i]);
2909 if (parmp->cmds_tofree[i])
2910 vim_free(parmp->commands[i]);
2911 }
2912 sourcing_name = NULL;
2913#ifdef FEAT_EVAL
2914 current_SID = 0;
2915#endif
2916 if (curwin->w_cursor.lnum == 0)
2917 curwin->w_cursor.lnum = 1;
2918
2919 if (!exmode_active)
2920 msg_scroll = FALSE;
2921
2922#ifdef FEAT_QUICKFIX
2923 /* When started with "-q errorfile" jump to first error again. */
2924 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002925 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002926#endif
2927 TIME_MSG("executing command arguments");
2928}
2929
2930/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002931 * Source startup scripts.
2932 */
2933 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002934source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002935{
2936 int i;
2937
2938 /*
2939 * For "evim" source evim.vim first of all, so that the user can overrule
2940 * any things he doesn't like.
2941 */
2942 if (parmp->evim_mode)
2943 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002944 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002945 TIME_MSG("source evim file");
2946 }
2947
2948 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002949 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002950 * nothing else.
2951 */
2952 if (parmp->use_vimrc != NULL)
2953 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002954 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2955 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002956 {
2957#ifdef FEAT_GUI
2958 if (use_gvimrc == NULL) /* don't load gvimrc either */
2959 use_gvimrc = parmp->use_vimrc;
2960#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002961 }
2962 else
2963 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002964 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002965 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2966 }
2967 }
2968 else if (!silent_mode)
2969 {
2970#ifdef AMIGA
2971 struct Process *proc = (struct Process *)FindTask(0L);
2972 APTR save_winptr = proc->pr_WindowPtr;
2973
2974 /* Avoid a requester here for a volume that doesn't exist. */
2975 proc->pr_WindowPtr = (APTR)-1L;
2976#endif
2977
2978 /*
2979 * Get system wide defaults, if the file name is defined.
2980 */
2981#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002982 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002983#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002984#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002985 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002986#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002987
2988 /*
2989 * Try to read initialization commands from the following places:
2990 * - environment variable VIMINIT
2991 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2992 * - second user vimrc file ($VIM/.vimrc for Dos)
2993 * - environment variable EXINIT
2994 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2995 * - second user exrc file ($VIM/.exrc for Dos)
2996 * The first that exists is used, the rest is ignored.
2997 */
2998 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2999 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003000 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003002 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3003 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004#endif
3005#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003006 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3007 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003009#ifdef USR_VIMRC_FILE4
3010 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3011 DOSO_VIMRC) == FAIL
3012#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003013 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003014 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003015#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003016 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003017#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003018 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003019 {
3020 /* When no .vimrc file was found: source defaults.vim. */
3021 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003022 }
3023 }
3024
3025 /*
3026 * Read initialization commands from ".vimrc" or ".exrc" in current
3027 * directory. This is only done if the 'exrc' option is set.
3028 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003029 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003030 * option has been reset in environment of global ".exrc" or ".vimrc".
3031 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3032 * SYS_VIMRC_FILE.
3033 */
3034 if (p_exrc)
3035 {
3036#if defined(UNIX) || defined(VMS)
3037 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3038 if (!file_owned(VIMRC_FILE))
3039#endif
3040 secure = p_secure;
3041
3042 i = FAIL;
3043 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3044 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3045#ifdef USR_VIMRC_FILE2
3046 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3047 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3048#endif
3049#ifdef USR_VIMRC_FILE3
3050 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3051 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3052#endif
3053#ifdef SYS_VIMRC_FILE
3054 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3055 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3056#endif
3057 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003058 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003059
3060 if (i == FAIL)
3061 {
3062#if defined(UNIX) || defined(VMS)
3063 /* if ".exrc" is not owned by user set 'secure' mode */
3064 if (!file_owned(EXRC_FILE))
3065 secure = p_secure;
3066 else
3067 secure = 0;
3068#endif
3069 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3070 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3071#ifdef USR_EXRC_FILE2
3072 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3073 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3074#endif
3075 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003076 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003077 }
3078 }
3079 if (secure == 2)
3080 need_wait_return = TRUE;
3081 secure = 0;
3082#ifdef AMIGA
3083 proc->pr_WindowPtr = save_winptr;
3084#endif
3085 }
3086 TIME_MSG("sourcing vimrc file(s)");
3087}
3088
3089/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003090 * Setup to start using the GUI. Exit with an error when not available.
3091 */
3092 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003093main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003094{
3095#ifdef FEAT_GUI
3096 gui.starting = TRUE; /* start GUI a bit later */
3097#else
3098 mch_errmsg(_(e_nogvim));
3099 mch_errmsg("\n");
3100 mch_exit(2);
3101#endif
3102}
3103
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003104#endif /* NO_VIM_MAIN */
3105
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003106/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003107 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003108 * Returns FAIL if the environment variable was not executed, OK otherwise.
3109 */
3110 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003111process_env(
3112 char_u *env,
3113 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003114{
3115 char_u *initstr;
3116 char_u *save_sourcing_name;
3117 linenr_T save_sourcing_lnum;
3118#ifdef FEAT_EVAL
3119 scid_T save_sid;
3120#endif
3121
3122 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3123 {
3124 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003125 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003126 save_sourcing_name = sourcing_name;
3127 save_sourcing_lnum = sourcing_lnum;
3128 sourcing_name = env;
3129 sourcing_lnum = 0;
3130#ifdef FEAT_EVAL
3131 save_sid = current_SID;
3132 current_SID = SID_ENV;
3133#endif
3134 do_cmdline_cmd(initstr);
3135 sourcing_name = save_sourcing_name;
3136 sourcing_lnum = save_sourcing_lnum;
3137#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003138 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003139#endif
3140 return OK;
3141 }
3142 return FAIL;
3143}
3144
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003145#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003146/*
3147 * Return TRUE if we are certain the user owns the file "fname".
3148 * Used for ".vimrc" and ".exrc".
3149 * Use both stat() and lstat() for extra security.
3150 */
3151 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003152file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003153{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003154 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003155# ifdef UNIX
3156 uid_t uid = getuid();
3157# else /* VMS */
3158 uid_t uid = ((getgid() << 16) | getuid());
3159# endif
3160
3161 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3162# ifdef HAVE_LSTAT
3163 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3164# endif
3165 );
3166}
3167#endif
3168
3169/*
3170 * Give an error message main_errors["n"] and exit.
3171 */
3172 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003173mainerr(
3174 int n, /* one of the ME_ defines */
3175 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003176{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003177#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003178 reset_signals(); /* kill us with CTRL-C here, if you like */
3179#endif
3180
3181 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003182 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003183 mch_errmsg(_(main_errors[n]));
3184 if (str != NULL)
3185 {
3186 mch_errmsg(": \"");
3187 mch_errmsg((char *)str);
3188 mch_errmsg("\"");
3189 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003190 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003191
3192 mch_exit(1);
3193}
3194
3195 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003196mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003197{
3198 mainerr(ME_ARG_MISSING, str);
3199}
3200
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003201#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202/*
3203 * print a message with three spaces prepended and '\n' appended.
3204 */
3205 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003206main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003207{
3208 mch_msg(" ");
3209 mch_msg(s);
3210 mch_msg("\n");
3211}
3212
3213/*
3214 * Print messages for "vim -h" or "vim --help" and exit.
3215 */
3216 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003217usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218{
3219 int i;
3220 static char *(use[]) =
3221 {
3222 N_("[file ..] edit specified file(s)"),
3223 N_("- read text from stdin"),
3224 N_("-t tag edit file where tag is defined"),
3225#ifdef FEAT_QUICKFIX
3226 N_("-q [errorfile] edit file with first error")
3227#endif
3228 };
3229
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003230#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003231 reset_signals(); /* kill us with CTRL-C here, if you like */
3232#endif
3233
3234 mch_msg(longVersion);
3235 mch_msg(_("\n\nusage:"));
3236 for (i = 0; ; ++i)
3237 {
3238 mch_msg(_(" vim [arguments] "));
3239 mch_msg(_(use[i]));
3240 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3241 break;
3242 mch_msg(_("\n or:"));
3243 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003244#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003245 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003246#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003247
3248 mch_msg(_("\n\nArguments:\n"));
3249 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003250#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003251 main_msg(_("--literal\t\tDon't expand wildcards"));
3252#endif
3253#ifdef FEAT_OLE
3254 main_msg(_("-register\t\tRegister this gvim for OLE"));
3255 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3256#endif
3257#ifdef FEAT_GUI
3258 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3259 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3260#endif
3261 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3262 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003263 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003264 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3265#ifdef FEAT_DIFF
3266 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3267#endif
3268 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3269 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3270 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3271 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3272 main_msg(_("-M\t\t\tModifications in text not allowed"));
3273 main_msg(_("-b\t\t\tBinary mode"));
3274#ifdef FEAT_LISP
3275 main_msg(_("-l\t\t\tLisp mode"));
3276#endif
3277 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3278 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003279 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3280#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003281 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003282#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003283 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3284 main_msg(_("-r\t\t\tList swap files and exit"));
3285 main_msg(_("-r (with file name)\tRecover crashed session"));
3286 main_msg(_("-L\t\t\tSame as -r"));
3287#ifdef AMIGA
3288 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3289 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3290#endif
3291#ifdef FEAT_ARABIC
3292 main_msg(_("-A\t\t\tstart in Arabic mode"));
3293#endif
3294#ifdef FEAT_RIGHTLEFT
3295 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3296#endif
3297#ifdef FEAT_FKMAP
3298 main_msg(_("-F\t\t\tStart in Farsi mode"));
3299#endif
3300 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003301 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003302 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3303#ifdef FEAT_GUI
3304 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3305#endif
3306 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003307#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003308 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003309 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3310 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003311#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003312 main_msg(_("+\t\t\tStart at end of file"));
3313 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003314 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003315 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3316 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3317 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3318 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3319 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3320#ifdef FEAT_CRYPT
3321 main_msg(_("-x\t\t\tEdit encrypted files"));
3322#endif
3323#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3324# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3325 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3326# endif
3327 main_msg(_("-X\t\t\tDo not connect to X server"));
3328#endif
3329#ifdef FEAT_CLIENTSERVER
3330 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3331 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3332 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3333 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003334# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003335 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003336# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003337 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3338 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3339 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3340 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3341#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003342#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003343 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003344#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003345#ifdef FEAT_VIMINFO
3346 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3347#endif
3348 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3349 main_msg(_("--version\t\tPrint version information and exit"));
3350
3351#ifdef FEAT_GUI_X11
3352# ifdef FEAT_GUI_MOTIF
3353 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3354# else
3355# ifdef FEAT_GUI_ATHENA
3356# ifdef FEAT_GUI_NEXTAW
3357 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3358# else
3359 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3360# endif
3361# endif
3362# endif
3363 main_msg(_("-display <display>\tRun vim on <display>"));
3364 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003365 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3366 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3367 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3368 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3369 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3370 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3371 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3372 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3373# ifdef FEAT_GUI_ATHENA
3374 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3375# endif
3376 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3377 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3378 main_msg(_("-xrm <resource>\tSet the specified resource"));
3379#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003380#ifdef FEAT_GUI_GTK
3381 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3382 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3383 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3384 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3385 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003388 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003389#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003390#ifdef FEAT_GUI_W32
3391 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003392 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393#endif
3394
3395#ifdef FEAT_GUI_GNOME
3396 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3397 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003398 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003400 gui.dofork = FALSE;
3401 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402 else
3403#endif
3404 mch_exit(0);
3405}
3406
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003407#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408/*
3409 * Check the result of the ATTENTION dialog:
3410 * When "Quit" selected, exit Vim.
3411 * When "Recover" selected, recover the file.
3412 */
3413 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003414check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415{
3416 if (swap_exists_action == SEA_QUIT)
3417 getout(1);
3418 handle_swap_exists(NULL);
3419}
3420#endif
3421
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003422#endif
3423
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003425static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003426
3427static struct timeval prev_timeval;
3428
Bram Moolenaar3f269672009-11-03 11:11:11 +00003429# ifdef WIN3264
3430/*
3431 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3432 */
3433 static int
3434gettimeofday(struct timeval *tv, char *dummy)
3435{
3436 long t = clock();
3437 tv->tv_sec = t / CLOCKS_PER_SEC;
3438 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3439 return 0;
3440}
3441# endif
3442
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443/*
3444 * Save the previous time before doing something that could nest.
3445 * set "*tv_rel" to the time elapsed so far.
3446 */
3447 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003448time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003449{
3450 *((struct timeval *)tv_rel) = prev_timeval;
3451 gettimeofday(&prev_timeval, NULL);
3452 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3453 - ((struct timeval *)tv_rel)->tv_usec;
3454 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3455 - ((struct timeval *)tv_rel)->tv_sec;
3456 if (((struct timeval *)tv_rel)->tv_usec < 0)
3457 {
3458 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3459 --((struct timeval *)tv_rel)->tv_sec;
3460 }
3461 *(struct timeval *)tv_start = prev_timeval;
3462}
3463
3464/*
3465 * Compute the previous time after doing something that could nest.
3466 * Subtract "*tp" from prev_timeval;
3467 * Note: The arguments are (void *) to avoid trouble with systems that don't
3468 * have struct timeval.
3469 */
3470 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003471time_pop(
3472 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003473{
3474 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3475 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3476 if (prev_timeval.tv_usec < 0)
3477 {
3478 prev_timeval.tv_usec += 1000000;
3479 --prev_timeval.tv_sec;
3480 }
3481}
3482
3483 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003484time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003485{
3486 long usec;
3487 long msec;
3488
3489 usec = now->tv_usec - then->tv_usec;
3490 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3491 usec = usec % 1000L;
3492 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3493}
3494
3495 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003496time_msg(
3497 char *mesg,
3498 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003499 (struct timeval *) */
3500{
3501 static struct timeval start;
3502 struct timeval now;
3503
3504 if (time_fd != NULL)
3505 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003506 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003507 {
3508 gettimeofday(&start, NULL);
3509 prev_timeval = start;
3510 fprintf(time_fd, "\n\ntimes in msec\n");
3511 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3512 fprintf(time_fd, " clock elapsed: other lines\n\n");
3513 }
3514 gettimeofday(&now, NULL);
3515 time_diff(&start, &now);
3516 if (((struct timeval *)tv_start) != NULL)
3517 {
3518 fprintf(time_fd, " ");
3519 time_diff(((struct timeval *)tv_start), &now);
3520 }
3521 fprintf(time_fd, " ");
3522 time_diff(&prev_timeval, &now);
3523 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003524 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003525 }
3526}
3527
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003528#endif
3529
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003530#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003531
3532/*
3533 * Common code for the X command server and the Win32 command server.
3534 */
3535
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003536static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003537
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003538/*
3539 * Do the client-server stuff, unless "--servername ''" was used.
3540 */
3541 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003542exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003543{
3544 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3545 {
3546# ifdef WIN32
3547 /* Initialise the client/server messaging infrastructure. */
3548 serverInitMessaging();
3549# endif
3550
3551 /*
3552 * When a command server argument was found, execute it. This may
3553 * exit Vim when it was successful. Otherwise it's executed further
3554 * on. Remember the encoding used here in "serverStrEnc".
3555 */
3556 if (parmp->serverArg)
3557 {
3558 cmdsrv_main(&parmp->argc, parmp->argv,
3559 parmp->serverName_arg, &parmp->serverStr);
3560# ifdef FEAT_MBYTE
3561 parmp->serverStrEnc = vim_strsave(p_enc);
3562# endif
3563 }
3564
3565 /* If we're still running, get the name to register ourselves.
3566 * On Win32 can register right now, for X11 need to setup the
3567 * clipboard first, it's further down. */
3568 parmp->servername = serverMakeName(parmp->serverName_arg,
3569 parmp->argv[0]);
3570# ifdef WIN32
3571 if (parmp->servername != NULL)
3572 {
3573 serverSetName(parmp->servername);
3574 vim_free(parmp->servername);
3575 }
3576# endif
3577 }
3578}
3579
3580/*
3581 * Prepare for running as a Vim server.
3582 */
3583 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003584prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003585{
3586# if defined(FEAT_X11)
3587 /*
3588 * Register for remote command execution with :serversend and --remote
3589 * unless there was a -X or a --servername '' on the command line.
3590 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003591 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003592 */
3593 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3594# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003595 (gui.in_use
3596# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003597 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003598# endif
3599 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003600# endif
3601 parmp->serverName_arg != NULL))
3602 {
3603 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3604 vim_free(parmp->servername);
3605 TIME_MSG("register server name");
3606 }
3607 else
3608 serverDelayedStartName = parmp->servername;
3609# endif
3610
3611 /*
3612 * Execute command ourselves if we're here because the send failed (or
3613 * else we would have exited above).
3614 */
3615 if (parmp->serverStr != NULL)
3616 {
3617 char_u *p;
3618
3619 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3620 parmp->serverStr, &p));
3621 vim_free(p);
3622 }
3623}
3624
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003625 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003626cmdsrv_main(
3627 int *argc,
3628 char **argv,
3629 char_u *serverName_arg,
3630 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003631{
3632 char_u *res;
3633 int i;
3634 char_u *sname;
3635 int ret;
3636 int didone = FALSE;
3637 int exiterr = 0;
3638 char **newArgV = argv + 1;
3639 int newArgC = 1,
3640 Argc = *argc;
3641 int argtype;
3642#define ARGTYPE_OTHER 0
3643#define ARGTYPE_EDIT 1
3644#define ARGTYPE_EDIT_WAIT 2
3645#define ARGTYPE_SEND 3
3646 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003647 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003648# ifndef FEAT_X11
3649 HWND srv;
3650# else
3651 Window srv;
3652
3653 setup_term_clip();
3654# endif
3655
3656 sname = serverMakeName(serverName_arg, argv[0]);
3657 if (sname == NULL)
3658 return;
3659
3660 /*
3661 * Execute the command server related arguments and remove them
3662 * from the argc/argv array; We may have to return into main()
3663 */
3664 for (i = 1; i < Argc; i++)
3665 {
3666 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003667 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003668 {
3669 for (; i < *argc; i++)
3670 {
3671 *newArgV++ = argv[i];
3672 newArgC++;
3673 }
3674 break;
3675 }
3676
Bram Moolenaareb94e552006-03-11 21:35:11 +00003677 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003678 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003679 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3680 {
3681 char *p = argv[i] + 8;
3682
3683 argtype = ARGTYPE_EDIT;
3684 while (*p != NUL)
3685 {
3686 if (STRNICMP(p, "-wait", 5) == 0)
3687 {
3688 argtype = ARGTYPE_EDIT_WAIT;
3689 p += 5;
3690 }
3691 else if (STRNICMP(p, "-silent", 7) == 0)
3692 {
3693 silent = TRUE;
3694 p += 7;
3695 }
3696 else if (STRNICMP(p, "-tab", 4) == 0)
3697 {
3698 tabs = TRUE;
3699 p += 4;
3700 }
3701 else
3702 {
3703 argtype = ARGTYPE_OTHER;
3704 break;
3705 }
3706 }
3707 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003708 else
3709 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003710
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003711 if (argtype != ARGTYPE_OTHER)
3712 {
3713 if (i == *argc - 1)
3714 mainerr_arg_missing((char_u *)argv[i]);
3715 if (argtype == ARGTYPE_SEND)
3716 {
3717 *serverStr = (char_u *)argv[i + 1];
3718 i++;
3719 }
3720 else
3721 {
3722 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003723 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003724 if (*serverStr == NULL)
3725 {
3726 /* Probably out of memory, exit. */
3727 didone = TRUE;
3728 exiterr = 1;
3729 break;
3730 }
3731 Argc = i;
3732 }
3733# ifdef FEAT_X11
3734 if (xterm_dpy == NULL)
3735 {
3736 mch_errmsg(_("No display"));
3737 ret = -1;
3738 }
3739 else
3740 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3741 NULL, &srv, 0, 0, silent);
3742# else
3743 /* Win32 always works? */
3744 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3745# endif
3746 if (ret < 0)
3747 {
3748 if (argtype == ARGTYPE_SEND)
3749 {
3750 /* Failed to send, abort. */
3751 mch_errmsg(_(": Send failed.\n"));
3752 didone = TRUE;
3753 exiterr = 1;
3754 }
3755 else if (!silent)
3756 /* Let vim start normally. */
3757 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3758 break;
3759 }
3760
3761# ifdef FEAT_GUI_W32
3762 /* Guess that when the server name starts with "g" it's a GUI
3763 * server, which we can bring to the foreground here.
3764 * Foreground() in the server doesn't work very well. */
3765 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3766 SetForegroundWindow(srv);
3767# endif
3768
3769 /*
3770 * For --remote-wait: Wait until the server did edit each
3771 * file. Also detect that the server no longer runs.
3772 */
3773 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3774 {
3775 int numFiles = *argc - i - 1;
3776 int j;
3777 char_u *done = alloc(numFiles);
3778 char_u *p;
3779# ifdef FEAT_GUI_W32
3780 NOTIFYICONDATA ni;
3781 int count = 0;
3782 extern HWND message_window;
3783# endif
3784
3785 if (numFiles > 0 && argv[i + 1][0] == '+')
3786 /* Skip "+cmd" argument, don't wait for it to be edited. */
3787 --numFiles;
3788
3789# ifdef FEAT_GUI_W32
3790 ni.cbSize = sizeof(ni);
3791 ni.hWnd = message_window;
3792 ni.uID = 0;
3793 ni.uFlags = NIF_ICON|NIF_TIP;
3794 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3795 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3796 Shell_NotifyIcon(NIM_ADD, &ni);
3797# endif
3798
3799 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003800 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003801 while (memchr(done, 0, numFiles) != NULL)
3802 {
3803# ifdef WIN32
3804 p = serverGetReply(srv, NULL, TRUE, TRUE);
3805 if (p == NULL)
3806 break;
3807# else
3808 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3809 break;
3810# endif
3811 j = atoi((char *)p);
3812 if (j >= 0 && j < numFiles)
3813 {
3814# ifdef FEAT_GUI_W32
3815 ++count;
3816 sprintf(ni.szTip, _("%d of %d edited"),
3817 count, numFiles);
3818 Shell_NotifyIcon(NIM_MODIFY, &ni);
3819# endif
3820 done[j] = 1;
3821 }
3822 }
3823# ifdef FEAT_GUI_W32
3824 Shell_NotifyIcon(NIM_DELETE, &ni);
3825# endif
3826 }
3827 }
3828 else if (STRICMP(argv[i], "--remote-expr") == 0)
3829 {
3830 if (i == *argc - 1)
3831 mainerr_arg_missing((char_u *)argv[i]);
3832# ifdef WIN32
3833 /* Win32 always works? */
3834 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3835 &res, NULL, 1, FALSE) < 0)
3836# else
3837 if (xterm_dpy == NULL)
3838 mch_errmsg(_("No display: Send expression failed.\n"));
3839 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3840 &res, NULL, 1, 1, FALSE) < 0)
3841# endif
3842 {
3843 if (res != NULL && *res != NUL)
3844 {
3845 /* Output error from remote */
3846 mch_errmsg((char *)res);
3847 vim_free(res);
3848 res = NULL;
3849 }
3850 mch_errmsg(_(": Send expression failed.\n"));
3851 }
3852 }
3853 else if (STRICMP(argv[i], "--serverlist") == 0)
3854 {
3855# ifdef WIN32
3856 /* Win32 always works? */
3857 res = serverGetVimNames();
3858# else
3859 if (xterm_dpy != NULL)
3860 res = serverGetVimNames(xterm_dpy);
3861# endif
3862 if (called_emsg)
3863 mch_errmsg("\n");
3864 }
3865 else if (STRICMP(argv[i], "--servername") == 0)
3866 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003867 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003868 i++;
3869 continue;
3870 }
3871 else
3872 {
3873 *newArgV++ = argv[i];
3874 newArgC++;
3875 continue;
3876 }
3877 didone = TRUE;
3878 if (res != NULL && *res != NUL)
3879 {
3880 mch_msg((char *)res);
3881 if (res[STRLEN(res) - 1] != '\n')
3882 mch_msg("\n");
3883 }
3884 vim_free(res);
3885 }
3886
3887 if (didone)
3888 {
3889 display_errors(); /* display any collected messages */
3890 exit(exiterr); /* Mission accomplished - get out */
3891 }
3892
3893 /* Return back into main() */
3894 *argc = newArgC;
3895 vim_free(sname);
3896}
3897
3898/*
3899 * Build a ":drop" command to send to a Vim server.
3900 */
3901 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003902build_drop_cmd(
3903 int filec,
3904 char **filev,
3905 int tabs, /* Use ":tab drop" instead of ":drop". */
3906 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003907{
3908 garray_T ga;
3909 int i;
3910 char_u *inicmd = NULL;
3911 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003912 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003913 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003914
3915 if (filec > 0 && filev[0][0] == '+')
3916 {
3917 inicmd = (char_u *)filev[0] + 1;
3918 filev++;
3919 filec--;
3920 }
3921 /* Check if we have at least one argument. */
3922 if (filec <= 0)
3923 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003924
3925 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003926 cwd = alloc(MAXPATHL);
3927 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003928 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003929 if (mch_dirname(cwd, MAXPATHL) != OK)
3930 {
3931 vim_free(cwd);
3932 return NULL;
3933 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003934 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003935#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003936 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003937#else
3938 PATH_ESC_CHARS,
3939#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003940 '\\', TRUE);
3941 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003942 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003943 return NULL;
3944 ga_init2(&ga, 1, 100);
3945 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003946 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003947
3948 /* Call inputsave() so that a prompt for an encryption key works. */
3949 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3950 if (tabs)
3951 ga_concat(&ga, (char_u *)"tab ");
3952 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003953 for (i = 0; i < filec; i++)
3954 {
3955 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003956 * do it again in the Vim server. On MS-Windows only escape
3957 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003958 p = vim_strsave_escaped((char_u *)filev[i],
3959#ifdef UNIX
3960 PATH_ESC_CHARS
3961#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003962 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003963#endif
3964 );
3965 if (p == NULL)
3966 {
3967 vim_free(ga.ga_data);
3968 return NULL;
3969 }
3970 ga_concat(&ga, (char_u *)" ");
3971 ga_concat(&ga, p);
3972 vim_free(p);
3973 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003974 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3975
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003976 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3977 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003978 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3979
3980 /* Switch back to the correct current directory (prior to temporary path
3981 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003982 * correct after the :drop command. With line breaks and spaces:
3983 * if !exists('+acd') || !&acd
3984 * if haslocaldir()
3985 * cd -
3986 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003987 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003988 * cd -
3989 * endif
3990 * endif
3991 */
3992 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003993 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003994 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003995 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003996 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003997
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003998 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003999 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4000 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004001 if (inicmd != NULL)
4002 {
4003 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4004 * the following commands to be inserted as text. Use a "|",
4005 * hopefully "inicmd" does allow this... */
4006 ga_concat(&ga, inicmd);
4007 ga_concat(&ga, (char_u *)"|");
4008 }
4009 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4010 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004011 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004012 ga_append(&ga, NUL);
4013 return ga.ga_data;
4014}
4015
4016/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004017 * Make our basic server name: use the specified "arg" if given, otherwise use
4018 * the tail of the command "cmd" we were started with.
4019 * Return the name in allocated memory. This doesn't include a serial number.
4020 */
4021 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004022serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004023{
4024 char_u *p;
4025
4026 if (arg != NULL && *arg != NUL)
4027 p = vim_strsave_up(arg);
4028 else
4029 {
4030 p = vim_strsave_up(gettail((char_u *)cmd));
4031 /* Remove .exe or .bat from the name. */
4032 if (p != NULL && vim_strchr(p, '.') != NULL)
4033 *vim_strchr(p, '.') = NUL;
4034 }
4035 return p;
4036}
4037#endif /* FEAT_CLIENTSERVER */
4038
4039#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4040/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004041 * Replace termcodes such as <CR> and insert as key presses if there is room.
4042 */
4043 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004044server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004045{
4046 char_u *ptr = NULL;
4047 char_u *cpo_save = p_cpo;
4048
4049 /* Set 'cpoptions' the way we want it.
4050 * B set - backslashes are *not* treated specially
4051 * k set - keycodes are *not* reverse-engineered
4052 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004053 * The last but one parameter of replace_termcodes() is TRUE so that the
4054 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004055 */
4056 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004057 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004058 p_cpo = cpo_save;
4059
4060 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4061 {
4062 /*
4063 * Add the string to the input stream.
4064 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4065 *
4066 * First clear typed characters from the typeahead buffer, there could
4067 * be half a mapping there. Then append to the existing string, so
4068 * that multiple commands from a client are concatenated.
4069 */
4070 if (typebuf.tb_maplen < typebuf.tb_len)
4071 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4072 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4073
4074 /* Let input_available() know we inserted text in the typeahead
4075 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004076 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004077 }
4078 vim_free((char_u *)ptr);
4079}
4080
4081/*
4082 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004083 */
4084 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004085eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004086{
4087 char_u *res;
4088 int save_dbl = debug_break_level;
4089 int save_ro = redir_off;
4090
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004091 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4092 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004093 debug_break_level = -1;
4094 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004095 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4096 * to be typed. Do generate errors so that try/catch works. */
4097 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004098
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004099 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004100
4101 debug_break_level = save_dbl;
4102 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004103 --emsg_silent;
4104 if (emsg_silent < 0)
4105 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004106
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004107 /* A client can tell us to redraw, but not to display the cursor, so do
4108 * that here. */
4109 setcursor();
4110 out_flush();
4111#ifdef FEAT_GUI
4112 if (gui.in_use)
4113 gui_update_cursor(FALSE, FALSE);
4114#endif
4115
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004116 return res;
4117}
4118
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004119/*
4120 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4121 * return an allocated string. Otherwise return "data".
4122 * "*tofree" is set to the result when it needs to be freed later.
4123 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004124 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004125serverConvert(
4126 char_u *client_enc UNUSED,
4127 char_u *data,
4128 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004129{
4130 char_u *res = data;
4131
4132 *tofree = NULL;
4133# ifdef FEAT_MBYTE
4134 if (client_enc != NULL && p_enc != NULL)
4135 {
4136 vimconv_T vimconv;
4137
4138 vimconv.vc_type = CONV_NONE;
4139 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4140 && vimconv.vc_type != CONV_NONE)
4141 {
4142 res = string_convert(&vimconv, data, NULL);
4143 if (res == NULL)
4144 res = data;
4145 else
4146 *tofree = res;
4147 }
4148 convert_setup(&vimconv, NULL, NULL);
4149 }
4150# endif
4151 return res;
4152}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004153#endif