blob: ff82a9f16044ef959f2fde27104977571e0a743e [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". */
745 for (wp = firstwin; wp != NULL; wp = wp->w_next)
746 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;
1378 for (wp = (tp == curtab)
1379 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001380 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001381 if (wp->w_buffer == NULL)
1382 /* Autocmd must have close the buffer already, skip. */
1383 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001384 buf = wp->w_buffer;
1385 if (buf->b_changedtick != -1)
1386 {
1387 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1388 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001389 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001390 /* start all over, autocommands may mess up the lists */
1391 next_tp = first_tabpage;
1392 break;
1393 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001394 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001395 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001396# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001397 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1398 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001399# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001400
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001401 /* Trigger BufUnload for buffers that are loaded */
1402 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1403 if (buf->b_ml.ml_mfp != NULL)
1404 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001405 bufref_T bufref;
1406
1407 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001408 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001410 if (!bufref_valid(&bufref))
1411 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001412 break;
1413 }
1414 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1415 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001416#endif
1417
1418#ifdef FEAT_VIMINFO
1419 if (*p_viminfo != NUL)
1420 /* Write out the registers, history, marks etc, to the viminfo file */
1421 write_viminfo(NULL, FALSE);
1422#endif
1423
1424#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001425 if (get_vim_var_nr(VV_DYING) <= 1)
1426 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001427#endif
1428
Bram Moolenaar05159a02005-02-26 23:04:13 +00001429#ifdef FEAT_PROFILE
1430 profile_dump();
1431#endif
1432
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001433 if (did_emsg
1434#ifdef FEAT_GUI
1435 || (gui.in_use && msg_didany && p_verbose > 0)
1436#endif
1437 )
1438 {
1439 /* give the user a chance to read the (error) message */
1440 no_wait_return = FALSE;
1441 wait_return(FALSE);
1442 }
1443
1444#ifdef FEAT_AUTOCMD
1445 /* Position the cursor again, the autocommands may have moved it */
1446# ifdef FEAT_GUI
1447 if (!gui.in_use)
1448# endif
1449 windgoto((int)Rows - 1, 0);
1450#endif
1451
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001452#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001453 job_stop_on_exit();
1454#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001455#ifdef FEAT_LUA
1456 lua_end();
1457#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001458#ifdef FEAT_MZSCHEME
1459 mzscheme_end();
1460#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001461#ifdef FEAT_TCL
1462 tcl_end();
1463#endif
1464#ifdef FEAT_RUBY
1465 ruby_end();
1466#endif
1467#ifdef FEAT_PYTHON
1468 python_end();
1469#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001470#ifdef FEAT_PYTHON3
1471 python3_end();
1472#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001473#ifdef FEAT_PERL
1474 perl_end();
1475#endif
1476#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1477 iconv_end();
1478#endif
1479#ifdef FEAT_NETBEANS_INTG
1480 netbeans_end();
1481#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001482#ifdef FEAT_CSCOPE
1483 cs_end();
1484#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001485#ifdef FEAT_EVAL
1486 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001487 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001488#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001489#if defined(WIN32) && defined(FEAT_MBYTE)
1490 free_cmd_argsW();
1491#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001492
1493 mch_exit(exitval);
1494}
1495
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001496#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1497/*
1498 * Setup to use the current locale (for ctype() and many other things).
1499 */
1500 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001501init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001502{
1503 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001504
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001505# ifdef FEAT_GUI_GTK
1506 /* Tell Gtk not to change our locale settings. */
1507 gtk_disable_setlocale();
1508# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001509# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1510 /* Make sure strtod() uses a decimal point, not a comma. */
1511 setlocale(LC_NUMERIC, "C");
1512# endif
1513
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001514# ifdef WIN32
1515 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1516 * text while it's expecting text in the current locale. This call avoids
1517 * that. */
1518 setlocale(LC_CTYPE, "C");
1519# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001520
1521# ifdef FEAT_GETTEXT
1522 {
1523 int mustfree = FALSE;
1524 char_u *p;
1525
1526# ifdef DYNAMIC_GETTEXT
1527 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001528 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001529# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001530 /* expand_env() doesn't work yet, because g_chartab[] is not
1531 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001532 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1533 if (p != NULL && *p != NUL)
1534 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001535 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001536 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1537 }
1538 if (mustfree)
1539 vim_free(p);
1540 textdomain(VIMPACKAGE);
1541 }
1542# endif
1543}
1544#endif
1545
1546/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001547 * Get the name of the display, before gui_prepare() removes it from
1548 * argv[]. Used for the xterm-clipboard display.
1549 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001550 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001551 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001552 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001553early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001554{
Bram Moolenaar03005972008-11-20 13:12:36 +00001555#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1556 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001557 int argc = parmp->argc;
1558 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001559 int i;
1560
1561 for (i = 1; i < argc; i++)
1562 {
1563 if (STRCMP(argv[i], "--") == 0)
1564 break;
1565# ifdef FEAT_XCLIPBOARD
1566 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001567# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001568 || STRICMP(argv[i], "--display") == 0
1569# endif
1570 )
1571 {
1572 if (i == argc - 1)
1573 mainerr_arg_missing((char_u *)argv[i]);
1574 xterm_display = argv[++i];
1575 }
1576# endif
1577# ifdef FEAT_CLIENTSERVER
1578 else if (STRICMP(argv[i], "--servername") == 0)
1579 {
1580 if (i == argc - 1)
1581 mainerr_arg_missing((char_u *)argv[i]);
1582 parmp->serverName_arg = (char_u *)argv[++i];
1583 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001584 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001585 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001586 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001587 {
1588 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001589# ifdef FEAT_GUI
1590 if (strstr(argv[i], "-wait") != 0)
1591 /* don't fork() when starting the GUI to edit files ourself */
1592 gui.dofork = FALSE;
1593# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001594 }
1595# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001596
1597# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1598# ifdef FEAT_GUI_W32
1599 else if (STRICMP(argv[i], "--windowid") == 0)
1600# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001601 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001602# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001603 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001604 long_u id;
1605 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001606
1607 if (i == argc - 1)
1608 mainerr_arg_missing((char_u *)argv[i]);
1609 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001610 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001611 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001612 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001613 if (count != 1)
1614 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1615 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001616# ifdef FEAT_GUI_W32
1617 win_socket_id = id;
1618# else
1619 gtk_socket_id = id;
1620# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001621 i++;
1622 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001623# endif
1624# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001625 else if (STRICMP(argv[i], "--echo-wid") == 0)
1626 echo_wid_arg = TRUE;
1627# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001628# ifndef FEAT_NETBEANS_INTG
1629 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001630 {
1631 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1632 mch_exit(2);
1633 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001634# endif
1635
Bram Moolenaar58d98232005-07-23 22:25:46 +00001636 }
1637#endif
1638}
1639
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001640#ifndef NO_VIM_MAIN
1641/*
1642 * Get a (optional) count for a Vim argument.
1643 */
1644 static int
1645get_number_arg(
1646 char_u *p, /* pointer to argument */
1647 int *idx, /* index in argument, is incremented */
1648 int def) /* default value */
1649{
1650 if (vim_isdigit(p[*idx]))
1651 {
1652 def = atoi((char *)&(p[*idx]));
1653 while (vim_isdigit(p[*idx]))
1654 *idx = *idx + 1;
1655 }
1656 return def;
1657}
1658
1659/*
1660 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1661 * If the executable name starts with "r" we disable shell commands.
1662 * If the next character is "e" we run in Easy mode.
1663 * If the next character is "g" we run the GUI version.
1664 * If the next characters are "view" we start in readonly mode.
1665 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1666 * If the next characters are "ex" we start in Ex mode. If it's followed
1667 * by "im" use improved Ex mode.
1668 */
1669 static void
1670parse_command_name(mparm_T *parmp)
1671{
1672 char_u *initstr;
1673
1674 initstr = gettail((char_u *)parmp->argv[0]);
1675
1676#ifdef MACOS_X_UNIX
1677 /* An issue has been seen when launching Vim in such a way that
1678 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1679 * executable or a symbolic link of it. Until this issue is resolved
1680 * we prohibit the GUI from being used.
1681 */
1682 if (STRCMP(initstr, parmp->argv[0]) == 0)
1683 disallow_gui = TRUE;
1684
1685 /* TODO: On MacOS X default to gui if argv[0] ends in:
1686 * /Vim.app/Contents/MacOS/Vim */
1687#endif
1688
1689#ifdef FEAT_EVAL
1690 set_vim_var_string(VV_PROGNAME, initstr, -1);
1691 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
1692#endif
1693
1694 if (TOLOWER_ASC(initstr[0]) == 'r')
1695 {
1696 restricted = TRUE;
1697 ++initstr;
1698 }
1699
1700 /* Use evim mode for "evim" and "egvim", not for "editor". */
1701 if (TOLOWER_ASC(initstr[0]) == 'e'
1702 && (TOLOWER_ASC(initstr[1]) == 'v'
1703 || TOLOWER_ASC(initstr[1]) == 'g'))
1704 {
1705#ifdef FEAT_GUI
1706 gui.starting = TRUE;
1707#endif
1708 parmp->evim_mode = TRUE;
1709 ++initstr;
1710 }
1711
1712 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1713 if (TOLOWER_ASC(initstr[0]) == 'g')
1714 {
1715 main_start_gui();
1716#ifdef FEAT_GUI
1717 ++initstr;
1718#endif
1719 }
1720
1721 if (STRNICMP(initstr, "view", 4) == 0)
1722 {
1723 readonlymode = TRUE;
1724 curbuf->b_p_ro = TRUE;
1725 p_uc = 10000; /* don't update very often */
1726 initstr += 4;
1727 }
1728 else if (STRNICMP(initstr, "vim", 3) == 0)
1729 initstr += 3;
1730
1731 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1732 if (STRICMP(initstr, "diff") == 0)
1733 {
1734#ifdef FEAT_DIFF
1735 parmp->diff_mode = TRUE;
1736#else
1737 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1738 mch_errmsg("\n");
1739 mch_exit(2);
1740#endif
1741 }
1742
1743 if (STRNICMP(initstr, "ex", 2) == 0)
1744 {
1745 if (STRNICMP(initstr + 2, "im", 2) == 0)
1746 exmode_active = EXMODE_VIM;
1747 else
1748 exmode_active = EXMODE_NORMAL;
1749 change_compatible(TRUE); /* set 'compatible' */
1750 }
1751}
1752
Bram Moolenaar58d98232005-07-23 22:25:46 +00001753/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001754 * Scan the command line arguments.
1755 */
1756 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001757command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001758{
1759 int argc = parmp->argc;
1760 char **argv = parmp->argv;
1761 int argv_idx; /* index in argv[n][] */
1762 int had_minmin = FALSE; /* found "--" argument */
1763 int want_argument; /* option argument with argument */
1764 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001765 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001766 long n;
1767
1768 --argc;
1769 ++argv;
1770 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1771 while (argc > 0)
1772 {
1773 /*
1774 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1775 */
1776 if (argv[0][0] == '+' && !had_minmin)
1777 {
1778 if (parmp->n_commands >= MAX_ARG_CMDS)
1779 mainerr(ME_EXTRA_CMD, NULL);
1780 argv_idx = -1; /* skip to next argument */
1781 if (argv[0][1] == NUL)
1782 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1783 else
1784 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1785 }
1786
1787 /*
1788 * Optional argument.
1789 */
1790 else if (argv[0][0] == '-' && !had_minmin)
1791 {
1792 want_argument = FALSE;
1793 c = argv[0][argv_idx++];
1794#ifdef VMS
1795 /*
1796 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1797 * and "-/X" as "-X".
1798 */
1799 if (c == '/')
1800 {
1801 c = argv[0][argv_idx++];
1802 c = TOUPPER_ASC(c);
1803 }
1804 else
1805 c = TOLOWER_ASC(c);
1806#endif
1807 switch (c)
1808 {
1809 case NUL: /* "vim -" read from stdin */
1810 /* "ex -" silent mode */
1811 if (exmode_active)
1812 silent_mode = TRUE;
1813 else
1814 {
1815 if (parmp->edit_type != EDIT_NONE)
1816 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1817 parmp->edit_type = EDIT_STDIN;
1818 read_cmd_fd = 2; /* read from stderr instead of stdin */
1819 }
1820 argv_idx = -1; /* skip to next argument */
1821 break;
1822
1823 case '-': /* "--" don't take any more option arguments */
1824 /* "--help" give help message */
1825 /* "--version" give version message */
1826 /* "--literal" take files literally */
1827 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001828 /* "--not-a-term" don't warn for not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001829 /* "--noplugin[s]" skip plugins */
1830 /* "--cmd <cmd>" execute cmd before vimrc */
1831 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1832 usage();
1833 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1834 {
1835 Columns = 80; /* need to init Columns */
1836 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1837 list_version();
1838 msg_putchar('\n');
1839 msg_didout = FALSE;
1840 mch_exit(0);
1841 }
1842 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1843 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001844#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001845 parmp->literal = TRUE;
1846#endif
1847 }
1848 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1849 {
1850#ifdef FEAT_GUI
1851 gui.dofork = FALSE; /* don't fork() when starting GUI */
1852#endif
1853 }
1854 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1855 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001856 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1857 parmp->not_a_term = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001858 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1859 {
1860 want_argument = TRUE;
1861 argv_idx += 3;
1862 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001863 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1864 {
1865 want_argument = TRUE;
1866 argv_idx += 11;
1867 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001868#ifdef FEAT_CLIENTSERVER
1869 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1870 ; /* already processed -- no arg */
1871 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1872 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1873 {
1874 /* already processed -- snatch the following arg */
1875 if (argc > 1)
1876 {
1877 --argc;
1878 ++argv;
1879 }
1880 }
1881#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001882#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1883# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001884 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001885# else
1886 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1887# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001888 {
1889 /* already processed -- snatch the following arg */
1890 if (argc > 1)
1891 {
1892 --argc;
1893 ++argv;
1894 }
1895 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001896#endif
1897#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001898 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1899 {
1900 /* already processed, skip */
1901 }
1902#endif
1903 else
1904 {
1905 if (argv[0][argv_idx])
1906 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1907 had_minmin = TRUE;
1908 }
1909 if (!want_argument)
1910 argv_idx = -1; /* skip to next argument */
1911 break;
1912
1913 case 'A': /* "-A" start in Arabic mode */
1914#ifdef FEAT_ARABIC
1915 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1916#else
1917 mch_errmsg(_(e_noarabic));
1918 mch_exit(2);
1919#endif
1920 break;
1921
1922 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001923 /* Needs to be effective before expanding file names, because
1924 * for Win32 this makes us edit a shortcut file itself,
1925 * instead of the file it links to. */
1926 set_options_bin(curbuf->b_p_bin, 1, 0);
1927 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001928 break;
1929
1930 case 'C': /* "-C" Compatible */
1931 change_compatible(TRUE);
1932 break;
1933
1934 case 'e': /* "-e" Ex mode */
1935 exmode_active = EXMODE_NORMAL;
1936 break;
1937
1938 case 'E': /* "-E" Improved Ex mode */
1939 exmode_active = EXMODE_VIM;
1940 break;
1941
1942 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1943 window directly, not with newcli */
1944#ifdef FEAT_GUI
1945 gui.dofork = FALSE; /* don't fork() when starting GUI */
1946#endif
1947 break;
1948
1949 case 'g': /* "-g" start GUI */
1950 main_start_gui();
1951 break;
1952
1953 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1954#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001955 p_fkmap = TRUE;
1956 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001957#else
1958 mch_errmsg(_(e_nofarsi));
1959 mch_exit(2);
1960#endif
1961 break;
1962
1963 case 'h': /* "-h" give help message */
1964#ifdef FEAT_GUI_GNOME
1965 /* Tell usage() to exit for "gvim". */
1966 gui.starting = FALSE;
1967#endif
1968 usage();
1969 break;
1970
1971 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1972#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001973 p_hkmap = TRUE;
1974 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001975#else
1976 mch_errmsg(_(e_nohebrew));
1977 mch_exit(2);
1978#endif
1979 break;
1980
1981 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1982#ifdef FEAT_LISP
1983 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1984 p_sm = TRUE;
1985#endif
1986 break;
1987
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001988 case 'M': /* "-M" no changes or writing of files */
1989 reset_modifiable();
1990 /* FALLTHROUGH */
1991
1992 case 'm': /* "-m" no writing of files */
1993 p_write = FALSE;
1994 break;
1995
1996 case 'y': /* "-y" easy mode */
1997#ifdef FEAT_GUI
1998 gui.starting = TRUE; /* start GUI a bit later */
1999#endif
2000 parmp->evim_mode = TRUE;
2001 break;
2002
2003 case 'N': /* "-N" Nocompatible */
2004 change_compatible(FALSE);
2005 break;
2006
2007 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002008#ifdef FEAT_NETBEANS_INTG
2009 /* checking for "-nb", netbeans parameters */
2010 if (argv[0][argv_idx] == 'b')
2011 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002012 netbeansArg = argv[0];
2013 argv_idx = -1; /* skip to next argument */
2014 }
2015 else
2016#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002017 parmp->no_swap_file = TRUE;
2018 break;
2019
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002020 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002021#ifdef TARGET_API_MAC_OSX
2022 /* For some reason on MacOS X, an argument like:
2023 -psn_0_10223617 is passed in when invoke from Finder
2024 or with the 'open' command */
2025 if (argv[0][argv_idx] == 's')
2026 {
2027 argv_idx = -1; /* bypass full -psn */
2028 main_start_gui();
2029 break;
2030 }
2031#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002032#ifdef FEAT_WINDOWS
2033 /* default is 0: open window for each file */
2034 parmp->window_count = get_number_arg((char_u *)argv[0],
2035 &argv_idx, 0);
2036 parmp->window_layout = WIN_TABS;
2037#endif
2038 break;
2039
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002040 case 'o': /* "-o[N]" open N horizontal split windows */
2041#ifdef FEAT_WINDOWS
2042 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002043 parmp->window_count = get_number_arg((char_u *)argv[0],
2044 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002045 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002046#endif
2047 break;
2048
2049 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaar44a2f922016-03-19 22:11:51 +01002050#ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002051 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002052 parmp->window_count = get_number_arg((char_u *)argv[0],
2053 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002054 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002055#endif
2056 break;
2057
2058#ifdef FEAT_QUICKFIX
2059 case 'q': /* "-q" QuickFix mode */
2060 if (parmp->edit_type != EDIT_NONE)
2061 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2062 parmp->edit_type = EDIT_QF;
2063 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2064 {
2065 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2066 argv_idx = -1;
2067 }
2068 else if (argc > 1) /* "-q {errorfile}" */
2069 want_argument = TRUE;
2070 break;
2071#endif
2072
2073 case 'R': /* "-R" readonly mode */
2074 readonlymode = TRUE;
2075 curbuf->b_p_ro = TRUE;
2076 p_uc = 10000; /* don't update very often */
2077 break;
2078
2079 case 'r': /* "-r" recovery mode */
2080 case 'L': /* "-L" recovery mode */
2081 recoverymode = 1;
2082 break;
2083
2084 case 's':
2085 if (exmode_active) /* "-s" silent (batch) mode */
2086 silent_mode = TRUE;
2087 else /* "-s {scriptin}" read from script file */
2088 want_argument = TRUE;
2089 break;
2090
2091 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2092 if (parmp->edit_type != EDIT_NONE)
2093 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2094 parmp->edit_type = EDIT_TAG;
2095 if (argv[0][argv_idx]) /* "-t{tag}" */
2096 {
2097 parmp->tagname = (char_u *)argv[0] + argv_idx;
2098 argv_idx = -1;
2099 }
2100 else /* "-t {tag}" */
2101 want_argument = TRUE;
2102 break;
2103
2104#ifdef FEAT_EVAL
2105 case 'D': /* "-D" Debugging */
2106 parmp->use_debug_break_level = 9999;
2107 break;
2108#endif
2109#ifdef FEAT_DIFF
2110 case 'd': /* "-d" 'diff' */
2111# ifdef AMIGA
2112 /* check for "-dev {device}" */
2113 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2114 want_argument = TRUE;
2115 else
2116# endif
2117 parmp->diff_mode = TRUE;
2118 break;
2119#endif
2120 case 'V': /* "-V{N}" Verbose level */
2121 /* default is 10: a little bit verbose */
2122 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2123 if (argv[0][argv_idx] != NUL)
2124 {
2125 set_option_value((char_u *)"verbosefile", 0L,
2126 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002127 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002128 }
2129 break;
2130
2131 case 'v': /* "-v" Vi-mode (as if called "vi") */
2132 exmode_active = 0;
2133#ifdef FEAT_GUI
2134 gui.starting = FALSE; /* don't start GUI */
2135#endif
2136 break;
2137
2138 case 'w': /* "-w{number}" set window height */
2139 /* "-w {scriptout}" write to script */
2140 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2141 {
2142 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2143 set_option_value((char_u *)"window", n, NULL, 0);
2144 break;
2145 }
2146 want_argument = TRUE;
2147 break;
2148
2149#ifdef FEAT_CRYPT
2150 case 'x': /* "-x" encrypted reading/writing of files */
2151 parmp->ask_for_key = TRUE;
2152 break;
2153#endif
2154
2155 case 'X': /* "-X" don't connect to X server */
2156#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2157 x_no_connect = TRUE;
2158#endif
2159 break;
2160
2161 case 'Z': /* "-Z" restricted mode */
2162 restricted = TRUE;
2163 break;
2164
2165 case 'c': /* "-c{command}" or "-c {command}" execute
2166 command */
2167 if (argv[0][argv_idx] != NUL)
2168 {
2169 if (parmp->n_commands >= MAX_ARG_CMDS)
2170 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002171 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2172 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002173 argv_idx = -1;
2174 break;
2175 }
2176 /*FALLTHROUGH*/
2177 case 'S': /* "-S {file}" execute Vim script */
2178 case 'i': /* "-i {viminfo}" use for viminfo */
2179#ifndef FEAT_DIFF
2180 case 'd': /* "-d {device}" device (for Amiga) */
2181#endif
2182 case 'T': /* "-T {terminal}" terminal name */
2183 case 'u': /* "-u {vimrc}" vim inits file */
2184 case 'U': /* "-U {gvimrc}" gvim inits file */
2185 case 'W': /* "-W {scriptout}" overwrite */
2186#ifdef FEAT_GUI_W32
2187 case 'P': /* "-P {parent title}" MDI parent */
2188#endif
2189 want_argument = TRUE;
2190 break;
2191
2192 default:
2193 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2194 }
2195
2196 /*
2197 * Handle option arguments with argument.
2198 */
2199 if (want_argument)
2200 {
2201 /*
2202 * Check for garbage immediately after the option letter.
2203 */
2204 if (argv[0][argv_idx] != NUL)
2205 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2206
2207 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002208 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002209 mainerr_arg_missing((char_u *)argv[0]);
2210 ++argv;
2211 argv_idx = -1;
2212
2213 switch (c)
2214 {
2215 case 'c': /* "-c {command}" execute command */
2216 case 'S': /* "-S {file}" execute Vim script */
2217 if (parmp->n_commands >= MAX_ARG_CMDS)
2218 mainerr(ME_EXTRA_CMD, NULL);
2219 if (c == 'S')
2220 {
2221 char *a;
2222
2223 if (argc < 1)
2224 /* "-S" without argument: use default session file
2225 * name. */
2226 a = SESSION_FILE;
2227 else if (argv[0][0] == '-')
2228 {
2229 /* "-S" followed by another option: use default
2230 * session file name. */
2231 a = SESSION_FILE;
2232 ++argc;
2233 --argv;
2234 }
2235 else
2236 a = argv[0];
2237 p = alloc((unsigned)(STRLEN(a) + 4));
2238 if (p == NULL)
2239 mch_exit(2);
2240 sprintf((char *)p, "so %s", a);
2241 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2242 parmp->commands[parmp->n_commands++] = p;
2243 }
2244 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002245 parmp->commands[parmp->n_commands++] =
2246 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002247 break;
2248
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002249 case '-':
2250 if (argv[-1][2] == 'c')
2251 {
2252 /* "--cmd {command}" execute command */
2253 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2254 mainerr(ME_EXTRA_CMD, NULL);
2255 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002256 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002257 }
2258 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002259 break;
2260
2261 /* case 'd': -d {device} is handled in mch_check_win() for the
2262 * Amiga */
2263
2264#ifdef FEAT_QUICKFIX
2265 case 'q': /* "-q {errorfile}" QuickFix mode */
2266 parmp->use_ef = (char_u *)argv[0];
2267 break;
2268#endif
2269
2270 case 'i': /* "-i {viminfo}" use for viminfo */
2271 use_viminfo = (char_u *)argv[0];
2272 break;
2273
2274 case 's': /* "-s {scriptin}" read from script file */
2275 if (scriptin[0] != NULL)
2276 {
2277scripterror:
2278 mch_errmsg(_("Attempt to open script file again: \""));
2279 mch_errmsg(argv[-1]);
2280 mch_errmsg(" ");
2281 mch_errmsg(argv[0]);
2282 mch_errmsg("\"\n");
2283 mch_exit(2);
2284 }
2285 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2286 {
2287 mch_errmsg(_("Cannot open for reading: \""));
2288 mch_errmsg(argv[0]);
2289 mch_errmsg("\"\n");
2290 mch_exit(2);
2291 }
2292 if (save_typebuf() == FAIL)
2293 mch_exit(2); /* out of memory */
2294 break;
2295
2296 case 't': /* "-t {tag}" */
2297 parmp->tagname = (char_u *)argv[0];
2298 break;
2299
2300 case 'T': /* "-T {terminal}" terminal name */
2301 /*
2302 * The -T term argument is always available and when
2303 * HAVE_TERMLIB is supported it overrides the environment
2304 * variable TERM.
2305 */
2306#ifdef FEAT_GUI
2307 if (term_is_gui((char_u *)argv[0]))
2308 gui.starting = TRUE; /* start GUI a bit later */
2309 else
2310#endif
2311 parmp->term = (char_u *)argv[0];
2312 break;
2313
2314 case 'u': /* "-u {vimrc}" vim inits file */
2315 parmp->use_vimrc = (char_u *)argv[0];
2316 break;
2317
2318 case 'U': /* "-U {gvimrc}" gvim inits file */
2319#ifdef FEAT_GUI
2320 use_gvimrc = (char_u *)argv[0];
2321#endif
2322 break;
2323
2324 case 'w': /* "-w {nr}" 'window' value */
2325 /* "-w {scriptout}" append to script file */
2326 if (vim_isdigit(*((char_u *)argv[0])))
2327 {
2328 argv_idx = 0;
2329 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2330 set_option_value((char_u *)"window", n, NULL, 0);
2331 argv_idx = -1;
2332 break;
2333 }
2334 /*FALLTHROUGH*/
2335 case 'W': /* "-W {scriptout}" overwrite script file */
2336 if (scriptout != NULL)
2337 goto scripterror;
2338 if ((scriptout = mch_fopen(argv[0],
2339 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2340 {
2341 mch_errmsg(_("Cannot open for script output: \""));
2342 mch_errmsg(argv[0]);
2343 mch_errmsg("\"\n");
2344 mch_exit(2);
2345 }
2346 break;
2347
2348#ifdef FEAT_GUI_W32
2349 case 'P': /* "-P {parent title}" MDI parent */
2350 gui_mch_set_parent(argv[0]);
2351 break;
2352#endif
2353 }
2354 }
2355 }
2356
2357 /*
2358 * File name argument.
2359 */
2360 else
2361 {
2362 argv_idx = -1; /* skip to next argument */
2363
2364 /* Check for only one type of editing. */
2365 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2366 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2367 parmp->edit_type = EDIT_FILE;
2368
2369#ifdef MSWIN
2370 /* Remember if the argument was a full path before changing
2371 * slashes to backslashes. */
2372 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2373 parmp->full_path = TRUE;
2374#endif
2375
2376 /* Add the file to the global argument list. */
2377 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2378 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2379 mch_exit(2);
2380#ifdef FEAT_DIFF
2381 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2382 && !mch_isdir(alist_name(&GARGLIST[0])))
2383 {
2384 char_u *r;
2385
2386 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2387 if (r != NULL)
2388 {
2389 vim_free(p);
2390 p = r;
2391 }
2392 }
2393#endif
2394#if defined(__CYGWIN32__) && !defined(WIN32)
2395 /*
2396 * If vim is invoked by non-Cygwin tools, convert away any
2397 * DOS paths, so things like .swp files are created correctly.
2398 * Look for evidence of non-Cygwin paths before we bother.
2399 * This is only for when using the Unix files.
2400 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002401 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002402 {
2403 char posix_path[PATH_MAX];
2404
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002405# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2406 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2407# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002408 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002409# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002410 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002411 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002412 if (p == NULL)
2413 mch_exit(2);
2414 }
2415#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002416
2417#ifdef USE_FNAME_CASE
2418 /* Make the case of the file name match the actual file. */
2419 fname_case(p, 0);
2420#endif
2421
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002422 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002423#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002424 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002425#else
2426 2 /* add buffer number now and use curbuf */
2427#endif
2428 );
2429
2430#if defined(FEAT_MBYTE) && defined(WIN32)
2431 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002432 /* Remember this argument has been added to the argument list.
2433 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002434 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002435# ifdef FEAT_DIFF
2436 parmp->diff_mode
2437# else
2438 FALSE
2439# endif
2440 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002441 }
2442#endif
2443 }
2444
2445 /*
2446 * If there are no more letters after the current "-", go to next
2447 * argument. argv_idx is set to -1 when the current argument is to be
2448 * skipped.
2449 */
2450 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2451 {
2452 --argc;
2453 ++argv;
2454 argv_idx = 1;
2455 }
2456 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002457
2458#ifdef FEAT_EVAL
2459 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2460 * one. */
2461 if (parmp->n_commands > 0)
2462 {
2463 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2464 if (p != NULL)
2465 {
2466 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2467 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2468 vim_free(p);
2469 }
2470 }
2471#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472}
2473
2474/*
2475 * Print a warning if stdout is not a terminal.
2476 * When starting in Ex mode and commands come from a file, set Silent mode.
2477 */
2478 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002479check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002480{
2481 int input_isatty; /* is active input a terminal? */
2482
2483 input_isatty = mch_input_isatty();
2484 if (exmode_active)
2485 {
2486 if (!input_isatty)
2487 silent_mode = TRUE;
2488 }
2489 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2490#ifdef FEAT_GUI
2491 /* don't want the delay when started from the desktop */
2492 && !gui.starting
2493#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002494 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002495 {
2496#ifdef NBDEBUG
2497 /*
2498 * This shouldn't be necessary. But if I run netbeans with the log
2499 * output coming to the console and XOpenDisplay fails, I get vim
2500 * trying to start with input/output to my console tty. This fills my
2501 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002502 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002503 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002504 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002505 {
2506 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2507 exit(1);
2508 }
2509#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002510#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2511 if (is_cygpty_used())
2512 {
2513 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2514 exit(1);
2515 }
2516#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002517 if (!parmp->stdout_isatty)
2518 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2519 if (!input_isatty)
2520 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2521 out_flush();
2522 if (scriptin[0] == NULL)
2523 ui_delay(2000L, TRUE);
2524 TIME_MSG("Warning delay");
2525 }
2526}
2527
2528/*
2529 * Read text from stdin.
2530 */
2531 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002532read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002533{
2534 int i;
2535
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002536#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 /* When getting the ATTENTION prompt here, use a dialog */
2538 swap_exists_action = SEA_DIALOG;
2539#endif
2540 no_wait_return = TRUE;
2541 i = msg_didany;
2542 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002543 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002544 no_wait_return = FALSE;
2545 msg_didany = i;
2546 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002547#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002548 check_swap_exists_action();
2549#endif
2550#if !(defined(AMIGA) || defined(MACOS))
2551 /*
2552 * Close stdin and dup it from stderr. Required for GPM to work
2553 * properly, and for running external commands.
2554 * Is there any other system that cannot do this?
2555 */
2556 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002557 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558#endif
2559}
2560
2561/*
2562 * Create the requested number of windows and edit buffers in them.
2563 * Also does recovery if "recoverymode" set.
2564 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002565 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002566create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567{
2568#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002569 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002570 int done = 0;
2571
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002572 /*
2573 * Create the number of windows that was requested.
2574 */
2575 if (parmp->window_count == -1) /* was not set */
2576 parmp->window_count = 1;
2577 if (parmp->window_count == 0)
2578 parmp->window_count = GARGCOUNT;
2579 if (parmp->window_count > 1)
2580 {
2581 /* Don't change the windows if there was a command in .vimrc that
2582 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002583 if (parmp->window_layout == 0)
2584 parmp->window_layout = WIN_HOR;
2585 if (parmp->window_layout == WIN_TABS)
2586 {
2587 parmp->window_count = make_tabpages(parmp->window_count);
2588 TIME_MSG("making tab pages");
2589 }
2590 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002591 {
2592 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002593 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594 TIME_MSG("making windows");
2595 }
2596 else
2597 parmp->window_count = win_count();
2598 }
2599 else
2600 parmp->window_count = 1;
2601#endif
2602
2603 if (recoverymode) /* do recover */
2604 {
2605 msg_scroll = TRUE; /* scroll message up */
2606 ml_recover();
2607 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2608 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002609 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002610 }
2611 else
2612 {
2613 /*
2614 * Open a buffer for windows that don't have one yet.
2615 * Commands in the .vimrc might have loaded a file or split the window.
2616 * Watch out for autocommands that delete a window.
2617 */
2618#ifdef FEAT_AUTOCMD
2619 /*
2620 * Don't execute Win/Buf Enter/Leave autocommands here
2621 */
2622 ++autocmd_no_enter;
2623 ++autocmd_no_leave;
2624#endif
2625#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002626 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002627 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002628 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002629 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002630 {
2631 if (parmp->window_layout == WIN_TABS)
2632 goto_tabpage(1);
2633 else
2634 curwin = firstwin;
2635 }
2636 else if (parmp->window_layout == WIN_TABS)
2637 {
2638 if (curtab->tp_next == NULL)
2639 break;
2640 goto_tabpage(0);
2641 }
2642 else
2643 {
2644 if (curwin->w_next == NULL)
2645 break;
2646 curwin = curwin->w_next;
2647 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002648 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002649#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002650 curbuf = curwin->w_buffer;
2651 if (curbuf->b_ml.ml_mfp == NULL)
2652 {
2653#ifdef FEAT_FOLDING
2654 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2655 if (p_fdls >= 0)
2656 curwin->w_p_fdl = p_fdls;
2657#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002658#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002659 /* When getting the ATTENTION prompt here, use a dialog */
2660 swap_exists_action = SEA_DIALOG;
2661#endif
2662 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002663
2664 /* create memfile, read file */
2665 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002666
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002667#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002668 if (swap_exists_action == SEA_QUIT)
2669 {
2670 if (got_int || only_one_window())
2671 {
2672 /* abort selected or quit and only one window */
2673 did_emsg = FALSE; /* avoid hit-enter prompt */
2674 getout(1);
2675 }
2676 /* We can't close the window, it would disturb what
2677 * happens next. Clear the file name and set the arg
2678 * index to -1 to delete it later. */
2679 setfname(curbuf, NULL, NULL, FALSE);
2680 curwin->w_arg_idx = -1;
2681 swap_exists_action = SEA_NONE;
2682 }
2683 else
2684 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002685#endif
2686#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002687 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002688#endif
2689 }
2690#ifdef FEAT_WINDOWS
2691 ui_breakcheck();
2692 if (got_int)
2693 {
2694 (void)vgetc(); /* only break the file loading, not the rest */
2695 break;
2696 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002697 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002698#endif
2699#ifdef FEAT_WINDOWS
2700 if (parmp->window_layout == WIN_TABS)
2701 goto_tabpage(1);
2702 else
2703 curwin = firstwin;
2704 curbuf = curwin->w_buffer;
2705#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002706#ifdef FEAT_AUTOCMD
2707 --autocmd_no_enter;
2708 --autocmd_no_leave;
2709#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002710 }
2711}
2712
2713#ifdef FEAT_WINDOWS
2714 /*
2715 * If opened more than one window, start editing files in the other
2716 * windows. make_windows() has already opened the windows.
2717 */
2718 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002719edit_buffers(
2720 mparm_T *parmp,
2721 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002722{
2723 int arg_idx; /* index in argument list */
2724 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002725 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002726 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727
2728# ifdef FEAT_AUTOCMD
2729 /*
2730 * Don't execute Win/Buf Enter/Leave autocommands here
2731 */
2732 ++autocmd_no_enter;
2733 ++autocmd_no_leave;
2734# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002735
2736 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2737 if (curwin->w_arg_idx == -1)
2738 {
2739 win_close(curwin, TRUE);
2740 advance = FALSE;
2741 }
2742
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002743 arg_idx = 1;
2744 for (i = 1; i < parmp->window_count; ++i)
2745 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002746 if (cwd != NULL)
2747 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002748 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2749 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002750 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002751 ++arg_idx;
2752 win_close(curwin, TRUE);
2753 advance = FALSE;
2754 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002755 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002756
Bram Moolenaar84212822006-11-07 21:59:47 +00002757 if (advance)
2758 {
2759 if (parmp->window_layout == WIN_TABS)
2760 {
2761 if (curtab->tp_next == NULL) /* just checking */
2762 break;
2763 goto_tabpage(0);
2764 }
2765 else
2766 {
2767 if (curwin->w_next == NULL) /* just checking */
2768 break;
2769 win_enter(curwin->w_next, FALSE);
2770 }
2771 }
2772 advance = TRUE;
2773
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002775 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002776 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2777 {
2778 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002779 /* Edit file from arg list, if there is one. When "Quit" selected
2780 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002781# ifdef HAS_SWAP_EXISTS_ACTION
2782 swap_exists_did_quit = FALSE;
2783# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002784 (void)do_ecmd(0, arg_idx < GARGCOUNT
2785 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002786 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002787# ifdef HAS_SWAP_EXISTS_ACTION
2788 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002789 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002790 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002791 if (got_int || only_one_window())
2792 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002793 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002794 did_emsg = FALSE; /* avoid hit-enter prompt */
2795 getout(1);
2796 }
2797 win_close(curwin, TRUE);
2798 advance = FALSE;
2799 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002800# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002801 if (arg_idx == GARGCOUNT - 1)
2802 arg_had_last = TRUE;
2803 ++arg_idx;
2804 }
2805 ui_breakcheck();
2806 if (got_int)
2807 {
2808 (void)vgetc(); /* only break the file loading, not the rest */
2809 break;
2810 }
2811 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002812
2813 if (parmp->window_layout == WIN_TABS)
2814 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002815# ifdef FEAT_AUTOCMD
2816 --autocmd_no_enter;
2817# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002818
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002819 /* make the first window the current window */
2820 win = firstwin;
2821#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2822 /* Avoid making a preview window the current window. */
2823 while (win->w_p_pvw)
2824 {
2825 win = win->w_next;
2826 if (win == NULL)
2827 {
2828 win = firstwin;
2829 break;
2830 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002831 }
2832#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002833 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002834
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002835# ifdef FEAT_AUTOCMD
2836 --autocmd_no_leave;
2837# endif
2838 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002839 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002840 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2841}
2842#endif /* FEAT_WINDOWS */
2843
2844/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002845 * Execute the commands from --cmd arguments "cmds[cnt]".
2846 */
2847 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002848exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002849{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850 char_u **cmds = parmp->pre_commands;
2851 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002852 int i;
2853
2854 if (cnt > 0)
2855 {
2856 curwin->w_cursor.lnum = 0; /* just in case.. */
2857 sourcing_name = (char_u *)_("pre-vimrc command line");
2858# ifdef FEAT_EVAL
2859 current_SID = SID_CMDARG;
2860# endif
2861 for (i = 0; i < cnt; ++i)
2862 do_cmdline_cmd(cmds[i]);
2863 sourcing_name = NULL;
2864# ifdef FEAT_EVAL
2865 current_SID = 0;
2866# endif
2867 TIME_MSG("--cmd commands");
2868 }
2869}
2870
2871/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002872 * Execute "+", "-c" and "-S" arguments.
2873 */
2874 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002875exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002876{
2877 int i;
2878
2879 /*
2880 * We start commands on line 0, make "vim +/pat file" match a
2881 * pattern on line 1. But don't move the cursor when an autocommand
2882 * with g`" was used.
2883 */
2884 msg_scroll = TRUE;
2885 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2886 curwin->w_cursor.lnum = 0;
2887 sourcing_name = (char_u *)"command line";
2888#ifdef FEAT_EVAL
2889 current_SID = SID_CARG;
2890#endif
2891 for (i = 0; i < parmp->n_commands; ++i)
2892 {
2893 do_cmdline_cmd(parmp->commands[i]);
2894 if (parmp->cmds_tofree[i])
2895 vim_free(parmp->commands[i]);
2896 }
2897 sourcing_name = NULL;
2898#ifdef FEAT_EVAL
2899 current_SID = 0;
2900#endif
2901 if (curwin->w_cursor.lnum == 0)
2902 curwin->w_cursor.lnum = 1;
2903
2904 if (!exmode_active)
2905 msg_scroll = FALSE;
2906
2907#ifdef FEAT_QUICKFIX
2908 /* When started with "-q errorfile" jump to first error again. */
2909 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002910 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002911#endif
2912 TIME_MSG("executing command arguments");
2913}
2914
2915/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002916 * Source startup scripts.
2917 */
2918 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002919source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002920{
2921 int i;
2922
2923 /*
2924 * For "evim" source evim.vim first of all, so that the user can overrule
2925 * any things he doesn't like.
2926 */
2927 if (parmp->evim_mode)
2928 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002929 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002930 TIME_MSG("source evim file");
2931 }
2932
2933 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002934 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002935 * nothing else.
2936 */
2937 if (parmp->use_vimrc != NULL)
2938 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002939 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2940 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002941 {
2942#ifdef FEAT_GUI
2943 if (use_gvimrc == NULL) /* don't load gvimrc either */
2944 use_gvimrc = parmp->use_vimrc;
2945#endif
2946 if (parmp->use_vimrc[2] == 'N')
2947 p_lpl = FALSE; /* don't load plugins either */
2948 }
2949 else
2950 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002951 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002952 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2953 }
2954 }
2955 else if (!silent_mode)
2956 {
2957#ifdef AMIGA
2958 struct Process *proc = (struct Process *)FindTask(0L);
2959 APTR save_winptr = proc->pr_WindowPtr;
2960
2961 /* Avoid a requester here for a volume that doesn't exist. */
2962 proc->pr_WindowPtr = (APTR)-1L;
2963#endif
2964
2965 /*
2966 * Get system wide defaults, if the file name is defined.
2967 */
2968#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002969 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002970#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002971#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002972 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002973#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002974
2975 /*
2976 * Try to read initialization commands from the following places:
2977 * - environment variable VIMINIT
2978 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2979 * - second user vimrc file ($VIM/.vimrc for Dos)
2980 * - environment variable EXINIT
2981 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2982 * - second user exrc file ($VIM/.exrc for Dos)
2983 * The first that exists is used, the rest is ignored.
2984 */
2985 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2986 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002987 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002988#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002989 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2990 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002991#endif
2992#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002993 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2994 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002995#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02002996#ifdef USR_VIMRC_FILE4
2997 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
2998 DOSO_VIMRC) == FAIL
2999#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003000 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003001 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003002 {
3003#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003004 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003005#endif
3006 }
3007 }
3008
3009 /*
3010 * Read initialization commands from ".vimrc" or ".exrc" in current
3011 * directory. This is only done if the 'exrc' option is set.
3012 * Because of security reasons we disallow shell and write commands
3013 * now, except for unix if the file is owned by the user or 'secure'
3014 * option has been reset in environment of global ".exrc" or ".vimrc".
3015 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3016 * SYS_VIMRC_FILE.
3017 */
3018 if (p_exrc)
3019 {
3020#if defined(UNIX) || defined(VMS)
3021 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3022 if (!file_owned(VIMRC_FILE))
3023#endif
3024 secure = p_secure;
3025
3026 i = FAIL;
3027 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3028 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3029#ifdef USR_VIMRC_FILE2
3030 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3031 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3032#endif
3033#ifdef USR_VIMRC_FILE3
3034 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3035 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3036#endif
3037#ifdef SYS_VIMRC_FILE
3038 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3039 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3040#endif
3041 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003042 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003043
3044 if (i == FAIL)
3045 {
3046#if defined(UNIX) || defined(VMS)
3047 /* if ".exrc" is not owned by user set 'secure' mode */
3048 if (!file_owned(EXRC_FILE))
3049 secure = p_secure;
3050 else
3051 secure = 0;
3052#endif
3053 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3054 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3055#ifdef USR_EXRC_FILE2
3056 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3057 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3058#endif
3059 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003060 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003061 }
3062 }
3063 if (secure == 2)
3064 need_wait_return = TRUE;
3065 secure = 0;
3066#ifdef AMIGA
3067 proc->pr_WindowPtr = save_winptr;
3068#endif
3069 }
3070 TIME_MSG("sourcing vimrc file(s)");
3071}
3072
3073/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003074 * Setup to start using the GUI. Exit with an error when not available.
3075 */
3076 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003077main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003078{
3079#ifdef FEAT_GUI
3080 gui.starting = TRUE; /* start GUI a bit later */
3081#else
3082 mch_errmsg(_(e_nogvim));
3083 mch_errmsg("\n");
3084 mch_exit(2);
3085#endif
3086}
3087
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003088#endif /* NO_VIM_MAIN */
3089
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003090/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003091 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003092 * Returns FAIL if the environment variable was not executed, OK otherwise.
3093 */
3094 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003095process_env(
3096 char_u *env,
3097 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003098{
3099 char_u *initstr;
3100 char_u *save_sourcing_name;
3101 linenr_T save_sourcing_lnum;
3102#ifdef FEAT_EVAL
3103 scid_T save_sid;
3104#endif
3105
3106 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3107 {
3108 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003109 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003110 save_sourcing_name = sourcing_name;
3111 save_sourcing_lnum = sourcing_lnum;
3112 sourcing_name = env;
3113 sourcing_lnum = 0;
3114#ifdef FEAT_EVAL
3115 save_sid = current_SID;
3116 current_SID = SID_ENV;
3117#endif
3118 do_cmdline_cmd(initstr);
3119 sourcing_name = save_sourcing_name;
3120 sourcing_lnum = save_sourcing_lnum;
3121#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003122 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003123#endif
3124 return OK;
3125 }
3126 return FAIL;
3127}
3128
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003129#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003130/*
3131 * Return TRUE if we are certain the user owns the file "fname".
3132 * Used for ".vimrc" and ".exrc".
3133 * Use both stat() and lstat() for extra security.
3134 */
3135 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003136file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003137{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003138 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003139# ifdef UNIX
3140 uid_t uid = getuid();
3141# else /* VMS */
3142 uid_t uid = ((getgid() << 16) | getuid());
3143# endif
3144
3145 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3146# ifdef HAVE_LSTAT
3147 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3148# endif
3149 );
3150}
3151#endif
3152
3153/*
3154 * Give an error message main_errors["n"] and exit.
3155 */
3156 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003157mainerr(
3158 int n, /* one of the ME_ defines */
3159 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003160{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003161#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003162 reset_signals(); /* kill us with CTRL-C here, if you like */
3163#endif
3164
3165 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003166 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003167 mch_errmsg(_(main_errors[n]));
3168 if (str != NULL)
3169 {
3170 mch_errmsg(": \"");
3171 mch_errmsg((char *)str);
3172 mch_errmsg("\"");
3173 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003174 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003175
3176 mch_exit(1);
3177}
3178
3179 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003180mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003181{
3182 mainerr(ME_ARG_MISSING, str);
3183}
3184
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003185#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003186/*
3187 * print a message with three spaces prepended and '\n' appended.
3188 */
3189 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003190main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003191{
3192 mch_msg(" ");
3193 mch_msg(s);
3194 mch_msg("\n");
3195}
3196
3197/*
3198 * Print messages for "vim -h" or "vim --help" and exit.
3199 */
3200 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003201usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202{
3203 int i;
3204 static char *(use[]) =
3205 {
3206 N_("[file ..] edit specified file(s)"),
3207 N_("- read text from stdin"),
3208 N_("-t tag edit file where tag is defined"),
3209#ifdef FEAT_QUICKFIX
3210 N_("-q [errorfile] edit file with first error")
3211#endif
3212 };
3213
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003214#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003215 reset_signals(); /* kill us with CTRL-C here, if you like */
3216#endif
3217
3218 mch_msg(longVersion);
3219 mch_msg(_("\n\nusage:"));
3220 for (i = 0; ; ++i)
3221 {
3222 mch_msg(_(" vim [arguments] "));
3223 mch_msg(_(use[i]));
3224 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3225 break;
3226 mch_msg(_("\n or:"));
3227 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003228#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003229 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003230#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003231
3232 mch_msg(_("\n\nArguments:\n"));
3233 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003234#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003235 main_msg(_("--literal\t\tDon't expand wildcards"));
3236#endif
3237#ifdef FEAT_OLE
3238 main_msg(_("-register\t\tRegister this gvim for OLE"));
3239 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3240#endif
3241#ifdef FEAT_GUI
3242 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3243 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3244#endif
3245 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3246 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003247 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003248 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3249#ifdef FEAT_DIFF
3250 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3251#endif
3252 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3253 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3254 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3255 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3256 main_msg(_("-M\t\t\tModifications in text not allowed"));
3257 main_msg(_("-b\t\t\tBinary mode"));
3258#ifdef FEAT_LISP
3259 main_msg(_("-l\t\t\tLisp mode"));
3260#endif
3261 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3262 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003263 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3264#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003266#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003267 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3268 main_msg(_("-r\t\t\tList swap files and exit"));
3269 main_msg(_("-r (with file name)\tRecover crashed session"));
3270 main_msg(_("-L\t\t\tSame as -r"));
3271#ifdef AMIGA
3272 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3273 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3274#endif
3275#ifdef FEAT_ARABIC
3276 main_msg(_("-A\t\t\tstart in Arabic mode"));
3277#endif
3278#ifdef FEAT_RIGHTLEFT
3279 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3280#endif
3281#ifdef FEAT_FKMAP
3282 main_msg(_("-F\t\t\tStart in Farsi mode"));
3283#endif
3284 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003285 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003286 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3287#ifdef FEAT_GUI
3288 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3289#endif
3290 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003291#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003292 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003293 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3294 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003295#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003296 main_msg(_("+\t\t\tStart at end of file"));
3297 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003299 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3300 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3301 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3302 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3303 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3304#ifdef FEAT_CRYPT
3305 main_msg(_("-x\t\t\tEdit encrypted files"));
3306#endif
3307#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3308# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3309 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3310# endif
3311 main_msg(_("-X\t\t\tDo not connect to X server"));
3312#endif
3313#ifdef FEAT_CLIENTSERVER
3314 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3315 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3316 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3317 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003318# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003319 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003320# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003321 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3322 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3323 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3324 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3325#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003326#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003327 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003328#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003329#ifdef FEAT_VIMINFO
3330 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3331#endif
3332 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3333 main_msg(_("--version\t\tPrint version information and exit"));
3334
3335#ifdef FEAT_GUI_X11
3336# ifdef FEAT_GUI_MOTIF
3337 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3338# else
3339# ifdef FEAT_GUI_ATHENA
3340# ifdef FEAT_GUI_NEXTAW
3341 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3342# else
3343 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3344# endif
3345# endif
3346# endif
3347 main_msg(_("-display <display>\tRun vim on <display>"));
3348 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3350 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3351 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3352 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3353 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3354 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3355 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3356 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3357# ifdef FEAT_GUI_ATHENA
3358 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3359# endif
3360 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3361 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3362 main_msg(_("-xrm <resource>\tSet the specified resource"));
3363#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003364#ifdef FEAT_GUI_GTK
3365 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3366 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3367 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3368 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3369 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003370 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003371 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003372 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003373#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003374#ifdef FEAT_GUI_W32
3375 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003376 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003377#endif
3378
3379#ifdef FEAT_GUI_GNOME
3380 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3381 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003382 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003383 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003384 gui.dofork = FALSE;
3385 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386 else
3387#endif
3388 mch_exit(0);
3389}
3390
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003391#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003392/*
3393 * Check the result of the ATTENTION dialog:
3394 * When "Quit" selected, exit Vim.
3395 * When "Recover" selected, recover the file.
3396 */
3397 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003398check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399{
3400 if (swap_exists_action == SEA_QUIT)
3401 getout(1);
3402 handle_swap_exists(NULL);
3403}
3404#endif
3405
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003406#endif
3407
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003409static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003410
3411static struct timeval prev_timeval;
3412
Bram Moolenaar3f269672009-11-03 11:11:11 +00003413# ifdef WIN3264
3414/*
3415 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3416 */
3417 static int
3418gettimeofday(struct timeval *tv, char *dummy)
3419{
3420 long t = clock();
3421 tv->tv_sec = t / CLOCKS_PER_SEC;
3422 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3423 return 0;
3424}
3425# endif
3426
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427/*
3428 * Save the previous time before doing something that could nest.
3429 * set "*tv_rel" to the time elapsed so far.
3430 */
3431 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003432time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003433{
3434 *((struct timeval *)tv_rel) = prev_timeval;
3435 gettimeofday(&prev_timeval, NULL);
3436 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3437 - ((struct timeval *)tv_rel)->tv_usec;
3438 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3439 - ((struct timeval *)tv_rel)->tv_sec;
3440 if (((struct timeval *)tv_rel)->tv_usec < 0)
3441 {
3442 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3443 --((struct timeval *)tv_rel)->tv_sec;
3444 }
3445 *(struct timeval *)tv_start = prev_timeval;
3446}
3447
3448/*
3449 * Compute the previous time after doing something that could nest.
3450 * Subtract "*tp" from prev_timeval;
3451 * Note: The arguments are (void *) to avoid trouble with systems that don't
3452 * have struct timeval.
3453 */
3454 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003455time_pop(
3456 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003457{
3458 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3459 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3460 if (prev_timeval.tv_usec < 0)
3461 {
3462 prev_timeval.tv_usec += 1000000;
3463 --prev_timeval.tv_sec;
3464 }
3465}
3466
3467 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003468time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003469{
3470 long usec;
3471 long msec;
3472
3473 usec = now->tv_usec - then->tv_usec;
3474 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3475 usec = usec % 1000L;
3476 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3477}
3478
3479 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003480time_msg(
3481 char *mesg,
3482 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003483 (struct timeval *) */
3484{
3485 static struct timeval start;
3486 struct timeval now;
3487
3488 if (time_fd != NULL)
3489 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003490 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003491 {
3492 gettimeofday(&start, NULL);
3493 prev_timeval = start;
3494 fprintf(time_fd, "\n\ntimes in msec\n");
3495 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3496 fprintf(time_fd, " clock elapsed: other lines\n\n");
3497 }
3498 gettimeofday(&now, NULL);
3499 time_diff(&start, &now);
3500 if (((struct timeval *)tv_start) != NULL)
3501 {
3502 fprintf(time_fd, " ");
3503 time_diff(((struct timeval *)tv_start), &now);
3504 }
3505 fprintf(time_fd, " ");
3506 time_diff(&prev_timeval, &now);
3507 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003508 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003509 }
3510}
3511
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003512#endif
3513
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003514#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003515
3516/*
3517 * Common code for the X command server and the Win32 command server.
3518 */
3519
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003520static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003521
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003522/*
3523 * Do the client-server stuff, unless "--servername ''" was used.
3524 */
3525 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003526exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003527{
3528 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3529 {
3530# ifdef WIN32
3531 /* Initialise the client/server messaging infrastructure. */
3532 serverInitMessaging();
3533# endif
3534
3535 /*
3536 * When a command server argument was found, execute it. This may
3537 * exit Vim when it was successful. Otherwise it's executed further
3538 * on. Remember the encoding used here in "serverStrEnc".
3539 */
3540 if (parmp->serverArg)
3541 {
3542 cmdsrv_main(&parmp->argc, parmp->argv,
3543 parmp->serverName_arg, &parmp->serverStr);
3544# ifdef FEAT_MBYTE
3545 parmp->serverStrEnc = vim_strsave(p_enc);
3546# endif
3547 }
3548
3549 /* If we're still running, get the name to register ourselves.
3550 * On Win32 can register right now, for X11 need to setup the
3551 * clipboard first, it's further down. */
3552 parmp->servername = serverMakeName(parmp->serverName_arg,
3553 parmp->argv[0]);
3554# ifdef WIN32
3555 if (parmp->servername != NULL)
3556 {
3557 serverSetName(parmp->servername);
3558 vim_free(parmp->servername);
3559 }
3560# endif
3561 }
3562}
3563
3564/*
3565 * Prepare for running as a Vim server.
3566 */
3567 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003568prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003569{
3570# if defined(FEAT_X11)
3571 /*
3572 * Register for remote command execution with :serversend and --remote
3573 * unless there was a -X or a --servername '' on the command line.
3574 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003575 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003576 */
3577 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3578# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003579 (gui.in_use
3580# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003581 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003582# endif
3583 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003584# endif
3585 parmp->serverName_arg != NULL))
3586 {
3587 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3588 vim_free(parmp->servername);
3589 TIME_MSG("register server name");
3590 }
3591 else
3592 serverDelayedStartName = parmp->servername;
3593# endif
3594
3595 /*
3596 * Execute command ourselves if we're here because the send failed (or
3597 * else we would have exited above).
3598 */
3599 if (parmp->serverStr != NULL)
3600 {
3601 char_u *p;
3602
3603 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3604 parmp->serverStr, &p));
3605 vim_free(p);
3606 }
3607}
3608
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003609 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003610cmdsrv_main(
3611 int *argc,
3612 char **argv,
3613 char_u *serverName_arg,
3614 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003615{
3616 char_u *res;
3617 int i;
3618 char_u *sname;
3619 int ret;
3620 int didone = FALSE;
3621 int exiterr = 0;
3622 char **newArgV = argv + 1;
3623 int newArgC = 1,
3624 Argc = *argc;
3625 int argtype;
3626#define ARGTYPE_OTHER 0
3627#define ARGTYPE_EDIT 1
3628#define ARGTYPE_EDIT_WAIT 2
3629#define ARGTYPE_SEND 3
3630 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003631 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003632# ifndef FEAT_X11
3633 HWND srv;
3634# else
3635 Window srv;
3636
3637 setup_term_clip();
3638# endif
3639
3640 sname = serverMakeName(serverName_arg, argv[0]);
3641 if (sname == NULL)
3642 return;
3643
3644 /*
3645 * Execute the command server related arguments and remove them
3646 * from the argc/argv array; We may have to return into main()
3647 */
3648 for (i = 1; i < Argc; i++)
3649 {
3650 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003651 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003652 {
3653 for (; i < *argc; i++)
3654 {
3655 *newArgV++ = argv[i];
3656 newArgC++;
3657 }
3658 break;
3659 }
3660
Bram Moolenaareb94e552006-03-11 21:35:11 +00003661 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003662 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003663 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3664 {
3665 char *p = argv[i] + 8;
3666
3667 argtype = ARGTYPE_EDIT;
3668 while (*p != NUL)
3669 {
3670 if (STRNICMP(p, "-wait", 5) == 0)
3671 {
3672 argtype = ARGTYPE_EDIT_WAIT;
3673 p += 5;
3674 }
3675 else if (STRNICMP(p, "-silent", 7) == 0)
3676 {
3677 silent = TRUE;
3678 p += 7;
3679 }
3680 else if (STRNICMP(p, "-tab", 4) == 0)
3681 {
3682 tabs = TRUE;
3683 p += 4;
3684 }
3685 else
3686 {
3687 argtype = ARGTYPE_OTHER;
3688 break;
3689 }
3690 }
3691 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003692 else
3693 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003694
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003695 if (argtype != ARGTYPE_OTHER)
3696 {
3697 if (i == *argc - 1)
3698 mainerr_arg_missing((char_u *)argv[i]);
3699 if (argtype == ARGTYPE_SEND)
3700 {
3701 *serverStr = (char_u *)argv[i + 1];
3702 i++;
3703 }
3704 else
3705 {
3706 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003707 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003708 if (*serverStr == NULL)
3709 {
3710 /* Probably out of memory, exit. */
3711 didone = TRUE;
3712 exiterr = 1;
3713 break;
3714 }
3715 Argc = i;
3716 }
3717# ifdef FEAT_X11
3718 if (xterm_dpy == NULL)
3719 {
3720 mch_errmsg(_("No display"));
3721 ret = -1;
3722 }
3723 else
3724 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3725 NULL, &srv, 0, 0, silent);
3726# else
3727 /* Win32 always works? */
3728 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3729# endif
3730 if (ret < 0)
3731 {
3732 if (argtype == ARGTYPE_SEND)
3733 {
3734 /* Failed to send, abort. */
3735 mch_errmsg(_(": Send failed.\n"));
3736 didone = TRUE;
3737 exiterr = 1;
3738 }
3739 else if (!silent)
3740 /* Let vim start normally. */
3741 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3742 break;
3743 }
3744
3745# ifdef FEAT_GUI_W32
3746 /* Guess that when the server name starts with "g" it's a GUI
3747 * server, which we can bring to the foreground here.
3748 * Foreground() in the server doesn't work very well. */
3749 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3750 SetForegroundWindow(srv);
3751# endif
3752
3753 /*
3754 * For --remote-wait: Wait until the server did edit each
3755 * file. Also detect that the server no longer runs.
3756 */
3757 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3758 {
3759 int numFiles = *argc - i - 1;
3760 int j;
3761 char_u *done = alloc(numFiles);
3762 char_u *p;
3763# ifdef FEAT_GUI_W32
3764 NOTIFYICONDATA ni;
3765 int count = 0;
3766 extern HWND message_window;
3767# endif
3768
3769 if (numFiles > 0 && argv[i + 1][0] == '+')
3770 /* Skip "+cmd" argument, don't wait for it to be edited. */
3771 --numFiles;
3772
3773# ifdef FEAT_GUI_W32
3774 ni.cbSize = sizeof(ni);
3775 ni.hWnd = message_window;
3776 ni.uID = 0;
3777 ni.uFlags = NIF_ICON|NIF_TIP;
3778 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3779 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3780 Shell_NotifyIcon(NIM_ADD, &ni);
3781# endif
3782
3783 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003784 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003785 while (memchr(done, 0, numFiles) != NULL)
3786 {
3787# ifdef WIN32
3788 p = serverGetReply(srv, NULL, TRUE, TRUE);
3789 if (p == NULL)
3790 break;
3791# else
3792 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3793 break;
3794# endif
3795 j = atoi((char *)p);
3796 if (j >= 0 && j < numFiles)
3797 {
3798# ifdef FEAT_GUI_W32
3799 ++count;
3800 sprintf(ni.szTip, _("%d of %d edited"),
3801 count, numFiles);
3802 Shell_NotifyIcon(NIM_MODIFY, &ni);
3803# endif
3804 done[j] = 1;
3805 }
3806 }
3807# ifdef FEAT_GUI_W32
3808 Shell_NotifyIcon(NIM_DELETE, &ni);
3809# endif
3810 }
3811 }
3812 else if (STRICMP(argv[i], "--remote-expr") == 0)
3813 {
3814 if (i == *argc - 1)
3815 mainerr_arg_missing((char_u *)argv[i]);
3816# ifdef WIN32
3817 /* Win32 always works? */
3818 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3819 &res, NULL, 1, FALSE) < 0)
3820# else
3821 if (xterm_dpy == NULL)
3822 mch_errmsg(_("No display: Send expression failed.\n"));
3823 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3824 &res, NULL, 1, 1, FALSE) < 0)
3825# endif
3826 {
3827 if (res != NULL && *res != NUL)
3828 {
3829 /* Output error from remote */
3830 mch_errmsg((char *)res);
3831 vim_free(res);
3832 res = NULL;
3833 }
3834 mch_errmsg(_(": Send expression failed.\n"));
3835 }
3836 }
3837 else if (STRICMP(argv[i], "--serverlist") == 0)
3838 {
3839# ifdef WIN32
3840 /* Win32 always works? */
3841 res = serverGetVimNames();
3842# else
3843 if (xterm_dpy != NULL)
3844 res = serverGetVimNames(xterm_dpy);
3845# endif
3846 if (called_emsg)
3847 mch_errmsg("\n");
3848 }
3849 else if (STRICMP(argv[i], "--servername") == 0)
3850 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003851 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003852 i++;
3853 continue;
3854 }
3855 else
3856 {
3857 *newArgV++ = argv[i];
3858 newArgC++;
3859 continue;
3860 }
3861 didone = TRUE;
3862 if (res != NULL && *res != NUL)
3863 {
3864 mch_msg((char *)res);
3865 if (res[STRLEN(res) - 1] != '\n')
3866 mch_msg("\n");
3867 }
3868 vim_free(res);
3869 }
3870
3871 if (didone)
3872 {
3873 display_errors(); /* display any collected messages */
3874 exit(exiterr); /* Mission accomplished - get out */
3875 }
3876
3877 /* Return back into main() */
3878 *argc = newArgC;
3879 vim_free(sname);
3880}
3881
3882/*
3883 * Build a ":drop" command to send to a Vim server.
3884 */
3885 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003886build_drop_cmd(
3887 int filec,
3888 char **filev,
3889 int tabs, /* Use ":tab drop" instead of ":drop". */
3890 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003891{
3892 garray_T ga;
3893 int i;
3894 char_u *inicmd = NULL;
3895 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003896 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003897 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003898
3899 if (filec > 0 && filev[0][0] == '+')
3900 {
3901 inicmd = (char_u *)filev[0] + 1;
3902 filev++;
3903 filec--;
3904 }
3905 /* Check if we have at least one argument. */
3906 if (filec <= 0)
3907 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003908
3909 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003910 cwd = alloc(MAXPATHL);
3911 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003912 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003913 if (mch_dirname(cwd, MAXPATHL) != OK)
3914 {
3915 vim_free(cwd);
3916 return NULL;
3917 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003918 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003919#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003920 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003921#else
3922 PATH_ESC_CHARS,
3923#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003924 '\\', TRUE);
3925 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003926 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003927 return NULL;
3928 ga_init2(&ga, 1, 100);
3929 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003930 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003931
3932 /* Call inputsave() so that a prompt for an encryption key works. */
3933 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3934 if (tabs)
3935 ga_concat(&ga, (char_u *)"tab ");
3936 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003937 for (i = 0; i < filec; i++)
3938 {
3939 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003940 * do it again in the Vim server. On MS-Windows only escape
3941 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003942 p = vim_strsave_escaped((char_u *)filev[i],
3943#ifdef UNIX
3944 PATH_ESC_CHARS
3945#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003946 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003947#endif
3948 );
3949 if (p == NULL)
3950 {
3951 vim_free(ga.ga_data);
3952 return NULL;
3953 }
3954 ga_concat(&ga, (char_u *)" ");
3955 ga_concat(&ga, p);
3956 vim_free(p);
3957 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003958 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3959
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003960 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3961 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003962 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3963
3964 /* Switch back to the correct current directory (prior to temporary path
3965 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003966 * correct after the :drop command. With line breaks and spaces:
3967 * if !exists('+acd') || !&acd
3968 * if haslocaldir()
3969 * cd -
3970 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003971 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003972 * cd -
3973 * endif
3974 * endif
3975 */
3976 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003977 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003978 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003979 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003980 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003981
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003982 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003983 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3984 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003985 if (inicmd != NULL)
3986 {
3987 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3988 * the following commands to be inserted as text. Use a "|",
3989 * hopefully "inicmd" does allow this... */
3990 ga_concat(&ga, inicmd);
3991 ga_concat(&ga, (char_u *)"|");
3992 }
3993 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3994 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003995 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003996 ga_append(&ga, NUL);
3997 return ga.ga_data;
3998}
3999
4000/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004001 * Make our basic server name: use the specified "arg" if given, otherwise use
4002 * the tail of the command "cmd" we were started with.
4003 * Return the name in allocated memory. This doesn't include a serial number.
4004 */
4005 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004006serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004007{
4008 char_u *p;
4009
4010 if (arg != NULL && *arg != NUL)
4011 p = vim_strsave_up(arg);
4012 else
4013 {
4014 p = vim_strsave_up(gettail((char_u *)cmd));
4015 /* Remove .exe or .bat from the name. */
4016 if (p != NULL && vim_strchr(p, '.') != NULL)
4017 *vim_strchr(p, '.') = NUL;
4018 }
4019 return p;
4020}
4021#endif /* FEAT_CLIENTSERVER */
4022
4023#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4024/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004025 * Replace termcodes such as <CR> and insert as key presses if there is room.
4026 */
4027 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004028server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004029{
4030 char_u *ptr = NULL;
4031 char_u *cpo_save = p_cpo;
4032
4033 /* Set 'cpoptions' the way we want it.
4034 * B set - backslashes are *not* treated specially
4035 * k set - keycodes are *not* reverse-engineered
4036 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004037 * The last but one parameter of replace_termcodes() is TRUE so that the
4038 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004039 */
4040 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004041 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004042 p_cpo = cpo_save;
4043
4044 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4045 {
4046 /*
4047 * Add the string to the input stream.
4048 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4049 *
4050 * First clear typed characters from the typeahead buffer, there could
4051 * be half a mapping there. Then append to the existing string, so
4052 * that multiple commands from a client are concatenated.
4053 */
4054 if (typebuf.tb_maplen < typebuf.tb_len)
4055 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4056 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4057
4058 /* Let input_available() know we inserted text in the typeahead
4059 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004060 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004061 }
4062 vim_free((char_u *)ptr);
4063}
4064
4065/*
4066 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004067 */
4068 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004069eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004070{
4071 char_u *res;
4072 int save_dbl = debug_break_level;
4073 int save_ro = redir_off;
4074
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004075 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4076 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004077 debug_break_level = -1;
4078 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004079 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4080 * to be typed. Do generate errors so that try/catch works. */
4081 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004082
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004083 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004084
4085 debug_break_level = save_dbl;
4086 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004087 --emsg_silent;
4088 if (emsg_silent < 0)
4089 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004091 /* A client can tell us to redraw, but not to display the cursor, so do
4092 * that here. */
4093 setcursor();
4094 out_flush();
4095#ifdef FEAT_GUI
4096 if (gui.in_use)
4097 gui_update_cursor(FALSE, FALSE);
4098#endif
4099
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004100 return res;
4101}
4102
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004103/*
4104 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4105 * return an allocated string. Otherwise return "data".
4106 * "*tofree" is set to the result when it needs to be freed later.
4107 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004108 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004109serverConvert(
4110 char_u *client_enc UNUSED,
4111 char_u *data,
4112 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004113{
4114 char_u *res = data;
4115
4116 *tofree = NULL;
4117# ifdef FEAT_MBYTE
4118 if (client_enc != NULL && p_enc != NULL)
4119 {
4120 vimconv_T vimconv;
4121
4122 vimconv.vc_type = CONV_NONE;
4123 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4124 && vimconv.vc_type != CONV_NONE)
4125 {
4126 res = string_convert(&vimconv, data, NULL);
4127 if (res == NULL)
4128 res = data;
4129 else
4130 *tofree = res;
4131 }
4132 convert_setup(&vimconv, NULL, NULL);
4133 }
4134# endif
4135 return res;
4136}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004137#endif