blob: abcf0c974eeac198e7a77b4f17746b64ec43d182 [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
13#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000014# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000015#endif
16
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017#ifdef __CYGWIN__
18# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000019# include <cygwin/version.h>
20# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
21 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000022# endif
23# include <limits.h>
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Maximum number of commands from + or -c arguments. */
27#define MAX_ARG_CMDS 10
28
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000029/* values for "window_layout" */
30#define WIN_HOR 1 /* "-o" horizontally split windows */
31#define WIN_VER 2 /* "-O" vertically split windows */
32#define WIN_TABS 3 /* "-p" windows on tab pages */
33
Bram Moolenaar58d98232005-07-23 22:25:46 +000034/* Struct for various parameters passed between main() and other functions. */
35typedef struct
36{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 int argc;
38 char **argv;
39
40 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000041 char_u *use_vimrc; /* vimrc from -u argument */
42
43 int n_commands; /* no. of commands from + or -c */
44 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
45 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
46 int n_pre_commands; /* no. of commands from --cmd */
47 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
48
49 int edit_type; /* type of editing to do */
50 char_u *tagname; /* tag from -t argument */
51#ifdef FEAT_QUICKFIX
52 char_u *use_ef; /* 'errorfile' from -q argument */
53#endif
54
55 int want_full_screen;
56 int stdout_isatty; /* is stdout a terminal? */
57 char_u *term; /* specified terminal name */
58#ifdef FEAT_CRYPT
59 int ask_for_key; /* -x argument */
60#endif
61 int no_swap_file; /* "-n" argument used */
62#ifdef FEAT_EVAL
63 int use_debug_break_level;
64#endif
65#ifdef FEAT_WINDOWS
66 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000067 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000068#endif
69
70#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000071 int serverArg; /* TRUE when argument for a server */
72 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000073 char_u *serverStr; /* remote server command */
74 char_u *serverStrEnc; /* encoding of serverStr */
75 char_u *servername; /* allocated name for our server */
76#endif
77#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
78 int literal; /* don't expand file names */
79#endif
80#ifdef MSWIN
81 int full_path; /* file name argument was full path */
82#endif
83#ifdef FEAT_DIFF
84 int diff_mode; /* start with 'diff' set */
85#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000086} mparm_T;
87
Bram Moolenaarc013cb62005-07-24 21:18:31 +000088/* Values for edit_type. */
89#define EDIT_NONE 0 /* no edit type yet */
90#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
91#define EDIT_STDIN 2 /* read file from stdin */
92#define EDIT_TAG 3 /* tag name argument given, use tagname */
93#define EDIT_QF 4 /* start in quickfix mode */
94
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010095#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000096static int file_owned __ARGS((char *fname));
97#endif
98static void mainerr __ARGS((int, char_u *));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010099#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100static void main_msg __ARGS((char *s));
101static void usage __ARGS((void));
102static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100103# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000104static void init_locale __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100105# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000106static void parse_command_name __ARGS((mparm_T *parmp));
107static void early_arg_scan __ARGS((mparm_T *parmp));
108static void command_line_scan __ARGS((mparm_T *parmp));
109static void check_tty __ARGS((mparm_T *parmp));
110static void read_stdin __ARGS((void));
111static void create_windows __ARGS((mparm_T *parmp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100112# ifdef FEAT_WINDOWS
Bram Moolenaard87c36e2015-04-03 14:56:49 +0200113static void edit_buffers __ARGS((mparm_T *parmp, char_u *cwd));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100114# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000115static void exe_pre_commands __ARGS((mparm_T *parmp));
116static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000117static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118static void main_start_gui __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100119# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120static void check_swap_exists_action __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100121# endif
122# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000123static void exec_on_server __ARGS((mparm_T *parmp));
124static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
126static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100127# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000128#endif
129
130
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000131/*
132 * Different types of error messages.
133 */
134static char *(main_errors[]) =
135{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000137#define ME_UNKNOWN_OPTION 0
138 N_("Too many edit arguments"),
139#define ME_TOO_MANY_ARGS 1
140 N_("Argument missing after"),
141#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000143#define ME_GARBAGE 3
144 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
145#define ME_EXTRA_CMD 4
146 N_("Invalid argument for"),
147#define ME_INVALID_ARG 5
148};
149
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100150#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100151#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +0200152
153static char_u *start_dir = NULL; /* current working dir on startup */
154
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000155 int
156# ifdef VIMDLL
157_export
158# endif
159# ifdef FEAT_GUI_MSWIN
160# ifdef __BORLANDC__
161_cdecl
162# endif
163VimMain
164# else
165main
166# endif
167(argc, argv)
168 int argc;
169 char **argv;
170{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000171 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000172 mparm_T params; /* various parameters passed between
173 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000174#ifdef STARTUPTIME
175 int i;
176#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000177
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178 /*
179 * Do any system-specific initialisations. These can NOT use IObuff or
180 * NameBuff. Thus emsg2() cannot be called!
181 */
182 mch_early_init();
183
Bram Moolenaar14993322014-09-09 12:25:33 +0200184#if defined(WIN32) && defined(FEAT_MBYTE)
185 /*
186 * MingW expands command line arguments, which confuses our code to
187 * convert when 'encoding' changes. Get the unexpanded arguments.
188 */
189 argc = get_cmd_argsW(&argv);
190#endif
191
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000192 /* Many variables are in "params" so that we can pass them to invoked
193 * functions without a lot of arguments. "argc" and "argv" are also
194 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000195 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000196 params.argc = argc;
197 params.argv = argv;
198 params.want_full_screen = TRUE;
199#ifdef FEAT_EVAL
200 params.use_debug_break_level = -1;
201#endif
202#ifdef FEAT_WINDOWS
203 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000204#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000205
Bram Moolenaar99685e62013-05-11 13:56:18 +0200206#ifdef FEAT_RUBY
207 {
208 int ruby_stack_start;
209 vim_ruby_init((void *)&ruby_stack_start);
210 }
211#endif
212
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000214 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215#endif
216
217#ifdef MEM_PROFILE
218 atexit(vim_mem_profile_dump);
219#endif
220
221#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000222 for (i = 1; i < argc; ++i)
223 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000224 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000225 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000226 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000227 TIME_MSG("--- VIM STARTING ---");
228 break;
229 }
230 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000232 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233
234#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000235 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000236#endif
237
238#ifdef FEAT_MBYTE
239 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
240#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000241#ifdef FEAT_EVAL
242 eval_init(); /* init global variables */
243#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000244
245#ifdef __QNXNTO__
246 qnx_init(); /* PhAttach() for clipboard, (and gui) */
247#endif
248
249#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000250 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000251 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000252 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000253 TIME_MSG("GUI prepared");
254#endif
255
256 /* Init the table of Normal mode commands. */
257 init_normal_cmds();
258
259#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000260 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000261#endif
262
263 /*
264 * Allocate space for the generic buffers (needed for set_init_1() and
265 * EMSG2()).
266 */
267 if ((IObuff = alloc(IOSIZE)) == NULL
268 || (NameBuff = alloc(MAXPATHL)) == NULL)
269 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000270 TIME_MSG("Allocated generic buffers");
271
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000272#ifdef NBDEBUG
273 /* Wait a moment for debugging NetBeans. Must be after allocating
274 * NameBuff. */
275 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
276 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000277 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000278#endif
279
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000280#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
281 /*
282 * Setup to use the current locale (for ctype() and many other things).
283 * NOTE: Translated messages with encodings other than latin1 will not
284 * work until set_init_1() has been called!
285 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287 TIME_MSG("locale set");
288#endif
289
290#ifdef FEAT_GUI
291 gui.dofork = TRUE; /* default is to use fork() */
292#endif
293
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000294 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000295 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000297 * --server...
298 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000299 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000301 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302
303#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000304 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000305#endif
306#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000307 /* Prepare for possibly starting GUI sometime */
308 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000309 TIME_MSG("GUI prepared");
310#endif
311
312#ifdef FEAT_CLIPBOARD
313 clip_init(FALSE); /* Initialise clipboard stuff */
314 TIME_MSG("clipboard setup");
315#endif
316
317 /*
318 * Check if we have an interactive window.
319 * On the Amiga: If there is no window, we open one with a newcli command
320 * (needed for :! to * work). mch_check_win() will also handle the -d or
321 * -dev argument.
322 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000323 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324 TIME_MSG("window checked");
325
326 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000327 * Allocate the first window and buffer.
328 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000330 if (win_alloc_first() == FAIL)
331 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000332
333 init_yank(); /* init yank buffers */
334
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000335 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200336 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000337
338 /*
339 * Set the default values for the options.
340 * NOTE: Non-latin1 translated messages are working only after this,
341 * because this is where "has_mbyte" will be set, which is used by
342 * msg_outtrans_len_attr().
343 * First find out the home directory, needed to expand "~" in options.
344 */
345 init_homedir(); /* find real value of $HOME */
346 set_init_1();
347 TIME_MSG("inits 1");
348
349#ifdef FEAT_EVAL
350 set_lang_var(); /* set v:lang and v:ctype */
351#endif
352
353#ifdef FEAT_CLIENTSERVER
354 /*
355 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000356 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000357 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000358 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359#endif
360
361 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000362 * Figure out the way to work from the command name argv[0].
363 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000365 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366
367 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000368 * Process the command line arguments. File names are put in the global
369 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000371 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000372 TIME_MSG("parsing arguments");
373
374 /*
375 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000376 * there is no terminal version, and on Windows we can't fork one off with
377 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000378 */
379#ifdef ALWAYS_USE_GUI
380 gui.starting = TRUE;
381#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000382# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 /*
384 * Check if the GUI can be started. Reset gui.starting if not.
385 * Don't know about other systems, stay on the safe side and don't check.
386 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000387 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000388 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000389 if (gui_init_check() == FAIL)
390 {
391 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000392
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000393 /* When running "evim" or "gvim -y" we need the menus, exit if we
394 * don't have them. */
395 if (params.evim_mode)
396 mch_exit(1);
397 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000398 }
399# endif
400#endif
401
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000402 if (GARGCOUNT > 0)
403 {
404#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
405 /*
406 * Expand wildcards in file names.
407 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000408 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000409 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200410 start_dir = alloc(MAXPATHL);
411 if (start_dir != NULL)
412 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000413 /* Temporarily add '(' and ')' to 'isfname'. These are valid
414 * filename characters but are excluded from 'isfname' to make
415 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
416 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000417 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000418 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200419 if (start_dir != NULL)
420 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000421 }
422#endif
423 fname = alist_name(&GARGLIST[0]);
424 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000425
426#if defined(WIN32) && defined(FEAT_MBYTE)
427 {
428 extern void set_alist_count(void);
429
430 /* Remember the number of entries in the argument list. If it changes
431 * we don't react on setting 'encoding'. */
432 set_alist_count();
433 }
434#endif
435
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000436#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000437 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000438 {
439 /*
440 * If there is one filename, fully qualified, we have very probably
441 * been invoked from explorer, so change to the file's directory.
442 * Hint: to avoid this when typing a command use a forward slash.
443 * If the cd fails, it doesn't matter.
444 */
445 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200446 if (start_dir != NULL)
447 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000448 }
449#endif
450 TIME_MSG("expanding arguments");
451
452#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000453 if (params.diff_mode && params.window_count == -1)
454 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000455#endif
456
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000457 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000458 ++RedrawingDisabled;
459
460 /*
461 * When listing swap file names, don't do cursor positioning et. al.
462 */
463 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000464 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000465
466 /*
467 * When certain to start the GUI, don't check capabilities of terminal.
468 * For GTK we can't be sure, but when started from the desktop it doesn't
469 * make sense to try using a terminal.
470 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000471#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000472 if (gui.starting
473# ifdef FEAT_GUI_GTK
474 && !isatty(2)
475# endif
476 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000477 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000478#endif
479
480#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
481 /* When the GUI is started from Finder, need to display messages in a
482 * message box. isatty(2) returns TRUE anyway, thus we need to check the
483 * name to know we're not started from a terminal. */
484 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000485 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000486 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000487
488 /* Avoid always using "/" as the current directory. Note that when
489 * started from Finder the arglist will be filled later in
490 * HandleODocAE() and "fname" will be NULL. */
491 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
492 && STRCMP(NameBuff, "/") == 0)
493 {
494 if (fname != NULL)
495 (void)vim_chdirfile(fname);
496 else
497 {
498 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
499 vim_chdir(NameBuff);
500 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200501 if (start_dir != NULL)
502 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000503 }
504 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000505#endif
506
507 /*
508 * mch_init() sets up the terminal (window) for use. This must be
509 * done after resetting full_screen, otherwise it may move the cursor
510 * (MSDOS).
511 * Note that we may use mch_exit() before mch_init()!
512 */
513 mch_init();
514 TIME_MSG("shell init");
515
516#ifdef USE_XSMP
517 /*
518 * For want of anywhere else to do it, try to connect to xsmp here.
519 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
520 * Hijacking -X 'no X connection' to also disable XSMP connection as that
521 * has a similar delay upon failure.
522 * Only try if SESSION_MANAGER is set to something non-null.
523 */
524 if (!x_no_connect)
525 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000526 char *p = getenv("SESSION_MANAGER");
527
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528 if (p != NULL && *p != NUL)
529 {
530 xsmp_init();
531 TIME_MSG("xsmp init");
532 }
533 }
534#endif
535
536 /*
537 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000539 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000540
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000541 /* This message comes before term inits, but after setting "silent_mode"
542 * when the input is not a tty. */
543 if (GARGCOUNT > 1 && !silent_mode)
544 printf(_("%d files to edit\n"), GARGCOUNT);
545
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000546 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000548 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000549 capabilities (will set full_screen) */
550 screen_start(); /* don't know where cursor is now */
551 TIME_MSG("Termcap init");
552 }
553
554 /*
555 * Set the default values for the options that use Rows and Columns.
556 */
557 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000558 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000559#ifdef FEAT_DIFF
560 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
561 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000562 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000563 diff_win_options(firstwin, FALSE);
564#endif
565
566 cmdline_row = Rows - p_ch;
567 msg_row = cmdline_row;
568 screenalloc(FALSE); /* allocate screen buffers */
569 set_init_2();
570 TIME_MSG("inits 2");
571
572 msg_scroll = TRUE;
573 no_wait_return = TRUE;
574
575 init_mappings(); /* set up initial mappings */
576
577 init_highlight(TRUE, FALSE); /* set the default highlight groups */
578 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000579
580#ifdef FEAT_EVAL
581 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000582 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000583#endif
584
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100585#ifdef FEAT_MZSCHEME
586 /*
587 * Newer version of MzScheme (Racket) require earlier (trampolined)
588 * initialisation via scheme_main_setup.
589 * Implement this by initialising it as early as possible
590 * and splitting off remaining Vim main into vim_main2
591 */
592 {
593 /* Pack up preprocessed command line arguments.
594 * It is safe because Scheme does not access argc/argv. */
595 char *args[2];
596 args[0] = (char *)fname;
597 args[1] = (char *)&params;
598 return mzscheme_main(2, args);
599 }
600}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100601#endif
602#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100603
Bram Moolenaar8866d272012-11-28 15:55:42 +0100604/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
605 * NO_VIM_MAIN is defined. */
606#ifdef FEAT_MZSCHEME
607 int
608vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100609{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100610# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100611 char_u *fname = (char_u *)argv[0];
612 mparm_T params;
613
614 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100615# else
616 return 0;
617}
618# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100619#endif
620
Bram Moolenaar8866d272012-11-28 15:55:42 +0100621#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000622 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000623 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000624
Bram Moolenaar58d98232005-07-23 22:25:46 +0000625 /* Source startup scripts. */
626 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000627
628#ifdef FEAT_EVAL
629 /*
630 * Read all the plugin files.
631 * Only when compiled with +eval, since most plugins need it.
632 */
633 if (p_lpl)
634 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000635# ifdef VMS /* Somehow VMS doesn't handle the "**". */
636 source_runtime((char_u *)"plugin/*.vim", TRUE);
637# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000638 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000639# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000640 TIME_MSG("loading plugins");
641 }
642#endif
643
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000644#ifdef FEAT_DIFF
645 /* Decide about window layout for diff mode after reading vimrc. */
646 if (params.diff_mode && params.window_layout == 0)
647 {
648 if (diffopt_horizontal())
649 params.window_layout = WIN_HOR; /* use horizontal split */
650 else
651 params.window_layout = WIN_VER; /* use vertical split */
652 }
653#endif
654
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000655 /*
656 * Recovery mode without a file name: List swap files.
657 * This uses the 'dir' option, therefore it must be after the
658 * initializations.
659 */
660 if (recoverymode && fname == NULL)
661 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200662 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000663 mch_exit(0);
664 }
665
666 /*
667 * Set a few option defaults after reading .vimrc files:
668 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
669 */
670 set_init_3();
671 TIME_MSG("inits 3");
672
673 /*
674 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
675 * Note that this overrides anything from a vimrc file.
676 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000677 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000678 p_uc = 0;
679
680#ifdef FEAT_FKMAP
681 if (curwin->w_p_rl && p_altkeymap)
682 {
683 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
684# ifdef FEAT_ARABIC
685 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
686# endif
687 p_fkmap = TRUE; /* Set the Farsi keymap mode */
688 }
689#endif
690
691#ifdef FEAT_GUI
692 if (gui.starting)
693 {
694#if defined(UNIX) || defined(VMS)
695 /* When something caused a message from a vimrc script, need to output
696 * an extra newline before the shell prompt. */
697 if (did_emsg || msg_didout)
698 putchar('\n');
699#endif
700
701 gui_start(); /* will set full_screen to TRUE */
702 TIME_MSG("starting GUI");
703
704 /* When running "evim" or "gvim -y" we need the menus, exit if we
705 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000706 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000707 mch_exit(1);
708 }
709#endif
710
711#ifdef SPAWNO /* special MSDOS swapping library */
712 init_SPAWNO("", SWAP_ANY);
713#endif
714
715#ifdef FEAT_VIMINFO
716 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000717 * Read in registers, history etc, but not marks, from the viminfo file.
718 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000719 */
720 if (*p_viminfo != NUL)
721 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000722 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000723 TIME_MSG("reading viminfo");
724 }
725#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100726#ifdef FEAT_EVAL
727 /* It's better to make v:oldfiles an empty list than NULL. */
728 if (get_vim_var_list(VV_OLDFILES) == NULL)
729 set_vim_var_list(VV_OLDFILES, list_alloc());
730#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000731
732#ifdef FEAT_QUICKFIX
733 /*
734 * "-q errorfile": Load the error file now.
735 * If the error file can't be read, exit before doing anything else.
736 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000737 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000738 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000739 if (params.use_ef != NULL)
740 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000741 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200742 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
743 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000744 {
745 out_char('\n');
746 mch_exit(3);
747 }
748 TIME_MSG("reading errorfile");
749 }
750#endif
751
752 /*
753 * Start putting things on the screen.
754 * Scroll screen down before drawing over it
755 * Clear screen now, so file message will not be cleared.
756 */
757 starting = NO_BUFFERS;
758 no_wait_return = FALSE;
759 if (!exmode_active)
760 msg_scroll = FALSE;
761
762#ifdef FEAT_GUI
763 /*
764 * This seems to be required to make callbacks to be called now, instead
765 * of after things have been put on the screen, which then may be deleted
766 * when getting a resize callback.
767 * For the Mac this handles putting files dropped on the Vim icon to
768 * global_alist.
769 */
770 if (gui.in_use)
771 {
772# ifdef FEAT_SUN_WORKSHOP
773 if (!usingSunWorkShop)
774# endif
775 gui_wait_for_chars(50L);
776 TIME_MSG("GUI delay");
777 }
778#endif
779
780#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
781 qnx_clip_init();
782#endif
783
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200784#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
785 clip_init(TRUE);
786#endif
787
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000788#ifdef FEAT_XCLIPBOARD
789 /* Start using the X clipboard, unless the GUI was started. */
790# ifdef FEAT_GUI
791 if (!gui.in_use)
792# endif
793 {
794 setup_term_clip();
795 TIME_MSG("setup clipboard");
796 }
797#endif
798
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000799#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000800 /* Prepare for being a Vim server. */
801 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000802#endif
803
804 /*
805 * If "-" argument given: Read file from stdin.
806 * Do this before starting Raw mode, because it may change things that the
807 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
808 * are the same terminal: "cat | vim -".
809 * Using autocommands here may cause trouble...
810 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000811 if (params.edit_type == EDIT_STDIN && !recoverymode)
812 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000813
814#if defined(UNIX) || defined(VMS)
815 /* When switching screens and something caused a message from a vimrc
816 * script, need to output an extra newline on exit. */
817 if ((did_emsg || msg_didout) && *T_TI != NUL)
818 newline_on_exit = TRUE;
819#endif
820
821 /*
822 * When done something that is not allowed or error message call
823 * wait_return. This must be done before starttermcap(), because it may
824 * switch to another screen. It must be done after settmode(TMODE_RAW),
825 * because we want to react on a single key stroke.
826 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000827 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000828 */
829 settmode(TMODE_RAW);
830 TIME_MSG("setting raw mode");
831
832 if (need_wait_return || msg_didany)
833 {
834 wait_return(TRUE);
835 TIME_MSG("waiting for return");
836 }
837
838 starttermcap(); /* start termcap if not done by wait_return() */
839 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200840#if defined(FEAT_TERMRESPONSE)
841# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200842 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200843# endif
844 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100845#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000846
847#ifdef FEAT_MOUSE
848 setmouse(); /* may start using the mouse */
849#endif
850 if (scroll_region)
851 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000852 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000853
854 /*
855 * Don't clear the screen when starting in Ex mode, unless using the GUI.
856 */
857 if (exmode_active
858#ifdef FEAT_GUI
859 && !gui.in_use
860#endif
861 )
862 must_redraw = CLEAR;
863 else
864 {
865 screenclear(); /* clear screen */
866 TIME_MSG("clearing screen");
867 }
868
869#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000870 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000871 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100872 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200873 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000874 TIME_MSG("getting crypt key");
875 }
876#endif
877
878 no_wait_return = TRUE;
879
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000881 * Create the requested number of windows and edit buffers in them.
882 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000883 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000884 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000885 TIME_MSG("opening buffers");
886
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000887#ifdef FEAT_EVAL
888 /* clear v:swapcommand */
889 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
890#endif
891
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000892 /* Ex starts at last line of the file */
893 if (exmode_active)
894 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
895
896#ifdef FEAT_AUTOCMD
897 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
898 TIME_MSG("BufEnter autocommands");
899#endif
900 setpcmark();
901
902#ifdef FEAT_QUICKFIX
903 /*
904 * When started with "-q errorfile" jump to first error now.
905 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000906 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000907 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000908 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000909 TIME_MSG("jump to first error");
910 }
911#endif
912
913#ifdef FEAT_WINDOWS
914 /*
915 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000916 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000917 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200918 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000919#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200920 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000921
922#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000923 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000924 {
925 win_T *wp;
926
927 /* set options in each window for "vimdiff". */
928 for (wp = firstwin; wp != NULL; wp = wp->w_next)
929 diff_win_options(wp, TRUE);
930 }
931#endif
932
933 /*
934 * Shorten any of the filenames, but only when absolute.
935 */
936 shorten_fnames(FALSE);
937
938 /*
939 * Need to jump to the tag before executing the '-c command'.
940 * Makes "vim -c '/return' -t main" work.
941 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000942 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000943 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000944#if defined(HAS_SWAP_EXISTS_ACTION)
945 swap_exists_did_quit = FALSE;
946#endif
947
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000948 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000949 do_cmdline_cmd(IObuff);
950 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000951
952#if defined(HAS_SWAP_EXISTS_ACTION)
953 /* If the user doesn't want to edit the file then we quit here. */
954 if (swap_exists_did_quit)
955 getout(1);
956#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000957 }
958
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000959 /* Execute any "+", "-c" and "-S" arguments. */
960 if (params.n_commands > 0)
961 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000962
963 RedrawingDisabled = 0;
964 redraw_all_later(NOT_VALID);
965 no_wait_return = FALSE;
966 starting = 0;
967
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000968#ifdef FEAT_TERMRESPONSE
969 /* Requesting the termresponse is postponed until here, so that a "-c q"
970 * argument doesn't make it appear in the shell Vim was started from. */
971 may_req_termresponse();
972#endif
973
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000974 /* start in insert mode */
975 if (p_im)
976 need_start_insertmode = TRUE;
977
978#ifdef FEAT_AUTOCMD
979 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
980 TIME_MSG("VimEnter autocommands");
981#endif
982
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200983#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
984 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
985 * done after the clipboard is available and all initial commands that may
986 * modify the 'clipboard' setting have run; i.e. just before entering the
987 * main loop. */
988 {
989 int default_regname = 0;
990 adjust_clip_reg(&default_regname);
991 set_reg_var(default_regname);
992 }
993#endif
994
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000995#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
996 /* When a startup script or session file setup for diff'ing and
997 * scrollbind, sync the scrollbind now. */
998 if (curwin->w_p_diff && curwin->w_p_scb)
999 {
1000 update_topline();
1001 check_scrollbind((linenr_T)0, 0L);
1002 TIME_MSG("diff scrollbinding");
1003 }
1004#endif
1005
1006#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1007 mch_set_winsize_now(); /* Allow winsize changes from now on */
1008#endif
1009
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001010#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1011 /* When tab pages were created, may need to update the tab pages line and
1012 * scrollbars. This is skipped while creating them. */
1013 if (first_tabpage->tp_next != NULL)
1014 {
1015 out_flush();
1016 gui_init_which_components(NULL);
1017 gui_update_scrollbars(TRUE);
1018 }
1019 need_mouse_correct = TRUE;
1020#endif
1021
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001022 /* If ":startinsert" command used, stuff a dummy command to be able to
1023 * call normal_cmd(), which will then start Insert mode. */
1024 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001025 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026
1027#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001028 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001029 {
1030# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001031# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001032 && !defined(FEAT_GUI_W32)
1033 if (gui.in_use)
1034 {
1035 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1036 mch_exit(2);
1037 }
1038# endif
1039# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001040 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001041 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001042 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043#endif
1044
1045 TIME_MSG("before starting main loop");
1046
1047 /*
1048 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001049 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001050 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051
1052 return 0;
1053}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001054#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001055#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001056
1057/*
1058 * Main loop: Execute Normal mode commands until exiting Vim.
1059 * Also used to handle commands in the command-line window, until the window
1060 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 * Also used to handle ":visual" command after ":global": execute Normal mode
1062 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001063 */
1064 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065main_loop(cmdwin, noexmode)
1066 int cmdwin; /* TRUE when working in the command-line window */
1067 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001068{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001069 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001070 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001071#ifdef FEAT_CONCEAL
1072 linenr_T conceal_old_cursor_line = 0;
1073 linenr_T conceal_new_cursor_line = 0;
1074 int conceal_update_lines = FALSE;
1075#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001076
1077#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1078 /* Setup to catch a terminating error from the X server. Just ignore
1079 * it, restore the state and continue. This might not always work
1080 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001081 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001082 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001083 {
1084 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001085 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001086 got_int = TRUE;
1087 need_wait_return = FALSE;
1088 global_busy = FALSE;
1089 exmode_active = 0;
1090 skip_redraw = FALSE;
1091 RedrawingDisabled = 0;
1092 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001093 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001094# ifdef FEAT_EVAL
1095 emsg_skip = 0;
1096# endif
1097 emsg_off = 0;
1098# ifdef FEAT_MOUSE
1099 setmouse();
1100# endif
1101 settmode(TMODE_RAW);
1102 starttermcap();
1103 scroll_start();
1104 redraw_later_clear();
1105 }
1106#endif
1107
1108 clear_oparg(&oa);
1109 while (!cmdwin
1110#ifdef FEAT_CMDWIN
1111 || cmdwin_result == 0
1112#endif
1113 )
1114 {
1115 if (stuff_empty())
1116 {
1117 did_check_timestamps = FALSE;
1118 if (need_check_timestamps)
1119 check_timestamps(FALSE);
1120 if (need_wait_return) /* if wait_return still needed ... */
1121 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001122 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001123 {
1124 need_start_insertmode = FALSE;
1125 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1126 /* skip the fileinfo message now, because it would be shown
1127 * after insert mode finishes! */
1128 need_fileinfo = FALSE;
1129 }
1130 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001131
1132 /* Reset "got_int" now that we got back to the main loop. Except when
1133 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1134 * the ":g" command.
1135 * For ":g/pat/vi" we reset "got_int" when used once. When used
1136 * a second time we go back to Ex mode and abort the ":g" command. */
1137 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001138 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001139 if (noexmode && global_busy && !exmode_active && previous_got_int)
1140 {
1141 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1142 * used and keep "got_int" set, so that it aborts ":g". */
1143 exmode_active = EXMODE_NORMAL;
1144 State = NORMAL;
1145 }
1146 else if (!global_busy || !exmode_active)
1147 {
1148 if (!quit_more)
1149 (void)vgetc(); /* flush all buffers */
1150 got_int = FALSE;
1151 }
1152 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001153 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001154 else
1155 previous_got_int = FALSE;
1156
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001157 if (!exmode_active)
1158 msg_scroll = FALSE;
1159 quit_more = FALSE;
1160
1161 /*
1162 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1163 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1164 * update cursor and redraw.
1165 */
1166 if (skip_redraw || exmode_active)
1167 skip_redraw = FALSE;
1168 else if (do_redraw || stuff_empty())
1169 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001170#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001171 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001172 if (!finish_op && (
1173# ifdef FEAT_AUTOCMD
1174 has_cursormoved()
1175# endif
1176# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1177 ||
1178# endif
1179# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001180 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001181# endif
1182 )
1183 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001184 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001185# ifdef FEAT_AUTOCMD
1186 if (has_cursormoved())
1187 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1188 FALSE, curbuf);
1189# endif
1190# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001191 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001192 {
1193 conceal_old_cursor_line = last_cursormoved.lnum;
1194 conceal_new_cursor_line = curwin->w_cursor.lnum;
1195 conceal_update_lines = TRUE;
1196 }
1197# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001198 last_cursormoved = curwin->w_cursor;
1199 }
1200#endif
1201
Bram Moolenaar186628f2013-03-19 13:33:23 +01001202#ifdef FEAT_AUTOCMD
1203 /* Trigger TextChanged if b_changedtick differs. */
1204 if (!finish_op && has_textchanged()
1205 && last_changedtick != curbuf->b_changedtick)
1206 {
1207 if (last_changedtick_buf == curbuf)
1208 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1209 FALSE, curbuf);
1210 last_changedtick_buf = curbuf;
1211 last_changedtick = curbuf->b_changedtick;
1212 }
1213#endif
1214
Bram Moolenaar33aec762006-01-22 23:30:12 +00001215#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1216 /* Scroll-binding for diff mode may have been postponed until
1217 * here. Avoids doing it for every change. */
1218 if (diff_need_scrollbind)
1219 {
1220 check_scrollbind((linenr_T)0, 0L);
1221 diff_need_scrollbind = FALSE;
1222 }
1223#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001224#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001225 /* Include a closed fold completely in the Visual area. */
1226 foldAdjustVisual();
1227#endif
1228#ifdef FEAT_FOLDING
1229 /*
1230 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1231 * contain the cursor.
1232 * When 'foldopen' is "all", open the fold(s) under the cursor.
1233 * This may mark the window for redrawing.
1234 */
1235 if (hasAnyFolding(curwin) && !char_avail())
1236 {
1237 foldCheckClose();
1238 if (fdo_flags & FDO_ALL)
1239 foldOpenCursor();
1240 }
1241#endif
1242
1243 /*
1244 * Before redrawing, make sure w_topline is correct, and w_leftcol
1245 * if lines don't wrap, and w_skipcol if lines wrap.
1246 */
1247 update_topline();
1248 validate_cursor();
1249
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001250 if (VIsual_active)
1251 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001252 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 update_screen(0);
1254 else if (redraw_cmdline || clear_cmdline)
1255 showmode();
1256#ifdef FEAT_WINDOWS
1257 redraw_statuslines();
1258#endif
1259#ifdef FEAT_TITLE
1260 if (need_maketitle)
1261 maketitle();
1262#endif
1263 /* display message after redraw */
1264 if (keep_msg != NULL)
1265 {
1266 char_u *p;
1267
1268 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001269 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1270 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001271 p = keep_msg;
1272 msg_attr(p, keep_msg_attr);
1273 vim_free(p);
1274 }
1275 if (need_fileinfo) /* show file info after redraw */
1276 {
1277 fileinfo(FALSE, TRUE, FALSE);
1278 need_fileinfo = FALSE;
1279 }
1280
1281 emsg_on_display = FALSE; /* can delete error message now */
1282 did_emsg = FALSE;
1283 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001284 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001285 showruler(FALSE);
1286
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001287# if defined(FEAT_CONCEAL)
1288 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001289 && (conceal_old_cursor_line != conceal_new_cursor_line
1290 || conceal_cursor_line(curwin)
1291 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001292 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001293 if (conceal_old_cursor_line != conceal_new_cursor_line
1294 && conceal_old_cursor_line
1295 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001296 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001297 update_single_line(curwin, conceal_new_cursor_line);
1298 curwin->w_valid &= ~VALID_CROW;
1299 }
1300# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001301 setcursor();
1302 cursor_on();
1303
1304 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001305
1306#ifdef STARTUPTIME
1307 /* Now that we have drawn the first screen all the startup stuff
1308 * has been done, close any file for startup messages. */
1309 if (time_fd != NULL)
1310 {
1311 TIME_MSG("first screen update");
1312 TIME_MSG("--- VIM STARTED ---");
1313 fclose(time_fd);
1314 time_fd = NULL;
1315 }
1316#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001317 }
1318#ifdef FEAT_GUI
1319 if (need_mouse_correct)
1320 gui_mouse_correct();
1321#endif
1322
1323 /*
1324 * Update w_curswant if w_set_curswant has been set.
1325 * Postponed until here to avoid computing w_virtcol too often.
1326 */
1327 update_curswant();
1328
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001329#ifdef FEAT_EVAL
1330 /*
1331 * May perform garbage collection when waiting for a character, but
1332 * only at the very toplevel. Otherwise we may be using a List or
1333 * Dict internally somewhere.
1334 * "may_garbage_collect" is reset in vgetc() which is invoked through
1335 * do_exmode() and normal_cmd().
1336 */
1337 may_garbage_collect = (!cmdwin && !noexmode);
1338#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001339 /*
1340 * If we're invoked as ex, do a round of ex commands.
1341 * Otherwise, get and execute a normal mode command.
1342 */
1343 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001344 {
1345 if (noexmode) /* End of ":global/path/visual" commands */
1346 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001347 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001348 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001349 else
1350 normal_cmd(&oa, TRUE);
1351 }
1352}
1353
1354
1355#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1356/*
1357 * Exit, but leave behind swap files for modified buffers.
1358 */
1359 void
1360getout_preserve_modified(exitval)
1361 int exitval;
1362{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001363# if defined(SIGHUP) && defined(SIG_IGN)
1364 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1365 * makes Vim exit and then handling SIGHUP causes various reentrance
1366 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001367 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001368# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001369
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370 ml_close_notmod(); /* close all not-modified buffers */
1371 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1372 ml_close_all(FALSE); /* close all memfiles, without deleting */
1373 getout(exitval); /* exit Vim properly */
1374}
1375#endif
1376
1377
1378/* Exit properly */
1379 void
1380getout(exitval)
1381 int exitval;
1382{
1383#ifdef FEAT_AUTOCMD
1384 buf_T *buf;
1385 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001386 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001387#endif
1388
1389 exiting = TRUE;
1390
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001391 /* When running in Ex mode an error causes us to exit with a non-zero exit
1392 * code. POSIX requires this, although it's not 100% clear from the
1393 * standard. */
1394 if (exmode_active)
1395 exitval += ex_exitval;
1396
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001397 /* Position the cursor on the last screen line, below all the text */
1398#ifdef FEAT_GUI
1399 if (!gui.in_use)
1400#endif
1401 windgoto((int)Rows - 1, 0);
1402
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001403#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1404 /* Optionally print hashtable efficiency. */
1405 hash_debug_results();
1406#endif
1407
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001408#ifdef FEAT_GUI
1409 msg_didany = FALSE;
1410#endif
1411
1412#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001413 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001414 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001415 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1416# if defined FEAT_WINDOWS
1417 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001418 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001419 next_tp = tp->tp_next;
1420 for (wp = (tp == curtab)
1421 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001422 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001423 if (wp->w_buffer == NULL)
1424 /* Autocmd must have close the buffer already, skip. */
1425 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 buf = wp->w_buffer;
1427 if (buf->b_changedtick != -1)
1428 {
1429 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1430 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001431 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 /* start all over, autocommands may mess up the lists */
1433 next_tp = first_tabpage;
1434 break;
1435 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001436 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001437 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001438# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001439 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1440 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001441# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001442
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001443 /* Trigger BufUnload for buffers that are loaded */
1444 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1445 if (buf->b_ml.ml_mfp != NULL)
1446 {
1447 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001448 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001449 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1450 break;
1451 }
1452 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1453 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001454#endif
1455
1456#ifdef FEAT_VIMINFO
1457 if (*p_viminfo != NUL)
1458 /* Write out the registers, history, marks etc, to the viminfo file */
1459 write_viminfo(NULL, FALSE);
1460#endif
1461
1462#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001463 if (get_vim_var_nr(VV_DYING) <= 1)
1464 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001465#endif
1466
Bram Moolenaar05159a02005-02-26 23:04:13 +00001467#ifdef FEAT_PROFILE
1468 profile_dump();
1469#endif
1470
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001471 if (did_emsg
1472#ifdef FEAT_GUI
1473 || (gui.in_use && msg_didany && p_verbose > 0)
1474#endif
1475 )
1476 {
1477 /* give the user a chance to read the (error) message */
1478 no_wait_return = FALSE;
1479 wait_return(FALSE);
1480 }
1481
1482#ifdef FEAT_AUTOCMD
1483 /* Position the cursor again, the autocommands may have moved it */
1484# ifdef FEAT_GUI
1485 if (!gui.in_use)
1486# endif
1487 windgoto((int)Rows - 1, 0);
1488#endif
1489
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001490#ifdef FEAT_LUA
1491 lua_end();
1492#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001493#ifdef FEAT_MZSCHEME
1494 mzscheme_end();
1495#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001496#ifdef FEAT_TCL
1497 tcl_end();
1498#endif
1499#ifdef FEAT_RUBY
1500 ruby_end();
1501#endif
1502#ifdef FEAT_PYTHON
1503 python_end();
1504#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001505#ifdef FEAT_PYTHON3
1506 python3_end();
1507#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001508#ifdef FEAT_PERL
1509 perl_end();
1510#endif
1511#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1512 iconv_end();
1513#endif
1514#ifdef FEAT_NETBEANS_INTG
1515 netbeans_end();
1516#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001517#ifdef FEAT_CSCOPE
1518 cs_end();
1519#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001520#ifdef FEAT_EVAL
1521 if (garbage_collect_at_exit)
1522 garbage_collect();
1523#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001524#if defined(WIN32) && defined(FEAT_MBYTE)
1525 free_cmd_argsW();
1526#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001527
1528 mch_exit(exitval);
1529}
1530
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001531#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001532/*
1533 * Get a (optional) count for a Vim argument.
1534 */
1535 static int
1536get_number_arg(p, idx, def)
1537 char_u *p; /* pointer to argument */
1538 int *idx; /* index in argument, is incremented */
1539 int def; /* default value */
1540{
1541 if (vim_isdigit(p[*idx]))
1542 {
1543 def = atoi((char *)&(p[*idx]));
1544 while (vim_isdigit(p[*idx]))
1545 *idx = *idx + 1;
1546 }
1547 return def;
1548}
1549
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001550#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1551/*
1552 * Setup to use the current locale (for ctype() and many other things).
1553 */
1554 static void
1555init_locale()
1556{
1557 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001558
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001559# ifdef FEAT_GUI_GTK
1560 /* Tell Gtk not to change our locale settings. */
1561 gtk_disable_setlocale();
1562# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1564 /* Make sure strtod() uses a decimal point, not a comma. */
1565 setlocale(LC_NUMERIC, "C");
1566# endif
1567
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001568# ifdef WIN32
1569 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1570 * text while it's expecting text in the current locale. This call avoids
1571 * that. */
1572 setlocale(LC_CTYPE, "C");
1573# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001574
1575# ifdef FEAT_GETTEXT
1576 {
1577 int mustfree = FALSE;
1578 char_u *p;
1579
1580# ifdef DYNAMIC_GETTEXT
1581 /* Initialize the gettext library */
1582 dyn_libintl_init(NULL);
1583# endif
1584 /* expand_env() doesn't work yet, because chartab[] is not initialized
1585 * yet, call vim_getenv() directly */
1586 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1587 if (p != NULL && *p != NUL)
1588 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001589 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001590 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1591 }
1592 if (mustfree)
1593 vim_free(p);
1594 textdomain(VIMPACKAGE);
1595 }
1596# endif
1597}
1598#endif
1599
1600/*
1601 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1602 * If the executable name starts with "r" we disable shell commands.
1603 * If the next character is "e" we run in Easy mode.
1604 * If the next character is "g" we run the GUI version.
1605 * If the next characters are "view" we start in readonly mode.
1606 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1607 * If the next characters are "ex" we start in Ex mode. If it's followed
1608 * by "im" use improved Ex mode.
1609 */
1610 static void
1611parse_command_name(parmp)
1612 mparm_T *parmp;
1613{
1614 char_u *initstr;
1615
1616 initstr = gettail((char_u *)parmp->argv[0]);
1617
1618#ifdef MACOS_X_UNIX
1619 /* An issue has been seen when launching Vim in such a way that
1620 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1621 * executable or a symbolic link of it. Until this issue is resolved
1622 * we prohibit the GUI from being used.
1623 */
1624 if (STRCMP(initstr, parmp->argv[0]) == 0)
1625 disallow_gui = TRUE;
1626
1627 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001628 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001629#endif
1630
1631#ifdef FEAT_EVAL
1632 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001633 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001634#endif
1635
1636 if (TOLOWER_ASC(initstr[0]) == 'r')
1637 {
1638 restricted = TRUE;
1639 ++initstr;
1640 }
1641
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001642 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001643 if (TOLOWER_ASC(initstr[0]) == 'e'
1644 && (TOLOWER_ASC(initstr[1]) == 'v'
1645 || TOLOWER_ASC(initstr[1]) == 'g'))
1646 {
1647#ifdef FEAT_GUI
1648 gui.starting = TRUE;
1649#endif
1650 parmp->evim_mode = TRUE;
1651 ++initstr;
1652 }
1653
Bram Moolenaar806875d2008-09-18 18:57:10 +00001654 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1655 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001656 {
1657 main_start_gui();
1658#ifdef FEAT_GUI
1659 ++initstr;
1660#endif
1661 }
1662
1663 if (STRNICMP(initstr, "view", 4) == 0)
1664 {
1665 readonlymode = TRUE;
1666 curbuf->b_p_ro = TRUE;
1667 p_uc = 10000; /* don't update very often */
1668 initstr += 4;
1669 }
1670 else if (STRNICMP(initstr, "vim", 3) == 0)
1671 initstr += 3;
1672
1673 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1674 if (STRICMP(initstr, "diff") == 0)
1675 {
1676#ifdef FEAT_DIFF
1677 parmp->diff_mode = TRUE;
1678#else
1679 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1680 mch_errmsg("\n");
1681 mch_exit(2);
1682#endif
1683 }
1684
1685 if (STRNICMP(initstr, "ex", 2) == 0)
1686 {
1687 if (STRNICMP(initstr + 2, "im", 2) == 0)
1688 exmode_active = EXMODE_VIM;
1689 else
1690 exmode_active = EXMODE_NORMAL;
1691 change_compatible(TRUE); /* set 'compatible' */
1692 }
1693}
1694
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001695/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001696 * Get the name of the display, before gui_prepare() removes it from
1697 * argv[]. Used for the xterm-clipboard display.
1698 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001699 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001700 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001701 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001702early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001703 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001704{
Bram Moolenaar03005972008-11-20 13:12:36 +00001705#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1706 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001707 int argc = parmp->argc;
1708 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001709 int i;
1710
1711 for (i = 1; i < argc; i++)
1712 {
1713 if (STRCMP(argv[i], "--") == 0)
1714 break;
1715# ifdef FEAT_XCLIPBOARD
1716 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001717# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001718 || STRICMP(argv[i], "--display") == 0
1719# endif
1720 )
1721 {
1722 if (i == argc - 1)
1723 mainerr_arg_missing((char_u *)argv[i]);
1724 xterm_display = argv[++i];
1725 }
1726# endif
1727# ifdef FEAT_CLIENTSERVER
1728 else if (STRICMP(argv[i], "--servername") == 0)
1729 {
1730 if (i == argc - 1)
1731 mainerr_arg_missing((char_u *)argv[i]);
1732 parmp->serverName_arg = (char_u *)argv[++i];
1733 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001734 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001735 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001736 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001737 {
1738 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001739# ifdef FEAT_GUI
1740 if (strstr(argv[i], "-wait") != 0)
1741 /* don't fork() when starting the GUI to edit files ourself */
1742 gui.dofork = FALSE;
1743# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001744 }
1745# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001746
1747# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1748# ifdef FEAT_GUI_W32
1749 else if (STRICMP(argv[i], "--windowid") == 0)
1750# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001751 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001752# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001753 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001754 long_u id;
1755 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001756
1757 if (i == argc - 1)
1758 mainerr_arg_missing((char_u *)argv[i]);
1759 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001760 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001761 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001762 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001763 if (count != 1)
1764 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1765 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001766# ifdef FEAT_GUI_W32
1767 win_socket_id = id;
1768# else
1769 gtk_socket_id = id;
1770# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001771 i++;
1772 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001773# endif
1774# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001775 else if (STRICMP(argv[i], "--echo-wid") == 0)
1776 echo_wid_arg = TRUE;
1777# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001778# ifndef FEAT_NETBEANS_INTG
1779 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001780 {
1781 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1782 mch_exit(2);
1783 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001784# endif
1785
Bram Moolenaar58d98232005-07-23 22:25:46 +00001786 }
1787#endif
1788}
1789
1790/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001791 * Scan the command line arguments.
1792 */
1793 static void
1794command_line_scan(parmp)
1795 mparm_T *parmp;
1796{
1797 int argc = parmp->argc;
1798 char **argv = parmp->argv;
1799 int argv_idx; /* index in argv[n][] */
1800 int had_minmin = FALSE; /* found "--" argument */
1801 int want_argument; /* option argument with argument */
1802 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001803 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001804 long n;
1805
1806 --argc;
1807 ++argv;
1808 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1809 while (argc > 0)
1810 {
1811 /*
1812 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1813 */
1814 if (argv[0][0] == '+' && !had_minmin)
1815 {
1816 if (parmp->n_commands >= MAX_ARG_CMDS)
1817 mainerr(ME_EXTRA_CMD, NULL);
1818 argv_idx = -1; /* skip to next argument */
1819 if (argv[0][1] == NUL)
1820 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1821 else
1822 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1823 }
1824
1825 /*
1826 * Optional argument.
1827 */
1828 else if (argv[0][0] == '-' && !had_minmin)
1829 {
1830 want_argument = FALSE;
1831 c = argv[0][argv_idx++];
1832#ifdef VMS
1833 /*
1834 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1835 * and "-/X" as "-X".
1836 */
1837 if (c == '/')
1838 {
1839 c = argv[0][argv_idx++];
1840 c = TOUPPER_ASC(c);
1841 }
1842 else
1843 c = TOLOWER_ASC(c);
1844#endif
1845 switch (c)
1846 {
1847 case NUL: /* "vim -" read from stdin */
1848 /* "ex -" silent mode */
1849 if (exmode_active)
1850 silent_mode = TRUE;
1851 else
1852 {
1853 if (parmp->edit_type != EDIT_NONE)
1854 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1855 parmp->edit_type = EDIT_STDIN;
1856 read_cmd_fd = 2; /* read from stderr instead of stdin */
1857 }
1858 argv_idx = -1; /* skip to next argument */
1859 break;
1860
1861 case '-': /* "--" don't take any more option arguments */
1862 /* "--help" give help message */
1863 /* "--version" give version message */
1864 /* "--literal" take files literally */
1865 /* "--nofork" don't fork */
1866 /* "--noplugin[s]" skip plugins */
1867 /* "--cmd <cmd>" execute cmd before vimrc */
1868 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1869 usage();
1870 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1871 {
1872 Columns = 80; /* need to init Columns */
1873 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1874 list_version();
1875 msg_putchar('\n');
1876 msg_didout = FALSE;
1877 mch_exit(0);
1878 }
1879 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1880 {
1881#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1882 parmp->literal = TRUE;
1883#endif
1884 }
1885 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1886 {
1887#ifdef FEAT_GUI
1888 gui.dofork = FALSE; /* don't fork() when starting GUI */
1889#endif
1890 }
1891 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1892 p_lpl = FALSE;
1893 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1894 {
1895 want_argument = TRUE;
1896 argv_idx += 3;
1897 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001898 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1899 {
1900 want_argument = TRUE;
1901 argv_idx += 11;
1902 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001903#ifdef FEAT_CLIENTSERVER
1904 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1905 ; /* already processed -- no arg */
1906 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1907 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1908 {
1909 /* already processed -- snatch the following arg */
1910 if (argc > 1)
1911 {
1912 --argc;
1913 ++argv;
1914 }
1915 }
1916#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001917#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1918# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001919 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001920# else
1921 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1922# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001923 {
1924 /* already processed -- snatch the following arg */
1925 if (argc > 1)
1926 {
1927 --argc;
1928 ++argv;
1929 }
1930 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001931#endif
1932#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001933 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1934 {
1935 /* already processed, skip */
1936 }
1937#endif
1938 else
1939 {
1940 if (argv[0][argv_idx])
1941 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1942 had_minmin = TRUE;
1943 }
1944 if (!want_argument)
1945 argv_idx = -1; /* skip to next argument */
1946 break;
1947
1948 case 'A': /* "-A" start in Arabic mode */
1949#ifdef FEAT_ARABIC
1950 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1951#else
1952 mch_errmsg(_(e_noarabic));
1953 mch_exit(2);
1954#endif
1955 break;
1956
1957 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001958 /* Needs to be effective before expanding file names, because
1959 * for Win32 this makes us edit a shortcut file itself,
1960 * instead of the file it links to. */
1961 set_options_bin(curbuf->b_p_bin, 1, 0);
1962 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001963 break;
1964
1965 case 'C': /* "-C" Compatible */
1966 change_compatible(TRUE);
1967 break;
1968
1969 case 'e': /* "-e" Ex mode */
1970 exmode_active = EXMODE_NORMAL;
1971 break;
1972
1973 case 'E': /* "-E" Improved Ex mode */
1974 exmode_active = EXMODE_VIM;
1975 break;
1976
1977 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1978 window directly, not with newcli */
1979#ifdef FEAT_GUI
1980 gui.dofork = FALSE; /* don't fork() when starting GUI */
1981#endif
1982 break;
1983
1984 case 'g': /* "-g" start GUI */
1985 main_start_gui();
1986 break;
1987
1988 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1989#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001990 p_fkmap = TRUE;
1991 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001992#else
1993 mch_errmsg(_(e_nofarsi));
1994 mch_exit(2);
1995#endif
1996 break;
1997
1998 case 'h': /* "-h" give help message */
1999#ifdef FEAT_GUI_GNOME
2000 /* Tell usage() to exit for "gvim". */
2001 gui.starting = FALSE;
2002#endif
2003 usage();
2004 break;
2005
2006 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2007#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002008 p_hkmap = TRUE;
2009 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002010#else
2011 mch_errmsg(_(e_nohebrew));
2012 mch_exit(2);
2013#endif
2014 break;
2015
2016 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2017#ifdef FEAT_LISP
2018 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2019 p_sm = TRUE;
2020#endif
2021 break;
2022
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002023 case 'M': /* "-M" no changes or writing of files */
2024 reset_modifiable();
2025 /* FALLTHROUGH */
2026
2027 case 'm': /* "-m" no writing of files */
2028 p_write = FALSE;
2029 break;
2030
2031 case 'y': /* "-y" easy mode */
2032#ifdef FEAT_GUI
2033 gui.starting = TRUE; /* start GUI a bit later */
2034#endif
2035 parmp->evim_mode = TRUE;
2036 break;
2037
2038 case 'N': /* "-N" Nocompatible */
2039 change_compatible(FALSE);
2040 break;
2041
2042 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002043#ifdef FEAT_NETBEANS_INTG
2044 /* checking for "-nb", netbeans parameters */
2045 if (argv[0][argv_idx] == 'b')
2046 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002047 netbeansArg = argv[0];
2048 argv_idx = -1; /* skip to next argument */
2049 }
2050 else
2051#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002052 parmp->no_swap_file = TRUE;
2053 break;
2054
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002055 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002056#ifdef TARGET_API_MAC_OSX
2057 /* For some reason on MacOS X, an argument like:
2058 -psn_0_10223617 is passed in when invoke from Finder
2059 or with the 'open' command */
2060 if (argv[0][argv_idx] == 's')
2061 {
2062 argv_idx = -1; /* bypass full -psn */
2063 main_start_gui();
2064 break;
2065 }
2066#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002067#ifdef FEAT_WINDOWS
2068 /* default is 0: open window for each file */
2069 parmp->window_count = get_number_arg((char_u *)argv[0],
2070 &argv_idx, 0);
2071 parmp->window_layout = WIN_TABS;
2072#endif
2073 break;
2074
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002075 case 'o': /* "-o[N]" open N horizontal split windows */
2076#ifdef FEAT_WINDOWS
2077 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002078 parmp->window_count = get_number_arg((char_u *)argv[0],
2079 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002080 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002081#endif
2082 break;
2083
2084 case 'O': /* "-O[N]" open N vertical split windows */
2085#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2086 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002087 parmp->window_count = get_number_arg((char_u *)argv[0],
2088 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002089 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002090#endif
2091 break;
2092
2093#ifdef FEAT_QUICKFIX
2094 case 'q': /* "-q" QuickFix mode */
2095 if (parmp->edit_type != EDIT_NONE)
2096 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2097 parmp->edit_type = EDIT_QF;
2098 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2099 {
2100 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2101 argv_idx = -1;
2102 }
2103 else if (argc > 1) /* "-q {errorfile}" */
2104 want_argument = TRUE;
2105 break;
2106#endif
2107
2108 case 'R': /* "-R" readonly mode */
2109 readonlymode = TRUE;
2110 curbuf->b_p_ro = TRUE;
2111 p_uc = 10000; /* don't update very often */
2112 break;
2113
2114 case 'r': /* "-r" recovery mode */
2115 case 'L': /* "-L" recovery mode */
2116 recoverymode = 1;
2117 break;
2118
2119 case 's':
2120 if (exmode_active) /* "-s" silent (batch) mode */
2121 silent_mode = TRUE;
2122 else /* "-s {scriptin}" read from script file */
2123 want_argument = TRUE;
2124 break;
2125
2126 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2127 if (parmp->edit_type != EDIT_NONE)
2128 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2129 parmp->edit_type = EDIT_TAG;
2130 if (argv[0][argv_idx]) /* "-t{tag}" */
2131 {
2132 parmp->tagname = (char_u *)argv[0] + argv_idx;
2133 argv_idx = -1;
2134 }
2135 else /* "-t {tag}" */
2136 want_argument = TRUE;
2137 break;
2138
2139#ifdef FEAT_EVAL
2140 case 'D': /* "-D" Debugging */
2141 parmp->use_debug_break_level = 9999;
2142 break;
2143#endif
2144#ifdef FEAT_DIFF
2145 case 'd': /* "-d" 'diff' */
2146# ifdef AMIGA
2147 /* check for "-dev {device}" */
2148 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2149 want_argument = TRUE;
2150 else
2151# endif
2152 parmp->diff_mode = TRUE;
2153 break;
2154#endif
2155 case 'V': /* "-V{N}" Verbose level */
2156 /* default is 10: a little bit verbose */
2157 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2158 if (argv[0][argv_idx] != NUL)
2159 {
2160 set_option_value((char_u *)"verbosefile", 0L,
2161 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002162 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002163 }
2164 break;
2165
2166 case 'v': /* "-v" Vi-mode (as if called "vi") */
2167 exmode_active = 0;
2168#ifdef FEAT_GUI
2169 gui.starting = FALSE; /* don't start GUI */
2170#endif
2171 break;
2172
2173 case 'w': /* "-w{number}" set window height */
2174 /* "-w {scriptout}" write to script */
2175 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2176 {
2177 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2178 set_option_value((char_u *)"window", n, NULL, 0);
2179 break;
2180 }
2181 want_argument = TRUE;
2182 break;
2183
2184#ifdef FEAT_CRYPT
2185 case 'x': /* "-x" encrypted reading/writing of files */
2186 parmp->ask_for_key = TRUE;
2187 break;
2188#endif
2189
2190 case 'X': /* "-X" don't connect to X server */
2191#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2192 x_no_connect = TRUE;
2193#endif
2194 break;
2195
2196 case 'Z': /* "-Z" restricted mode */
2197 restricted = TRUE;
2198 break;
2199
2200 case 'c': /* "-c{command}" or "-c {command}" execute
2201 command */
2202 if (argv[0][argv_idx] != NUL)
2203 {
2204 if (parmp->n_commands >= MAX_ARG_CMDS)
2205 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002206 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2207 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002208 argv_idx = -1;
2209 break;
2210 }
2211 /*FALLTHROUGH*/
2212 case 'S': /* "-S {file}" execute Vim script */
2213 case 'i': /* "-i {viminfo}" use for viminfo */
2214#ifndef FEAT_DIFF
2215 case 'd': /* "-d {device}" device (for Amiga) */
2216#endif
2217 case 'T': /* "-T {terminal}" terminal name */
2218 case 'u': /* "-u {vimrc}" vim inits file */
2219 case 'U': /* "-U {gvimrc}" gvim inits file */
2220 case 'W': /* "-W {scriptout}" overwrite */
2221#ifdef FEAT_GUI_W32
2222 case 'P': /* "-P {parent title}" MDI parent */
2223#endif
2224 want_argument = TRUE;
2225 break;
2226
2227 default:
2228 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2229 }
2230
2231 /*
2232 * Handle option arguments with argument.
2233 */
2234 if (want_argument)
2235 {
2236 /*
2237 * Check for garbage immediately after the option letter.
2238 */
2239 if (argv[0][argv_idx] != NUL)
2240 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2241
2242 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002243 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002244 mainerr_arg_missing((char_u *)argv[0]);
2245 ++argv;
2246 argv_idx = -1;
2247
2248 switch (c)
2249 {
2250 case 'c': /* "-c {command}" execute command */
2251 case 'S': /* "-S {file}" execute Vim script */
2252 if (parmp->n_commands >= MAX_ARG_CMDS)
2253 mainerr(ME_EXTRA_CMD, NULL);
2254 if (c == 'S')
2255 {
2256 char *a;
2257
2258 if (argc < 1)
2259 /* "-S" without argument: use default session file
2260 * name. */
2261 a = SESSION_FILE;
2262 else if (argv[0][0] == '-')
2263 {
2264 /* "-S" followed by another option: use default
2265 * session file name. */
2266 a = SESSION_FILE;
2267 ++argc;
2268 --argv;
2269 }
2270 else
2271 a = argv[0];
2272 p = alloc((unsigned)(STRLEN(a) + 4));
2273 if (p == NULL)
2274 mch_exit(2);
2275 sprintf((char *)p, "so %s", a);
2276 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2277 parmp->commands[parmp->n_commands++] = p;
2278 }
2279 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002280 parmp->commands[parmp->n_commands++] =
2281 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002282 break;
2283
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002284 case '-':
2285 if (argv[-1][2] == 'c')
2286 {
2287 /* "--cmd {command}" execute command */
2288 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2289 mainerr(ME_EXTRA_CMD, NULL);
2290 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002291 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002292 }
2293 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002294 break;
2295
2296 /* case 'd': -d {device} is handled in mch_check_win() for the
2297 * Amiga */
2298
2299#ifdef FEAT_QUICKFIX
2300 case 'q': /* "-q {errorfile}" QuickFix mode */
2301 parmp->use_ef = (char_u *)argv[0];
2302 break;
2303#endif
2304
2305 case 'i': /* "-i {viminfo}" use for viminfo */
2306 use_viminfo = (char_u *)argv[0];
2307 break;
2308
2309 case 's': /* "-s {scriptin}" read from script file */
2310 if (scriptin[0] != NULL)
2311 {
2312scripterror:
2313 mch_errmsg(_("Attempt to open script file again: \""));
2314 mch_errmsg(argv[-1]);
2315 mch_errmsg(" ");
2316 mch_errmsg(argv[0]);
2317 mch_errmsg("\"\n");
2318 mch_exit(2);
2319 }
2320 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2321 {
2322 mch_errmsg(_("Cannot open for reading: \""));
2323 mch_errmsg(argv[0]);
2324 mch_errmsg("\"\n");
2325 mch_exit(2);
2326 }
2327 if (save_typebuf() == FAIL)
2328 mch_exit(2); /* out of memory */
2329 break;
2330
2331 case 't': /* "-t {tag}" */
2332 parmp->tagname = (char_u *)argv[0];
2333 break;
2334
2335 case 'T': /* "-T {terminal}" terminal name */
2336 /*
2337 * The -T term argument is always available and when
2338 * HAVE_TERMLIB is supported it overrides the environment
2339 * variable TERM.
2340 */
2341#ifdef FEAT_GUI
2342 if (term_is_gui((char_u *)argv[0]))
2343 gui.starting = TRUE; /* start GUI a bit later */
2344 else
2345#endif
2346 parmp->term = (char_u *)argv[0];
2347 break;
2348
2349 case 'u': /* "-u {vimrc}" vim inits file */
2350 parmp->use_vimrc = (char_u *)argv[0];
2351 break;
2352
2353 case 'U': /* "-U {gvimrc}" gvim inits file */
2354#ifdef FEAT_GUI
2355 use_gvimrc = (char_u *)argv[0];
2356#endif
2357 break;
2358
2359 case 'w': /* "-w {nr}" 'window' value */
2360 /* "-w {scriptout}" append to script file */
2361 if (vim_isdigit(*((char_u *)argv[0])))
2362 {
2363 argv_idx = 0;
2364 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2365 set_option_value((char_u *)"window", n, NULL, 0);
2366 argv_idx = -1;
2367 break;
2368 }
2369 /*FALLTHROUGH*/
2370 case 'W': /* "-W {scriptout}" overwrite script file */
2371 if (scriptout != NULL)
2372 goto scripterror;
2373 if ((scriptout = mch_fopen(argv[0],
2374 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2375 {
2376 mch_errmsg(_("Cannot open for script output: \""));
2377 mch_errmsg(argv[0]);
2378 mch_errmsg("\"\n");
2379 mch_exit(2);
2380 }
2381 break;
2382
2383#ifdef FEAT_GUI_W32
2384 case 'P': /* "-P {parent title}" MDI parent */
2385 gui_mch_set_parent(argv[0]);
2386 break;
2387#endif
2388 }
2389 }
2390 }
2391
2392 /*
2393 * File name argument.
2394 */
2395 else
2396 {
2397 argv_idx = -1; /* skip to next argument */
2398
2399 /* Check for only one type of editing. */
2400 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2401 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2402 parmp->edit_type = EDIT_FILE;
2403
2404#ifdef MSWIN
2405 /* Remember if the argument was a full path before changing
2406 * slashes to backslashes. */
2407 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2408 parmp->full_path = TRUE;
2409#endif
2410
2411 /* Add the file to the global argument list. */
2412 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2413 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2414 mch_exit(2);
2415#ifdef FEAT_DIFF
2416 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2417 && !mch_isdir(alist_name(&GARGLIST[0])))
2418 {
2419 char_u *r;
2420
2421 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2422 if (r != NULL)
2423 {
2424 vim_free(p);
2425 p = r;
2426 }
2427 }
2428#endif
2429#if defined(__CYGWIN32__) && !defined(WIN32)
2430 /*
2431 * If vim is invoked by non-Cygwin tools, convert away any
2432 * DOS paths, so things like .swp files are created correctly.
2433 * Look for evidence of non-Cygwin paths before we bother.
2434 * This is only for when using the Unix files.
2435 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002436 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002437 {
2438 char posix_path[PATH_MAX];
2439
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002440# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2441 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2442# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002444# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002445 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002446 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002447 if (p == NULL)
2448 mch_exit(2);
2449 }
2450#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002451
2452#ifdef USE_FNAME_CASE
2453 /* Make the case of the file name match the actual file. */
2454 fname_case(p, 0);
2455#endif
2456
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002457 alist_add(&global_alist, p,
2458#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002459 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460#else
2461 2 /* add buffer number now and use curbuf */
2462#endif
2463 );
2464
2465#if defined(FEAT_MBYTE) && defined(WIN32)
2466 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002467 /* Remember this argument has been added to the argument list.
2468 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002469 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002470# ifdef FEAT_DIFF
2471 parmp->diff_mode
2472# else
2473 FALSE
2474# endif
2475 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002476 }
2477#endif
2478 }
2479
2480 /*
2481 * If there are no more letters after the current "-", go to next
2482 * argument. argv_idx is set to -1 when the current argument is to be
2483 * skipped.
2484 */
2485 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2486 {
2487 --argc;
2488 ++argv;
2489 argv_idx = 1;
2490 }
2491 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002492
2493#ifdef FEAT_EVAL
2494 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2495 * one. */
2496 if (parmp->n_commands > 0)
2497 {
2498 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2499 if (p != NULL)
2500 {
2501 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2502 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2503 vim_free(p);
2504 }
2505 }
2506#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002507}
2508
2509/*
2510 * Print a warning if stdout is not a terminal.
2511 * When starting in Ex mode and commands come from a file, set Silent mode.
2512 */
2513 static void
2514check_tty(parmp)
2515 mparm_T *parmp;
2516{
2517 int input_isatty; /* is active input a terminal? */
2518
2519 input_isatty = mch_input_isatty();
2520 if (exmode_active)
2521 {
2522 if (!input_isatty)
2523 silent_mode = TRUE;
2524 }
2525 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2526#ifdef FEAT_GUI
2527 /* don't want the delay when started from the desktop */
2528 && !gui.starting
2529#endif
2530 )
2531 {
2532#ifdef NBDEBUG
2533 /*
2534 * This shouldn't be necessary. But if I run netbeans with the log
2535 * output coming to the console and XOpenDisplay fails, I get vim
2536 * trying to start with input/output to my console tty. This fills my
2537 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002538 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002539 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002540 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 {
2542 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2543 exit(1);
2544 }
2545#endif
2546 if (!parmp->stdout_isatty)
2547 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2548 if (!input_isatty)
2549 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2550 out_flush();
2551 if (scriptin[0] == NULL)
2552 ui_delay(2000L, TRUE);
2553 TIME_MSG("Warning delay");
2554 }
2555}
2556
2557/*
2558 * Read text from stdin.
2559 */
2560 static void
2561read_stdin()
2562{
2563 int i;
2564
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002565#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002566 /* When getting the ATTENTION prompt here, use a dialog */
2567 swap_exists_action = SEA_DIALOG;
2568#endif
2569 no_wait_return = TRUE;
2570 i = msg_didany;
2571 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002572 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573 no_wait_return = FALSE;
2574 msg_didany = i;
2575 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002576#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002577 check_swap_exists_action();
2578#endif
2579#if !(defined(AMIGA) || defined(MACOS))
2580 /*
2581 * Close stdin and dup it from stderr. Required for GPM to work
2582 * properly, and for running external commands.
2583 * Is there any other system that cannot do this?
2584 */
2585 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002586 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002587#endif
2588}
2589
2590/*
2591 * Create the requested number of windows and edit buffers in them.
2592 * Also does recovery if "recoverymode" set.
2593 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002594 static void
2595create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002596 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597{
2598#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002599 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002600 int done = 0;
2601
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002602 /*
2603 * Create the number of windows that was requested.
2604 */
2605 if (parmp->window_count == -1) /* was not set */
2606 parmp->window_count = 1;
2607 if (parmp->window_count == 0)
2608 parmp->window_count = GARGCOUNT;
2609 if (parmp->window_count > 1)
2610 {
2611 /* Don't change the windows if there was a command in .vimrc that
2612 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002613 if (parmp->window_layout == 0)
2614 parmp->window_layout = WIN_HOR;
2615 if (parmp->window_layout == WIN_TABS)
2616 {
2617 parmp->window_count = make_tabpages(parmp->window_count);
2618 TIME_MSG("making tab pages");
2619 }
2620 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621 {
2622 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002623 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002624 TIME_MSG("making windows");
2625 }
2626 else
2627 parmp->window_count = win_count();
2628 }
2629 else
2630 parmp->window_count = 1;
2631#endif
2632
2633 if (recoverymode) /* do recover */
2634 {
2635 msg_scroll = TRUE; /* scroll message up */
2636 ml_recover();
2637 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2638 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002639 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002640 }
2641 else
2642 {
2643 /*
2644 * Open a buffer for windows that don't have one yet.
2645 * Commands in the .vimrc might have loaded a file or split the window.
2646 * Watch out for autocommands that delete a window.
2647 */
2648#ifdef FEAT_AUTOCMD
2649 /*
2650 * Don't execute Win/Buf Enter/Leave autocommands here
2651 */
2652 ++autocmd_no_enter;
2653 ++autocmd_no_leave;
2654#endif
2655#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002656 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002657 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002658 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002659 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002660 {
2661 if (parmp->window_layout == WIN_TABS)
2662 goto_tabpage(1);
2663 else
2664 curwin = firstwin;
2665 }
2666 else if (parmp->window_layout == WIN_TABS)
2667 {
2668 if (curtab->tp_next == NULL)
2669 break;
2670 goto_tabpage(0);
2671 }
2672 else
2673 {
2674 if (curwin->w_next == NULL)
2675 break;
2676 curwin = curwin->w_next;
2677 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002678 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002679#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002680 curbuf = curwin->w_buffer;
2681 if (curbuf->b_ml.ml_mfp == NULL)
2682 {
2683#ifdef FEAT_FOLDING
2684 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2685 if (p_fdls >= 0)
2686 curwin->w_p_fdl = p_fdls;
2687#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002688#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002689 /* When getting the ATTENTION prompt here, use a dialog */
2690 swap_exists_action = SEA_DIALOG;
2691#endif
2692 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002693
2694 /* create memfile, read file */
2695 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002696
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002697#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002698 if (swap_exists_action == SEA_QUIT)
2699 {
2700 if (got_int || only_one_window())
2701 {
2702 /* abort selected or quit and only one window */
2703 did_emsg = FALSE; /* avoid hit-enter prompt */
2704 getout(1);
2705 }
2706 /* We can't close the window, it would disturb what
2707 * happens next. Clear the file name and set the arg
2708 * index to -1 to delete it later. */
2709 setfname(curbuf, NULL, NULL, FALSE);
2710 curwin->w_arg_idx = -1;
2711 swap_exists_action = SEA_NONE;
2712 }
2713 else
2714 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002715#endif
2716#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002717 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718#endif
2719 }
2720#ifdef FEAT_WINDOWS
2721 ui_breakcheck();
2722 if (got_int)
2723 {
2724 (void)vgetc(); /* only break the file loading, not the rest */
2725 break;
2726 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002728#endif
2729#ifdef FEAT_WINDOWS
2730 if (parmp->window_layout == WIN_TABS)
2731 goto_tabpage(1);
2732 else
2733 curwin = firstwin;
2734 curbuf = curwin->w_buffer;
2735#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002736#ifdef FEAT_AUTOCMD
2737 --autocmd_no_enter;
2738 --autocmd_no_leave;
2739#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002740 }
2741}
2742
2743#ifdef FEAT_WINDOWS
2744 /*
2745 * If opened more than one window, start editing files in the other
2746 * windows. make_windows() has already opened the windows.
2747 */
2748 static void
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002749edit_buffers(parmp, cwd)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002750 mparm_T *parmp;
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002751 char_u *cwd; /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752{
2753 int arg_idx; /* index in argument list */
2754 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002755 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002756 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757
2758# ifdef FEAT_AUTOCMD
2759 /*
2760 * Don't execute Win/Buf Enter/Leave autocommands here
2761 */
2762 ++autocmd_no_enter;
2763 ++autocmd_no_leave;
2764# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002765
2766 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2767 if (curwin->w_arg_idx == -1)
2768 {
2769 win_close(curwin, TRUE);
2770 advance = FALSE;
2771 }
2772
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773 arg_idx = 1;
2774 for (i = 1; i < parmp->window_count; ++i)
2775 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002776 if (cwd != NULL)
2777 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002778 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2779 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002780 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002781 ++arg_idx;
2782 win_close(curwin, TRUE);
2783 advance = FALSE;
2784 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002785 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002786
Bram Moolenaar84212822006-11-07 21:59:47 +00002787 if (advance)
2788 {
2789 if (parmp->window_layout == WIN_TABS)
2790 {
2791 if (curtab->tp_next == NULL) /* just checking */
2792 break;
2793 goto_tabpage(0);
2794 }
2795 else
2796 {
2797 if (curwin->w_next == NULL) /* just checking */
2798 break;
2799 win_enter(curwin->w_next, FALSE);
2800 }
2801 }
2802 advance = TRUE;
2803
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002804 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002805 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002806 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2807 {
2808 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002809 /* Edit file from arg list, if there is one. When "Quit" selected
2810 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002811# ifdef HAS_SWAP_EXISTS_ACTION
2812 swap_exists_did_quit = FALSE;
2813# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002814 (void)do_ecmd(0, arg_idx < GARGCOUNT
2815 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002816 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002817# ifdef HAS_SWAP_EXISTS_ACTION
2818 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002819 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002820 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002821 if (got_int || only_one_window())
2822 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002823 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002824 did_emsg = FALSE; /* avoid hit-enter prompt */
2825 getout(1);
2826 }
2827 win_close(curwin, TRUE);
2828 advance = FALSE;
2829 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002830# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002831 if (arg_idx == GARGCOUNT - 1)
2832 arg_had_last = TRUE;
2833 ++arg_idx;
2834 }
2835 ui_breakcheck();
2836 if (got_int)
2837 {
2838 (void)vgetc(); /* only break the file loading, not the rest */
2839 break;
2840 }
2841 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002842
2843 if (parmp->window_layout == WIN_TABS)
2844 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002845# ifdef FEAT_AUTOCMD
2846 --autocmd_no_enter;
2847# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002848
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002849 /* make the first window the current window */
2850 win = firstwin;
2851#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2852 /* Avoid making a preview window the current window. */
2853 while (win->w_p_pvw)
2854 {
2855 win = win->w_next;
2856 if (win == NULL)
2857 {
2858 win = firstwin;
2859 break;
2860 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002861 }
2862#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002863 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002864
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002865# ifdef FEAT_AUTOCMD
2866 --autocmd_no_leave;
2867# endif
2868 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002869 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002870 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2871}
2872#endif /* FEAT_WINDOWS */
2873
2874/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002875 * Execute the commands from --cmd arguments "cmds[cnt]".
2876 */
2877 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002878exe_pre_commands(parmp)
2879 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002880{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002881 char_u **cmds = parmp->pre_commands;
2882 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002883 int i;
2884
2885 if (cnt > 0)
2886 {
2887 curwin->w_cursor.lnum = 0; /* just in case.. */
2888 sourcing_name = (char_u *)_("pre-vimrc command line");
2889# ifdef FEAT_EVAL
2890 current_SID = SID_CMDARG;
2891# endif
2892 for (i = 0; i < cnt; ++i)
2893 do_cmdline_cmd(cmds[i]);
2894 sourcing_name = NULL;
2895# ifdef FEAT_EVAL
2896 current_SID = 0;
2897# endif
2898 TIME_MSG("--cmd commands");
2899 }
2900}
2901
2902/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002903 * Execute "+", "-c" and "-S" arguments.
2904 */
2905 static void
2906exe_commands(parmp)
2907 mparm_T *parmp;
2908{
2909 int i;
2910
2911 /*
2912 * We start commands on line 0, make "vim +/pat file" match a
2913 * pattern on line 1. But don't move the cursor when an autocommand
2914 * with g`" was used.
2915 */
2916 msg_scroll = TRUE;
2917 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2918 curwin->w_cursor.lnum = 0;
2919 sourcing_name = (char_u *)"command line";
2920#ifdef FEAT_EVAL
2921 current_SID = SID_CARG;
2922#endif
2923 for (i = 0; i < parmp->n_commands; ++i)
2924 {
2925 do_cmdline_cmd(parmp->commands[i]);
2926 if (parmp->cmds_tofree[i])
2927 vim_free(parmp->commands[i]);
2928 }
2929 sourcing_name = NULL;
2930#ifdef FEAT_EVAL
2931 current_SID = 0;
2932#endif
2933 if (curwin->w_cursor.lnum == 0)
2934 curwin->w_cursor.lnum = 1;
2935
2936 if (!exmode_active)
2937 msg_scroll = FALSE;
2938
2939#ifdef FEAT_QUICKFIX
2940 /* When started with "-q errorfile" jump to first error again. */
2941 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002942 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002943#endif
2944 TIME_MSG("executing command arguments");
2945}
2946
2947/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002948 * Source startup scripts.
2949 */
2950 static void
2951source_startup_scripts(parmp)
2952 mparm_T *parmp;
2953{
2954 int i;
2955
2956 /*
2957 * For "evim" source evim.vim first of all, so that the user can overrule
2958 * any things he doesn't like.
2959 */
2960 if (parmp->evim_mode)
2961 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002962 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002963 TIME_MSG("source evim file");
2964 }
2965
2966 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002967 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002968 * nothing else.
2969 */
2970 if (parmp->use_vimrc != NULL)
2971 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002972 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2973 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002974 {
2975#ifdef FEAT_GUI
2976 if (use_gvimrc == NULL) /* don't load gvimrc either */
2977 use_gvimrc = parmp->use_vimrc;
2978#endif
2979 if (parmp->use_vimrc[2] == 'N')
2980 p_lpl = FALSE; /* don't load plugins either */
2981 }
2982 else
2983 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002984 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002985 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2986 }
2987 }
2988 else if (!silent_mode)
2989 {
2990#ifdef AMIGA
2991 struct Process *proc = (struct Process *)FindTask(0L);
2992 APTR save_winptr = proc->pr_WindowPtr;
2993
2994 /* Avoid a requester here for a volume that doesn't exist. */
2995 proc->pr_WindowPtr = (APTR)-1L;
2996#endif
2997
2998 /*
2999 * Get system wide defaults, if the file name is defined.
3000 */
3001#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003002 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003003#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003004#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003005 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003006#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003007
3008 /*
3009 * Try to read initialization commands from the following places:
3010 * - environment variable VIMINIT
3011 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3012 * - second user vimrc file ($VIM/.vimrc for Dos)
3013 * - environment variable EXINIT
3014 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3015 * - second user exrc file ($VIM/.exrc for Dos)
3016 * The first that exists is used, the rest is ignored.
3017 */
3018 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3019 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003020 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003021#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003022 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3023 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003024#endif
3025#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003026 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3027 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003028#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003029#ifdef USR_VIMRC_FILE4
3030 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3031 DOSO_VIMRC) == FAIL
3032#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003033 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003034 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003035 {
3036#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003037 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003038#endif
3039 }
3040 }
3041
3042 /*
3043 * Read initialization commands from ".vimrc" or ".exrc" in current
3044 * directory. This is only done if the 'exrc' option is set.
3045 * Because of security reasons we disallow shell and write commands
3046 * now, except for unix if the file is owned by the user or 'secure'
3047 * option has been reset in environment of global ".exrc" or ".vimrc".
3048 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3049 * SYS_VIMRC_FILE.
3050 */
3051 if (p_exrc)
3052 {
3053#if defined(UNIX) || defined(VMS)
3054 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3055 if (!file_owned(VIMRC_FILE))
3056#endif
3057 secure = p_secure;
3058
3059 i = FAIL;
3060 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3061 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3062#ifdef USR_VIMRC_FILE2
3063 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3064 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3065#endif
3066#ifdef USR_VIMRC_FILE3
3067 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3068 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3069#endif
3070#ifdef SYS_VIMRC_FILE
3071 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3072 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3073#endif
3074 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003075 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003076
3077 if (i == FAIL)
3078 {
3079#if defined(UNIX) || defined(VMS)
3080 /* if ".exrc" is not owned by user set 'secure' mode */
3081 if (!file_owned(EXRC_FILE))
3082 secure = p_secure;
3083 else
3084 secure = 0;
3085#endif
3086 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3087 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3088#ifdef USR_EXRC_FILE2
3089 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3090 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3091#endif
3092 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003093 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003094 }
3095 }
3096 if (secure == 2)
3097 need_wait_return = TRUE;
3098 secure = 0;
3099#ifdef AMIGA
3100 proc->pr_WindowPtr = save_winptr;
3101#endif
3102 }
3103 TIME_MSG("sourcing vimrc file(s)");
3104}
3105
3106/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003107 * Setup to start using the GUI. Exit with an error when not available.
3108 */
3109 static void
3110main_start_gui()
3111{
3112#ifdef FEAT_GUI
3113 gui.starting = TRUE; /* start GUI a bit later */
3114#else
3115 mch_errmsg(_(e_nogvim));
3116 mch_errmsg("\n");
3117 mch_exit(2);
3118#endif
3119}
3120
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003121#endif /* NO_VIM_MAIN */
3122
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003123/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003124 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003125 * Returns FAIL if the environment variable was not executed, OK otherwise.
3126 */
3127 int
3128process_env(env, is_viminit)
3129 char_u *env;
3130 int is_viminit; /* when TRUE, called for VIMINIT */
3131{
3132 char_u *initstr;
3133 char_u *save_sourcing_name;
3134 linenr_T save_sourcing_lnum;
3135#ifdef FEAT_EVAL
3136 scid_T save_sid;
3137#endif
3138
3139 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3140 {
3141 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003142 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003143 save_sourcing_name = sourcing_name;
3144 save_sourcing_lnum = sourcing_lnum;
3145 sourcing_name = env;
3146 sourcing_lnum = 0;
3147#ifdef FEAT_EVAL
3148 save_sid = current_SID;
3149 current_SID = SID_ENV;
3150#endif
3151 do_cmdline_cmd(initstr);
3152 sourcing_name = save_sourcing_name;
3153 sourcing_lnum = save_sourcing_lnum;
3154#ifdef FEAT_EVAL
3155 current_SID = save_sid;;
3156#endif
3157 return OK;
3158 }
3159 return FAIL;
3160}
3161
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003162#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003163/*
3164 * Return TRUE if we are certain the user owns the file "fname".
3165 * Used for ".vimrc" and ".exrc".
3166 * Use both stat() and lstat() for extra security.
3167 */
3168 static int
3169file_owned(fname)
3170 char *fname;
3171{
3172 struct stat s;
3173# ifdef UNIX
3174 uid_t uid = getuid();
3175# else /* VMS */
3176 uid_t uid = ((getgid() << 16) | getuid());
3177# endif
3178
3179 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3180# ifdef HAVE_LSTAT
3181 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3182# endif
3183 );
3184}
3185#endif
3186
3187/*
3188 * Give an error message main_errors["n"] and exit.
3189 */
3190 static void
3191mainerr(n, str)
3192 int n; /* one of the ME_ defines */
3193 char_u *str; /* extra argument or NULL */
3194{
3195#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3196 reset_signals(); /* kill us with CTRL-C here, if you like */
3197#endif
3198
3199 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003200 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003201 mch_errmsg(_(main_errors[n]));
3202 if (str != NULL)
3203 {
3204 mch_errmsg(": \"");
3205 mch_errmsg((char *)str);
3206 mch_errmsg("\"");
3207 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003208 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003209
3210 mch_exit(1);
3211}
3212
3213 void
3214mainerr_arg_missing(str)
3215 char_u *str;
3216{
3217 mainerr(ME_ARG_MISSING, str);
3218}
3219
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003220#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003221/*
3222 * print a message with three spaces prepended and '\n' appended.
3223 */
3224 static void
3225main_msg(s)
3226 char *s;
3227{
3228 mch_msg(" ");
3229 mch_msg(s);
3230 mch_msg("\n");
3231}
3232
3233/*
3234 * Print messages for "vim -h" or "vim --help" and exit.
3235 */
3236 static void
3237usage()
3238{
3239 int i;
3240 static char *(use[]) =
3241 {
3242 N_("[file ..] edit specified file(s)"),
3243 N_("- read text from stdin"),
3244 N_("-t tag edit file where tag is defined"),
3245#ifdef FEAT_QUICKFIX
3246 N_("-q [errorfile] edit file with first error")
3247#endif
3248 };
3249
3250#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3251 reset_signals(); /* kill us with CTRL-C here, if you like */
3252#endif
3253
3254 mch_msg(longVersion);
3255 mch_msg(_("\n\nusage:"));
3256 for (i = 0; ; ++i)
3257 {
3258 mch_msg(_(" vim [arguments] "));
3259 mch_msg(_(use[i]));
3260 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3261 break;
3262 mch_msg(_("\n or:"));
3263 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003264#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003265 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003266#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003267
3268 mch_msg(_("\n\nArguments:\n"));
3269 main_msg(_("--\t\t\tOnly file names after this"));
3270#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3271 main_msg(_("--literal\t\tDon't expand wildcards"));
3272#endif
3273#ifdef FEAT_OLE
3274 main_msg(_("-register\t\tRegister this gvim for OLE"));
3275 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3276#endif
3277#ifdef FEAT_GUI
3278 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3279 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3280#endif
3281 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3282 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003283 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003284 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3285#ifdef FEAT_DIFF
3286 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3287#endif
3288 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3289 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3290 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3291 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3292 main_msg(_("-M\t\t\tModifications in text not allowed"));
3293 main_msg(_("-b\t\t\tBinary mode"));
3294#ifdef FEAT_LISP
3295 main_msg(_("-l\t\t\tLisp mode"));
3296#endif
3297 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3298 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003299 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3300#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003301 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003302#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003303 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3304 main_msg(_("-r\t\t\tList swap files and exit"));
3305 main_msg(_("-r (with file name)\tRecover crashed session"));
3306 main_msg(_("-L\t\t\tSame as -r"));
3307#ifdef AMIGA
3308 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3309 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3310#endif
3311#ifdef FEAT_ARABIC
3312 main_msg(_("-A\t\t\tstart in Arabic mode"));
3313#endif
3314#ifdef FEAT_RIGHTLEFT
3315 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3316#endif
3317#ifdef FEAT_FKMAP
3318 main_msg(_("-F\t\t\tStart in Farsi mode"));
3319#endif
3320 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3321 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3322#ifdef FEAT_GUI
3323 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3324#endif
3325 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003326#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003327 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3329 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003330#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331 main_msg(_("+\t\t\tStart at end of file"));
3332 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003334 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3335 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3336 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3337 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3338 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3339#ifdef FEAT_CRYPT
3340 main_msg(_("-x\t\t\tEdit encrypted files"));
3341#endif
3342#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3343# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3344 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3345# endif
3346 main_msg(_("-X\t\t\tDo not connect to X server"));
3347#endif
3348#ifdef FEAT_CLIENTSERVER
3349 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3350 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3351 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3352 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003353# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003354 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003355# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003356 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3357 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3358 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3359 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3360#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003361#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003362 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003363#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003364#ifdef FEAT_VIMINFO
3365 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3366#endif
3367 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3368 main_msg(_("--version\t\tPrint version information and exit"));
3369
3370#ifdef FEAT_GUI_X11
3371# ifdef FEAT_GUI_MOTIF
3372 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3373# else
3374# ifdef FEAT_GUI_ATHENA
3375# ifdef FEAT_GUI_NEXTAW
3376 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3377# else
3378 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3379# endif
3380# endif
3381# endif
3382 main_msg(_("-display <display>\tRun vim on <display>"));
3383 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003384 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3385 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3386 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3387 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3388 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3389 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3390 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3391 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3392# ifdef FEAT_GUI_ATHENA
3393 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3394# endif
3395 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3396 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3397 main_msg(_("-xrm <resource>\tSet the specified resource"));
3398#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399#ifdef FEAT_GUI_GTK
3400 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3401 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3402 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3403 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3404 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003405 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003407 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003408#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409#ifdef FEAT_GUI_W32
3410 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003411 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003412#endif
3413
3414#ifdef FEAT_GUI_GNOME
3415 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3416 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003417 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003418 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003419 gui.dofork = FALSE;
3420 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421 else
3422#endif
3423 mch_exit(0);
3424}
3425
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003426#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003427/*
3428 * Check the result of the ATTENTION dialog:
3429 * When "Quit" selected, exit Vim.
3430 * When "Recover" selected, recover the file.
3431 */
3432 static void
3433check_swap_exists_action()
3434{
3435 if (swap_exists_action == SEA_QUIT)
3436 getout(1);
3437 handle_swap_exists(NULL);
3438}
3439#endif
3440
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003441#endif
3442
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003443#if defined(STARTUPTIME) || defined(PROTO)
3444static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3445
3446static struct timeval prev_timeval;
3447
Bram Moolenaar3f269672009-11-03 11:11:11 +00003448# ifdef WIN3264
3449/*
3450 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3451 */
3452 static int
3453gettimeofday(struct timeval *tv, char *dummy)
3454{
3455 long t = clock();
3456 tv->tv_sec = t / CLOCKS_PER_SEC;
3457 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3458 return 0;
3459}
3460# endif
3461
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003462/*
3463 * Save the previous time before doing something that could nest.
3464 * set "*tv_rel" to the time elapsed so far.
3465 */
3466 void
3467time_push(tv_rel, tv_start)
3468 void *tv_rel, *tv_start;
3469{
3470 *((struct timeval *)tv_rel) = prev_timeval;
3471 gettimeofday(&prev_timeval, NULL);
3472 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3473 - ((struct timeval *)tv_rel)->tv_usec;
3474 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3475 - ((struct timeval *)tv_rel)->tv_sec;
3476 if (((struct timeval *)tv_rel)->tv_usec < 0)
3477 {
3478 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3479 --((struct timeval *)tv_rel)->tv_sec;
3480 }
3481 *(struct timeval *)tv_start = prev_timeval;
3482}
3483
3484/*
3485 * Compute the previous time after doing something that could nest.
3486 * Subtract "*tp" from prev_timeval;
3487 * Note: The arguments are (void *) to avoid trouble with systems that don't
3488 * have struct timeval.
3489 */
3490 void
3491time_pop(tp)
3492 void *tp; /* actually (struct timeval *) */
3493{
3494 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3495 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3496 if (prev_timeval.tv_usec < 0)
3497 {
3498 prev_timeval.tv_usec += 1000000;
3499 --prev_timeval.tv_sec;
3500 }
3501}
3502
3503 static void
3504time_diff(then, now)
3505 struct timeval *then;
3506 struct timeval *now;
3507{
3508 long usec;
3509 long msec;
3510
3511 usec = now->tv_usec - then->tv_usec;
3512 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3513 usec = usec % 1000L;
3514 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3515}
3516
3517 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003518time_msg(mesg, tv_start)
3519 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520 void *tv_start; /* only for do_source: start time; actually
3521 (struct timeval *) */
3522{
3523 static struct timeval start;
3524 struct timeval now;
3525
3526 if (time_fd != NULL)
3527 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003528 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 {
3530 gettimeofday(&start, NULL);
3531 prev_timeval = start;
3532 fprintf(time_fd, "\n\ntimes in msec\n");
3533 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3534 fprintf(time_fd, " clock elapsed: other lines\n\n");
3535 }
3536 gettimeofday(&now, NULL);
3537 time_diff(&start, &now);
3538 if (((struct timeval *)tv_start) != NULL)
3539 {
3540 fprintf(time_fd, " ");
3541 time_diff(((struct timeval *)tv_start), &now);
3542 }
3543 fprintf(time_fd, " ");
3544 time_diff(&prev_timeval, &now);
3545 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003546 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003547 }
3548}
3549
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003550#endif
3551
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003552#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003553
3554/*
3555 * Common code for the X command server and the Win32 command server.
3556 */
3557
Bram Moolenaareb94e552006-03-11 21:35:11 +00003558static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003559
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003560/*
3561 * Do the client-server stuff, unless "--servername ''" was used.
3562 */
3563 static void
3564exec_on_server(parmp)
3565 mparm_T *parmp;
3566{
3567 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3568 {
3569# ifdef WIN32
3570 /* Initialise the client/server messaging infrastructure. */
3571 serverInitMessaging();
3572# endif
3573
3574 /*
3575 * When a command server argument was found, execute it. This may
3576 * exit Vim when it was successful. Otherwise it's executed further
3577 * on. Remember the encoding used here in "serverStrEnc".
3578 */
3579 if (parmp->serverArg)
3580 {
3581 cmdsrv_main(&parmp->argc, parmp->argv,
3582 parmp->serverName_arg, &parmp->serverStr);
3583# ifdef FEAT_MBYTE
3584 parmp->serverStrEnc = vim_strsave(p_enc);
3585# endif
3586 }
3587
3588 /* If we're still running, get the name to register ourselves.
3589 * On Win32 can register right now, for X11 need to setup the
3590 * clipboard first, it's further down. */
3591 parmp->servername = serverMakeName(parmp->serverName_arg,
3592 parmp->argv[0]);
3593# ifdef WIN32
3594 if (parmp->servername != NULL)
3595 {
3596 serverSetName(parmp->servername);
3597 vim_free(parmp->servername);
3598 }
3599# endif
3600 }
3601}
3602
3603/*
3604 * Prepare for running as a Vim server.
3605 */
3606 static void
3607prepare_server(parmp)
3608 mparm_T *parmp;
3609{
3610# if defined(FEAT_X11)
3611 /*
3612 * Register for remote command execution with :serversend and --remote
3613 * unless there was a -X or a --servername '' on the command line.
3614 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003615 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003616 */
3617 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3618# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003619 (gui.in_use
3620# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003621 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003622# endif
3623 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003624# endif
3625 parmp->serverName_arg != NULL))
3626 {
3627 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3628 vim_free(parmp->servername);
3629 TIME_MSG("register server name");
3630 }
3631 else
3632 serverDelayedStartName = parmp->servername;
3633# endif
3634
3635 /*
3636 * Execute command ourselves if we're here because the send failed (or
3637 * else we would have exited above).
3638 */
3639 if (parmp->serverStr != NULL)
3640 {
3641 char_u *p;
3642
3643 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3644 parmp->serverStr, &p));
3645 vim_free(p);
3646 }
3647}
3648
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003649 static void
3650cmdsrv_main(argc, argv, serverName_arg, serverStr)
3651 int *argc;
3652 char **argv;
3653 char_u *serverName_arg;
3654 char_u **serverStr;
3655{
3656 char_u *res;
3657 int i;
3658 char_u *sname;
3659 int ret;
3660 int didone = FALSE;
3661 int exiterr = 0;
3662 char **newArgV = argv + 1;
3663 int newArgC = 1,
3664 Argc = *argc;
3665 int argtype;
3666#define ARGTYPE_OTHER 0
3667#define ARGTYPE_EDIT 1
3668#define ARGTYPE_EDIT_WAIT 2
3669#define ARGTYPE_SEND 3
3670 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003671 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003672# ifndef FEAT_X11
3673 HWND srv;
3674# else
3675 Window srv;
3676
3677 setup_term_clip();
3678# endif
3679
3680 sname = serverMakeName(serverName_arg, argv[0]);
3681 if (sname == NULL)
3682 return;
3683
3684 /*
3685 * Execute the command server related arguments and remove them
3686 * from the argc/argv array; We may have to return into main()
3687 */
3688 for (i = 1; i < Argc; i++)
3689 {
3690 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003691 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003692 {
3693 for (; i < *argc; i++)
3694 {
3695 *newArgV++ = argv[i];
3696 newArgC++;
3697 }
3698 break;
3699 }
3700
Bram Moolenaareb94e552006-03-11 21:35:11 +00003701 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003702 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003703 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3704 {
3705 char *p = argv[i] + 8;
3706
3707 argtype = ARGTYPE_EDIT;
3708 while (*p != NUL)
3709 {
3710 if (STRNICMP(p, "-wait", 5) == 0)
3711 {
3712 argtype = ARGTYPE_EDIT_WAIT;
3713 p += 5;
3714 }
3715 else if (STRNICMP(p, "-silent", 7) == 0)
3716 {
3717 silent = TRUE;
3718 p += 7;
3719 }
3720 else if (STRNICMP(p, "-tab", 4) == 0)
3721 {
3722 tabs = TRUE;
3723 p += 4;
3724 }
3725 else
3726 {
3727 argtype = ARGTYPE_OTHER;
3728 break;
3729 }
3730 }
3731 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003732 else
3733 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003734
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003735 if (argtype != ARGTYPE_OTHER)
3736 {
3737 if (i == *argc - 1)
3738 mainerr_arg_missing((char_u *)argv[i]);
3739 if (argtype == ARGTYPE_SEND)
3740 {
3741 *serverStr = (char_u *)argv[i + 1];
3742 i++;
3743 }
3744 else
3745 {
3746 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003747 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003748 if (*serverStr == NULL)
3749 {
3750 /* Probably out of memory, exit. */
3751 didone = TRUE;
3752 exiterr = 1;
3753 break;
3754 }
3755 Argc = i;
3756 }
3757# ifdef FEAT_X11
3758 if (xterm_dpy == NULL)
3759 {
3760 mch_errmsg(_("No display"));
3761 ret = -1;
3762 }
3763 else
3764 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3765 NULL, &srv, 0, 0, silent);
3766# else
3767 /* Win32 always works? */
3768 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3769# endif
3770 if (ret < 0)
3771 {
3772 if (argtype == ARGTYPE_SEND)
3773 {
3774 /* Failed to send, abort. */
3775 mch_errmsg(_(": Send failed.\n"));
3776 didone = TRUE;
3777 exiterr = 1;
3778 }
3779 else if (!silent)
3780 /* Let vim start normally. */
3781 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3782 break;
3783 }
3784
3785# ifdef FEAT_GUI_W32
3786 /* Guess that when the server name starts with "g" it's a GUI
3787 * server, which we can bring to the foreground here.
3788 * Foreground() in the server doesn't work very well. */
3789 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3790 SetForegroundWindow(srv);
3791# endif
3792
3793 /*
3794 * For --remote-wait: Wait until the server did edit each
3795 * file. Also detect that the server no longer runs.
3796 */
3797 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3798 {
3799 int numFiles = *argc - i - 1;
3800 int j;
3801 char_u *done = alloc(numFiles);
3802 char_u *p;
3803# ifdef FEAT_GUI_W32
3804 NOTIFYICONDATA ni;
3805 int count = 0;
3806 extern HWND message_window;
3807# endif
3808
3809 if (numFiles > 0 && argv[i + 1][0] == '+')
3810 /* Skip "+cmd" argument, don't wait for it to be edited. */
3811 --numFiles;
3812
3813# ifdef FEAT_GUI_W32
3814 ni.cbSize = sizeof(ni);
3815 ni.hWnd = message_window;
3816 ni.uID = 0;
3817 ni.uFlags = NIF_ICON|NIF_TIP;
3818 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3819 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3820 Shell_NotifyIcon(NIM_ADD, &ni);
3821# endif
3822
3823 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003824 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003825 while (memchr(done, 0, numFiles) != NULL)
3826 {
3827# ifdef WIN32
3828 p = serverGetReply(srv, NULL, TRUE, TRUE);
3829 if (p == NULL)
3830 break;
3831# else
3832 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3833 break;
3834# endif
3835 j = atoi((char *)p);
3836 if (j >= 0 && j < numFiles)
3837 {
3838# ifdef FEAT_GUI_W32
3839 ++count;
3840 sprintf(ni.szTip, _("%d of %d edited"),
3841 count, numFiles);
3842 Shell_NotifyIcon(NIM_MODIFY, &ni);
3843# endif
3844 done[j] = 1;
3845 }
3846 }
3847# ifdef FEAT_GUI_W32
3848 Shell_NotifyIcon(NIM_DELETE, &ni);
3849# endif
3850 }
3851 }
3852 else if (STRICMP(argv[i], "--remote-expr") == 0)
3853 {
3854 if (i == *argc - 1)
3855 mainerr_arg_missing((char_u *)argv[i]);
3856# ifdef WIN32
3857 /* Win32 always works? */
3858 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3859 &res, NULL, 1, FALSE) < 0)
3860# else
3861 if (xterm_dpy == NULL)
3862 mch_errmsg(_("No display: Send expression failed.\n"));
3863 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3864 &res, NULL, 1, 1, FALSE) < 0)
3865# endif
3866 {
3867 if (res != NULL && *res != NUL)
3868 {
3869 /* Output error from remote */
3870 mch_errmsg((char *)res);
3871 vim_free(res);
3872 res = NULL;
3873 }
3874 mch_errmsg(_(": Send expression failed.\n"));
3875 }
3876 }
3877 else if (STRICMP(argv[i], "--serverlist") == 0)
3878 {
3879# ifdef WIN32
3880 /* Win32 always works? */
3881 res = serverGetVimNames();
3882# else
3883 if (xterm_dpy != NULL)
3884 res = serverGetVimNames(xterm_dpy);
3885# endif
3886 if (called_emsg)
3887 mch_errmsg("\n");
3888 }
3889 else if (STRICMP(argv[i], "--servername") == 0)
3890 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003891 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003892 i++;
3893 continue;
3894 }
3895 else
3896 {
3897 *newArgV++ = argv[i];
3898 newArgC++;
3899 continue;
3900 }
3901 didone = TRUE;
3902 if (res != NULL && *res != NUL)
3903 {
3904 mch_msg((char *)res);
3905 if (res[STRLEN(res) - 1] != '\n')
3906 mch_msg("\n");
3907 }
3908 vim_free(res);
3909 }
3910
3911 if (didone)
3912 {
3913 display_errors(); /* display any collected messages */
3914 exit(exiterr); /* Mission accomplished - get out */
3915 }
3916
3917 /* Return back into main() */
3918 *argc = newArgC;
3919 vim_free(sname);
3920}
3921
3922/*
3923 * Build a ":drop" command to send to a Vim server.
3924 */
3925 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003926build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003927 int filec;
3928 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003929 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003930 int sendReply;
3931{
3932 garray_T ga;
3933 int i;
3934 char_u *inicmd = NULL;
3935 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003936 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003937 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003938
3939 if (filec > 0 && filev[0][0] == '+')
3940 {
3941 inicmd = (char_u *)filev[0] + 1;
3942 filev++;
3943 filec--;
3944 }
3945 /* Check if we have at least one argument. */
3946 if (filec <= 0)
3947 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003948
3949 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003950 cwd = alloc(MAXPATHL);
3951 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003952 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003953 if (mch_dirname(cwd, MAXPATHL) != OK)
3954 {
3955 vim_free(cwd);
3956 return NULL;
3957 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003958 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003959#ifdef BACKSLASH_IN_FILENAME
3960 "", /* rem_backslash() will tell what chars to escape */
3961#else
3962 PATH_ESC_CHARS,
3963#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003964 '\\', TRUE);
3965 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003966 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003967 return NULL;
3968 ga_init2(&ga, 1, 100);
3969 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003970 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003971
3972 /* Call inputsave() so that a prompt for an encryption key works. */
3973 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3974 if (tabs)
3975 ga_concat(&ga, (char_u *)"tab ");
3976 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003977 for (i = 0; i < filec; i++)
3978 {
3979 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003980 * do it again in the Vim server. On MS-Windows only escape
3981 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003982 p = vim_strsave_escaped((char_u *)filev[i],
3983#ifdef UNIX
3984 PATH_ESC_CHARS
3985#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003986 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003987#endif
3988 );
3989 if (p == NULL)
3990 {
3991 vim_free(ga.ga_data);
3992 return NULL;
3993 }
3994 ga_concat(&ga, (char_u *)" ");
3995 ga_concat(&ga, p);
3996 vim_free(p);
3997 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003998 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3999
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004000 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4001 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004002 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4003
4004 /* Switch back to the correct current directory (prior to temporary path
4005 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004006 * correct after the :drop command. With line breaks and spaces:
4007 * if !exists('+acd') || !&acd
4008 * if haslocaldir()
4009 * cd -
4010 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004011 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004012 * cd -
4013 * endif
4014 * endif
4015 */
4016 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004017 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004018 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004019 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004020 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004021
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004022 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004023 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4024 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004025 if (inicmd != NULL)
4026 {
4027 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4028 * the following commands to be inserted as text. Use a "|",
4029 * hopefully "inicmd" does allow this... */
4030 ga_concat(&ga, inicmd);
4031 ga_concat(&ga, (char_u *)"|");
4032 }
4033 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4034 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004035 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004036 ga_append(&ga, NUL);
4037 return ga.ga_data;
4038}
4039
4040/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004041 * Make our basic server name: use the specified "arg" if given, otherwise use
4042 * the tail of the command "cmd" we were started with.
4043 * Return the name in allocated memory. This doesn't include a serial number.
4044 */
4045 static char_u *
4046serverMakeName(arg, cmd)
4047 char_u *arg;
4048 char *cmd;
4049{
4050 char_u *p;
4051
4052 if (arg != NULL && *arg != NUL)
4053 p = vim_strsave_up(arg);
4054 else
4055 {
4056 p = vim_strsave_up(gettail((char_u *)cmd));
4057 /* Remove .exe or .bat from the name. */
4058 if (p != NULL && vim_strchr(p, '.') != NULL)
4059 *vim_strchr(p, '.') = NUL;
4060 }
4061 return p;
4062}
4063#endif /* FEAT_CLIENTSERVER */
4064
4065#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4066/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004067 * Replace termcodes such as <CR> and insert as key presses if there is room.
4068 */
4069 void
4070server_to_input_buf(str)
4071 char_u *str;
4072{
4073 char_u *ptr = NULL;
4074 char_u *cpo_save = p_cpo;
4075
4076 /* Set 'cpoptions' the way we want it.
4077 * B set - backslashes are *not* treated specially
4078 * k set - keycodes are *not* reverse-engineered
4079 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004080 * The last but one parameter of replace_termcodes() is TRUE so that the
4081 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004082 */
4083 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004084 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004085 p_cpo = cpo_save;
4086
4087 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4088 {
4089 /*
4090 * Add the string to the input stream.
4091 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4092 *
4093 * First clear typed characters from the typeahead buffer, there could
4094 * be half a mapping there. Then append to the existing string, so
4095 * that multiple commands from a client are concatenated.
4096 */
4097 if (typebuf.tb_maplen < typebuf.tb_len)
4098 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4099 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4100
4101 /* Let input_available() know we inserted text in the typeahead
4102 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004103 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004104 }
4105 vim_free((char_u *)ptr);
4106}
4107
4108/*
4109 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004110 */
4111 char_u *
4112eval_client_expr_to_string(expr)
4113 char_u *expr;
4114{
4115 char_u *res;
4116 int save_dbl = debug_break_level;
4117 int save_ro = redir_off;
4118
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004119 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4120 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004121 debug_break_level = -1;
4122 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004123 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4124 * to be typed. Do generate errors so that try/catch works. */
4125 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004126
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004127 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004128
4129 debug_break_level = save_dbl;
4130 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004131 --emsg_silent;
4132 if (emsg_silent < 0)
4133 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004134
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004135 /* A client can tell us to redraw, but not to display the cursor, so do
4136 * that here. */
4137 setcursor();
4138 out_flush();
4139#ifdef FEAT_GUI
4140 if (gui.in_use)
4141 gui_update_cursor(FALSE, FALSE);
4142#endif
4143
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004144 return res;
4145}
4146
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004147/*
4148 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4149 * return an allocated string. Otherwise return "data".
4150 * "*tofree" is set to the result when it needs to be freed later.
4151 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004152 char_u *
4153serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004154 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004155 char_u *data;
4156 char_u **tofree;
4157{
4158 char_u *res = data;
4159
4160 *tofree = NULL;
4161# ifdef FEAT_MBYTE
4162 if (client_enc != NULL && p_enc != NULL)
4163 {
4164 vimconv_T vimconv;
4165
4166 vimconv.vc_type = CONV_NONE;
4167 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4168 && vimconv.vc_type != CONV_NONE)
4169 {
4170 res = string_convert(&vimconv, data, NULL);
4171 if (res == NULL)
4172 res = data;
4173 else
4174 *tofree = res;
4175 }
4176 convert_setup(&vimconv, NULL, NULL);
4177 }
4178# endif
4179 return res;
4180}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004181#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004182
4183/*
4184 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4185 */
4186#if defined(FEAT_FKMAP) || defined(PROTO)
4187# include "farsi.c"
4188#endif
4189
4190/*
4191 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4192 */
4193#if defined(FEAT_ARABIC) || defined(PROTO)
4194# include "arabic.c"
4195#endif