blob: f24bda2387f6b58027c346bce66d246365434b1d [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 Moolenaarb4210b32004-06-13 14:51:16 +000093 int
94# ifdef VIMDLL
95_export
96# endif
97# ifdef FEAT_GUI_MSWIN
98# ifdef __BORLANDC__
99_cdecl
100# endif
101VimMain
102# else
103main
104# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100105(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000106{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000107 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000108 mparm_T params; /* various parameters passed between
109 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000110#ifdef STARTUPTIME
111 int i;
112#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000113
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000114 /*
115 * Do any system-specific initialisations. These can NOT use IObuff or
116 * NameBuff. Thus emsg2() cannot be called!
117 */
118 mch_early_init();
119
Bram Moolenaar14993322014-09-09 12:25:33 +0200120#if defined(WIN32) && defined(FEAT_MBYTE)
121 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200122 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200123 * convert when 'encoding' changes. Get the unexpanded arguments.
124 */
125 argc = get_cmd_argsW(&argv);
126#endif
127
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000128 /* Many variables are in "params" so that we can pass them to invoked
129 * functions without a lot of arguments. "argc" and "argv" are also
130 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000131 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000132 params.argc = argc;
133 params.argv = argv;
134 params.want_full_screen = TRUE;
135#ifdef FEAT_EVAL
136 params.use_debug_break_level = -1;
137#endif
138#ifdef FEAT_WINDOWS
139 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000140#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000141
Bram Moolenaar99685e62013-05-11 13:56:18 +0200142#ifdef FEAT_RUBY
143 {
144 int ruby_stack_start;
145 vim_ruby_init((void *)&ruby_stack_start);
146 }
147#endif
148
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000149#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000150 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000151#endif
152
153#ifdef MEM_PROFILE
154 atexit(vim_mem_profile_dump);
155#endif
156
157#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000158 for (i = 1; i < argc; ++i)
159 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000160 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000161 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000162 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000163 TIME_MSG("--- VIM STARTING ---");
164 break;
165 }
166 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000167#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000168 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000169
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200170 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000171
172#ifdef FEAT_CLIENTSERVER
173 /*
174 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000175 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000177 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178#endif
179
180 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000181 * Figure out the way to work from the command name argv[0].
182 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000183 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000184 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000185
186 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000187 * Process the command line arguments. File names are put in the global
188 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000189 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000190 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000191 TIME_MSG("parsing arguments");
192
193 /*
194 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000195 * there is no terminal version, and on Windows we can't fork one off with
196 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000197 */
198#ifdef ALWAYS_USE_GUI
199 gui.starting = TRUE;
200#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000201# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000202 /*
203 * Check if the GUI can be started. Reset gui.starting if not.
204 * Don't know about other systems, stay on the safe side and don't check.
205 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000206 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000207 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000208 if (gui_init_check() == FAIL)
209 {
210 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000211
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000212 /* When running "evim" or "gvim -y" we need the menus, exit if we
213 * don't have them. */
214 if (params.evim_mode)
215 mch_exit(1);
216 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000217 }
218# endif
219#endif
220
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000221 if (GARGCOUNT > 0)
222 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100223#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 /*
225 * Expand wildcards in file names.
226 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000227 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000228 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200229 start_dir = alloc(MAXPATHL);
230 if (start_dir != NULL)
231 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232 /* Temporarily add '(' and ')' to 'isfname'. These are valid
233 * filename characters but are excluded from 'isfname' to make
234 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
235 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000236 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000237 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200238 if (start_dir != NULL)
239 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 }
241#endif
242 fname = alist_name(&GARGLIST[0]);
243 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000244
245#if defined(WIN32) && defined(FEAT_MBYTE)
246 {
247 extern void set_alist_count(void);
248
249 /* Remember the number of entries in the argument list. If it changes
250 * we don't react on setting 'encoding'. */
251 set_alist_count();
252 }
253#endif
254
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000255#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000256 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000257 {
258 /*
259 * If there is one filename, fully qualified, we have very probably
260 * been invoked from explorer, so change to the file's directory.
261 * Hint: to avoid this when typing a command use a forward slash.
262 * If the cd fails, it doesn't matter.
263 */
264 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200265 if (start_dir != NULL)
266 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000267 }
268#endif
269 TIME_MSG("expanding arguments");
270
271#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000272 if (params.diff_mode && params.window_count == -1)
273 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000274#endif
275
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000276 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277 ++RedrawingDisabled;
278
279 /*
280 * When listing swap file names, don't do cursor positioning et. al.
281 */
282 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000283 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000284
285 /*
286 * When certain to start the GUI, don't check capabilities of terminal.
287 * For GTK we can't be sure, but when started from the desktop it doesn't
288 * make sense to try using a terminal.
289 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000290#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291 if (gui.starting
292# ifdef FEAT_GUI_GTK
293 && !isatty(2)
294# endif
295 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000297#endif
298
299#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
300 /* When the GUI is started from Finder, need to display messages in a
301 * message box. isatty(2) returns TRUE anyway, thus we need to check the
302 * name to know we're not started from a terminal. */
303 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000304 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000305 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000306
307 /* Avoid always using "/" as the current directory. Note that when
308 * started from Finder the arglist will be filled later in
309 * HandleODocAE() and "fname" will be NULL. */
310 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
311 && STRCMP(NameBuff, "/") == 0)
312 {
313 if (fname != NULL)
314 (void)vim_chdirfile(fname);
315 else
316 {
317 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
318 vim_chdir(NameBuff);
319 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200320 if (start_dir != NULL)
321 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000322 }
323 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324#endif
325
326 /*
327 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100328 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329 * Note that we may use mch_exit() before mch_init()!
330 */
331 mch_init();
332 TIME_MSG("shell init");
333
334#ifdef USE_XSMP
335 /*
336 * For want of anywhere else to do it, try to connect to xsmp here.
337 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
338 * Hijacking -X 'no X connection' to also disable XSMP connection as that
339 * has a similar delay upon failure.
340 * Only try if SESSION_MANAGER is set to something non-null.
341 */
342 if (!x_no_connect)
343 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000344 char *p = getenv("SESSION_MANAGER");
345
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000346 if (p != NULL && *p != NUL)
347 {
348 xsmp_init();
349 TIME_MSG("xsmp init");
350 }
351 }
352#endif
353
354 /*
355 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000356 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000358
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000359 /* This message comes before term inits, but after setting "silent_mode"
360 * when the input is not a tty. */
361 if (GARGCOUNT > 1 && !silent_mode)
362 printf(_("%d files to edit\n"), GARGCOUNT);
363
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000366 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 capabilities (will set full_screen) */
368 screen_start(); /* don't know where cursor is now */
369 TIME_MSG("Termcap init");
370 }
371
372 /*
373 * Set the default values for the options that use Rows and Columns.
374 */
375 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000376 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000377#ifdef FEAT_DIFF
378 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
379 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000380 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000381 diff_win_options(firstwin, FALSE);
382#endif
383
384 cmdline_row = Rows - p_ch;
385 msg_row = cmdline_row;
386 screenalloc(FALSE); /* allocate screen buffers */
387 set_init_2();
388 TIME_MSG("inits 2");
389
390 msg_scroll = TRUE;
391 no_wait_return = TRUE;
392
393 init_mappings(); /* set up initial mappings */
394
395 init_highlight(TRUE, FALSE); /* set the default highlight groups */
396 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397
398#ifdef FEAT_EVAL
399 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000400 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000401#endif
402
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100403#ifdef FEAT_MZSCHEME
404 /*
405 * Newer version of MzScheme (Racket) require earlier (trampolined)
406 * initialisation via scheme_main_setup.
407 * Implement this by initialising it as early as possible
408 * and splitting off remaining Vim main into vim_main2
409 */
410 {
411 /* Pack up preprocessed command line arguments.
412 * It is safe because Scheme does not access argc/argv. */
413 char *args[2];
414 args[0] = (char *)fname;
415 args[1] = (char *)&params;
416 return mzscheme_main(2, args);
417 }
418}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100419#endif
420#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100421
Bram Moolenaar8866d272012-11-28 15:55:42 +0100422/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
423 * NO_VIM_MAIN is defined. */
424#ifdef FEAT_MZSCHEME
425 int
426vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100427{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100428# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100429 char_u *fname = (char_u *)argv[0];
430 mparm_T params;
431
432 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100433# else
434 return 0;
435}
436# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100437#endif
438
Bram Moolenaar8866d272012-11-28 15:55:42 +0100439#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000440 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000441 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000442
Bram Moolenaar58d98232005-07-23 22:25:46 +0000443 /* Source startup scripts. */
444 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000445
446#ifdef FEAT_EVAL
447 /*
448 * Read all the plugin files.
449 * Only when compiled with +eval, since most plugins need it.
450 */
451 if (p_lpl)
452 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000453# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100454 source_runtime((char_u *)"plugin/*.vim", DIP_ALL);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000455# else
Bram Moolenaar7f8989d2016-03-12 22:11:39 +0100456 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000457# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000458 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100459
Bram Moolenaar2d8f56a2016-03-12 20:34:27 +0100460 ex_packloadall(NULL);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100461 TIME_MSG("loading packages");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000462 }
463#endif
464
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000465#ifdef FEAT_DIFF
466 /* Decide about window layout for diff mode after reading vimrc. */
467 if (params.diff_mode && params.window_layout == 0)
468 {
469 if (diffopt_horizontal())
470 params.window_layout = WIN_HOR; /* use horizontal split */
471 else
472 params.window_layout = WIN_VER; /* use vertical split */
473 }
474#endif
475
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000476 /*
477 * Recovery mode without a file name: List swap files.
478 * This uses the 'dir' option, therefore it must be after the
479 * initializations.
480 */
481 if (recoverymode && fname == NULL)
482 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200483 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000484 mch_exit(0);
485 }
486
487 /*
488 * Set a few option defaults after reading .vimrc files:
489 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
490 */
491 set_init_3();
492 TIME_MSG("inits 3");
493
494 /*
495 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
496 * Note that this overrides anything from a vimrc file.
497 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000498 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000499 p_uc = 0;
500
501#ifdef FEAT_FKMAP
502 if (curwin->w_p_rl && p_altkeymap)
503 {
504 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
505# ifdef FEAT_ARABIC
506 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
507# endif
508 p_fkmap = TRUE; /* Set the Farsi keymap mode */
509 }
510#endif
511
512#ifdef FEAT_GUI
513 if (gui.starting)
514 {
515#if defined(UNIX) || defined(VMS)
516 /* When something caused a message from a vimrc script, need to output
517 * an extra newline before the shell prompt. */
518 if (did_emsg || msg_didout)
519 putchar('\n');
520#endif
521
522 gui_start(); /* will set full_screen to TRUE */
523 TIME_MSG("starting GUI");
524
525 /* When running "evim" or "gvim -y" we need the menus, exit if we
526 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000527 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528 mch_exit(1);
529 }
530#endif
531
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532#ifdef FEAT_VIMINFO
533 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000534 * Read in registers, history etc, but not marks, from the viminfo file.
535 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000536 */
537 if (*p_viminfo != NUL)
538 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000539 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000540 TIME_MSG("reading viminfo");
541 }
542#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100543#ifdef FEAT_EVAL
544 /* It's better to make v:oldfiles an empty list than NULL. */
545 if (get_vim_var_list(VV_OLDFILES) == NULL)
546 set_vim_var_list(VV_OLDFILES, list_alloc());
547#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548
549#ifdef FEAT_QUICKFIX
550 /*
551 * "-q errorfile": Load the error file now.
552 * If the error file can't be read, exit before doing anything else.
553 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000554 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000556 if (params.use_ef != NULL)
557 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000558 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200559 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
560 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561 {
562 out_char('\n');
563 mch_exit(3);
564 }
565 TIME_MSG("reading errorfile");
566 }
567#endif
568
569 /*
570 * Start putting things on the screen.
571 * Scroll screen down before drawing over it
572 * Clear screen now, so file message will not be cleared.
573 */
574 starting = NO_BUFFERS;
575 no_wait_return = FALSE;
576 if (!exmode_active)
577 msg_scroll = FALSE;
578
579#ifdef FEAT_GUI
580 /*
581 * This seems to be required to make callbacks to be called now, instead
582 * of after things have been put on the screen, which then may be deleted
583 * when getting a resize callback.
584 * For the Mac this handles putting files dropped on the Vim icon to
585 * global_alist.
586 */
587 if (gui.in_use)
588 {
589# ifdef FEAT_SUN_WORKSHOP
590 if (!usingSunWorkShop)
591# endif
592 gui_wait_for_chars(50L);
593 TIME_MSG("GUI delay");
594 }
595#endif
596
597#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
598 qnx_clip_init();
599#endif
600
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200601#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
602 clip_init(TRUE);
603#endif
604
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000605#ifdef FEAT_XCLIPBOARD
606 /* Start using the X clipboard, unless the GUI was started. */
607# ifdef FEAT_GUI
608 if (!gui.in_use)
609# endif
610 {
611 setup_term_clip();
612 TIME_MSG("setup clipboard");
613 }
614#endif
615
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000616#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000617 /* Prepare for being a Vim server. */
618 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000619#endif
620
621 /*
622 * If "-" argument given: Read file from stdin.
623 * Do this before starting Raw mode, because it may change things that the
624 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
625 * are the same terminal: "cat | vim -".
626 * Using autocommands here may cause trouble...
627 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000628 if (params.edit_type == EDIT_STDIN && !recoverymode)
629 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000630
631#if defined(UNIX) || defined(VMS)
632 /* When switching screens and something caused a message from a vimrc
633 * script, need to output an extra newline on exit. */
634 if ((did_emsg || msg_didout) && *T_TI != NUL)
635 newline_on_exit = TRUE;
636#endif
637
638 /*
639 * When done something that is not allowed or error message call
640 * wait_return. This must be done before starttermcap(), because it may
641 * switch to another screen. It must be done after settmode(TMODE_RAW),
642 * because we want to react on a single key stroke.
643 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000644 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000645 */
646 settmode(TMODE_RAW);
647 TIME_MSG("setting raw mode");
648
649 if (need_wait_return || msg_didany)
650 {
651 wait_return(TRUE);
652 TIME_MSG("waiting for return");
653 }
654
655 starttermcap(); /* start termcap if not done by wait_return() */
656 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200657#if defined(FEAT_TERMRESPONSE)
658# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200659 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200660# endif
661 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100662#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000663
664#ifdef FEAT_MOUSE
665 setmouse(); /* may start using the mouse */
666#endif
667 if (scroll_region)
668 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000669 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000670
671 /*
672 * Don't clear the screen when starting in Ex mode, unless using the GUI.
673 */
674 if (exmode_active
675#ifdef FEAT_GUI
676 && !gui.in_use
677#endif
678 )
679 must_redraw = CLEAR;
680 else
681 {
682 screenclear(); /* clear screen */
683 TIME_MSG("clearing screen");
684 }
685
686#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000687 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000688 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100689 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200690 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691 TIME_MSG("getting crypt key");
692 }
693#endif
694
695 no_wait_return = TRUE;
696
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000697 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000698 * Create the requested number of windows and edit buffers in them.
699 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000700 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000701 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702 TIME_MSG("opening buffers");
703
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000704#ifdef FEAT_EVAL
705 /* clear v:swapcommand */
706 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
707#endif
708
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000709 /* Ex starts at last line of the file */
710 if (exmode_active)
711 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
712
713#ifdef FEAT_AUTOCMD
714 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
715 TIME_MSG("BufEnter autocommands");
716#endif
717 setpcmark();
718
719#ifdef FEAT_QUICKFIX
720 /*
721 * When started with "-q errorfile" jump to first error now.
722 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000723 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000725 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726 TIME_MSG("jump to first error");
727 }
728#endif
729
730#ifdef FEAT_WINDOWS
731 /*
732 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000733 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000734 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200735 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000736#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200737 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738
739#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000740 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000741 {
742 win_T *wp;
743
744 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200745 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000746 diff_win_options(wp, TRUE);
747 }
748#endif
749
750 /*
751 * Shorten any of the filenames, but only when absolute.
752 */
753 shorten_fnames(FALSE);
754
755 /*
756 * Need to jump to the tag before executing the '-c command'.
757 * Makes "vim -c '/return' -t main" work.
758 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000759 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000760 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000761#if defined(HAS_SWAP_EXISTS_ACTION)
762 swap_exists_did_quit = FALSE;
763#endif
764
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000765 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000766 do_cmdline_cmd(IObuff);
767 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000768
769#if defined(HAS_SWAP_EXISTS_ACTION)
770 /* If the user doesn't want to edit the file then we quit here. */
771 if (swap_exists_did_quit)
772 getout(1);
773#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000774 }
775
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000776 /* Execute any "+", "-c" and "-S" arguments. */
777 if (params.n_commands > 0)
778 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000779
780 RedrawingDisabled = 0;
781 redraw_all_later(NOT_VALID);
782 no_wait_return = FALSE;
783 starting = 0;
784
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200785 /* 'autochdir' has been postponed */
786 DO_AUTOCHDIR
787
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000788#ifdef FEAT_TERMRESPONSE
789 /* Requesting the termresponse is postponed until here, so that a "-c q"
790 * argument doesn't make it appear in the shell Vim was started from. */
791 may_req_termresponse();
792#endif
793
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000794 /* start in insert mode */
795 if (p_im)
796 need_start_insertmode = TRUE;
797
Bram Moolenaar14735512016-03-26 21:00:08 +0100798#ifdef FEAT_EVAL
799 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
800#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801#ifdef FEAT_AUTOCMD
802 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
803 TIME_MSG("VimEnter autocommands");
804#endif
805
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200806#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
807 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
808 * done after the clipboard is available and all initial commands that may
809 * modify the 'clipboard' setting have run; i.e. just before entering the
810 * main loop. */
811 {
812 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100813
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200814 adjust_clip_reg(&default_regname);
815 set_reg_var(default_regname);
816 }
817#endif
818
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000819#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
820 /* When a startup script or session file setup for diff'ing and
821 * scrollbind, sync the scrollbind now. */
822 if (curwin->w_p_diff && curwin->w_p_scb)
823 {
824 update_topline();
825 check_scrollbind((linenr_T)0, 0L);
826 TIME_MSG("diff scrollbinding");
827 }
828#endif
829
830#if defined(WIN3264) && !defined(FEAT_GUI_W32)
831 mch_set_winsize_now(); /* Allow winsize changes from now on */
832#endif
833
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000834#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
835 /* When tab pages were created, may need to update the tab pages line and
836 * scrollbars. This is skipped while creating them. */
837 if (first_tabpage->tp_next != NULL)
838 {
839 out_flush();
840 gui_init_which_components(NULL);
841 gui_update_scrollbars(TRUE);
842 }
843 need_mouse_correct = TRUE;
844#endif
845
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000846 /* If ":startinsert" command used, stuff a dummy command to be able to
847 * call normal_cmd(), which will then start Insert mode. */
848 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000849 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850
851#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200852 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200853 {
854# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200855# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200856 && !defined(FEAT_GUI_W32)
857 if (gui.in_use)
858 {
859 mch_errmsg(_("netbeans is not supported with this GUI\n"));
860 mch_exit(2);
861 }
862# endif
863# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000864 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200865 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200866 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000867#endif
868
869 TIME_MSG("before starting main loop");
870
871 /*
872 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100873 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000874 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875
876 return 0;
877}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100878#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100879#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880
881/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200882 * Initialisation shared by main() and some tests.
883 */
884 void
885common_init(mparm_T *params)
886{
887
888#ifdef FEAT_MBYTE
889 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
890#endif
891#ifdef FEAT_EVAL
892 eval_init(); /* init global variables */
893#endif
894
895#ifdef __QNXNTO__
896 qnx_init(); /* PhAttach() for clipboard, (and gui) */
897#endif
898
899#ifdef MAC_OS_CLASSIC
900 /* Prepare for possibly starting GUI sometime */
901 /* Macintosh needs this before any memory is allocated. */
902 gui_prepare(&params->argc, params->argv);
903 TIME_MSG("GUI prepared");
904#endif
905
906 /* Init the table of Normal mode commands. */
907 init_normal_cmds();
908
909#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
910 make_version(); /* Construct the long version string. */
911#endif
912
913 /*
914 * Allocate space for the generic buffers (needed for set_init_1() and
915 * EMSG2()).
916 */
917 if ((IObuff = alloc(IOSIZE)) == NULL
918 || (NameBuff = alloc(MAXPATHL)) == NULL)
919 mch_exit(0);
920 TIME_MSG("Allocated generic buffers");
921
922#ifdef NBDEBUG
923 /* Wait a moment for debugging NetBeans. Must be after allocating
924 * NameBuff. */
925 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
926 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
927 TIME_MSG("NetBeans debug wait");
928#endif
929
930#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
931 /*
932 * Setup to use the current locale (for ctype() and many other things).
933 * NOTE: Translated messages with encodings other than latin1 will not
934 * work until set_init_1() has been called!
935 */
936 init_locale();
937 TIME_MSG("locale set");
938#endif
939
940#ifdef FEAT_GUI
941 gui.dofork = TRUE; /* default is to use fork() */
942#endif
943
944 /*
945 * Do a first scan of the arguments in "argv[]":
946 * -display or --display
947 * --server...
948 * --socketid
949 * --windowid
950 */
951 early_arg_scan(params);
952
953#ifdef FEAT_SUN_WORKSHOP
954 findYourself(params->argv[0]);
955#endif
956#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
957 /* Prepare for possibly starting GUI sometime */
958 gui_prepare(&params->argc, params->argv);
959 TIME_MSG("GUI prepared");
960#endif
961
962#ifdef FEAT_CLIPBOARD
963 clip_init(FALSE); /* Initialise clipboard stuff */
964 TIME_MSG("clipboard setup");
965#endif
966
967 /*
968 * Check if we have an interactive window.
969 * On the Amiga: If there is no window, we open one with a newcli command
970 * (needed for :! to * work). mch_check_win() will also handle the -d or
971 * -dev argument.
972 */
973 params->stdout_isatty = (mch_check_win(params->argc, params->argv) != FAIL);
974 TIME_MSG("window checked");
975
976 /*
977 * Allocate the first window and buffer.
978 * Can't do anything without it, exit when it fails.
979 */
980 if (win_alloc_first() == FAIL)
981 mch_exit(0);
982
983 init_yank(); /* init yank buffers */
984
985 alist_init(&global_alist); /* Init the argument list to empty. */
986 global_alist.id = 0;
987
988 /*
989 * Set the default values for the options.
990 * NOTE: Non-latin1 translated messages are working only after this,
991 * because this is where "has_mbyte" will be set, which is used by
992 * msg_outtrans_len_attr().
993 * First find out the home directory, needed to expand "~" in options.
994 */
995 init_homedir(); /* find real value of $HOME */
996 set_init_1();
997 TIME_MSG("inits 1");
998
999#ifdef FEAT_EVAL
1000 set_lang_var(); /* set v:lang and v:ctype */
1001#endif
1002}
1003
1004/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001005 * Main loop: Execute Normal mode commands until exiting Vim.
1006 * Also used to handle commands in the command-line window, until the window
1007 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001008 * Also used to handle ":visual" command after ":global": execute Normal mode
1009 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001010 */
1011 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001012main_loop(
1013 int cmdwin, /* TRUE when working in the command-line window */
1014 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001015{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001016 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001017 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001018#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001019 /* these are static to avoid a compiler warning */
1020 static linenr_T conceal_old_cursor_line = 0;
1021 static linenr_T conceal_new_cursor_line = 0;
1022 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001023#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001024
1025#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1026 /* Setup to catch a terminating error from the X server. Just ignore
1027 * it, restore the state and continue. This might not always work
1028 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001029 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001030 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001031 {
1032 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001033 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001034 got_int = TRUE;
1035 need_wait_return = FALSE;
1036 global_busy = FALSE;
1037 exmode_active = 0;
1038 skip_redraw = FALSE;
1039 RedrawingDisabled = 0;
1040 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001041 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001042# ifdef FEAT_EVAL
1043 emsg_skip = 0;
1044# endif
1045 emsg_off = 0;
1046# ifdef FEAT_MOUSE
1047 setmouse();
1048# endif
1049 settmode(TMODE_RAW);
1050 starttermcap();
1051 scroll_start();
1052 redraw_later_clear();
1053 }
1054#endif
1055
1056 clear_oparg(&oa);
1057 while (!cmdwin
1058#ifdef FEAT_CMDWIN
1059 || cmdwin_result == 0
1060#endif
1061 )
1062 {
1063 if (stuff_empty())
1064 {
1065 did_check_timestamps = FALSE;
1066 if (need_check_timestamps)
1067 check_timestamps(FALSE);
1068 if (need_wait_return) /* if wait_return still needed ... */
1069 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001070 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001071 {
1072 need_start_insertmode = FALSE;
1073 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1074 /* skip the fileinfo message now, because it would be shown
1075 * after insert mode finishes! */
1076 need_fileinfo = FALSE;
1077 }
1078 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001079
1080 /* Reset "got_int" now that we got back to the main loop. Except when
1081 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1082 * the ":g" command.
1083 * For ":g/pat/vi" we reset "got_int" when used once. When used
1084 * a second time we go back to Ex mode and abort the ":g" command. */
1085 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001086 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001087 if (noexmode && global_busy && !exmode_active && previous_got_int)
1088 {
1089 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1090 * used and keep "got_int" set, so that it aborts ":g". */
1091 exmode_active = EXMODE_NORMAL;
1092 State = NORMAL;
1093 }
1094 else if (!global_busy || !exmode_active)
1095 {
1096 if (!quit_more)
1097 (void)vgetc(); /* flush all buffers */
1098 got_int = FALSE;
1099 }
1100 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001101 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001102 else
1103 previous_got_int = FALSE;
1104
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001105 if (!exmode_active)
1106 msg_scroll = FALSE;
1107 quit_more = FALSE;
1108
1109 /*
1110 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1111 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1112 * update cursor and redraw.
1113 */
1114 if (skip_redraw || exmode_active)
1115 skip_redraw = FALSE;
1116 else if (do_redraw || stuff_empty())
1117 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001118#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001119 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001120 if (!finish_op && (
1121# ifdef FEAT_AUTOCMD
1122 has_cursormoved()
1123# endif
1124# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1125 ||
1126# endif
1127# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001128 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001129# endif
1130 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001131# ifdef FEAT_AUTOCMD
1132 && !equalpos(last_cursormoved, curwin->w_cursor)
1133# endif
1134 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001135 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001136# ifdef FEAT_AUTOCMD
1137 if (has_cursormoved())
1138 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1139 FALSE, curbuf);
1140# endif
1141# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001142 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001143 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001144# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001145 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001146# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001147 conceal_new_cursor_line = curwin->w_cursor.lnum;
1148 conceal_update_lines = TRUE;
1149 }
1150# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001151# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001152 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001153# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001154 }
1155#endif
1156
Bram Moolenaar186628f2013-03-19 13:33:23 +01001157#ifdef FEAT_AUTOCMD
1158 /* Trigger TextChanged if b_changedtick differs. */
1159 if (!finish_op && has_textchanged()
1160 && last_changedtick != curbuf->b_changedtick)
1161 {
1162 if (last_changedtick_buf == curbuf)
1163 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1164 FALSE, curbuf);
1165 last_changedtick_buf = curbuf;
1166 last_changedtick = curbuf->b_changedtick;
1167 }
1168#endif
1169
Bram Moolenaar33aec762006-01-22 23:30:12 +00001170#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1171 /* Scroll-binding for diff mode may have been postponed until
1172 * here. Avoids doing it for every change. */
1173 if (diff_need_scrollbind)
1174 {
1175 check_scrollbind((linenr_T)0, 0L);
1176 diff_need_scrollbind = FALSE;
1177 }
1178#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001179#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001180 /* Include a closed fold completely in the Visual area. */
1181 foldAdjustVisual();
1182#endif
1183#ifdef FEAT_FOLDING
1184 /*
1185 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1186 * contain the cursor.
1187 * When 'foldopen' is "all", open the fold(s) under the cursor.
1188 * This may mark the window for redrawing.
1189 */
1190 if (hasAnyFolding(curwin) && !char_avail())
1191 {
1192 foldCheckClose();
1193 if (fdo_flags & FDO_ALL)
1194 foldOpenCursor();
1195 }
1196#endif
1197
1198 /*
1199 * Before redrawing, make sure w_topline is correct, and w_leftcol
1200 * if lines don't wrap, and w_skipcol if lines wrap.
1201 */
1202 update_topline();
1203 validate_cursor();
1204
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001205 if (VIsual_active)
1206 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001207 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001208 update_screen(0);
1209 else if (redraw_cmdline || clear_cmdline)
1210 showmode();
1211#ifdef FEAT_WINDOWS
1212 redraw_statuslines();
1213#endif
1214#ifdef FEAT_TITLE
1215 if (need_maketitle)
1216 maketitle();
1217#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001218#ifdef FEAT_VIMINFO
1219 curbuf->b_last_used = vim_time();
1220#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001221 /* display message after redraw */
1222 if (keep_msg != NULL)
1223 {
1224 char_u *p;
1225
1226 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001227 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1228 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 p = keep_msg;
1230 msg_attr(p, keep_msg_attr);
1231 vim_free(p);
1232 }
1233 if (need_fileinfo) /* show file info after redraw */
1234 {
1235 fileinfo(FALSE, TRUE, FALSE);
1236 need_fileinfo = FALSE;
1237 }
1238
1239 emsg_on_display = FALSE; /* can delete error message now */
1240 did_emsg = FALSE;
1241 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001242 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001243 showruler(FALSE);
1244
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001245# if defined(FEAT_CONCEAL)
1246 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001247 && (conceal_old_cursor_line != conceal_new_cursor_line
1248 || conceal_cursor_line(curwin)
1249 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001250 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001251 if (conceal_old_cursor_line != conceal_new_cursor_line
1252 && conceal_old_cursor_line
1253 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001254 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001255 update_single_line(curwin, conceal_new_cursor_line);
1256 curwin->w_valid &= ~VALID_CROW;
1257 }
1258# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001259 setcursor();
1260 cursor_on();
1261
1262 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001263
1264#ifdef STARTUPTIME
1265 /* Now that we have drawn the first screen all the startup stuff
1266 * has been done, close any file for startup messages. */
1267 if (time_fd != NULL)
1268 {
1269 TIME_MSG("first screen update");
1270 TIME_MSG("--- VIM STARTED ---");
1271 fclose(time_fd);
1272 time_fd = NULL;
1273 }
1274#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001275 }
1276#ifdef FEAT_GUI
1277 if (need_mouse_correct)
1278 gui_mouse_correct();
1279#endif
1280
1281 /*
1282 * Update w_curswant if w_set_curswant has been set.
1283 * Postponed until here to avoid computing w_virtcol too often.
1284 */
1285 update_curswant();
1286
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001287#ifdef FEAT_EVAL
1288 /*
1289 * May perform garbage collection when waiting for a character, but
1290 * only at the very toplevel. Otherwise we may be using a List or
1291 * Dict internally somewhere.
1292 * "may_garbage_collect" is reset in vgetc() which is invoked through
1293 * do_exmode() and normal_cmd().
1294 */
1295 may_garbage_collect = (!cmdwin && !noexmode);
1296#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001297 /*
1298 * If we're invoked as ex, do a round of ex commands.
1299 * Otherwise, get and execute a normal mode command.
1300 */
1301 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001302 {
1303 if (noexmode) /* End of ":global/path/visual" commands */
1304 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001305 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001306 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001307 else
1308 normal_cmd(&oa, TRUE);
1309 }
1310}
1311
1312
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001313#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001314/*
1315 * Exit, but leave behind swap files for modified buffers.
1316 */
1317 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001318getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001319{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001320# if defined(SIGHUP) && defined(SIG_IGN)
1321 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1322 * makes Vim exit and then handling SIGHUP causes various reentrance
1323 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001324 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001325# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001326
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001327 ml_close_notmod(); /* close all not-modified buffers */
1328 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1329 ml_close_all(FALSE); /* close all memfiles, without deleting */
1330 getout(exitval); /* exit Vim properly */
1331}
1332#endif
1333
1334
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001335/*
1336 * Exit properly.
1337 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001338 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001339getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001340{
1341#ifdef FEAT_AUTOCMD
1342 buf_T *buf;
1343 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001344 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001345#endif
1346
1347 exiting = TRUE;
1348
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001349 /* When running in Ex mode an error causes us to exit with a non-zero exit
1350 * code. POSIX requires this, although it's not 100% clear from the
1351 * standard. */
1352 if (exmode_active)
1353 exitval += ex_exitval;
1354
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001355 /* Position the cursor on the last screen line, below all the text */
1356#ifdef FEAT_GUI
1357 if (!gui.in_use)
1358#endif
1359 windgoto((int)Rows - 1, 0);
1360
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001361#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1362 /* Optionally print hashtable efficiency. */
1363 hash_debug_results();
1364#endif
1365
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001366#ifdef FEAT_GUI
1367 msg_didany = FALSE;
1368#endif
1369
1370#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001371 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001372 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001373 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1374# if defined FEAT_WINDOWS
1375 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001376 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001377 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001378 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001379 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001380 if (wp->w_buffer == NULL)
1381 /* Autocmd must have close the buffer already, skip. */
1382 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001383 buf = wp->w_buffer;
1384 if (buf->b_changedtick != -1)
1385 {
1386 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1387 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001388 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001389 /* start all over, autocommands may mess up the lists */
1390 next_tp = first_tabpage;
1391 break;
1392 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001393 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001394 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001395# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001396 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1397 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001398# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001399
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001400 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001401 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001402 if (buf->b_ml.ml_mfp != NULL)
1403 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001404 bufref_T bufref;
1405
1406 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001407 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001408 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001409 if (!bufref_valid(&bufref))
1410 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001411 break;
1412 }
1413 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1414 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001415#endif
1416
1417#ifdef FEAT_VIMINFO
1418 if (*p_viminfo != NUL)
1419 /* Write out the registers, history, marks etc, to the viminfo file */
1420 write_viminfo(NULL, FALSE);
1421#endif
1422
1423#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001424 if (get_vim_var_nr(VV_DYING) <= 1)
1425 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001426#endif
1427
Bram Moolenaar05159a02005-02-26 23:04:13 +00001428#ifdef FEAT_PROFILE
1429 profile_dump();
1430#endif
1431
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001432 if (did_emsg
1433#ifdef FEAT_GUI
1434 || (gui.in_use && msg_didany && p_verbose > 0)
1435#endif
1436 )
1437 {
1438 /* give the user a chance to read the (error) message */
1439 no_wait_return = FALSE;
1440 wait_return(FALSE);
1441 }
1442
1443#ifdef FEAT_AUTOCMD
1444 /* Position the cursor again, the autocommands may have moved it */
1445# ifdef FEAT_GUI
1446 if (!gui.in_use)
1447# endif
1448 windgoto((int)Rows - 1, 0);
1449#endif
1450
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001451#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001452 job_stop_on_exit();
1453#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001454#ifdef FEAT_LUA
1455 lua_end();
1456#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001457#ifdef FEAT_MZSCHEME
1458 mzscheme_end();
1459#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001460#ifdef FEAT_TCL
1461 tcl_end();
1462#endif
1463#ifdef FEAT_RUBY
1464 ruby_end();
1465#endif
1466#ifdef FEAT_PYTHON
1467 python_end();
1468#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001469#ifdef FEAT_PYTHON3
1470 python3_end();
1471#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001472#ifdef FEAT_PERL
1473 perl_end();
1474#endif
1475#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1476 iconv_end();
1477#endif
1478#ifdef FEAT_NETBEANS_INTG
1479 netbeans_end();
1480#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001481#ifdef FEAT_CSCOPE
1482 cs_end();
1483#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001484#ifdef FEAT_EVAL
1485 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001486 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001487#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001488#if defined(WIN32) && defined(FEAT_MBYTE)
1489 free_cmd_argsW();
1490#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001491
1492 mch_exit(exitval);
1493}
1494
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001495#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1496/*
1497 * Setup to use the current locale (for ctype() and many other things).
1498 */
1499 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001500init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001501{
1502 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001503
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001504# ifdef FEAT_GUI_GTK
1505 /* Tell Gtk not to change our locale settings. */
1506 gtk_disable_setlocale();
1507# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001508# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1509 /* Make sure strtod() uses a decimal point, not a comma. */
1510 setlocale(LC_NUMERIC, "C");
1511# endif
1512
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001513# ifdef WIN32
1514 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1515 * text while it's expecting text in the current locale. This call avoids
1516 * that. */
1517 setlocale(LC_CTYPE, "C");
1518# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001519
1520# ifdef FEAT_GETTEXT
1521 {
1522 int mustfree = FALSE;
1523 char_u *p;
1524
1525# ifdef DYNAMIC_GETTEXT
1526 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001527 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001528# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001529 /* expand_env() doesn't work yet, because g_chartab[] is not
1530 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001531 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1532 if (p != NULL && *p != NUL)
1533 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001534 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001535 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1536 }
1537 if (mustfree)
1538 vim_free(p);
1539 textdomain(VIMPACKAGE);
1540 }
1541# endif
1542}
1543#endif
1544
1545/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001546 * Get the name of the display, before gui_prepare() removes it from
1547 * argv[]. Used for the xterm-clipboard display.
1548 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001549 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001550 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001551 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001552early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001553{
Bram Moolenaar03005972008-11-20 13:12:36 +00001554#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1555 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001556 int argc = parmp->argc;
1557 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001558 int i;
1559
1560 for (i = 1; i < argc; i++)
1561 {
1562 if (STRCMP(argv[i], "--") == 0)
1563 break;
1564# ifdef FEAT_XCLIPBOARD
1565 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001566# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001567 || STRICMP(argv[i], "--display") == 0
1568# endif
1569 )
1570 {
1571 if (i == argc - 1)
1572 mainerr_arg_missing((char_u *)argv[i]);
1573 xterm_display = argv[++i];
1574 }
1575# endif
1576# ifdef FEAT_CLIENTSERVER
1577 else if (STRICMP(argv[i], "--servername") == 0)
1578 {
1579 if (i == argc - 1)
1580 mainerr_arg_missing((char_u *)argv[i]);
1581 parmp->serverName_arg = (char_u *)argv[++i];
1582 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001583 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001584 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001585 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001586 {
1587 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001588# ifdef FEAT_GUI
1589 if (strstr(argv[i], "-wait") != 0)
1590 /* don't fork() when starting the GUI to edit files ourself */
1591 gui.dofork = FALSE;
1592# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001593 }
1594# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001595
1596# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1597# ifdef FEAT_GUI_W32
1598 else if (STRICMP(argv[i], "--windowid") == 0)
1599# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001600 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001601# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001603 long_u id;
1604 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001605
1606 if (i == argc - 1)
1607 mainerr_arg_missing((char_u *)argv[i]);
1608 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001609 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001610 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001611 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001612 if (count != 1)
1613 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1614 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001615# ifdef FEAT_GUI_W32
1616 win_socket_id = id;
1617# else
1618 gtk_socket_id = id;
1619# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001620 i++;
1621 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001622# endif
1623# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001624 else if (STRICMP(argv[i], "--echo-wid") == 0)
1625 echo_wid_arg = TRUE;
1626# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001627# ifndef FEAT_NETBEANS_INTG
1628 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001629 {
1630 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1631 mch_exit(2);
1632 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001633# endif
1634
Bram Moolenaar58d98232005-07-23 22:25:46 +00001635 }
1636#endif
1637}
1638
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001639#ifndef NO_VIM_MAIN
1640/*
1641 * Get a (optional) count for a Vim argument.
1642 */
1643 static int
1644get_number_arg(
1645 char_u *p, /* pointer to argument */
1646 int *idx, /* index in argument, is incremented */
1647 int def) /* default value */
1648{
1649 if (vim_isdigit(p[*idx]))
1650 {
1651 def = atoi((char *)&(p[*idx]));
1652 while (vim_isdigit(p[*idx]))
1653 *idx = *idx + 1;
1654 }
1655 return def;
1656}
1657
1658/*
1659 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1660 * If the executable name starts with "r" we disable shell commands.
1661 * If the next character is "e" we run in Easy mode.
1662 * If the next character is "g" we run the GUI version.
1663 * If the next characters are "view" we start in readonly mode.
1664 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1665 * If the next characters are "ex" we start in Ex mode. If it's followed
1666 * by "im" use improved Ex mode.
1667 */
1668 static void
1669parse_command_name(mparm_T *parmp)
1670{
1671 char_u *initstr;
1672
1673 initstr = gettail((char_u *)parmp->argv[0]);
1674
1675#ifdef MACOS_X_UNIX
1676 /* An issue has been seen when launching Vim in such a way that
1677 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1678 * executable or a symbolic link of it. Until this issue is resolved
1679 * we prohibit the GUI from being used.
1680 */
1681 if (STRCMP(initstr, parmp->argv[0]) == 0)
1682 disallow_gui = TRUE;
1683
1684 /* TODO: On MacOS X default to gui if argv[0] ends in:
1685 * /Vim.app/Contents/MacOS/Vim */
1686#endif
1687
1688#ifdef FEAT_EVAL
1689 set_vim_var_string(VV_PROGNAME, initstr, -1);
1690 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
1691#endif
1692
1693 if (TOLOWER_ASC(initstr[0]) == 'r')
1694 {
1695 restricted = TRUE;
1696 ++initstr;
1697 }
1698
1699 /* Use evim mode for "evim" and "egvim", not for "editor". */
1700 if (TOLOWER_ASC(initstr[0]) == 'e'
1701 && (TOLOWER_ASC(initstr[1]) == 'v'
1702 || TOLOWER_ASC(initstr[1]) == 'g'))
1703 {
1704#ifdef FEAT_GUI
1705 gui.starting = TRUE;
1706#endif
1707 parmp->evim_mode = TRUE;
1708 ++initstr;
1709 }
1710
1711 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1712 if (TOLOWER_ASC(initstr[0]) == 'g')
1713 {
1714 main_start_gui();
1715#ifdef FEAT_GUI
1716 ++initstr;
1717#endif
1718 }
1719
1720 if (STRNICMP(initstr, "view", 4) == 0)
1721 {
1722 readonlymode = TRUE;
1723 curbuf->b_p_ro = TRUE;
1724 p_uc = 10000; /* don't update very often */
1725 initstr += 4;
1726 }
1727 else if (STRNICMP(initstr, "vim", 3) == 0)
1728 initstr += 3;
1729
1730 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1731 if (STRICMP(initstr, "diff") == 0)
1732 {
1733#ifdef FEAT_DIFF
1734 parmp->diff_mode = TRUE;
1735#else
1736 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1737 mch_errmsg("\n");
1738 mch_exit(2);
1739#endif
1740 }
1741
1742 if (STRNICMP(initstr, "ex", 2) == 0)
1743 {
1744 if (STRNICMP(initstr + 2, "im", 2) == 0)
1745 exmode_active = EXMODE_VIM;
1746 else
1747 exmode_active = EXMODE_NORMAL;
1748 change_compatible(TRUE); /* set 'compatible' */
1749 }
1750}
1751
Bram Moolenaar58d98232005-07-23 22:25:46 +00001752/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001753 * Scan the command line arguments.
1754 */
1755 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001756command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001757{
1758 int argc = parmp->argc;
1759 char **argv = parmp->argv;
1760 int argv_idx; /* index in argv[n][] */
1761 int had_minmin = FALSE; /* found "--" argument */
1762 int want_argument; /* option argument with argument */
1763 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001764 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001765 long n;
1766
1767 --argc;
1768 ++argv;
1769 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1770 while (argc > 0)
1771 {
1772 /*
1773 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1774 */
1775 if (argv[0][0] == '+' && !had_minmin)
1776 {
1777 if (parmp->n_commands >= MAX_ARG_CMDS)
1778 mainerr(ME_EXTRA_CMD, NULL);
1779 argv_idx = -1; /* skip to next argument */
1780 if (argv[0][1] == NUL)
1781 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1782 else
1783 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1784 }
1785
1786 /*
1787 * Optional argument.
1788 */
1789 else if (argv[0][0] == '-' && !had_minmin)
1790 {
1791 want_argument = FALSE;
1792 c = argv[0][argv_idx++];
1793#ifdef VMS
1794 /*
1795 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1796 * and "-/X" as "-X".
1797 */
1798 if (c == '/')
1799 {
1800 c = argv[0][argv_idx++];
1801 c = TOUPPER_ASC(c);
1802 }
1803 else
1804 c = TOLOWER_ASC(c);
1805#endif
1806 switch (c)
1807 {
1808 case NUL: /* "vim -" read from stdin */
1809 /* "ex -" silent mode */
1810 if (exmode_active)
1811 silent_mode = TRUE;
1812 else
1813 {
1814 if (parmp->edit_type != EDIT_NONE)
1815 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1816 parmp->edit_type = EDIT_STDIN;
1817 read_cmd_fd = 2; /* read from stderr instead of stdin */
1818 }
1819 argv_idx = -1; /* skip to next argument */
1820 break;
1821
1822 case '-': /* "--" don't take any more option arguments */
1823 /* "--help" give help message */
1824 /* "--version" give version message */
1825 /* "--literal" take files literally */
1826 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001827 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001828 /* "--noplugin[s]" skip plugins */
1829 /* "--cmd <cmd>" execute cmd before vimrc */
1830 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1831 usage();
1832 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1833 {
1834 Columns = 80; /* need to init Columns */
1835 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1836 list_version();
1837 msg_putchar('\n');
1838 msg_didout = FALSE;
1839 mch_exit(0);
1840 }
1841 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1842 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001843#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001844 parmp->literal = TRUE;
1845#endif
1846 }
1847 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1848 {
1849#ifdef FEAT_GUI
1850 gui.dofork = FALSE; /* don't fork() when starting GUI */
1851#endif
1852 }
1853 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1854 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001855 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1856 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001857 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1858 {
1859 want_argument = TRUE;
1860 argv_idx += 3;
1861 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001862 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1863 {
1864 want_argument = TRUE;
1865 argv_idx += 11;
1866 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001867#ifdef FEAT_CLIENTSERVER
1868 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1869 ; /* already processed -- no arg */
1870 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1871 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1872 {
1873 /* already processed -- snatch the following arg */
1874 if (argc > 1)
1875 {
1876 --argc;
1877 ++argv;
1878 }
1879 }
1880#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001881#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1882# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001883 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001884# else
1885 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1886# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001887 {
1888 /* already processed -- snatch the following arg */
1889 if (argc > 1)
1890 {
1891 --argc;
1892 ++argv;
1893 }
1894 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001895#endif
1896#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001897 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1898 {
1899 /* already processed, skip */
1900 }
1901#endif
1902 else
1903 {
1904 if (argv[0][argv_idx])
1905 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1906 had_minmin = TRUE;
1907 }
1908 if (!want_argument)
1909 argv_idx = -1; /* skip to next argument */
1910 break;
1911
1912 case 'A': /* "-A" start in Arabic mode */
1913#ifdef FEAT_ARABIC
1914 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1915#else
1916 mch_errmsg(_(e_noarabic));
1917 mch_exit(2);
1918#endif
1919 break;
1920
1921 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001922 /* Needs to be effective before expanding file names, because
1923 * for Win32 this makes us edit a shortcut file itself,
1924 * instead of the file it links to. */
1925 set_options_bin(curbuf->b_p_bin, 1, 0);
1926 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001927 break;
1928
1929 case 'C': /* "-C" Compatible */
1930 change_compatible(TRUE);
1931 break;
1932
1933 case 'e': /* "-e" Ex mode */
1934 exmode_active = EXMODE_NORMAL;
1935 break;
1936
1937 case 'E': /* "-E" Improved Ex mode */
1938 exmode_active = EXMODE_VIM;
1939 break;
1940
1941 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1942 window directly, not with newcli */
1943#ifdef FEAT_GUI
1944 gui.dofork = FALSE; /* don't fork() when starting GUI */
1945#endif
1946 break;
1947
1948 case 'g': /* "-g" start GUI */
1949 main_start_gui();
1950 break;
1951
1952 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1953#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001954 p_fkmap = TRUE;
1955 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001956#else
1957 mch_errmsg(_(e_nofarsi));
1958 mch_exit(2);
1959#endif
1960 break;
1961
1962 case 'h': /* "-h" give help message */
1963#ifdef FEAT_GUI_GNOME
1964 /* Tell usage() to exit for "gvim". */
1965 gui.starting = FALSE;
1966#endif
1967 usage();
1968 break;
1969
1970 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1971#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001972 p_hkmap = TRUE;
1973 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001974#else
1975 mch_errmsg(_(e_nohebrew));
1976 mch_exit(2);
1977#endif
1978 break;
1979
1980 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1981#ifdef FEAT_LISP
1982 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1983 p_sm = TRUE;
1984#endif
1985 break;
1986
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001987 case 'M': /* "-M" no changes or writing of files */
1988 reset_modifiable();
1989 /* FALLTHROUGH */
1990
1991 case 'm': /* "-m" no writing of files */
1992 p_write = FALSE;
1993 break;
1994
1995 case 'y': /* "-y" easy mode */
1996#ifdef FEAT_GUI
1997 gui.starting = TRUE; /* start GUI a bit later */
1998#endif
1999 parmp->evim_mode = TRUE;
2000 break;
2001
2002 case 'N': /* "-N" Nocompatible */
2003 change_compatible(FALSE);
2004 break;
2005
2006 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002007#ifdef FEAT_NETBEANS_INTG
2008 /* checking for "-nb", netbeans parameters */
2009 if (argv[0][argv_idx] == 'b')
2010 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002011 netbeansArg = argv[0];
2012 argv_idx = -1; /* skip to next argument */
2013 }
2014 else
2015#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002016 parmp->no_swap_file = TRUE;
2017 break;
2018
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002019 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002020#ifdef TARGET_API_MAC_OSX
2021 /* For some reason on MacOS X, an argument like:
2022 -psn_0_10223617 is passed in when invoke from Finder
2023 or with the 'open' command */
2024 if (argv[0][argv_idx] == 's')
2025 {
2026 argv_idx = -1; /* bypass full -psn */
2027 main_start_gui();
2028 break;
2029 }
2030#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002031#ifdef FEAT_WINDOWS
2032 /* default is 0: open window for each file */
2033 parmp->window_count = get_number_arg((char_u *)argv[0],
2034 &argv_idx, 0);
2035 parmp->window_layout = WIN_TABS;
2036#endif
2037 break;
2038
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002039 case 'o': /* "-o[N]" open N horizontal split windows */
2040#ifdef FEAT_WINDOWS
2041 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002042 parmp->window_count = get_number_arg((char_u *)argv[0],
2043 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002044 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002045#endif
2046 break;
2047
2048 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002049#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002050 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002051 parmp->window_count = get_number_arg((char_u *)argv[0],
2052 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002053 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002054#endif
2055 break;
2056
2057#ifdef FEAT_QUICKFIX
2058 case 'q': /* "-q" QuickFix mode */
2059 if (parmp->edit_type != EDIT_NONE)
2060 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2061 parmp->edit_type = EDIT_QF;
2062 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2063 {
2064 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2065 argv_idx = -1;
2066 }
2067 else if (argc > 1) /* "-q {errorfile}" */
2068 want_argument = TRUE;
2069 break;
2070#endif
2071
2072 case 'R': /* "-R" readonly mode */
2073 readonlymode = TRUE;
2074 curbuf->b_p_ro = TRUE;
2075 p_uc = 10000; /* don't update very often */
2076 break;
2077
2078 case 'r': /* "-r" recovery mode */
2079 case 'L': /* "-L" recovery mode */
2080 recoverymode = 1;
2081 break;
2082
2083 case 's':
2084 if (exmode_active) /* "-s" silent (batch) mode */
2085 silent_mode = TRUE;
2086 else /* "-s {scriptin}" read from script file */
2087 want_argument = TRUE;
2088 break;
2089
2090 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2091 if (parmp->edit_type != EDIT_NONE)
2092 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2093 parmp->edit_type = EDIT_TAG;
2094 if (argv[0][argv_idx]) /* "-t{tag}" */
2095 {
2096 parmp->tagname = (char_u *)argv[0] + argv_idx;
2097 argv_idx = -1;
2098 }
2099 else /* "-t {tag}" */
2100 want_argument = TRUE;
2101 break;
2102
2103#ifdef FEAT_EVAL
2104 case 'D': /* "-D" Debugging */
2105 parmp->use_debug_break_level = 9999;
2106 break;
2107#endif
2108#ifdef FEAT_DIFF
2109 case 'd': /* "-d" 'diff' */
2110# ifdef AMIGA
2111 /* check for "-dev {device}" */
2112 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2113 want_argument = TRUE;
2114 else
2115# endif
2116 parmp->diff_mode = TRUE;
2117 break;
2118#endif
2119 case 'V': /* "-V{N}" Verbose level */
2120 /* default is 10: a little bit verbose */
2121 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2122 if (argv[0][argv_idx] != NUL)
2123 {
2124 set_option_value((char_u *)"verbosefile", 0L,
2125 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002126 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002127 }
2128 break;
2129
2130 case 'v': /* "-v" Vi-mode (as if called "vi") */
2131 exmode_active = 0;
2132#ifdef FEAT_GUI
2133 gui.starting = FALSE; /* don't start GUI */
2134#endif
2135 break;
2136
2137 case 'w': /* "-w{number}" set window height */
2138 /* "-w {scriptout}" write to script */
2139 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2140 {
2141 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2142 set_option_value((char_u *)"window", n, NULL, 0);
2143 break;
2144 }
2145 want_argument = TRUE;
2146 break;
2147
2148#ifdef FEAT_CRYPT
2149 case 'x': /* "-x" encrypted reading/writing of files */
2150 parmp->ask_for_key = TRUE;
2151 break;
2152#endif
2153
2154 case 'X': /* "-X" don't connect to X server */
2155#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2156 x_no_connect = TRUE;
2157#endif
2158 break;
2159
2160 case 'Z': /* "-Z" restricted mode */
2161 restricted = TRUE;
2162 break;
2163
2164 case 'c': /* "-c{command}" or "-c {command}" execute
2165 command */
2166 if (argv[0][argv_idx] != NUL)
2167 {
2168 if (parmp->n_commands >= MAX_ARG_CMDS)
2169 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002170 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2171 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002172 argv_idx = -1;
2173 break;
2174 }
2175 /*FALLTHROUGH*/
2176 case 'S': /* "-S {file}" execute Vim script */
2177 case 'i': /* "-i {viminfo}" use for viminfo */
2178#ifndef FEAT_DIFF
2179 case 'd': /* "-d {device}" device (for Amiga) */
2180#endif
2181 case 'T': /* "-T {terminal}" terminal name */
2182 case 'u': /* "-u {vimrc}" vim inits file */
2183 case 'U': /* "-U {gvimrc}" gvim inits file */
2184 case 'W': /* "-W {scriptout}" overwrite */
2185#ifdef FEAT_GUI_W32
2186 case 'P': /* "-P {parent title}" MDI parent */
2187#endif
2188 want_argument = TRUE;
2189 break;
2190
2191 default:
2192 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2193 }
2194
2195 /*
2196 * Handle option arguments with argument.
2197 */
2198 if (want_argument)
2199 {
2200 /*
2201 * Check for garbage immediately after the option letter.
2202 */
2203 if (argv[0][argv_idx] != NUL)
2204 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2205
2206 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002207 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002208 mainerr_arg_missing((char_u *)argv[0]);
2209 ++argv;
2210 argv_idx = -1;
2211
2212 switch (c)
2213 {
2214 case 'c': /* "-c {command}" execute command */
2215 case 'S': /* "-S {file}" execute Vim script */
2216 if (parmp->n_commands >= MAX_ARG_CMDS)
2217 mainerr(ME_EXTRA_CMD, NULL);
2218 if (c == 'S')
2219 {
2220 char *a;
2221
2222 if (argc < 1)
2223 /* "-S" without argument: use default session file
2224 * name. */
2225 a = SESSION_FILE;
2226 else if (argv[0][0] == '-')
2227 {
2228 /* "-S" followed by another option: use default
2229 * session file name. */
2230 a = SESSION_FILE;
2231 ++argc;
2232 --argv;
2233 }
2234 else
2235 a = argv[0];
2236 p = alloc((unsigned)(STRLEN(a) + 4));
2237 if (p == NULL)
2238 mch_exit(2);
2239 sprintf((char *)p, "so %s", a);
2240 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2241 parmp->commands[parmp->n_commands++] = p;
2242 }
2243 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002244 parmp->commands[parmp->n_commands++] =
2245 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002246 break;
2247
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002248 case '-':
2249 if (argv[-1][2] == 'c')
2250 {
2251 /* "--cmd {command}" execute command */
2252 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2253 mainerr(ME_EXTRA_CMD, NULL);
2254 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002255 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002256 }
2257 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002258 break;
2259
2260 /* case 'd': -d {device} is handled in mch_check_win() for the
2261 * Amiga */
2262
2263#ifdef FEAT_QUICKFIX
2264 case 'q': /* "-q {errorfile}" QuickFix mode */
2265 parmp->use_ef = (char_u *)argv[0];
2266 break;
2267#endif
2268
2269 case 'i': /* "-i {viminfo}" use for viminfo */
2270 use_viminfo = (char_u *)argv[0];
2271 break;
2272
2273 case 's': /* "-s {scriptin}" read from script file */
2274 if (scriptin[0] != NULL)
2275 {
2276scripterror:
2277 mch_errmsg(_("Attempt to open script file again: \""));
2278 mch_errmsg(argv[-1]);
2279 mch_errmsg(" ");
2280 mch_errmsg(argv[0]);
2281 mch_errmsg("\"\n");
2282 mch_exit(2);
2283 }
2284 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2285 {
2286 mch_errmsg(_("Cannot open for reading: \""));
2287 mch_errmsg(argv[0]);
2288 mch_errmsg("\"\n");
2289 mch_exit(2);
2290 }
2291 if (save_typebuf() == FAIL)
2292 mch_exit(2); /* out of memory */
2293 break;
2294
2295 case 't': /* "-t {tag}" */
2296 parmp->tagname = (char_u *)argv[0];
2297 break;
2298
2299 case 'T': /* "-T {terminal}" terminal name */
2300 /*
2301 * The -T term argument is always available and when
2302 * HAVE_TERMLIB is supported it overrides the environment
2303 * variable TERM.
2304 */
2305#ifdef FEAT_GUI
2306 if (term_is_gui((char_u *)argv[0]))
2307 gui.starting = TRUE; /* start GUI a bit later */
2308 else
2309#endif
2310 parmp->term = (char_u *)argv[0];
2311 break;
2312
2313 case 'u': /* "-u {vimrc}" vim inits file */
2314 parmp->use_vimrc = (char_u *)argv[0];
2315 break;
2316
2317 case 'U': /* "-U {gvimrc}" gvim inits file */
2318#ifdef FEAT_GUI
2319 use_gvimrc = (char_u *)argv[0];
2320#endif
2321 break;
2322
2323 case 'w': /* "-w {nr}" 'window' value */
2324 /* "-w {scriptout}" append to script file */
2325 if (vim_isdigit(*((char_u *)argv[0])))
2326 {
2327 argv_idx = 0;
2328 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2329 set_option_value((char_u *)"window", n, NULL, 0);
2330 argv_idx = -1;
2331 break;
2332 }
2333 /*FALLTHROUGH*/
2334 case 'W': /* "-W {scriptout}" overwrite script file */
2335 if (scriptout != NULL)
2336 goto scripterror;
2337 if ((scriptout = mch_fopen(argv[0],
2338 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2339 {
2340 mch_errmsg(_("Cannot open for script output: \""));
2341 mch_errmsg(argv[0]);
2342 mch_errmsg("\"\n");
2343 mch_exit(2);
2344 }
2345 break;
2346
2347#ifdef FEAT_GUI_W32
2348 case 'P': /* "-P {parent title}" MDI parent */
2349 gui_mch_set_parent(argv[0]);
2350 break;
2351#endif
2352 }
2353 }
2354 }
2355
2356 /*
2357 * File name argument.
2358 */
2359 else
2360 {
2361 argv_idx = -1; /* skip to next argument */
2362
2363 /* Check for only one type of editing. */
2364 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2365 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2366 parmp->edit_type = EDIT_FILE;
2367
2368#ifdef MSWIN
2369 /* Remember if the argument was a full path before changing
2370 * slashes to backslashes. */
2371 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2372 parmp->full_path = TRUE;
2373#endif
2374
2375 /* Add the file to the global argument list. */
2376 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2377 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2378 mch_exit(2);
2379#ifdef FEAT_DIFF
2380 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2381 && !mch_isdir(alist_name(&GARGLIST[0])))
2382 {
2383 char_u *r;
2384
2385 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2386 if (r != NULL)
2387 {
2388 vim_free(p);
2389 p = r;
2390 }
2391 }
2392#endif
2393#if defined(__CYGWIN32__) && !defined(WIN32)
2394 /*
2395 * If vim is invoked by non-Cygwin tools, convert away any
2396 * DOS paths, so things like .swp files are created correctly.
2397 * Look for evidence of non-Cygwin paths before we bother.
2398 * This is only for when using the Unix files.
2399 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002400 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002401 {
2402 char posix_path[PATH_MAX];
2403
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002404# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2405 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2406# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002407 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002408# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002409 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002410 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002411 if (p == NULL)
2412 mch_exit(2);
2413 }
2414#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002415
2416#ifdef USE_FNAME_CASE
2417 /* Make the case of the file name match the actual file. */
2418 fname_case(p, 0);
2419#endif
2420
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002421 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002422#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002423 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002424#else
2425 2 /* add buffer number now and use curbuf */
2426#endif
2427 );
2428
2429#if defined(FEAT_MBYTE) && defined(WIN32)
2430 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002431 /* Remember this argument has been added to the argument list.
2432 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002433 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002434# ifdef FEAT_DIFF
2435 parmp->diff_mode
2436# else
2437 FALSE
2438# endif
2439 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 }
2441#endif
2442 }
2443
2444 /*
2445 * If there are no more letters after the current "-", go to next
2446 * argument. argv_idx is set to -1 when the current argument is to be
2447 * skipped.
2448 */
2449 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2450 {
2451 --argc;
2452 ++argv;
2453 argv_idx = 1;
2454 }
2455 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002456
2457#ifdef FEAT_EVAL
2458 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2459 * one. */
2460 if (parmp->n_commands > 0)
2461 {
2462 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2463 if (p != NULL)
2464 {
2465 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2466 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2467 vim_free(p);
2468 }
2469 }
2470#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002471}
2472
2473/*
2474 * Print a warning if stdout is not a terminal.
2475 * When starting in Ex mode and commands come from a file, set Silent mode.
2476 */
2477 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002478check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002479{
2480 int input_isatty; /* is active input a terminal? */
2481
2482 input_isatty = mch_input_isatty();
2483 if (exmode_active)
2484 {
2485 if (!input_isatty)
2486 silent_mode = TRUE;
2487 }
2488 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2489#ifdef FEAT_GUI
2490 /* don't want the delay when started from the desktop */
2491 && !gui.starting
2492#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002493 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002494 {
2495#ifdef NBDEBUG
2496 /*
2497 * This shouldn't be necessary. But if I run netbeans with the log
2498 * output coming to the console and XOpenDisplay fails, I get vim
2499 * trying to start with input/output to my console tty. This fills my
2500 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002501 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002502 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002503 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002504 {
2505 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2506 exit(1);
2507 }
2508#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002509#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2510 if (is_cygpty_used())
2511 {
2512 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2513 exit(1);
2514 }
2515#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002516 if (!parmp->stdout_isatty)
2517 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2518 if (!input_isatty)
2519 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2520 out_flush();
2521 if (scriptin[0] == NULL)
2522 ui_delay(2000L, TRUE);
2523 TIME_MSG("Warning delay");
2524 }
2525}
2526
2527/*
2528 * Read text from stdin.
2529 */
2530 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002531read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002532{
2533 int i;
2534
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002535#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002536 /* When getting the ATTENTION prompt here, use a dialog */
2537 swap_exists_action = SEA_DIALOG;
2538#endif
2539 no_wait_return = TRUE;
2540 i = msg_didany;
2541 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002542 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002543 no_wait_return = FALSE;
2544 msg_didany = i;
2545 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002546#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002547 check_swap_exists_action();
2548#endif
2549#if !(defined(AMIGA) || defined(MACOS))
2550 /*
2551 * Close stdin and dup it from stderr. Required for GPM to work
2552 * properly, and for running external commands.
2553 * Is there any other system that cannot do this?
2554 */
2555 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002556 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002557#endif
2558}
2559
2560/*
2561 * Create the requested number of windows and edit buffers in them.
2562 * Also does recovery if "recoverymode" set.
2563 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002564 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002565create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002566{
2567#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002568 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002569 int done = 0;
2570
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002571 /*
2572 * Create the number of windows that was requested.
2573 */
2574 if (parmp->window_count == -1) /* was not set */
2575 parmp->window_count = 1;
2576 if (parmp->window_count == 0)
2577 parmp->window_count = GARGCOUNT;
2578 if (parmp->window_count > 1)
2579 {
2580 /* Don't change the windows if there was a command in .vimrc that
2581 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002582 if (parmp->window_layout == 0)
2583 parmp->window_layout = WIN_HOR;
2584 if (parmp->window_layout == WIN_TABS)
2585 {
2586 parmp->window_count = make_tabpages(parmp->window_count);
2587 TIME_MSG("making tab pages");
2588 }
2589 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590 {
2591 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002592 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002593 TIME_MSG("making windows");
2594 }
2595 else
2596 parmp->window_count = win_count();
2597 }
2598 else
2599 parmp->window_count = 1;
2600#endif
2601
2602 if (recoverymode) /* do recover */
2603 {
2604 msg_scroll = TRUE; /* scroll message up */
2605 ml_recover();
2606 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2607 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002608 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002609 }
2610 else
2611 {
2612 /*
2613 * Open a buffer for windows that don't have one yet.
2614 * Commands in the .vimrc might have loaded a file or split the window.
2615 * Watch out for autocommands that delete a window.
2616 */
2617#ifdef FEAT_AUTOCMD
2618 /*
2619 * Don't execute Win/Buf Enter/Leave autocommands here
2620 */
2621 ++autocmd_no_enter;
2622 ++autocmd_no_leave;
2623#endif
2624#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002625 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002626 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002627 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002628 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002629 {
2630 if (parmp->window_layout == WIN_TABS)
2631 goto_tabpage(1);
2632 else
2633 curwin = firstwin;
2634 }
2635 else if (parmp->window_layout == WIN_TABS)
2636 {
2637 if (curtab->tp_next == NULL)
2638 break;
2639 goto_tabpage(0);
2640 }
2641 else
2642 {
2643 if (curwin->w_next == NULL)
2644 break;
2645 curwin = curwin->w_next;
2646 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002647 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002648#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002649 curbuf = curwin->w_buffer;
2650 if (curbuf->b_ml.ml_mfp == NULL)
2651 {
2652#ifdef FEAT_FOLDING
2653 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2654 if (p_fdls >= 0)
2655 curwin->w_p_fdl = p_fdls;
2656#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002657#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002658 /* When getting the ATTENTION prompt here, use a dialog */
2659 swap_exists_action = SEA_DIALOG;
2660#endif
2661 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002662
2663 /* create memfile, read file */
2664 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002665
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002666#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002667 if (swap_exists_action == SEA_QUIT)
2668 {
2669 if (got_int || only_one_window())
2670 {
2671 /* abort selected or quit and only one window */
2672 did_emsg = FALSE; /* avoid hit-enter prompt */
2673 getout(1);
2674 }
2675 /* We can't close the window, it would disturb what
2676 * happens next. Clear the file name and set the arg
2677 * index to -1 to delete it later. */
2678 setfname(curbuf, NULL, NULL, FALSE);
2679 curwin->w_arg_idx = -1;
2680 swap_exists_action = SEA_NONE;
2681 }
2682 else
2683 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002684#endif
2685#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002686 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002687#endif
2688 }
2689#ifdef FEAT_WINDOWS
2690 ui_breakcheck();
2691 if (got_int)
2692 {
2693 (void)vgetc(); /* only break the file loading, not the rest */
2694 break;
2695 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002696 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002697#endif
2698#ifdef FEAT_WINDOWS
2699 if (parmp->window_layout == WIN_TABS)
2700 goto_tabpage(1);
2701 else
2702 curwin = firstwin;
2703 curbuf = curwin->w_buffer;
2704#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002705#ifdef FEAT_AUTOCMD
2706 --autocmd_no_enter;
2707 --autocmd_no_leave;
2708#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709 }
2710}
2711
2712#ifdef FEAT_WINDOWS
2713 /*
2714 * If opened more than one window, start editing files in the other
2715 * windows. make_windows() has already opened the windows.
2716 */
2717 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002718edit_buffers(
2719 mparm_T *parmp,
2720 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002721{
2722 int arg_idx; /* index in argument list */
2723 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002724 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002725 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726
2727# ifdef FEAT_AUTOCMD
2728 /*
2729 * Don't execute Win/Buf Enter/Leave autocommands here
2730 */
2731 ++autocmd_no_enter;
2732 ++autocmd_no_leave;
2733# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002734
2735 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2736 if (curwin->w_arg_idx == -1)
2737 {
2738 win_close(curwin, TRUE);
2739 advance = FALSE;
2740 }
2741
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002742 arg_idx = 1;
2743 for (i = 1; i < parmp->window_count; ++i)
2744 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002745 if (cwd != NULL)
2746 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002747 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2748 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002749 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002750 ++arg_idx;
2751 win_close(curwin, TRUE);
2752 advance = FALSE;
2753 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002754 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002755
Bram Moolenaar84212822006-11-07 21:59:47 +00002756 if (advance)
2757 {
2758 if (parmp->window_layout == WIN_TABS)
2759 {
2760 if (curtab->tp_next == NULL) /* just checking */
2761 break;
2762 goto_tabpage(0);
2763 }
2764 else
2765 {
2766 if (curwin->w_next == NULL) /* just checking */
2767 break;
2768 win_enter(curwin->w_next, FALSE);
2769 }
2770 }
2771 advance = TRUE;
2772
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002774 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002775 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2776 {
2777 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002778 /* Edit file from arg list, if there is one. When "Quit" selected
2779 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002780# ifdef HAS_SWAP_EXISTS_ACTION
2781 swap_exists_did_quit = FALSE;
2782# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783 (void)do_ecmd(0, arg_idx < GARGCOUNT
2784 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002785 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002786# ifdef HAS_SWAP_EXISTS_ACTION
2787 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002788 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002789 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002790 if (got_int || only_one_window())
2791 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002792 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002793 did_emsg = FALSE; /* avoid hit-enter prompt */
2794 getout(1);
2795 }
2796 win_close(curwin, TRUE);
2797 advance = FALSE;
2798 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002799# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002800 if (arg_idx == GARGCOUNT - 1)
2801 arg_had_last = TRUE;
2802 ++arg_idx;
2803 }
2804 ui_breakcheck();
2805 if (got_int)
2806 {
2807 (void)vgetc(); /* only break the file loading, not the rest */
2808 break;
2809 }
2810 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002811
2812 if (parmp->window_layout == WIN_TABS)
2813 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002814# ifdef FEAT_AUTOCMD
2815 --autocmd_no_enter;
2816# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002817
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002818 /* make the first window the current window */
2819 win = firstwin;
2820#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2821 /* Avoid making a preview window the current window. */
2822 while (win->w_p_pvw)
2823 {
2824 win = win->w_next;
2825 if (win == NULL)
2826 {
2827 win = firstwin;
2828 break;
2829 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002830 }
2831#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002832 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002833
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002834# ifdef FEAT_AUTOCMD
2835 --autocmd_no_leave;
2836# endif
2837 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002838 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002839 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2840}
2841#endif /* FEAT_WINDOWS */
2842
2843/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002844 * Execute the commands from --cmd arguments "cmds[cnt]".
2845 */
2846 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002847exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002848{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002849 char_u **cmds = parmp->pre_commands;
2850 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002851 int i;
2852
2853 if (cnt > 0)
2854 {
2855 curwin->w_cursor.lnum = 0; /* just in case.. */
2856 sourcing_name = (char_u *)_("pre-vimrc command line");
2857# ifdef FEAT_EVAL
2858 current_SID = SID_CMDARG;
2859# endif
2860 for (i = 0; i < cnt; ++i)
2861 do_cmdline_cmd(cmds[i]);
2862 sourcing_name = NULL;
2863# ifdef FEAT_EVAL
2864 current_SID = 0;
2865# endif
2866 TIME_MSG("--cmd commands");
2867 }
2868}
2869
2870/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871 * Execute "+", "-c" and "-S" arguments.
2872 */
2873 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002874exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875{
2876 int i;
2877
2878 /*
2879 * We start commands on line 0, make "vim +/pat file" match a
2880 * pattern on line 1. But don't move the cursor when an autocommand
2881 * with g`" was used.
2882 */
2883 msg_scroll = TRUE;
2884 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2885 curwin->w_cursor.lnum = 0;
2886 sourcing_name = (char_u *)"command line";
2887#ifdef FEAT_EVAL
2888 current_SID = SID_CARG;
2889#endif
2890 for (i = 0; i < parmp->n_commands; ++i)
2891 {
2892 do_cmdline_cmd(parmp->commands[i]);
2893 if (parmp->cmds_tofree[i])
2894 vim_free(parmp->commands[i]);
2895 }
2896 sourcing_name = NULL;
2897#ifdef FEAT_EVAL
2898 current_SID = 0;
2899#endif
2900 if (curwin->w_cursor.lnum == 0)
2901 curwin->w_cursor.lnum = 1;
2902
2903 if (!exmode_active)
2904 msg_scroll = FALSE;
2905
2906#ifdef FEAT_QUICKFIX
2907 /* When started with "-q errorfile" jump to first error again. */
2908 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002909 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002910#endif
2911 TIME_MSG("executing command arguments");
2912}
2913
2914/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002915 * Source startup scripts.
2916 */
2917 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002918source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002919{
2920 int i;
2921
2922 /*
2923 * For "evim" source evim.vim first of all, so that the user can overrule
2924 * any things he doesn't like.
2925 */
2926 if (parmp->evim_mode)
2927 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002928 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002929 TIME_MSG("source evim file");
2930 }
2931
2932 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002933 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002934 * nothing else.
2935 */
2936 if (parmp->use_vimrc != NULL)
2937 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002938 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2939 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002940 {
2941#ifdef FEAT_GUI
2942 if (use_gvimrc == NULL) /* don't load gvimrc either */
2943 use_gvimrc = parmp->use_vimrc;
2944#endif
2945 if (parmp->use_vimrc[2] == 'N')
2946 p_lpl = FALSE; /* don't load plugins either */
2947 }
2948 else
2949 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002950 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002951 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2952 }
2953 }
2954 else if (!silent_mode)
2955 {
2956#ifdef AMIGA
2957 struct Process *proc = (struct Process *)FindTask(0L);
2958 APTR save_winptr = proc->pr_WindowPtr;
2959
2960 /* Avoid a requester here for a volume that doesn't exist. */
2961 proc->pr_WindowPtr = (APTR)-1L;
2962#endif
2963
2964 /*
2965 * Get system wide defaults, if the file name is defined.
2966 */
2967#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002968 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002969#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002970#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002971 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002972#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002973
2974 /*
2975 * Try to read initialization commands from the following places:
2976 * - environment variable VIMINIT
2977 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2978 * - second user vimrc file ($VIM/.vimrc for Dos)
2979 * - environment variable EXINIT
2980 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2981 * - second user exrc file ($VIM/.exrc for Dos)
2982 * The first that exists is used, the rest is ignored.
2983 */
2984 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2985 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002986 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002987#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002988 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2989 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002990#endif
2991#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002992 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2993 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002994#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02002995#ifdef USR_VIMRC_FILE4
2996 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
2997 DOSO_VIMRC) == FAIL
2998#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002999 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003000 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003002 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003#endif
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003004 )
3005 {
3006 /* When no .vimrc file was found: source defaults.vim. */
3007 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008 }
3009 }
3010
3011 /*
3012 * Read initialization commands from ".vimrc" or ".exrc" in current
3013 * directory. This is only done if the 'exrc' option is set.
3014 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003015 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003016 * option has been reset in environment of global ".exrc" or ".vimrc".
3017 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3018 * SYS_VIMRC_FILE.
3019 */
3020 if (p_exrc)
3021 {
3022#if defined(UNIX) || defined(VMS)
3023 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3024 if (!file_owned(VIMRC_FILE))
3025#endif
3026 secure = p_secure;
3027
3028 i = FAIL;
3029 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3030 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3031#ifdef USR_VIMRC_FILE2
3032 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3033 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3034#endif
3035#ifdef USR_VIMRC_FILE3
3036 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3037 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3038#endif
3039#ifdef SYS_VIMRC_FILE
3040 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3041 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3042#endif
3043 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003044 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003045
3046 if (i == FAIL)
3047 {
3048#if defined(UNIX) || defined(VMS)
3049 /* if ".exrc" is not owned by user set 'secure' mode */
3050 if (!file_owned(EXRC_FILE))
3051 secure = p_secure;
3052 else
3053 secure = 0;
3054#endif
3055 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3056 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3057#ifdef USR_EXRC_FILE2
3058 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3059 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3060#endif
3061 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003062 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003063 }
3064 }
3065 if (secure == 2)
3066 need_wait_return = TRUE;
3067 secure = 0;
3068#ifdef AMIGA
3069 proc->pr_WindowPtr = save_winptr;
3070#endif
3071 }
3072 TIME_MSG("sourcing vimrc file(s)");
3073}
3074
3075/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003076 * Setup to start using the GUI. Exit with an error when not available.
3077 */
3078 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003079main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003080{
3081#ifdef FEAT_GUI
3082 gui.starting = TRUE; /* start GUI a bit later */
3083#else
3084 mch_errmsg(_(e_nogvim));
3085 mch_errmsg("\n");
3086 mch_exit(2);
3087#endif
3088}
3089
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003090#endif /* NO_VIM_MAIN */
3091
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003092/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003093 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003094 * Returns FAIL if the environment variable was not executed, OK otherwise.
3095 */
3096 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003097process_env(
3098 char_u *env,
3099 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003100{
3101 char_u *initstr;
3102 char_u *save_sourcing_name;
3103 linenr_T save_sourcing_lnum;
3104#ifdef FEAT_EVAL
3105 scid_T save_sid;
3106#endif
3107
3108 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3109 {
3110 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003111 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003112 save_sourcing_name = sourcing_name;
3113 save_sourcing_lnum = sourcing_lnum;
3114 sourcing_name = env;
3115 sourcing_lnum = 0;
3116#ifdef FEAT_EVAL
3117 save_sid = current_SID;
3118 current_SID = SID_ENV;
3119#endif
3120 do_cmdline_cmd(initstr);
3121 sourcing_name = save_sourcing_name;
3122 sourcing_lnum = save_sourcing_lnum;
3123#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003124 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003125#endif
3126 return OK;
3127 }
3128 return FAIL;
3129}
3130
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003131#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003132/*
3133 * Return TRUE if we are certain the user owns the file "fname".
3134 * Used for ".vimrc" and ".exrc".
3135 * Use both stat() and lstat() for extra security.
3136 */
3137 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003138file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003139{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003140 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003141# ifdef UNIX
3142 uid_t uid = getuid();
3143# else /* VMS */
3144 uid_t uid = ((getgid() << 16) | getuid());
3145# endif
3146
3147 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3148# ifdef HAVE_LSTAT
3149 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3150# endif
3151 );
3152}
3153#endif
3154
3155/*
3156 * Give an error message main_errors["n"] and exit.
3157 */
3158 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003159mainerr(
3160 int n, /* one of the ME_ defines */
3161 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003162{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003163#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003164 reset_signals(); /* kill us with CTRL-C here, if you like */
3165#endif
3166
3167 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003168 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003169 mch_errmsg(_(main_errors[n]));
3170 if (str != NULL)
3171 {
3172 mch_errmsg(": \"");
3173 mch_errmsg((char *)str);
3174 mch_errmsg("\"");
3175 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003176 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003177
3178 mch_exit(1);
3179}
3180
3181 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003182mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003183{
3184 mainerr(ME_ARG_MISSING, str);
3185}
3186
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003187#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003188/*
3189 * print a message with three spaces prepended and '\n' appended.
3190 */
3191 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003192main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003193{
3194 mch_msg(" ");
3195 mch_msg(s);
3196 mch_msg("\n");
3197}
3198
3199/*
3200 * Print messages for "vim -h" or "vim --help" and exit.
3201 */
3202 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003203usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003204{
3205 int i;
3206 static char *(use[]) =
3207 {
3208 N_("[file ..] edit specified file(s)"),
3209 N_("- read text from stdin"),
3210 N_("-t tag edit file where tag is defined"),
3211#ifdef FEAT_QUICKFIX
3212 N_("-q [errorfile] edit file with first error")
3213#endif
3214 };
3215
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003216#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003217 reset_signals(); /* kill us with CTRL-C here, if you like */
3218#endif
3219
3220 mch_msg(longVersion);
3221 mch_msg(_("\n\nusage:"));
3222 for (i = 0; ; ++i)
3223 {
3224 mch_msg(_(" vim [arguments] "));
3225 mch_msg(_(use[i]));
3226 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3227 break;
3228 mch_msg(_("\n or:"));
3229 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003230#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003231 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003232#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003233
3234 mch_msg(_("\n\nArguments:\n"));
3235 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003236#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003237 main_msg(_("--literal\t\tDon't expand wildcards"));
3238#endif
3239#ifdef FEAT_OLE
3240 main_msg(_("-register\t\tRegister this gvim for OLE"));
3241 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3242#endif
3243#ifdef FEAT_GUI
3244 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3245 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3246#endif
3247 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3248 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003249 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003250 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3251#ifdef FEAT_DIFF
3252 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3253#endif
3254 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3255 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3256 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3257 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3258 main_msg(_("-M\t\t\tModifications in text not allowed"));
3259 main_msg(_("-b\t\t\tBinary mode"));
3260#ifdef FEAT_LISP
3261 main_msg(_("-l\t\t\tLisp mode"));
3262#endif
3263 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3264 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003265 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3266#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003267 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003268#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003269 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3270 main_msg(_("-r\t\t\tList swap files and exit"));
3271 main_msg(_("-r (with file name)\tRecover crashed session"));
3272 main_msg(_("-L\t\t\tSame as -r"));
3273#ifdef AMIGA
3274 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3275 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3276#endif
3277#ifdef FEAT_ARABIC
3278 main_msg(_("-A\t\t\tstart in Arabic mode"));
3279#endif
3280#ifdef FEAT_RIGHTLEFT
3281 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3282#endif
3283#ifdef FEAT_FKMAP
3284 main_msg(_("-F\t\t\tStart in Farsi mode"));
3285#endif
3286 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003287 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003288 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3289#ifdef FEAT_GUI
3290 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3291#endif
3292 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003293#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003294 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003295 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3296 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003297#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 main_msg(_("+\t\t\tStart at end of file"));
3299 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003300 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3302 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3303 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3304 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3305 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3306#ifdef FEAT_CRYPT
3307 main_msg(_("-x\t\t\tEdit encrypted files"));
3308#endif
3309#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3310# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3311 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3312# endif
3313 main_msg(_("-X\t\t\tDo not connect to X server"));
3314#endif
3315#ifdef FEAT_CLIENTSERVER
3316 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3317 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3318 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3319 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003320# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003321 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003322# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003323 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3324 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3325 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3326 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3327#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003328#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003329 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003330#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331#ifdef FEAT_VIMINFO
3332 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3333#endif
3334 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3335 main_msg(_("--version\t\tPrint version information and exit"));
3336
3337#ifdef FEAT_GUI_X11
3338# ifdef FEAT_GUI_MOTIF
3339 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3340# else
3341# ifdef FEAT_GUI_ATHENA
3342# ifdef FEAT_GUI_NEXTAW
3343 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3344# else
3345 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3346# endif
3347# endif
3348# endif
3349 main_msg(_("-display <display>\tRun vim on <display>"));
3350 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003351 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3352 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3353 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3354 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3355 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3356 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3357 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3358 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3359# ifdef FEAT_GUI_ATHENA
3360 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3361# endif
3362 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3363 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3364 main_msg(_("-xrm <resource>\tSet the specified resource"));
3365#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003366#ifdef FEAT_GUI_GTK
3367 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3368 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3369 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3370 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3371 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003372 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003373 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003374 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003375#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003376#ifdef FEAT_GUI_W32
3377 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003378 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003379#endif
3380
3381#ifdef FEAT_GUI_GNOME
3382 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3383 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003384 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003385 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003386 gui.dofork = FALSE;
3387 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388 else
3389#endif
3390 mch_exit(0);
3391}
3392
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003393#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003394/*
3395 * Check the result of the ATTENTION dialog:
3396 * When "Quit" selected, exit Vim.
3397 * When "Recover" selected, recover the file.
3398 */
3399 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003400check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003401{
3402 if (swap_exists_action == SEA_QUIT)
3403 getout(1);
3404 handle_swap_exists(NULL);
3405}
3406#endif
3407
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003408#endif
3409
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003410#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003411static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003412
3413static struct timeval prev_timeval;
3414
Bram Moolenaar3f269672009-11-03 11:11:11 +00003415# ifdef WIN3264
3416/*
3417 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3418 */
3419 static int
3420gettimeofday(struct timeval *tv, char *dummy)
3421{
3422 long t = clock();
3423 tv->tv_sec = t / CLOCKS_PER_SEC;
3424 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3425 return 0;
3426}
3427# endif
3428
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003429/*
3430 * Save the previous time before doing something that could nest.
3431 * set "*tv_rel" to the time elapsed so far.
3432 */
3433 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003434time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003435{
3436 *((struct timeval *)tv_rel) = prev_timeval;
3437 gettimeofday(&prev_timeval, NULL);
3438 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3439 - ((struct timeval *)tv_rel)->tv_usec;
3440 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3441 - ((struct timeval *)tv_rel)->tv_sec;
3442 if (((struct timeval *)tv_rel)->tv_usec < 0)
3443 {
3444 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3445 --((struct timeval *)tv_rel)->tv_sec;
3446 }
3447 *(struct timeval *)tv_start = prev_timeval;
3448}
3449
3450/*
3451 * Compute the previous time after doing something that could nest.
3452 * Subtract "*tp" from prev_timeval;
3453 * Note: The arguments are (void *) to avoid trouble with systems that don't
3454 * have struct timeval.
3455 */
3456 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003457time_pop(
3458 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003459{
3460 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3461 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3462 if (prev_timeval.tv_usec < 0)
3463 {
3464 prev_timeval.tv_usec += 1000000;
3465 --prev_timeval.tv_sec;
3466 }
3467}
3468
3469 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003470time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003471{
3472 long usec;
3473 long msec;
3474
3475 usec = now->tv_usec - then->tv_usec;
3476 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3477 usec = usec % 1000L;
3478 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3479}
3480
3481 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003482time_msg(
3483 char *mesg,
3484 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003485 (struct timeval *) */
3486{
3487 static struct timeval start;
3488 struct timeval now;
3489
3490 if (time_fd != NULL)
3491 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003492 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003493 {
3494 gettimeofday(&start, NULL);
3495 prev_timeval = start;
3496 fprintf(time_fd, "\n\ntimes in msec\n");
3497 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3498 fprintf(time_fd, " clock elapsed: other lines\n\n");
3499 }
3500 gettimeofday(&now, NULL);
3501 time_diff(&start, &now);
3502 if (((struct timeval *)tv_start) != NULL)
3503 {
3504 fprintf(time_fd, " ");
3505 time_diff(((struct timeval *)tv_start), &now);
3506 }
3507 fprintf(time_fd, " ");
3508 time_diff(&prev_timeval, &now);
3509 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003510 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003511 }
3512}
3513
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003514#endif
3515
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003516#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003517
3518/*
3519 * Common code for the X command server and the Win32 command server.
3520 */
3521
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003522static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003523
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003524/*
3525 * Do the client-server stuff, unless "--servername ''" was used.
3526 */
3527 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003528exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003529{
3530 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3531 {
3532# ifdef WIN32
3533 /* Initialise the client/server messaging infrastructure. */
3534 serverInitMessaging();
3535# endif
3536
3537 /*
3538 * When a command server argument was found, execute it. This may
3539 * exit Vim when it was successful. Otherwise it's executed further
3540 * on. Remember the encoding used here in "serverStrEnc".
3541 */
3542 if (parmp->serverArg)
3543 {
3544 cmdsrv_main(&parmp->argc, parmp->argv,
3545 parmp->serverName_arg, &parmp->serverStr);
3546# ifdef FEAT_MBYTE
3547 parmp->serverStrEnc = vim_strsave(p_enc);
3548# endif
3549 }
3550
3551 /* If we're still running, get the name to register ourselves.
3552 * On Win32 can register right now, for X11 need to setup the
3553 * clipboard first, it's further down. */
3554 parmp->servername = serverMakeName(parmp->serverName_arg,
3555 parmp->argv[0]);
3556# ifdef WIN32
3557 if (parmp->servername != NULL)
3558 {
3559 serverSetName(parmp->servername);
3560 vim_free(parmp->servername);
3561 }
3562# endif
3563 }
3564}
3565
3566/*
3567 * Prepare for running as a Vim server.
3568 */
3569 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003570prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003571{
3572# if defined(FEAT_X11)
3573 /*
3574 * Register for remote command execution with :serversend and --remote
3575 * unless there was a -X or a --servername '' on the command line.
3576 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003577 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003578 */
3579 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3580# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003581 (gui.in_use
3582# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003583 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003584# endif
3585 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003586# endif
3587 parmp->serverName_arg != NULL))
3588 {
3589 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3590 vim_free(parmp->servername);
3591 TIME_MSG("register server name");
3592 }
3593 else
3594 serverDelayedStartName = parmp->servername;
3595# endif
3596
3597 /*
3598 * Execute command ourselves if we're here because the send failed (or
3599 * else we would have exited above).
3600 */
3601 if (parmp->serverStr != NULL)
3602 {
3603 char_u *p;
3604
3605 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3606 parmp->serverStr, &p));
3607 vim_free(p);
3608 }
3609}
3610
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003611 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003612cmdsrv_main(
3613 int *argc,
3614 char **argv,
3615 char_u *serverName_arg,
3616 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003617{
3618 char_u *res;
3619 int i;
3620 char_u *sname;
3621 int ret;
3622 int didone = FALSE;
3623 int exiterr = 0;
3624 char **newArgV = argv + 1;
3625 int newArgC = 1,
3626 Argc = *argc;
3627 int argtype;
3628#define ARGTYPE_OTHER 0
3629#define ARGTYPE_EDIT 1
3630#define ARGTYPE_EDIT_WAIT 2
3631#define ARGTYPE_SEND 3
3632 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003633 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003634# ifndef FEAT_X11
3635 HWND srv;
3636# else
3637 Window srv;
3638
3639 setup_term_clip();
3640# endif
3641
3642 sname = serverMakeName(serverName_arg, argv[0]);
3643 if (sname == NULL)
3644 return;
3645
3646 /*
3647 * Execute the command server related arguments and remove them
3648 * from the argc/argv array; We may have to return into main()
3649 */
3650 for (i = 1; i < Argc; i++)
3651 {
3652 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003653 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003654 {
3655 for (; i < *argc; i++)
3656 {
3657 *newArgV++ = argv[i];
3658 newArgC++;
3659 }
3660 break;
3661 }
3662
Bram Moolenaareb94e552006-03-11 21:35:11 +00003663 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003664 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003665 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3666 {
3667 char *p = argv[i] + 8;
3668
3669 argtype = ARGTYPE_EDIT;
3670 while (*p != NUL)
3671 {
3672 if (STRNICMP(p, "-wait", 5) == 0)
3673 {
3674 argtype = ARGTYPE_EDIT_WAIT;
3675 p += 5;
3676 }
3677 else if (STRNICMP(p, "-silent", 7) == 0)
3678 {
3679 silent = TRUE;
3680 p += 7;
3681 }
3682 else if (STRNICMP(p, "-tab", 4) == 0)
3683 {
3684 tabs = TRUE;
3685 p += 4;
3686 }
3687 else
3688 {
3689 argtype = ARGTYPE_OTHER;
3690 break;
3691 }
3692 }
3693 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003694 else
3695 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003696
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003697 if (argtype != ARGTYPE_OTHER)
3698 {
3699 if (i == *argc - 1)
3700 mainerr_arg_missing((char_u *)argv[i]);
3701 if (argtype == ARGTYPE_SEND)
3702 {
3703 *serverStr = (char_u *)argv[i + 1];
3704 i++;
3705 }
3706 else
3707 {
3708 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003709 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003710 if (*serverStr == NULL)
3711 {
3712 /* Probably out of memory, exit. */
3713 didone = TRUE;
3714 exiterr = 1;
3715 break;
3716 }
3717 Argc = i;
3718 }
3719# ifdef FEAT_X11
3720 if (xterm_dpy == NULL)
3721 {
3722 mch_errmsg(_("No display"));
3723 ret = -1;
3724 }
3725 else
3726 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3727 NULL, &srv, 0, 0, silent);
3728# else
3729 /* Win32 always works? */
3730 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3731# endif
3732 if (ret < 0)
3733 {
3734 if (argtype == ARGTYPE_SEND)
3735 {
3736 /* Failed to send, abort. */
3737 mch_errmsg(_(": Send failed.\n"));
3738 didone = TRUE;
3739 exiterr = 1;
3740 }
3741 else if (!silent)
3742 /* Let vim start normally. */
3743 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3744 break;
3745 }
3746
3747# ifdef FEAT_GUI_W32
3748 /* Guess that when the server name starts with "g" it's a GUI
3749 * server, which we can bring to the foreground here.
3750 * Foreground() in the server doesn't work very well. */
3751 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3752 SetForegroundWindow(srv);
3753# endif
3754
3755 /*
3756 * For --remote-wait: Wait until the server did edit each
3757 * file. Also detect that the server no longer runs.
3758 */
3759 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3760 {
3761 int numFiles = *argc - i - 1;
3762 int j;
3763 char_u *done = alloc(numFiles);
3764 char_u *p;
3765# ifdef FEAT_GUI_W32
3766 NOTIFYICONDATA ni;
3767 int count = 0;
3768 extern HWND message_window;
3769# endif
3770
3771 if (numFiles > 0 && argv[i + 1][0] == '+')
3772 /* Skip "+cmd" argument, don't wait for it to be edited. */
3773 --numFiles;
3774
3775# ifdef FEAT_GUI_W32
3776 ni.cbSize = sizeof(ni);
3777 ni.hWnd = message_window;
3778 ni.uID = 0;
3779 ni.uFlags = NIF_ICON|NIF_TIP;
3780 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3781 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3782 Shell_NotifyIcon(NIM_ADD, &ni);
3783# endif
3784
3785 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003786 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003787 while (memchr(done, 0, numFiles) != NULL)
3788 {
3789# ifdef WIN32
3790 p = serverGetReply(srv, NULL, TRUE, TRUE);
3791 if (p == NULL)
3792 break;
3793# else
3794 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3795 break;
3796# endif
3797 j = atoi((char *)p);
3798 if (j >= 0 && j < numFiles)
3799 {
3800# ifdef FEAT_GUI_W32
3801 ++count;
3802 sprintf(ni.szTip, _("%d of %d edited"),
3803 count, numFiles);
3804 Shell_NotifyIcon(NIM_MODIFY, &ni);
3805# endif
3806 done[j] = 1;
3807 }
3808 }
3809# ifdef FEAT_GUI_W32
3810 Shell_NotifyIcon(NIM_DELETE, &ni);
3811# endif
3812 }
3813 }
3814 else if (STRICMP(argv[i], "--remote-expr") == 0)
3815 {
3816 if (i == *argc - 1)
3817 mainerr_arg_missing((char_u *)argv[i]);
3818# ifdef WIN32
3819 /* Win32 always works? */
3820 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3821 &res, NULL, 1, FALSE) < 0)
3822# else
3823 if (xterm_dpy == NULL)
3824 mch_errmsg(_("No display: Send expression failed.\n"));
3825 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3826 &res, NULL, 1, 1, FALSE) < 0)
3827# endif
3828 {
3829 if (res != NULL && *res != NUL)
3830 {
3831 /* Output error from remote */
3832 mch_errmsg((char *)res);
3833 vim_free(res);
3834 res = NULL;
3835 }
3836 mch_errmsg(_(": Send expression failed.\n"));
3837 }
3838 }
3839 else if (STRICMP(argv[i], "--serverlist") == 0)
3840 {
3841# ifdef WIN32
3842 /* Win32 always works? */
3843 res = serverGetVimNames();
3844# else
3845 if (xterm_dpy != NULL)
3846 res = serverGetVimNames(xterm_dpy);
3847# endif
3848 if (called_emsg)
3849 mch_errmsg("\n");
3850 }
3851 else if (STRICMP(argv[i], "--servername") == 0)
3852 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003853 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003854 i++;
3855 continue;
3856 }
3857 else
3858 {
3859 *newArgV++ = argv[i];
3860 newArgC++;
3861 continue;
3862 }
3863 didone = TRUE;
3864 if (res != NULL && *res != NUL)
3865 {
3866 mch_msg((char *)res);
3867 if (res[STRLEN(res) - 1] != '\n')
3868 mch_msg("\n");
3869 }
3870 vim_free(res);
3871 }
3872
3873 if (didone)
3874 {
3875 display_errors(); /* display any collected messages */
3876 exit(exiterr); /* Mission accomplished - get out */
3877 }
3878
3879 /* Return back into main() */
3880 *argc = newArgC;
3881 vim_free(sname);
3882}
3883
3884/*
3885 * Build a ":drop" command to send to a Vim server.
3886 */
3887 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003888build_drop_cmd(
3889 int filec,
3890 char **filev,
3891 int tabs, /* Use ":tab drop" instead of ":drop". */
3892 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003893{
3894 garray_T ga;
3895 int i;
3896 char_u *inicmd = NULL;
3897 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003898 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003899 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003900
3901 if (filec > 0 && filev[0][0] == '+')
3902 {
3903 inicmd = (char_u *)filev[0] + 1;
3904 filev++;
3905 filec--;
3906 }
3907 /* Check if we have at least one argument. */
3908 if (filec <= 0)
3909 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003910
3911 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003912 cwd = alloc(MAXPATHL);
3913 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003914 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003915 if (mch_dirname(cwd, MAXPATHL) != OK)
3916 {
3917 vim_free(cwd);
3918 return NULL;
3919 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003920 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003921#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003922 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003923#else
3924 PATH_ESC_CHARS,
3925#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003926 '\\', TRUE);
3927 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003928 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003929 return NULL;
3930 ga_init2(&ga, 1, 100);
3931 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003932 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003933
3934 /* Call inputsave() so that a prompt for an encryption key works. */
3935 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3936 if (tabs)
3937 ga_concat(&ga, (char_u *)"tab ");
3938 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003939 for (i = 0; i < filec; i++)
3940 {
3941 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003942 * do it again in the Vim server. On MS-Windows only escape
3943 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003944 p = vim_strsave_escaped((char_u *)filev[i],
3945#ifdef UNIX
3946 PATH_ESC_CHARS
3947#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003948 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003949#endif
3950 );
3951 if (p == NULL)
3952 {
3953 vim_free(ga.ga_data);
3954 return NULL;
3955 }
3956 ga_concat(&ga, (char_u *)" ");
3957 ga_concat(&ga, p);
3958 vim_free(p);
3959 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003960 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3961
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003962 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3963 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003964 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3965
3966 /* Switch back to the correct current directory (prior to temporary path
3967 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003968 * correct after the :drop command. With line breaks and spaces:
3969 * if !exists('+acd') || !&acd
3970 * if haslocaldir()
3971 * cd -
3972 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003973 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003974 * cd -
3975 * endif
3976 * endif
3977 */
3978 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003979 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003980 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003981 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003982 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003983
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003984 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003985 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3986 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003987 if (inicmd != NULL)
3988 {
3989 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3990 * the following commands to be inserted as text. Use a "|",
3991 * hopefully "inicmd" does allow this... */
3992 ga_concat(&ga, inicmd);
3993 ga_concat(&ga, (char_u *)"|");
3994 }
3995 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3996 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003997 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003998 ga_append(&ga, NUL);
3999 return ga.ga_data;
4000}
4001
4002/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004003 * Make our basic server name: use the specified "arg" if given, otherwise use
4004 * the tail of the command "cmd" we were started with.
4005 * Return the name in allocated memory. This doesn't include a serial number.
4006 */
4007 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004008serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004009{
4010 char_u *p;
4011
4012 if (arg != NULL && *arg != NUL)
4013 p = vim_strsave_up(arg);
4014 else
4015 {
4016 p = vim_strsave_up(gettail((char_u *)cmd));
4017 /* Remove .exe or .bat from the name. */
4018 if (p != NULL && vim_strchr(p, '.') != NULL)
4019 *vim_strchr(p, '.') = NUL;
4020 }
4021 return p;
4022}
4023#endif /* FEAT_CLIENTSERVER */
4024
4025#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4026/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004027 * Replace termcodes such as <CR> and insert as key presses if there is room.
4028 */
4029 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004030server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004031{
4032 char_u *ptr = NULL;
4033 char_u *cpo_save = p_cpo;
4034
4035 /* Set 'cpoptions' the way we want it.
4036 * B set - backslashes are *not* treated specially
4037 * k set - keycodes are *not* reverse-engineered
4038 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004039 * The last but one parameter of replace_termcodes() is TRUE so that the
4040 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004041 */
4042 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004043 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004044 p_cpo = cpo_save;
4045
4046 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4047 {
4048 /*
4049 * Add the string to the input stream.
4050 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4051 *
4052 * First clear typed characters from the typeahead buffer, there could
4053 * be half a mapping there. Then append to the existing string, so
4054 * that multiple commands from a client are concatenated.
4055 */
4056 if (typebuf.tb_maplen < typebuf.tb_len)
4057 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4058 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4059
4060 /* Let input_available() know we inserted text in the typeahead
4061 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004062 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004063 }
4064 vim_free((char_u *)ptr);
4065}
4066
4067/*
4068 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004069 */
4070 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004071eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004072{
4073 char_u *res;
4074 int save_dbl = debug_break_level;
4075 int save_ro = redir_off;
4076
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004077 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4078 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004079 debug_break_level = -1;
4080 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004081 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4082 * to be typed. Do generate errors so that try/catch works. */
4083 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004084
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004085 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004086
4087 debug_break_level = save_dbl;
4088 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004089 --emsg_silent;
4090 if (emsg_silent < 0)
4091 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004092
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004093 /* A client can tell us to redraw, but not to display the cursor, so do
4094 * that here. */
4095 setcursor();
4096 out_flush();
4097#ifdef FEAT_GUI
4098 if (gui.in_use)
4099 gui_update_cursor(FALSE, FALSE);
4100#endif
4101
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004102 return res;
4103}
4104
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004105/*
4106 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4107 * return an allocated string. Otherwise return "data".
4108 * "*tofree" is set to the result when it needs to be freed later.
4109 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004110 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004111serverConvert(
4112 char_u *client_enc UNUSED,
4113 char_u *data,
4114 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004115{
4116 char_u *res = data;
4117
4118 *tofree = NULL;
4119# ifdef FEAT_MBYTE
4120 if (client_enc != NULL && p_enc != NULL)
4121 {
4122 vimconv_T vimconv;
4123
4124 vimconv.vc_type = CONV_NONE;
4125 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4126 && vimconv.vc_type != CONV_NONE)
4127 {
4128 res = string_convert(&vimconv, data, NULL);
4129 if (res == NULL)
4130 res = data;
4131 else
4132 *tofree = res;
4133 }
4134 convert_setup(&vimconv, NULL, NULL);
4135 }
4136# endif
4137 return res;
4138}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004139#endif