blob: 72f70b714d4952b0bf50a23c5be6629117a8c488 [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 Moolenaarf6303872015-04-03 17:59:43 +0200150static char_u *start_dir = NULL; /* current working dir on startup */
151
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100152#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100153#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000154 int
155# ifdef VIMDLL
156_export
157# endif
158# ifdef FEAT_GUI_MSWIN
159# ifdef __BORLANDC__
160_cdecl
161# endif
162VimMain
163# else
164main
165# endif
166(argc, argv)
167 int argc;
168 char **argv;
169{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000170 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000171 mparm_T params; /* various parameters passed between
172 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000173#ifdef STARTUPTIME
174 int i;
175#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000177 /*
178 * Do any system-specific initialisations. These can NOT use IObuff or
179 * NameBuff. Thus emsg2() cannot be called!
180 */
181 mch_early_init();
182
Bram Moolenaar14993322014-09-09 12:25:33 +0200183#if defined(WIN32) && defined(FEAT_MBYTE)
184 /*
185 * MingW expands command line arguments, which confuses our code to
186 * convert when 'encoding' changes. Get the unexpanded arguments.
187 */
188 argc = get_cmd_argsW(&argv);
189#endif
190
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 /* Many variables are in "params" so that we can pass them to invoked
192 * functions without a lot of arguments. "argc" and "argv" are also
193 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000194 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000195 params.argc = argc;
196 params.argv = argv;
197 params.want_full_screen = TRUE;
198#ifdef FEAT_EVAL
199 params.use_debug_break_level = -1;
200#endif
201#ifdef FEAT_WINDOWS
202 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000204
Bram Moolenaar99685e62013-05-11 13:56:18 +0200205#ifdef FEAT_RUBY
206 {
207 int ruby_stack_start;
208 vim_ruby_init((void *)&ruby_stack_start);
209 }
210#endif
211
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000213 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214#endif
215
216#ifdef MEM_PROFILE
217 atexit(vim_mem_profile_dump);
218#endif
219
220#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000221 for (i = 1; i < argc; ++i)
222 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000223 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000224 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000225 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000226 TIME_MSG("--- VIM STARTING ---");
227 break;
228 }
229 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000231 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232
233#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000234 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235#endif
236
237#ifdef FEAT_MBYTE
238 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
239#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000240#ifdef FEAT_EVAL
241 eval_init(); /* init global variables */
242#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243
244#ifdef __QNXNTO__
245 qnx_init(); /* PhAttach() for clipboard, (and gui) */
246#endif
247
248#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000249 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000251 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000252 TIME_MSG("GUI prepared");
253#endif
254
255 /* Init the table of Normal mode commands. */
256 init_normal_cmds();
257
258#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000259 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000260#endif
261
262 /*
263 * Allocate space for the generic buffers (needed for set_init_1() and
264 * EMSG2()).
265 */
266 if ((IObuff = alloc(IOSIZE)) == NULL
267 || (NameBuff = alloc(MAXPATHL)) == NULL)
268 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269 TIME_MSG("Allocated generic buffers");
270
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000271#ifdef NBDEBUG
272 /* Wait a moment for debugging NetBeans. Must be after allocating
273 * NameBuff. */
274 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
275 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000276 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000277#endif
278
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000279#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
280 /*
281 * Setup to use the current locale (for ctype() and many other things).
282 * NOTE: Translated messages with encodings other than latin1 will not
283 * work until set_init_1() has been called!
284 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000285 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000286 TIME_MSG("locale set");
287#endif
288
289#ifdef FEAT_GUI
290 gui.dofork = TRUE; /* default is to use fork() */
291#endif
292
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000293 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000294 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000295 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000296 * --server...
297 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000298 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000299 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000300 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000301
302#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000303 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000304#endif
305#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000306 /* Prepare for possibly starting GUI sometime */
307 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000308 TIME_MSG("GUI prepared");
309#endif
310
311#ifdef FEAT_CLIPBOARD
312 clip_init(FALSE); /* Initialise clipboard stuff */
313 TIME_MSG("clipboard setup");
314#endif
315
316 /*
317 * Check if we have an interactive window.
318 * On the Amiga: If there is no window, we open one with a newcli command
319 * (needed for :! to * work). mch_check_win() will also handle the -d or
320 * -dev argument.
321 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000322 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000323 TIME_MSG("window checked");
324
325 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000326 * Allocate the first window and buffer.
327 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000328 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000329 if (win_alloc_first() == FAIL)
330 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331
332 init_yank(); /* init yank buffers */
333
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000334 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200335 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336
337 /*
338 * Set the default values for the options.
339 * NOTE: Non-latin1 translated messages are working only after this,
340 * because this is where "has_mbyte" will be set, which is used by
341 * msg_outtrans_len_attr().
342 * First find out the home directory, needed to expand "~" in options.
343 */
344 init_homedir(); /* find real value of $HOME */
345 set_init_1();
346 TIME_MSG("inits 1");
347
348#ifdef FEAT_EVAL
349 set_lang_var(); /* set v:lang and v:ctype */
350#endif
351
352#ifdef FEAT_CLIENTSERVER
353 /*
354 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000355 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000356 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000358#endif
359
360 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000361 * Figure out the way to work from the command name argv[0].
362 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000363 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365
366 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 * Process the command line arguments. File names are put in the global
368 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000369 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000370 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 TIME_MSG("parsing arguments");
372
373 /*
374 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000375 * there is no terminal version, and on Windows we can't fork one off with
376 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000377 */
378#ifdef ALWAYS_USE_GUI
379 gui.starting = TRUE;
380#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000381# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000382 /*
383 * Check if the GUI can be started. Reset gui.starting if not.
384 * Don't know about other systems, stay on the safe side and don't check.
385 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000386 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000387 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000388 if (gui_init_check() == FAIL)
389 {
390 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000391
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000392 /* When running "evim" or "gvim -y" we need the menus, exit if we
393 * don't have them. */
394 if (params.evim_mode)
395 mch_exit(1);
396 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397 }
398# endif
399#endif
400
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000401 if (GARGCOUNT > 0)
402 {
403#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
404 /*
405 * Expand wildcards in file names.
406 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000407 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200409 start_dir = alloc(MAXPATHL);
410 if (start_dir != NULL)
411 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000412 /* Temporarily add '(' and ')' to 'isfname'. These are valid
413 * filename characters but are excluded from 'isfname' to make
414 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
415 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000416 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000417 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200418 if (start_dir != NULL)
419 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000420 }
421#endif
422 fname = alist_name(&GARGLIST[0]);
423 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000424
425#if defined(WIN32) && defined(FEAT_MBYTE)
426 {
427 extern void set_alist_count(void);
428
429 /* Remember the number of entries in the argument list. If it changes
430 * we don't react on setting 'encoding'. */
431 set_alist_count();
432 }
433#endif
434
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000435#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000436 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000437 {
438 /*
439 * If there is one filename, fully qualified, we have very probably
440 * been invoked from explorer, so change to the file's directory.
441 * Hint: to avoid this when typing a command use a forward slash.
442 * If the cd fails, it doesn't matter.
443 */
444 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200445 if (start_dir != NULL)
446 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447 }
448#endif
449 TIME_MSG("expanding arguments");
450
451#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000452 if (params.diff_mode && params.window_count == -1)
453 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000454#endif
455
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000456 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000457 ++RedrawingDisabled;
458
459 /*
460 * When listing swap file names, don't do cursor positioning et. al.
461 */
462 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000463 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000464
465 /*
466 * When certain to start the GUI, don't check capabilities of terminal.
467 * For GTK we can't be sure, but when started from the desktop it doesn't
468 * make sense to try using a terminal.
469 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000470#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000471 if (gui.starting
472# ifdef FEAT_GUI_GTK
473 && !isatty(2)
474# endif
475 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000476 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000477#endif
478
479#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
480 /* When the GUI is started from Finder, need to display messages in a
481 * message box. isatty(2) returns TRUE anyway, thus we need to check the
482 * name to know we're not started from a terminal. */
483 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000484 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000485 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000486
487 /* Avoid always using "/" as the current directory. Note that when
488 * started from Finder the arglist will be filled later in
489 * HandleODocAE() and "fname" will be NULL. */
490 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
491 && STRCMP(NameBuff, "/") == 0)
492 {
493 if (fname != NULL)
494 (void)vim_chdirfile(fname);
495 else
496 {
497 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
498 vim_chdir(NameBuff);
499 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200500 if (start_dir != NULL)
501 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000502 }
503 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000504#endif
505
506 /*
507 * mch_init() sets up the terminal (window) for use. This must be
508 * done after resetting full_screen, otherwise it may move the cursor
509 * (MSDOS).
510 * Note that we may use mch_exit() before mch_init()!
511 */
512 mch_init();
513 TIME_MSG("shell init");
514
515#ifdef USE_XSMP
516 /*
517 * For want of anywhere else to do it, try to connect to xsmp here.
518 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
519 * Hijacking -X 'no X connection' to also disable XSMP connection as that
520 * has a similar delay upon failure.
521 * Only try if SESSION_MANAGER is set to something non-null.
522 */
523 if (!x_no_connect)
524 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000525 char *p = getenv("SESSION_MANAGER");
526
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527 if (p != NULL && *p != NUL)
528 {
529 xsmp_init();
530 TIME_MSG("xsmp init");
531 }
532 }
533#endif
534
535 /*
536 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000538 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 /* This message comes before term inits, but after setting "silent_mode"
541 * when the input is not a tty. */
542 if (GARGCOUNT > 1 && !silent_mode)
543 printf(_("%d files to edit\n"), GARGCOUNT);
544
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000545 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000547 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548 capabilities (will set full_screen) */
549 screen_start(); /* don't know where cursor is now */
550 TIME_MSG("Termcap init");
551 }
552
553 /*
554 * Set the default values for the options that use Rows and Columns.
555 */
556 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000557 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000558#ifdef FEAT_DIFF
559 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
560 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000561 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562 diff_win_options(firstwin, FALSE);
563#endif
564
565 cmdline_row = Rows - p_ch;
566 msg_row = cmdline_row;
567 screenalloc(FALSE); /* allocate screen buffers */
568 set_init_2();
569 TIME_MSG("inits 2");
570
571 msg_scroll = TRUE;
572 no_wait_return = TRUE;
573
574 init_mappings(); /* set up initial mappings */
575
576 init_highlight(TRUE, FALSE); /* set the default highlight groups */
577 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578
579#ifdef FEAT_EVAL
580 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000581 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000582#endif
583
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100584#ifdef FEAT_MZSCHEME
585 /*
586 * Newer version of MzScheme (Racket) require earlier (trampolined)
587 * initialisation via scheme_main_setup.
588 * Implement this by initialising it as early as possible
589 * and splitting off remaining Vim main into vim_main2
590 */
591 {
592 /* Pack up preprocessed command line arguments.
593 * It is safe because Scheme does not access argc/argv. */
594 char *args[2];
595 args[0] = (char *)fname;
596 args[1] = (char *)&params;
597 return mzscheme_main(2, args);
598 }
599}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100600#endif
601#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100602
Bram Moolenaar8866d272012-11-28 15:55:42 +0100603/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
604 * NO_VIM_MAIN is defined. */
605#ifdef FEAT_MZSCHEME
606 int
607vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100608{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100609# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100610 char_u *fname = (char_u *)argv[0];
611 mparm_T params;
612
613 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100614# else
615 return 0;
616}
617# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100618#endif
619
Bram Moolenaar8866d272012-11-28 15:55:42 +0100620#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000621 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000622 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000623
Bram Moolenaar58d98232005-07-23 22:25:46 +0000624 /* Source startup scripts. */
625 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000626
627#ifdef FEAT_EVAL
628 /*
629 * Read all the plugin files.
630 * Only when compiled with +eval, since most plugins need it.
631 */
632 if (p_lpl)
633 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000634# ifdef VMS /* Somehow VMS doesn't handle the "**". */
635 source_runtime((char_u *)"plugin/*.vim", TRUE);
636# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000637 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000638# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000639 TIME_MSG("loading plugins");
640 }
641#endif
642
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000643#ifdef FEAT_DIFF
644 /* Decide about window layout for diff mode after reading vimrc. */
645 if (params.diff_mode && params.window_layout == 0)
646 {
647 if (diffopt_horizontal())
648 params.window_layout = WIN_HOR; /* use horizontal split */
649 else
650 params.window_layout = WIN_VER; /* use vertical split */
651 }
652#endif
653
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000654 /*
655 * Recovery mode without a file name: List swap files.
656 * This uses the 'dir' option, therefore it must be after the
657 * initializations.
658 */
659 if (recoverymode && fname == NULL)
660 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200661 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000662 mch_exit(0);
663 }
664
665 /*
666 * Set a few option defaults after reading .vimrc files:
667 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
668 */
669 set_init_3();
670 TIME_MSG("inits 3");
671
672 /*
673 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
674 * Note that this overrides anything from a vimrc file.
675 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000676 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000677 p_uc = 0;
678
679#ifdef FEAT_FKMAP
680 if (curwin->w_p_rl && p_altkeymap)
681 {
682 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
683# ifdef FEAT_ARABIC
684 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
685# endif
686 p_fkmap = TRUE; /* Set the Farsi keymap mode */
687 }
688#endif
689
690#ifdef FEAT_GUI
691 if (gui.starting)
692 {
693#if defined(UNIX) || defined(VMS)
694 /* When something caused a message from a vimrc script, need to output
695 * an extra newline before the shell prompt. */
696 if (did_emsg || msg_didout)
697 putchar('\n');
698#endif
699
700 gui_start(); /* will set full_screen to TRUE */
701 TIME_MSG("starting GUI");
702
703 /* When running "evim" or "gvim -y" we need the menus, exit if we
704 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000705 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000706 mch_exit(1);
707 }
708#endif
709
710#ifdef SPAWNO /* special MSDOS swapping library */
711 init_SPAWNO("", SWAP_ANY);
712#endif
713
714#ifdef FEAT_VIMINFO
715 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000716 * Read in registers, history etc, but not marks, from the viminfo file.
717 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 */
719 if (*p_viminfo != NUL)
720 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000721 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000722 TIME_MSG("reading viminfo");
723 }
724#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100725#ifdef FEAT_EVAL
726 /* It's better to make v:oldfiles an empty list than NULL. */
727 if (get_vim_var_list(VV_OLDFILES) == NULL)
728 set_vim_var_list(VV_OLDFILES, list_alloc());
729#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000730
731#ifdef FEAT_QUICKFIX
732 /*
733 * "-q errorfile": Load the error file now.
734 * If the error file can't be read, exit before doing anything else.
735 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000736 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000737 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000738 if (params.use_ef != NULL)
739 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000740 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200741 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
742 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000743 {
744 out_char('\n');
745 mch_exit(3);
746 }
747 TIME_MSG("reading errorfile");
748 }
749#endif
750
751 /*
752 * Start putting things on the screen.
753 * Scroll screen down before drawing over it
754 * Clear screen now, so file message will not be cleared.
755 */
756 starting = NO_BUFFERS;
757 no_wait_return = FALSE;
758 if (!exmode_active)
759 msg_scroll = FALSE;
760
761#ifdef FEAT_GUI
762 /*
763 * This seems to be required to make callbacks to be called now, instead
764 * of after things have been put on the screen, which then may be deleted
765 * when getting a resize callback.
766 * For the Mac this handles putting files dropped on the Vim icon to
767 * global_alist.
768 */
769 if (gui.in_use)
770 {
771# ifdef FEAT_SUN_WORKSHOP
772 if (!usingSunWorkShop)
773# endif
774 gui_wait_for_chars(50L);
775 TIME_MSG("GUI delay");
776 }
777#endif
778
779#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
780 qnx_clip_init();
781#endif
782
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200783#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
784 clip_init(TRUE);
785#endif
786
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000787#ifdef FEAT_XCLIPBOARD
788 /* Start using the X clipboard, unless the GUI was started. */
789# ifdef FEAT_GUI
790 if (!gui.in_use)
791# endif
792 {
793 setup_term_clip();
794 TIME_MSG("setup clipboard");
795 }
796#endif
797
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000798#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000799 /* Prepare for being a Vim server. */
800 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801#endif
802
803 /*
804 * If "-" argument given: Read file from stdin.
805 * Do this before starting Raw mode, because it may change things that the
806 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
807 * are the same terminal: "cat | vim -".
808 * Using autocommands here may cause trouble...
809 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000810 if (params.edit_type == EDIT_STDIN && !recoverymode)
811 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000812
813#if defined(UNIX) || defined(VMS)
814 /* When switching screens and something caused a message from a vimrc
815 * script, need to output an extra newline on exit. */
816 if ((did_emsg || msg_didout) && *T_TI != NUL)
817 newline_on_exit = TRUE;
818#endif
819
820 /*
821 * When done something that is not allowed or error message call
822 * wait_return. This must be done before starttermcap(), because it may
823 * switch to another screen. It must be done after settmode(TMODE_RAW),
824 * because we want to react on a single key stroke.
825 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000826 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000827 */
828 settmode(TMODE_RAW);
829 TIME_MSG("setting raw mode");
830
831 if (need_wait_return || msg_didany)
832 {
833 wait_return(TRUE);
834 TIME_MSG("waiting for return");
835 }
836
837 starttermcap(); /* start termcap if not done by wait_return() */
838 TIME_MSG("start termcap");
Bram Moolenaar9584b312013-03-13 19:29:28 +0100839#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200840 may_req_ambiguous_char_width();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100841#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000842
843#ifdef FEAT_MOUSE
844 setmouse(); /* may start using the mouse */
845#endif
846 if (scroll_region)
847 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000848 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000849
850 /*
851 * Don't clear the screen when starting in Ex mode, unless using the GUI.
852 */
853 if (exmode_active
854#ifdef FEAT_GUI
855 && !gui.in_use
856#endif
857 )
858 must_redraw = CLEAR;
859 else
860 {
861 screenclear(); /* clear screen */
862 TIME_MSG("clearing screen");
863 }
864
865#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000866 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000867 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100868 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200869 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000870 TIME_MSG("getting crypt key");
871 }
872#endif
873
874 no_wait_return = TRUE;
875
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000876 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000877 * Create the requested number of windows and edit buffers in them.
878 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000879 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000880 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881 TIME_MSG("opening buffers");
882
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000883#ifdef FEAT_EVAL
884 /* clear v:swapcommand */
885 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
886#endif
887
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000888 /* Ex starts at last line of the file */
889 if (exmode_active)
890 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
891
892#ifdef FEAT_AUTOCMD
893 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
894 TIME_MSG("BufEnter autocommands");
895#endif
896 setpcmark();
897
898#ifdef FEAT_QUICKFIX
899 /*
900 * When started with "-q errorfile" jump to first error now.
901 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000902 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000903 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000904 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000905 TIME_MSG("jump to first error");
906 }
907#endif
908
909#ifdef FEAT_WINDOWS
910 /*
911 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000912 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000913 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200914 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000915#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200916 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000917
918#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000919 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000920 {
921 win_T *wp;
922
923 /* set options in each window for "vimdiff". */
924 for (wp = firstwin; wp != NULL; wp = wp->w_next)
925 diff_win_options(wp, TRUE);
926 }
927#endif
928
929 /*
930 * Shorten any of the filenames, but only when absolute.
931 */
932 shorten_fnames(FALSE);
933
934 /*
935 * Need to jump to the tag before executing the '-c command'.
936 * Makes "vim -c '/return' -t main" work.
937 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000938 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000939 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000940#if defined(HAS_SWAP_EXISTS_ACTION)
941 swap_exists_did_quit = FALSE;
942#endif
943
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000944 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000945 do_cmdline_cmd(IObuff);
946 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000947
948#if defined(HAS_SWAP_EXISTS_ACTION)
949 /* If the user doesn't want to edit the file then we quit here. */
950 if (swap_exists_did_quit)
951 getout(1);
952#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000953 }
954
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000955 /* Execute any "+", "-c" and "-S" arguments. */
956 if (params.n_commands > 0)
957 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000958
959 RedrawingDisabled = 0;
960 redraw_all_later(NOT_VALID);
961 no_wait_return = FALSE;
962 starting = 0;
963
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000964#ifdef FEAT_TERMRESPONSE
965 /* Requesting the termresponse is postponed until here, so that a "-c q"
966 * argument doesn't make it appear in the shell Vim was started from. */
967 may_req_termresponse();
968#endif
969
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000970 /* start in insert mode */
971 if (p_im)
972 need_start_insertmode = TRUE;
973
974#ifdef FEAT_AUTOCMD
975 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
976 TIME_MSG("VimEnter autocommands");
977#endif
978
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200979#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
980 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
981 * done after the clipboard is available and all initial commands that may
982 * modify the 'clipboard' setting have run; i.e. just before entering the
983 * main loop. */
984 {
985 int default_regname = 0;
986 adjust_clip_reg(&default_regname);
987 set_reg_var(default_regname);
988 }
989#endif
990
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000991#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
992 /* When a startup script or session file setup for diff'ing and
993 * scrollbind, sync the scrollbind now. */
994 if (curwin->w_p_diff && curwin->w_p_scb)
995 {
996 update_topline();
997 check_scrollbind((linenr_T)0, 0L);
998 TIME_MSG("diff scrollbinding");
999 }
1000#endif
1001
1002#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1003 mch_set_winsize_now(); /* Allow winsize changes from now on */
1004#endif
1005
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001006#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1007 /* When tab pages were created, may need to update the tab pages line and
1008 * scrollbars. This is skipped while creating them. */
1009 if (first_tabpage->tp_next != NULL)
1010 {
1011 out_flush();
1012 gui_init_which_components(NULL);
1013 gui_update_scrollbars(TRUE);
1014 }
1015 need_mouse_correct = TRUE;
1016#endif
1017
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001018 /* If ":startinsert" command used, stuff a dummy command to be able to
1019 * call normal_cmd(), which will then start Insert mode. */
1020 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001021 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001022
1023#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001024 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001025 {
1026# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001027# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001028 && !defined(FEAT_GUI_W32)
1029 if (gui.in_use)
1030 {
1031 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1032 mch_exit(2);
1033 }
1034# endif
1035# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001036 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001037 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001038 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001039#endif
1040
1041 TIME_MSG("before starting main loop");
1042
1043 /*
1044 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001045 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001046 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001047
1048 return 0;
1049}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001050#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001051#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001052
1053/*
1054 * Main loop: Execute Normal mode commands until exiting Vim.
1055 * Also used to handle commands in the command-line window, until the window
1056 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 * Also used to handle ":visual" command after ":global": execute Normal mode
1058 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001059 */
1060 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061main_loop(cmdwin, noexmode)
1062 int cmdwin; /* TRUE when working in the command-line window */
1063 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001064{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001065 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001066 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001067#ifdef FEAT_CONCEAL
1068 linenr_T conceal_old_cursor_line = 0;
1069 linenr_T conceal_new_cursor_line = 0;
1070 int conceal_update_lines = FALSE;
1071#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001072
1073#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1074 /* Setup to catch a terminating error from the X server. Just ignore
1075 * it, restore the state and continue. This might not always work
1076 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001077 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001078 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001079 {
1080 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001081 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001082 got_int = TRUE;
1083 need_wait_return = FALSE;
1084 global_busy = FALSE;
1085 exmode_active = 0;
1086 skip_redraw = FALSE;
1087 RedrawingDisabled = 0;
1088 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001089 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001090# ifdef FEAT_EVAL
1091 emsg_skip = 0;
1092# endif
1093 emsg_off = 0;
1094# ifdef FEAT_MOUSE
1095 setmouse();
1096# endif
1097 settmode(TMODE_RAW);
1098 starttermcap();
1099 scroll_start();
1100 redraw_later_clear();
1101 }
1102#endif
1103
1104 clear_oparg(&oa);
1105 while (!cmdwin
1106#ifdef FEAT_CMDWIN
1107 || cmdwin_result == 0
1108#endif
1109 )
1110 {
1111 if (stuff_empty())
1112 {
1113 did_check_timestamps = FALSE;
1114 if (need_check_timestamps)
1115 check_timestamps(FALSE);
1116 if (need_wait_return) /* if wait_return still needed ... */
1117 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001118 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001119 {
1120 need_start_insertmode = FALSE;
1121 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1122 /* skip the fileinfo message now, because it would be shown
1123 * after insert mode finishes! */
1124 need_fileinfo = FALSE;
1125 }
1126 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001127
1128 /* Reset "got_int" now that we got back to the main loop. Except when
1129 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1130 * the ":g" command.
1131 * For ":g/pat/vi" we reset "got_int" when used once. When used
1132 * a second time we go back to Ex mode and abort the ":g" command. */
1133 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001134 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001135 if (noexmode && global_busy && !exmode_active && previous_got_int)
1136 {
1137 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1138 * used and keep "got_int" set, so that it aborts ":g". */
1139 exmode_active = EXMODE_NORMAL;
1140 State = NORMAL;
1141 }
1142 else if (!global_busy || !exmode_active)
1143 {
1144 if (!quit_more)
1145 (void)vgetc(); /* flush all buffers */
1146 got_int = FALSE;
1147 }
1148 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001149 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001150 else
1151 previous_got_int = FALSE;
1152
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001153 if (!exmode_active)
1154 msg_scroll = FALSE;
1155 quit_more = FALSE;
1156
1157 /*
1158 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1159 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1160 * update cursor and redraw.
1161 */
1162 if (skip_redraw || exmode_active)
1163 skip_redraw = FALSE;
1164 else if (do_redraw || stuff_empty())
1165 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001166#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001167 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001168 if (!finish_op && (
1169# ifdef FEAT_AUTOCMD
1170 has_cursormoved()
1171# endif
1172# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1173 ||
1174# endif
1175# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001176 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001177# endif
1178 )
1179 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001180 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001181# ifdef FEAT_AUTOCMD
1182 if (has_cursormoved())
1183 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1184 FALSE, curbuf);
1185# endif
1186# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001187 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001188 {
1189 conceal_old_cursor_line = last_cursormoved.lnum;
1190 conceal_new_cursor_line = curwin->w_cursor.lnum;
1191 conceal_update_lines = TRUE;
1192 }
1193# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001194 last_cursormoved = curwin->w_cursor;
1195 }
1196#endif
1197
Bram Moolenaar186628f2013-03-19 13:33:23 +01001198#ifdef FEAT_AUTOCMD
1199 /* Trigger TextChanged if b_changedtick differs. */
1200 if (!finish_op && has_textchanged()
1201 && last_changedtick != curbuf->b_changedtick)
1202 {
1203 if (last_changedtick_buf == curbuf)
1204 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1205 FALSE, curbuf);
1206 last_changedtick_buf = curbuf;
1207 last_changedtick = curbuf->b_changedtick;
1208 }
1209#endif
1210
Bram Moolenaar33aec762006-01-22 23:30:12 +00001211#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1212 /* Scroll-binding for diff mode may have been postponed until
1213 * here. Avoids doing it for every change. */
1214 if (diff_need_scrollbind)
1215 {
1216 check_scrollbind((linenr_T)0, 0L);
1217 diff_need_scrollbind = FALSE;
1218 }
1219#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001220#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001221 /* Include a closed fold completely in the Visual area. */
1222 foldAdjustVisual();
1223#endif
1224#ifdef FEAT_FOLDING
1225 /*
1226 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1227 * contain the cursor.
1228 * When 'foldopen' is "all", open the fold(s) under the cursor.
1229 * This may mark the window for redrawing.
1230 */
1231 if (hasAnyFolding(curwin) && !char_avail())
1232 {
1233 foldCheckClose();
1234 if (fdo_flags & FDO_ALL)
1235 foldOpenCursor();
1236 }
1237#endif
1238
1239 /*
1240 * Before redrawing, make sure w_topline is correct, and w_leftcol
1241 * if lines don't wrap, and w_skipcol if lines wrap.
1242 */
1243 update_topline();
1244 validate_cursor();
1245
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001246 if (VIsual_active)
1247 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001248 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001249 update_screen(0);
1250 else if (redraw_cmdline || clear_cmdline)
1251 showmode();
1252#ifdef FEAT_WINDOWS
1253 redraw_statuslines();
1254#endif
1255#ifdef FEAT_TITLE
1256 if (need_maketitle)
1257 maketitle();
1258#endif
1259 /* display message after redraw */
1260 if (keep_msg != NULL)
1261 {
1262 char_u *p;
1263
1264 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001265 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1266 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001267 p = keep_msg;
1268 msg_attr(p, keep_msg_attr);
1269 vim_free(p);
1270 }
1271 if (need_fileinfo) /* show file info after redraw */
1272 {
1273 fileinfo(FALSE, TRUE, FALSE);
1274 need_fileinfo = FALSE;
1275 }
1276
1277 emsg_on_display = FALSE; /* can delete error message now */
1278 did_emsg = FALSE;
1279 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001280 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001281 showruler(FALSE);
1282
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001283# if defined(FEAT_CONCEAL)
1284 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001285 && (conceal_old_cursor_line != conceal_new_cursor_line
1286 || conceal_cursor_line(curwin)
1287 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001288 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001289 if (conceal_old_cursor_line != conceal_new_cursor_line
1290 && conceal_old_cursor_line
1291 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001292 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001293 update_single_line(curwin, conceal_new_cursor_line);
1294 curwin->w_valid &= ~VALID_CROW;
1295 }
1296# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001297 setcursor();
1298 cursor_on();
1299
1300 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001301
1302#ifdef STARTUPTIME
1303 /* Now that we have drawn the first screen all the startup stuff
1304 * has been done, close any file for startup messages. */
1305 if (time_fd != NULL)
1306 {
1307 TIME_MSG("first screen update");
1308 TIME_MSG("--- VIM STARTED ---");
1309 fclose(time_fd);
1310 time_fd = NULL;
1311 }
1312#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001313 }
1314#ifdef FEAT_GUI
1315 if (need_mouse_correct)
1316 gui_mouse_correct();
1317#endif
1318
1319 /*
1320 * Update w_curswant if w_set_curswant has been set.
1321 * Postponed until here to avoid computing w_virtcol too often.
1322 */
1323 update_curswant();
1324
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001325#ifdef FEAT_EVAL
1326 /*
1327 * May perform garbage collection when waiting for a character, but
1328 * only at the very toplevel. Otherwise we may be using a List or
1329 * Dict internally somewhere.
1330 * "may_garbage_collect" is reset in vgetc() which is invoked through
1331 * do_exmode() and normal_cmd().
1332 */
1333 may_garbage_collect = (!cmdwin && !noexmode);
1334#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001335 /*
1336 * If we're invoked as ex, do a round of ex commands.
1337 * Otherwise, get and execute a normal mode command.
1338 */
1339 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001340 {
1341 if (noexmode) /* End of ":global/path/visual" commands */
1342 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001343 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001344 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001345 else
1346 normal_cmd(&oa, TRUE);
1347 }
1348}
1349
1350
1351#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1352/*
1353 * Exit, but leave behind swap files for modified buffers.
1354 */
1355 void
1356getout_preserve_modified(exitval)
1357 int exitval;
1358{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001359# if defined(SIGHUP) && defined(SIG_IGN)
1360 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1361 * makes Vim exit and then handling SIGHUP causes various reentrance
1362 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001363 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001364# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001365
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001366 ml_close_notmod(); /* close all not-modified buffers */
1367 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1368 ml_close_all(FALSE); /* close all memfiles, without deleting */
1369 getout(exitval); /* exit Vim properly */
1370}
1371#endif
1372
1373
1374/* Exit properly */
1375 void
1376getout(exitval)
1377 int exitval;
1378{
1379#ifdef FEAT_AUTOCMD
1380 buf_T *buf;
1381 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001382 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383#endif
1384
1385 exiting = TRUE;
1386
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001387 /* When running in Ex mode an error causes us to exit with a non-zero exit
1388 * code. POSIX requires this, although it's not 100% clear from the
1389 * standard. */
1390 if (exmode_active)
1391 exitval += ex_exitval;
1392
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001393 /* Position the cursor on the last screen line, below all the text */
1394#ifdef FEAT_GUI
1395 if (!gui.in_use)
1396#endif
1397 windgoto((int)Rows - 1, 0);
1398
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001399#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1400 /* Optionally print hashtable efficiency. */
1401 hash_debug_results();
1402#endif
1403
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001404#ifdef FEAT_GUI
1405 msg_didany = FALSE;
1406#endif
1407
1408#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001409 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001410 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001411 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1412# if defined FEAT_WINDOWS
1413 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001414 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001415 next_tp = tp->tp_next;
1416 for (wp = (tp == curtab)
1417 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001418 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001419 if (wp->w_buffer == NULL)
1420 /* Autocmd must have close the buffer already, skip. */
1421 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 buf = wp->w_buffer;
1423 if (buf->b_changedtick != -1)
1424 {
1425 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1426 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001427 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001428 /* start all over, autocommands may mess up the lists */
1429 next_tp = first_tabpage;
1430 break;
1431 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001432 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001433 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001434# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001435 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1436 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001437# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001438
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 /* Trigger BufUnload for buffers that are loaded */
1440 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1441 if (buf->b_ml.ml_mfp != NULL)
1442 {
1443 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001444 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001445 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1446 break;
1447 }
1448 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1449 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450#endif
1451
1452#ifdef FEAT_VIMINFO
1453 if (*p_viminfo != NUL)
1454 /* Write out the registers, history, marks etc, to the viminfo file */
1455 write_viminfo(NULL, FALSE);
1456#endif
1457
1458#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001459 if (get_vim_var_nr(VV_DYING) <= 1)
1460 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001461#endif
1462
Bram Moolenaar05159a02005-02-26 23:04:13 +00001463#ifdef FEAT_PROFILE
1464 profile_dump();
1465#endif
1466
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001467 if (did_emsg
1468#ifdef FEAT_GUI
1469 || (gui.in_use && msg_didany && p_verbose > 0)
1470#endif
1471 )
1472 {
1473 /* give the user a chance to read the (error) message */
1474 no_wait_return = FALSE;
1475 wait_return(FALSE);
1476 }
1477
1478#ifdef FEAT_AUTOCMD
1479 /* Position the cursor again, the autocommands may have moved it */
1480# ifdef FEAT_GUI
1481 if (!gui.in_use)
1482# endif
1483 windgoto((int)Rows - 1, 0);
1484#endif
1485
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001486#ifdef FEAT_LUA
1487 lua_end();
1488#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001489#ifdef FEAT_MZSCHEME
1490 mzscheme_end();
1491#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001492#ifdef FEAT_TCL
1493 tcl_end();
1494#endif
1495#ifdef FEAT_RUBY
1496 ruby_end();
1497#endif
1498#ifdef FEAT_PYTHON
1499 python_end();
1500#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001501#ifdef FEAT_PYTHON3
1502 python3_end();
1503#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504#ifdef FEAT_PERL
1505 perl_end();
1506#endif
1507#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1508 iconv_end();
1509#endif
1510#ifdef FEAT_NETBEANS_INTG
1511 netbeans_end();
1512#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001513#ifdef FEAT_CSCOPE
1514 cs_end();
1515#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001516#ifdef FEAT_EVAL
1517 if (garbage_collect_at_exit)
1518 garbage_collect();
1519#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001520#if defined(WIN32) && defined(FEAT_MBYTE)
1521 free_cmd_argsW();
1522#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001523
1524 mch_exit(exitval);
1525}
1526
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001527#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001528/*
1529 * Get a (optional) count for a Vim argument.
1530 */
1531 static int
1532get_number_arg(p, idx, def)
1533 char_u *p; /* pointer to argument */
1534 int *idx; /* index in argument, is incremented */
1535 int def; /* default value */
1536{
1537 if (vim_isdigit(p[*idx]))
1538 {
1539 def = atoi((char *)&(p[*idx]));
1540 while (vim_isdigit(p[*idx]))
1541 *idx = *idx + 1;
1542 }
1543 return def;
1544}
1545
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001546#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1547/*
1548 * Setup to use the current locale (for ctype() and many other things).
1549 */
1550 static void
1551init_locale()
1552{
1553 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001555# ifdef FEAT_GUI_GTK
1556 /* Tell Gtk not to change our locale settings. */
1557 gtk_disable_setlocale();
1558# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1560 /* Make sure strtod() uses a decimal point, not a comma. */
1561 setlocale(LC_NUMERIC, "C");
1562# endif
1563
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001564# ifdef WIN32
1565 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1566 * text while it's expecting text in the current locale. This call avoids
1567 * that. */
1568 setlocale(LC_CTYPE, "C");
1569# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001570
1571# ifdef FEAT_GETTEXT
1572 {
1573 int mustfree = FALSE;
1574 char_u *p;
1575
1576# ifdef DYNAMIC_GETTEXT
1577 /* Initialize the gettext library */
1578 dyn_libintl_init(NULL);
1579# endif
1580 /* expand_env() doesn't work yet, because chartab[] is not initialized
1581 * yet, call vim_getenv() directly */
1582 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1583 if (p != NULL && *p != NUL)
1584 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001585 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001586 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1587 }
1588 if (mustfree)
1589 vim_free(p);
1590 textdomain(VIMPACKAGE);
1591 }
1592# endif
1593}
1594#endif
1595
1596/*
1597 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1598 * If the executable name starts with "r" we disable shell commands.
1599 * If the next character is "e" we run in Easy mode.
1600 * If the next character is "g" we run the GUI version.
1601 * If the next characters are "view" we start in readonly mode.
1602 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1603 * If the next characters are "ex" we start in Ex mode. If it's followed
1604 * by "im" use improved Ex mode.
1605 */
1606 static void
1607parse_command_name(parmp)
1608 mparm_T *parmp;
1609{
1610 char_u *initstr;
1611
1612 initstr = gettail((char_u *)parmp->argv[0]);
1613
1614#ifdef MACOS_X_UNIX
1615 /* An issue has been seen when launching Vim in such a way that
1616 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1617 * executable or a symbolic link of it. Until this issue is resolved
1618 * we prohibit the GUI from being used.
1619 */
1620 if (STRCMP(initstr, parmp->argv[0]) == 0)
1621 disallow_gui = TRUE;
1622
1623 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001624 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001625#endif
1626
1627#ifdef FEAT_EVAL
1628 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001629 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001630#endif
1631
1632 if (TOLOWER_ASC(initstr[0]) == 'r')
1633 {
1634 restricted = TRUE;
1635 ++initstr;
1636 }
1637
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001638 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001639 if (TOLOWER_ASC(initstr[0]) == 'e'
1640 && (TOLOWER_ASC(initstr[1]) == 'v'
1641 || TOLOWER_ASC(initstr[1]) == 'g'))
1642 {
1643#ifdef FEAT_GUI
1644 gui.starting = TRUE;
1645#endif
1646 parmp->evim_mode = TRUE;
1647 ++initstr;
1648 }
1649
Bram Moolenaar806875d2008-09-18 18:57:10 +00001650 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1651 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001652 {
1653 main_start_gui();
1654#ifdef FEAT_GUI
1655 ++initstr;
1656#endif
1657 }
1658
1659 if (STRNICMP(initstr, "view", 4) == 0)
1660 {
1661 readonlymode = TRUE;
1662 curbuf->b_p_ro = TRUE;
1663 p_uc = 10000; /* don't update very often */
1664 initstr += 4;
1665 }
1666 else if (STRNICMP(initstr, "vim", 3) == 0)
1667 initstr += 3;
1668
1669 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1670 if (STRICMP(initstr, "diff") == 0)
1671 {
1672#ifdef FEAT_DIFF
1673 parmp->diff_mode = TRUE;
1674#else
1675 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1676 mch_errmsg("\n");
1677 mch_exit(2);
1678#endif
1679 }
1680
1681 if (STRNICMP(initstr, "ex", 2) == 0)
1682 {
1683 if (STRNICMP(initstr + 2, "im", 2) == 0)
1684 exmode_active = EXMODE_VIM;
1685 else
1686 exmode_active = EXMODE_NORMAL;
1687 change_compatible(TRUE); /* set 'compatible' */
1688 }
1689}
1690
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001691/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001692 * Get the name of the display, before gui_prepare() removes it from
1693 * argv[]. Used for the xterm-clipboard display.
1694 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001695 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001696 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001697 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001698early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001699 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001700{
Bram Moolenaar03005972008-11-20 13:12:36 +00001701#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1702 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001703 int argc = parmp->argc;
1704 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001705 int i;
1706
1707 for (i = 1; i < argc; i++)
1708 {
1709 if (STRCMP(argv[i], "--") == 0)
1710 break;
1711# ifdef FEAT_XCLIPBOARD
1712 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001713# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001714 || STRICMP(argv[i], "--display") == 0
1715# endif
1716 )
1717 {
1718 if (i == argc - 1)
1719 mainerr_arg_missing((char_u *)argv[i]);
1720 xterm_display = argv[++i];
1721 }
1722# endif
1723# ifdef FEAT_CLIENTSERVER
1724 else if (STRICMP(argv[i], "--servername") == 0)
1725 {
1726 if (i == argc - 1)
1727 mainerr_arg_missing((char_u *)argv[i]);
1728 parmp->serverName_arg = (char_u *)argv[++i];
1729 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001730 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001731 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001732 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001733 {
1734 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001735# ifdef FEAT_GUI
1736 if (strstr(argv[i], "-wait") != 0)
1737 /* don't fork() when starting the GUI to edit files ourself */
1738 gui.dofork = FALSE;
1739# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001740 }
1741# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001742
1743# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1744# ifdef FEAT_GUI_W32
1745 else if (STRICMP(argv[i], "--windowid") == 0)
1746# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001747 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001748# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001749 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001750 long_u id;
1751 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001752
1753 if (i == argc - 1)
1754 mainerr_arg_missing((char_u *)argv[i]);
1755 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001756 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001757 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001758 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001759 if (count != 1)
1760 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1761 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001762# ifdef FEAT_GUI_W32
1763 win_socket_id = id;
1764# else
1765 gtk_socket_id = id;
1766# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001767 i++;
1768 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001769# endif
1770# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001771 else if (STRICMP(argv[i], "--echo-wid") == 0)
1772 echo_wid_arg = TRUE;
1773# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001774# ifndef FEAT_NETBEANS_INTG
1775 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001776 {
1777 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1778 mch_exit(2);
1779 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001780# endif
1781
Bram Moolenaar58d98232005-07-23 22:25:46 +00001782 }
1783#endif
1784}
1785
1786/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001787 * Scan the command line arguments.
1788 */
1789 static void
1790command_line_scan(parmp)
1791 mparm_T *parmp;
1792{
1793 int argc = parmp->argc;
1794 char **argv = parmp->argv;
1795 int argv_idx; /* index in argv[n][] */
1796 int had_minmin = FALSE; /* found "--" argument */
1797 int want_argument; /* option argument with argument */
1798 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001799 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001800 long n;
1801
1802 --argc;
1803 ++argv;
1804 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1805 while (argc > 0)
1806 {
1807 /*
1808 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1809 */
1810 if (argv[0][0] == '+' && !had_minmin)
1811 {
1812 if (parmp->n_commands >= MAX_ARG_CMDS)
1813 mainerr(ME_EXTRA_CMD, NULL);
1814 argv_idx = -1; /* skip to next argument */
1815 if (argv[0][1] == NUL)
1816 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1817 else
1818 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1819 }
1820
1821 /*
1822 * Optional argument.
1823 */
1824 else if (argv[0][0] == '-' && !had_minmin)
1825 {
1826 want_argument = FALSE;
1827 c = argv[0][argv_idx++];
1828#ifdef VMS
1829 /*
1830 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1831 * and "-/X" as "-X".
1832 */
1833 if (c == '/')
1834 {
1835 c = argv[0][argv_idx++];
1836 c = TOUPPER_ASC(c);
1837 }
1838 else
1839 c = TOLOWER_ASC(c);
1840#endif
1841 switch (c)
1842 {
1843 case NUL: /* "vim -" read from stdin */
1844 /* "ex -" silent mode */
1845 if (exmode_active)
1846 silent_mode = TRUE;
1847 else
1848 {
1849 if (parmp->edit_type != EDIT_NONE)
1850 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1851 parmp->edit_type = EDIT_STDIN;
1852 read_cmd_fd = 2; /* read from stderr instead of stdin */
1853 }
1854 argv_idx = -1; /* skip to next argument */
1855 break;
1856
1857 case '-': /* "--" don't take any more option arguments */
1858 /* "--help" give help message */
1859 /* "--version" give version message */
1860 /* "--literal" take files literally */
1861 /* "--nofork" don't fork */
1862 /* "--noplugin[s]" skip plugins */
1863 /* "--cmd <cmd>" execute cmd before vimrc */
1864 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1865 usage();
1866 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1867 {
1868 Columns = 80; /* need to init Columns */
1869 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1870 list_version();
1871 msg_putchar('\n');
1872 msg_didout = FALSE;
1873 mch_exit(0);
1874 }
1875 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1876 {
1877#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1878 parmp->literal = TRUE;
1879#endif
1880 }
1881 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1882 {
1883#ifdef FEAT_GUI
1884 gui.dofork = FALSE; /* don't fork() when starting GUI */
1885#endif
1886 }
1887 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1888 p_lpl = FALSE;
1889 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1890 {
1891 want_argument = TRUE;
1892 argv_idx += 3;
1893 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001894 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1895 {
1896 want_argument = TRUE;
1897 argv_idx += 11;
1898 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001899#ifdef FEAT_CLIENTSERVER
1900 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1901 ; /* already processed -- no arg */
1902 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1903 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1904 {
1905 /* already processed -- snatch the following arg */
1906 if (argc > 1)
1907 {
1908 --argc;
1909 ++argv;
1910 }
1911 }
1912#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001913#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1914# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001915 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001916# else
1917 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1918# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001919 {
1920 /* already processed -- snatch the following arg */
1921 if (argc > 1)
1922 {
1923 --argc;
1924 ++argv;
1925 }
1926 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001927#endif
1928#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001929 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1930 {
1931 /* already processed, skip */
1932 }
1933#endif
1934 else
1935 {
1936 if (argv[0][argv_idx])
1937 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1938 had_minmin = TRUE;
1939 }
1940 if (!want_argument)
1941 argv_idx = -1; /* skip to next argument */
1942 break;
1943
1944 case 'A': /* "-A" start in Arabic mode */
1945#ifdef FEAT_ARABIC
1946 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1947#else
1948 mch_errmsg(_(e_noarabic));
1949 mch_exit(2);
1950#endif
1951 break;
1952
1953 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001954 /* Needs to be effective before expanding file names, because
1955 * for Win32 this makes us edit a shortcut file itself,
1956 * instead of the file it links to. */
1957 set_options_bin(curbuf->b_p_bin, 1, 0);
1958 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001959 break;
1960
1961 case 'C': /* "-C" Compatible */
1962 change_compatible(TRUE);
1963 break;
1964
1965 case 'e': /* "-e" Ex mode */
1966 exmode_active = EXMODE_NORMAL;
1967 break;
1968
1969 case 'E': /* "-E" Improved Ex mode */
1970 exmode_active = EXMODE_VIM;
1971 break;
1972
1973 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1974 window directly, not with newcli */
1975#ifdef FEAT_GUI
1976 gui.dofork = FALSE; /* don't fork() when starting GUI */
1977#endif
1978 break;
1979
1980 case 'g': /* "-g" start GUI */
1981 main_start_gui();
1982 break;
1983
1984 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1985#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001986 p_fkmap = TRUE;
1987 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001988#else
1989 mch_errmsg(_(e_nofarsi));
1990 mch_exit(2);
1991#endif
1992 break;
1993
1994 case 'h': /* "-h" give help message */
1995#ifdef FEAT_GUI_GNOME
1996 /* Tell usage() to exit for "gvim". */
1997 gui.starting = FALSE;
1998#endif
1999 usage();
2000 break;
2001
2002 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2003#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002004 p_hkmap = TRUE;
2005 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002006#else
2007 mch_errmsg(_(e_nohebrew));
2008 mch_exit(2);
2009#endif
2010 break;
2011
2012 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2013#ifdef FEAT_LISP
2014 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2015 p_sm = TRUE;
2016#endif
2017 break;
2018
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002019 case 'M': /* "-M" no changes or writing of files */
2020 reset_modifiable();
2021 /* FALLTHROUGH */
2022
2023 case 'm': /* "-m" no writing of files */
2024 p_write = FALSE;
2025 break;
2026
2027 case 'y': /* "-y" easy mode */
2028#ifdef FEAT_GUI
2029 gui.starting = TRUE; /* start GUI a bit later */
2030#endif
2031 parmp->evim_mode = TRUE;
2032 break;
2033
2034 case 'N': /* "-N" Nocompatible */
2035 change_compatible(FALSE);
2036 break;
2037
2038 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002039#ifdef FEAT_NETBEANS_INTG
2040 /* checking for "-nb", netbeans parameters */
2041 if (argv[0][argv_idx] == 'b')
2042 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002043 netbeansArg = argv[0];
2044 argv_idx = -1; /* skip to next argument */
2045 }
2046 else
2047#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002048 parmp->no_swap_file = TRUE;
2049 break;
2050
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002051 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002052#ifdef TARGET_API_MAC_OSX
2053 /* For some reason on MacOS X, an argument like:
2054 -psn_0_10223617 is passed in when invoke from Finder
2055 or with the 'open' command */
2056 if (argv[0][argv_idx] == 's')
2057 {
2058 argv_idx = -1; /* bypass full -psn */
2059 main_start_gui();
2060 break;
2061 }
2062#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002063#ifdef FEAT_WINDOWS
2064 /* default is 0: open window for each file */
2065 parmp->window_count = get_number_arg((char_u *)argv[0],
2066 &argv_idx, 0);
2067 parmp->window_layout = WIN_TABS;
2068#endif
2069 break;
2070
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002071 case 'o': /* "-o[N]" open N horizontal split windows */
2072#ifdef FEAT_WINDOWS
2073 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002074 parmp->window_count = get_number_arg((char_u *)argv[0],
2075 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002076 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002077#endif
2078 break;
2079
2080 case 'O': /* "-O[N]" open N vertical split windows */
2081#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2082 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002083 parmp->window_count = get_number_arg((char_u *)argv[0],
2084 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002085 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002086#endif
2087 break;
2088
2089#ifdef FEAT_QUICKFIX
2090 case 'q': /* "-q" QuickFix mode */
2091 if (parmp->edit_type != EDIT_NONE)
2092 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2093 parmp->edit_type = EDIT_QF;
2094 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2095 {
2096 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2097 argv_idx = -1;
2098 }
2099 else if (argc > 1) /* "-q {errorfile}" */
2100 want_argument = TRUE;
2101 break;
2102#endif
2103
2104 case 'R': /* "-R" readonly mode */
2105 readonlymode = TRUE;
2106 curbuf->b_p_ro = TRUE;
2107 p_uc = 10000; /* don't update very often */
2108 break;
2109
2110 case 'r': /* "-r" recovery mode */
2111 case 'L': /* "-L" recovery mode */
2112 recoverymode = 1;
2113 break;
2114
2115 case 's':
2116 if (exmode_active) /* "-s" silent (batch) mode */
2117 silent_mode = TRUE;
2118 else /* "-s {scriptin}" read from script file */
2119 want_argument = TRUE;
2120 break;
2121
2122 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2123 if (parmp->edit_type != EDIT_NONE)
2124 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2125 parmp->edit_type = EDIT_TAG;
2126 if (argv[0][argv_idx]) /* "-t{tag}" */
2127 {
2128 parmp->tagname = (char_u *)argv[0] + argv_idx;
2129 argv_idx = -1;
2130 }
2131 else /* "-t {tag}" */
2132 want_argument = TRUE;
2133 break;
2134
2135#ifdef FEAT_EVAL
2136 case 'D': /* "-D" Debugging */
2137 parmp->use_debug_break_level = 9999;
2138 break;
2139#endif
2140#ifdef FEAT_DIFF
2141 case 'd': /* "-d" 'diff' */
2142# ifdef AMIGA
2143 /* check for "-dev {device}" */
2144 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2145 want_argument = TRUE;
2146 else
2147# endif
2148 parmp->diff_mode = TRUE;
2149 break;
2150#endif
2151 case 'V': /* "-V{N}" Verbose level */
2152 /* default is 10: a little bit verbose */
2153 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2154 if (argv[0][argv_idx] != NUL)
2155 {
2156 set_option_value((char_u *)"verbosefile", 0L,
2157 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002158 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002159 }
2160 break;
2161
2162 case 'v': /* "-v" Vi-mode (as if called "vi") */
2163 exmode_active = 0;
2164#ifdef FEAT_GUI
2165 gui.starting = FALSE; /* don't start GUI */
2166#endif
2167 break;
2168
2169 case 'w': /* "-w{number}" set window height */
2170 /* "-w {scriptout}" write to script */
2171 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2172 {
2173 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2174 set_option_value((char_u *)"window", n, NULL, 0);
2175 break;
2176 }
2177 want_argument = TRUE;
2178 break;
2179
2180#ifdef FEAT_CRYPT
2181 case 'x': /* "-x" encrypted reading/writing of files */
2182 parmp->ask_for_key = TRUE;
2183 break;
2184#endif
2185
2186 case 'X': /* "-X" don't connect to X server */
2187#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2188 x_no_connect = TRUE;
2189#endif
2190 break;
2191
2192 case 'Z': /* "-Z" restricted mode */
2193 restricted = TRUE;
2194 break;
2195
2196 case 'c': /* "-c{command}" or "-c {command}" execute
2197 command */
2198 if (argv[0][argv_idx] != NUL)
2199 {
2200 if (parmp->n_commands >= MAX_ARG_CMDS)
2201 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002202 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2203 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002204 argv_idx = -1;
2205 break;
2206 }
2207 /*FALLTHROUGH*/
2208 case 'S': /* "-S {file}" execute Vim script */
2209 case 'i': /* "-i {viminfo}" use for viminfo */
2210#ifndef FEAT_DIFF
2211 case 'd': /* "-d {device}" device (for Amiga) */
2212#endif
2213 case 'T': /* "-T {terminal}" terminal name */
2214 case 'u': /* "-u {vimrc}" vim inits file */
2215 case 'U': /* "-U {gvimrc}" gvim inits file */
2216 case 'W': /* "-W {scriptout}" overwrite */
2217#ifdef FEAT_GUI_W32
2218 case 'P': /* "-P {parent title}" MDI parent */
2219#endif
2220 want_argument = TRUE;
2221 break;
2222
2223 default:
2224 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2225 }
2226
2227 /*
2228 * Handle option arguments with argument.
2229 */
2230 if (want_argument)
2231 {
2232 /*
2233 * Check for garbage immediately after the option letter.
2234 */
2235 if (argv[0][argv_idx] != NUL)
2236 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2237
2238 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002239 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002240 mainerr_arg_missing((char_u *)argv[0]);
2241 ++argv;
2242 argv_idx = -1;
2243
2244 switch (c)
2245 {
2246 case 'c': /* "-c {command}" execute command */
2247 case 'S': /* "-S {file}" execute Vim script */
2248 if (parmp->n_commands >= MAX_ARG_CMDS)
2249 mainerr(ME_EXTRA_CMD, NULL);
2250 if (c == 'S')
2251 {
2252 char *a;
2253
2254 if (argc < 1)
2255 /* "-S" without argument: use default session file
2256 * name. */
2257 a = SESSION_FILE;
2258 else if (argv[0][0] == '-')
2259 {
2260 /* "-S" followed by another option: use default
2261 * session file name. */
2262 a = SESSION_FILE;
2263 ++argc;
2264 --argv;
2265 }
2266 else
2267 a = argv[0];
2268 p = alloc((unsigned)(STRLEN(a) + 4));
2269 if (p == NULL)
2270 mch_exit(2);
2271 sprintf((char *)p, "so %s", a);
2272 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2273 parmp->commands[parmp->n_commands++] = p;
2274 }
2275 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002276 parmp->commands[parmp->n_commands++] =
2277 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002278 break;
2279
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002280 case '-':
2281 if (argv[-1][2] == 'c')
2282 {
2283 /* "--cmd {command}" execute command */
2284 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2285 mainerr(ME_EXTRA_CMD, NULL);
2286 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002287 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002288 }
2289 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002290 break;
2291
2292 /* case 'd': -d {device} is handled in mch_check_win() for the
2293 * Amiga */
2294
2295#ifdef FEAT_QUICKFIX
2296 case 'q': /* "-q {errorfile}" QuickFix mode */
2297 parmp->use_ef = (char_u *)argv[0];
2298 break;
2299#endif
2300
2301 case 'i': /* "-i {viminfo}" use for viminfo */
2302 use_viminfo = (char_u *)argv[0];
2303 break;
2304
2305 case 's': /* "-s {scriptin}" read from script file */
2306 if (scriptin[0] != NULL)
2307 {
2308scripterror:
2309 mch_errmsg(_("Attempt to open script file again: \""));
2310 mch_errmsg(argv[-1]);
2311 mch_errmsg(" ");
2312 mch_errmsg(argv[0]);
2313 mch_errmsg("\"\n");
2314 mch_exit(2);
2315 }
2316 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2317 {
2318 mch_errmsg(_("Cannot open for reading: \""));
2319 mch_errmsg(argv[0]);
2320 mch_errmsg("\"\n");
2321 mch_exit(2);
2322 }
2323 if (save_typebuf() == FAIL)
2324 mch_exit(2); /* out of memory */
2325 break;
2326
2327 case 't': /* "-t {tag}" */
2328 parmp->tagname = (char_u *)argv[0];
2329 break;
2330
2331 case 'T': /* "-T {terminal}" terminal name */
2332 /*
2333 * The -T term argument is always available and when
2334 * HAVE_TERMLIB is supported it overrides the environment
2335 * variable TERM.
2336 */
2337#ifdef FEAT_GUI
2338 if (term_is_gui((char_u *)argv[0]))
2339 gui.starting = TRUE; /* start GUI a bit later */
2340 else
2341#endif
2342 parmp->term = (char_u *)argv[0];
2343 break;
2344
2345 case 'u': /* "-u {vimrc}" vim inits file */
2346 parmp->use_vimrc = (char_u *)argv[0];
2347 break;
2348
2349 case 'U': /* "-U {gvimrc}" gvim inits file */
2350#ifdef FEAT_GUI
2351 use_gvimrc = (char_u *)argv[0];
2352#endif
2353 break;
2354
2355 case 'w': /* "-w {nr}" 'window' value */
2356 /* "-w {scriptout}" append to script file */
2357 if (vim_isdigit(*((char_u *)argv[0])))
2358 {
2359 argv_idx = 0;
2360 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2361 set_option_value((char_u *)"window", n, NULL, 0);
2362 argv_idx = -1;
2363 break;
2364 }
2365 /*FALLTHROUGH*/
2366 case 'W': /* "-W {scriptout}" overwrite script file */
2367 if (scriptout != NULL)
2368 goto scripterror;
2369 if ((scriptout = mch_fopen(argv[0],
2370 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2371 {
2372 mch_errmsg(_("Cannot open for script output: \""));
2373 mch_errmsg(argv[0]);
2374 mch_errmsg("\"\n");
2375 mch_exit(2);
2376 }
2377 break;
2378
2379#ifdef FEAT_GUI_W32
2380 case 'P': /* "-P {parent title}" MDI parent */
2381 gui_mch_set_parent(argv[0]);
2382 break;
2383#endif
2384 }
2385 }
2386 }
2387
2388 /*
2389 * File name argument.
2390 */
2391 else
2392 {
2393 argv_idx = -1; /* skip to next argument */
2394
2395 /* Check for only one type of editing. */
2396 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2397 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2398 parmp->edit_type = EDIT_FILE;
2399
2400#ifdef MSWIN
2401 /* Remember if the argument was a full path before changing
2402 * slashes to backslashes. */
2403 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2404 parmp->full_path = TRUE;
2405#endif
2406
2407 /* Add the file to the global argument list. */
2408 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2409 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2410 mch_exit(2);
2411#ifdef FEAT_DIFF
2412 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2413 && !mch_isdir(alist_name(&GARGLIST[0])))
2414 {
2415 char_u *r;
2416
2417 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2418 if (r != NULL)
2419 {
2420 vim_free(p);
2421 p = r;
2422 }
2423 }
2424#endif
2425#if defined(__CYGWIN32__) && !defined(WIN32)
2426 /*
2427 * If vim is invoked by non-Cygwin tools, convert away any
2428 * DOS paths, so things like .swp files are created correctly.
2429 * Look for evidence of non-Cygwin paths before we bother.
2430 * This is only for when using the Unix files.
2431 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002432 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002433 {
2434 char posix_path[PATH_MAX];
2435
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002436# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2437 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2438# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002440# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002441 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002442 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443 if (p == NULL)
2444 mch_exit(2);
2445 }
2446#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002447
2448#ifdef USE_FNAME_CASE
2449 /* Make the case of the file name match the actual file. */
2450 fname_case(p, 0);
2451#endif
2452
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002453 alist_add(&global_alist, p,
2454#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002455 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456#else
2457 2 /* add buffer number now and use curbuf */
2458#endif
2459 );
2460
2461#if defined(FEAT_MBYTE) && defined(WIN32)
2462 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002463 /* Remember this argument has been added to the argument list.
2464 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002465 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002466# ifdef FEAT_DIFF
2467 parmp->diff_mode
2468# else
2469 FALSE
2470# endif
2471 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002472 }
2473#endif
2474 }
2475
2476 /*
2477 * If there are no more letters after the current "-", go to next
2478 * argument. argv_idx is set to -1 when the current argument is to be
2479 * skipped.
2480 */
2481 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2482 {
2483 --argc;
2484 ++argv;
2485 argv_idx = 1;
2486 }
2487 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002488
2489#ifdef FEAT_EVAL
2490 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2491 * one. */
2492 if (parmp->n_commands > 0)
2493 {
2494 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2495 if (p != NULL)
2496 {
2497 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2498 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2499 vim_free(p);
2500 }
2501 }
2502#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002503}
2504
2505/*
2506 * Print a warning if stdout is not a terminal.
2507 * When starting in Ex mode and commands come from a file, set Silent mode.
2508 */
2509 static void
2510check_tty(parmp)
2511 mparm_T *parmp;
2512{
2513 int input_isatty; /* is active input a terminal? */
2514
2515 input_isatty = mch_input_isatty();
2516 if (exmode_active)
2517 {
2518 if (!input_isatty)
2519 silent_mode = TRUE;
2520 }
2521 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2522#ifdef FEAT_GUI
2523 /* don't want the delay when started from the desktop */
2524 && !gui.starting
2525#endif
2526 )
2527 {
2528#ifdef NBDEBUG
2529 /*
2530 * This shouldn't be necessary. But if I run netbeans with the log
2531 * output coming to the console and XOpenDisplay fails, I get vim
2532 * trying to start with input/output to my console tty. This fills my
2533 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002534 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002535 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002536 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002537 {
2538 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2539 exit(1);
2540 }
2541#endif
2542 if (!parmp->stdout_isatty)
2543 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2544 if (!input_isatty)
2545 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2546 out_flush();
2547 if (scriptin[0] == NULL)
2548 ui_delay(2000L, TRUE);
2549 TIME_MSG("Warning delay");
2550 }
2551}
2552
2553/*
2554 * Read text from stdin.
2555 */
2556 static void
2557read_stdin()
2558{
2559 int i;
2560
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002561#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562 /* When getting the ATTENTION prompt here, use a dialog */
2563 swap_exists_action = SEA_DIALOG;
2564#endif
2565 no_wait_return = TRUE;
2566 i = msg_didany;
2567 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002568 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002569 no_wait_return = FALSE;
2570 msg_didany = i;
2571 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002572#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573 check_swap_exists_action();
2574#endif
2575#if !(defined(AMIGA) || defined(MACOS))
2576 /*
2577 * Close stdin and dup it from stderr. Required for GPM to work
2578 * properly, and for running external commands.
2579 * Is there any other system that cannot do this?
2580 */
2581 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002582 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002583#endif
2584}
2585
2586/*
2587 * Create the requested number of windows and edit buffers in them.
2588 * Also does recovery if "recoverymode" set.
2589 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002590 static void
2591create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002592 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002593{
2594#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002595 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002596 int done = 0;
2597
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002598 /*
2599 * Create the number of windows that was requested.
2600 */
2601 if (parmp->window_count == -1) /* was not set */
2602 parmp->window_count = 1;
2603 if (parmp->window_count == 0)
2604 parmp->window_count = GARGCOUNT;
2605 if (parmp->window_count > 1)
2606 {
2607 /* Don't change the windows if there was a command in .vimrc that
2608 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002609 if (parmp->window_layout == 0)
2610 parmp->window_layout = WIN_HOR;
2611 if (parmp->window_layout == WIN_TABS)
2612 {
2613 parmp->window_count = make_tabpages(parmp->window_count);
2614 TIME_MSG("making tab pages");
2615 }
2616 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002617 {
2618 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002619 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002620 TIME_MSG("making windows");
2621 }
2622 else
2623 parmp->window_count = win_count();
2624 }
2625 else
2626 parmp->window_count = 1;
2627#endif
2628
2629 if (recoverymode) /* do recover */
2630 {
2631 msg_scroll = TRUE; /* scroll message up */
2632 ml_recover();
2633 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2634 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002635 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002636 }
2637 else
2638 {
2639 /*
2640 * Open a buffer for windows that don't have one yet.
2641 * Commands in the .vimrc might have loaded a file or split the window.
2642 * Watch out for autocommands that delete a window.
2643 */
2644#ifdef FEAT_AUTOCMD
2645 /*
2646 * Don't execute Win/Buf Enter/Leave autocommands here
2647 */
2648 ++autocmd_no_enter;
2649 ++autocmd_no_leave;
2650#endif
2651#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002652 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002653 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002654 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002655 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002656 {
2657 if (parmp->window_layout == WIN_TABS)
2658 goto_tabpage(1);
2659 else
2660 curwin = firstwin;
2661 }
2662 else if (parmp->window_layout == WIN_TABS)
2663 {
2664 if (curtab->tp_next == NULL)
2665 break;
2666 goto_tabpage(0);
2667 }
2668 else
2669 {
2670 if (curwin->w_next == NULL)
2671 break;
2672 curwin = curwin->w_next;
2673 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002674 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002675#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002676 curbuf = curwin->w_buffer;
2677 if (curbuf->b_ml.ml_mfp == NULL)
2678 {
2679#ifdef FEAT_FOLDING
2680 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2681 if (p_fdls >= 0)
2682 curwin->w_p_fdl = p_fdls;
2683#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002684#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002685 /* When getting the ATTENTION prompt here, use a dialog */
2686 swap_exists_action = SEA_DIALOG;
2687#endif
2688 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002689
2690 /* create memfile, read file */
2691 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002692
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002693#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002694 if (swap_exists_action == SEA_QUIT)
2695 {
2696 if (got_int || only_one_window())
2697 {
2698 /* abort selected or quit and only one window */
2699 did_emsg = FALSE; /* avoid hit-enter prompt */
2700 getout(1);
2701 }
2702 /* We can't close the window, it would disturb what
2703 * happens next. Clear the file name and set the arg
2704 * index to -1 to delete it later. */
2705 setfname(curbuf, NULL, NULL, FALSE);
2706 curwin->w_arg_idx = -1;
2707 swap_exists_action = SEA_NONE;
2708 }
2709 else
2710 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002711#endif
2712#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002713 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002714#endif
2715 }
2716#ifdef FEAT_WINDOWS
2717 ui_breakcheck();
2718 if (got_int)
2719 {
2720 (void)vgetc(); /* only break the file loading, not the rest */
2721 break;
2722 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002723 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002724#endif
2725#ifdef FEAT_WINDOWS
2726 if (parmp->window_layout == WIN_TABS)
2727 goto_tabpage(1);
2728 else
2729 curwin = firstwin;
2730 curbuf = curwin->w_buffer;
2731#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002732#ifdef FEAT_AUTOCMD
2733 --autocmd_no_enter;
2734 --autocmd_no_leave;
2735#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002736 }
2737}
2738
2739#ifdef FEAT_WINDOWS
2740 /*
2741 * If opened more than one window, start editing files in the other
2742 * windows. make_windows() has already opened the windows.
2743 */
2744 static void
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002745edit_buffers(parmp, cwd)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002746 mparm_T *parmp;
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002747 char_u *cwd; /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002748{
2749 int arg_idx; /* index in argument list */
2750 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002751 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002752 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002753
2754# ifdef FEAT_AUTOCMD
2755 /*
2756 * Don't execute Win/Buf Enter/Leave autocommands here
2757 */
2758 ++autocmd_no_enter;
2759 ++autocmd_no_leave;
2760# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002761
2762 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2763 if (curwin->w_arg_idx == -1)
2764 {
2765 win_close(curwin, TRUE);
2766 advance = FALSE;
2767 }
2768
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002769 arg_idx = 1;
2770 for (i = 1; i < parmp->window_count; ++i)
2771 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002772 if (cwd != NULL)
2773 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002774 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2775 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002776 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002777 ++arg_idx;
2778 win_close(curwin, TRUE);
2779 advance = FALSE;
2780 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002781 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002782
Bram Moolenaar84212822006-11-07 21:59:47 +00002783 if (advance)
2784 {
2785 if (parmp->window_layout == WIN_TABS)
2786 {
2787 if (curtab->tp_next == NULL) /* just checking */
2788 break;
2789 goto_tabpage(0);
2790 }
2791 else
2792 {
2793 if (curwin->w_next == NULL) /* just checking */
2794 break;
2795 win_enter(curwin->w_next, FALSE);
2796 }
2797 }
2798 advance = TRUE;
2799
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002800 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002801 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002802 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2803 {
2804 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002805 /* Edit file from arg list, if there is one. When "Quit" selected
2806 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002807# ifdef HAS_SWAP_EXISTS_ACTION
2808 swap_exists_did_quit = FALSE;
2809# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810 (void)do_ecmd(0, arg_idx < GARGCOUNT
2811 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002812 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002813# ifdef HAS_SWAP_EXISTS_ACTION
2814 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002815 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002816 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002817 if (got_int || only_one_window())
2818 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002819 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002820 did_emsg = FALSE; /* avoid hit-enter prompt */
2821 getout(1);
2822 }
2823 win_close(curwin, TRUE);
2824 advance = FALSE;
2825 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002826# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002827 if (arg_idx == GARGCOUNT - 1)
2828 arg_had_last = TRUE;
2829 ++arg_idx;
2830 }
2831 ui_breakcheck();
2832 if (got_int)
2833 {
2834 (void)vgetc(); /* only break the file loading, not the rest */
2835 break;
2836 }
2837 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002838
2839 if (parmp->window_layout == WIN_TABS)
2840 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002841# ifdef FEAT_AUTOCMD
2842 --autocmd_no_enter;
2843# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002844
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002845 /* make the first window the current window */
2846 win = firstwin;
2847#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2848 /* Avoid making a preview window the current window. */
2849 while (win->w_p_pvw)
2850 {
2851 win = win->w_next;
2852 if (win == NULL)
2853 {
2854 win = firstwin;
2855 break;
2856 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002857 }
2858#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002859 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002860
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002861# ifdef FEAT_AUTOCMD
2862 --autocmd_no_leave;
2863# endif
2864 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002865 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002866 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2867}
2868#endif /* FEAT_WINDOWS */
2869
2870/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002871 * Execute the commands from --cmd arguments "cmds[cnt]".
2872 */
2873 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002874exe_pre_commands(parmp)
2875 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002876{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002877 char_u **cmds = parmp->pre_commands;
2878 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002879 int i;
2880
2881 if (cnt > 0)
2882 {
2883 curwin->w_cursor.lnum = 0; /* just in case.. */
2884 sourcing_name = (char_u *)_("pre-vimrc command line");
2885# ifdef FEAT_EVAL
2886 current_SID = SID_CMDARG;
2887# endif
2888 for (i = 0; i < cnt; ++i)
2889 do_cmdline_cmd(cmds[i]);
2890 sourcing_name = NULL;
2891# ifdef FEAT_EVAL
2892 current_SID = 0;
2893# endif
2894 TIME_MSG("--cmd commands");
2895 }
2896}
2897
2898/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002899 * Execute "+", "-c" and "-S" arguments.
2900 */
2901 static void
2902exe_commands(parmp)
2903 mparm_T *parmp;
2904{
2905 int i;
2906
2907 /*
2908 * We start commands on line 0, make "vim +/pat file" match a
2909 * pattern on line 1. But don't move the cursor when an autocommand
2910 * with g`" was used.
2911 */
2912 msg_scroll = TRUE;
2913 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2914 curwin->w_cursor.lnum = 0;
2915 sourcing_name = (char_u *)"command line";
2916#ifdef FEAT_EVAL
2917 current_SID = SID_CARG;
2918#endif
2919 for (i = 0; i < parmp->n_commands; ++i)
2920 {
2921 do_cmdline_cmd(parmp->commands[i]);
2922 if (parmp->cmds_tofree[i])
2923 vim_free(parmp->commands[i]);
2924 }
2925 sourcing_name = NULL;
2926#ifdef FEAT_EVAL
2927 current_SID = 0;
2928#endif
2929 if (curwin->w_cursor.lnum == 0)
2930 curwin->w_cursor.lnum = 1;
2931
2932 if (!exmode_active)
2933 msg_scroll = FALSE;
2934
2935#ifdef FEAT_QUICKFIX
2936 /* When started with "-q errorfile" jump to first error again. */
2937 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002938 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002939#endif
2940 TIME_MSG("executing command arguments");
2941}
2942
2943/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002944 * Source startup scripts.
2945 */
2946 static void
2947source_startup_scripts(parmp)
2948 mparm_T *parmp;
2949{
2950 int i;
2951
2952 /*
2953 * For "evim" source evim.vim first of all, so that the user can overrule
2954 * any things he doesn't like.
2955 */
2956 if (parmp->evim_mode)
2957 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002958 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002959 TIME_MSG("source evim file");
2960 }
2961
2962 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002963 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002964 * nothing else.
2965 */
2966 if (parmp->use_vimrc != NULL)
2967 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002968 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2969 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002970 {
2971#ifdef FEAT_GUI
2972 if (use_gvimrc == NULL) /* don't load gvimrc either */
2973 use_gvimrc = parmp->use_vimrc;
2974#endif
2975 if (parmp->use_vimrc[2] == 'N')
2976 p_lpl = FALSE; /* don't load plugins either */
2977 }
2978 else
2979 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002980 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002981 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2982 }
2983 }
2984 else if (!silent_mode)
2985 {
2986#ifdef AMIGA
2987 struct Process *proc = (struct Process *)FindTask(0L);
2988 APTR save_winptr = proc->pr_WindowPtr;
2989
2990 /* Avoid a requester here for a volume that doesn't exist. */
2991 proc->pr_WindowPtr = (APTR)-1L;
2992#endif
2993
2994 /*
2995 * Get system wide defaults, if the file name is defined.
2996 */
2997#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002998 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002999#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003000#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003001 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003002#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003
3004 /*
3005 * Try to read initialization commands from the following places:
3006 * - environment variable VIMINIT
3007 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3008 * - second user vimrc file ($VIM/.vimrc for Dos)
3009 * - environment variable EXINIT
3010 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3011 * - second user exrc file ($VIM/.exrc for Dos)
3012 * The first that exists is used, the rest is ignored.
3013 */
3014 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3015 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003016 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003017#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003018 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3019 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003020#endif
3021#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003022 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3023 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003024#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003025#ifdef USR_VIMRC_FILE4
3026 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3027 DOSO_VIMRC) == FAIL
3028#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003030 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003031 {
3032#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003033 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003034#endif
3035 }
3036 }
3037
3038 /*
3039 * Read initialization commands from ".vimrc" or ".exrc" in current
3040 * directory. This is only done if the 'exrc' option is set.
3041 * Because of security reasons we disallow shell and write commands
3042 * now, except for unix if the file is owned by the user or 'secure'
3043 * option has been reset in environment of global ".exrc" or ".vimrc".
3044 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3045 * SYS_VIMRC_FILE.
3046 */
3047 if (p_exrc)
3048 {
3049#if defined(UNIX) || defined(VMS)
3050 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3051 if (!file_owned(VIMRC_FILE))
3052#endif
3053 secure = p_secure;
3054
3055 i = FAIL;
3056 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3057 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3058#ifdef USR_VIMRC_FILE2
3059 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3060 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3061#endif
3062#ifdef USR_VIMRC_FILE3
3063 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3064 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3065#endif
3066#ifdef SYS_VIMRC_FILE
3067 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3068 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3069#endif
3070 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003071 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003072
3073 if (i == FAIL)
3074 {
3075#if defined(UNIX) || defined(VMS)
3076 /* if ".exrc" is not owned by user set 'secure' mode */
3077 if (!file_owned(EXRC_FILE))
3078 secure = p_secure;
3079 else
3080 secure = 0;
3081#endif
3082 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3083 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3084#ifdef USR_EXRC_FILE2
3085 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3086 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3087#endif
3088 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003089 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003090 }
3091 }
3092 if (secure == 2)
3093 need_wait_return = TRUE;
3094 secure = 0;
3095#ifdef AMIGA
3096 proc->pr_WindowPtr = save_winptr;
3097#endif
3098 }
3099 TIME_MSG("sourcing vimrc file(s)");
3100}
3101
3102/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003103 * Setup to start using the GUI. Exit with an error when not available.
3104 */
3105 static void
3106main_start_gui()
3107{
3108#ifdef FEAT_GUI
3109 gui.starting = TRUE; /* start GUI a bit later */
3110#else
3111 mch_errmsg(_(e_nogvim));
3112 mch_errmsg("\n");
3113 mch_exit(2);
3114#endif
3115}
3116
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003117#endif /* NO_VIM_MAIN */
3118
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003119/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003120 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003121 * Returns FAIL if the environment variable was not executed, OK otherwise.
3122 */
3123 int
3124process_env(env, is_viminit)
3125 char_u *env;
3126 int is_viminit; /* when TRUE, called for VIMINIT */
3127{
3128 char_u *initstr;
3129 char_u *save_sourcing_name;
3130 linenr_T save_sourcing_lnum;
3131#ifdef FEAT_EVAL
3132 scid_T save_sid;
3133#endif
3134
3135 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3136 {
3137 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003138 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003139 save_sourcing_name = sourcing_name;
3140 save_sourcing_lnum = sourcing_lnum;
3141 sourcing_name = env;
3142 sourcing_lnum = 0;
3143#ifdef FEAT_EVAL
3144 save_sid = current_SID;
3145 current_SID = SID_ENV;
3146#endif
3147 do_cmdline_cmd(initstr);
3148 sourcing_name = save_sourcing_name;
3149 sourcing_lnum = save_sourcing_lnum;
3150#ifdef FEAT_EVAL
3151 current_SID = save_sid;;
3152#endif
3153 return OK;
3154 }
3155 return FAIL;
3156}
3157
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003158#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003159/*
3160 * Return TRUE if we are certain the user owns the file "fname".
3161 * Used for ".vimrc" and ".exrc".
3162 * Use both stat() and lstat() for extra security.
3163 */
3164 static int
3165file_owned(fname)
3166 char *fname;
3167{
3168 struct stat s;
3169# ifdef UNIX
3170 uid_t uid = getuid();
3171# else /* VMS */
3172 uid_t uid = ((getgid() << 16) | getuid());
3173# endif
3174
3175 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3176# ifdef HAVE_LSTAT
3177 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3178# endif
3179 );
3180}
3181#endif
3182
3183/*
3184 * Give an error message main_errors["n"] and exit.
3185 */
3186 static void
3187mainerr(n, str)
3188 int n; /* one of the ME_ defines */
3189 char_u *str; /* extra argument or NULL */
3190{
3191#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3192 reset_signals(); /* kill us with CTRL-C here, if you like */
3193#endif
3194
3195 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003196 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003197 mch_errmsg(_(main_errors[n]));
3198 if (str != NULL)
3199 {
3200 mch_errmsg(": \"");
3201 mch_errmsg((char *)str);
3202 mch_errmsg("\"");
3203 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003204 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003205
3206 mch_exit(1);
3207}
3208
3209 void
3210mainerr_arg_missing(str)
3211 char_u *str;
3212{
3213 mainerr(ME_ARG_MISSING, str);
3214}
3215
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003216#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003217/*
3218 * print a message with three spaces prepended and '\n' appended.
3219 */
3220 static void
3221main_msg(s)
3222 char *s;
3223{
3224 mch_msg(" ");
3225 mch_msg(s);
3226 mch_msg("\n");
3227}
3228
3229/*
3230 * Print messages for "vim -h" or "vim --help" and exit.
3231 */
3232 static void
3233usage()
3234{
3235 int i;
3236 static char *(use[]) =
3237 {
3238 N_("[file ..] edit specified file(s)"),
3239 N_("- read text from stdin"),
3240 N_("-t tag edit file where tag is defined"),
3241#ifdef FEAT_QUICKFIX
3242 N_("-q [errorfile] edit file with first error")
3243#endif
3244 };
3245
3246#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3247 reset_signals(); /* kill us with CTRL-C here, if you like */
3248#endif
3249
3250 mch_msg(longVersion);
3251 mch_msg(_("\n\nusage:"));
3252 for (i = 0; ; ++i)
3253 {
3254 mch_msg(_(" vim [arguments] "));
3255 mch_msg(_(use[i]));
3256 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3257 break;
3258 mch_msg(_("\n or:"));
3259 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003260#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003261 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003262#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003263
3264 mch_msg(_("\n\nArguments:\n"));
3265 main_msg(_("--\t\t\tOnly file names after this"));
3266#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3267 main_msg(_("--literal\t\tDon't expand wildcards"));
3268#endif
3269#ifdef FEAT_OLE
3270 main_msg(_("-register\t\tRegister this gvim for OLE"));
3271 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3272#endif
3273#ifdef FEAT_GUI
3274 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3275 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3276#endif
3277 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3278 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003279 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003280 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3281#ifdef FEAT_DIFF
3282 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3283#endif
3284 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3285 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3286 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3287 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3288 main_msg(_("-M\t\t\tModifications in text not allowed"));
3289 main_msg(_("-b\t\t\tBinary mode"));
3290#ifdef FEAT_LISP
3291 main_msg(_("-l\t\t\tLisp mode"));
3292#endif
3293 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3294 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003295 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3296#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003297 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003298#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003299 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3300 main_msg(_("-r\t\t\tList swap files and exit"));
3301 main_msg(_("-r (with file name)\tRecover crashed session"));
3302 main_msg(_("-L\t\t\tSame as -r"));
3303#ifdef AMIGA
3304 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3305 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3306#endif
3307#ifdef FEAT_ARABIC
3308 main_msg(_("-A\t\t\tstart in Arabic mode"));
3309#endif
3310#ifdef FEAT_RIGHTLEFT
3311 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3312#endif
3313#ifdef FEAT_FKMAP
3314 main_msg(_("-F\t\t\tStart in Farsi mode"));
3315#endif
3316 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3317 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3318#ifdef FEAT_GUI
3319 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3320#endif
3321 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003322#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003323 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003324 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3325 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003326#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003327 main_msg(_("+\t\t\tStart at end of file"));
3328 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003329 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3331 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3332 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3333 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3334 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3335#ifdef FEAT_CRYPT
3336 main_msg(_("-x\t\t\tEdit encrypted files"));
3337#endif
3338#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3339# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3340 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3341# endif
3342 main_msg(_("-X\t\t\tDo not connect to X server"));
3343#endif
3344#ifdef FEAT_CLIENTSERVER
3345 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3346 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3347 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3348 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003349# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003350 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003351# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3353 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3354 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3355 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3356#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003357#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003358 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003359#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003360#ifdef FEAT_VIMINFO
3361 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3362#endif
3363 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3364 main_msg(_("--version\t\tPrint version information and exit"));
3365
3366#ifdef FEAT_GUI_X11
3367# ifdef FEAT_GUI_MOTIF
3368 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3369# else
3370# ifdef FEAT_GUI_ATHENA
3371# ifdef FEAT_GUI_NEXTAW
3372 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3373# else
3374 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3375# endif
3376# endif
3377# endif
3378 main_msg(_("-display <display>\tRun vim on <display>"));
3379 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003380 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3381 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3382 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3383 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3384 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3385 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3386 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3387 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3388# ifdef FEAT_GUI_ATHENA
3389 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3390# endif
3391 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3392 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3393 main_msg(_("-xrm <resource>\tSet the specified resource"));
3394#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003395#ifdef FEAT_GUI_GTK
3396 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3397 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3398 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3399 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3400 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003401 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003403 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405#ifdef FEAT_GUI_W32
3406 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003407 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408#endif
3409
3410#ifdef FEAT_GUI_GNOME
3411 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3412 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003413 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003414 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003415 gui.dofork = FALSE;
3416 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003417 else
3418#endif
3419 mch_exit(0);
3420}
3421
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003422#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003423/*
3424 * Check the result of the ATTENTION dialog:
3425 * When "Quit" selected, exit Vim.
3426 * When "Recover" selected, recover the file.
3427 */
3428 static void
3429check_swap_exists_action()
3430{
3431 if (swap_exists_action == SEA_QUIT)
3432 getout(1);
3433 handle_swap_exists(NULL);
3434}
3435#endif
3436
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003437#endif
3438
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439#if defined(STARTUPTIME) || defined(PROTO)
3440static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3441
3442static struct timeval prev_timeval;
3443
Bram Moolenaar3f269672009-11-03 11:11:11 +00003444# ifdef WIN3264
3445/*
3446 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3447 */
3448 static int
3449gettimeofday(struct timeval *tv, char *dummy)
3450{
3451 long t = clock();
3452 tv->tv_sec = t / CLOCKS_PER_SEC;
3453 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3454 return 0;
3455}
3456# endif
3457
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003458/*
3459 * Save the previous time before doing something that could nest.
3460 * set "*tv_rel" to the time elapsed so far.
3461 */
3462 void
3463time_push(tv_rel, tv_start)
3464 void *tv_rel, *tv_start;
3465{
3466 *((struct timeval *)tv_rel) = prev_timeval;
3467 gettimeofday(&prev_timeval, NULL);
3468 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3469 - ((struct timeval *)tv_rel)->tv_usec;
3470 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3471 - ((struct timeval *)tv_rel)->tv_sec;
3472 if (((struct timeval *)tv_rel)->tv_usec < 0)
3473 {
3474 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3475 --((struct timeval *)tv_rel)->tv_sec;
3476 }
3477 *(struct timeval *)tv_start = prev_timeval;
3478}
3479
3480/*
3481 * Compute the previous time after doing something that could nest.
3482 * Subtract "*tp" from prev_timeval;
3483 * Note: The arguments are (void *) to avoid trouble with systems that don't
3484 * have struct timeval.
3485 */
3486 void
3487time_pop(tp)
3488 void *tp; /* actually (struct timeval *) */
3489{
3490 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3491 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3492 if (prev_timeval.tv_usec < 0)
3493 {
3494 prev_timeval.tv_usec += 1000000;
3495 --prev_timeval.tv_sec;
3496 }
3497}
3498
3499 static void
3500time_diff(then, now)
3501 struct timeval *then;
3502 struct timeval *now;
3503{
3504 long usec;
3505 long msec;
3506
3507 usec = now->tv_usec - then->tv_usec;
3508 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3509 usec = usec % 1000L;
3510 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3511}
3512
3513 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003514time_msg(mesg, tv_start)
3515 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003516 void *tv_start; /* only for do_source: start time; actually
3517 (struct timeval *) */
3518{
3519 static struct timeval start;
3520 struct timeval now;
3521
3522 if (time_fd != NULL)
3523 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003524 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003525 {
3526 gettimeofday(&start, NULL);
3527 prev_timeval = start;
3528 fprintf(time_fd, "\n\ntimes in msec\n");
3529 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3530 fprintf(time_fd, " clock elapsed: other lines\n\n");
3531 }
3532 gettimeofday(&now, NULL);
3533 time_diff(&start, &now);
3534 if (((struct timeval *)tv_start) != NULL)
3535 {
3536 fprintf(time_fd, " ");
3537 time_diff(((struct timeval *)tv_start), &now);
3538 }
3539 fprintf(time_fd, " ");
3540 time_diff(&prev_timeval, &now);
3541 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003542 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003543 }
3544}
3545
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003546#endif
3547
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003548#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003549
3550/*
3551 * Common code for the X command server and the Win32 command server.
3552 */
3553
Bram Moolenaareb94e552006-03-11 21:35:11 +00003554static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003555
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003556/*
3557 * Do the client-server stuff, unless "--servername ''" was used.
3558 */
3559 static void
3560exec_on_server(parmp)
3561 mparm_T *parmp;
3562{
3563 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3564 {
3565# ifdef WIN32
3566 /* Initialise the client/server messaging infrastructure. */
3567 serverInitMessaging();
3568# endif
3569
3570 /*
3571 * When a command server argument was found, execute it. This may
3572 * exit Vim when it was successful. Otherwise it's executed further
3573 * on. Remember the encoding used here in "serverStrEnc".
3574 */
3575 if (parmp->serverArg)
3576 {
3577 cmdsrv_main(&parmp->argc, parmp->argv,
3578 parmp->serverName_arg, &parmp->serverStr);
3579# ifdef FEAT_MBYTE
3580 parmp->serverStrEnc = vim_strsave(p_enc);
3581# endif
3582 }
3583
3584 /* If we're still running, get the name to register ourselves.
3585 * On Win32 can register right now, for X11 need to setup the
3586 * clipboard first, it's further down. */
3587 parmp->servername = serverMakeName(parmp->serverName_arg,
3588 parmp->argv[0]);
3589# ifdef WIN32
3590 if (parmp->servername != NULL)
3591 {
3592 serverSetName(parmp->servername);
3593 vim_free(parmp->servername);
3594 }
3595# endif
3596 }
3597}
3598
3599/*
3600 * Prepare for running as a Vim server.
3601 */
3602 static void
3603prepare_server(parmp)
3604 mparm_T *parmp;
3605{
3606# if defined(FEAT_X11)
3607 /*
3608 * Register for remote command execution with :serversend and --remote
3609 * unless there was a -X or a --servername '' on the command line.
3610 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003611 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003612 */
3613 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3614# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003615 (gui.in_use
3616# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003617 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003618# endif
3619 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003620# endif
3621 parmp->serverName_arg != NULL))
3622 {
3623 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3624 vim_free(parmp->servername);
3625 TIME_MSG("register server name");
3626 }
3627 else
3628 serverDelayedStartName = parmp->servername;
3629# endif
3630
3631 /*
3632 * Execute command ourselves if we're here because the send failed (or
3633 * else we would have exited above).
3634 */
3635 if (parmp->serverStr != NULL)
3636 {
3637 char_u *p;
3638
3639 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3640 parmp->serverStr, &p));
3641 vim_free(p);
3642 }
3643}
3644
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003645 static void
3646cmdsrv_main(argc, argv, serverName_arg, serverStr)
3647 int *argc;
3648 char **argv;
3649 char_u *serverName_arg;
3650 char_u **serverStr;
3651{
3652 char_u *res;
3653 int i;
3654 char_u *sname;
3655 int ret;
3656 int didone = FALSE;
3657 int exiterr = 0;
3658 char **newArgV = argv + 1;
3659 int newArgC = 1,
3660 Argc = *argc;
3661 int argtype;
3662#define ARGTYPE_OTHER 0
3663#define ARGTYPE_EDIT 1
3664#define ARGTYPE_EDIT_WAIT 2
3665#define ARGTYPE_SEND 3
3666 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003667 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003668# ifndef FEAT_X11
3669 HWND srv;
3670# else
3671 Window srv;
3672
3673 setup_term_clip();
3674# endif
3675
3676 sname = serverMakeName(serverName_arg, argv[0]);
3677 if (sname == NULL)
3678 return;
3679
3680 /*
3681 * Execute the command server related arguments and remove them
3682 * from the argc/argv array; We may have to return into main()
3683 */
3684 for (i = 1; i < Argc; i++)
3685 {
3686 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003687 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003688 {
3689 for (; i < *argc; i++)
3690 {
3691 *newArgV++ = argv[i];
3692 newArgC++;
3693 }
3694 break;
3695 }
3696
Bram Moolenaareb94e552006-03-11 21:35:11 +00003697 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003698 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003699 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3700 {
3701 char *p = argv[i] + 8;
3702
3703 argtype = ARGTYPE_EDIT;
3704 while (*p != NUL)
3705 {
3706 if (STRNICMP(p, "-wait", 5) == 0)
3707 {
3708 argtype = ARGTYPE_EDIT_WAIT;
3709 p += 5;
3710 }
3711 else if (STRNICMP(p, "-silent", 7) == 0)
3712 {
3713 silent = TRUE;
3714 p += 7;
3715 }
3716 else if (STRNICMP(p, "-tab", 4) == 0)
3717 {
3718 tabs = TRUE;
3719 p += 4;
3720 }
3721 else
3722 {
3723 argtype = ARGTYPE_OTHER;
3724 break;
3725 }
3726 }
3727 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003728 else
3729 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003730
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003731 if (argtype != ARGTYPE_OTHER)
3732 {
3733 if (i == *argc - 1)
3734 mainerr_arg_missing((char_u *)argv[i]);
3735 if (argtype == ARGTYPE_SEND)
3736 {
3737 *serverStr = (char_u *)argv[i + 1];
3738 i++;
3739 }
3740 else
3741 {
3742 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003743 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003744 if (*serverStr == NULL)
3745 {
3746 /* Probably out of memory, exit. */
3747 didone = TRUE;
3748 exiterr = 1;
3749 break;
3750 }
3751 Argc = i;
3752 }
3753# ifdef FEAT_X11
3754 if (xterm_dpy == NULL)
3755 {
3756 mch_errmsg(_("No display"));
3757 ret = -1;
3758 }
3759 else
3760 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3761 NULL, &srv, 0, 0, silent);
3762# else
3763 /* Win32 always works? */
3764 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3765# endif
3766 if (ret < 0)
3767 {
3768 if (argtype == ARGTYPE_SEND)
3769 {
3770 /* Failed to send, abort. */
3771 mch_errmsg(_(": Send failed.\n"));
3772 didone = TRUE;
3773 exiterr = 1;
3774 }
3775 else if (!silent)
3776 /* Let vim start normally. */
3777 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3778 break;
3779 }
3780
3781# ifdef FEAT_GUI_W32
3782 /* Guess that when the server name starts with "g" it's a GUI
3783 * server, which we can bring to the foreground here.
3784 * Foreground() in the server doesn't work very well. */
3785 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3786 SetForegroundWindow(srv);
3787# endif
3788
3789 /*
3790 * For --remote-wait: Wait until the server did edit each
3791 * file. Also detect that the server no longer runs.
3792 */
3793 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3794 {
3795 int numFiles = *argc - i - 1;
3796 int j;
3797 char_u *done = alloc(numFiles);
3798 char_u *p;
3799# ifdef FEAT_GUI_W32
3800 NOTIFYICONDATA ni;
3801 int count = 0;
3802 extern HWND message_window;
3803# endif
3804
3805 if (numFiles > 0 && argv[i + 1][0] == '+')
3806 /* Skip "+cmd" argument, don't wait for it to be edited. */
3807 --numFiles;
3808
3809# ifdef FEAT_GUI_W32
3810 ni.cbSize = sizeof(ni);
3811 ni.hWnd = message_window;
3812 ni.uID = 0;
3813 ni.uFlags = NIF_ICON|NIF_TIP;
3814 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3815 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3816 Shell_NotifyIcon(NIM_ADD, &ni);
3817# endif
3818
3819 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003820 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003821 while (memchr(done, 0, numFiles) != NULL)
3822 {
3823# ifdef WIN32
3824 p = serverGetReply(srv, NULL, TRUE, TRUE);
3825 if (p == NULL)
3826 break;
3827# else
3828 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3829 break;
3830# endif
3831 j = atoi((char *)p);
3832 if (j >= 0 && j < numFiles)
3833 {
3834# ifdef FEAT_GUI_W32
3835 ++count;
3836 sprintf(ni.szTip, _("%d of %d edited"),
3837 count, numFiles);
3838 Shell_NotifyIcon(NIM_MODIFY, &ni);
3839# endif
3840 done[j] = 1;
3841 }
3842 }
3843# ifdef FEAT_GUI_W32
3844 Shell_NotifyIcon(NIM_DELETE, &ni);
3845# endif
3846 }
3847 }
3848 else if (STRICMP(argv[i], "--remote-expr") == 0)
3849 {
3850 if (i == *argc - 1)
3851 mainerr_arg_missing((char_u *)argv[i]);
3852# ifdef WIN32
3853 /* Win32 always works? */
3854 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3855 &res, NULL, 1, FALSE) < 0)
3856# else
3857 if (xterm_dpy == NULL)
3858 mch_errmsg(_("No display: Send expression failed.\n"));
3859 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3860 &res, NULL, 1, 1, FALSE) < 0)
3861# endif
3862 {
3863 if (res != NULL && *res != NUL)
3864 {
3865 /* Output error from remote */
3866 mch_errmsg((char *)res);
3867 vim_free(res);
3868 res = NULL;
3869 }
3870 mch_errmsg(_(": Send expression failed.\n"));
3871 }
3872 }
3873 else if (STRICMP(argv[i], "--serverlist") == 0)
3874 {
3875# ifdef WIN32
3876 /* Win32 always works? */
3877 res = serverGetVimNames();
3878# else
3879 if (xterm_dpy != NULL)
3880 res = serverGetVimNames(xterm_dpy);
3881# endif
3882 if (called_emsg)
3883 mch_errmsg("\n");
3884 }
3885 else if (STRICMP(argv[i], "--servername") == 0)
3886 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003887 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003888 i++;
3889 continue;
3890 }
3891 else
3892 {
3893 *newArgV++ = argv[i];
3894 newArgC++;
3895 continue;
3896 }
3897 didone = TRUE;
3898 if (res != NULL && *res != NUL)
3899 {
3900 mch_msg((char *)res);
3901 if (res[STRLEN(res) - 1] != '\n')
3902 mch_msg("\n");
3903 }
3904 vim_free(res);
3905 }
3906
3907 if (didone)
3908 {
3909 display_errors(); /* display any collected messages */
3910 exit(exiterr); /* Mission accomplished - get out */
3911 }
3912
3913 /* Return back into main() */
3914 *argc = newArgC;
3915 vim_free(sname);
3916}
3917
3918/*
3919 * Build a ":drop" command to send to a Vim server.
3920 */
3921 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003922build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003923 int filec;
3924 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003925 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003926 int sendReply;
3927{
3928 garray_T ga;
3929 int i;
3930 char_u *inicmd = NULL;
3931 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003932 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003933 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003934
3935 if (filec > 0 && filev[0][0] == '+')
3936 {
3937 inicmd = (char_u *)filev[0] + 1;
3938 filev++;
3939 filec--;
3940 }
3941 /* Check if we have at least one argument. */
3942 if (filec <= 0)
3943 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003944
3945 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003946 cwd = alloc(MAXPATHL);
3947 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003948 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003949 if (mch_dirname(cwd, MAXPATHL) != OK)
3950 {
3951 vim_free(cwd);
3952 return NULL;
3953 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003954 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003955#ifdef BACKSLASH_IN_FILENAME
3956 "", /* rem_backslash() will tell what chars to escape */
3957#else
3958 PATH_ESC_CHARS,
3959#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003960 '\\', TRUE);
3961 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003962 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003963 return NULL;
3964 ga_init2(&ga, 1, 100);
3965 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003966 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003967
3968 /* Call inputsave() so that a prompt for an encryption key works. */
3969 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3970 if (tabs)
3971 ga_concat(&ga, (char_u *)"tab ");
3972 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003973 for (i = 0; i < filec; i++)
3974 {
3975 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003976 * do it again in the Vim server. On MS-Windows only escape
3977 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003978 p = vim_strsave_escaped((char_u *)filev[i],
3979#ifdef UNIX
3980 PATH_ESC_CHARS
3981#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003982 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003983#endif
3984 );
3985 if (p == NULL)
3986 {
3987 vim_free(ga.ga_data);
3988 return NULL;
3989 }
3990 ga_concat(&ga, (char_u *)" ");
3991 ga_concat(&ga, p);
3992 vim_free(p);
3993 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003994 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3995
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003996 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3997 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003998 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3999
4000 /* Switch back to the correct current directory (prior to temporary path
4001 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004002 * correct after the :drop command. With line breaks and spaces:
4003 * if !exists('+acd') || !&acd
4004 * if haslocaldir()
4005 * cd -
4006 * lcd -
4007 * elseif getcwd() ==# "current path"
4008 * cd -
4009 * endif
4010 * endif
4011 */
4012 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
4013 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# \"");
4014 ga_concat(&ga, cdp);
4015 ga_concat(&ga, (char_u *)"\"|cd -|endif|endif<CR>");
4016 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004017
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004018 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004019 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4020 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004021 if (inicmd != NULL)
4022 {
4023 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4024 * the following commands to be inserted as text. Use a "|",
4025 * hopefully "inicmd" does allow this... */
4026 ga_concat(&ga, inicmd);
4027 ga_concat(&ga, (char_u *)"|");
4028 }
4029 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4030 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004031 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004032 ga_append(&ga, NUL);
4033 return ga.ga_data;
4034}
4035
4036/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004037 * Make our basic server name: use the specified "arg" if given, otherwise use
4038 * the tail of the command "cmd" we were started with.
4039 * Return the name in allocated memory. This doesn't include a serial number.
4040 */
4041 static char_u *
4042serverMakeName(arg, cmd)
4043 char_u *arg;
4044 char *cmd;
4045{
4046 char_u *p;
4047
4048 if (arg != NULL && *arg != NUL)
4049 p = vim_strsave_up(arg);
4050 else
4051 {
4052 p = vim_strsave_up(gettail((char_u *)cmd));
4053 /* Remove .exe or .bat from the name. */
4054 if (p != NULL && vim_strchr(p, '.') != NULL)
4055 *vim_strchr(p, '.') = NUL;
4056 }
4057 return p;
4058}
4059#endif /* FEAT_CLIENTSERVER */
4060
4061#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4062/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004063 * Replace termcodes such as <CR> and insert as key presses if there is room.
4064 */
4065 void
4066server_to_input_buf(str)
4067 char_u *str;
4068{
4069 char_u *ptr = NULL;
4070 char_u *cpo_save = p_cpo;
4071
4072 /* Set 'cpoptions' the way we want it.
4073 * B set - backslashes are *not* treated specially
4074 * k set - keycodes are *not* reverse-engineered
4075 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004076 * The last but one parameter of replace_termcodes() is TRUE so that the
4077 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004078 */
4079 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004080 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004081 p_cpo = cpo_save;
4082
4083 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4084 {
4085 /*
4086 * Add the string to the input stream.
4087 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4088 *
4089 * First clear typed characters from the typeahead buffer, there could
4090 * be half a mapping there. Then append to the existing string, so
4091 * that multiple commands from a client are concatenated.
4092 */
4093 if (typebuf.tb_maplen < typebuf.tb_len)
4094 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4095 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4096
4097 /* Let input_available() know we inserted text in the typeahead
4098 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004099 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004100 }
4101 vim_free((char_u *)ptr);
4102}
4103
4104/*
4105 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004106 */
4107 char_u *
4108eval_client_expr_to_string(expr)
4109 char_u *expr;
4110{
4111 char_u *res;
4112 int save_dbl = debug_break_level;
4113 int save_ro = redir_off;
4114
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004115 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4116 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004117 debug_break_level = -1;
4118 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004119 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4120 * to be typed. Do generate errors so that try/catch works. */
4121 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004122
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004123 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004124
4125 debug_break_level = save_dbl;
4126 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004127 --emsg_silent;
4128 if (emsg_silent < 0)
4129 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004130
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004131 /* A client can tell us to redraw, but not to display the cursor, so do
4132 * that here. */
4133 setcursor();
4134 out_flush();
4135#ifdef FEAT_GUI
4136 if (gui.in_use)
4137 gui_update_cursor(FALSE, FALSE);
4138#endif
4139
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004140 return res;
4141}
4142
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004143/*
4144 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4145 * return an allocated string. Otherwise return "data".
4146 * "*tofree" is set to the result when it needs to be freed later.
4147 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004148 char_u *
4149serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004150 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004151 char_u *data;
4152 char_u **tofree;
4153{
4154 char_u *res = data;
4155
4156 *tofree = NULL;
4157# ifdef FEAT_MBYTE
4158 if (client_enc != NULL && p_enc != NULL)
4159 {
4160 vimconv_T vimconv;
4161
4162 vimconv.vc_type = CONV_NONE;
4163 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4164 && vimconv.vc_type != CONV_NONE)
4165 {
4166 res = string_convert(&vimconv, data, NULL);
4167 if (res == NULL)
4168 res = data;
4169 else
4170 *tofree = res;
4171 }
4172 convert_setup(&vimconv, NULL, NULL);
4173 }
4174# endif
4175 return res;
4176}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004177#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004178
4179/*
4180 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4181 */
4182#if defined(FEAT_FKMAP) || defined(PROTO)
4183# include "farsi.c"
4184#endif
4185
4186/*
4187 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4188 */
4189#if defined(FEAT_ARABIC) || defined(PROTO)
4190# include "arabic.c"
4191#endif