blob: 9cf10bd7817924cb7eea7612f025db65f0e8dfc9 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarb4210b32004-06-13 14:51:16 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
14# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
16# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
17 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000018# endif
19# include <limits.h>
20#endif
21
Bram Moolenaar97ff9b92016-06-26 20:37:46 +020022#if defined(WIN3264) && !defined(FEAT_GUI_W32)
23# include "iscygpty.h"
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Values for edit_type. */
27#define EDIT_NONE 0 /* no edit type yet */
28#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
29#define EDIT_STDIN 2 /* read file from stdin */
30#define EDIT_TAG 3 /* tag name argument given, use tagname */
31#define EDIT_QF 4 /* start in quickfix mode */
32
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010033#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010034static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000035#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010036static void mainerr(int, char_u *);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +020037# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
38static void init_locale(void);
39# endif
40static void early_arg_scan(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010041#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010042static void main_msg(char *s);
43static void usage(void);
44static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010045static void parse_command_name(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010046static void command_line_scan(mparm_T *parmp);
47static void check_tty(mparm_T *parmp);
48static void read_stdin(void);
49static void create_windows(mparm_T *parmp);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010050static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010051static void exe_pre_commands(mparm_T *parmp);
52static void exe_commands(mparm_T *parmp);
53static void source_startup_scripts(mparm_T *parmp);
54static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010055# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010056static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010057# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +010058# ifdef FEAT_EVAL
59static void set_progpath(char_u *argv0);
60# endif
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010061# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010062static void exec_on_server(mparm_T *parmp);
63static void prepare_server(mparm_T *parmp);
64static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
65static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010066# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +000067#endif
68
69
Bram Moolenaarb4210b32004-06-13 14:51:16 +000070/*
71 * Different types of error messages.
72 */
73static char *(main_errors[]) =
74{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000075 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000076#define ME_UNKNOWN_OPTION 0
77 N_("Too many edit arguments"),
78#define ME_TOO_MANY_ARGS 1
79 N_("Argument missing after"),
80#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +000081 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +000082#define ME_GARBAGE 3
83 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
84#define ME_EXTRA_CMD 4
85 N_("Invalid argument for"),
86#define ME_INVALID_ARG 5
87};
88
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010089#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaara6044292017-04-02 18:19:53 +020090
91/* Various parameters passed between main() and other functions. */
92static mparm_T params;
93
Bram Moolenaar8866d272012-11-28 15:55:42 +010094#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +020095
96static char_u *start_dir = NULL; /* current working dir on startup */
97
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +020098static int has_dash_c_arg = FALSE;
99
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100 int
101# ifdef VIMDLL
102_export
103# endif
104# ifdef FEAT_GUI_MSWIN
105# ifdef __BORLANDC__
106_cdecl
107# endif
108VimMain
109# else
110main
111# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100112(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000113{
Bram Moolenaar3f269672009-11-03 11:11:11 +0000114#ifdef STARTUPTIME
115 int i;
116#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000117
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118 /*
119 * Do any system-specific initialisations. These can NOT use IObuff or
120 * NameBuff. Thus emsg2() cannot be called!
121 */
122 mch_early_init();
123
Bram Moolenaar14993322014-09-09 12:25:33 +0200124#if defined(WIN32) && defined(FEAT_MBYTE)
125 /*
Bram Moolenaara06ecab2016-07-16 14:47:36 +0200126 * MinGW expands command line arguments, which confuses our code to
Bram Moolenaar14993322014-09-09 12:25:33 +0200127 * convert when 'encoding' changes. Get the unexpanded arguments.
128 */
129 argc = get_cmd_argsW(&argv);
130#endif
131
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000132 /* Many variables are in "params" so that we can pass them to invoked
133 * functions without a lot of arguments. "argc" and "argv" are also
134 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000135 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 params.argc = argc;
137 params.argv = argv;
138 params.want_full_screen = TRUE;
139#ifdef FEAT_EVAL
140 params.use_debug_break_level = -1;
141#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142 params.window_count = -1;
Bram Moolenaar58d98232005-07-23 22:25:46 +0000143
Bram Moolenaar99685e62013-05-11 13:56:18 +0200144#ifdef FEAT_RUBY
145 {
146 int ruby_stack_start;
147 vim_ruby_init((void *)&ruby_stack_start);
148 }
149#endif
150
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000151#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000152 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000153#endif
154
155#ifdef MEM_PROFILE
156 atexit(vim_mem_profile_dump);
157#endif
158
159#ifdef STARTUPTIME
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200160 /* Need to find "--startuptime" before actually parsing arguments. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000161 for (i = 1; i < argc; ++i)
162 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000163 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000164 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000165 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000166 TIME_MSG("--- VIM STARTING ---");
167 break;
168 }
169 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000170#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000171 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000172
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200173 common_init(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174
175#ifdef FEAT_CLIENTSERVER
176 /*
177 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000178 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000179 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000180 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000181#endif
182
183 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000184 * Figure out the way to work from the command name argv[0].
185 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000186 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000187 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000188
189 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000190 * Process the command line arguments. File names are put in the global
191 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000192 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000194 TIME_MSG("parsing arguments");
195
196 /*
197 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000198 * there is no terminal version, and on Windows we can't fork one off with
199 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000200 */
201#ifdef ALWAYS_USE_GUI
202 gui.starting = TRUE;
203#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000204# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000205 /*
206 * Check if the GUI can be started. Reset gui.starting if not.
207 * Don't know about other systems, stay on the safe side and don't check.
208 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000209 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000211 if (gui_init_check() == FAIL)
212 {
213 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000215 /* When running "evim" or "gvim -y" we need the menus, exit if we
216 * don't have them. */
217 if (params.evim_mode)
218 mch_exit(1);
219 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000220 }
221# endif
222#endif
223
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000224 if (GARGCOUNT > 0)
225 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100226#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000227 /*
228 * Expand wildcards in file names.
229 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000230 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200232 start_dir = alloc(MAXPATHL);
233 if (start_dir != NULL)
234 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235 /* Temporarily add '(' and ')' to 'isfname'. These are valid
236 * filename characters but are excluded from 'isfname' to make
237 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
238 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000239 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200241 if (start_dir != NULL)
242 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243 }
244#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200245 params.fname = alist_name(&GARGLIST[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000247
248#if defined(WIN32) && defined(FEAT_MBYTE)
249 {
250 extern void set_alist_count(void);
251
252 /* Remember the number of entries in the argument list. If it changes
253 * we don't react on setting 'encoding'. */
254 set_alist_count();
255 }
256#endif
257
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000258#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000259 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000260 {
261 /*
262 * If there is one filename, fully qualified, we have very probably
263 * been invoked from explorer, so change to the file's directory.
264 * Hint: to avoid this when typing a command use a forward slash.
265 * If the cd fails, it doesn't matter.
266 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200267 (void)vim_chdirfile(params.fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200268 if (start_dir != NULL)
269 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 }
271#endif
272 TIME_MSG("expanding arguments");
273
274#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000275 if (params.diff_mode && params.window_count == -1)
276 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277#endif
278
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000279 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280 ++RedrawingDisabled;
281
282 /*
283 * When listing swap file names, don't do cursor positioning et. al.
284 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200285 if (recoverymode && params.fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287
288 /*
289 * When certain to start the GUI, don't check capabilities of terminal.
290 * For GTK we can't be sure, but when started from the desktop it doesn't
291 * make sense to try using a terminal.
292 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000293#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000294 if (gui.starting
295# ifdef FEAT_GUI_GTK
296 && !isatty(2)
297# endif
298 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000299 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300#endif
301
Bram Moolenaard0573012017-10-28 21:11:06 +0200302#if defined(FEAT_GUI_MAC) && defined(MACOS_X_DARWIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000303 /* When the GUI is started from Finder, need to display messages in a
304 * message box. isatty(2) returns TRUE anyway, thus we need to check the
305 * name to know we're not started from a terminal. */
306 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000307 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000308 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000309
310 /* Avoid always using "/" as the current directory. Note that when
311 * started from Finder the arglist will be filled later in
312 * HandleODocAE() and "fname" will be NULL. */
313 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
314 && STRCMP(NameBuff, "/") == 0)
315 {
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200316 if (params.fname != NULL)
317 (void)vim_chdirfile(params.fname);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000318 else
319 {
320 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
321 vim_chdir(NameBuff);
322 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200323 if (start_dir != NULL)
324 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000325 }
326 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000327#endif
328
329 /*
330 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100331 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000332 * Note that we may use mch_exit() before mch_init()!
333 */
334 mch_init();
335 TIME_MSG("shell init");
336
337#ifdef USE_XSMP
338 /*
339 * For want of anywhere else to do it, try to connect to xsmp here.
340 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
341 * Hijacking -X 'no X connection' to also disable XSMP connection as that
342 * has a similar delay upon failure.
343 * Only try if SESSION_MANAGER is set to something non-null.
344 */
345 if (!x_no_connect)
346 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000347 char *p = getenv("SESSION_MANAGER");
348
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000349 if (p != NULL && *p != NUL)
350 {
351 xsmp_init();
352 TIME_MSG("xsmp init");
353 }
354 }
355#endif
356
357 /*
358 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000360 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000361
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000362 /* This message comes before term inits, but after setting "silent_mode"
363 * when the input is not a tty. */
364 if (GARGCOUNT > 1 && !silent_mode)
365 printf(_("%d files to edit\n"), GARGCOUNT);
366
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000368 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000369 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370 capabilities (will set full_screen) */
371 screen_start(); /* don't know where cursor is now */
372 TIME_MSG("Termcap init");
373 }
374
375 /*
376 * Set the default values for the options that use Rows and Columns.
377 */
378 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000379 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380#ifdef FEAT_DIFF
381 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
382 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000383 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 diff_win_options(firstwin, FALSE);
385#endif
386
387 cmdline_row = Rows - p_ch;
388 msg_row = cmdline_row;
389 screenalloc(FALSE); /* allocate screen buffers */
390 set_init_2();
391 TIME_MSG("inits 2");
392
393 msg_scroll = TRUE;
394 no_wait_return = TRUE;
395
396 init_mappings(); /* set up initial mappings */
397
398 init_highlight(TRUE, FALSE); /* set the default highlight groups */
399 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400
401#ifdef FEAT_EVAL
402 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000403 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404#endif
405
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200406 /* Reset 'loadplugins' for "-u NONE" before "--cmd" arguments.
407 * Allows for setting 'loadplugins' there. */
408 if (params.use_vimrc != NULL
409 && (STRCMP(params.use_vimrc, "NONE") == 0
410 || STRCMP(params.use_vimrc, "DEFAULTS") == 0))
411 p_lpl = FALSE;
412
413 /* Execute --cmd arguments. */
414 exe_pre_commands(&params);
415
416 /* Source startup scripts. */
417 source_startup_scripts(&params);
418
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100419#ifdef FEAT_MZSCHEME
420 /*
421 * Newer version of MzScheme (Racket) require earlier (trampolined)
422 * initialisation via scheme_main_setup.
423 * Implement this by initialising it as early as possible
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200424 * and splitting off remaining Vim main into vim_main2().
Bram Moolenaar2e4cb3b2017-10-22 21:11:17 +0200425 * Do source startup scripts, so that 'mzschemedll' can be set.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100426 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200427 return mzscheme_main();
428#else
429 return vim_main2();
Bram Moolenaar8866d272012-11-28 15:55:42 +0100430#endif
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200431}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100432#endif /* NO_VIM_MAIN */
Bram Moolenaara357e442016-08-10 20:45:07 +0200433#endif /* PROTO */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100434
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200435/*
436 * vim_main2() is needed for FEAT_MZSCHEME, but we define it always to keep
437 * things simple.
438 * It is also defined when NO_VIM_MAIN is defined, but then it's empty.
439 */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100440 int
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200441vim_main2(void)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100442{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100443#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000444#ifdef FEAT_EVAL
445 /*
446 * Read all the plugin files.
447 * Only when compiled with +eval, since most plugins need it.
448 */
449 if (p_lpl)
450 {
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200451 char_u *rtp_copy = NULL;
452
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200453 /* First add all package directories to 'runtimepath', so that their
454 * autoload directories can be found. Only if not done already with a
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200455 * :packloadall command.
456 * Make a copy of 'runtimepath', so that source_runtime does not use
457 * the pack directories. */
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200458 if (!did_source_packages)
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200459 {
460 rtp_copy = vim_strsave(p_rtp);
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200461 add_pack_start_dirs();
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200462 }
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200463
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200464 source_in_path(rtp_copy == NULL ? p_rtp : rtp_copy,
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000465# ifdef VMS /* Somehow VMS doesn't handle the "**". */
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200466 (char_u *)"plugin/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000467# else
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200468 (char_u *)"plugin/**/*.vim",
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000469# endif
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200470 DIP_ALL | DIP_NOAFTER);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000471 TIME_MSG("loading plugins");
Bram Moolenaar07ecfa62017-06-27 14:43:55 +0200472 vim_free(rtp_copy);
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100473
Bram Moolenaarce876aa2017-06-04 17:47:42 +0200474 /* Only source "start" packages if not done already with a :packloadall
475 * command. */
476 if (!did_source_packages)
477 load_start_packages();
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100478 TIME_MSG("loading packages");
Bram Moolenaar66459b72016-08-06 19:01:55 +0200479
480# ifdef VMS /* Somehow VMS doesn't handle the "**". */
481 source_runtime((char_u *)"plugin/*.vim", DIP_ALL | DIP_AFTER);
482# else
483 source_runtime((char_u *)"plugin/**/*.vim", DIP_ALL | DIP_AFTER);
484# endif
485 TIME_MSG("loading after plugins");
486
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000487 }
488#endif
489
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000490#ifdef FEAT_DIFF
491 /* Decide about window layout for diff mode after reading vimrc. */
492 if (params.diff_mode && params.window_layout == 0)
493 {
494 if (diffopt_horizontal())
495 params.window_layout = WIN_HOR; /* use horizontal split */
496 else
497 params.window_layout = WIN_VER; /* use vertical split */
498 }
499#endif
500
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000501 /*
502 * Recovery mode without a file name: List swap files.
503 * This uses the 'dir' option, therefore it must be after the
504 * initializations.
505 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200506 if (recoverymode && params.fname == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200508 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000509 mch_exit(0);
510 }
511
512 /*
513 * Set a few option defaults after reading .vimrc files:
514 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
515 */
516 set_init_3();
517 TIME_MSG("inits 3");
518
519 /*
520 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
521 * Note that this overrides anything from a vimrc file.
522 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000523 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000524 p_uc = 0;
525
526#ifdef FEAT_FKMAP
527 if (curwin->w_p_rl && p_altkeymap)
528 {
529 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
530# ifdef FEAT_ARABIC
531 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
532# endif
533 p_fkmap = TRUE; /* Set the Farsi keymap mode */
534 }
535#endif
536
537#ifdef FEAT_GUI
538 if (gui.starting)
539 {
540#if defined(UNIX) || defined(VMS)
541 /* When something caused a message from a vimrc script, need to output
542 * an extra newline before the shell prompt. */
543 if (did_emsg || msg_didout)
544 putchar('\n');
545#endif
546
547 gui_start(); /* will set full_screen to TRUE */
548 TIME_MSG("starting GUI");
549
550 /* When running "evim" or "gvim -y" we need the menus, exit if we
551 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000552 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000553 mch_exit(1);
554 }
555#endif
556
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557#ifdef FEAT_VIMINFO
558 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000559 * Read in registers, history etc, but not marks, from the viminfo file.
560 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561 */
562 if (*p_viminfo != NUL)
563 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000564 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000565 TIME_MSG("reading viminfo");
566 }
567#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100568#ifdef FEAT_EVAL
569 /* It's better to make v:oldfiles an empty list than NULL. */
570 if (get_vim_var_list(VV_OLDFILES) == NULL)
571 set_vim_var_list(VV_OLDFILES, list_alloc());
572#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000573
574#ifdef FEAT_QUICKFIX
575 /*
576 * "-q errorfile": Load the error file now.
577 * If the error file can't be read, exit before doing anything else.
578 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000579 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000580 {
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100581 char_u *enc = NULL;
582
583# ifdef FEAT_MBYTE
584 enc = p_menc;
585# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000586 if (params.use_ef != NULL)
587 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000588 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200589 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +0100590 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff, enc) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000591 {
592 out_char('\n');
593 mch_exit(3);
594 }
595 TIME_MSG("reading errorfile");
596 }
597#endif
598
599 /*
600 * Start putting things on the screen.
601 * Scroll screen down before drawing over it
602 * Clear screen now, so file message will not be cleared.
603 */
604 starting = NO_BUFFERS;
605 no_wait_return = FALSE;
606 if (!exmode_active)
607 msg_scroll = FALSE;
608
609#ifdef FEAT_GUI
610 /*
611 * This seems to be required to make callbacks to be called now, instead
612 * of after things have been put on the screen, which then may be deleted
613 * when getting a resize callback.
614 * For the Mac this handles putting files dropped on the Vim icon to
615 * global_alist.
616 */
617 if (gui.in_use)
618 {
619# ifdef FEAT_SUN_WORKSHOP
620 if (!usingSunWorkShop)
621# endif
Bram Moolenaarc9e649a2017-12-18 18:14:47 +0100622 gui_wait_for_chars(50L, typebuf.tb_change_cnt);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000623 TIME_MSG("GUI delay");
624 }
625#endif
626
627#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
628 qnx_clip_init();
629#endif
630
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200631#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
632 clip_init(TRUE);
633#endif
634
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635#ifdef FEAT_XCLIPBOARD
636 /* Start using the X clipboard, unless the GUI was started. */
637# ifdef FEAT_GUI
638 if (!gui.in_use)
639# endif
640 {
641 setup_term_clip();
642 TIME_MSG("setup clipboard");
643 }
644#endif
645
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000646#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000647 /* Prepare for being a Vim server. */
648 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000649#endif
650
651 /*
652 * If "-" argument given: Read file from stdin.
653 * Do this before starting Raw mode, because it may change things that the
654 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
655 * are the same terminal: "cat | vim -".
656 * Using autocommands here may cause trouble...
657 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000658 if (params.edit_type == EDIT_STDIN && !recoverymode)
659 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000660
661#if defined(UNIX) || defined(VMS)
662 /* When switching screens and something caused a message from a vimrc
663 * script, need to output an extra newline on exit. */
664 if ((did_emsg || msg_didout) && *T_TI != NUL)
665 newline_on_exit = TRUE;
666#endif
667
668 /*
669 * When done something that is not allowed or error message call
670 * wait_return. This must be done before starttermcap(), because it may
671 * switch to another screen. It must be done after settmode(TMODE_RAW),
672 * because we want to react on a single key stroke.
673 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000674 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000675 */
676 settmode(TMODE_RAW);
677 TIME_MSG("setting raw mode");
678
679 if (need_wait_return || msg_didany)
680 {
681 wait_return(TRUE);
682 TIME_MSG("waiting for return");
683 }
684
685 starttermcap(); /* start termcap if not done by wait_return() */
686 TIME_MSG("start termcap");
687
688#ifdef FEAT_MOUSE
689 setmouse(); /* may start using the mouse */
690#endif
691 if (scroll_region)
692 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000693 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000694
695 /*
696 * Don't clear the screen when starting in Ex mode, unless using the GUI.
697 */
698 if (exmode_active
699#ifdef FEAT_GUI
700 && !gui.in_use
701#endif
702 )
703 must_redraw = CLEAR;
704 else
705 {
706 screenclear(); /* clear screen */
707 TIME_MSG("clearing screen");
708 }
709
710#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000711 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100713 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200714 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000715 TIME_MSG("getting crypt key");
716 }
717#endif
718
719 no_wait_return = TRUE;
720
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000722 * Create the requested number of windows and edit buffers in them.
723 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000725 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726 TIME_MSG("opening buffers");
727
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000728#ifdef FEAT_EVAL
729 /* clear v:swapcommand */
730 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
731#endif
732
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000733 /* Ex starts at last line of the file */
734 if (exmode_active)
735 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
736
737#ifdef FEAT_AUTOCMD
738 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
739 TIME_MSG("BufEnter autocommands");
740#endif
741 setpcmark();
742
743#ifdef FEAT_QUICKFIX
744 /*
745 * When started with "-q errorfile" jump to first error now.
746 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000747 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000748 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000749 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000750 TIME_MSG("jump to first error");
751 }
752#endif
753
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000754 /*
755 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000756 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000757 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200758 edit_buffers(&params, start_dir);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200759 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000760
761#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000762 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000763 {
764 win_T *wp;
765
766 /* set options in each window for "vimdiff". */
Bram Moolenaar29323592016-07-24 22:04:11 +0200767 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000768 diff_win_options(wp, TRUE);
769 }
770#endif
771
772 /*
773 * Shorten any of the filenames, but only when absolute.
774 */
775 shorten_fnames(FALSE);
776
777 /*
778 * Need to jump to the tag before executing the '-c command'.
779 * Makes "vim -c '/return' -t main" work.
780 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000781 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000782 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000783#if defined(HAS_SWAP_EXISTS_ACTION)
784 swap_exists_did_quit = FALSE;
785#endif
786
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000787 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000788 do_cmdline_cmd(IObuff);
789 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000790
791#if defined(HAS_SWAP_EXISTS_ACTION)
792 /* If the user doesn't want to edit the file then we quit here. */
793 if (swap_exists_did_quit)
794 getout(1);
795#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000796 }
797
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000798 /* Execute any "+", "-c" and "-S" arguments. */
799 if (params.n_commands > 0)
800 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801
Bram Moolenaar6b1da332017-06-09 21:35:47 +0200802 /* Must come before the may_req_ calls. */
803 starting = 0;
804
Bram Moolenaar976787d2017-06-04 15:45:50 +0200805#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
806 /* Must be done before redrawing, puts a few characters on the screen. */
807 may_req_ambiguous_char_width();
808#endif
809
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000810 RedrawingDisabled = 0;
811 redraw_all_later(NOT_VALID);
812 no_wait_return = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813
Bram Moolenaarbaec5c12016-04-06 23:06:23 +0200814 /* 'autochdir' has been postponed */
815 DO_AUTOCHDIR
816
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000817#ifdef FEAT_TERMRESPONSE
818 /* Requesting the termresponse is postponed until here, so that a "-c q"
819 * argument doesn't make it appear in the shell Vim was started from. */
820 may_req_termresponse();
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200821
Bram Moolenaarfc8f1112017-04-18 18:51:35 +0200822 may_req_bg_color();
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000823#endif
824
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000825 /* start in insert mode */
826 if (p_im)
827 need_start_insertmode = TRUE;
828
Bram Moolenaar14735512016-03-26 21:00:08 +0100829#ifdef FEAT_EVAL
830 set_vim_var_nr(VV_VIM_DID_ENTER, 1L);
831#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000832#ifdef FEAT_AUTOCMD
833 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
834 TIME_MSG("VimEnter autocommands");
835#endif
836
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200837#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
838 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
839 * done after the clipboard is available and all initial commands that may
840 * modify the 'clipboard' setting have run; i.e. just before entering the
841 * main loop. */
842 {
843 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100844
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200845 adjust_clip_reg(&default_regname);
846 set_reg_var(default_regname);
847 }
848#endif
849
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
851 /* When a startup script or session file setup for diff'ing and
852 * scrollbind, sync the scrollbind now. */
853 if (curwin->w_p_diff && curwin->w_p_scb)
854 {
855 update_topline();
856 check_scrollbind((linenr_T)0, 0L);
857 TIME_MSG("diff scrollbinding");
858 }
859#endif
860
861#if defined(WIN3264) && !defined(FEAT_GUI_W32)
862 mch_set_winsize_now(); /* Allow winsize changes from now on */
863#endif
864
Bram Moolenaar4033c552017-09-16 20:54:51 +0200865#if defined(FEAT_GUI)
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000866 /* When tab pages were created, may need to update the tab pages line and
867 * scrollbars. This is skipped while creating them. */
868 if (first_tabpage->tp_next != NULL)
869 {
870 out_flush();
871 gui_init_which_components(NULL);
872 gui_update_scrollbars(TRUE);
873 }
874 need_mouse_correct = TRUE;
875#endif
876
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877 /* If ":startinsert" command used, stuff a dummy command to be able to
878 * call normal_cmd(), which will then start Insert mode. */
879 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000880 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881
882#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200883 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200884 {
885# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200886# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200887 && !defined(FEAT_GUI_W32)
888 if (gui.in_use)
889 {
890 mch_errmsg(_("netbeans is not supported with this GUI\n"));
891 mch_exit(2);
892 }
893# endif
894# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000895 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200896 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +0200897 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000898#endif
899
900 TIME_MSG("before starting main loop");
901
902 /*
903 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100904 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000905 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000906
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200907#endif /* NO_VIM_MAIN */
908
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000909 return 0;
910}
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000911
912/*
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200913 * Initialisation shared by main() and some tests.
914 */
915 void
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200916common_init(mparm_T *paramp)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200917{
918
919#ifdef FEAT_MBYTE
920 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
921#endif
922#ifdef FEAT_EVAL
923 eval_init(); /* init global variables */
924#endif
925
926#ifdef __QNXNTO__
927 qnx_init(); /* PhAttach() for clipboard, (and gui) */
928#endif
929
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200930 /* Init the table of Normal mode commands. */
931 init_normal_cmds();
932
933#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
934 make_version(); /* Construct the long version string. */
935#endif
936
937 /*
938 * Allocate space for the generic buffers (needed for set_init_1() and
939 * EMSG2()).
940 */
941 if ((IObuff = alloc(IOSIZE)) == NULL
942 || (NameBuff = alloc(MAXPATHL)) == NULL)
943 mch_exit(0);
944 TIME_MSG("Allocated generic buffers");
945
946#ifdef NBDEBUG
947 /* Wait a moment for debugging NetBeans. Must be after allocating
948 * NameBuff. */
949 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
950 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
951 TIME_MSG("NetBeans debug wait");
952#endif
953
954#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
955 /*
956 * Setup to use the current locale (for ctype() and many other things).
957 * NOTE: Translated messages with encodings other than latin1 will not
958 * work until set_init_1() has been called!
959 */
960 init_locale();
961 TIME_MSG("locale set");
962#endif
963
964#ifdef FEAT_GUI
965 gui.dofork = TRUE; /* default is to use fork() */
966#endif
967
968 /*
969 * Do a first scan of the arguments in "argv[]":
970 * -display or --display
971 * --server...
972 * --socketid
973 * --windowid
974 */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200975 early_arg_scan(paramp);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200976
977#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200978 findYourself(paramp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200979#endif
Bram Moolenaard0573012017-10-28 21:11:06 +0200980#if defined(FEAT_GUI)
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200981 /* Prepare for possibly starting GUI sometime */
Bram Moolenaara8e691d2016-08-07 15:19:26 +0200982 gui_prepare(&paramp->argc, paramp->argv);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200983 TIME_MSG("GUI prepared");
984#endif
985
986#ifdef FEAT_CLIPBOARD
987 clip_init(FALSE); /* Initialise clipboard stuff */
988 TIME_MSG("clipboard setup");
989#endif
990
991 /*
992 * Check if we have an interactive window.
993 * On the Amiga: If there is no window, we open one with a newcli command
994 * (needed for :! to * work). mch_check_win() will also handle the -d or
995 * -dev argument.
996 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +0100997 stdout_isatty = (mch_check_win(paramp->argc, paramp->argv) != FAIL);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +0200998 TIME_MSG("window checked");
999
1000 /*
1001 * Allocate the first window and buffer.
1002 * Can't do anything without it, exit when it fails.
1003 */
1004 if (win_alloc_first() == FAIL)
1005 mch_exit(0);
1006
1007 init_yank(); /* init yank buffers */
1008
1009 alist_init(&global_alist); /* Init the argument list to empty. */
1010 global_alist.id = 0;
1011
1012 /*
1013 * Set the default values for the options.
1014 * NOTE: Non-latin1 translated messages are working only after this,
1015 * because this is where "has_mbyte" will be set, which is used by
1016 * msg_outtrans_len_attr().
1017 * First find out the home directory, needed to expand "~" in options.
1018 */
1019 init_homedir(); /* find real value of $HOME */
1020 set_init_1();
1021 TIME_MSG("inits 1");
1022
1023#ifdef FEAT_EVAL
1024 set_lang_var(); /* set v:lang and v:ctype */
1025#endif
1026}
1027
1028/*
Bram Moolenaar08f88b12017-04-02 17:21:16 +02001029 * Return TRUE when the --not-a-term argument was found.
1030 */
1031 int
1032is_not_a_term()
1033{
1034 return params.not_a_term;
1035}
1036
1037/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038 * Main loop: Execute Normal mode commands until exiting Vim.
1039 * Also used to handle commands in the command-line window, until the window
1040 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001041 * Also used to handle ":visual" command after ":global": execute Normal mode
1042 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043 */
1044 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001045main_loop(
1046 int cmdwin, /* TRUE when working in the command-line window */
1047 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001048{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001049 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001050 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001051#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001052 /* these are static to avoid a compiler warning */
1053 static linenr_T conceal_old_cursor_line = 0;
1054 static linenr_T conceal_new_cursor_line = 0;
1055 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001056#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001057
1058#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1059 /* Setup to catch a terminating error from the X server. Just ignore
1060 * it, restore the state and continue. This might not always work
1061 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001062 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001064 {
1065 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001066 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001067 got_int = TRUE;
1068 need_wait_return = FALSE;
1069 global_busy = FALSE;
1070 exmode_active = 0;
1071 skip_redraw = FALSE;
1072 RedrawingDisabled = 0;
1073 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001074 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001075# ifdef FEAT_EVAL
1076 emsg_skip = 0;
1077# endif
1078 emsg_off = 0;
1079# ifdef FEAT_MOUSE
1080 setmouse();
1081# endif
1082 settmode(TMODE_RAW);
1083 starttermcap();
1084 scroll_start();
1085 redraw_later_clear();
1086 }
1087#endif
1088
1089 clear_oparg(&oa);
1090 while (!cmdwin
1091#ifdef FEAT_CMDWIN
1092 || cmdwin_result == 0
1093#endif
1094 )
1095 {
1096 if (stuff_empty())
1097 {
1098 did_check_timestamps = FALSE;
1099 if (need_check_timestamps)
1100 check_timestamps(FALSE);
1101 if (need_wait_return) /* if wait_return still needed ... */
1102 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001103 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001104 {
1105 need_start_insertmode = FALSE;
1106 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1107 /* skip the fileinfo message now, because it would be shown
1108 * after insert mode finishes! */
1109 need_fileinfo = FALSE;
1110 }
1111 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001112
1113 /* Reset "got_int" now that we got back to the main loop. Except when
1114 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1115 * the ":g" command.
1116 * For ":g/pat/vi" we reset "got_int" when used once. When used
1117 * a second time we go back to Ex mode and abort the ":g" command. */
1118 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001119 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001120 if (noexmode && global_busy && !exmode_active && previous_got_int)
1121 {
1122 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1123 * used and keep "got_int" set, so that it aborts ":g". */
1124 exmode_active = EXMODE_NORMAL;
1125 State = NORMAL;
1126 }
1127 else if (!global_busy || !exmode_active)
1128 {
1129 if (!quit_more)
1130 (void)vgetc(); /* flush all buffers */
1131 got_int = FALSE;
1132 }
1133 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001134 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001135 else
1136 previous_got_int = FALSE;
1137
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001138 if (!exmode_active)
1139 msg_scroll = FALSE;
1140 quit_more = FALSE;
1141
1142 /*
1143 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1144 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1145 * update cursor and redraw.
1146 */
1147 if (skip_redraw || exmode_active)
1148 skip_redraw = FALSE;
1149 else if (do_redraw || stuff_empty())
1150 {
Bram Moolenaar6b40f302017-02-03 22:01:47 +01001151# ifdef FEAT_GUI
1152 /* If ui_breakcheck() was used a resize may have been postponed. */
1153 gui_may_resize_shell();
1154# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001155#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001156 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001157 if (!finish_op && (
1158# ifdef FEAT_AUTOCMD
1159 has_cursormoved()
1160# endif
1161# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1162 ||
1163# endif
1164# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001165 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001166# endif
1167 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001168# ifdef FEAT_AUTOCMD
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001169 && !EQUAL_POS(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001170# endif
1171 )
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001172 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001173# ifdef FEAT_AUTOCMD
1174 if (has_cursormoved())
1175 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1176 FALSE, curbuf);
1177# endif
1178# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001179 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001180 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001181# ifdef FEAT_AUTOCMD
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001182 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001183# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001184 conceal_new_cursor_line = curwin->w_cursor.lnum;
1185 conceal_update_lines = TRUE;
1186 }
1187# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001188# ifdef FEAT_AUTOCMD
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001189 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001190# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001191 }
1192#endif
1193
Bram Moolenaar186628f2013-03-19 13:33:23 +01001194#ifdef FEAT_AUTOCMD
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001195 /* Trigger TextChanged if b:changedtick differs. */
Bram Moolenaar186628f2013-03-19 13:33:23 +01001196 if (!finish_op && has_textchanged()
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001197 && last_changedtick != CHANGEDTICK(curbuf))
Bram Moolenaar186628f2013-03-19 13:33:23 +01001198 {
1199 if (last_changedtick_buf == curbuf)
1200 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1201 FALSE, curbuf);
1202 last_changedtick_buf = curbuf;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001203 last_changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar186628f2013-03-19 13:33:23 +01001204 }
1205#endif
1206
Bram Moolenaar33aec762006-01-22 23:30:12 +00001207#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1208 /* Scroll-binding for diff mode may have been postponed until
1209 * here. Avoids doing it for every change. */
1210 if (diff_need_scrollbind)
1211 {
1212 check_scrollbind((linenr_T)0, 0L);
1213 diff_need_scrollbind = FALSE;
1214 }
1215#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001216#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001217 /* Include a closed fold completely in the Visual area. */
1218 foldAdjustVisual();
1219#endif
1220#ifdef FEAT_FOLDING
1221 /*
1222 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1223 * contain the cursor.
1224 * When 'foldopen' is "all", open the fold(s) under the cursor.
1225 * This may mark the window for redrawing.
1226 */
1227 if (hasAnyFolding(curwin) && !char_avail())
1228 {
1229 foldCheckClose();
1230 if (fdo_flags & FDO_ALL)
1231 foldOpenCursor();
1232 }
1233#endif
1234
1235 /*
1236 * Before redrawing, make sure w_topline is correct, and w_leftcol
1237 * if lines don't wrap, and w_skipcol if lines wrap.
1238 */
1239 update_topline();
1240 validate_cursor();
1241
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001242 if (VIsual_active)
1243 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001244 else if (must_redraw)
Bram Moolenaara338adc2018-01-31 20:51:47 +01001245 {
1246 mch_disable_flush(); /* Stop issuing gui_mch_flush(). */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001247 update_screen(0);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001248 mch_enable_flush();
1249 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001250 else if (redraw_cmdline || clear_cmdline)
1251 showmode();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001252 redraw_statuslines();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253#ifdef FEAT_TITLE
1254 if (need_maketitle)
1255 maketitle();
1256#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001257#ifdef FEAT_VIMINFO
1258 curbuf->b_last_used = vim_time();
1259#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001260 /* display message after redraw */
1261 if (keep_msg != NULL)
1262 {
1263 char_u *p;
1264
1265 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001266 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1267 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001268 p = keep_msg;
1269 msg_attr(p, keep_msg_attr);
1270 vim_free(p);
1271 }
1272 if (need_fileinfo) /* show file info after redraw */
1273 {
1274 fileinfo(FALSE, TRUE, FALSE);
1275 need_fileinfo = FALSE;
1276 }
1277
1278 emsg_on_display = FALSE; /* can delete error message now */
1279 did_emsg = FALSE;
1280 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001281 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001282 showruler(FALSE);
1283
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001284# if defined(FEAT_CONCEAL)
1285 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001286 && (conceal_old_cursor_line != conceal_new_cursor_line
1287 || conceal_cursor_line(curwin)
1288 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001289 {
Bram Moolenaara338adc2018-01-31 20:51:47 +01001290 mch_disable_flush(); /* Stop issuing gui_mch_flush(). */
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001291 if (conceal_old_cursor_line != conceal_new_cursor_line
1292 && conceal_old_cursor_line
1293 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001294 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001295 update_single_line(curwin, conceal_new_cursor_line);
Bram Moolenaara338adc2018-01-31 20:51:47 +01001296 mch_enable_flush();
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001297 curwin->w_valid &= ~VALID_CROW;
1298 }
1299# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001300 setcursor();
1301 cursor_on();
1302
1303 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001304
1305#ifdef STARTUPTIME
1306 /* Now that we have drawn the first screen all the startup stuff
1307 * has been done, close any file for startup messages. */
1308 if (time_fd != NULL)
1309 {
1310 TIME_MSG("first screen update");
1311 TIME_MSG("--- VIM STARTED ---");
1312 fclose(time_fd);
1313 time_fd = NULL;
1314 }
1315#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001316 }
1317#ifdef FEAT_GUI
1318 if (need_mouse_correct)
1319 gui_mouse_correct();
1320#endif
1321
1322 /*
1323 * Update w_curswant if w_set_curswant has been set.
1324 * Postponed until here to avoid computing w_virtcol too often.
1325 */
1326 update_curswant();
1327
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001328#ifdef FEAT_EVAL
1329 /*
1330 * May perform garbage collection when waiting for a character, but
1331 * only at the very toplevel. Otherwise we may be using a List or
1332 * Dict internally somewhere.
1333 * "may_garbage_collect" is reset in vgetc() which is invoked through
1334 * do_exmode() and normal_cmd().
1335 */
1336 may_garbage_collect = (!cmdwin && !noexmode);
1337#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001338 /*
1339 * If we're invoked as ex, do a round of ex commands.
1340 * Otherwise, get and execute a normal mode command.
1341 */
1342 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001343 {
1344 if (noexmode) /* End of ":global/path/visual" commands */
1345 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001346 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001347 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001348 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001349 {
1350#ifdef FEAT_TERMINAL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001351 if (term_use_loop()
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001352 && oa.op_type == OP_NOP && oa.regname == NUL
1353 && !VIsual_active)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001354 {
1355 /* If terminal_loop() returns OK we got a key that is handled
Bram Moolenaar6d819742017-08-06 14:57:49 +02001356 * in Normal model. With FAIL we first need to position the
1357 * cursor and the screen needs to be redrawn. */
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02001358 if (terminal_loop(TRUE) == OK)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001359 normal_cmd(&oa, TRUE);
1360 }
1361 else
Bram Moolenaar938783d2017-07-16 20:13:26 +02001362#endif
Bram Moolenaar423802d2017-07-30 16:52:24 +02001363 normal_cmd(&oa, TRUE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001364 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365 }
1366}
1367
1368
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001369#if defined(USE_XSMP) || defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370/*
1371 * Exit, but leave behind swap files for modified buffers.
1372 */
1373 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001374getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001375{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001376# if defined(SIGHUP) && defined(SIG_IGN)
1377 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1378 * makes Vim exit and then handling SIGHUP causes various reentrance
1379 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001380 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001381# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001382
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383 ml_close_notmod(); /* close all not-modified buffers */
1384 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1385 ml_close_all(FALSE); /* close all memfiles, without deleting */
1386 getout(exitval); /* exit Vim properly */
1387}
1388#endif
1389
1390
Bram Moolenaar6d8d8492016-03-19 14:48:31 +01001391/*
1392 * Exit properly.
1393 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001394 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001395getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396{
1397#ifdef FEAT_AUTOCMD
1398 buf_T *buf;
1399 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001400 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401#endif
1402
1403 exiting = TRUE;
1404
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001405 /* When running in Ex mode an error causes us to exit with a non-zero exit
1406 * code. POSIX requires this, although it's not 100% clear from the
1407 * standard. */
1408 if (exmode_active)
1409 exitval += ex_exitval;
1410
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001411 /* Position the cursor on the last screen line, below all the text */
1412#ifdef FEAT_GUI
1413 if (!gui.in_use)
1414#endif
1415 windgoto((int)Rows - 1, 0);
1416
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001417#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1418 /* Optionally print hashtable efficiency. */
1419 hash_debug_results();
1420#endif
1421
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001422#ifdef FEAT_GUI
1423 msg_didany = FALSE;
1424#endif
1425
1426#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001427 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001428 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001429 /* Trigger BufWinLeave for all windows, but only once per buffer. */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001430 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 next_tp = tp->tp_next;
Bram Moolenaar29323592016-07-24 22:04:11 +02001433 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001434 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001435 if (wp->w_buffer == NULL)
1436 /* Autocmd must have close the buffer already, skip. */
1437 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001438 buf = wp->w_buffer;
Bram Moolenaar95c526e2017-02-25 14:59:34 +01001439 if (CHANGEDTICK(buf) != -1)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001440 {
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001441 bufref_T bufref;
1442
1443 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001444 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1445 buf->b_fname, FALSE, buf);
Bram Moolenaar606d45c2017-12-18 16:21:44 +01001446 if (bufref_valid(&bufref))
1447 CHANGEDTICK(buf) = -1; /* note we did it already */
1448
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001449 /* start all over, autocommands may mess up the lists */
1450 next_tp = first_tabpage;
1451 break;
1452 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001453 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001454 }
Bram Moolenaar33aec762006-01-22 23:30:12 +00001455
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001456 /* Trigger BufUnload for buffers that are loaded */
Bram Moolenaar29323592016-07-24 22:04:11 +02001457 FOR_ALL_BUFFERS(buf)
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001458 if (buf->b_ml.ml_mfp != NULL)
1459 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001460 bufref_T bufref;
1461
1462 set_bufref(&bufref, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001463 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001464 FALSE, buf);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001465 if (!bufref_valid(&bufref))
1466 /* autocmd deleted the buffer */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001467 break;
1468 }
1469 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1470 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001471#endif
1472
1473#ifdef FEAT_VIMINFO
1474 if (*p_viminfo != NUL)
1475 /* Write out the registers, history, marks etc, to the viminfo file */
1476 write_viminfo(NULL, FALSE);
1477#endif
1478
1479#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001480 if (get_vim_var_nr(VV_DYING) <= 1)
1481 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001482#endif
1483
Bram Moolenaar05159a02005-02-26 23:04:13 +00001484#ifdef FEAT_PROFILE
1485 profile_dump();
1486#endif
1487
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001488 if (did_emsg
1489#ifdef FEAT_GUI
1490 || (gui.in_use && msg_didany && p_verbose > 0)
1491#endif
1492 )
1493 {
1494 /* give the user a chance to read the (error) message */
1495 no_wait_return = FALSE;
1496 wait_return(FALSE);
1497 }
1498
1499#ifdef FEAT_AUTOCMD
1500 /* Position the cursor again, the autocommands may have moved it */
1501# ifdef FEAT_GUI
1502 if (!gui.in_use)
1503# endif
1504 windgoto((int)Rows - 1, 0);
1505#endif
1506
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01001507#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar65edff82016-02-21 16:40:11 +01001508 job_stop_on_exit();
1509#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001510#ifdef FEAT_LUA
1511 lua_end();
1512#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001513#ifdef FEAT_MZSCHEME
1514 mzscheme_end();
1515#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001516#ifdef FEAT_TCL
1517 tcl_end();
1518#endif
1519#ifdef FEAT_RUBY
1520 ruby_end();
1521#endif
1522#ifdef FEAT_PYTHON
1523 python_end();
1524#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001525#ifdef FEAT_PYTHON3
1526 python3_end();
1527#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001528#ifdef FEAT_PERL
1529 perl_end();
1530#endif
1531#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1532 iconv_end();
1533#endif
1534#ifdef FEAT_NETBEANS_INTG
1535 netbeans_end();
1536#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001537#ifdef FEAT_CSCOPE
1538 cs_end();
1539#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001540#ifdef FEAT_EVAL
1541 if (garbage_collect_at_exit)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001542 garbage_collect(FALSE);
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001543#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001544#if defined(WIN32) && defined(FEAT_MBYTE)
1545 free_cmd_argsW();
1546#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001547
1548 mch_exit(exitval);
1549}
1550
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001551#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1552/*
1553 * Setup to use the current locale (for ctype() and many other things).
1554 */
1555 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001556init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001557{
1558 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001560# ifdef FEAT_GUI_GTK
1561 /* Tell Gtk not to change our locale settings. */
1562 gtk_disable_setlocale();
1563# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001564# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1565 /* Make sure strtod() uses a decimal point, not a comma. */
1566 setlocale(LC_NUMERIC, "C");
1567# endif
1568
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001569# ifdef WIN32
1570 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1571 * text while it's expecting text in the current locale. This call avoids
1572 * that. */
1573 setlocale(LC_CTYPE, "C");
1574# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001575
1576# ifdef FEAT_GETTEXT
1577 {
1578 int mustfree = FALSE;
1579 char_u *p;
1580
1581# ifdef DYNAMIC_GETTEXT
1582 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001583 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001584# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001585 /* expand_env() doesn't work yet, because g_chartab[] is not
1586 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001587 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1588 if (p != NULL && *p != NUL)
1589 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001590 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001591 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1592 }
1593 if (mustfree)
1594 vim_free(p);
1595 textdomain(VIMPACKAGE);
1596 }
1597# endif
1598}
1599#endif
1600
1601/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001602 * Get the name of the display, before gui_prepare() removes it from
1603 * argv[]. Used for the xterm-clipboard display.
1604 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001605 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001606 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001607 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001608early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001609{
Bram Moolenaar03005972008-11-20 13:12:36 +00001610#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1611 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001612 int argc = parmp->argc;
1613 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001614 int i;
1615
1616 for (i = 1; i < argc; i++)
1617 {
1618 if (STRCMP(argv[i], "--") == 0)
1619 break;
1620# ifdef FEAT_XCLIPBOARD
1621 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001622# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001623 || STRICMP(argv[i], "--display") == 0
1624# endif
1625 )
1626 {
1627 if (i == argc - 1)
1628 mainerr_arg_missing((char_u *)argv[i]);
1629 xterm_display = argv[++i];
1630 }
1631# endif
1632# ifdef FEAT_CLIENTSERVER
1633 else if (STRICMP(argv[i], "--servername") == 0)
1634 {
1635 if (i == argc - 1)
1636 mainerr_arg_missing((char_u *)argv[i]);
1637 parmp->serverName_arg = (char_u *)argv[++i];
1638 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001639 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001640 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001641 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001642 {
1643 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001644# ifdef FEAT_GUI
1645 if (strstr(argv[i], "-wait") != 0)
1646 /* don't fork() when starting the GUI to edit files ourself */
1647 gui.dofork = FALSE;
1648# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001649 }
1650# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001651
1652# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1653# ifdef FEAT_GUI_W32
1654 else if (STRICMP(argv[i], "--windowid") == 0)
1655# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001656 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001657# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001658 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001659 long_u id;
1660 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001661
1662 if (i == argc - 1)
1663 mainerr_arg_missing((char_u *)argv[i]);
1664 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001665 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001666 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001667 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001668 if (count != 1)
1669 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1670 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001671# ifdef FEAT_GUI_W32
1672 win_socket_id = id;
1673# else
1674 gtk_socket_id = id;
1675# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001676 i++;
1677 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001678# endif
1679# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001680 else if (STRICMP(argv[i], "--echo-wid") == 0)
1681 echo_wid_arg = TRUE;
1682# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001683# ifndef FEAT_NETBEANS_INTG
1684 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001685 {
1686 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1687 mch_exit(2);
1688 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001689# endif
1690
Bram Moolenaar58d98232005-07-23 22:25:46 +00001691 }
1692#endif
1693}
1694
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001695#ifndef NO_VIM_MAIN
1696/*
1697 * Get a (optional) count for a Vim argument.
1698 */
1699 static int
1700get_number_arg(
1701 char_u *p, /* pointer to argument */
1702 int *idx, /* index in argument, is incremented */
1703 int def) /* default value */
1704{
1705 if (vim_isdigit(p[*idx]))
1706 {
1707 def = atoi((char *)&(p[*idx]));
1708 while (vim_isdigit(p[*idx]))
1709 *idx = *idx + 1;
1710 }
1711 return def;
1712}
1713
1714/*
1715 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1716 * If the executable name starts with "r" we disable shell commands.
1717 * If the next character is "e" we run in Easy mode.
1718 * If the next character is "g" we run the GUI version.
1719 * If the next characters are "view" we start in readonly mode.
1720 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1721 * If the next characters are "ex" we start in Ex mode. If it's followed
1722 * by "im" use improved Ex mode.
1723 */
1724 static void
1725parse_command_name(mparm_T *parmp)
1726{
1727 char_u *initstr;
1728
1729 initstr = gettail((char_u *)parmp->argv[0]);
1730
Bram Moolenaard0573012017-10-28 21:11:06 +02001731#ifdef FEAT_GUI_MAC
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001732 /* An issue has been seen when launching Vim in such a way that
1733 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1734 * executable or a symbolic link of it. Until this issue is resolved
1735 * we prohibit the GUI from being used.
1736 */
1737 if (STRCMP(initstr, parmp->argv[0]) == 0)
1738 disallow_gui = TRUE;
1739
1740 /* TODO: On MacOS X default to gui if argv[0] ends in:
1741 * /Vim.app/Contents/MacOS/Vim */
1742#endif
1743
1744#ifdef FEAT_EVAL
1745 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaar08cab962017-03-04 14:37:18 +01001746 set_progpath((char_u *)parmp->argv[0]);
Bram Moolenaar502ae4b2016-07-16 19:50:13 +02001747#endif
1748
1749 if (TOLOWER_ASC(initstr[0]) == 'r')
1750 {
1751 restricted = TRUE;
1752 ++initstr;
1753 }
1754
1755 /* Use evim mode for "evim" and "egvim", not for "editor". */
1756 if (TOLOWER_ASC(initstr[0]) == 'e'
1757 && (TOLOWER_ASC(initstr[1]) == 'v'
1758 || TOLOWER_ASC(initstr[1]) == 'g'))
1759 {
1760#ifdef FEAT_GUI
1761 gui.starting = TRUE;
1762#endif
1763 parmp->evim_mode = TRUE;
1764 ++initstr;
1765 }
1766
1767 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1768 if (TOLOWER_ASC(initstr[0]) == 'g')
1769 {
1770 main_start_gui();
1771#ifdef FEAT_GUI
1772 ++initstr;
1773#endif
1774 }
1775
1776 if (STRNICMP(initstr, "view", 4) == 0)
1777 {
1778 readonlymode = TRUE;
1779 curbuf->b_p_ro = TRUE;
1780 p_uc = 10000; /* don't update very often */
1781 initstr += 4;
1782 }
1783 else if (STRNICMP(initstr, "vim", 3) == 0)
1784 initstr += 3;
1785
1786 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1787 if (STRICMP(initstr, "diff") == 0)
1788 {
1789#ifdef FEAT_DIFF
1790 parmp->diff_mode = TRUE;
1791#else
1792 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1793 mch_errmsg("\n");
1794 mch_exit(2);
1795#endif
1796 }
1797
1798 if (STRNICMP(initstr, "ex", 2) == 0)
1799 {
1800 if (STRNICMP(initstr + 2, "im", 2) == 0)
1801 exmode_active = EXMODE_VIM;
1802 else
1803 exmode_active = EXMODE_NORMAL;
1804 change_compatible(TRUE); /* set 'compatible' */
1805 }
1806}
1807
Bram Moolenaar58d98232005-07-23 22:25:46 +00001808/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001809 * Scan the command line arguments.
1810 */
1811 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001812command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001813{
1814 int argc = parmp->argc;
1815 char **argv = parmp->argv;
1816 int argv_idx; /* index in argv[n][] */
1817 int had_minmin = FALSE; /* found "--" argument */
1818 int want_argument; /* option argument with argument */
1819 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001820 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001821 long n;
1822
1823 --argc;
1824 ++argv;
1825 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1826 while (argc > 0)
1827 {
1828 /*
1829 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1830 */
1831 if (argv[0][0] == '+' && !had_minmin)
1832 {
1833 if (parmp->n_commands >= MAX_ARG_CMDS)
1834 mainerr(ME_EXTRA_CMD, NULL);
1835 argv_idx = -1; /* skip to next argument */
1836 if (argv[0][1] == NUL)
1837 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1838 else
1839 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1840 }
1841
1842 /*
1843 * Optional argument.
1844 */
1845 else if (argv[0][0] == '-' && !had_minmin)
1846 {
1847 want_argument = FALSE;
1848 c = argv[0][argv_idx++];
1849#ifdef VMS
1850 /*
1851 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1852 * and "-/X" as "-X".
1853 */
1854 if (c == '/')
1855 {
1856 c = argv[0][argv_idx++];
1857 c = TOUPPER_ASC(c);
1858 }
1859 else
1860 c = TOLOWER_ASC(c);
1861#endif
1862 switch (c)
1863 {
1864 case NUL: /* "vim -" read from stdin */
1865 /* "ex -" silent mode */
1866 if (exmode_active)
1867 silent_mode = TRUE;
1868 else
1869 {
1870 if (parmp->edit_type != EDIT_NONE)
1871 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1872 parmp->edit_type = EDIT_STDIN;
1873 read_cmd_fd = 2; /* read from stderr instead of stdin */
1874 }
1875 argv_idx = -1; /* skip to next argument */
1876 break;
1877
1878 case '-': /* "--" don't take any more option arguments */
1879 /* "--help" give help message */
1880 /* "--version" give version message */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001881 /* "--clean" clean context */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001882 /* "--literal" take files literally */
1883 /* "--nofork" don't fork */
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001884 /* "--not-a-term" don't warn for not a term */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001885 /* "--ttyfail" exit if not a term */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001886 /* "--noplugin[s]" skip plugins */
1887 /* "--cmd <cmd>" execute cmd before vimrc */
1888 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1889 usage();
1890 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1891 {
1892 Columns = 80; /* need to init Columns */
1893 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1894 list_version();
1895 msg_putchar('\n');
1896 msg_didout = FALSE;
1897 mch_exit(0);
1898 }
Bram Moolenaarc4da1132017-07-15 19:39:43 +02001899 else if (STRNICMP(argv[0] + argv_idx, "clean", 5) == 0)
1900 {
1901 parmp->use_vimrc = (char_u *)"DEFAULTS";
1902 set_option_value((char_u *)"vif", 0L, (char_u *)"NONE", 0);
1903 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001904 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1905 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001906#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001907 parmp->literal = TRUE;
1908#endif
1909 }
1910 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1911 {
1912#ifdef FEAT_GUI
1913 gui.dofork = FALSE; /* don't fork() when starting GUI */
1914#endif
1915 }
1916 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1917 p_lpl = FALSE;
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01001918 else if (STRNICMP(argv[0] + argv_idx, "not-a-term", 10) == 0)
1919 parmp->not_a_term = TRUE;
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01001920 else if (STRNICMP(argv[0] + argv_idx, "ttyfail", 7) == 0)
1921 parmp->tty_fail = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001922 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1923 {
1924 want_argument = TRUE;
1925 argv_idx += 3;
1926 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001927 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1928 {
1929 want_argument = TRUE;
1930 argv_idx += 11;
1931 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001932#ifdef FEAT_CLIENTSERVER
1933 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1934 ; /* already processed -- no arg */
1935 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1936 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1937 {
1938 /* already processed -- snatch the following arg */
1939 if (argc > 1)
1940 {
1941 --argc;
1942 ++argv;
1943 }
1944 }
1945#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001946#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1947# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001948 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001949# else
1950 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1951# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001952 {
1953 /* already processed -- snatch the following arg */
1954 if (argc > 1)
1955 {
1956 --argc;
1957 ++argv;
1958 }
1959 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001960#endif
1961#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001962 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1963 {
1964 /* already processed, skip */
1965 }
1966#endif
1967 else
1968 {
1969 if (argv[0][argv_idx])
1970 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1971 had_minmin = TRUE;
1972 }
1973 if (!want_argument)
1974 argv_idx = -1; /* skip to next argument */
1975 break;
1976
1977 case 'A': /* "-A" start in Arabic mode */
1978#ifdef FEAT_ARABIC
1979 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1980#else
1981 mch_errmsg(_(e_noarabic));
1982 mch_exit(2);
1983#endif
1984 break;
1985
1986 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001987 /* Needs to be effective before expanding file names, because
1988 * for Win32 this makes us edit a shortcut file itself,
1989 * instead of the file it links to. */
1990 set_options_bin(curbuf->b_p_bin, 1, 0);
1991 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001992 break;
1993
1994 case 'C': /* "-C" Compatible */
1995 change_compatible(TRUE);
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02001996 has_dash_c_arg = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001997 break;
1998
1999 case 'e': /* "-e" Ex mode */
2000 exmode_active = EXMODE_NORMAL;
2001 break;
2002
2003 case 'E': /* "-E" Improved Ex mode */
2004 exmode_active = EXMODE_VIM;
2005 break;
2006
2007 case 'f': /* "-f" GUI: run in foreground. Amiga: open
2008 window directly, not with newcli */
2009#ifdef FEAT_GUI
2010 gui.dofork = FALSE; /* don't fork() when starting GUI */
2011#endif
2012 break;
2013
2014 case 'g': /* "-g" start GUI */
2015 main_start_gui();
2016 break;
2017
2018 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
2019#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002020 p_fkmap = TRUE;
2021 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002022#else
2023 mch_errmsg(_(e_nofarsi));
2024 mch_exit(2);
2025#endif
2026 break;
2027
2028 case 'h': /* "-h" give help message */
2029#ifdef FEAT_GUI_GNOME
2030 /* Tell usage() to exit for "gvim". */
2031 gui.starting = FALSE;
2032#endif
2033 usage();
2034 break;
2035
2036 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2037#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002038 p_hkmap = TRUE;
2039 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002040#else
2041 mch_errmsg(_(e_nohebrew));
2042 mch_exit(2);
2043#endif
2044 break;
2045
2046 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2047#ifdef FEAT_LISP
2048 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2049 p_sm = TRUE;
2050#endif
2051 break;
2052
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002053 case 'M': /* "-M" no changes or writing of files */
2054 reset_modifiable();
2055 /* FALLTHROUGH */
2056
2057 case 'm': /* "-m" no writing of files */
2058 p_write = FALSE;
2059 break;
2060
2061 case 'y': /* "-y" easy mode */
2062#ifdef FEAT_GUI
2063 gui.starting = TRUE; /* start GUI a bit later */
2064#endif
2065 parmp->evim_mode = TRUE;
2066 break;
2067
2068 case 'N': /* "-N" Nocompatible */
2069 change_compatible(FALSE);
2070 break;
2071
2072 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002073#ifdef FEAT_NETBEANS_INTG
2074 /* checking for "-nb", netbeans parameters */
2075 if (argv[0][argv_idx] == 'b')
2076 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002077 netbeansArg = argv[0];
2078 argv_idx = -1; /* skip to next argument */
2079 }
2080 else
2081#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002082 parmp->no_swap_file = TRUE;
2083 break;
2084
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002085 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002086#ifdef TARGET_API_MAC_OSX
2087 /* For some reason on MacOS X, an argument like:
2088 -psn_0_10223617 is passed in when invoke from Finder
2089 or with the 'open' command */
2090 if (argv[0][argv_idx] == 's')
2091 {
2092 argv_idx = -1; /* bypass full -psn */
2093 main_start_gui();
2094 break;
2095 }
2096#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002097 /* default is 0: open window for each file */
2098 parmp->window_count = get_number_arg((char_u *)argv[0],
2099 &argv_idx, 0);
2100 parmp->window_layout = WIN_TABS;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002101 break;
2102
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002103 case 'o': /* "-o[N]" open N horizontal split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002104 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002105 parmp->window_count = get_number_arg((char_u *)argv[0],
2106 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002107 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002108 break;
2109
2110 case 'O': /* "-O[N]" open N vertical split windows */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002111 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002112 parmp->window_count = get_number_arg((char_u *)argv[0],
2113 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002114 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002115 break;
2116
2117#ifdef FEAT_QUICKFIX
2118 case 'q': /* "-q" QuickFix mode */
2119 if (parmp->edit_type != EDIT_NONE)
2120 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2121 parmp->edit_type = EDIT_QF;
2122 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2123 {
2124 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2125 argv_idx = -1;
2126 }
2127 else if (argc > 1) /* "-q {errorfile}" */
2128 want_argument = TRUE;
2129 break;
2130#endif
2131
2132 case 'R': /* "-R" readonly mode */
2133 readonlymode = TRUE;
2134 curbuf->b_p_ro = TRUE;
2135 p_uc = 10000; /* don't update very often */
2136 break;
2137
2138 case 'r': /* "-r" recovery mode */
2139 case 'L': /* "-L" recovery mode */
2140 recoverymode = 1;
2141 break;
2142
2143 case 's':
2144 if (exmode_active) /* "-s" silent (batch) mode */
2145 silent_mode = TRUE;
2146 else /* "-s {scriptin}" read from script file */
2147 want_argument = TRUE;
2148 break;
2149
2150 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2151 if (parmp->edit_type != EDIT_NONE)
2152 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2153 parmp->edit_type = EDIT_TAG;
2154 if (argv[0][argv_idx]) /* "-t{tag}" */
2155 {
2156 parmp->tagname = (char_u *)argv[0] + argv_idx;
2157 argv_idx = -1;
2158 }
2159 else /* "-t {tag}" */
2160 want_argument = TRUE;
2161 break;
2162
2163#ifdef FEAT_EVAL
2164 case 'D': /* "-D" Debugging */
2165 parmp->use_debug_break_level = 9999;
2166 break;
2167#endif
2168#ifdef FEAT_DIFF
2169 case 'd': /* "-d" 'diff' */
2170# ifdef AMIGA
2171 /* check for "-dev {device}" */
2172 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2173 want_argument = TRUE;
2174 else
2175# endif
2176 parmp->diff_mode = TRUE;
2177 break;
2178#endif
2179 case 'V': /* "-V{N}" Verbose level */
2180 /* default is 10: a little bit verbose */
2181 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2182 if (argv[0][argv_idx] != NUL)
2183 {
2184 set_option_value((char_u *)"verbosefile", 0L,
2185 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002186 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002187 }
2188 break;
2189
2190 case 'v': /* "-v" Vi-mode (as if called "vi") */
2191 exmode_active = 0;
2192#ifdef FEAT_GUI
2193 gui.starting = FALSE; /* don't start GUI */
2194#endif
2195 break;
2196
2197 case 'w': /* "-w{number}" set window height */
2198 /* "-w {scriptout}" write to script */
2199 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2200 {
2201 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2202 set_option_value((char_u *)"window", n, NULL, 0);
2203 break;
2204 }
2205 want_argument = TRUE;
2206 break;
2207
2208#ifdef FEAT_CRYPT
2209 case 'x': /* "-x" encrypted reading/writing of files */
2210 parmp->ask_for_key = TRUE;
2211 break;
2212#endif
2213
2214 case 'X': /* "-X" don't connect to X server */
2215#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2216 x_no_connect = TRUE;
2217#endif
2218 break;
2219
2220 case 'Z': /* "-Z" restricted mode */
2221 restricted = TRUE;
2222 break;
2223
2224 case 'c': /* "-c{command}" or "-c {command}" execute
2225 command */
2226 if (argv[0][argv_idx] != NUL)
2227 {
2228 if (parmp->n_commands >= MAX_ARG_CMDS)
2229 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002230 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2231 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002232 argv_idx = -1;
2233 break;
2234 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002235 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002236 case 'S': /* "-S {file}" execute Vim script */
2237 case 'i': /* "-i {viminfo}" use for viminfo */
2238#ifndef FEAT_DIFF
2239 case 'd': /* "-d {device}" device (for Amiga) */
2240#endif
2241 case 'T': /* "-T {terminal}" terminal name */
2242 case 'u': /* "-u {vimrc}" vim inits file */
2243 case 'U': /* "-U {gvimrc}" gvim inits file */
2244 case 'W': /* "-W {scriptout}" overwrite */
2245#ifdef FEAT_GUI_W32
2246 case 'P': /* "-P {parent title}" MDI parent */
2247#endif
2248 want_argument = TRUE;
2249 break;
2250
2251 default:
2252 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2253 }
2254
2255 /*
2256 * Handle option arguments with argument.
2257 */
2258 if (want_argument)
2259 {
2260 /*
2261 * Check for garbage immediately after the option letter.
2262 */
2263 if (argv[0][argv_idx] != NUL)
2264 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2265
2266 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002267 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002268 mainerr_arg_missing((char_u *)argv[0]);
2269 ++argv;
2270 argv_idx = -1;
2271
2272 switch (c)
2273 {
2274 case 'c': /* "-c {command}" execute command */
2275 case 'S': /* "-S {file}" execute Vim script */
2276 if (parmp->n_commands >= MAX_ARG_CMDS)
2277 mainerr(ME_EXTRA_CMD, NULL);
2278 if (c == 'S')
2279 {
2280 char *a;
2281
2282 if (argc < 1)
2283 /* "-S" without argument: use default session file
2284 * name. */
2285 a = SESSION_FILE;
2286 else if (argv[0][0] == '-')
2287 {
2288 /* "-S" followed by another option: use default
2289 * session file name. */
2290 a = SESSION_FILE;
2291 ++argc;
2292 --argv;
2293 }
2294 else
2295 a = argv[0];
2296 p = alloc((unsigned)(STRLEN(a) + 4));
2297 if (p == NULL)
2298 mch_exit(2);
2299 sprintf((char *)p, "so %s", a);
2300 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2301 parmp->commands[parmp->n_commands++] = p;
2302 }
2303 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002304 parmp->commands[parmp->n_commands++] =
2305 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002306 break;
2307
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002308 case '-':
2309 if (argv[-1][2] == 'c')
2310 {
2311 /* "--cmd {command}" execute command */
2312 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2313 mainerr(ME_EXTRA_CMD, NULL);
2314 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002315 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002316 }
2317 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002318 break;
2319
2320 /* case 'd': -d {device} is handled in mch_check_win() for the
2321 * Amiga */
2322
2323#ifdef FEAT_QUICKFIX
2324 case 'q': /* "-q {errorfile}" QuickFix mode */
2325 parmp->use_ef = (char_u *)argv[0];
2326 break;
2327#endif
2328
2329 case 'i': /* "-i {viminfo}" use for viminfo */
Bram Moolenaarc4da1132017-07-15 19:39:43 +02002330 set_option_value((char_u *)"vif", 0L, (char_u *)argv[0], 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002331 break;
2332
2333 case 's': /* "-s {scriptin}" read from script file */
2334 if (scriptin[0] != NULL)
2335 {
2336scripterror:
2337 mch_errmsg(_("Attempt to open script file again: \""));
2338 mch_errmsg(argv[-1]);
2339 mch_errmsg(" ");
2340 mch_errmsg(argv[0]);
2341 mch_errmsg("\"\n");
2342 mch_exit(2);
2343 }
2344 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2345 {
2346 mch_errmsg(_("Cannot open for reading: \""));
2347 mch_errmsg(argv[0]);
2348 mch_errmsg("\"\n");
2349 mch_exit(2);
2350 }
2351 if (save_typebuf() == FAIL)
2352 mch_exit(2); /* out of memory */
2353 break;
2354
2355 case 't': /* "-t {tag}" */
2356 parmp->tagname = (char_u *)argv[0];
2357 break;
2358
2359 case 'T': /* "-T {terminal}" terminal name */
2360 /*
2361 * The -T term argument is always available and when
2362 * HAVE_TERMLIB is supported it overrides the environment
2363 * variable TERM.
2364 */
2365#ifdef FEAT_GUI
2366 if (term_is_gui((char_u *)argv[0]))
2367 gui.starting = TRUE; /* start GUI a bit later */
2368 else
2369#endif
2370 parmp->term = (char_u *)argv[0];
2371 break;
2372
2373 case 'u': /* "-u {vimrc}" vim inits file */
2374 parmp->use_vimrc = (char_u *)argv[0];
2375 break;
2376
2377 case 'U': /* "-U {gvimrc}" gvim inits file */
2378#ifdef FEAT_GUI
2379 use_gvimrc = (char_u *)argv[0];
2380#endif
2381 break;
2382
2383 case 'w': /* "-w {nr}" 'window' value */
2384 /* "-w {scriptout}" append to script file */
2385 if (vim_isdigit(*((char_u *)argv[0])))
2386 {
2387 argv_idx = 0;
2388 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2389 set_option_value((char_u *)"window", n, NULL, 0);
2390 argv_idx = -1;
2391 break;
2392 }
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002393 /* FALLTHROUGH */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002394 case 'W': /* "-W {scriptout}" overwrite script file */
2395 if (scriptout != NULL)
2396 goto scripterror;
2397 if ((scriptout = mch_fopen(argv[0],
2398 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2399 {
2400 mch_errmsg(_("Cannot open for script output: \""));
2401 mch_errmsg(argv[0]);
2402 mch_errmsg("\"\n");
2403 mch_exit(2);
2404 }
2405 break;
2406
2407#ifdef FEAT_GUI_W32
2408 case 'P': /* "-P {parent title}" MDI parent */
2409 gui_mch_set_parent(argv[0]);
2410 break;
2411#endif
2412 }
2413 }
2414 }
2415
2416 /*
2417 * File name argument.
2418 */
2419 else
2420 {
2421 argv_idx = -1; /* skip to next argument */
2422
2423 /* Check for only one type of editing. */
2424 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2425 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2426 parmp->edit_type = EDIT_FILE;
2427
2428#ifdef MSWIN
2429 /* Remember if the argument was a full path before changing
2430 * slashes to backslashes. */
2431 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2432 parmp->full_path = TRUE;
2433#endif
2434
2435 /* Add the file to the global argument list. */
2436 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2437 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2438 mch_exit(2);
2439#ifdef FEAT_DIFF
2440 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2441 && !mch_isdir(alist_name(&GARGLIST[0])))
2442 {
2443 char_u *r;
2444
2445 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2446 if (r != NULL)
2447 {
2448 vim_free(p);
2449 p = r;
2450 }
2451 }
2452#endif
2453#if defined(__CYGWIN32__) && !defined(WIN32)
2454 /*
2455 * If vim is invoked by non-Cygwin tools, convert away any
2456 * DOS paths, so things like .swp files are created correctly.
2457 * Look for evidence of non-Cygwin paths before we bother.
2458 * This is only for when using the Unix files.
2459 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002460 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002461 {
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002462 char posix_path[MAXPATHL];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002463
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002464# if CYGWIN_VERSION_DLL_MAJOR >= 1007
Bram Moolenaara9f8ee02017-08-14 23:40:45 +02002465 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, MAXPATHL);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002466# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002467 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002468# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002470 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002471 if (p == NULL)
2472 mch_exit(2);
2473 }
2474#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002475
2476#ifdef USE_FNAME_CASE
2477 /* Make the case of the file name match the actual file. */
2478 fname_case(p, 0);
2479#endif
2480
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002481 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002482#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002483 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002484#else
2485 2 /* add buffer number now and use curbuf */
2486#endif
2487 );
2488
2489#if defined(FEAT_MBYTE) && defined(WIN32)
2490 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002491 /* Remember this argument has been added to the argument list.
2492 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002493 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002494# ifdef FEAT_DIFF
2495 parmp->diff_mode
2496# else
2497 FALSE
2498# endif
2499 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002500 }
2501#endif
2502 }
2503
2504 /*
2505 * If there are no more letters after the current "-", go to next
2506 * argument. argv_idx is set to -1 when the current argument is to be
2507 * skipped.
2508 */
2509 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2510 {
2511 --argc;
2512 ++argv;
2513 argv_idx = 1;
2514 }
2515 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002516
2517#ifdef FEAT_EVAL
2518 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2519 * one. */
2520 if (parmp->n_commands > 0)
2521 {
2522 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2523 if (p != NULL)
2524 {
2525 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2526 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2527 vim_free(p);
2528 }
2529 }
2530#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002531}
2532
2533/*
2534 * Print a warning if stdout is not a terminal.
2535 * When starting in Ex mode and commands come from a file, set Silent mode.
2536 */
2537 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002538check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002539{
2540 int input_isatty; /* is active input a terminal? */
2541
2542 input_isatty = mch_input_isatty();
2543 if (exmode_active)
2544 {
2545 if (!input_isatty)
2546 silent_mode = TRUE;
2547 }
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002548 else if (parmp->want_full_screen && (!stdout_isatty || !input_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002549#ifdef FEAT_GUI
2550 /* don't want the delay when started from the desktop */
2551 && !gui.starting
2552#endif
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01002553 && !parmp->not_a_term)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002554 {
2555#ifdef NBDEBUG
2556 /*
2557 * This shouldn't be necessary. But if I run netbeans with the log
2558 * output coming to the console and XOpenDisplay fails, I get vim
2559 * trying to start with input/output to my console tty. This fills my
2560 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002561 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562 */
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002563 if (netbeans_active() && (!stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002564 {
2565 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2566 exit(1);
2567 }
2568#endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002569#if defined(WIN3264) && !defined(FEAT_GUI_W32)
2570 if (is_cygpty_used())
2571 {
Bram Moolenaar2a027452017-09-26 19:10:37 +02002572# if defined(FEAT_MBYTE) && defined(HAVE_BIND_TEXTDOMAIN_CODESET) \
2573 && defined(FEAT_GETTEXT)
2574 char *s, *tofree = NULL;
2575
2576 /* Set the encoding of the error message based on $LC_ALL or
2577 * other environment variables instead of 'encoding'.
2578 * Note that the message is shown on a Cygwin terminal (e.g.
2579 * mintty) which encoding is based on $LC_ALL or etc., not the
2580 * current codepage used by normal Win32 console programs. */
Bram Moolenaar9cf39cc2017-09-27 21:46:19 +02002581 tofree = s = (char *)enc_locale_env(NULL);
Bram Moolenaar2a027452017-09-26 19:10:37 +02002582 if (s == NULL)
2583 s = "utf-8"; /* Use "utf-8" by default. */
2584 (void)bind_textdomain_codeset(VIMPACKAGE, s);
2585 vim_free(tofree);
2586# endif
Bram Moolenaar97ff9b92016-06-26 20:37:46 +02002587 mch_errmsg(_("Vim: Error: This version of Vim does not run in a Cygwin terminal\n"));
2588 exit(1);
2589 }
2590#endif
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002591 if (!stdout_isatty)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2593 if (!input_isatty)
2594 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2595 out_flush();
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01002596 if (parmp->tty_fail && (!stdout_isatty || !input_isatty))
2597 exit(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002598 if (scriptin[0] == NULL)
2599 ui_delay(2000L, TRUE);
2600 TIME_MSG("Warning delay");
2601 }
2602}
2603
2604/*
2605 * Read text from stdin.
2606 */
2607 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002608read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002609{
2610 int i;
2611
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002612#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002613 /* When getting the ATTENTION prompt here, use a dialog */
2614 swap_exists_action = SEA_DIALOG;
2615#endif
2616 no_wait_return = TRUE;
2617 i = msg_didany;
2618 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002619 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002620 no_wait_return = FALSE;
2621 msg_didany = i;
2622 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002623#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002624 check_swap_exists_action();
2625#endif
Bram Moolenaard0573012017-10-28 21:11:06 +02002626#if !(defined(AMIGA) || defined(MACOS_X))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002627 /*
2628 * Close stdin and dup it from stderr. Required for GPM to work
2629 * properly, and for running external commands.
2630 * Is there any other system that cannot do this?
2631 */
2632 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002633 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002634#endif
2635}
2636
2637/*
2638 * Create the requested number of windows and edit buffers in them.
2639 * Also does recovery if "recoverymode" set.
2640 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002641 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002642create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002643{
Bram Moolenaar89d40322006-08-29 15:30:07 +00002644 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002645 int done = 0;
2646
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002647 /*
2648 * Create the number of windows that was requested.
2649 */
2650 if (parmp->window_count == -1) /* was not set */
2651 parmp->window_count = 1;
2652 if (parmp->window_count == 0)
2653 parmp->window_count = GARGCOUNT;
2654 if (parmp->window_count > 1)
2655 {
2656 /* Don't change the windows if there was a command in .vimrc that
2657 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002658 if (parmp->window_layout == 0)
2659 parmp->window_layout = WIN_HOR;
2660 if (parmp->window_layout == WIN_TABS)
2661 {
2662 parmp->window_count = make_tabpages(parmp->window_count);
2663 TIME_MSG("making tab pages");
2664 }
2665 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002666 {
2667 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002668 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002669 TIME_MSG("making windows");
2670 }
2671 else
2672 parmp->window_count = win_count();
2673 }
2674 else
2675 parmp->window_count = 1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002676
2677 if (recoverymode) /* do recover */
2678 {
2679 msg_scroll = TRUE; /* scroll message up */
2680 ml_recover();
2681 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2682 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002683 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002684 }
2685 else
2686 {
2687 /*
2688 * Open a buffer for windows that don't have one yet.
2689 * Commands in the .vimrc might have loaded a file or split the window.
2690 * Watch out for autocommands that delete a window.
2691 */
2692#ifdef FEAT_AUTOCMD
2693 /*
2694 * Don't execute Win/Buf Enter/Leave autocommands here
2695 */
2696 ++autocmd_no_enter;
2697 ++autocmd_no_leave;
2698#endif
Bram Moolenaar89d40322006-08-29 15:30:07 +00002699 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002700 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002701 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002702 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002703 {
2704 if (parmp->window_layout == WIN_TABS)
2705 goto_tabpage(1);
2706 else
2707 curwin = firstwin;
2708 }
2709 else if (parmp->window_layout == WIN_TABS)
2710 {
2711 if (curtab->tp_next == NULL)
2712 break;
2713 goto_tabpage(0);
2714 }
2715 else
2716 {
2717 if (curwin->w_next == NULL)
2718 break;
2719 curwin = curwin->w_next;
2720 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002721 dorewind = FALSE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002722 curbuf = curwin->w_buffer;
2723 if (curbuf->b_ml.ml_mfp == NULL)
2724 {
2725#ifdef FEAT_FOLDING
2726 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2727 if (p_fdls >= 0)
2728 curwin->w_p_fdl = p_fdls;
2729#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002730#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002731 /* When getting the ATTENTION prompt here, use a dialog */
2732 swap_exists_action = SEA_DIALOG;
2733#endif
2734 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002735
2736 /* create memfile, read file */
2737 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002738
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002739#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002740 if (swap_exists_action == SEA_QUIT)
2741 {
2742 if (got_int || only_one_window())
2743 {
2744 /* abort selected or quit and only one window */
2745 did_emsg = FALSE; /* avoid hit-enter prompt */
2746 getout(1);
2747 }
2748 /* We can't close the window, it would disturb what
2749 * happens next. Clear the file name and set the arg
2750 * index to -1 to delete it later. */
2751 setfname(curbuf, NULL, NULL, FALSE);
2752 curwin->w_arg_idx = -1;
2753 swap_exists_action = SEA_NONE;
2754 }
2755 else
2756 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757#endif
2758#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002759 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002760#endif
2761 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002762 ui_breakcheck();
2763 if (got_int)
2764 {
2765 (void)vgetc(); /* only break the file loading, not the rest */
2766 break;
2767 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002768 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002769 if (parmp->window_layout == WIN_TABS)
2770 goto_tabpage(1);
2771 else
2772 curwin = firstwin;
2773 curbuf = curwin->w_buffer;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774#ifdef FEAT_AUTOCMD
2775 --autocmd_no_enter;
2776 --autocmd_no_leave;
2777#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002778 }
2779}
2780
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781 /*
2782 * If opened more than one window, start editing files in the other
2783 * windows. make_windows() has already opened the windows.
2784 */
2785 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002786edit_buffers(
2787 mparm_T *parmp,
2788 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002789{
2790 int arg_idx; /* index in argument list */
2791 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002792 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002793 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794
2795# ifdef FEAT_AUTOCMD
2796 /*
2797 * Don't execute Win/Buf Enter/Leave autocommands here
2798 */
2799 ++autocmd_no_enter;
2800 ++autocmd_no_leave;
2801# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002802
2803 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2804 if (curwin->w_arg_idx == -1)
2805 {
2806 win_close(curwin, TRUE);
2807 advance = FALSE;
2808 }
2809
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810 arg_idx = 1;
2811 for (i = 1; i < parmp->window_count; ++i)
2812 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002813 if (cwd != NULL)
2814 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002815 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2816 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002817 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002818 ++arg_idx;
2819 win_close(curwin, TRUE);
2820 advance = FALSE;
2821 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002822 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002823
Bram Moolenaar84212822006-11-07 21:59:47 +00002824 if (advance)
2825 {
2826 if (parmp->window_layout == WIN_TABS)
2827 {
2828 if (curtab->tp_next == NULL) /* just checking */
2829 break;
2830 goto_tabpage(0);
2831 }
2832 else
2833 {
2834 if (curwin->w_next == NULL) /* just checking */
2835 break;
2836 win_enter(curwin->w_next, FALSE);
2837 }
2838 }
2839 advance = TRUE;
2840
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002841 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002842 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002843 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2844 {
2845 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002846 /* Edit file from arg list, if there is one. When "Quit" selected
2847 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002848# ifdef HAS_SWAP_EXISTS_ACTION
2849 swap_exists_did_quit = FALSE;
2850# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002851 (void)do_ecmd(0, arg_idx < GARGCOUNT
2852 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002853 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002854# ifdef HAS_SWAP_EXISTS_ACTION
2855 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002856 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002857 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002858 if (got_int || only_one_window())
2859 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002860 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002861 did_emsg = FALSE; /* avoid hit-enter prompt */
2862 getout(1);
2863 }
2864 win_close(curwin, TRUE);
2865 advance = FALSE;
2866 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002867# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002868 if (arg_idx == GARGCOUNT - 1)
2869 arg_had_last = TRUE;
2870 ++arg_idx;
2871 }
2872 ui_breakcheck();
2873 if (got_int)
2874 {
2875 (void)vgetc(); /* only break the file loading, not the rest */
2876 break;
2877 }
2878 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002879
2880 if (parmp->window_layout == WIN_TABS)
2881 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002882# ifdef FEAT_AUTOCMD
2883 --autocmd_no_enter;
2884# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002885
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002886 /* make the first window the current window */
2887 win = firstwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02002888#if defined(FEAT_QUICKFIX)
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002889 /* Avoid making a preview window the current window. */
2890 while (win->w_p_pvw)
2891 {
2892 win = win->w_next;
2893 if (win == NULL)
2894 {
2895 win = firstwin;
2896 break;
2897 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002898 }
2899#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002900 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002901
Bram Moolenaar4033c552017-09-16 20:54:51 +02002902#ifdef FEAT_AUTOCMD
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903 --autocmd_no_leave;
Bram Moolenaar4033c552017-09-16 20:54:51 +02002904#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002905 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002906 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002907 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2908}
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002909
2910/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002911 * Execute the commands from --cmd arguments "cmds[cnt]".
2912 */
2913 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002914exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002915{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002916 char_u **cmds = parmp->pre_commands;
2917 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002918 int i;
2919
2920 if (cnt > 0)
2921 {
2922 curwin->w_cursor.lnum = 0; /* just in case.. */
2923 sourcing_name = (char_u *)_("pre-vimrc command line");
2924# ifdef FEAT_EVAL
2925 current_SID = SID_CMDARG;
2926# endif
2927 for (i = 0; i < cnt; ++i)
2928 do_cmdline_cmd(cmds[i]);
2929 sourcing_name = NULL;
2930# ifdef FEAT_EVAL
2931 current_SID = 0;
2932# endif
2933 TIME_MSG("--cmd commands");
2934 }
2935}
2936
2937/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002938 * Execute "+", "-c" and "-S" arguments.
2939 */
2940 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002941exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002942{
2943 int i;
2944
2945 /*
2946 * We start commands on line 0, make "vim +/pat file" match a
2947 * pattern on line 1. But don't move the cursor when an autocommand
2948 * with g`" was used.
2949 */
2950 msg_scroll = TRUE;
2951 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2952 curwin->w_cursor.lnum = 0;
2953 sourcing_name = (char_u *)"command line";
2954#ifdef FEAT_EVAL
2955 current_SID = SID_CARG;
2956#endif
2957 for (i = 0; i < parmp->n_commands; ++i)
2958 {
2959 do_cmdline_cmd(parmp->commands[i]);
2960 if (parmp->cmds_tofree[i])
2961 vim_free(parmp->commands[i]);
2962 }
2963 sourcing_name = NULL;
2964#ifdef FEAT_EVAL
2965 current_SID = 0;
2966#endif
2967 if (curwin->w_cursor.lnum == 0)
2968 curwin->w_cursor.lnum = 1;
2969
2970 if (!exmode_active)
2971 msg_scroll = FALSE;
2972
2973#ifdef FEAT_QUICKFIX
2974 /* When started with "-q errorfile" jump to first error again. */
2975 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002976 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002977#endif
2978 TIME_MSG("executing command arguments");
2979}
2980
2981/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002982 * Source startup scripts.
2983 */
2984 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002985source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002986{
2987 int i;
2988
2989 /*
2990 * For "evim" source evim.vim first of all, so that the user can overrule
2991 * any things he doesn't like.
2992 */
2993 if (parmp->evim_mode)
2994 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002995 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002996 TIME_MSG("source evim file");
2997 }
2998
2999 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003000 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001 * nothing else.
3002 */
3003 if (parmp->use_vimrc != NULL)
3004 {
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003005 if (STRCMP(parmp->use_vimrc, "DEFAULTS") == 0)
3006 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
3007 else if (STRCMP(parmp->use_vimrc, "NONE") == 0
Bram Moolenaar231334e2005-07-25 20:46:57 +00003008 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003009 {
3010#ifdef FEAT_GUI
3011 if (use_gvimrc == NULL) /* don't load gvimrc either */
3012 use_gvimrc = parmp->use_vimrc;
3013#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003014 }
3015 else
3016 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003017 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003018 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
3019 }
3020 }
3021 else if (!silent_mode)
3022 {
3023#ifdef AMIGA
3024 struct Process *proc = (struct Process *)FindTask(0L);
3025 APTR save_winptr = proc->pr_WindowPtr;
3026
3027 /* Avoid a requester here for a volume that doesn't exist. */
3028 proc->pr_WindowPtr = (APTR)-1L;
3029#endif
3030
3031 /*
3032 * Get system wide defaults, if the file name is defined.
3033 */
3034#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003035 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003036#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003037#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003038 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003039#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003040
3041 /*
3042 * Try to read initialization commands from the following places:
3043 * - environment variable VIMINIT
3044 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3045 * - second user vimrc file ($VIM/.vimrc for Dos)
3046 * - environment variable EXINIT
3047 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3048 * - second user exrc file ($VIM/.exrc for Dos)
3049 * The first that exists is used, the rest is ignored.
3050 */
3051 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3052 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003053 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003054#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003055 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3056 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003057#endif
3058#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003059 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3060 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003061#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003062#ifdef USR_VIMRC_FILE4
3063 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3064 DOSO_VIMRC) == FAIL
3065#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003066 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003067 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003068#ifdef USR_EXRC_FILE2
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003069 && do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003070#endif
Bram Moolenaarb9a46fe2016-07-29 18:13:42 +02003071 && !has_dash_c_arg)
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003072 {
3073 /* When no .vimrc file was found: source defaults.vim. */
3074 do_source((char_u *)VIM_DEFAULTS_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003075 }
3076 }
3077
3078 /*
3079 * Read initialization commands from ".vimrc" or ".exrc" in current
3080 * directory. This is only done if the 'exrc' option is set.
3081 * Because of security reasons we disallow shell and write commands
Bram Moolenaar8c08b5b2016-07-28 22:24:15 +02003082 * now, except for Unix if the file is owned by the user or 'secure'
Bram Moolenaar58d98232005-07-23 22:25:46 +00003083 * option has been reset in environment of global ".exrc" or ".vimrc".
3084 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3085 * SYS_VIMRC_FILE.
3086 */
3087 if (p_exrc)
3088 {
3089#if defined(UNIX) || defined(VMS)
3090 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3091 if (!file_owned(VIMRC_FILE))
3092#endif
3093 secure = p_secure;
3094
3095 i = FAIL;
3096 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3097 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3098#ifdef USR_VIMRC_FILE2
3099 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3100 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3101#endif
3102#ifdef USR_VIMRC_FILE3
3103 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3104 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3105#endif
3106#ifdef SYS_VIMRC_FILE
3107 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3108 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3109#endif
3110 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003111 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003112
3113 if (i == FAIL)
3114 {
3115#if defined(UNIX) || defined(VMS)
3116 /* if ".exrc" is not owned by user set 'secure' mode */
3117 if (!file_owned(EXRC_FILE))
3118 secure = p_secure;
3119 else
3120 secure = 0;
3121#endif
3122 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3123 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3124#ifdef USR_EXRC_FILE2
3125 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3126 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3127#endif
3128 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003129 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003130 }
3131 }
3132 if (secure == 2)
3133 need_wait_return = TRUE;
3134 secure = 0;
3135#ifdef AMIGA
3136 proc->pr_WindowPtr = save_winptr;
3137#endif
3138 }
3139 TIME_MSG("sourcing vimrc file(s)");
3140}
3141
3142/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003143 * Setup to start using the GUI. Exit with an error when not available.
3144 */
3145 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003146main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003147{
3148#ifdef FEAT_GUI
3149 gui.starting = TRUE; /* start GUI a bit later */
3150#else
3151 mch_errmsg(_(e_nogvim));
3152 mch_errmsg("\n");
3153 mch_exit(2);
3154#endif
3155}
3156
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003157#endif /* NO_VIM_MAIN */
3158
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003159/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003160 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003161 * Returns FAIL if the environment variable was not executed, OK otherwise.
3162 */
3163 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003164process_env(
3165 char_u *env,
3166 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003167{
3168 char_u *initstr;
3169 char_u *save_sourcing_name;
3170 linenr_T save_sourcing_lnum;
3171#ifdef FEAT_EVAL
3172 scid_T save_sid;
3173#endif
3174
3175 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3176 {
3177 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003178 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003179 save_sourcing_name = sourcing_name;
3180 save_sourcing_lnum = sourcing_lnum;
3181 sourcing_name = env;
3182 sourcing_lnum = 0;
3183#ifdef FEAT_EVAL
3184 save_sid = current_SID;
3185 current_SID = SID_ENV;
3186#endif
3187 do_cmdline_cmd(initstr);
3188 sourcing_name = save_sourcing_name;
3189 sourcing_lnum = save_sourcing_lnum;
3190#ifdef FEAT_EVAL
Bram Moolenaar945ec092016-06-08 21:17:43 +02003191 current_SID = save_sid;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003192#endif
3193 return OK;
3194 }
3195 return FAIL;
3196}
3197
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003198#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003199/*
3200 * Return TRUE if we are certain the user owns the file "fname".
3201 * Used for ".vimrc" and ".exrc".
3202 * Use both stat() and lstat() for extra security.
3203 */
3204 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003205file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003206{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003207 stat_T s;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003208# ifdef UNIX
3209 uid_t uid = getuid();
3210# else /* VMS */
3211 uid_t uid = ((getgid() << 16) | getuid());
3212# endif
3213
3214 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3215# ifdef HAVE_LSTAT
3216 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3217# endif
3218 );
3219}
3220#endif
3221
3222/*
3223 * Give an error message main_errors["n"] and exit.
3224 */
3225 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003226mainerr(
3227 int n, /* one of the ME_ defines */
3228 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003229{
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003230#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003231 reset_signals(); /* kill us with CTRL-C here, if you like */
3232#endif
3233
3234 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003235 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003236 mch_errmsg(_(main_errors[n]));
3237 if (str != NULL)
3238 {
3239 mch_errmsg(": \"");
3240 mch_errmsg((char *)str);
3241 mch_errmsg("\"");
3242 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003243 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003244
3245 mch_exit(1);
3246}
3247
3248 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003249mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003250{
3251 mainerr(ME_ARG_MISSING, str);
3252}
3253
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003254#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003255/*
3256 * print a message with three spaces prepended and '\n' appended.
3257 */
3258 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003259main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003260{
3261 mch_msg(" ");
3262 mch_msg(s);
3263 mch_msg("\n");
3264}
3265
3266/*
3267 * Print messages for "vim -h" or "vim --help" and exit.
3268 */
3269 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003270usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003271{
3272 int i;
3273 static char *(use[]) =
3274 {
3275 N_("[file ..] edit specified file(s)"),
3276 N_("- read text from stdin"),
3277 N_("-t tag edit file where tag is defined"),
3278#ifdef FEAT_QUICKFIX
3279 N_("-q [errorfile] edit file with first error")
3280#endif
3281 };
3282
Bram Moolenaara06ecab2016-07-16 14:47:36 +02003283#if defined(UNIX) || defined(VMS)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003284 reset_signals(); /* kill us with CTRL-C here, if you like */
3285#endif
3286
3287 mch_msg(longVersion);
3288 mch_msg(_("\n\nusage:"));
3289 for (i = 0; ; ++i)
3290 {
3291 mch_msg(_(" vim [arguments] "));
3292 mch_msg(_(use[i]));
3293 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3294 break;
3295 mch_msg(_("\n or:"));
3296 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003297#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003298 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003299#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003300
3301 mch_msg(_("\n\nArguments:\n"));
3302 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003303#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003304 main_msg(_("--literal\t\tDon't expand wildcards"));
3305#endif
3306#ifdef FEAT_OLE
3307 main_msg(_("-register\t\tRegister this gvim for OLE"));
3308 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3309#endif
3310#ifdef FEAT_GUI
3311 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3312 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3313#endif
3314 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3315 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003316 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003317 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3318#ifdef FEAT_DIFF
3319 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3320#endif
3321 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3322 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3323 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3324 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3325 main_msg(_("-M\t\t\tModifications in text not allowed"));
3326 main_msg(_("-b\t\t\tBinary mode"));
3327#ifdef FEAT_LISP
3328 main_msg(_("-l\t\t\tLisp mode"));
3329#endif
3330 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3331 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003332 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3333#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003334 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003335#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003336 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3337 main_msg(_("-r\t\t\tList swap files and exit"));
3338 main_msg(_("-r (with file name)\tRecover crashed session"));
3339 main_msg(_("-L\t\t\tSame as -r"));
3340#ifdef AMIGA
3341 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3342 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3343#endif
3344#ifdef FEAT_ARABIC
3345 main_msg(_("-A\t\t\tstart in Arabic mode"));
3346#endif
3347#ifdef FEAT_RIGHTLEFT
3348 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3349#endif
3350#ifdef FEAT_FKMAP
3351 main_msg(_("-F\t\t\tStart in Farsi mode"));
3352#endif
3353 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
Bram Moolenaar49c39ff2016-02-25 21:21:52 +01003354 main_msg(_("--not-a-term\t\tSkip warning for input/output not being a terminal"));
Bram Moolenaar2cab0e12016-11-24 15:09:07 +01003355 main_msg(_("--ttyfail\t\tExit if input or output is not a terminal"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003356 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3357#ifdef FEAT_GUI
3358 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3359#endif
3360 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003361 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003362 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3363 main_msg(_("-O[N]\t\tLike -o but split vertically"));
3364 main_msg(_("+\t\t\tStart at end of file"));
3365 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003366 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003367 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3368 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3369 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3370 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3371 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3372#ifdef FEAT_CRYPT
3373 main_msg(_("-x\t\t\tEdit encrypted files"));
3374#endif
3375#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3376# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3377 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3378# endif
3379 main_msg(_("-X\t\t\tDo not connect to X server"));
3380#endif
3381#ifdef FEAT_CLIENTSERVER
3382 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3383 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3384 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3385 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003386 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3388 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3389 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3390 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3391#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003392#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003393 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003394#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395#ifdef FEAT_VIMINFO
3396 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3397#endif
Bram Moolenaarc4da1132017-07-15 19:39:43 +02003398 main_msg(_("--clean\t\t'nocompatible', Vim defaults, no plugins, no viminfo"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3400 main_msg(_("--version\t\tPrint version information and exit"));
3401
3402#ifdef FEAT_GUI_X11
3403# ifdef FEAT_GUI_MOTIF
3404 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3405# else
3406# ifdef FEAT_GUI_ATHENA
3407# ifdef FEAT_GUI_NEXTAW
3408 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3409# else
3410 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3411# endif
3412# endif
3413# endif
3414 main_msg(_("-display <display>\tRun vim on <display>"));
3415 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003416 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3417 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3418 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3419 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3420 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3421 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3422 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3423 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3424# ifdef FEAT_GUI_ATHENA
3425 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3426# endif
3427 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3428 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3429 main_msg(_("-xrm <resource>\tSet the specified resource"));
3430#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003431#ifdef FEAT_GUI_GTK
3432 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3433 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3434 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3435 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3436 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003437 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003438 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003439 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003440#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003441#ifdef FEAT_GUI_W32
3442 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003443 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003444#endif
3445
3446#ifdef FEAT_GUI_GNOME
3447 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3448 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003449 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003450 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003451 gui.dofork = FALSE;
3452 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003453 else
3454#endif
3455 mch_exit(0);
3456}
3457
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003458#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003459/*
3460 * Check the result of the ATTENTION dialog:
3461 * When "Quit" selected, exit Vim.
3462 * When "Recover" selected, recover the file.
3463 */
3464 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003465check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003466{
3467 if (swap_exists_action == SEA_QUIT)
3468 getout(1);
3469 handle_swap_exists(NULL);
3470}
3471#endif
3472
Bram Moolenaar08cab962017-03-04 14:37:18 +01003473#endif /* NO_VIM_MAIN */
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003474
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003475#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003476static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003477
3478static struct timeval prev_timeval;
3479
Bram Moolenaar3f269672009-11-03 11:11:11 +00003480# ifdef WIN3264
3481/*
3482 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3483 */
3484 static int
3485gettimeofday(struct timeval *tv, char *dummy)
3486{
3487 long t = clock();
3488 tv->tv_sec = t / CLOCKS_PER_SEC;
3489 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3490 return 0;
3491}
3492# endif
3493
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003494/*
3495 * Save the previous time before doing something that could nest.
3496 * set "*tv_rel" to the time elapsed so far.
3497 */
3498 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003499time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003500{
3501 *((struct timeval *)tv_rel) = prev_timeval;
3502 gettimeofday(&prev_timeval, NULL);
3503 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3504 - ((struct timeval *)tv_rel)->tv_usec;
3505 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3506 - ((struct timeval *)tv_rel)->tv_sec;
3507 if (((struct timeval *)tv_rel)->tv_usec < 0)
3508 {
3509 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3510 --((struct timeval *)tv_rel)->tv_sec;
3511 }
3512 *(struct timeval *)tv_start = prev_timeval;
3513}
3514
3515/*
3516 * Compute the previous time after doing something that could nest.
3517 * Subtract "*tp" from prev_timeval;
3518 * Note: The arguments are (void *) to avoid trouble with systems that don't
3519 * have struct timeval.
3520 */
3521 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003522time_pop(
3523 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003524{
3525 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3526 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3527 if (prev_timeval.tv_usec < 0)
3528 {
3529 prev_timeval.tv_usec += 1000000;
3530 --prev_timeval.tv_sec;
3531 }
3532}
3533
3534 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003535time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003536{
3537 long usec;
3538 long msec;
3539
3540 usec = now->tv_usec - then->tv_usec;
3541 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3542 usec = usec % 1000L;
3543 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3544}
3545
3546 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003547time_msg(
3548 char *mesg,
3549 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003550 (struct timeval *) */
3551{
3552 static struct timeval start;
3553 struct timeval now;
3554
3555 if (time_fd != NULL)
3556 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003557 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003558 {
3559 gettimeofday(&start, NULL);
3560 prev_timeval = start;
3561 fprintf(time_fd, "\n\ntimes in msec\n");
3562 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3563 fprintf(time_fd, " clock elapsed: other lines\n\n");
3564 }
3565 gettimeofday(&now, NULL);
3566 time_diff(&start, &now);
3567 if (((struct timeval *)tv_start) != NULL)
3568 {
3569 fprintf(time_fd, " ");
3570 time_diff(((struct timeval *)tv_start), &now);
3571 }
3572 fprintf(time_fd, " ");
3573 time_diff(&prev_timeval, &now);
3574 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003575 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003576 }
3577}
3578
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003579#endif
3580
Bram Moolenaar595297d2017-03-04 19:11:12 +01003581#if !defined(NO_VIM_MAIN) && defined(FEAT_EVAL)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003582 static void
3583set_progpath(char_u *argv0)
3584{
3585 char_u *val = argv0;
Bram Moolenaar08cab962017-03-04 14:37:18 +01003586
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003587# if defined(WIN32)
Bram Moolenaar08cab962017-03-04 14:37:18 +01003588 /* A relative path containing a "/" will become invalid when using ":cd",
3589 * turn it into a full path.
Bram Moolenaar066029e2017-03-05 15:19:32 +01003590 * On MS-Windows "vim" should be expanded to "vim.exe", thus always do
3591 * this. */
Bram Moolenaar066029e2017-03-05 15:19:32 +01003592 char_u *path = NULL;
3593
3594 if (mch_can_exe(argv0, &path, FALSE) && path != NULL)
3595 val = path;
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003596# else
3597 char_u buf[MAXPATHL + 1];
3598# ifdef PROC_EXE_LINK
3599 char linkbuf[MAXPATHL + 1];
3600 ssize_t len;
Bram Moolenaar066029e2017-03-05 15:19:32 +01003601
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003602 len = readlink(PROC_EXE_LINK, linkbuf, MAXPATHL);
3603 if (len > 0)
Bram Moolenaar43663192017-03-05 14:29:12 +01003604 {
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003605 linkbuf[len] = NUL;
3606 val = (char_u *)linkbuf;
Bram Moolenaar43663192017-03-05 14:29:12 +01003607 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003608# endif
Bram Moolenaarbc906e42017-08-17 17:21:05 +02003609
3610 if (!mch_isFullName(val))
3611 {
3612 if (gettail(val) != val
3613 && vim_FullName(val, buf, MAXPATHL, TRUE) != FAIL)
3614 val = buf;
3615 }
Bram Moolenaar066029e2017-03-05 15:19:32 +01003616# endif
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003617
Bram Moolenaar08cab962017-03-04 14:37:18 +01003618 set_vim_var_string(VV_PROGPATH, val, -1);
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01003619
Bram Moolenaar066029e2017-03-05 15:19:32 +01003620# ifdef WIN32
Bram Moolenaar43663192017-03-05 14:29:12 +01003621 vim_free(path);
Bram Moolenaar066029e2017-03-05 15:19:32 +01003622# endif
Bram Moolenaar08cab962017-03-04 14:37:18 +01003623}
3624
3625#endif /* NO_VIM_MAIN */
3626
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003627#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003628
3629/*
3630 * Common code for the X command server and the Win32 command server.
3631 */
3632
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003633static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003634
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003635/*
3636 * Do the client-server stuff, unless "--servername ''" was used.
3637 */
3638 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003639exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003640{
3641 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3642 {
3643# ifdef WIN32
3644 /* Initialise the client/server messaging infrastructure. */
3645 serverInitMessaging();
3646# endif
3647
3648 /*
3649 * When a command server argument was found, execute it. This may
3650 * exit Vim when it was successful. Otherwise it's executed further
3651 * on. Remember the encoding used here in "serverStrEnc".
3652 */
3653 if (parmp->serverArg)
3654 {
3655 cmdsrv_main(&parmp->argc, parmp->argv,
3656 parmp->serverName_arg, &parmp->serverStr);
3657# ifdef FEAT_MBYTE
3658 parmp->serverStrEnc = vim_strsave(p_enc);
3659# endif
3660 }
3661
3662 /* If we're still running, get the name to register ourselves.
3663 * On Win32 can register right now, for X11 need to setup the
3664 * clipboard first, it's further down. */
3665 parmp->servername = serverMakeName(parmp->serverName_arg,
3666 parmp->argv[0]);
3667# ifdef WIN32
3668 if (parmp->servername != NULL)
3669 {
3670 serverSetName(parmp->servername);
3671 vim_free(parmp->servername);
3672 }
3673# endif
3674 }
3675}
3676
3677/*
3678 * Prepare for running as a Vim server.
3679 */
3680 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003681prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003682{
3683# if defined(FEAT_X11)
3684 /*
3685 * Register for remote command execution with :serversend and --remote
3686 * unless there was a -X or a --servername '' on the command line.
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003687 * Only register nongui-vim's with an explicit --servername argument,
3688 * or when compiling with autoservername.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003689 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003690 */
3691 if (X_DISPLAY != NULL && parmp->servername != NULL && (
Bram Moolenaare42a6d22017-11-12 19:21:51 +01003692# if defined(FEAT_AUTOSERVERNAME) || defined(FEAT_GUI)
3693 (
3694# if defined(FEAT_AUTOSERVERNAME)
3695 1
3696# else
3697 gui.in_use
3698# endif
Bram Moolenaar5f402312006-08-15 19:40:35 +00003699# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003700 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003701# endif
3702 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003703# endif
3704 parmp->serverName_arg != NULL))
3705 {
3706 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3707 vim_free(parmp->servername);
3708 TIME_MSG("register server name");
3709 }
3710 else
3711 serverDelayedStartName = parmp->servername;
3712# endif
3713
3714 /*
3715 * Execute command ourselves if we're here because the send failed (or
3716 * else we would have exited above).
3717 */
3718 if (parmp->serverStr != NULL)
3719 {
3720 char_u *p;
3721
3722 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3723 parmp->serverStr, &p));
3724 vim_free(p);
3725 }
3726}
3727
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003728 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003729cmdsrv_main(
3730 int *argc,
3731 char **argv,
3732 char_u *serverName_arg,
3733 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003734{
3735 char_u *res;
3736 int i;
3737 char_u *sname;
3738 int ret;
3739 int didone = FALSE;
3740 int exiterr = 0;
3741 char **newArgV = argv + 1;
3742 int newArgC = 1,
3743 Argc = *argc;
3744 int argtype;
3745#define ARGTYPE_OTHER 0
3746#define ARGTYPE_EDIT 1
3747#define ARGTYPE_EDIT_WAIT 2
3748#define ARGTYPE_SEND 3
3749 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003750 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003751# ifndef FEAT_X11
3752 HWND srv;
3753# else
3754 Window srv;
3755
3756 setup_term_clip();
3757# endif
3758
3759 sname = serverMakeName(serverName_arg, argv[0]);
3760 if (sname == NULL)
3761 return;
3762
3763 /*
3764 * Execute the command server related arguments and remove them
3765 * from the argc/argv array; We may have to return into main()
3766 */
3767 for (i = 1; i < Argc; i++)
3768 {
3769 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003770 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003771 {
3772 for (; i < *argc; i++)
3773 {
3774 *newArgV++ = argv[i];
3775 newArgC++;
3776 }
3777 break;
3778 }
3779
Bram Moolenaareb94e552006-03-11 21:35:11 +00003780 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003781 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003782 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3783 {
3784 char *p = argv[i] + 8;
3785
3786 argtype = ARGTYPE_EDIT;
3787 while (*p != NUL)
3788 {
3789 if (STRNICMP(p, "-wait", 5) == 0)
3790 {
3791 argtype = ARGTYPE_EDIT_WAIT;
3792 p += 5;
3793 }
3794 else if (STRNICMP(p, "-silent", 7) == 0)
3795 {
3796 silent = TRUE;
3797 p += 7;
3798 }
3799 else if (STRNICMP(p, "-tab", 4) == 0)
3800 {
3801 tabs = TRUE;
3802 p += 4;
3803 }
3804 else
3805 {
3806 argtype = ARGTYPE_OTHER;
3807 break;
3808 }
3809 }
3810 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003811 else
3812 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003813
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003814 if (argtype != ARGTYPE_OTHER)
3815 {
3816 if (i == *argc - 1)
3817 mainerr_arg_missing((char_u *)argv[i]);
3818 if (argtype == ARGTYPE_SEND)
3819 {
3820 *serverStr = (char_u *)argv[i + 1];
3821 i++;
3822 }
3823 else
3824 {
3825 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003826 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003827 if (*serverStr == NULL)
3828 {
3829 /* Probably out of memory, exit. */
3830 didone = TRUE;
3831 exiterr = 1;
3832 break;
3833 }
3834 Argc = i;
3835 }
3836# ifdef FEAT_X11
3837 if (xterm_dpy == NULL)
3838 {
3839 mch_errmsg(_("No display"));
3840 ret = -1;
3841 }
3842 else
3843 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003844 NULL, &srv, 0, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003845# else
3846 /* Win32 always works? */
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003847 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, 0, silent);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003848# endif
3849 if (ret < 0)
3850 {
3851 if (argtype == ARGTYPE_SEND)
3852 {
3853 /* Failed to send, abort. */
3854 mch_errmsg(_(": Send failed.\n"));
3855 didone = TRUE;
3856 exiterr = 1;
3857 }
3858 else if (!silent)
3859 /* Let vim start normally. */
3860 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3861 break;
3862 }
3863
3864# ifdef FEAT_GUI_W32
3865 /* Guess that when the server name starts with "g" it's a GUI
3866 * server, which we can bring to the foreground here.
3867 * Foreground() in the server doesn't work very well. */
3868 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3869 SetForegroundWindow(srv);
3870# endif
3871
3872 /*
3873 * For --remote-wait: Wait until the server did edit each
3874 * file. Also detect that the server no longer runs.
3875 */
3876 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3877 {
3878 int numFiles = *argc - i - 1;
3879 int j;
3880 char_u *done = alloc(numFiles);
3881 char_u *p;
3882# ifdef FEAT_GUI_W32
3883 NOTIFYICONDATA ni;
3884 int count = 0;
3885 extern HWND message_window;
3886# endif
3887
3888 if (numFiles > 0 && argv[i + 1][0] == '+')
3889 /* Skip "+cmd" argument, don't wait for it to be edited. */
3890 --numFiles;
3891
3892# ifdef FEAT_GUI_W32
3893 ni.cbSize = sizeof(ni);
3894 ni.hWnd = message_window;
3895 ni.uID = 0;
3896 ni.uFlags = NIF_ICON|NIF_TIP;
3897 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3898 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3899 Shell_NotifyIcon(NIM_ADD, &ni);
3900# endif
3901
3902 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003903 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003904 while (memchr(done, 0, numFiles) != NULL)
3905 {
3906# ifdef WIN32
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003907 p = serverGetReply(srv, NULL, TRUE, TRUE, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003908 if (p == NULL)
3909 break;
3910# else
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003911 if (serverReadReply(xterm_dpy, srv, &p, TRUE, -1) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003912 break;
3913# endif
3914 j = atoi((char *)p);
3915 if (j >= 0 && j < numFiles)
3916 {
3917# ifdef FEAT_GUI_W32
3918 ++count;
3919 sprintf(ni.szTip, _("%d of %d edited"),
3920 count, numFiles);
3921 Shell_NotifyIcon(NIM_MODIFY, &ni);
3922# endif
3923 done[j] = 1;
3924 }
3925 }
3926# ifdef FEAT_GUI_W32
3927 Shell_NotifyIcon(NIM_DELETE, &ni);
3928# endif
3929 }
3930 }
3931 else if (STRICMP(argv[i], "--remote-expr") == 0)
3932 {
3933 if (i == *argc - 1)
3934 mainerr_arg_missing((char_u *)argv[i]);
3935# ifdef WIN32
3936 /* Win32 always works? */
3937 if (serverSendToVim(sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003938 &res, NULL, 1, 0, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003939# else
3940 if (xterm_dpy == NULL)
3941 mch_errmsg(_("No display: Send expression failed.\n"));
3942 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
Bram Moolenaar81b9d0b2017-03-19 21:20:53 +01003943 &res, NULL, 1, 0, 1, FALSE) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003944# endif
3945 {
3946 if (res != NULL && *res != NUL)
3947 {
3948 /* Output error from remote */
3949 mch_errmsg((char *)res);
3950 vim_free(res);
3951 res = NULL;
3952 }
3953 mch_errmsg(_(": Send expression failed.\n"));
3954 }
3955 }
3956 else if (STRICMP(argv[i], "--serverlist") == 0)
3957 {
3958# ifdef WIN32
3959 /* Win32 always works? */
3960 res = serverGetVimNames();
3961# else
3962 if (xterm_dpy != NULL)
3963 res = serverGetVimNames(xterm_dpy);
3964# endif
3965 if (called_emsg)
3966 mch_errmsg("\n");
3967 }
3968 else if (STRICMP(argv[i], "--servername") == 0)
3969 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003970 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003971 i++;
3972 continue;
3973 }
3974 else
3975 {
3976 *newArgV++ = argv[i];
3977 newArgC++;
3978 continue;
3979 }
3980 didone = TRUE;
3981 if (res != NULL && *res != NUL)
3982 {
3983 mch_msg((char *)res);
3984 if (res[STRLEN(res) - 1] != '\n')
3985 mch_msg("\n");
3986 }
3987 vim_free(res);
3988 }
3989
3990 if (didone)
3991 {
3992 display_errors(); /* display any collected messages */
3993 exit(exiterr); /* Mission accomplished - get out */
3994 }
3995
3996 /* Return back into main() */
3997 *argc = newArgC;
3998 vim_free(sname);
3999}
4000
4001/*
4002 * Build a ":drop" command to send to a Vim server.
4003 */
4004 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004005build_drop_cmd(
4006 int filec,
4007 char **filev,
4008 int tabs, /* Use ":tab drop" instead of ":drop". */
4009 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004010{
4011 garray_T ga;
4012 int i;
4013 char_u *inicmd = NULL;
4014 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004015 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004016 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004017
4018 if (filec > 0 && filev[0][0] == '+')
4019 {
4020 inicmd = (char_u *)filev[0] + 1;
4021 filev++;
4022 filec--;
4023 }
4024 /* Check if we have at least one argument. */
4025 if (filec <= 0)
4026 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004027
4028 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02004029 cwd = alloc(MAXPATHL);
4030 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004031 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02004032 if (mch_dirname(cwd, MAXPATHL) != OK)
4033 {
4034 vim_free(cwd);
4035 return NULL;
4036 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004037 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00004038#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01004039 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00004040#else
4041 PATH_ESC_CHARS,
4042#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02004043 '\\', TRUE);
4044 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004045 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004046 return NULL;
4047 ga_init2(&ga, 1, 100);
4048 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004049 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00004050
4051 /* Call inputsave() so that a prompt for an encryption key works. */
4052 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
4053 if (tabs)
4054 ga_concat(&ga, (char_u *)"tab ");
4055 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004056 for (i = 0; i < filec; i++)
4057 {
4058 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004059 * do it again in the Vim server. On MS-Windows only escape
4060 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004061 p = vim_strsave_escaped((char_u *)filev[i],
4062#ifdef UNIX
4063 PATH_ESC_CHARS
4064#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00004065 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004066#endif
4067 );
4068 if (p == NULL)
4069 {
4070 vim_free(ga.ga_data);
4071 return NULL;
4072 }
4073 ga_concat(&ga, (char_u *)" ");
4074 ga_concat(&ga, p);
4075 vim_free(p);
4076 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004077 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4078
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004079 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4080 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004081 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4082
4083 /* Switch back to the correct current directory (prior to temporary path
4084 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004085 * correct after the :drop command. With line breaks and spaces:
4086 * if !exists('+acd') || !&acd
4087 * if haslocaldir()
4088 * cd -
4089 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004090 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004091 * cd -
4092 * endif
4093 * endif
4094 */
4095 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004096 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004097 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004098 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004099 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004100
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004101 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004102 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4103 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004104 if (inicmd != NULL)
4105 {
4106 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4107 * the following commands to be inserted as text. Use a "|",
4108 * hopefully "inicmd" does allow this... */
4109 ga_concat(&ga, inicmd);
4110 ga_concat(&ga, (char_u *)"|");
4111 }
4112 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4113 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004114 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004115 ga_append(&ga, NUL);
4116 return ga.ga_data;
4117}
4118
4119/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004120 * Make our basic server name: use the specified "arg" if given, otherwise use
4121 * the tail of the command "cmd" we were started with.
4122 * Return the name in allocated memory. This doesn't include a serial number.
4123 */
4124 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004125serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004126{
4127 char_u *p;
4128
4129 if (arg != NULL && *arg != NUL)
4130 p = vim_strsave_up(arg);
4131 else
4132 {
4133 p = vim_strsave_up(gettail((char_u *)cmd));
4134 /* Remove .exe or .bat from the name. */
4135 if (p != NULL && vim_strchr(p, '.') != NULL)
4136 *vim_strchr(p, '.') = NUL;
4137 }
4138 return p;
4139}
4140#endif /* FEAT_CLIENTSERVER */
4141
4142#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4143/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004144 * Replace termcodes such as <CR> and insert as key presses if there is room.
4145 */
4146 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004147server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004148{
4149 char_u *ptr = NULL;
4150 char_u *cpo_save = p_cpo;
4151
4152 /* Set 'cpoptions' the way we want it.
4153 * B set - backslashes are *not* treated specially
4154 * k set - keycodes are *not* reverse-engineered
4155 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004156 * The last but one parameter of replace_termcodes() is TRUE so that the
4157 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004158 */
4159 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004160 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004161 p_cpo = cpo_save;
4162
4163 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4164 {
4165 /*
4166 * Add the string to the input stream.
4167 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4168 *
4169 * First clear typed characters from the typeahead buffer, there could
4170 * be half a mapping there. Then append to the existing string, so
4171 * that multiple commands from a client are concatenated.
4172 */
4173 if (typebuf.tb_maplen < typebuf.tb_len)
4174 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4175 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4176
4177 /* Let input_available() know we inserted text in the typeahead
4178 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004179 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004180 }
4181 vim_free((char_u *)ptr);
4182}
4183
4184/*
4185 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004186 */
4187 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004188eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004189{
4190 char_u *res;
4191 int save_dbl = debug_break_level;
4192 int save_ro = redir_off;
Bram Moolenaard99388b2017-10-26 14:28:32 +02004193 void *fc = NULL;
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004194
4195 /* Evaluate the expression at the toplevel, don't use variables local to
Bram Moolenaard99388b2017-10-26 14:28:32 +02004196 * the calling function. Except when in debug mode. */
4197 if (!debug_mode)
4198 fc = clear_current_funccal();
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004199
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004200 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4201 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004202 debug_break_level = -1;
4203 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004204 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4205 * to be typed. Do generate errors so that try/catch works. */
4206 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004207
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004208 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004209
4210 debug_break_level = save_dbl;
4211 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004212 --emsg_silent;
4213 if (emsg_silent < 0)
4214 emsg_silent = 0;
Bram Moolenaard99388b2017-10-26 14:28:32 +02004215 if (fc != NULL)
4216 restore_current_funccal(fc);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004217
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004218 /* A client can tell us to redraw, but not to display the cursor, so do
4219 * that here. */
4220 setcursor();
Bram Moolenaara338adc2018-01-31 20:51:47 +01004221 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004222
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004223 return res;
4224}
4225
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004226/*
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004227 * Evaluate a command or expression sent to ourselves.
4228 */
4229 int
4230sendToLocalVim(char_u *cmd, int asExpr, char_u **result)
4231{
4232 if (asExpr)
4233 {
4234 char_u *ret;
4235
4236 ret = eval_client_expr_to_string(cmd);
4237 if (result != NULL)
4238 {
4239 if (ret == NULL)
4240 {
4241 char *err = _(e_invexprmsg);
4242 size_t len = STRLEN(cmd) + STRLEN(err) + 5;
4243 char_u *msg;
4244
Bram Moolenaar1662ce12017-03-19 21:47:50 +01004245 msg = alloc((unsigned)len);
Bram Moolenaar7a43cb92017-03-18 18:15:16 +01004246 if (msg != NULL)
4247 vim_snprintf((char *)msg, len, "%s: \"%s\"", err, cmd);
4248 *result = msg;
4249 }
4250 else
4251 *result = ret;
4252 }
4253 else
4254 vim_free(ret);
4255 return ret == NULL ? -1 : 0;
4256 }
4257 server_to_input_buf(cmd);
4258 return 0;
4259}
4260
4261/*
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004262 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4263 * return an allocated string. Otherwise return "data".
4264 * "*tofree" is set to the result when it needs to be freed later.
4265 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004266 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004267serverConvert(
4268 char_u *client_enc UNUSED,
4269 char_u *data,
4270 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004271{
4272 char_u *res = data;
4273
4274 *tofree = NULL;
4275# ifdef FEAT_MBYTE
4276 if (client_enc != NULL && p_enc != NULL)
4277 {
4278 vimconv_T vimconv;
4279
4280 vimconv.vc_type = CONV_NONE;
4281 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4282 && vimconv.vc_type != CONV_NONE)
4283 {
4284 res = string_convert(&vimconv, data, NULL);
4285 if (res == NULL)
4286 res = data;
4287 else
4288 *tofree = res;
4289 }
4290 convert_setup(&vimconv, NULL, NULL);
4291 }
4292# endif
4293 return res;
4294}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004295#endif