blob: d20c4b09582fa3cb104a05d17cc5afaad66b4198 [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
13#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000014# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000015#endif
16
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017#ifdef __CYGWIN__
18# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000019# include <cygwin/version.h>
20# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
21 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000022# endif
23# include <limits.h>
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Maximum number of commands from + or -c arguments. */
27#define MAX_ARG_CMDS 10
28
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000029/* values for "window_layout" */
30#define WIN_HOR 1 /* "-o" horizontally split windows */
31#define WIN_VER 2 /* "-O" vertically split windows */
32#define WIN_TABS 3 /* "-p" windows on tab pages */
33
Bram Moolenaar58d98232005-07-23 22:25:46 +000034/* Struct for various parameters passed between main() and other functions. */
35typedef struct
36{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 int argc;
38 char **argv;
39
40 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000041 char_u *use_vimrc; /* vimrc from -u argument */
42
43 int n_commands; /* no. of commands from + or -c */
44 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
45 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
46 int n_pre_commands; /* no. of commands from --cmd */
47 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
48
49 int edit_type; /* type of editing to do */
50 char_u *tagname; /* tag from -t argument */
51#ifdef FEAT_QUICKFIX
52 char_u *use_ef; /* 'errorfile' from -q argument */
53#endif
54
55 int want_full_screen;
56 int stdout_isatty; /* is stdout a terminal? */
57 char_u *term; /* specified terminal name */
58#ifdef FEAT_CRYPT
59 int ask_for_key; /* -x argument */
60#endif
61 int no_swap_file; /* "-n" argument used */
62#ifdef FEAT_EVAL
63 int use_debug_break_level;
64#endif
65#ifdef FEAT_WINDOWS
66 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000067 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000068#endif
69
70#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000071 int serverArg; /* TRUE when argument for a server */
72 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000073 char_u *serverStr; /* remote server command */
74 char_u *serverStrEnc; /* encoding of serverStr */
75 char_u *servername; /* allocated name for our server */
76#endif
77#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
78 int literal; /* don't expand file names */
79#endif
80#ifdef MSWIN
81 int full_path; /* file name argument was full path */
82#endif
83#ifdef FEAT_DIFF
84 int diff_mode; /* start with 'diff' set */
85#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000086} mparm_T;
87
Bram Moolenaarc013cb62005-07-24 21:18:31 +000088/* Values for edit_type. */
89#define EDIT_NONE 0 /* no edit type yet */
90#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
91#define EDIT_STDIN 2 /* read file from stdin */
92#define EDIT_TAG 3 /* tag name argument given, use tagname */
93#define EDIT_QF 4 /* start in quickfix mode */
94
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010095#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000096static int file_owned __ARGS((char *fname));
97#endif
98static void mainerr __ARGS((int, char_u *));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010099#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100static void main_msg __ARGS((char *s));
101static void usage __ARGS((void));
102static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100103# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000104static void init_locale __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100105# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000106static void parse_command_name __ARGS((mparm_T *parmp));
107static void early_arg_scan __ARGS((mparm_T *parmp));
108static void command_line_scan __ARGS((mparm_T *parmp));
109static void check_tty __ARGS((mparm_T *parmp));
110static void read_stdin __ARGS((void));
111static void create_windows __ARGS((mparm_T *parmp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100112# ifdef FEAT_WINDOWS
Bram Moolenaard87c36e2015-04-03 14:56:49 +0200113static void edit_buffers __ARGS((mparm_T *parmp, char_u *cwd));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100114# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000115static void exe_pre_commands __ARGS((mparm_T *parmp));
116static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000117static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118static void main_start_gui __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100119# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120static void check_swap_exists_action __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100121# endif
122# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000123static void exec_on_server __ARGS((mparm_T *parmp));
124static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
126static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100127# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000128#endif
129
130
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000131/*
132 * Different types of error messages.
133 */
134static char *(main_errors[]) =
135{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000137#define ME_UNKNOWN_OPTION 0
138 N_("Too many edit arguments"),
139#define ME_TOO_MANY_ARGS 1
140 N_("Argument missing after"),
141#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000143#define ME_GARBAGE 3
144 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
145#define ME_EXTRA_CMD 4
146 N_("Invalid argument for"),
147#define ME_INVALID_ARG 5
148};
149
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100150#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100151#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +0200152
153static char_u *start_dir = NULL; /* current working dir on startup */
154
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000155 int
156# ifdef VIMDLL
157_export
158# endif
159# ifdef FEAT_GUI_MSWIN
160# ifdef __BORLANDC__
161_cdecl
162# endif
163VimMain
164# else
165main
166# endif
167(argc, argv)
168 int argc;
169 char **argv;
170{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000171 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000172 mparm_T params; /* various parameters passed between
173 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000174#ifdef STARTUPTIME
175 int i;
176#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000177
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178 /*
179 * Do any system-specific initialisations. These can NOT use IObuff or
180 * NameBuff. Thus emsg2() cannot be called!
181 */
182 mch_early_init();
183
Bram Moolenaar14993322014-09-09 12:25:33 +0200184#if defined(WIN32) && defined(FEAT_MBYTE)
185 /*
186 * MingW expands command line arguments, which confuses our code to
187 * convert when 'encoding' changes. Get the unexpanded arguments.
188 */
189 argc = get_cmd_argsW(&argv);
190#endif
191
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000192 /* Many variables are in "params" so that we can pass them to invoked
193 * functions without a lot of arguments. "argc" and "argv" are also
194 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000195 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000196 params.argc = argc;
197 params.argv = argv;
198 params.want_full_screen = TRUE;
199#ifdef FEAT_EVAL
200 params.use_debug_break_level = -1;
201#endif
202#ifdef FEAT_WINDOWS
203 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000204#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000205
Bram Moolenaar99685e62013-05-11 13:56:18 +0200206#ifdef FEAT_RUBY
207 {
208 int ruby_stack_start;
209 vim_ruby_init((void *)&ruby_stack_start);
210 }
211#endif
212
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000214 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215#endif
216
217#ifdef MEM_PROFILE
218 atexit(vim_mem_profile_dump);
219#endif
220
221#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000222 for (i = 1; i < argc; ++i)
223 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000224 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000225 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000226 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000227 TIME_MSG("--- VIM STARTING ---");
228 break;
229 }
230 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000232 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233
234#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000235 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000236#endif
237
238#ifdef FEAT_MBYTE
239 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
240#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000241#ifdef FEAT_EVAL
242 eval_init(); /* init global variables */
243#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244
245#ifdef __QNXNTO__
246 qnx_init(); /* PhAttach() for clipboard, (and gui) */
247#endif
248
249#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000250 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000251 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000252 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000253 TIME_MSG("GUI prepared");
254#endif
255
256 /* Init the table of Normal mode commands. */
257 init_normal_cmds();
258
259#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000260 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000261#endif
262
263 /*
264 * Allocate space for the generic buffers (needed for set_init_1() and
265 * EMSG2()).
266 */
267 if ((IObuff = alloc(IOSIZE)) == NULL
268 || (NameBuff = alloc(MAXPATHL)) == NULL)
269 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 TIME_MSG("Allocated generic buffers");
271
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000272#ifdef NBDEBUG
273 /* Wait a moment for debugging NetBeans. Must be after allocating
274 * NameBuff. */
275 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
276 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000277 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000278#endif
279
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
281 /*
282 * Setup to use the current locale (for ctype() and many other things).
283 * NOTE: Translated messages with encodings other than latin1 will not
284 * work until set_init_1() has been called!
285 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287 TIME_MSG("locale set");
288#endif
289
290#ifdef FEAT_GUI
291 gui.dofork = TRUE; /* default is to use fork() */
292#endif
293
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000294 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000295 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000297 * --server...
298 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000299 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000301 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302
303#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000304 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305#endif
306#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000307 /* Prepare for possibly starting GUI sometime */
308 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000309 TIME_MSG("GUI prepared");
310#endif
311
312#ifdef FEAT_CLIPBOARD
313 clip_init(FALSE); /* Initialise clipboard stuff */
314 TIME_MSG("clipboard setup");
315#endif
316
317 /*
318 * Check if we have an interactive window.
319 * On the Amiga: If there is no window, we open one with a newcli command
320 * (needed for :! to * work). mch_check_win() will also handle the -d or
321 * -dev argument.
322 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000323 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324 TIME_MSG("window checked");
325
326 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000327 * Allocate the first window and buffer.
328 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000330 if (win_alloc_first() == FAIL)
331 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000332
333 init_yank(); /* init yank buffers */
334
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000335 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200336 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000337
338 /*
339 * Set the default values for the options.
340 * NOTE: Non-latin1 translated messages are working only after this,
341 * because this is where "has_mbyte" will be set, which is used by
342 * msg_outtrans_len_attr().
343 * First find out the home directory, needed to expand "~" in options.
344 */
345 init_homedir(); /* find real value of $HOME */
346 set_init_1();
347 TIME_MSG("inits 1");
348
349#ifdef FEAT_EVAL
350 set_lang_var(); /* set v:lang and v:ctype */
351#endif
352
353#ifdef FEAT_CLIENTSERVER
354 /*
355 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000356 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000357 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000358 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359#endif
360
361 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000362 * Figure out the way to work from the command name argv[0].
363 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000365 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366
367 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000368 * Process the command line arguments. File names are put in the global
369 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000371 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372 TIME_MSG("parsing arguments");
373
374 /*
375 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000376 * there is no terminal version, and on Windows we can't fork one off with
377 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000378 */
379#ifdef ALWAYS_USE_GUI
380 gui.starting = TRUE;
381#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000382# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 /*
384 * Check if the GUI can be started. Reset gui.starting if not.
385 * Don't know about other systems, stay on the safe side and don't check.
386 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000387 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000389 if (gui_init_check() == FAIL)
390 {
391 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000392
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000393 /* When running "evim" or "gvim -y" we need the menus, exit if we
394 * don't have them. */
395 if (params.evim_mode)
396 mch_exit(1);
397 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 }
399# endif
400#endif
401
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000402 if (GARGCOUNT > 0)
403 {
404#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
405 /*
406 * Expand wildcards in file names.
407 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000408 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000409 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200410 start_dir = alloc(MAXPATHL);
411 if (start_dir != NULL)
412 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000413 /* Temporarily add '(' and ')' to 'isfname'. These are valid
414 * filename characters but are excluded from 'isfname' to make
415 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
416 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000417 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000418 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200419 if (start_dir != NULL)
420 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000421 }
422#endif
423 fname = alist_name(&GARGLIST[0]);
424 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000425
426#if defined(WIN32) && defined(FEAT_MBYTE)
427 {
428 extern void set_alist_count(void);
429
430 /* Remember the number of entries in the argument list. If it changes
431 * we don't react on setting 'encoding'. */
432 set_alist_count();
433 }
434#endif
435
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000436#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000437 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000438 {
439 /*
440 * If there is one filename, fully qualified, we have very probably
441 * been invoked from explorer, so change to the file's directory.
442 * Hint: to avoid this when typing a command use a forward slash.
443 * If the cd fails, it doesn't matter.
444 */
445 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200446 if (start_dir != NULL)
447 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000448 }
449#endif
450 TIME_MSG("expanding arguments");
451
452#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000453 if (params.diff_mode && params.window_count == -1)
454 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000455#endif
456
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000457 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000458 ++RedrawingDisabled;
459
460 /*
461 * When listing swap file names, don't do cursor positioning et. al.
462 */
463 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000464 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000465
466 /*
467 * When certain to start the GUI, don't check capabilities of terminal.
468 * For GTK we can't be sure, but when started from the desktop it doesn't
469 * make sense to try using a terminal.
470 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000471#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000472 if (gui.starting
473# ifdef FEAT_GUI_GTK
474 && !isatty(2)
475# endif
476 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000477 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000478#endif
479
480#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
481 /* When the GUI is started from Finder, need to display messages in a
482 * message box. isatty(2) returns TRUE anyway, thus we need to check the
483 * name to know we're not started from a terminal. */
484 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000485 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000486 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000487
488 /* Avoid always using "/" as the current directory. Note that when
489 * started from Finder the arglist will be filled later in
490 * HandleODocAE() and "fname" will be NULL. */
491 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
492 && STRCMP(NameBuff, "/") == 0)
493 {
494 if (fname != NULL)
495 (void)vim_chdirfile(fname);
496 else
497 {
498 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
499 vim_chdir(NameBuff);
500 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200501 if (start_dir != NULL)
502 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000503 }
504 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000505#endif
506
507 /*
508 * mch_init() sets up the terminal (window) for use. This must be
509 * done after resetting full_screen, otherwise it may move the cursor
510 * (MSDOS).
511 * Note that we may use mch_exit() before mch_init()!
512 */
513 mch_init();
514 TIME_MSG("shell init");
515
516#ifdef USE_XSMP
517 /*
518 * For want of anywhere else to do it, try to connect to xsmp here.
519 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
520 * Hijacking -X 'no X connection' to also disable XSMP connection as that
521 * has a similar delay upon failure.
522 * Only try if SESSION_MANAGER is set to something non-null.
523 */
524 if (!x_no_connect)
525 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000526 char *p = getenv("SESSION_MANAGER");
527
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528 if (p != NULL && *p != NUL)
529 {
530 xsmp_init();
531 TIME_MSG("xsmp init");
532 }
533 }
534#endif
535
536 /*
537 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000539 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000540
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000541 /* This message comes before term inits, but after setting "silent_mode"
542 * when the input is not a tty. */
543 if (GARGCOUNT > 1 && !silent_mode)
544 printf(_("%d files to edit\n"), GARGCOUNT);
545
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000546 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000548 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000549 capabilities (will set full_screen) */
550 screen_start(); /* don't know where cursor is now */
551 TIME_MSG("Termcap init");
552 }
553
554 /*
555 * Set the default values for the options that use Rows and Columns.
556 */
557 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000558 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000559#ifdef FEAT_DIFF
560 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
561 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000562 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000563 diff_win_options(firstwin, FALSE);
564#endif
565
566 cmdline_row = Rows - p_ch;
567 msg_row = cmdline_row;
568 screenalloc(FALSE); /* allocate screen buffers */
569 set_init_2();
570 TIME_MSG("inits 2");
571
572 msg_scroll = TRUE;
573 no_wait_return = TRUE;
574
575 init_mappings(); /* set up initial mappings */
576
577 init_highlight(TRUE, FALSE); /* set the default highlight groups */
578 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000579
580#ifdef FEAT_EVAL
581 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000582 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000583#endif
584
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100585#ifdef FEAT_MZSCHEME
586 /*
587 * Newer version of MzScheme (Racket) require earlier (trampolined)
588 * initialisation via scheme_main_setup.
589 * Implement this by initialising it as early as possible
590 * and splitting off remaining Vim main into vim_main2
591 */
592 {
593 /* Pack up preprocessed command line arguments.
594 * It is safe because Scheme does not access argc/argv. */
595 char *args[2];
596 args[0] = (char *)fname;
597 args[1] = (char *)&params;
598 return mzscheme_main(2, args);
599 }
600}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100601#endif
602#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100603
Bram Moolenaar8866d272012-11-28 15:55:42 +0100604/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
605 * NO_VIM_MAIN is defined. */
606#ifdef FEAT_MZSCHEME
607 int
608vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100609{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100610# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100611 char_u *fname = (char_u *)argv[0];
612 mparm_T params;
613
614 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100615# else
616 return 0;
617}
618# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100619#endif
620
Bram Moolenaar8866d272012-11-28 15:55:42 +0100621#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000622 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000623 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000624
Bram Moolenaar58d98232005-07-23 22:25:46 +0000625 /* Source startup scripts. */
626 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000627
628#ifdef FEAT_EVAL
629 /*
630 * Read all the plugin files.
631 * Only when compiled with +eval, since most plugins need it.
632 */
633 if (p_lpl)
634 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000635# ifdef VMS /* Somehow VMS doesn't handle the "**". */
636 source_runtime((char_u *)"plugin/*.vim", TRUE);
637# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000638 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000639# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000640 TIME_MSG("loading plugins");
641 }
642#endif
643
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000644#ifdef FEAT_DIFF
645 /* Decide about window layout for diff mode after reading vimrc. */
646 if (params.diff_mode && params.window_layout == 0)
647 {
648 if (diffopt_horizontal())
649 params.window_layout = WIN_HOR; /* use horizontal split */
650 else
651 params.window_layout = WIN_VER; /* use vertical split */
652 }
653#endif
654
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000655 /*
656 * Recovery mode without a file name: List swap files.
657 * This uses the 'dir' option, therefore it must be after the
658 * initializations.
659 */
660 if (recoverymode && fname == NULL)
661 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200662 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000663 mch_exit(0);
664 }
665
666 /*
667 * Set a few option defaults after reading .vimrc files:
668 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
669 */
670 set_init_3();
671 TIME_MSG("inits 3");
672
673 /*
674 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
675 * Note that this overrides anything from a vimrc file.
676 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000677 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000678 p_uc = 0;
679
680#ifdef FEAT_FKMAP
681 if (curwin->w_p_rl && p_altkeymap)
682 {
683 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
684# ifdef FEAT_ARABIC
685 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
686# endif
687 p_fkmap = TRUE; /* Set the Farsi keymap mode */
688 }
689#endif
690
691#ifdef FEAT_GUI
692 if (gui.starting)
693 {
694#if defined(UNIX) || defined(VMS)
695 /* When something caused a message from a vimrc script, need to output
696 * an extra newline before the shell prompt. */
697 if (did_emsg || msg_didout)
698 putchar('\n');
699#endif
700
701 gui_start(); /* will set full_screen to TRUE */
702 TIME_MSG("starting GUI");
703
704 /* When running "evim" or "gvim -y" we need the menus, exit if we
705 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000706 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000707 mch_exit(1);
708 }
709#endif
710
711#ifdef SPAWNO /* special MSDOS swapping library */
712 init_SPAWNO("", SWAP_ANY);
713#endif
714
715#ifdef FEAT_VIMINFO
716 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000717 * Read in registers, history etc, but not marks, from the viminfo file.
718 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000719 */
720 if (*p_viminfo != NUL)
721 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000722 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000723 TIME_MSG("reading viminfo");
724 }
725#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100726#ifdef FEAT_EVAL
727 /* It's better to make v:oldfiles an empty list than NULL. */
728 if (get_vim_var_list(VV_OLDFILES) == NULL)
729 set_vim_var_list(VV_OLDFILES, list_alloc());
730#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000731
732#ifdef FEAT_QUICKFIX
733 /*
734 * "-q errorfile": Load the error file now.
735 * If the error file can't be read, exit before doing anything else.
736 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000737 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000739 if (params.use_ef != NULL)
740 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000741 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200742 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
743 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000744 {
745 out_char('\n');
746 mch_exit(3);
747 }
748 TIME_MSG("reading errorfile");
749 }
750#endif
751
752 /*
753 * Start putting things on the screen.
754 * Scroll screen down before drawing over it
755 * Clear screen now, so file message will not be cleared.
756 */
757 starting = NO_BUFFERS;
758 no_wait_return = FALSE;
759 if (!exmode_active)
760 msg_scroll = FALSE;
761
762#ifdef FEAT_GUI
763 /*
764 * This seems to be required to make callbacks to be called now, instead
765 * of after things have been put on the screen, which then may be deleted
766 * when getting a resize callback.
767 * For the Mac this handles putting files dropped on the Vim icon to
768 * global_alist.
769 */
770 if (gui.in_use)
771 {
772# ifdef FEAT_SUN_WORKSHOP
773 if (!usingSunWorkShop)
774# endif
775 gui_wait_for_chars(50L);
776 TIME_MSG("GUI delay");
777 }
778#endif
779
780#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
781 qnx_clip_init();
782#endif
783
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200784#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
785 clip_init(TRUE);
786#endif
787
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000788#ifdef FEAT_XCLIPBOARD
789 /* Start using the X clipboard, unless the GUI was started. */
790# ifdef FEAT_GUI
791 if (!gui.in_use)
792# endif
793 {
794 setup_term_clip();
795 TIME_MSG("setup clipboard");
796 }
797#endif
798
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000799#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000800 /* Prepare for being a Vim server. */
801 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000802#endif
803
804 /*
805 * If "-" argument given: Read file from stdin.
806 * Do this before starting Raw mode, because it may change things that the
807 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
808 * are the same terminal: "cat | vim -".
809 * Using autocommands here may cause trouble...
810 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000811 if (params.edit_type == EDIT_STDIN && !recoverymode)
812 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813
814#if defined(UNIX) || defined(VMS)
815 /* When switching screens and something caused a message from a vimrc
816 * script, need to output an extra newline on exit. */
817 if ((did_emsg || msg_didout) && *T_TI != NUL)
818 newline_on_exit = TRUE;
819#endif
820
821 /*
822 * When done something that is not allowed or error message call
823 * wait_return. This must be done before starttermcap(), because it may
824 * switch to another screen. It must be done after settmode(TMODE_RAW),
825 * because we want to react on a single key stroke.
826 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000827 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000828 */
829 settmode(TMODE_RAW);
830 TIME_MSG("setting raw mode");
831
832 if (need_wait_return || msg_didany)
833 {
834 wait_return(TRUE);
835 TIME_MSG("waiting for return");
836 }
837
838 starttermcap(); /* start termcap if not done by wait_return() */
839 TIME_MSG("start termcap");
Bram Moolenaar9584b312013-03-13 19:29:28 +0100840#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200841 may_req_ambiguous_char_width();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100842#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000843
844#ifdef FEAT_MOUSE
845 setmouse(); /* may start using the mouse */
846#endif
847 if (scroll_region)
848 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000849 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000850
851 /*
852 * Don't clear the screen when starting in Ex mode, unless using the GUI.
853 */
854 if (exmode_active
855#ifdef FEAT_GUI
856 && !gui.in_use
857#endif
858 )
859 must_redraw = CLEAR;
860 else
861 {
862 screenclear(); /* clear screen */
863 TIME_MSG("clearing screen");
864 }
865
866#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000867 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100869 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200870 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000871 TIME_MSG("getting crypt key");
872 }
873#endif
874
875 no_wait_return = TRUE;
876
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000877 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000878 * Create the requested number of windows and edit buffers in them.
879 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000881 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882 TIME_MSG("opening buffers");
883
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000884#ifdef FEAT_EVAL
885 /* clear v:swapcommand */
886 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
887#endif
888
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000889 /* Ex starts at last line of the file */
890 if (exmode_active)
891 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
892
893#ifdef FEAT_AUTOCMD
894 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
895 TIME_MSG("BufEnter autocommands");
896#endif
897 setpcmark();
898
899#ifdef FEAT_QUICKFIX
900 /*
901 * When started with "-q errorfile" jump to first error now.
902 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000903 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000904 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000905 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000906 TIME_MSG("jump to first error");
907 }
908#endif
909
910#ifdef FEAT_WINDOWS
911 /*
912 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000913 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000914 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200915 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000916#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200917 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918
919#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000920 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000921 {
922 win_T *wp;
923
924 /* set options in each window for "vimdiff". */
925 for (wp = firstwin; wp != NULL; wp = wp->w_next)
926 diff_win_options(wp, TRUE);
927 }
928#endif
929
930 /*
931 * Shorten any of the filenames, but only when absolute.
932 */
933 shorten_fnames(FALSE);
934
935 /*
936 * Need to jump to the tag before executing the '-c command'.
937 * Makes "vim -c '/return' -t main" work.
938 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000939 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000940 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000941#if defined(HAS_SWAP_EXISTS_ACTION)
942 swap_exists_did_quit = FALSE;
943#endif
944
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000945 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000946 do_cmdline_cmd(IObuff);
947 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000948
949#if defined(HAS_SWAP_EXISTS_ACTION)
950 /* If the user doesn't want to edit the file then we quit here. */
951 if (swap_exists_did_quit)
952 getout(1);
953#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000954 }
955
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000956 /* Execute any "+", "-c" and "-S" arguments. */
957 if (params.n_commands > 0)
958 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000959
960 RedrawingDisabled = 0;
961 redraw_all_later(NOT_VALID);
962 no_wait_return = FALSE;
963 starting = 0;
964
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000965#ifdef FEAT_TERMRESPONSE
966 /* Requesting the termresponse is postponed until here, so that a "-c q"
967 * argument doesn't make it appear in the shell Vim was started from. */
968 may_req_termresponse();
969#endif
970
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000971 /* start in insert mode */
972 if (p_im)
973 need_start_insertmode = TRUE;
974
975#ifdef FEAT_AUTOCMD
976 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
977 TIME_MSG("VimEnter autocommands");
978#endif
979
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200980#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
981 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
982 * done after the clipboard is available and all initial commands that may
983 * modify the 'clipboard' setting have run; i.e. just before entering the
984 * main loop. */
985 {
986 int default_regname = 0;
987 adjust_clip_reg(&default_regname);
988 set_reg_var(default_regname);
989 }
990#endif
991
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000992#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
993 /* When a startup script or session file setup for diff'ing and
994 * scrollbind, sync the scrollbind now. */
995 if (curwin->w_p_diff && curwin->w_p_scb)
996 {
997 update_topline();
998 check_scrollbind((linenr_T)0, 0L);
999 TIME_MSG("diff scrollbinding");
1000 }
1001#endif
1002
1003#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1004 mch_set_winsize_now(); /* Allow winsize changes from now on */
1005#endif
1006
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001007#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1008 /* When tab pages were created, may need to update the tab pages line and
1009 * scrollbars. This is skipped while creating them. */
1010 if (first_tabpage->tp_next != NULL)
1011 {
1012 out_flush();
1013 gui_init_which_components(NULL);
1014 gui_update_scrollbars(TRUE);
1015 }
1016 need_mouse_correct = TRUE;
1017#endif
1018
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001019 /* If ":startinsert" command used, stuff a dummy command to be able to
1020 * call normal_cmd(), which will then start Insert mode. */
1021 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001022 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001023
1024#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001025 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001026 {
1027# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001028# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001029 && !defined(FEAT_GUI_W32)
1030 if (gui.in_use)
1031 {
1032 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1033 mch_exit(2);
1034 }
1035# endif
1036# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001037 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001038 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001039 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001040#endif
1041
1042 TIME_MSG("before starting main loop");
1043
1044 /*
1045 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001046 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001047 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001048
1049 return 0;
1050}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001051#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001052#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001053
1054/*
1055 * Main loop: Execute Normal mode commands until exiting Vim.
1056 * Also used to handle commands in the command-line window, until the window
1057 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001058 * Also used to handle ":visual" command after ":global": execute Normal mode
1059 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001060 */
1061 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062main_loop(cmdwin, noexmode)
1063 int cmdwin; /* TRUE when working in the command-line window */
1064 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001065{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001066 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001067 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001068#ifdef FEAT_CONCEAL
1069 linenr_T conceal_old_cursor_line = 0;
1070 linenr_T conceal_new_cursor_line = 0;
1071 int conceal_update_lines = FALSE;
1072#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001073
1074#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1075 /* Setup to catch a terminating error from the X server. Just ignore
1076 * it, restore the state and continue. This might not always work
1077 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001078 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001079 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001080 {
1081 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001082 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001083 got_int = TRUE;
1084 need_wait_return = FALSE;
1085 global_busy = FALSE;
1086 exmode_active = 0;
1087 skip_redraw = FALSE;
1088 RedrawingDisabled = 0;
1089 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001090 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001091# ifdef FEAT_EVAL
1092 emsg_skip = 0;
1093# endif
1094 emsg_off = 0;
1095# ifdef FEAT_MOUSE
1096 setmouse();
1097# endif
1098 settmode(TMODE_RAW);
1099 starttermcap();
1100 scroll_start();
1101 redraw_later_clear();
1102 }
1103#endif
1104
1105 clear_oparg(&oa);
1106 while (!cmdwin
1107#ifdef FEAT_CMDWIN
1108 || cmdwin_result == 0
1109#endif
1110 )
1111 {
1112 if (stuff_empty())
1113 {
1114 did_check_timestamps = FALSE;
1115 if (need_check_timestamps)
1116 check_timestamps(FALSE);
1117 if (need_wait_return) /* if wait_return still needed ... */
1118 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001119 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001120 {
1121 need_start_insertmode = FALSE;
1122 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1123 /* skip the fileinfo message now, because it would be shown
1124 * after insert mode finishes! */
1125 need_fileinfo = FALSE;
1126 }
1127 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001128
1129 /* Reset "got_int" now that we got back to the main loop. Except when
1130 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1131 * the ":g" command.
1132 * For ":g/pat/vi" we reset "got_int" when used once. When used
1133 * a second time we go back to Ex mode and abort the ":g" command. */
1134 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001135 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001136 if (noexmode && global_busy && !exmode_active && previous_got_int)
1137 {
1138 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1139 * used and keep "got_int" set, so that it aborts ":g". */
1140 exmode_active = EXMODE_NORMAL;
1141 State = NORMAL;
1142 }
1143 else if (!global_busy || !exmode_active)
1144 {
1145 if (!quit_more)
1146 (void)vgetc(); /* flush all buffers */
1147 got_int = FALSE;
1148 }
1149 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001150 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001151 else
1152 previous_got_int = FALSE;
1153
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001154 if (!exmode_active)
1155 msg_scroll = FALSE;
1156 quit_more = FALSE;
1157
1158 /*
1159 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1160 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1161 * update cursor and redraw.
1162 */
1163 if (skip_redraw || exmode_active)
1164 skip_redraw = FALSE;
1165 else if (do_redraw || stuff_empty())
1166 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001168 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001169 if (!finish_op && (
1170# ifdef FEAT_AUTOCMD
1171 has_cursormoved()
1172# endif
1173# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1174 ||
1175# endif
1176# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001177 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001178# endif
1179 )
1180 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001181 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001182# ifdef FEAT_AUTOCMD
1183 if (has_cursormoved())
1184 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1185 FALSE, curbuf);
1186# endif
1187# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001188 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001189 {
1190 conceal_old_cursor_line = last_cursormoved.lnum;
1191 conceal_new_cursor_line = curwin->w_cursor.lnum;
1192 conceal_update_lines = TRUE;
1193 }
1194# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001195 last_cursormoved = curwin->w_cursor;
1196 }
1197#endif
1198
Bram Moolenaar186628f2013-03-19 13:33:23 +01001199#ifdef FEAT_AUTOCMD
1200 /* Trigger TextChanged if b_changedtick differs. */
1201 if (!finish_op && has_textchanged()
1202 && last_changedtick != curbuf->b_changedtick)
1203 {
1204 if (last_changedtick_buf == curbuf)
1205 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1206 FALSE, curbuf);
1207 last_changedtick_buf = curbuf;
1208 last_changedtick = curbuf->b_changedtick;
1209 }
1210#endif
1211
Bram Moolenaar33aec762006-01-22 23:30:12 +00001212#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1213 /* Scroll-binding for diff mode may have been postponed until
1214 * here. Avoids doing it for every change. */
1215 if (diff_need_scrollbind)
1216 {
1217 check_scrollbind((linenr_T)0, 0L);
1218 diff_need_scrollbind = FALSE;
1219 }
1220#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001221#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001222 /* Include a closed fold completely in the Visual area. */
1223 foldAdjustVisual();
1224#endif
1225#ifdef FEAT_FOLDING
1226 /*
1227 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1228 * contain the cursor.
1229 * When 'foldopen' is "all", open the fold(s) under the cursor.
1230 * This may mark the window for redrawing.
1231 */
1232 if (hasAnyFolding(curwin) && !char_avail())
1233 {
1234 foldCheckClose();
1235 if (fdo_flags & FDO_ALL)
1236 foldOpenCursor();
1237 }
1238#endif
1239
1240 /*
1241 * Before redrawing, make sure w_topline is correct, and w_leftcol
1242 * if lines don't wrap, and w_skipcol if lines wrap.
1243 */
1244 update_topline();
1245 validate_cursor();
1246
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001247 if (VIsual_active)
1248 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001249 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001250 update_screen(0);
1251 else if (redraw_cmdline || clear_cmdline)
1252 showmode();
1253#ifdef FEAT_WINDOWS
1254 redraw_statuslines();
1255#endif
1256#ifdef FEAT_TITLE
1257 if (need_maketitle)
1258 maketitle();
1259#endif
1260 /* 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 Moolenaarc2b4c622011-02-15 16:29:59 +01001290 if (conceal_old_cursor_line != conceal_new_cursor_line
1291 && conceal_old_cursor_line
1292 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001293 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001294 update_single_line(curwin, conceal_new_cursor_line);
1295 curwin->w_valid &= ~VALID_CROW;
1296 }
1297# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001298 setcursor();
1299 cursor_on();
1300
1301 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001302
1303#ifdef STARTUPTIME
1304 /* Now that we have drawn the first screen all the startup stuff
1305 * has been done, close any file for startup messages. */
1306 if (time_fd != NULL)
1307 {
1308 TIME_MSG("first screen update");
1309 TIME_MSG("--- VIM STARTED ---");
1310 fclose(time_fd);
1311 time_fd = NULL;
1312 }
1313#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001314 }
1315#ifdef FEAT_GUI
1316 if (need_mouse_correct)
1317 gui_mouse_correct();
1318#endif
1319
1320 /*
1321 * Update w_curswant if w_set_curswant has been set.
1322 * Postponed until here to avoid computing w_virtcol too often.
1323 */
1324 update_curswant();
1325
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001326#ifdef FEAT_EVAL
1327 /*
1328 * May perform garbage collection when waiting for a character, but
1329 * only at the very toplevel. Otherwise we may be using a List or
1330 * Dict internally somewhere.
1331 * "may_garbage_collect" is reset in vgetc() which is invoked through
1332 * do_exmode() and normal_cmd().
1333 */
1334 may_garbage_collect = (!cmdwin && !noexmode);
1335#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001336 /*
1337 * If we're invoked as ex, do a round of ex commands.
1338 * Otherwise, get and execute a normal mode command.
1339 */
1340 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001341 {
1342 if (noexmode) /* End of ":global/path/visual" commands */
1343 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001344 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001345 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001346 else
1347 normal_cmd(&oa, TRUE);
1348 }
1349}
1350
1351
1352#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1353/*
1354 * Exit, but leave behind swap files for modified buffers.
1355 */
1356 void
1357getout_preserve_modified(exitval)
1358 int exitval;
1359{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001360# if defined(SIGHUP) && defined(SIG_IGN)
1361 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1362 * makes Vim exit and then handling SIGHUP causes various reentrance
1363 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001364 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001365# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001366
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001367 ml_close_notmod(); /* close all not-modified buffers */
1368 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1369 ml_close_all(FALSE); /* close all memfiles, without deleting */
1370 getout(exitval); /* exit Vim properly */
1371}
1372#endif
1373
1374
1375/* Exit properly */
1376 void
1377getout(exitval)
1378 int exitval;
1379{
1380#ifdef FEAT_AUTOCMD
1381 buf_T *buf;
1382 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001383 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001384#endif
1385
1386 exiting = TRUE;
1387
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001388 /* When running in Ex mode an error causes us to exit with a non-zero exit
1389 * code. POSIX requires this, although it's not 100% clear from the
1390 * standard. */
1391 if (exmode_active)
1392 exitval += ex_exitval;
1393
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001394 /* Position the cursor on the last screen line, below all the text */
1395#ifdef FEAT_GUI
1396 if (!gui.in_use)
1397#endif
1398 windgoto((int)Rows - 1, 0);
1399
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001400#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1401 /* Optionally print hashtable efficiency. */
1402 hash_debug_results();
1403#endif
1404
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001405#ifdef FEAT_GUI
1406 msg_didany = FALSE;
1407#endif
1408
1409#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001410 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001411 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001412 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1413# if defined FEAT_WINDOWS
1414 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001415 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001416 next_tp = tp->tp_next;
1417 for (wp = (tp == curtab)
1418 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001419 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001420 if (wp->w_buffer == NULL)
1421 /* Autocmd must have close the buffer already, skip. */
1422 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001423 buf = wp->w_buffer;
1424 if (buf->b_changedtick != -1)
1425 {
1426 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1427 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001428 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001429 /* start all over, autocommands may mess up the lists */
1430 next_tp = first_tabpage;
1431 break;
1432 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001433 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001434 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001435# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001436 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1437 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001438# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001439
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001440 /* Trigger BufUnload for buffers that are loaded */
1441 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1442 if (buf->b_ml.ml_mfp != NULL)
1443 {
1444 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001445 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001446 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1447 break;
1448 }
1449 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1450 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001451#endif
1452
1453#ifdef FEAT_VIMINFO
1454 if (*p_viminfo != NUL)
1455 /* Write out the registers, history, marks etc, to the viminfo file */
1456 write_viminfo(NULL, FALSE);
1457#endif
1458
1459#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001460 if (get_vim_var_nr(VV_DYING) <= 1)
1461 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001462#endif
1463
Bram Moolenaar05159a02005-02-26 23:04:13 +00001464#ifdef FEAT_PROFILE
1465 profile_dump();
1466#endif
1467
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001468 if (did_emsg
1469#ifdef FEAT_GUI
1470 || (gui.in_use && msg_didany && p_verbose > 0)
1471#endif
1472 )
1473 {
1474 /* give the user a chance to read the (error) message */
1475 no_wait_return = FALSE;
1476 wait_return(FALSE);
1477 }
1478
1479#ifdef FEAT_AUTOCMD
1480 /* Position the cursor again, the autocommands may have moved it */
1481# ifdef FEAT_GUI
1482 if (!gui.in_use)
1483# endif
1484 windgoto((int)Rows - 1, 0);
1485#endif
1486
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001487#ifdef FEAT_LUA
1488 lua_end();
1489#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001490#ifdef FEAT_MZSCHEME
1491 mzscheme_end();
1492#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001493#ifdef FEAT_TCL
1494 tcl_end();
1495#endif
1496#ifdef FEAT_RUBY
1497 ruby_end();
1498#endif
1499#ifdef FEAT_PYTHON
1500 python_end();
1501#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001502#ifdef FEAT_PYTHON3
1503 python3_end();
1504#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001505#ifdef FEAT_PERL
1506 perl_end();
1507#endif
1508#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1509 iconv_end();
1510#endif
1511#ifdef FEAT_NETBEANS_INTG
1512 netbeans_end();
1513#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001514#ifdef FEAT_CSCOPE
1515 cs_end();
1516#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001517#ifdef FEAT_EVAL
1518 if (garbage_collect_at_exit)
1519 garbage_collect();
1520#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001521#if defined(WIN32) && defined(FEAT_MBYTE)
1522 free_cmd_argsW();
1523#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001524
1525 mch_exit(exitval);
1526}
1527
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001528#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001529/*
1530 * Get a (optional) count for a Vim argument.
1531 */
1532 static int
1533get_number_arg(p, idx, def)
1534 char_u *p; /* pointer to argument */
1535 int *idx; /* index in argument, is incremented */
1536 int def; /* default value */
1537{
1538 if (vim_isdigit(p[*idx]))
1539 {
1540 def = atoi((char *)&(p[*idx]));
1541 while (vim_isdigit(p[*idx]))
1542 *idx = *idx + 1;
1543 }
1544 return def;
1545}
1546
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001547#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1548/*
1549 * Setup to use the current locale (for ctype() and many other things).
1550 */
1551 static void
1552init_locale()
1553{
1554 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001555
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001556# ifdef FEAT_GUI_GTK
1557 /* Tell Gtk not to change our locale settings. */
1558 gtk_disable_setlocale();
1559# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001560# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1561 /* Make sure strtod() uses a decimal point, not a comma. */
1562 setlocale(LC_NUMERIC, "C");
1563# endif
1564
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001565# ifdef WIN32
1566 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1567 * text while it's expecting text in the current locale. This call avoids
1568 * that. */
1569 setlocale(LC_CTYPE, "C");
1570# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001571
1572# ifdef FEAT_GETTEXT
1573 {
1574 int mustfree = FALSE;
1575 char_u *p;
1576
1577# ifdef DYNAMIC_GETTEXT
1578 /* Initialize the gettext library */
1579 dyn_libintl_init(NULL);
1580# endif
1581 /* expand_env() doesn't work yet, because chartab[] is not initialized
1582 * yet, call vim_getenv() directly */
1583 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1584 if (p != NULL && *p != NUL)
1585 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001586 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001587 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1588 }
1589 if (mustfree)
1590 vim_free(p);
1591 textdomain(VIMPACKAGE);
1592 }
1593# endif
1594}
1595#endif
1596
1597/*
1598 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1599 * If the executable name starts with "r" we disable shell commands.
1600 * If the next character is "e" we run in Easy mode.
1601 * If the next character is "g" we run the GUI version.
1602 * If the next characters are "view" we start in readonly mode.
1603 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1604 * If the next characters are "ex" we start in Ex mode. If it's followed
1605 * by "im" use improved Ex mode.
1606 */
1607 static void
1608parse_command_name(parmp)
1609 mparm_T *parmp;
1610{
1611 char_u *initstr;
1612
1613 initstr = gettail((char_u *)parmp->argv[0]);
1614
1615#ifdef MACOS_X_UNIX
1616 /* An issue has been seen when launching Vim in such a way that
1617 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1618 * executable or a symbolic link of it. Until this issue is resolved
1619 * we prohibit the GUI from being used.
1620 */
1621 if (STRCMP(initstr, parmp->argv[0]) == 0)
1622 disallow_gui = TRUE;
1623
1624 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001625 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001626#endif
1627
1628#ifdef FEAT_EVAL
1629 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001630 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001631#endif
1632
1633 if (TOLOWER_ASC(initstr[0]) == 'r')
1634 {
1635 restricted = TRUE;
1636 ++initstr;
1637 }
1638
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001639 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001640 if (TOLOWER_ASC(initstr[0]) == 'e'
1641 && (TOLOWER_ASC(initstr[1]) == 'v'
1642 || TOLOWER_ASC(initstr[1]) == 'g'))
1643 {
1644#ifdef FEAT_GUI
1645 gui.starting = TRUE;
1646#endif
1647 parmp->evim_mode = TRUE;
1648 ++initstr;
1649 }
1650
Bram Moolenaar806875d2008-09-18 18:57:10 +00001651 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1652 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001653 {
1654 main_start_gui();
1655#ifdef FEAT_GUI
1656 ++initstr;
1657#endif
1658 }
1659
1660 if (STRNICMP(initstr, "view", 4) == 0)
1661 {
1662 readonlymode = TRUE;
1663 curbuf->b_p_ro = TRUE;
1664 p_uc = 10000; /* don't update very often */
1665 initstr += 4;
1666 }
1667 else if (STRNICMP(initstr, "vim", 3) == 0)
1668 initstr += 3;
1669
1670 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1671 if (STRICMP(initstr, "diff") == 0)
1672 {
1673#ifdef FEAT_DIFF
1674 parmp->diff_mode = TRUE;
1675#else
1676 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1677 mch_errmsg("\n");
1678 mch_exit(2);
1679#endif
1680 }
1681
1682 if (STRNICMP(initstr, "ex", 2) == 0)
1683 {
1684 if (STRNICMP(initstr + 2, "im", 2) == 0)
1685 exmode_active = EXMODE_VIM;
1686 else
1687 exmode_active = EXMODE_NORMAL;
1688 change_compatible(TRUE); /* set 'compatible' */
1689 }
1690}
1691
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001692/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001693 * Get the name of the display, before gui_prepare() removes it from
1694 * argv[]. Used for the xterm-clipboard display.
1695 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001696 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001697 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001698 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001699early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001700 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001701{
Bram Moolenaar03005972008-11-20 13:12:36 +00001702#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1703 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001704 int argc = parmp->argc;
1705 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001706 int i;
1707
1708 for (i = 1; i < argc; i++)
1709 {
1710 if (STRCMP(argv[i], "--") == 0)
1711 break;
1712# ifdef FEAT_XCLIPBOARD
1713 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001714# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001715 || STRICMP(argv[i], "--display") == 0
1716# endif
1717 )
1718 {
1719 if (i == argc - 1)
1720 mainerr_arg_missing((char_u *)argv[i]);
1721 xterm_display = argv[++i];
1722 }
1723# endif
1724# ifdef FEAT_CLIENTSERVER
1725 else if (STRICMP(argv[i], "--servername") == 0)
1726 {
1727 if (i == argc - 1)
1728 mainerr_arg_missing((char_u *)argv[i]);
1729 parmp->serverName_arg = (char_u *)argv[++i];
1730 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001731 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001732 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001733 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001734 {
1735 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001736# ifdef FEAT_GUI
1737 if (strstr(argv[i], "-wait") != 0)
1738 /* don't fork() when starting the GUI to edit files ourself */
1739 gui.dofork = FALSE;
1740# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001741 }
1742# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001743
1744# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1745# ifdef FEAT_GUI_W32
1746 else if (STRICMP(argv[i], "--windowid") == 0)
1747# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001748 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001749# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001750 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001751 long_u id;
1752 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001753
1754 if (i == argc - 1)
1755 mainerr_arg_missing((char_u *)argv[i]);
1756 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001757 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001759 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001760 if (count != 1)
1761 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1762 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001763# ifdef FEAT_GUI_W32
1764 win_socket_id = id;
1765# else
1766 gtk_socket_id = id;
1767# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001768 i++;
1769 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001770# endif
1771# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001772 else if (STRICMP(argv[i], "--echo-wid") == 0)
1773 echo_wid_arg = TRUE;
1774# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001775# ifndef FEAT_NETBEANS_INTG
1776 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001777 {
1778 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1779 mch_exit(2);
1780 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001781# endif
1782
Bram Moolenaar58d98232005-07-23 22:25:46 +00001783 }
1784#endif
1785}
1786
1787/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001788 * Scan the command line arguments.
1789 */
1790 static void
1791command_line_scan(parmp)
1792 mparm_T *parmp;
1793{
1794 int argc = parmp->argc;
1795 char **argv = parmp->argv;
1796 int argv_idx; /* index in argv[n][] */
1797 int had_minmin = FALSE; /* found "--" argument */
1798 int want_argument; /* option argument with argument */
1799 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001800 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001801 long n;
1802
1803 --argc;
1804 ++argv;
1805 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1806 while (argc > 0)
1807 {
1808 /*
1809 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1810 */
1811 if (argv[0][0] == '+' && !had_minmin)
1812 {
1813 if (parmp->n_commands >= MAX_ARG_CMDS)
1814 mainerr(ME_EXTRA_CMD, NULL);
1815 argv_idx = -1; /* skip to next argument */
1816 if (argv[0][1] == NUL)
1817 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1818 else
1819 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1820 }
1821
1822 /*
1823 * Optional argument.
1824 */
1825 else if (argv[0][0] == '-' && !had_minmin)
1826 {
1827 want_argument = FALSE;
1828 c = argv[0][argv_idx++];
1829#ifdef VMS
1830 /*
1831 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1832 * and "-/X" as "-X".
1833 */
1834 if (c == '/')
1835 {
1836 c = argv[0][argv_idx++];
1837 c = TOUPPER_ASC(c);
1838 }
1839 else
1840 c = TOLOWER_ASC(c);
1841#endif
1842 switch (c)
1843 {
1844 case NUL: /* "vim -" read from stdin */
1845 /* "ex -" silent mode */
1846 if (exmode_active)
1847 silent_mode = TRUE;
1848 else
1849 {
1850 if (parmp->edit_type != EDIT_NONE)
1851 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1852 parmp->edit_type = EDIT_STDIN;
1853 read_cmd_fd = 2; /* read from stderr instead of stdin */
1854 }
1855 argv_idx = -1; /* skip to next argument */
1856 break;
1857
1858 case '-': /* "--" don't take any more option arguments */
1859 /* "--help" give help message */
1860 /* "--version" give version message */
1861 /* "--literal" take files literally */
1862 /* "--nofork" don't fork */
1863 /* "--noplugin[s]" skip plugins */
1864 /* "--cmd <cmd>" execute cmd before vimrc */
1865 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1866 usage();
1867 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1868 {
1869 Columns = 80; /* need to init Columns */
1870 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1871 list_version();
1872 msg_putchar('\n');
1873 msg_didout = FALSE;
1874 mch_exit(0);
1875 }
1876 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1877 {
1878#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1879 parmp->literal = TRUE;
1880#endif
1881 }
1882 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1883 {
1884#ifdef FEAT_GUI
1885 gui.dofork = FALSE; /* don't fork() when starting GUI */
1886#endif
1887 }
1888 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1889 p_lpl = FALSE;
1890 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1891 {
1892 want_argument = TRUE;
1893 argv_idx += 3;
1894 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001895 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1896 {
1897 want_argument = TRUE;
1898 argv_idx += 11;
1899 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001900#ifdef FEAT_CLIENTSERVER
1901 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1902 ; /* already processed -- no arg */
1903 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1904 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1905 {
1906 /* already processed -- snatch the following arg */
1907 if (argc > 1)
1908 {
1909 --argc;
1910 ++argv;
1911 }
1912 }
1913#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001914#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1915# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001916 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001917# else
1918 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1919# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001920 {
1921 /* already processed -- snatch the following arg */
1922 if (argc > 1)
1923 {
1924 --argc;
1925 ++argv;
1926 }
1927 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001928#endif
1929#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001930 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1931 {
1932 /* already processed, skip */
1933 }
1934#endif
1935 else
1936 {
1937 if (argv[0][argv_idx])
1938 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1939 had_minmin = TRUE;
1940 }
1941 if (!want_argument)
1942 argv_idx = -1; /* skip to next argument */
1943 break;
1944
1945 case 'A': /* "-A" start in Arabic mode */
1946#ifdef FEAT_ARABIC
1947 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1948#else
1949 mch_errmsg(_(e_noarabic));
1950 mch_exit(2);
1951#endif
1952 break;
1953
1954 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001955 /* Needs to be effective before expanding file names, because
1956 * for Win32 this makes us edit a shortcut file itself,
1957 * instead of the file it links to. */
1958 set_options_bin(curbuf->b_p_bin, 1, 0);
1959 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001960 break;
1961
1962 case 'C': /* "-C" Compatible */
1963 change_compatible(TRUE);
1964 break;
1965
1966 case 'e': /* "-e" Ex mode */
1967 exmode_active = EXMODE_NORMAL;
1968 break;
1969
1970 case 'E': /* "-E" Improved Ex mode */
1971 exmode_active = EXMODE_VIM;
1972 break;
1973
1974 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1975 window directly, not with newcli */
1976#ifdef FEAT_GUI
1977 gui.dofork = FALSE; /* don't fork() when starting GUI */
1978#endif
1979 break;
1980
1981 case 'g': /* "-g" start GUI */
1982 main_start_gui();
1983 break;
1984
1985 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1986#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001987 p_fkmap = TRUE;
1988 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001989#else
1990 mch_errmsg(_(e_nofarsi));
1991 mch_exit(2);
1992#endif
1993 break;
1994
1995 case 'h': /* "-h" give help message */
1996#ifdef FEAT_GUI_GNOME
1997 /* Tell usage() to exit for "gvim". */
1998 gui.starting = FALSE;
1999#endif
2000 usage();
2001 break;
2002
2003 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2004#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002005 p_hkmap = TRUE;
2006 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002007#else
2008 mch_errmsg(_(e_nohebrew));
2009 mch_exit(2);
2010#endif
2011 break;
2012
2013 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2014#ifdef FEAT_LISP
2015 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2016 p_sm = TRUE;
2017#endif
2018 break;
2019
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002020 case 'M': /* "-M" no changes or writing of files */
2021 reset_modifiable();
2022 /* FALLTHROUGH */
2023
2024 case 'm': /* "-m" no writing of files */
2025 p_write = FALSE;
2026 break;
2027
2028 case 'y': /* "-y" easy mode */
2029#ifdef FEAT_GUI
2030 gui.starting = TRUE; /* start GUI a bit later */
2031#endif
2032 parmp->evim_mode = TRUE;
2033 break;
2034
2035 case 'N': /* "-N" Nocompatible */
2036 change_compatible(FALSE);
2037 break;
2038
2039 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002040#ifdef FEAT_NETBEANS_INTG
2041 /* checking for "-nb", netbeans parameters */
2042 if (argv[0][argv_idx] == 'b')
2043 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002044 netbeansArg = argv[0];
2045 argv_idx = -1; /* skip to next argument */
2046 }
2047 else
2048#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002049 parmp->no_swap_file = TRUE;
2050 break;
2051
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002052 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002053#ifdef TARGET_API_MAC_OSX
2054 /* For some reason on MacOS X, an argument like:
2055 -psn_0_10223617 is passed in when invoke from Finder
2056 or with the 'open' command */
2057 if (argv[0][argv_idx] == 's')
2058 {
2059 argv_idx = -1; /* bypass full -psn */
2060 main_start_gui();
2061 break;
2062 }
2063#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002064#ifdef FEAT_WINDOWS
2065 /* default is 0: open window for each file */
2066 parmp->window_count = get_number_arg((char_u *)argv[0],
2067 &argv_idx, 0);
2068 parmp->window_layout = WIN_TABS;
2069#endif
2070 break;
2071
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002072 case 'o': /* "-o[N]" open N horizontal split windows */
2073#ifdef FEAT_WINDOWS
2074 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002075 parmp->window_count = get_number_arg((char_u *)argv[0],
2076 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002077 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002078#endif
2079 break;
2080
2081 case 'O': /* "-O[N]" open N vertical split windows */
2082#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2083 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002084 parmp->window_count = get_number_arg((char_u *)argv[0],
2085 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002086 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002087#endif
2088 break;
2089
2090#ifdef FEAT_QUICKFIX
2091 case 'q': /* "-q" QuickFix mode */
2092 if (parmp->edit_type != EDIT_NONE)
2093 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2094 parmp->edit_type = EDIT_QF;
2095 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2096 {
2097 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2098 argv_idx = -1;
2099 }
2100 else if (argc > 1) /* "-q {errorfile}" */
2101 want_argument = TRUE;
2102 break;
2103#endif
2104
2105 case 'R': /* "-R" readonly mode */
2106 readonlymode = TRUE;
2107 curbuf->b_p_ro = TRUE;
2108 p_uc = 10000; /* don't update very often */
2109 break;
2110
2111 case 'r': /* "-r" recovery mode */
2112 case 'L': /* "-L" recovery mode */
2113 recoverymode = 1;
2114 break;
2115
2116 case 's':
2117 if (exmode_active) /* "-s" silent (batch) mode */
2118 silent_mode = TRUE;
2119 else /* "-s {scriptin}" read from script file */
2120 want_argument = TRUE;
2121 break;
2122
2123 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2124 if (parmp->edit_type != EDIT_NONE)
2125 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2126 parmp->edit_type = EDIT_TAG;
2127 if (argv[0][argv_idx]) /* "-t{tag}" */
2128 {
2129 parmp->tagname = (char_u *)argv[0] + argv_idx;
2130 argv_idx = -1;
2131 }
2132 else /* "-t {tag}" */
2133 want_argument = TRUE;
2134 break;
2135
2136#ifdef FEAT_EVAL
2137 case 'D': /* "-D" Debugging */
2138 parmp->use_debug_break_level = 9999;
2139 break;
2140#endif
2141#ifdef FEAT_DIFF
2142 case 'd': /* "-d" 'diff' */
2143# ifdef AMIGA
2144 /* check for "-dev {device}" */
2145 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2146 want_argument = TRUE;
2147 else
2148# endif
2149 parmp->diff_mode = TRUE;
2150 break;
2151#endif
2152 case 'V': /* "-V{N}" Verbose level */
2153 /* default is 10: a little bit verbose */
2154 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2155 if (argv[0][argv_idx] != NUL)
2156 {
2157 set_option_value((char_u *)"verbosefile", 0L,
2158 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002159 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002160 }
2161 break;
2162
2163 case 'v': /* "-v" Vi-mode (as if called "vi") */
2164 exmode_active = 0;
2165#ifdef FEAT_GUI
2166 gui.starting = FALSE; /* don't start GUI */
2167#endif
2168 break;
2169
2170 case 'w': /* "-w{number}" set window height */
2171 /* "-w {scriptout}" write to script */
2172 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2173 {
2174 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2175 set_option_value((char_u *)"window", n, NULL, 0);
2176 break;
2177 }
2178 want_argument = TRUE;
2179 break;
2180
2181#ifdef FEAT_CRYPT
2182 case 'x': /* "-x" encrypted reading/writing of files */
2183 parmp->ask_for_key = TRUE;
2184 break;
2185#endif
2186
2187 case 'X': /* "-X" don't connect to X server */
2188#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2189 x_no_connect = TRUE;
2190#endif
2191 break;
2192
2193 case 'Z': /* "-Z" restricted mode */
2194 restricted = TRUE;
2195 break;
2196
2197 case 'c': /* "-c{command}" or "-c {command}" execute
2198 command */
2199 if (argv[0][argv_idx] != NUL)
2200 {
2201 if (parmp->n_commands >= MAX_ARG_CMDS)
2202 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002203 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2204 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002205 argv_idx = -1;
2206 break;
2207 }
2208 /*FALLTHROUGH*/
2209 case 'S': /* "-S {file}" execute Vim script */
2210 case 'i': /* "-i {viminfo}" use for viminfo */
2211#ifndef FEAT_DIFF
2212 case 'd': /* "-d {device}" device (for Amiga) */
2213#endif
2214 case 'T': /* "-T {terminal}" terminal name */
2215 case 'u': /* "-u {vimrc}" vim inits file */
2216 case 'U': /* "-U {gvimrc}" gvim inits file */
2217 case 'W': /* "-W {scriptout}" overwrite */
2218#ifdef FEAT_GUI_W32
2219 case 'P': /* "-P {parent title}" MDI parent */
2220#endif
2221 want_argument = TRUE;
2222 break;
2223
2224 default:
2225 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2226 }
2227
2228 /*
2229 * Handle option arguments with argument.
2230 */
2231 if (want_argument)
2232 {
2233 /*
2234 * Check for garbage immediately after the option letter.
2235 */
2236 if (argv[0][argv_idx] != NUL)
2237 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2238
2239 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002240 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002241 mainerr_arg_missing((char_u *)argv[0]);
2242 ++argv;
2243 argv_idx = -1;
2244
2245 switch (c)
2246 {
2247 case 'c': /* "-c {command}" execute command */
2248 case 'S': /* "-S {file}" execute Vim script */
2249 if (parmp->n_commands >= MAX_ARG_CMDS)
2250 mainerr(ME_EXTRA_CMD, NULL);
2251 if (c == 'S')
2252 {
2253 char *a;
2254
2255 if (argc < 1)
2256 /* "-S" without argument: use default session file
2257 * name. */
2258 a = SESSION_FILE;
2259 else if (argv[0][0] == '-')
2260 {
2261 /* "-S" followed by another option: use default
2262 * session file name. */
2263 a = SESSION_FILE;
2264 ++argc;
2265 --argv;
2266 }
2267 else
2268 a = argv[0];
2269 p = alloc((unsigned)(STRLEN(a) + 4));
2270 if (p == NULL)
2271 mch_exit(2);
2272 sprintf((char *)p, "so %s", a);
2273 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2274 parmp->commands[parmp->n_commands++] = p;
2275 }
2276 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002277 parmp->commands[parmp->n_commands++] =
2278 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002279 break;
2280
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002281 case '-':
2282 if (argv[-1][2] == 'c')
2283 {
2284 /* "--cmd {command}" execute command */
2285 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2286 mainerr(ME_EXTRA_CMD, NULL);
2287 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002288 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002289 }
2290 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002291 break;
2292
2293 /* case 'd': -d {device} is handled in mch_check_win() for the
2294 * Amiga */
2295
2296#ifdef FEAT_QUICKFIX
2297 case 'q': /* "-q {errorfile}" QuickFix mode */
2298 parmp->use_ef = (char_u *)argv[0];
2299 break;
2300#endif
2301
2302 case 'i': /* "-i {viminfo}" use for viminfo */
2303 use_viminfo = (char_u *)argv[0];
2304 break;
2305
2306 case 's': /* "-s {scriptin}" read from script file */
2307 if (scriptin[0] != NULL)
2308 {
2309scripterror:
2310 mch_errmsg(_("Attempt to open script file again: \""));
2311 mch_errmsg(argv[-1]);
2312 mch_errmsg(" ");
2313 mch_errmsg(argv[0]);
2314 mch_errmsg("\"\n");
2315 mch_exit(2);
2316 }
2317 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2318 {
2319 mch_errmsg(_("Cannot open for reading: \""));
2320 mch_errmsg(argv[0]);
2321 mch_errmsg("\"\n");
2322 mch_exit(2);
2323 }
2324 if (save_typebuf() == FAIL)
2325 mch_exit(2); /* out of memory */
2326 break;
2327
2328 case 't': /* "-t {tag}" */
2329 parmp->tagname = (char_u *)argv[0];
2330 break;
2331
2332 case 'T': /* "-T {terminal}" terminal name */
2333 /*
2334 * The -T term argument is always available and when
2335 * HAVE_TERMLIB is supported it overrides the environment
2336 * variable TERM.
2337 */
2338#ifdef FEAT_GUI
2339 if (term_is_gui((char_u *)argv[0]))
2340 gui.starting = TRUE; /* start GUI a bit later */
2341 else
2342#endif
2343 parmp->term = (char_u *)argv[0];
2344 break;
2345
2346 case 'u': /* "-u {vimrc}" vim inits file */
2347 parmp->use_vimrc = (char_u *)argv[0];
2348 break;
2349
2350 case 'U': /* "-U {gvimrc}" gvim inits file */
2351#ifdef FEAT_GUI
2352 use_gvimrc = (char_u *)argv[0];
2353#endif
2354 break;
2355
2356 case 'w': /* "-w {nr}" 'window' value */
2357 /* "-w {scriptout}" append to script file */
2358 if (vim_isdigit(*((char_u *)argv[0])))
2359 {
2360 argv_idx = 0;
2361 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2362 set_option_value((char_u *)"window", n, NULL, 0);
2363 argv_idx = -1;
2364 break;
2365 }
2366 /*FALLTHROUGH*/
2367 case 'W': /* "-W {scriptout}" overwrite script file */
2368 if (scriptout != NULL)
2369 goto scripterror;
2370 if ((scriptout = mch_fopen(argv[0],
2371 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2372 {
2373 mch_errmsg(_("Cannot open for script output: \""));
2374 mch_errmsg(argv[0]);
2375 mch_errmsg("\"\n");
2376 mch_exit(2);
2377 }
2378 break;
2379
2380#ifdef FEAT_GUI_W32
2381 case 'P': /* "-P {parent title}" MDI parent */
2382 gui_mch_set_parent(argv[0]);
2383 break;
2384#endif
2385 }
2386 }
2387 }
2388
2389 /*
2390 * File name argument.
2391 */
2392 else
2393 {
2394 argv_idx = -1; /* skip to next argument */
2395
2396 /* Check for only one type of editing. */
2397 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2398 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2399 parmp->edit_type = EDIT_FILE;
2400
2401#ifdef MSWIN
2402 /* Remember if the argument was a full path before changing
2403 * slashes to backslashes. */
2404 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2405 parmp->full_path = TRUE;
2406#endif
2407
2408 /* Add the file to the global argument list. */
2409 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2410 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2411 mch_exit(2);
2412#ifdef FEAT_DIFF
2413 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2414 && !mch_isdir(alist_name(&GARGLIST[0])))
2415 {
2416 char_u *r;
2417
2418 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2419 if (r != NULL)
2420 {
2421 vim_free(p);
2422 p = r;
2423 }
2424 }
2425#endif
2426#if defined(__CYGWIN32__) && !defined(WIN32)
2427 /*
2428 * If vim is invoked by non-Cygwin tools, convert away any
2429 * DOS paths, so things like .swp files are created correctly.
2430 * Look for evidence of non-Cygwin paths before we bother.
2431 * This is only for when using the Unix files.
2432 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002433 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002434 {
2435 char posix_path[PATH_MAX];
2436
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002437# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2438 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2439# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002441# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002442 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002443 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002444 if (p == NULL)
2445 mch_exit(2);
2446 }
2447#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002448
2449#ifdef USE_FNAME_CASE
2450 /* Make the case of the file name match the actual file. */
2451 fname_case(p, 0);
2452#endif
2453
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002454 alist_add(&global_alist, p,
2455#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002456 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002457#else
2458 2 /* add buffer number now and use curbuf */
2459#endif
2460 );
2461
2462#if defined(FEAT_MBYTE) && defined(WIN32)
2463 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002464 /* Remember this argument has been added to the argument list.
2465 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002466 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002467# ifdef FEAT_DIFF
2468 parmp->diff_mode
2469# else
2470 FALSE
2471# endif
2472 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002473 }
2474#endif
2475 }
2476
2477 /*
2478 * If there are no more letters after the current "-", go to next
2479 * argument. argv_idx is set to -1 when the current argument is to be
2480 * skipped.
2481 */
2482 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2483 {
2484 --argc;
2485 ++argv;
2486 argv_idx = 1;
2487 }
2488 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002489
2490#ifdef FEAT_EVAL
2491 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2492 * one. */
2493 if (parmp->n_commands > 0)
2494 {
2495 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2496 if (p != NULL)
2497 {
2498 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2499 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2500 vim_free(p);
2501 }
2502 }
2503#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002504}
2505
2506/*
2507 * Print a warning if stdout is not a terminal.
2508 * When starting in Ex mode and commands come from a file, set Silent mode.
2509 */
2510 static void
2511check_tty(parmp)
2512 mparm_T *parmp;
2513{
2514 int input_isatty; /* is active input a terminal? */
2515
2516 input_isatty = mch_input_isatty();
2517 if (exmode_active)
2518 {
2519 if (!input_isatty)
2520 silent_mode = TRUE;
2521 }
2522 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2523#ifdef FEAT_GUI
2524 /* don't want the delay when started from the desktop */
2525 && !gui.starting
2526#endif
2527 )
2528 {
2529#ifdef NBDEBUG
2530 /*
2531 * This shouldn't be necessary. But if I run netbeans with the log
2532 * output coming to the console and XOpenDisplay fails, I get vim
2533 * trying to start with input/output to my console tty. This fills my
2534 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002535 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002536 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002537 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002538 {
2539 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2540 exit(1);
2541 }
2542#endif
2543 if (!parmp->stdout_isatty)
2544 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2545 if (!input_isatty)
2546 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2547 out_flush();
2548 if (scriptin[0] == NULL)
2549 ui_delay(2000L, TRUE);
2550 TIME_MSG("Warning delay");
2551 }
2552}
2553
2554/*
2555 * Read text from stdin.
2556 */
2557 static void
2558read_stdin()
2559{
2560 int i;
2561
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002562#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002563 /* When getting the ATTENTION prompt here, use a dialog */
2564 swap_exists_action = SEA_DIALOG;
2565#endif
2566 no_wait_return = TRUE;
2567 i = msg_didany;
2568 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002569 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002570 no_wait_return = FALSE;
2571 msg_didany = i;
2572 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002573#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 check_swap_exists_action();
2575#endif
2576#if !(defined(AMIGA) || defined(MACOS))
2577 /*
2578 * Close stdin and dup it from stderr. Required for GPM to work
2579 * properly, and for running external commands.
2580 * Is there any other system that cannot do this?
2581 */
2582 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002583 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002584#endif
2585}
2586
2587/*
2588 * Create the requested number of windows and edit buffers in them.
2589 * Also does recovery if "recoverymode" set.
2590 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002591 static void
2592create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002593 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594{
2595#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002596 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002597 int done = 0;
2598
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599 /*
2600 * Create the number of windows that was requested.
2601 */
2602 if (parmp->window_count == -1) /* was not set */
2603 parmp->window_count = 1;
2604 if (parmp->window_count == 0)
2605 parmp->window_count = GARGCOUNT;
2606 if (parmp->window_count > 1)
2607 {
2608 /* Don't change the windows if there was a command in .vimrc that
2609 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002610 if (parmp->window_layout == 0)
2611 parmp->window_layout = WIN_HOR;
2612 if (parmp->window_layout == WIN_TABS)
2613 {
2614 parmp->window_count = make_tabpages(parmp->window_count);
2615 TIME_MSG("making tab pages");
2616 }
2617 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002618 {
2619 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002620 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621 TIME_MSG("making windows");
2622 }
2623 else
2624 parmp->window_count = win_count();
2625 }
2626 else
2627 parmp->window_count = 1;
2628#endif
2629
2630 if (recoverymode) /* do recover */
2631 {
2632 msg_scroll = TRUE; /* scroll message up */
2633 ml_recover();
2634 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2635 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002636 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002637 }
2638 else
2639 {
2640 /*
2641 * Open a buffer for windows that don't have one yet.
2642 * Commands in the .vimrc might have loaded a file or split the window.
2643 * Watch out for autocommands that delete a window.
2644 */
2645#ifdef FEAT_AUTOCMD
2646 /*
2647 * Don't execute Win/Buf Enter/Leave autocommands here
2648 */
2649 ++autocmd_no_enter;
2650 ++autocmd_no_leave;
2651#endif
2652#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002653 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002654 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002655 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002656 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002657 {
2658 if (parmp->window_layout == WIN_TABS)
2659 goto_tabpage(1);
2660 else
2661 curwin = firstwin;
2662 }
2663 else if (parmp->window_layout == WIN_TABS)
2664 {
2665 if (curtab->tp_next == NULL)
2666 break;
2667 goto_tabpage(0);
2668 }
2669 else
2670 {
2671 if (curwin->w_next == NULL)
2672 break;
2673 curwin = curwin->w_next;
2674 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002675 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002676#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002677 curbuf = curwin->w_buffer;
2678 if (curbuf->b_ml.ml_mfp == NULL)
2679 {
2680#ifdef FEAT_FOLDING
2681 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2682 if (p_fdls >= 0)
2683 curwin->w_p_fdl = p_fdls;
2684#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002685#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002686 /* When getting the ATTENTION prompt here, use a dialog */
2687 swap_exists_action = SEA_DIALOG;
2688#endif
2689 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002690
2691 /* create memfile, read file */
2692 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002693
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002694#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002695 if (swap_exists_action == SEA_QUIT)
2696 {
2697 if (got_int || only_one_window())
2698 {
2699 /* abort selected or quit and only one window */
2700 did_emsg = FALSE; /* avoid hit-enter prompt */
2701 getout(1);
2702 }
2703 /* We can't close the window, it would disturb what
2704 * happens next. Clear the file name and set the arg
2705 * index to -1 to delete it later. */
2706 setfname(curbuf, NULL, NULL, FALSE);
2707 curwin->w_arg_idx = -1;
2708 swap_exists_action = SEA_NONE;
2709 }
2710 else
2711 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002712#endif
2713#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002714 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002715#endif
2716 }
2717#ifdef FEAT_WINDOWS
2718 ui_breakcheck();
2719 if (got_int)
2720 {
2721 (void)vgetc(); /* only break the file loading, not the rest */
2722 break;
2723 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002724 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002725#endif
2726#ifdef FEAT_WINDOWS
2727 if (parmp->window_layout == WIN_TABS)
2728 goto_tabpage(1);
2729 else
2730 curwin = firstwin;
2731 curbuf = curwin->w_buffer;
2732#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002733#ifdef FEAT_AUTOCMD
2734 --autocmd_no_enter;
2735 --autocmd_no_leave;
2736#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737 }
2738}
2739
2740#ifdef FEAT_WINDOWS
2741 /*
2742 * If opened more than one window, start editing files in the other
2743 * windows. make_windows() has already opened the windows.
2744 */
2745 static void
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002746edit_buffers(parmp, cwd)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002747 mparm_T *parmp;
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002748 char_u *cwd; /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002749{
2750 int arg_idx; /* index in argument list */
2751 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002752 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002753 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002754
2755# ifdef FEAT_AUTOCMD
2756 /*
2757 * Don't execute Win/Buf Enter/Leave autocommands here
2758 */
2759 ++autocmd_no_enter;
2760 ++autocmd_no_leave;
2761# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002762
2763 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2764 if (curwin->w_arg_idx == -1)
2765 {
2766 win_close(curwin, TRUE);
2767 advance = FALSE;
2768 }
2769
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 arg_idx = 1;
2771 for (i = 1; i < parmp->window_count; ++i)
2772 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002773 if (cwd != NULL)
2774 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002775 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2776 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002777 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002778 ++arg_idx;
2779 win_close(curwin, TRUE);
2780 advance = FALSE;
2781 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002782 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002783
Bram Moolenaar84212822006-11-07 21:59:47 +00002784 if (advance)
2785 {
2786 if (parmp->window_layout == WIN_TABS)
2787 {
2788 if (curtab->tp_next == NULL) /* just checking */
2789 break;
2790 goto_tabpage(0);
2791 }
2792 else
2793 {
2794 if (curwin->w_next == NULL) /* just checking */
2795 break;
2796 win_enter(curwin->w_next, FALSE);
2797 }
2798 }
2799 advance = TRUE;
2800
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002801 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002802 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002803 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2804 {
2805 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002806 /* Edit file from arg list, if there is one. When "Quit" selected
2807 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002808# ifdef HAS_SWAP_EXISTS_ACTION
2809 swap_exists_did_quit = FALSE;
2810# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002811 (void)do_ecmd(0, arg_idx < GARGCOUNT
2812 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002813 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002814# ifdef HAS_SWAP_EXISTS_ACTION
2815 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002816 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002817 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002818 if (got_int || only_one_window())
2819 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002820 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002821 did_emsg = FALSE; /* avoid hit-enter prompt */
2822 getout(1);
2823 }
2824 win_close(curwin, TRUE);
2825 advance = FALSE;
2826 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002827# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002828 if (arg_idx == GARGCOUNT - 1)
2829 arg_had_last = TRUE;
2830 ++arg_idx;
2831 }
2832 ui_breakcheck();
2833 if (got_int)
2834 {
2835 (void)vgetc(); /* only break the file loading, not the rest */
2836 break;
2837 }
2838 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002839
2840 if (parmp->window_layout == WIN_TABS)
2841 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002842# ifdef FEAT_AUTOCMD
2843 --autocmd_no_enter;
2844# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002845
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002846 /* make the first window the current window */
2847 win = firstwin;
2848#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2849 /* Avoid making a preview window the current window. */
2850 while (win->w_p_pvw)
2851 {
2852 win = win->w_next;
2853 if (win == NULL)
2854 {
2855 win = firstwin;
2856 break;
2857 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002858 }
2859#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002860 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002861
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002862# ifdef FEAT_AUTOCMD
2863 --autocmd_no_leave;
2864# endif
2865 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002866 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002867 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2868}
2869#endif /* FEAT_WINDOWS */
2870
2871/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002872 * Execute the commands from --cmd arguments "cmds[cnt]".
2873 */
2874 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002875exe_pre_commands(parmp)
2876 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002877{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002878 char_u **cmds = parmp->pre_commands;
2879 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002880 int i;
2881
2882 if (cnt > 0)
2883 {
2884 curwin->w_cursor.lnum = 0; /* just in case.. */
2885 sourcing_name = (char_u *)_("pre-vimrc command line");
2886# ifdef FEAT_EVAL
2887 current_SID = SID_CMDARG;
2888# endif
2889 for (i = 0; i < cnt; ++i)
2890 do_cmdline_cmd(cmds[i]);
2891 sourcing_name = NULL;
2892# ifdef FEAT_EVAL
2893 current_SID = 0;
2894# endif
2895 TIME_MSG("--cmd commands");
2896 }
2897}
2898
2899/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002900 * Execute "+", "-c" and "-S" arguments.
2901 */
2902 static void
2903exe_commands(parmp)
2904 mparm_T *parmp;
2905{
2906 int i;
2907
2908 /*
2909 * We start commands on line 0, make "vim +/pat file" match a
2910 * pattern on line 1. But don't move the cursor when an autocommand
2911 * with g`" was used.
2912 */
2913 msg_scroll = TRUE;
2914 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2915 curwin->w_cursor.lnum = 0;
2916 sourcing_name = (char_u *)"command line";
2917#ifdef FEAT_EVAL
2918 current_SID = SID_CARG;
2919#endif
2920 for (i = 0; i < parmp->n_commands; ++i)
2921 {
2922 do_cmdline_cmd(parmp->commands[i]);
2923 if (parmp->cmds_tofree[i])
2924 vim_free(parmp->commands[i]);
2925 }
2926 sourcing_name = NULL;
2927#ifdef FEAT_EVAL
2928 current_SID = 0;
2929#endif
2930 if (curwin->w_cursor.lnum == 0)
2931 curwin->w_cursor.lnum = 1;
2932
2933 if (!exmode_active)
2934 msg_scroll = FALSE;
2935
2936#ifdef FEAT_QUICKFIX
2937 /* When started with "-q errorfile" jump to first error again. */
2938 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002939 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002940#endif
2941 TIME_MSG("executing command arguments");
2942}
2943
2944/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002945 * Source startup scripts.
2946 */
2947 static void
2948source_startup_scripts(parmp)
2949 mparm_T *parmp;
2950{
2951 int i;
2952
2953 /*
2954 * For "evim" source evim.vim first of all, so that the user can overrule
2955 * any things he doesn't like.
2956 */
2957 if (parmp->evim_mode)
2958 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002959 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002960 TIME_MSG("source evim file");
2961 }
2962
2963 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002964 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002965 * nothing else.
2966 */
2967 if (parmp->use_vimrc != NULL)
2968 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002969 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2970 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002971 {
2972#ifdef FEAT_GUI
2973 if (use_gvimrc == NULL) /* don't load gvimrc either */
2974 use_gvimrc = parmp->use_vimrc;
2975#endif
2976 if (parmp->use_vimrc[2] == 'N')
2977 p_lpl = FALSE; /* don't load plugins either */
2978 }
2979 else
2980 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002981 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002982 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2983 }
2984 }
2985 else if (!silent_mode)
2986 {
2987#ifdef AMIGA
2988 struct Process *proc = (struct Process *)FindTask(0L);
2989 APTR save_winptr = proc->pr_WindowPtr;
2990
2991 /* Avoid a requester here for a volume that doesn't exist. */
2992 proc->pr_WindowPtr = (APTR)-1L;
2993#endif
2994
2995 /*
2996 * Get system wide defaults, if the file name is defined.
2997 */
2998#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002999 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003000#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003001#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003002 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003003#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004
3005 /*
3006 * Try to read initialization commands from the following places:
3007 * - environment variable VIMINIT
3008 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3009 * - second user vimrc file ($VIM/.vimrc for Dos)
3010 * - environment variable EXINIT
3011 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3012 * - second user exrc file ($VIM/.exrc for Dos)
3013 * The first that exists is used, the rest is ignored.
3014 */
3015 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3016 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003017 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003018#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003019 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3020 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003021#endif
3022#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003023 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3024 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003025#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003026#ifdef USR_VIMRC_FILE4
3027 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3028 DOSO_VIMRC) == FAIL
3029#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003030 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003031 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003032 {
3033#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003034 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003035#endif
3036 }
3037 }
3038
3039 /*
3040 * Read initialization commands from ".vimrc" or ".exrc" in current
3041 * directory. This is only done if the 'exrc' option is set.
3042 * Because of security reasons we disallow shell and write commands
3043 * now, except for unix if the file is owned by the user or 'secure'
3044 * option has been reset in environment of global ".exrc" or ".vimrc".
3045 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3046 * SYS_VIMRC_FILE.
3047 */
3048 if (p_exrc)
3049 {
3050#if defined(UNIX) || defined(VMS)
3051 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3052 if (!file_owned(VIMRC_FILE))
3053#endif
3054 secure = p_secure;
3055
3056 i = FAIL;
3057 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3058 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3059#ifdef USR_VIMRC_FILE2
3060 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3061 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3062#endif
3063#ifdef USR_VIMRC_FILE3
3064 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3065 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3066#endif
3067#ifdef SYS_VIMRC_FILE
3068 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3069 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3070#endif
3071 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003072 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003073
3074 if (i == FAIL)
3075 {
3076#if defined(UNIX) || defined(VMS)
3077 /* if ".exrc" is not owned by user set 'secure' mode */
3078 if (!file_owned(EXRC_FILE))
3079 secure = p_secure;
3080 else
3081 secure = 0;
3082#endif
3083 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3084 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3085#ifdef USR_EXRC_FILE2
3086 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3087 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3088#endif
3089 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003090 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003091 }
3092 }
3093 if (secure == 2)
3094 need_wait_return = TRUE;
3095 secure = 0;
3096#ifdef AMIGA
3097 proc->pr_WindowPtr = save_winptr;
3098#endif
3099 }
3100 TIME_MSG("sourcing vimrc file(s)");
3101}
3102
3103/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003104 * Setup to start using the GUI. Exit with an error when not available.
3105 */
3106 static void
3107main_start_gui()
3108{
3109#ifdef FEAT_GUI
3110 gui.starting = TRUE; /* start GUI a bit later */
3111#else
3112 mch_errmsg(_(e_nogvim));
3113 mch_errmsg("\n");
3114 mch_exit(2);
3115#endif
3116}
3117
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003118#endif /* NO_VIM_MAIN */
3119
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003120/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003121 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003122 * Returns FAIL if the environment variable was not executed, OK otherwise.
3123 */
3124 int
3125process_env(env, is_viminit)
3126 char_u *env;
3127 int is_viminit; /* when TRUE, called for VIMINIT */
3128{
3129 char_u *initstr;
3130 char_u *save_sourcing_name;
3131 linenr_T save_sourcing_lnum;
3132#ifdef FEAT_EVAL
3133 scid_T save_sid;
3134#endif
3135
3136 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3137 {
3138 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003139 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003140 save_sourcing_name = sourcing_name;
3141 save_sourcing_lnum = sourcing_lnum;
3142 sourcing_name = env;
3143 sourcing_lnum = 0;
3144#ifdef FEAT_EVAL
3145 save_sid = current_SID;
3146 current_SID = SID_ENV;
3147#endif
3148 do_cmdline_cmd(initstr);
3149 sourcing_name = save_sourcing_name;
3150 sourcing_lnum = save_sourcing_lnum;
3151#ifdef FEAT_EVAL
3152 current_SID = save_sid;;
3153#endif
3154 return OK;
3155 }
3156 return FAIL;
3157}
3158
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003159#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003160/*
3161 * Return TRUE if we are certain the user owns the file "fname".
3162 * Used for ".vimrc" and ".exrc".
3163 * Use both stat() and lstat() for extra security.
3164 */
3165 static int
3166file_owned(fname)
3167 char *fname;
3168{
3169 struct stat s;
3170# ifdef UNIX
3171 uid_t uid = getuid();
3172# else /* VMS */
3173 uid_t uid = ((getgid() << 16) | getuid());
3174# endif
3175
3176 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3177# ifdef HAVE_LSTAT
3178 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3179# endif
3180 );
3181}
3182#endif
3183
3184/*
3185 * Give an error message main_errors["n"] and exit.
3186 */
3187 static void
3188mainerr(n, str)
3189 int n; /* one of the ME_ defines */
3190 char_u *str; /* extra argument or NULL */
3191{
3192#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3193 reset_signals(); /* kill us with CTRL-C here, if you like */
3194#endif
3195
3196 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003197 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003198 mch_errmsg(_(main_errors[n]));
3199 if (str != NULL)
3200 {
3201 mch_errmsg(": \"");
3202 mch_errmsg((char *)str);
3203 mch_errmsg("\"");
3204 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003205 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003206
3207 mch_exit(1);
3208}
3209
3210 void
3211mainerr_arg_missing(str)
3212 char_u *str;
3213{
3214 mainerr(ME_ARG_MISSING, str);
3215}
3216
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003217#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218/*
3219 * print a message with three spaces prepended and '\n' appended.
3220 */
3221 static void
3222main_msg(s)
3223 char *s;
3224{
3225 mch_msg(" ");
3226 mch_msg(s);
3227 mch_msg("\n");
3228}
3229
3230/*
3231 * Print messages for "vim -h" or "vim --help" and exit.
3232 */
3233 static void
3234usage()
3235{
3236 int i;
3237 static char *(use[]) =
3238 {
3239 N_("[file ..] edit specified file(s)"),
3240 N_("- read text from stdin"),
3241 N_("-t tag edit file where tag is defined"),
3242#ifdef FEAT_QUICKFIX
3243 N_("-q [errorfile] edit file with first error")
3244#endif
3245 };
3246
3247#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3248 reset_signals(); /* kill us with CTRL-C here, if you like */
3249#endif
3250
3251 mch_msg(longVersion);
3252 mch_msg(_("\n\nusage:"));
3253 for (i = 0; ; ++i)
3254 {
3255 mch_msg(_(" vim [arguments] "));
3256 mch_msg(_(use[i]));
3257 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3258 break;
3259 mch_msg(_("\n or:"));
3260 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003261#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003262 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003263#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003264
3265 mch_msg(_("\n\nArguments:\n"));
3266 main_msg(_("--\t\t\tOnly file names after this"));
3267#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3268 main_msg(_("--literal\t\tDon't expand wildcards"));
3269#endif
3270#ifdef FEAT_OLE
3271 main_msg(_("-register\t\tRegister this gvim for OLE"));
3272 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3273#endif
3274#ifdef FEAT_GUI
3275 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3276 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3277#endif
3278 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3279 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003280 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003281 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3282#ifdef FEAT_DIFF
3283 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3284#endif
3285 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3286 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3287 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3288 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3289 main_msg(_("-M\t\t\tModifications in text not allowed"));
3290 main_msg(_("-b\t\t\tBinary mode"));
3291#ifdef FEAT_LISP
3292 main_msg(_("-l\t\t\tLisp mode"));
3293#endif
3294 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3295 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003296 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3297#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003299#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003300 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3301 main_msg(_("-r\t\t\tList swap files and exit"));
3302 main_msg(_("-r (with file name)\tRecover crashed session"));
3303 main_msg(_("-L\t\t\tSame as -r"));
3304#ifdef AMIGA
3305 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3306 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3307#endif
3308#ifdef FEAT_ARABIC
3309 main_msg(_("-A\t\t\tstart in Arabic mode"));
3310#endif
3311#ifdef FEAT_RIGHTLEFT
3312 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3313#endif
3314#ifdef FEAT_FKMAP
3315 main_msg(_("-F\t\t\tStart in Farsi mode"));
3316#endif
3317 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3318 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3319#ifdef FEAT_GUI
3320 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3321#endif
3322 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003323#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003324 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003325 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3326 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003327#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 main_msg(_("+\t\t\tStart at end of file"));
3329 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3332 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3333 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3334 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3335 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3336#ifdef FEAT_CRYPT
3337 main_msg(_("-x\t\t\tEdit encrypted files"));
3338#endif
3339#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3340# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3341 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3342# endif
3343 main_msg(_("-X\t\t\tDo not connect to X server"));
3344#endif
3345#ifdef FEAT_CLIENTSERVER
3346 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3347 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3348 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3349 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003350# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003351 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003352# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3354 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3355 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3356 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3357#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003358#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003359 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003360#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003361#ifdef FEAT_VIMINFO
3362 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3363#endif
3364 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3365 main_msg(_("--version\t\tPrint version information and exit"));
3366
3367#ifdef FEAT_GUI_X11
3368# ifdef FEAT_GUI_MOTIF
3369 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3370# else
3371# ifdef FEAT_GUI_ATHENA
3372# ifdef FEAT_GUI_NEXTAW
3373 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3374# else
3375 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3376# endif
3377# endif
3378# endif
3379 main_msg(_("-display <display>\tRun vim on <display>"));
3380 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003381 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3382 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3383 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3384 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3385 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3386 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3387 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3388 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3389# ifdef FEAT_GUI_ATHENA
3390 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3391# endif
3392 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3393 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3394 main_msg(_("-xrm <resource>\tSet the specified resource"));
3395#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003396#ifdef FEAT_GUI_GTK
3397 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3398 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3399 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3400 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3401 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003404 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406#ifdef FEAT_GUI_W32
3407 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003408 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409#endif
3410
3411#ifdef FEAT_GUI_GNOME
3412 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3413 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003414 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003416 gui.dofork = FALSE;
3417 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418 else
3419#endif
3420 mch_exit(0);
3421}
3422
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003423#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003424/*
3425 * Check the result of the ATTENTION dialog:
3426 * When "Quit" selected, exit Vim.
3427 * When "Recover" selected, recover the file.
3428 */
3429 static void
3430check_swap_exists_action()
3431{
3432 if (swap_exists_action == SEA_QUIT)
3433 getout(1);
3434 handle_swap_exists(NULL);
3435}
3436#endif
3437
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003438#endif
3439
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003440#if defined(STARTUPTIME) || defined(PROTO)
3441static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3442
3443static struct timeval prev_timeval;
3444
Bram Moolenaar3f269672009-11-03 11:11:11 +00003445# ifdef WIN3264
3446/*
3447 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3448 */
3449 static int
3450gettimeofday(struct timeval *tv, char *dummy)
3451{
3452 long t = clock();
3453 tv->tv_sec = t / CLOCKS_PER_SEC;
3454 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3455 return 0;
3456}
3457# endif
3458
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003459/*
3460 * Save the previous time before doing something that could nest.
3461 * set "*tv_rel" to the time elapsed so far.
3462 */
3463 void
3464time_push(tv_rel, tv_start)
3465 void *tv_rel, *tv_start;
3466{
3467 *((struct timeval *)tv_rel) = prev_timeval;
3468 gettimeofday(&prev_timeval, NULL);
3469 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3470 - ((struct timeval *)tv_rel)->tv_usec;
3471 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3472 - ((struct timeval *)tv_rel)->tv_sec;
3473 if (((struct timeval *)tv_rel)->tv_usec < 0)
3474 {
3475 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3476 --((struct timeval *)tv_rel)->tv_sec;
3477 }
3478 *(struct timeval *)tv_start = prev_timeval;
3479}
3480
3481/*
3482 * Compute the previous time after doing something that could nest.
3483 * Subtract "*tp" from prev_timeval;
3484 * Note: The arguments are (void *) to avoid trouble with systems that don't
3485 * have struct timeval.
3486 */
3487 void
3488time_pop(tp)
3489 void *tp; /* actually (struct timeval *) */
3490{
3491 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3492 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3493 if (prev_timeval.tv_usec < 0)
3494 {
3495 prev_timeval.tv_usec += 1000000;
3496 --prev_timeval.tv_sec;
3497 }
3498}
3499
3500 static void
3501time_diff(then, now)
3502 struct timeval *then;
3503 struct timeval *now;
3504{
3505 long usec;
3506 long msec;
3507
3508 usec = now->tv_usec - then->tv_usec;
3509 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3510 usec = usec % 1000L;
3511 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3512}
3513
3514 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003515time_msg(mesg, tv_start)
3516 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003517 void *tv_start; /* only for do_source: start time; actually
3518 (struct timeval *) */
3519{
3520 static struct timeval start;
3521 struct timeval now;
3522
3523 if (time_fd != NULL)
3524 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003525 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003526 {
3527 gettimeofday(&start, NULL);
3528 prev_timeval = start;
3529 fprintf(time_fd, "\n\ntimes in msec\n");
3530 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3531 fprintf(time_fd, " clock elapsed: other lines\n\n");
3532 }
3533 gettimeofday(&now, NULL);
3534 time_diff(&start, &now);
3535 if (((struct timeval *)tv_start) != NULL)
3536 {
3537 fprintf(time_fd, " ");
3538 time_diff(((struct timeval *)tv_start), &now);
3539 }
3540 fprintf(time_fd, " ");
3541 time_diff(&prev_timeval, &now);
3542 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003543 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003544 }
3545}
3546
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003547#endif
3548
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003549#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003550
3551/*
3552 * Common code for the X command server and the Win32 command server.
3553 */
3554
Bram Moolenaareb94e552006-03-11 21:35:11 +00003555static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003556
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003557/*
3558 * Do the client-server stuff, unless "--servername ''" was used.
3559 */
3560 static void
3561exec_on_server(parmp)
3562 mparm_T *parmp;
3563{
3564 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3565 {
3566# ifdef WIN32
3567 /* Initialise the client/server messaging infrastructure. */
3568 serverInitMessaging();
3569# endif
3570
3571 /*
3572 * When a command server argument was found, execute it. This may
3573 * exit Vim when it was successful. Otherwise it's executed further
3574 * on. Remember the encoding used here in "serverStrEnc".
3575 */
3576 if (parmp->serverArg)
3577 {
3578 cmdsrv_main(&parmp->argc, parmp->argv,
3579 parmp->serverName_arg, &parmp->serverStr);
3580# ifdef FEAT_MBYTE
3581 parmp->serverStrEnc = vim_strsave(p_enc);
3582# endif
3583 }
3584
3585 /* If we're still running, get the name to register ourselves.
3586 * On Win32 can register right now, for X11 need to setup the
3587 * clipboard first, it's further down. */
3588 parmp->servername = serverMakeName(parmp->serverName_arg,
3589 parmp->argv[0]);
3590# ifdef WIN32
3591 if (parmp->servername != NULL)
3592 {
3593 serverSetName(parmp->servername);
3594 vim_free(parmp->servername);
3595 }
3596# endif
3597 }
3598}
3599
3600/*
3601 * Prepare for running as a Vim server.
3602 */
3603 static void
3604prepare_server(parmp)
3605 mparm_T *parmp;
3606{
3607# if defined(FEAT_X11)
3608 /*
3609 * Register for remote command execution with :serversend and --remote
3610 * unless there was a -X or a --servername '' on the command line.
3611 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003612 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003613 */
3614 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3615# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003616 (gui.in_use
3617# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003618 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003619# endif
3620 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003621# endif
3622 parmp->serverName_arg != NULL))
3623 {
3624 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3625 vim_free(parmp->servername);
3626 TIME_MSG("register server name");
3627 }
3628 else
3629 serverDelayedStartName = parmp->servername;
3630# endif
3631
3632 /*
3633 * Execute command ourselves if we're here because the send failed (or
3634 * else we would have exited above).
3635 */
3636 if (parmp->serverStr != NULL)
3637 {
3638 char_u *p;
3639
3640 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3641 parmp->serverStr, &p));
3642 vim_free(p);
3643 }
3644}
3645
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003646 static void
3647cmdsrv_main(argc, argv, serverName_arg, serverStr)
3648 int *argc;
3649 char **argv;
3650 char_u *serverName_arg;
3651 char_u **serverStr;
3652{
3653 char_u *res;
3654 int i;
3655 char_u *sname;
3656 int ret;
3657 int didone = FALSE;
3658 int exiterr = 0;
3659 char **newArgV = argv + 1;
3660 int newArgC = 1,
3661 Argc = *argc;
3662 int argtype;
3663#define ARGTYPE_OTHER 0
3664#define ARGTYPE_EDIT 1
3665#define ARGTYPE_EDIT_WAIT 2
3666#define ARGTYPE_SEND 3
3667 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003668 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003669# ifndef FEAT_X11
3670 HWND srv;
3671# else
3672 Window srv;
3673
3674 setup_term_clip();
3675# endif
3676
3677 sname = serverMakeName(serverName_arg, argv[0]);
3678 if (sname == NULL)
3679 return;
3680
3681 /*
3682 * Execute the command server related arguments and remove them
3683 * from the argc/argv array; We may have to return into main()
3684 */
3685 for (i = 1; i < Argc; i++)
3686 {
3687 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003688 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003689 {
3690 for (; i < *argc; i++)
3691 {
3692 *newArgV++ = argv[i];
3693 newArgC++;
3694 }
3695 break;
3696 }
3697
Bram Moolenaareb94e552006-03-11 21:35:11 +00003698 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003699 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003700 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3701 {
3702 char *p = argv[i] + 8;
3703
3704 argtype = ARGTYPE_EDIT;
3705 while (*p != NUL)
3706 {
3707 if (STRNICMP(p, "-wait", 5) == 0)
3708 {
3709 argtype = ARGTYPE_EDIT_WAIT;
3710 p += 5;
3711 }
3712 else if (STRNICMP(p, "-silent", 7) == 0)
3713 {
3714 silent = TRUE;
3715 p += 7;
3716 }
3717 else if (STRNICMP(p, "-tab", 4) == 0)
3718 {
3719 tabs = TRUE;
3720 p += 4;
3721 }
3722 else
3723 {
3724 argtype = ARGTYPE_OTHER;
3725 break;
3726 }
3727 }
3728 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003729 else
3730 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003731
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003732 if (argtype != ARGTYPE_OTHER)
3733 {
3734 if (i == *argc - 1)
3735 mainerr_arg_missing((char_u *)argv[i]);
3736 if (argtype == ARGTYPE_SEND)
3737 {
3738 *serverStr = (char_u *)argv[i + 1];
3739 i++;
3740 }
3741 else
3742 {
3743 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003744 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003745 if (*serverStr == NULL)
3746 {
3747 /* Probably out of memory, exit. */
3748 didone = TRUE;
3749 exiterr = 1;
3750 break;
3751 }
3752 Argc = i;
3753 }
3754# ifdef FEAT_X11
3755 if (xterm_dpy == NULL)
3756 {
3757 mch_errmsg(_("No display"));
3758 ret = -1;
3759 }
3760 else
3761 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3762 NULL, &srv, 0, 0, silent);
3763# else
3764 /* Win32 always works? */
3765 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3766# endif
3767 if (ret < 0)
3768 {
3769 if (argtype == ARGTYPE_SEND)
3770 {
3771 /* Failed to send, abort. */
3772 mch_errmsg(_(": Send failed.\n"));
3773 didone = TRUE;
3774 exiterr = 1;
3775 }
3776 else if (!silent)
3777 /* Let vim start normally. */
3778 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3779 break;
3780 }
3781
3782# ifdef FEAT_GUI_W32
3783 /* Guess that when the server name starts with "g" it's a GUI
3784 * server, which we can bring to the foreground here.
3785 * Foreground() in the server doesn't work very well. */
3786 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3787 SetForegroundWindow(srv);
3788# endif
3789
3790 /*
3791 * For --remote-wait: Wait until the server did edit each
3792 * file. Also detect that the server no longer runs.
3793 */
3794 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3795 {
3796 int numFiles = *argc - i - 1;
3797 int j;
3798 char_u *done = alloc(numFiles);
3799 char_u *p;
3800# ifdef FEAT_GUI_W32
3801 NOTIFYICONDATA ni;
3802 int count = 0;
3803 extern HWND message_window;
3804# endif
3805
3806 if (numFiles > 0 && argv[i + 1][0] == '+')
3807 /* Skip "+cmd" argument, don't wait for it to be edited. */
3808 --numFiles;
3809
3810# ifdef FEAT_GUI_W32
3811 ni.cbSize = sizeof(ni);
3812 ni.hWnd = message_window;
3813 ni.uID = 0;
3814 ni.uFlags = NIF_ICON|NIF_TIP;
3815 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3816 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3817 Shell_NotifyIcon(NIM_ADD, &ni);
3818# endif
3819
3820 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003821 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003822 while (memchr(done, 0, numFiles) != NULL)
3823 {
3824# ifdef WIN32
3825 p = serverGetReply(srv, NULL, TRUE, TRUE);
3826 if (p == NULL)
3827 break;
3828# else
3829 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3830 break;
3831# endif
3832 j = atoi((char *)p);
3833 if (j >= 0 && j < numFiles)
3834 {
3835# ifdef FEAT_GUI_W32
3836 ++count;
3837 sprintf(ni.szTip, _("%d of %d edited"),
3838 count, numFiles);
3839 Shell_NotifyIcon(NIM_MODIFY, &ni);
3840# endif
3841 done[j] = 1;
3842 }
3843 }
3844# ifdef FEAT_GUI_W32
3845 Shell_NotifyIcon(NIM_DELETE, &ni);
3846# endif
3847 }
3848 }
3849 else if (STRICMP(argv[i], "--remote-expr") == 0)
3850 {
3851 if (i == *argc - 1)
3852 mainerr_arg_missing((char_u *)argv[i]);
3853# ifdef WIN32
3854 /* Win32 always works? */
3855 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3856 &res, NULL, 1, FALSE) < 0)
3857# else
3858 if (xterm_dpy == NULL)
3859 mch_errmsg(_("No display: Send expression failed.\n"));
3860 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3861 &res, NULL, 1, 1, FALSE) < 0)
3862# endif
3863 {
3864 if (res != NULL && *res != NUL)
3865 {
3866 /* Output error from remote */
3867 mch_errmsg((char *)res);
3868 vim_free(res);
3869 res = NULL;
3870 }
3871 mch_errmsg(_(": Send expression failed.\n"));
3872 }
3873 }
3874 else if (STRICMP(argv[i], "--serverlist") == 0)
3875 {
3876# ifdef WIN32
3877 /* Win32 always works? */
3878 res = serverGetVimNames();
3879# else
3880 if (xterm_dpy != NULL)
3881 res = serverGetVimNames(xterm_dpy);
3882# endif
3883 if (called_emsg)
3884 mch_errmsg("\n");
3885 }
3886 else if (STRICMP(argv[i], "--servername") == 0)
3887 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003888 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003889 i++;
3890 continue;
3891 }
3892 else
3893 {
3894 *newArgV++ = argv[i];
3895 newArgC++;
3896 continue;
3897 }
3898 didone = TRUE;
3899 if (res != NULL && *res != NUL)
3900 {
3901 mch_msg((char *)res);
3902 if (res[STRLEN(res) - 1] != '\n')
3903 mch_msg("\n");
3904 }
3905 vim_free(res);
3906 }
3907
3908 if (didone)
3909 {
3910 display_errors(); /* display any collected messages */
3911 exit(exiterr); /* Mission accomplished - get out */
3912 }
3913
3914 /* Return back into main() */
3915 *argc = newArgC;
3916 vim_free(sname);
3917}
3918
3919/*
3920 * Build a ":drop" command to send to a Vim server.
3921 */
3922 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003923build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003924 int filec;
3925 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003926 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003927 int sendReply;
3928{
3929 garray_T ga;
3930 int i;
3931 char_u *inicmd = NULL;
3932 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003933 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003934 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003935
3936 if (filec > 0 && filev[0][0] == '+')
3937 {
3938 inicmd = (char_u *)filev[0] + 1;
3939 filev++;
3940 filec--;
3941 }
3942 /* Check if we have at least one argument. */
3943 if (filec <= 0)
3944 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003945
3946 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003947 cwd = alloc(MAXPATHL);
3948 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003949 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003950 if (mch_dirname(cwd, MAXPATHL) != OK)
3951 {
3952 vim_free(cwd);
3953 return NULL;
3954 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003955 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003956#ifdef BACKSLASH_IN_FILENAME
3957 "", /* rem_backslash() will tell what chars to escape */
3958#else
3959 PATH_ESC_CHARS,
3960#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003961 '\\', TRUE);
3962 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003963 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003964 return NULL;
3965 ga_init2(&ga, 1, 100);
3966 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003967 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003968
3969 /* Call inputsave() so that a prompt for an encryption key works. */
3970 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3971 if (tabs)
3972 ga_concat(&ga, (char_u *)"tab ");
3973 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003974 for (i = 0; i < filec; i++)
3975 {
3976 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003977 * do it again in the Vim server. On MS-Windows only escape
3978 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003979 p = vim_strsave_escaped((char_u *)filev[i],
3980#ifdef UNIX
3981 PATH_ESC_CHARS
3982#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003983 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003984#endif
3985 );
3986 if (p == NULL)
3987 {
3988 vim_free(ga.ga_data);
3989 return NULL;
3990 }
3991 ga_concat(&ga, (char_u *)" ");
3992 ga_concat(&ga, p);
3993 vim_free(p);
3994 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003995 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3996
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003997 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3998 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003999 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4000
4001 /* Switch back to the correct current directory (prior to temporary path
4002 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004003 * correct after the :drop command. With line breaks and spaces:
4004 * if !exists('+acd') || !&acd
4005 * if haslocaldir()
4006 * cd -
4007 * lcd -
4008 * elseif getcwd() ==# "current path"
4009 * cd -
4010 * endif
4011 * endif
4012 */
4013 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
4014 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# \"");
4015 ga_concat(&ga, cdp);
4016 ga_concat(&ga, (char_u *)"\"|cd -|endif|endif<CR>");
4017 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004018
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004019 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004020 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4021 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004022 if (inicmd != NULL)
4023 {
4024 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4025 * the following commands to be inserted as text. Use a "|",
4026 * hopefully "inicmd" does allow this... */
4027 ga_concat(&ga, inicmd);
4028 ga_concat(&ga, (char_u *)"|");
4029 }
4030 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4031 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004032 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004033 ga_append(&ga, NUL);
4034 return ga.ga_data;
4035}
4036
4037/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004038 * Make our basic server name: use the specified "arg" if given, otherwise use
4039 * the tail of the command "cmd" we were started with.
4040 * Return the name in allocated memory. This doesn't include a serial number.
4041 */
4042 static char_u *
4043serverMakeName(arg, cmd)
4044 char_u *arg;
4045 char *cmd;
4046{
4047 char_u *p;
4048
4049 if (arg != NULL && *arg != NUL)
4050 p = vim_strsave_up(arg);
4051 else
4052 {
4053 p = vim_strsave_up(gettail((char_u *)cmd));
4054 /* Remove .exe or .bat from the name. */
4055 if (p != NULL && vim_strchr(p, '.') != NULL)
4056 *vim_strchr(p, '.') = NUL;
4057 }
4058 return p;
4059}
4060#endif /* FEAT_CLIENTSERVER */
4061
4062#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4063/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004064 * Replace termcodes such as <CR> and insert as key presses if there is room.
4065 */
4066 void
4067server_to_input_buf(str)
4068 char_u *str;
4069{
4070 char_u *ptr = NULL;
4071 char_u *cpo_save = p_cpo;
4072
4073 /* Set 'cpoptions' the way we want it.
4074 * B set - backslashes are *not* treated specially
4075 * k set - keycodes are *not* reverse-engineered
4076 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004077 * The last but one parameter of replace_termcodes() is TRUE so that the
4078 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004079 */
4080 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004081 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004082 p_cpo = cpo_save;
4083
4084 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4085 {
4086 /*
4087 * Add the string to the input stream.
4088 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4089 *
4090 * First clear typed characters from the typeahead buffer, there could
4091 * be half a mapping there. Then append to the existing string, so
4092 * that multiple commands from a client are concatenated.
4093 */
4094 if (typebuf.tb_maplen < typebuf.tb_len)
4095 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4096 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4097
4098 /* Let input_available() know we inserted text in the typeahead
4099 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004100 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004101 }
4102 vim_free((char_u *)ptr);
4103}
4104
4105/*
4106 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004107 */
4108 char_u *
4109eval_client_expr_to_string(expr)
4110 char_u *expr;
4111{
4112 char_u *res;
4113 int save_dbl = debug_break_level;
4114 int save_ro = redir_off;
4115
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004116 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4117 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004118 debug_break_level = -1;
4119 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004120 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4121 * to be typed. Do generate errors so that try/catch works. */
4122 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004123
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004124 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004125
4126 debug_break_level = save_dbl;
4127 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004128 --emsg_silent;
4129 if (emsg_silent < 0)
4130 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004131
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004132 /* A client can tell us to redraw, but not to display the cursor, so do
4133 * that here. */
4134 setcursor();
4135 out_flush();
4136#ifdef FEAT_GUI
4137 if (gui.in_use)
4138 gui_update_cursor(FALSE, FALSE);
4139#endif
4140
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004141 return res;
4142}
4143
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004144/*
4145 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4146 * return an allocated string. Otherwise return "data".
4147 * "*tofree" is set to the result when it needs to be freed later.
4148 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004149 char_u *
4150serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004151 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004152 char_u *data;
4153 char_u **tofree;
4154{
4155 char_u *res = data;
4156
4157 *tofree = NULL;
4158# ifdef FEAT_MBYTE
4159 if (client_enc != NULL && p_enc != NULL)
4160 {
4161 vimconv_T vimconv;
4162
4163 vimconv.vc_type = CONV_NONE;
4164 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4165 && vimconv.vc_type != CONV_NONE)
4166 {
4167 res = string_convert(&vimconv, data, NULL);
4168 if (res == NULL)
4169 res = data;
4170 else
4171 *tofree = res;
4172 }
4173 convert_setup(&vimconv, NULL, NULL);
4174 }
4175# endif
4176 return res;
4177}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004178#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004179
4180/*
4181 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4182 */
4183#if defined(FEAT_FKMAP) || defined(PROTO)
4184# include "farsi.c"
4185#endif
4186
4187/*
4188 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4189 */
4190#if defined(FEAT_ARABIC) || defined(PROTO)
4191# include "arabic.c"
4192#endif