blob: 70d291171f44bda8544ca32ed2b5e1adf8ad5b7f [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 Moolenaarc013cb62005-07-24 21:18:31 +0000113static void edit_buffers __ARGS((mparm_T *parmp));
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 Moolenaarb4210b32004-06-13 14:51:16 +0000152 int
153# ifdef VIMDLL
154_export
155# endif
156# ifdef FEAT_GUI_MSWIN
157# ifdef __BORLANDC__
158_cdecl
159# endif
160VimMain
161# else
162main
163# endif
164(argc, argv)
165 int argc;
166 char **argv;
167{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000168 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000169 mparm_T params; /* various parameters passed between
170 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000171#ifdef STARTUPTIME
172 int i;
173#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175 /*
176 * Do any system-specific initialisations. These can NOT use IObuff or
177 * NameBuff. Thus emsg2() cannot be called!
178 */
179 mch_early_init();
180
Bram Moolenaar14993322014-09-09 12:25:33 +0200181#if defined(WIN32) && defined(FEAT_MBYTE)
182 /*
183 * MingW expands command line arguments, which confuses our code to
184 * convert when 'encoding' changes. Get the unexpanded arguments.
185 */
186 argc = get_cmd_argsW(&argv);
187#endif
188
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000189 /* Many variables are in "params" so that we can pass them to invoked
190 * functions without a lot of arguments. "argc" and "argv" are also
191 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000192 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193 params.argc = argc;
194 params.argv = argv;
195 params.want_full_screen = TRUE;
196#ifdef FEAT_EVAL
197 params.use_debug_break_level = -1;
198#endif
199#ifdef FEAT_WINDOWS
200 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000201#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000202
Bram Moolenaar99685e62013-05-11 13:56:18 +0200203#ifdef FEAT_RUBY
204 {
205 int ruby_stack_start;
206 vim_ruby_init((void *)&ruby_stack_start);
207 }
208#endif
209
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000211 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212#endif
213
214#ifdef MEM_PROFILE
215 atexit(vim_mem_profile_dump);
216#endif
217
218#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000219 for (i = 1; i < argc; ++i)
220 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000221 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000222 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000223 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000224 TIME_MSG("--- VIM STARTING ---");
225 break;
226 }
227 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000228#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000229 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230
231#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000232 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233#endif
234
235#ifdef FEAT_MBYTE
236 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
237#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000238#ifdef FEAT_EVAL
239 eval_init(); /* init global variables */
240#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000241
242#ifdef __QNXNTO__
243 qnx_init(); /* PhAttach() for clipboard, (and gui) */
244#endif
245
246#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000247 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000248 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000249 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 TIME_MSG("GUI prepared");
251#endif
252
253 /* Init the table of Normal mode commands. */
254 init_normal_cmds();
255
256#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000257 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000258#endif
259
260 /*
261 * Allocate space for the generic buffers (needed for set_init_1() and
262 * EMSG2()).
263 */
264 if ((IObuff = alloc(IOSIZE)) == NULL
265 || (NameBuff = alloc(MAXPATHL)) == NULL)
266 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000267 TIME_MSG("Allocated generic buffers");
268
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000269#ifdef NBDEBUG
270 /* Wait a moment for debugging NetBeans. Must be after allocating
271 * NameBuff. */
272 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
273 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000274 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000275#endif
276
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000277#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
278 /*
279 * Setup to use the current locale (for ctype() and many other things).
280 * NOTE: Translated messages with encodings other than latin1 will not
281 * work until set_init_1() has been called!
282 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000283 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000284 TIME_MSG("locale set");
285#endif
286
287#ifdef FEAT_GUI
288 gui.dofork = TRUE; /* default is to use fork() */
289#endif
290
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000292 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000293 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000294 * --server...
295 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000296 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000297 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000298 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000299
300#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000301 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000302#endif
303#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000304 /* Prepare for possibly starting GUI sometime */
305 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000306 TIME_MSG("GUI prepared");
307#endif
308
309#ifdef FEAT_CLIPBOARD
310 clip_init(FALSE); /* Initialise clipboard stuff */
311 TIME_MSG("clipboard setup");
312#endif
313
314 /*
315 * Check if we have an interactive window.
316 * On the Amiga: If there is no window, we open one with a newcli command
317 * (needed for :! to * work). mch_check_win() will also handle the -d or
318 * -dev argument.
319 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000320 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000321 TIME_MSG("window checked");
322
323 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000324 * Allocate the first window and buffer.
325 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000326 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000327 if (win_alloc_first() == FAIL)
328 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000329
330 init_yank(); /* init yank buffers */
331
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000332 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200333 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000334
335 /*
336 * Set the default values for the options.
337 * NOTE: Non-latin1 translated messages are working only after this,
338 * because this is where "has_mbyte" will be set, which is used by
339 * msg_outtrans_len_attr().
340 * First find out the home directory, needed to expand "~" in options.
341 */
342 init_homedir(); /* find real value of $HOME */
343 set_init_1();
344 TIME_MSG("inits 1");
345
346#ifdef FEAT_EVAL
347 set_lang_var(); /* set v:lang and v:ctype */
348#endif
349
350#ifdef FEAT_CLIENTSERVER
351 /*
352 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000354 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000355 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000356#endif
357
358 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000359 * Figure out the way to work from the command name argv[0].
360 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000361 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000362 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000363
364 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000365 * Process the command line arguments. File names are put in the global
366 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000368 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000369 TIME_MSG("parsing arguments");
370
371 /*
372 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000373 * there is no terminal version, and on Windows we can't fork one off with
374 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000375 */
376#ifdef ALWAYS_USE_GUI
377 gui.starting = TRUE;
378#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000379# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380 /*
381 * Check if the GUI can be started. Reset gui.starting if not.
382 * Don't know about other systems, stay on the safe side and don't check.
383 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000384 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000385 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000386 if (gui_init_check() == FAIL)
387 {
388 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000390 /* When running "evim" or "gvim -y" we need the menus, exit if we
391 * don't have them. */
392 if (params.evim_mode)
393 mch_exit(1);
394 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000395 }
396# endif
397#endif
398
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399 if (GARGCOUNT > 0)
400 {
401#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
402 /*
403 * Expand wildcards in file names.
404 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000405 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000406 {
407 /* Temporarily add '(' and ')' to 'isfname'. These are valid
408 * filename characters but are excluded from 'isfname' to make
409 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
410 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000411 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000412 do_cmdline_cmd((char_u *)":set isf&");
413 }
414#endif
415 fname = alist_name(&GARGLIST[0]);
416 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000417
418#if defined(WIN32) && defined(FEAT_MBYTE)
419 {
420 extern void set_alist_count(void);
421
422 /* Remember the number of entries in the argument list. If it changes
423 * we don't react on setting 'encoding'. */
424 set_alist_count();
425 }
426#endif
427
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000428#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000429 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000430 {
431 /*
432 * If there is one filename, fully qualified, we have very probably
433 * been invoked from explorer, so change to the file's directory.
434 * Hint: to avoid this when typing a command use a forward slash.
435 * If the cd fails, it doesn't matter.
436 */
437 (void)vim_chdirfile(fname);
438 }
439#endif
440 TIME_MSG("expanding arguments");
441
442#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000443 if (params.diff_mode && params.window_count == -1)
444 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000445#endif
446
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000447 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000448 ++RedrawingDisabled;
449
450 /*
451 * When listing swap file names, don't do cursor positioning et. al.
452 */
453 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000454 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000455
456 /*
457 * When certain to start the GUI, don't check capabilities of terminal.
458 * For GTK we can't be sure, but when started from the desktop it doesn't
459 * make sense to try using a terminal.
460 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000461#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000462 if (gui.starting
463# ifdef FEAT_GUI_GTK
464 && !isatty(2)
465# endif
466 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000467 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000468#endif
469
470#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
471 /* When the GUI is started from Finder, need to display messages in a
472 * message box. isatty(2) returns TRUE anyway, thus we need to check the
473 * name to know we're not started from a terminal. */
474 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000475 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000476 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000477
478 /* Avoid always using "/" as the current directory. Note that when
479 * started from Finder the arglist will be filled later in
480 * HandleODocAE() and "fname" will be NULL. */
481 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
482 && STRCMP(NameBuff, "/") == 0)
483 {
484 if (fname != NULL)
485 (void)vim_chdirfile(fname);
486 else
487 {
488 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
489 vim_chdir(NameBuff);
490 }
491 }
492 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000493#endif
494
495 /*
496 * mch_init() sets up the terminal (window) for use. This must be
497 * done after resetting full_screen, otherwise it may move the cursor
498 * (MSDOS).
499 * Note that we may use mch_exit() before mch_init()!
500 */
501 mch_init();
502 TIME_MSG("shell init");
503
504#ifdef USE_XSMP
505 /*
506 * For want of anywhere else to do it, try to connect to xsmp here.
507 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
508 * Hijacking -X 'no X connection' to also disable XSMP connection as that
509 * has a similar delay upon failure.
510 * Only try if SESSION_MANAGER is set to something non-null.
511 */
512 if (!x_no_connect)
513 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000514 char *p = getenv("SESSION_MANAGER");
515
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000516 if (p != NULL && *p != NUL)
517 {
518 xsmp_init();
519 TIME_MSG("xsmp init");
520 }
521 }
522#endif
523
524 /*
525 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000526 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000527 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000529 /* This message comes before term inits, but after setting "silent_mode"
530 * when the input is not a tty. */
531 if (GARGCOUNT > 1 && !silent_mode)
532 printf(_("%d files to edit\n"), GARGCOUNT);
533
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000534 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000536 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 capabilities (will set full_screen) */
538 screen_start(); /* don't know where cursor is now */
539 TIME_MSG("Termcap init");
540 }
541
542 /*
543 * Set the default values for the options that use Rows and Columns.
544 */
545 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000546 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547#ifdef FEAT_DIFF
548 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
549 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000550 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551 diff_win_options(firstwin, FALSE);
552#endif
553
554 cmdline_row = Rows - p_ch;
555 msg_row = cmdline_row;
556 screenalloc(FALSE); /* allocate screen buffers */
557 set_init_2();
558 TIME_MSG("inits 2");
559
560 msg_scroll = TRUE;
561 no_wait_return = TRUE;
562
563 init_mappings(); /* set up initial mappings */
564
565 init_highlight(TRUE, FALSE); /* set the default highlight groups */
566 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000567
568#ifdef FEAT_EVAL
569 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000570 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000571#endif
572
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100573#ifdef FEAT_MZSCHEME
574 /*
575 * Newer version of MzScheme (Racket) require earlier (trampolined)
576 * initialisation via scheme_main_setup.
577 * Implement this by initialising it as early as possible
578 * and splitting off remaining Vim main into vim_main2
579 */
580 {
581 /* Pack up preprocessed command line arguments.
582 * It is safe because Scheme does not access argc/argv. */
583 char *args[2];
584 args[0] = (char *)fname;
585 args[1] = (char *)&params;
586 return mzscheme_main(2, args);
587 }
588}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100589#endif
590#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100591
Bram Moolenaar8866d272012-11-28 15:55:42 +0100592/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
593 * NO_VIM_MAIN is defined. */
594#ifdef FEAT_MZSCHEME
595 int
596vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100597{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100598# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100599 char_u *fname = (char_u *)argv[0];
600 mparm_T params;
601
602 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100603# else
604 return 0;
605}
606# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100607#endif
608
Bram Moolenaar8866d272012-11-28 15:55:42 +0100609#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000610 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000611 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000612
Bram Moolenaar58d98232005-07-23 22:25:46 +0000613 /* Source startup scripts. */
614 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000615
616#ifdef FEAT_EVAL
617 /*
618 * Read all the plugin files.
619 * Only when compiled with +eval, since most plugins need it.
620 */
621 if (p_lpl)
622 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000623# ifdef VMS /* Somehow VMS doesn't handle the "**". */
624 source_runtime((char_u *)"plugin/*.vim", TRUE);
625# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000626 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000627# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000628 TIME_MSG("loading plugins");
629 }
630#endif
631
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000632#ifdef FEAT_DIFF
633 /* Decide about window layout for diff mode after reading vimrc. */
634 if (params.diff_mode && params.window_layout == 0)
635 {
636 if (diffopt_horizontal())
637 params.window_layout = WIN_HOR; /* use horizontal split */
638 else
639 params.window_layout = WIN_VER; /* use vertical split */
640 }
641#endif
642
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000643 /*
644 * Recovery mode without a file name: List swap files.
645 * This uses the 'dir' option, therefore it must be after the
646 * initializations.
647 */
648 if (recoverymode && fname == NULL)
649 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200650 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000651 mch_exit(0);
652 }
653
654 /*
655 * Set a few option defaults after reading .vimrc files:
656 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
657 */
658 set_init_3();
659 TIME_MSG("inits 3");
660
661 /*
662 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
663 * Note that this overrides anything from a vimrc file.
664 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000665 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000666 p_uc = 0;
667
668#ifdef FEAT_FKMAP
669 if (curwin->w_p_rl && p_altkeymap)
670 {
671 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
672# ifdef FEAT_ARABIC
673 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
674# endif
675 p_fkmap = TRUE; /* Set the Farsi keymap mode */
676 }
677#endif
678
679#ifdef FEAT_GUI
680 if (gui.starting)
681 {
682#if defined(UNIX) || defined(VMS)
683 /* When something caused a message from a vimrc script, need to output
684 * an extra newline before the shell prompt. */
685 if (did_emsg || msg_didout)
686 putchar('\n');
687#endif
688
689 gui_start(); /* will set full_screen to TRUE */
690 TIME_MSG("starting GUI");
691
692 /* When running "evim" or "gvim -y" we need the menus, exit if we
693 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000694 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000695 mch_exit(1);
696 }
697#endif
698
699#ifdef SPAWNO /* special MSDOS swapping library */
700 init_SPAWNO("", SWAP_ANY);
701#endif
702
703#ifdef FEAT_VIMINFO
704 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000705 * Read in registers, history etc, but not marks, from the viminfo file.
706 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000707 */
708 if (*p_viminfo != NUL)
709 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000710 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 TIME_MSG("reading viminfo");
712 }
713#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100714#ifdef FEAT_EVAL
715 /* It's better to make v:oldfiles an empty list than NULL. */
716 if (get_vim_var_list(VV_OLDFILES) == NULL)
717 set_vim_var_list(VV_OLDFILES, list_alloc());
718#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000719
720#ifdef FEAT_QUICKFIX
721 /*
722 * "-q errorfile": Load the error file now.
723 * If the error file can't be read, exit before doing anything else.
724 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000725 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000726 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000727 if (params.use_ef != NULL)
728 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000729 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200730 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
731 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732 {
733 out_char('\n');
734 mch_exit(3);
735 }
736 TIME_MSG("reading errorfile");
737 }
738#endif
739
740 /*
741 * Start putting things on the screen.
742 * Scroll screen down before drawing over it
743 * Clear screen now, so file message will not be cleared.
744 */
745 starting = NO_BUFFERS;
746 no_wait_return = FALSE;
747 if (!exmode_active)
748 msg_scroll = FALSE;
749
750#ifdef FEAT_GUI
751 /*
752 * This seems to be required to make callbacks to be called now, instead
753 * of after things have been put on the screen, which then may be deleted
754 * when getting a resize callback.
755 * For the Mac this handles putting files dropped on the Vim icon to
756 * global_alist.
757 */
758 if (gui.in_use)
759 {
760# ifdef FEAT_SUN_WORKSHOP
761 if (!usingSunWorkShop)
762# endif
763 gui_wait_for_chars(50L);
764 TIME_MSG("GUI delay");
765 }
766#endif
767
768#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
769 qnx_clip_init();
770#endif
771
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200772#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
773 clip_init(TRUE);
774#endif
775
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000776#ifdef FEAT_XCLIPBOARD
777 /* Start using the X clipboard, unless the GUI was started. */
778# ifdef FEAT_GUI
779 if (!gui.in_use)
780# endif
781 {
782 setup_term_clip();
783 TIME_MSG("setup clipboard");
784 }
785#endif
786
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000787#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000788 /* Prepare for being a Vim server. */
789 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000790#endif
791
792 /*
793 * If "-" argument given: Read file from stdin.
794 * Do this before starting Raw mode, because it may change things that the
795 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
796 * are the same terminal: "cat | vim -".
797 * Using autocommands here may cause trouble...
798 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000799 if (params.edit_type == EDIT_STDIN && !recoverymode)
800 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801
802#if defined(UNIX) || defined(VMS)
803 /* When switching screens and something caused a message from a vimrc
804 * script, need to output an extra newline on exit. */
805 if ((did_emsg || msg_didout) && *T_TI != NUL)
806 newline_on_exit = TRUE;
807#endif
808
809 /*
810 * When done something that is not allowed or error message call
811 * wait_return. This must be done before starttermcap(), because it may
812 * switch to another screen. It must be done after settmode(TMODE_RAW),
813 * because we want to react on a single key stroke.
814 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000815 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000816 */
817 settmode(TMODE_RAW);
818 TIME_MSG("setting raw mode");
819
820 if (need_wait_return || msg_didany)
821 {
822 wait_return(TRUE);
823 TIME_MSG("waiting for return");
824 }
825
826 starttermcap(); /* start termcap if not done by wait_return() */
827 TIME_MSG("start termcap");
Bram Moolenaar9584b312013-03-13 19:29:28 +0100828#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200829 may_req_ambiguous_char_width();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100830#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000831
832#ifdef FEAT_MOUSE
833 setmouse(); /* may start using the mouse */
834#endif
835 if (scroll_region)
836 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000837 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000838
839 /*
840 * Don't clear the screen when starting in Ex mode, unless using the GUI.
841 */
842 if (exmode_active
843#ifdef FEAT_GUI
844 && !gui.in_use
845#endif
846 )
847 must_redraw = CLEAR;
848 else
849 {
850 screenclear(); /* clear screen */
851 TIME_MSG("clearing screen");
852 }
853
854#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000855 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856 {
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200857 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000858 TIME_MSG("getting crypt key");
859 }
860#endif
861
862 no_wait_return = TRUE;
863
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000864 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000865 * Create the requested number of windows and edit buffers in them.
866 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000867 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000868 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869 TIME_MSG("opening buffers");
870
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000871#ifdef FEAT_EVAL
872 /* clear v:swapcommand */
873 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
874#endif
875
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000876 /* Ex starts at last line of the file */
877 if (exmode_active)
878 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
879
880#ifdef FEAT_AUTOCMD
881 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
882 TIME_MSG("BufEnter autocommands");
883#endif
884 setpcmark();
885
886#ifdef FEAT_QUICKFIX
887 /*
888 * When started with "-q errorfile" jump to first error now.
889 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000890 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000892 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893 TIME_MSG("jump to first error");
894 }
895#endif
896
897#ifdef FEAT_WINDOWS
898 /*
899 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000900 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000901 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000902 edit_buffers(&params);
903#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000904
905#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000906 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000907 {
908 win_T *wp;
909
910 /* set options in each window for "vimdiff". */
911 for (wp = firstwin; wp != NULL; wp = wp->w_next)
912 diff_win_options(wp, TRUE);
913 }
914#endif
915
916 /*
917 * Shorten any of the filenames, but only when absolute.
918 */
919 shorten_fnames(FALSE);
920
921 /*
922 * Need to jump to the tag before executing the '-c command'.
923 * Makes "vim -c '/return' -t main" work.
924 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000925 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000926 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000927#if defined(HAS_SWAP_EXISTS_ACTION)
928 swap_exists_did_quit = FALSE;
929#endif
930
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000931 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000932 do_cmdline_cmd(IObuff);
933 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000934
935#if defined(HAS_SWAP_EXISTS_ACTION)
936 /* If the user doesn't want to edit the file then we quit here. */
937 if (swap_exists_did_quit)
938 getout(1);
939#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000940 }
941
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000942 /* Execute any "+", "-c" and "-S" arguments. */
943 if (params.n_commands > 0)
944 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000945
946 RedrawingDisabled = 0;
947 redraw_all_later(NOT_VALID);
948 no_wait_return = FALSE;
949 starting = 0;
950
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000951#ifdef FEAT_TERMRESPONSE
952 /* Requesting the termresponse is postponed until here, so that a "-c q"
953 * argument doesn't make it appear in the shell Vim was started from. */
954 may_req_termresponse();
955#endif
956
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000957 /* start in insert mode */
958 if (p_im)
959 need_start_insertmode = TRUE;
960
961#ifdef FEAT_AUTOCMD
962 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
963 TIME_MSG("VimEnter autocommands");
964#endif
965
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200966#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
967 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
968 * done after the clipboard is available and all initial commands that may
969 * modify the 'clipboard' setting have run; i.e. just before entering the
970 * main loop. */
971 {
972 int default_regname = 0;
973 adjust_clip_reg(&default_regname);
974 set_reg_var(default_regname);
975 }
976#endif
977
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000978#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
979 /* When a startup script or session file setup for diff'ing and
980 * scrollbind, sync the scrollbind now. */
981 if (curwin->w_p_diff && curwin->w_p_scb)
982 {
983 update_topline();
984 check_scrollbind((linenr_T)0, 0L);
985 TIME_MSG("diff scrollbinding");
986 }
987#endif
988
989#if defined(WIN3264) && !defined(FEAT_GUI_W32)
990 mch_set_winsize_now(); /* Allow winsize changes from now on */
991#endif
992
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000993#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
994 /* When tab pages were created, may need to update the tab pages line and
995 * scrollbars. This is skipped while creating them. */
996 if (first_tabpage->tp_next != NULL)
997 {
998 out_flush();
999 gui_init_which_components(NULL);
1000 gui_update_scrollbars(TRUE);
1001 }
1002 need_mouse_correct = TRUE;
1003#endif
1004
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001005 /* If ":startinsert" command used, stuff a dummy command to be able to
1006 * call normal_cmd(), which will then start Insert mode. */
1007 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001008 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001009
1010#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001011 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001012 {
1013# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001014# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001015 && !defined(FEAT_GUI_W32)
1016 if (gui.in_use)
1017 {
1018 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1019 mch_exit(2);
1020 }
1021# endif
1022# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001023 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001024 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001025 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026#endif
1027
1028 TIME_MSG("before starting main loop");
1029
1030 /*
1031 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001032 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001033 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001034
1035 return 0;
1036}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001037#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001038#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001039
1040/*
1041 * Main loop: Execute Normal mode commands until exiting Vim.
1042 * Also used to handle commands in the command-line window, until the window
1043 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044 * Also used to handle ":visual" command after ":global": execute Normal mode
1045 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046 */
1047 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001048main_loop(cmdwin, noexmode)
1049 int cmdwin; /* TRUE when working in the command-line window */
1050 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001052 oparg_T oa; /* operator arguments */
1053 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001054#ifdef FEAT_CONCEAL
1055 linenr_T conceal_old_cursor_line = 0;
1056 linenr_T conceal_new_cursor_line = 0;
1057 int conceal_update_lines = FALSE;
1058#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001059
1060#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1061 /* Setup to catch a terminating error from the X server. Just ignore
1062 * it, restore the state and continue. This might not always work
1063 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001064 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001066 {
1067 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001068 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001069 got_int = TRUE;
1070 need_wait_return = FALSE;
1071 global_busy = FALSE;
1072 exmode_active = 0;
1073 skip_redraw = FALSE;
1074 RedrawingDisabled = 0;
1075 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001076 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001077# ifdef FEAT_EVAL
1078 emsg_skip = 0;
1079# endif
1080 emsg_off = 0;
1081# ifdef FEAT_MOUSE
1082 setmouse();
1083# endif
1084 settmode(TMODE_RAW);
1085 starttermcap();
1086 scroll_start();
1087 redraw_later_clear();
1088 }
1089#endif
1090
1091 clear_oparg(&oa);
1092 while (!cmdwin
1093#ifdef FEAT_CMDWIN
1094 || cmdwin_result == 0
1095#endif
1096 )
1097 {
1098 if (stuff_empty())
1099 {
1100 did_check_timestamps = FALSE;
1101 if (need_check_timestamps)
1102 check_timestamps(FALSE);
1103 if (need_wait_return) /* if wait_return still needed ... */
1104 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001105 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001106 {
1107 need_start_insertmode = FALSE;
1108 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1109 /* skip the fileinfo message now, because it would be shown
1110 * after insert mode finishes! */
1111 need_fileinfo = FALSE;
1112 }
1113 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001114
1115 /* Reset "got_int" now that we got back to the main loop. Except when
1116 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1117 * the ":g" command.
1118 * For ":g/pat/vi" we reset "got_int" when used once. When used
1119 * a second time we go back to Ex mode and abort the ":g" command. */
1120 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001121 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001122 if (noexmode && global_busy && !exmode_active && previous_got_int)
1123 {
1124 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1125 * used and keep "got_int" set, so that it aborts ":g". */
1126 exmode_active = EXMODE_NORMAL;
1127 State = NORMAL;
1128 }
1129 else if (!global_busy || !exmode_active)
1130 {
1131 if (!quit_more)
1132 (void)vgetc(); /* flush all buffers */
1133 got_int = FALSE;
1134 }
1135 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001136 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001137 else
1138 previous_got_int = FALSE;
1139
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001140 if (!exmode_active)
1141 msg_scroll = FALSE;
1142 quit_more = FALSE;
1143
1144 /*
1145 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1146 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1147 * update cursor and redraw.
1148 */
1149 if (skip_redraw || exmode_active)
1150 skip_redraw = FALSE;
1151 else if (do_redraw || stuff_empty())
1152 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001153#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001154 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001155 if (!finish_op && (
1156# ifdef FEAT_AUTOCMD
1157 has_cursormoved()
1158# endif
1159# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1160 ||
1161# endif
1162# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001163 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001164# endif
1165 )
1166 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001167 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001168# ifdef FEAT_AUTOCMD
1169 if (has_cursormoved())
1170 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1171 FALSE, curbuf);
1172# endif
1173# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001174 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001175 {
1176 conceal_old_cursor_line = last_cursormoved.lnum;
1177 conceal_new_cursor_line = curwin->w_cursor.lnum;
1178 conceal_update_lines = TRUE;
1179 }
1180# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001181 last_cursormoved = curwin->w_cursor;
1182 }
1183#endif
1184
Bram Moolenaar186628f2013-03-19 13:33:23 +01001185#ifdef FEAT_AUTOCMD
1186 /* Trigger TextChanged if b_changedtick differs. */
1187 if (!finish_op && has_textchanged()
1188 && last_changedtick != curbuf->b_changedtick)
1189 {
1190 if (last_changedtick_buf == curbuf)
1191 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1192 FALSE, curbuf);
1193 last_changedtick_buf = curbuf;
1194 last_changedtick = curbuf->b_changedtick;
1195 }
1196#endif
1197
Bram Moolenaar33aec762006-01-22 23:30:12 +00001198#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1199 /* Scroll-binding for diff mode may have been postponed until
1200 * here. Avoids doing it for every change. */
1201 if (diff_need_scrollbind)
1202 {
1203 check_scrollbind((linenr_T)0, 0L);
1204 diff_need_scrollbind = FALSE;
1205 }
1206#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001207#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001208 /* Include a closed fold completely in the Visual area. */
1209 foldAdjustVisual();
1210#endif
1211#ifdef FEAT_FOLDING
1212 /*
1213 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1214 * contain the cursor.
1215 * When 'foldopen' is "all", open the fold(s) under the cursor.
1216 * This may mark the window for redrawing.
1217 */
1218 if (hasAnyFolding(curwin) && !char_avail())
1219 {
1220 foldCheckClose();
1221 if (fdo_flags & FDO_ALL)
1222 foldOpenCursor();
1223 }
1224#endif
1225
1226 /*
1227 * Before redrawing, make sure w_topline is correct, and w_leftcol
1228 * if lines don't wrap, and w_skipcol if lines wrap.
1229 */
1230 update_topline();
1231 validate_cursor();
1232
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001233 if (VIsual_active)
1234 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001235 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001236 update_screen(0);
1237 else if (redraw_cmdline || clear_cmdline)
1238 showmode();
1239#ifdef FEAT_WINDOWS
1240 redraw_statuslines();
1241#endif
1242#ifdef FEAT_TITLE
1243 if (need_maketitle)
1244 maketitle();
1245#endif
1246 /* display message after redraw */
1247 if (keep_msg != NULL)
1248 {
1249 char_u *p;
1250
1251 /* msg_attr_keep() will set keep_msg to NULL, must free the
1252 * string here. */
1253 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001254 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001255 msg_attr(p, keep_msg_attr);
1256 vim_free(p);
1257 }
1258 if (need_fileinfo) /* show file info after redraw */
1259 {
1260 fileinfo(FALSE, TRUE, FALSE);
1261 need_fileinfo = FALSE;
1262 }
1263
1264 emsg_on_display = FALSE; /* can delete error message now */
1265 did_emsg = FALSE;
1266 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001267 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001268 showruler(FALSE);
1269
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001270# if defined(FEAT_CONCEAL)
1271 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001272 && (conceal_old_cursor_line != conceal_new_cursor_line
1273 || conceal_cursor_line(curwin)
1274 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001275 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001276 if (conceal_old_cursor_line != conceal_new_cursor_line
1277 && conceal_old_cursor_line
1278 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001279 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001280 update_single_line(curwin, conceal_new_cursor_line);
1281 curwin->w_valid &= ~VALID_CROW;
1282 }
1283# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001284 setcursor();
1285 cursor_on();
1286
1287 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001288
1289#ifdef STARTUPTIME
1290 /* Now that we have drawn the first screen all the startup stuff
1291 * has been done, close any file for startup messages. */
1292 if (time_fd != NULL)
1293 {
1294 TIME_MSG("first screen update");
1295 TIME_MSG("--- VIM STARTED ---");
1296 fclose(time_fd);
1297 time_fd = NULL;
1298 }
1299#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001300 }
1301#ifdef FEAT_GUI
1302 if (need_mouse_correct)
1303 gui_mouse_correct();
1304#endif
1305
1306 /*
1307 * Update w_curswant if w_set_curswant has been set.
1308 * Postponed until here to avoid computing w_virtcol too often.
1309 */
1310 update_curswant();
1311
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001312#ifdef FEAT_EVAL
1313 /*
1314 * May perform garbage collection when waiting for a character, but
1315 * only at the very toplevel. Otherwise we may be using a List or
1316 * Dict internally somewhere.
1317 * "may_garbage_collect" is reset in vgetc() which is invoked through
1318 * do_exmode() and normal_cmd().
1319 */
1320 may_garbage_collect = (!cmdwin && !noexmode);
1321#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001322 /*
1323 * If we're invoked as ex, do a round of ex commands.
1324 * Otherwise, get and execute a normal mode command.
1325 */
1326 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001327 {
1328 if (noexmode) /* End of ":global/path/visual" commands */
1329 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001330 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001331 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001332 else
1333 normal_cmd(&oa, TRUE);
1334 }
1335}
1336
1337
1338#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1339/*
1340 * Exit, but leave behind swap files for modified buffers.
1341 */
1342 void
1343getout_preserve_modified(exitval)
1344 int exitval;
1345{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001346# if defined(SIGHUP) && defined(SIG_IGN)
1347 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1348 * makes Vim exit and then handling SIGHUP causes various reentrance
1349 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001351# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001352
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001353 ml_close_notmod(); /* close all not-modified buffers */
1354 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1355 ml_close_all(FALSE); /* close all memfiles, without deleting */
1356 getout(exitval); /* exit Vim properly */
1357}
1358#endif
1359
1360
1361/* Exit properly */
1362 void
1363getout(exitval)
1364 int exitval;
1365{
1366#ifdef FEAT_AUTOCMD
1367 buf_T *buf;
1368 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001369 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370#endif
1371
1372 exiting = TRUE;
1373
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001374 /* When running in Ex mode an error causes us to exit with a non-zero exit
1375 * code. POSIX requires this, although it's not 100% clear from the
1376 * standard. */
1377 if (exmode_active)
1378 exitval += ex_exitval;
1379
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001380 /* Position the cursor on the last screen line, below all the text */
1381#ifdef FEAT_GUI
1382 if (!gui.in_use)
1383#endif
1384 windgoto((int)Rows - 1, 0);
1385
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001386#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1387 /* Optionally print hashtable efficiency. */
1388 hash_debug_results();
1389#endif
1390
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001391#ifdef FEAT_GUI
1392 msg_didany = FALSE;
1393#endif
1394
1395#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001396 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001397 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001398 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1399# if defined FEAT_WINDOWS
1400 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001402 next_tp = tp->tp_next;
1403 for (wp = (tp == curtab)
1404 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001405 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001406 if (wp->w_buffer == NULL)
1407 /* Autocmd must have close the buffer already, skip. */
1408 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001409 buf = wp->w_buffer;
1410 if (buf->b_changedtick != -1)
1411 {
1412 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1413 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001414 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001415 /* start all over, autocommands may mess up the lists */
1416 next_tp = first_tabpage;
1417 break;
1418 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001419 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001420 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001421# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1423 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001424# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001425
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001426 /* Trigger BufUnload for buffers that are loaded */
1427 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1428 if (buf->b_ml.ml_mfp != NULL)
1429 {
1430 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001431 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1433 break;
1434 }
1435 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1436 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001437#endif
1438
1439#ifdef FEAT_VIMINFO
1440 if (*p_viminfo != NUL)
1441 /* Write out the registers, history, marks etc, to the viminfo file */
1442 write_viminfo(NULL, FALSE);
1443#endif
1444
1445#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001446 if (get_vim_var_nr(VV_DYING) <= 1)
1447 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001448#endif
1449
Bram Moolenaar05159a02005-02-26 23:04:13 +00001450#ifdef FEAT_PROFILE
1451 profile_dump();
1452#endif
1453
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001454 if (did_emsg
1455#ifdef FEAT_GUI
1456 || (gui.in_use && msg_didany && p_verbose > 0)
1457#endif
1458 )
1459 {
1460 /* give the user a chance to read the (error) message */
1461 no_wait_return = FALSE;
1462 wait_return(FALSE);
1463 }
1464
1465#ifdef FEAT_AUTOCMD
1466 /* Position the cursor again, the autocommands may have moved it */
1467# ifdef FEAT_GUI
1468 if (!gui.in_use)
1469# endif
1470 windgoto((int)Rows - 1, 0);
1471#endif
1472
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001473#ifdef FEAT_LUA
1474 lua_end();
1475#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001476#ifdef FEAT_MZSCHEME
1477 mzscheme_end();
1478#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001479#ifdef FEAT_TCL
1480 tcl_end();
1481#endif
1482#ifdef FEAT_RUBY
1483 ruby_end();
1484#endif
1485#ifdef FEAT_PYTHON
1486 python_end();
1487#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001488#ifdef FEAT_PYTHON3
1489 python3_end();
1490#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001491#ifdef FEAT_PERL
1492 perl_end();
1493#endif
1494#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1495 iconv_end();
1496#endif
1497#ifdef FEAT_NETBEANS_INTG
1498 netbeans_end();
1499#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001500#ifdef FEAT_CSCOPE
1501 cs_end();
1502#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001503#ifdef FEAT_EVAL
1504 if (garbage_collect_at_exit)
1505 garbage_collect();
1506#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001507#if defined(WIN32) && defined(FEAT_MBYTE)
1508 free_cmd_argsW();
1509#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001510
1511 mch_exit(exitval);
1512}
1513
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001514#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001515/*
1516 * Get a (optional) count for a Vim argument.
1517 */
1518 static int
1519get_number_arg(p, idx, def)
1520 char_u *p; /* pointer to argument */
1521 int *idx; /* index in argument, is incremented */
1522 int def; /* default value */
1523{
1524 if (vim_isdigit(p[*idx]))
1525 {
1526 def = atoi((char *)&(p[*idx]));
1527 while (vim_isdigit(p[*idx]))
1528 *idx = *idx + 1;
1529 }
1530 return def;
1531}
1532
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001533#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1534/*
1535 * Setup to use the current locale (for ctype() and many other things).
1536 */
1537 static void
1538init_locale()
1539{
1540 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001541
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001542# ifdef FEAT_GUI_GTK
1543 /* Tell Gtk not to change our locale settings. */
1544 gtk_disable_setlocale();
1545# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001546# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1547 /* Make sure strtod() uses a decimal point, not a comma. */
1548 setlocale(LC_NUMERIC, "C");
1549# endif
1550
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001551# ifdef WIN32
1552 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1553 * text while it's expecting text in the current locale. This call avoids
1554 * that. */
1555 setlocale(LC_CTYPE, "C");
1556# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001557
1558# ifdef FEAT_GETTEXT
1559 {
1560 int mustfree = FALSE;
1561 char_u *p;
1562
1563# ifdef DYNAMIC_GETTEXT
1564 /* Initialize the gettext library */
1565 dyn_libintl_init(NULL);
1566# endif
1567 /* expand_env() doesn't work yet, because chartab[] is not initialized
1568 * yet, call vim_getenv() directly */
1569 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1570 if (p != NULL && *p != NUL)
1571 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001572 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001573 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1574 }
1575 if (mustfree)
1576 vim_free(p);
1577 textdomain(VIMPACKAGE);
1578 }
1579# endif
1580}
1581#endif
1582
1583/*
1584 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1585 * If the executable name starts with "r" we disable shell commands.
1586 * If the next character is "e" we run in Easy mode.
1587 * If the next character is "g" we run the GUI version.
1588 * If the next characters are "view" we start in readonly mode.
1589 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1590 * If the next characters are "ex" we start in Ex mode. If it's followed
1591 * by "im" use improved Ex mode.
1592 */
1593 static void
1594parse_command_name(parmp)
1595 mparm_T *parmp;
1596{
1597 char_u *initstr;
1598
1599 initstr = gettail((char_u *)parmp->argv[0]);
1600
1601#ifdef MACOS_X_UNIX
1602 /* An issue has been seen when launching Vim in such a way that
1603 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1604 * executable or a symbolic link of it. Until this issue is resolved
1605 * we prohibit the GUI from being used.
1606 */
1607 if (STRCMP(initstr, parmp->argv[0]) == 0)
1608 disallow_gui = TRUE;
1609
1610 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001611 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001612#endif
1613
1614#ifdef FEAT_EVAL
1615 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001616 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001617#endif
1618
1619 if (TOLOWER_ASC(initstr[0]) == 'r')
1620 {
1621 restricted = TRUE;
1622 ++initstr;
1623 }
1624
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001625 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001626 if (TOLOWER_ASC(initstr[0]) == 'e'
1627 && (TOLOWER_ASC(initstr[1]) == 'v'
1628 || TOLOWER_ASC(initstr[1]) == 'g'))
1629 {
1630#ifdef FEAT_GUI
1631 gui.starting = TRUE;
1632#endif
1633 parmp->evim_mode = TRUE;
1634 ++initstr;
1635 }
1636
Bram Moolenaar806875d2008-09-18 18:57:10 +00001637 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1638 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001639 {
1640 main_start_gui();
1641#ifdef FEAT_GUI
1642 ++initstr;
1643#endif
1644 }
1645
1646 if (STRNICMP(initstr, "view", 4) == 0)
1647 {
1648 readonlymode = TRUE;
1649 curbuf->b_p_ro = TRUE;
1650 p_uc = 10000; /* don't update very often */
1651 initstr += 4;
1652 }
1653 else if (STRNICMP(initstr, "vim", 3) == 0)
1654 initstr += 3;
1655
1656 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1657 if (STRICMP(initstr, "diff") == 0)
1658 {
1659#ifdef FEAT_DIFF
1660 parmp->diff_mode = TRUE;
1661#else
1662 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1663 mch_errmsg("\n");
1664 mch_exit(2);
1665#endif
1666 }
1667
1668 if (STRNICMP(initstr, "ex", 2) == 0)
1669 {
1670 if (STRNICMP(initstr + 2, "im", 2) == 0)
1671 exmode_active = EXMODE_VIM;
1672 else
1673 exmode_active = EXMODE_NORMAL;
1674 change_compatible(TRUE); /* set 'compatible' */
1675 }
1676}
1677
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001678/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001679 * Get the name of the display, before gui_prepare() removes it from
1680 * argv[]. Used for the xterm-clipboard display.
1681 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001682 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001683 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001684 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001685early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001686 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001687{
Bram Moolenaar03005972008-11-20 13:12:36 +00001688#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1689 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001690 int argc = parmp->argc;
1691 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001692 int i;
1693
1694 for (i = 1; i < argc; i++)
1695 {
1696 if (STRCMP(argv[i], "--") == 0)
1697 break;
1698# ifdef FEAT_XCLIPBOARD
1699 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001700# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001701 || STRICMP(argv[i], "--display") == 0
1702# endif
1703 )
1704 {
1705 if (i == argc - 1)
1706 mainerr_arg_missing((char_u *)argv[i]);
1707 xterm_display = argv[++i];
1708 }
1709# endif
1710# ifdef FEAT_CLIENTSERVER
1711 else if (STRICMP(argv[i], "--servername") == 0)
1712 {
1713 if (i == argc - 1)
1714 mainerr_arg_missing((char_u *)argv[i]);
1715 parmp->serverName_arg = (char_u *)argv[++i];
1716 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001717 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001718 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001719 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001720 {
1721 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001722# ifdef FEAT_GUI
1723 if (strstr(argv[i], "-wait") != 0)
1724 /* don't fork() when starting the GUI to edit files ourself */
1725 gui.dofork = FALSE;
1726# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001727 }
1728# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001729
1730# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1731# ifdef FEAT_GUI_W32
1732 else if (STRICMP(argv[i], "--windowid") == 0)
1733# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001734 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001735# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001736 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001737 long_u id;
1738 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001739
1740 if (i == argc - 1)
1741 mainerr_arg_missing((char_u *)argv[i]);
1742 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001743 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001744 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001745 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001746 if (count != 1)
1747 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1748 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001749# ifdef FEAT_GUI_W32
1750 win_socket_id = id;
1751# else
1752 gtk_socket_id = id;
1753# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001754 i++;
1755 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001756# endif
1757# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 else if (STRICMP(argv[i], "--echo-wid") == 0)
1759 echo_wid_arg = TRUE;
1760# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001761# ifndef FEAT_NETBEANS_INTG
1762 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001763 {
1764 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1765 mch_exit(2);
1766 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001767# endif
1768
Bram Moolenaar58d98232005-07-23 22:25:46 +00001769 }
1770#endif
1771}
1772
1773/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001774 * Scan the command line arguments.
1775 */
1776 static void
1777command_line_scan(parmp)
1778 mparm_T *parmp;
1779{
1780 int argc = parmp->argc;
1781 char **argv = parmp->argv;
1782 int argv_idx; /* index in argv[n][] */
1783 int had_minmin = FALSE; /* found "--" argument */
1784 int want_argument; /* option argument with argument */
1785 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001786 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001787 long n;
1788
1789 --argc;
1790 ++argv;
1791 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1792 while (argc > 0)
1793 {
1794 /*
1795 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1796 */
1797 if (argv[0][0] == '+' && !had_minmin)
1798 {
1799 if (parmp->n_commands >= MAX_ARG_CMDS)
1800 mainerr(ME_EXTRA_CMD, NULL);
1801 argv_idx = -1; /* skip to next argument */
1802 if (argv[0][1] == NUL)
1803 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1804 else
1805 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1806 }
1807
1808 /*
1809 * Optional argument.
1810 */
1811 else if (argv[0][0] == '-' && !had_minmin)
1812 {
1813 want_argument = FALSE;
1814 c = argv[0][argv_idx++];
1815#ifdef VMS
1816 /*
1817 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1818 * and "-/X" as "-X".
1819 */
1820 if (c == '/')
1821 {
1822 c = argv[0][argv_idx++];
1823 c = TOUPPER_ASC(c);
1824 }
1825 else
1826 c = TOLOWER_ASC(c);
1827#endif
1828 switch (c)
1829 {
1830 case NUL: /* "vim -" read from stdin */
1831 /* "ex -" silent mode */
1832 if (exmode_active)
1833 silent_mode = TRUE;
1834 else
1835 {
1836 if (parmp->edit_type != EDIT_NONE)
1837 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1838 parmp->edit_type = EDIT_STDIN;
1839 read_cmd_fd = 2; /* read from stderr instead of stdin */
1840 }
1841 argv_idx = -1; /* skip to next argument */
1842 break;
1843
1844 case '-': /* "--" don't take any more option arguments */
1845 /* "--help" give help message */
1846 /* "--version" give version message */
1847 /* "--literal" take files literally */
1848 /* "--nofork" don't fork */
1849 /* "--noplugin[s]" skip plugins */
1850 /* "--cmd <cmd>" execute cmd before vimrc */
1851 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1852 usage();
1853 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1854 {
1855 Columns = 80; /* need to init Columns */
1856 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1857 list_version();
1858 msg_putchar('\n');
1859 msg_didout = FALSE;
1860 mch_exit(0);
1861 }
1862 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1863 {
1864#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1865 parmp->literal = TRUE;
1866#endif
1867 }
1868 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1869 {
1870#ifdef FEAT_GUI
1871 gui.dofork = FALSE; /* don't fork() when starting GUI */
1872#endif
1873 }
1874 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1875 p_lpl = FALSE;
1876 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1877 {
1878 want_argument = TRUE;
1879 argv_idx += 3;
1880 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001881 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1882 {
1883 want_argument = TRUE;
1884 argv_idx += 11;
1885 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001886#ifdef FEAT_CLIENTSERVER
1887 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1888 ; /* already processed -- no arg */
1889 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1890 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1891 {
1892 /* already processed -- snatch the following arg */
1893 if (argc > 1)
1894 {
1895 --argc;
1896 ++argv;
1897 }
1898 }
1899#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001900#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1901# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001902 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001903# else
1904 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1905# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001906 {
1907 /* already processed -- snatch the following arg */
1908 if (argc > 1)
1909 {
1910 --argc;
1911 ++argv;
1912 }
1913 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001914#endif
1915#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001916 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1917 {
1918 /* already processed, skip */
1919 }
1920#endif
1921 else
1922 {
1923 if (argv[0][argv_idx])
1924 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1925 had_minmin = TRUE;
1926 }
1927 if (!want_argument)
1928 argv_idx = -1; /* skip to next argument */
1929 break;
1930
1931 case 'A': /* "-A" start in Arabic mode */
1932#ifdef FEAT_ARABIC
1933 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1934#else
1935 mch_errmsg(_(e_noarabic));
1936 mch_exit(2);
1937#endif
1938 break;
1939
1940 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001941 /* Needs to be effective before expanding file names, because
1942 * for Win32 this makes us edit a shortcut file itself,
1943 * instead of the file it links to. */
1944 set_options_bin(curbuf->b_p_bin, 1, 0);
1945 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001946 break;
1947
1948 case 'C': /* "-C" Compatible */
1949 change_compatible(TRUE);
1950 break;
1951
1952 case 'e': /* "-e" Ex mode */
1953 exmode_active = EXMODE_NORMAL;
1954 break;
1955
1956 case 'E': /* "-E" Improved Ex mode */
1957 exmode_active = EXMODE_VIM;
1958 break;
1959
1960 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1961 window directly, not with newcli */
1962#ifdef FEAT_GUI
1963 gui.dofork = FALSE; /* don't fork() when starting GUI */
1964#endif
1965 break;
1966
1967 case 'g': /* "-g" start GUI */
1968 main_start_gui();
1969 break;
1970
1971 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1972#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001973 p_fkmap = TRUE;
1974 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001975#else
1976 mch_errmsg(_(e_nofarsi));
1977 mch_exit(2);
1978#endif
1979 break;
1980
1981 case 'h': /* "-h" give help message */
1982#ifdef FEAT_GUI_GNOME
1983 /* Tell usage() to exit for "gvim". */
1984 gui.starting = FALSE;
1985#endif
1986 usage();
1987 break;
1988
1989 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1990#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001991 p_hkmap = TRUE;
1992 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001993#else
1994 mch_errmsg(_(e_nohebrew));
1995 mch_exit(2);
1996#endif
1997 break;
1998
1999 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2000#ifdef FEAT_LISP
2001 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2002 p_sm = TRUE;
2003#endif
2004 break;
2005
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002006 case 'M': /* "-M" no changes or writing of files */
2007 reset_modifiable();
2008 /* FALLTHROUGH */
2009
2010 case 'm': /* "-m" no writing of files */
2011 p_write = FALSE;
2012 break;
2013
2014 case 'y': /* "-y" easy mode */
2015#ifdef FEAT_GUI
2016 gui.starting = TRUE; /* start GUI a bit later */
2017#endif
2018 parmp->evim_mode = TRUE;
2019 break;
2020
2021 case 'N': /* "-N" Nocompatible */
2022 change_compatible(FALSE);
2023 break;
2024
2025 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002026#ifdef FEAT_NETBEANS_INTG
2027 /* checking for "-nb", netbeans parameters */
2028 if (argv[0][argv_idx] == 'b')
2029 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002030 netbeansArg = argv[0];
2031 argv_idx = -1; /* skip to next argument */
2032 }
2033 else
2034#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002035 parmp->no_swap_file = TRUE;
2036 break;
2037
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002038 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002039#ifdef TARGET_API_MAC_OSX
2040 /* For some reason on MacOS X, an argument like:
2041 -psn_0_10223617 is passed in when invoke from Finder
2042 or with the 'open' command */
2043 if (argv[0][argv_idx] == 's')
2044 {
2045 argv_idx = -1; /* bypass full -psn */
2046 main_start_gui();
2047 break;
2048 }
2049#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002050#ifdef FEAT_WINDOWS
2051 /* default is 0: open window for each file */
2052 parmp->window_count = get_number_arg((char_u *)argv[0],
2053 &argv_idx, 0);
2054 parmp->window_layout = WIN_TABS;
2055#endif
2056 break;
2057
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002058 case 'o': /* "-o[N]" open N horizontal split windows */
2059#ifdef FEAT_WINDOWS
2060 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002061 parmp->window_count = get_number_arg((char_u *)argv[0],
2062 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002063 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002064#endif
2065 break;
2066
2067 case 'O': /* "-O[N]" open N vertical split windows */
2068#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2069 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002070 parmp->window_count = get_number_arg((char_u *)argv[0],
2071 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002072 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002073#endif
2074 break;
2075
2076#ifdef FEAT_QUICKFIX
2077 case 'q': /* "-q" QuickFix mode */
2078 if (parmp->edit_type != EDIT_NONE)
2079 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2080 parmp->edit_type = EDIT_QF;
2081 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2082 {
2083 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2084 argv_idx = -1;
2085 }
2086 else if (argc > 1) /* "-q {errorfile}" */
2087 want_argument = TRUE;
2088 break;
2089#endif
2090
2091 case 'R': /* "-R" readonly mode */
2092 readonlymode = TRUE;
2093 curbuf->b_p_ro = TRUE;
2094 p_uc = 10000; /* don't update very often */
2095 break;
2096
2097 case 'r': /* "-r" recovery mode */
2098 case 'L': /* "-L" recovery mode */
2099 recoverymode = 1;
2100 break;
2101
2102 case 's':
2103 if (exmode_active) /* "-s" silent (batch) mode */
2104 silent_mode = TRUE;
2105 else /* "-s {scriptin}" read from script file */
2106 want_argument = TRUE;
2107 break;
2108
2109 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2110 if (parmp->edit_type != EDIT_NONE)
2111 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2112 parmp->edit_type = EDIT_TAG;
2113 if (argv[0][argv_idx]) /* "-t{tag}" */
2114 {
2115 parmp->tagname = (char_u *)argv[0] + argv_idx;
2116 argv_idx = -1;
2117 }
2118 else /* "-t {tag}" */
2119 want_argument = TRUE;
2120 break;
2121
2122#ifdef FEAT_EVAL
2123 case 'D': /* "-D" Debugging */
2124 parmp->use_debug_break_level = 9999;
2125 break;
2126#endif
2127#ifdef FEAT_DIFF
2128 case 'd': /* "-d" 'diff' */
2129# ifdef AMIGA
2130 /* check for "-dev {device}" */
2131 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2132 want_argument = TRUE;
2133 else
2134# endif
2135 parmp->diff_mode = TRUE;
2136 break;
2137#endif
2138 case 'V': /* "-V{N}" Verbose level */
2139 /* default is 10: a little bit verbose */
2140 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2141 if (argv[0][argv_idx] != NUL)
2142 {
2143 set_option_value((char_u *)"verbosefile", 0L,
2144 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002145 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002146 }
2147 break;
2148
2149 case 'v': /* "-v" Vi-mode (as if called "vi") */
2150 exmode_active = 0;
2151#ifdef FEAT_GUI
2152 gui.starting = FALSE; /* don't start GUI */
2153#endif
2154 break;
2155
2156 case 'w': /* "-w{number}" set window height */
2157 /* "-w {scriptout}" write to script */
2158 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2159 {
2160 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2161 set_option_value((char_u *)"window", n, NULL, 0);
2162 break;
2163 }
2164 want_argument = TRUE;
2165 break;
2166
2167#ifdef FEAT_CRYPT
2168 case 'x': /* "-x" encrypted reading/writing of files */
2169 parmp->ask_for_key = TRUE;
2170 break;
2171#endif
2172
2173 case 'X': /* "-X" don't connect to X server */
2174#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2175 x_no_connect = TRUE;
2176#endif
2177 break;
2178
2179 case 'Z': /* "-Z" restricted mode */
2180 restricted = TRUE;
2181 break;
2182
2183 case 'c': /* "-c{command}" or "-c {command}" execute
2184 command */
2185 if (argv[0][argv_idx] != NUL)
2186 {
2187 if (parmp->n_commands >= MAX_ARG_CMDS)
2188 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002189 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2190 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002191 argv_idx = -1;
2192 break;
2193 }
2194 /*FALLTHROUGH*/
2195 case 'S': /* "-S {file}" execute Vim script */
2196 case 'i': /* "-i {viminfo}" use for viminfo */
2197#ifndef FEAT_DIFF
2198 case 'd': /* "-d {device}" device (for Amiga) */
2199#endif
2200 case 'T': /* "-T {terminal}" terminal name */
2201 case 'u': /* "-u {vimrc}" vim inits file */
2202 case 'U': /* "-U {gvimrc}" gvim inits file */
2203 case 'W': /* "-W {scriptout}" overwrite */
2204#ifdef FEAT_GUI_W32
2205 case 'P': /* "-P {parent title}" MDI parent */
2206#endif
2207 want_argument = TRUE;
2208 break;
2209
2210 default:
2211 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2212 }
2213
2214 /*
2215 * Handle option arguments with argument.
2216 */
2217 if (want_argument)
2218 {
2219 /*
2220 * Check for garbage immediately after the option letter.
2221 */
2222 if (argv[0][argv_idx] != NUL)
2223 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2224
2225 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002226 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002227 mainerr_arg_missing((char_u *)argv[0]);
2228 ++argv;
2229 argv_idx = -1;
2230
2231 switch (c)
2232 {
2233 case 'c': /* "-c {command}" execute command */
2234 case 'S': /* "-S {file}" execute Vim script */
2235 if (parmp->n_commands >= MAX_ARG_CMDS)
2236 mainerr(ME_EXTRA_CMD, NULL);
2237 if (c == 'S')
2238 {
2239 char *a;
2240
2241 if (argc < 1)
2242 /* "-S" without argument: use default session file
2243 * name. */
2244 a = SESSION_FILE;
2245 else if (argv[0][0] == '-')
2246 {
2247 /* "-S" followed by another option: use default
2248 * session file name. */
2249 a = SESSION_FILE;
2250 ++argc;
2251 --argv;
2252 }
2253 else
2254 a = argv[0];
2255 p = alloc((unsigned)(STRLEN(a) + 4));
2256 if (p == NULL)
2257 mch_exit(2);
2258 sprintf((char *)p, "so %s", a);
2259 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2260 parmp->commands[parmp->n_commands++] = p;
2261 }
2262 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002263 parmp->commands[parmp->n_commands++] =
2264 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002265 break;
2266
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002267 case '-':
2268 if (argv[-1][2] == 'c')
2269 {
2270 /* "--cmd {command}" execute command */
2271 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2272 mainerr(ME_EXTRA_CMD, NULL);
2273 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002274 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002275 }
2276 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002277 break;
2278
2279 /* case 'd': -d {device} is handled in mch_check_win() for the
2280 * Amiga */
2281
2282#ifdef FEAT_QUICKFIX
2283 case 'q': /* "-q {errorfile}" QuickFix mode */
2284 parmp->use_ef = (char_u *)argv[0];
2285 break;
2286#endif
2287
2288 case 'i': /* "-i {viminfo}" use for viminfo */
2289 use_viminfo = (char_u *)argv[0];
2290 break;
2291
2292 case 's': /* "-s {scriptin}" read from script file */
2293 if (scriptin[0] != NULL)
2294 {
2295scripterror:
2296 mch_errmsg(_("Attempt to open script file again: \""));
2297 mch_errmsg(argv[-1]);
2298 mch_errmsg(" ");
2299 mch_errmsg(argv[0]);
2300 mch_errmsg("\"\n");
2301 mch_exit(2);
2302 }
2303 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2304 {
2305 mch_errmsg(_("Cannot open for reading: \""));
2306 mch_errmsg(argv[0]);
2307 mch_errmsg("\"\n");
2308 mch_exit(2);
2309 }
2310 if (save_typebuf() == FAIL)
2311 mch_exit(2); /* out of memory */
2312 break;
2313
2314 case 't': /* "-t {tag}" */
2315 parmp->tagname = (char_u *)argv[0];
2316 break;
2317
2318 case 'T': /* "-T {terminal}" terminal name */
2319 /*
2320 * The -T term argument is always available and when
2321 * HAVE_TERMLIB is supported it overrides the environment
2322 * variable TERM.
2323 */
2324#ifdef FEAT_GUI
2325 if (term_is_gui((char_u *)argv[0]))
2326 gui.starting = TRUE; /* start GUI a bit later */
2327 else
2328#endif
2329 parmp->term = (char_u *)argv[0];
2330 break;
2331
2332 case 'u': /* "-u {vimrc}" vim inits file */
2333 parmp->use_vimrc = (char_u *)argv[0];
2334 break;
2335
2336 case 'U': /* "-U {gvimrc}" gvim inits file */
2337#ifdef FEAT_GUI
2338 use_gvimrc = (char_u *)argv[0];
2339#endif
2340 break;
2341
2342 case 'w': /* "-w {nr}" 'window' value */
2343 /* "-w {scriptout}" append to script file */
2344 if (vim_isdigit(*((char_u *)argv[0])))
2345 {
2346 argv_idx = 0;
2347 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2348 set_option_value((char_u *)"window", n, NULL, 0);
2349 argv_idx = -1;
2350 break;
2351 }
2352 /*FALLTHROUGH*/
2353 case 'W': /* "-W {scriptout}" overwrite script file */
2354 if (scriptout != NULL)
2355 goto scripterror;
2356 if ((scriptout = mch_fopen(argv[0],
2357 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2358 {
2359 mch_errmsg(_("Cannot open for script output: \""));
2360 mch_errmsg(argv[0]);
2361 mch_errmsg("\"\n");
2362 mch_exit(2);
2363 }
2364 break;
2365
2366#ifdef FEAT_GUI_W32
2367 case 'P': /* "-P {parent title}" MDI parent */
2368 gui_mch_set_parent(argv[0]);
2369 break;
2370#endif
2371 }
2372 }
2373 }
2374
2375 /*
2376 * File name argument.
2377 */
2378 else
2379 {
2380 argv_idx = -1; /* skip to next argument */
2381
2382 /* Check for only one type of editing. */
2383 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2384 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2385 parmp->edit_type = EDIT_FILE;
2386
2387#ifdef MSWIN
2388 /* Remember if the argument was a full path before changing
2389 * slashes to backslashes. */
2390 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2391 parmp->full_path = TRUE;
2392#endif
2393
2394 /* Add the file to the global argument list. */
2395 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2396 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2397 mch_exit(2);
2398#ifdef FEAT_DIFF
2399 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2400 && !mch_isdir(alist_name(&GARGLIST[0])))
2401 {
2402 char_u *r;
2403
2404 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2405 if (r != NULL)
2406 {
2407 vim_free(p);
2408 p = r;
2409 }
2410 }
2411#endif
2412#if defined(__CYGWIN32__) && !defined(WIN32)
2413 /*
2414 * If vim is invoked by non-Cygwin tools, convert away any
2415 * DOS paths, so things like .swp files are created correctly.
2416 * Look for evidence of non-Cygwin paths before we bother.
2417 * This is only for when using the Unix files.
2418 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002419 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002420 {
2421 char posix_path[PATH_MAX];
2422
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002423# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2424 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2425# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002426 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002427# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002428 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002429 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002430 if (p == NULL)
2431 mch_exit(2);
2432 }
2433#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002434
2435#ifdef USE_FNAME_CASE
2436 /* Make the case of the file name match the actual file. */
2437 fname_case(p, 0);
2438#endif
2439
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 alist_add(&global_alist, p,
2441#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002442 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002443#else
2444 2 /* add buffer number now and use curbuf */
2445#endif
2446 );
2447
2448#if defined(FEAT_MBYTE) && defined(WIN32)
2449 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002450 /* Remember this argument has been added to the argument list.
2451 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002452 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002453# ifdef FEAT_DIFF
2454 parmp->diff_mode
2455# else
2456 FALSE
2457# endif
2458 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459 }
2460#endif
2461 }
2462
2463 /*
2464 * If there are no more letters after the current "-", go to next
2465 * argument. argv_idx is set to -1 when the current argument is to be
2466 * skipped.
2467 */
2468 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2469 {
2470 --argc;
2471 ++argv;
2472 argv_idx = 1;
2473 }
2474 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002475
2476#ifdef FEAT_EVAL
2477 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2478 * one. */
2479 if (parmp->n_commands > 0)
2480 {
2481 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2482 if (p != NULL)
2483 {
2484 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2485 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2486 vim_free(p);
2487 }
2488 }
2489#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002490}
2491
2492/*
2493 * Print a warning if stdout is not a terminal.
2494 * When starting in Ex mode and commands come from a file, set Silent mode.
2495 */
2496 static void
2497check_tty(parmp)
2498 mparm_T *parmp;
2499{
2500 int input_isatty; /* is active input a terminal? */
2501
2502 input_isatty = mch_input_isatty();
2503 if (exmode_active)
2504 {
2505 if (!input_isatty)
2506 silent_mode = TRUE;
2507 }
2508 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2509#ifdef FEAT_GUI
2510 /* don't want the delay when started from the desktop */
2511 && !gui.starting
2512#endif
2513 )
2514 {
2515#ifdef NBDEBUG
2516 /*
2517 * This shouldn't be necessary. But if I run netbeans with the log
2518 * output coming to the console and XOpenDisplay fails, I get vim
2519 * trying to start with input/output to my console tty. This fills my
2520 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002521 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002522 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002523 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002524 {
2525 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2526 exit(1);
2527 }
2528#endif
2529 if (!parmp->stdout_isatty)
2530 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2531 if (!input_isatty)
2532 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2533 out_flush();
2534 if (scriptin[0] == NULL)
2535 ui_delay(2000L, TRUE);
2536 TIME_MSG("Warning delay");
2537 }
2538}
2539
2540/*
2541 * Read text from stdin.
2542 */
2543 static void
2544read_stdin()
2545{
2546 int i;
2547
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002548#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002549 /* When getting the ATTENTION prompt here, use a dialog */
2550 swap_exists_action = SEA_DIALOG;
2551#endif
2552 no_wait_return = TRUE;
2553 i = msg_didany;
2554 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002555 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002556 no_wait_return = FALSE;
2557 msg_didany = i;
2558 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002559#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002560 check_swap_exists_action();
2561#endif
2562#if !(defined(AMIGA) || defined(MACOS))
2563 /*
2564 * Close stdin and dup it from stderr. Required for GPM to work
2565 * properly, and for running external commands.
2566 * Is there any other system that cannot do this?
2567 */
2568 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002569 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002570#endif
2571}
2572
2573/*
2574 * Create the requested number of windows and edit buffers in them.
2575 * Also does recovery if "recoverymode" set.
2576 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002577 static void
2578create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002579 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002580{
2581#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002582 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002583 int done = 0;
2584
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002585 /*
2586 * Create the number of windows that was requested.
2587 */
2588 if (parmp->window_count == -1) /* was not set */
2589 parmp->window_count = 1;
2590 if (parmp->window_count == 0)
2591 parmp->window_count = GARGCOUNT;
2592 if (parmp->window_count > 1)
2593 {
2594 /* Don't change the windows if there was a command in .vimrc that
2595 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002596 if (parmp->window_layout == 0)
2597 parmp->window_layout = WIN_HOR;
2598 if (parmp->window_layout == WIN_TABS)
2599 {
2600 parmp->window_count = make_tabpages(parmp->window_count);
2601 TIME_MSG("making tab pages");
2602 }
2603 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002604 {
2605 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002606 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002607 TIME_MSG("making windows");
2608 }
2609 else
2610 parmp->window_count = win_count();
2611 }
2612 else
2613 parmp->window_count = 1;
2614#endif
2615
2616 if (recoverymode) /* do recover */
2617 {
2618 msg_scroll = TRUE; /* scroll message up */
2619 ml_recover();
2620 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2621 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002622 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002623 }
2624 else
2625 {
2626 /*
2627 * Open a buffer for windows that don't have one yet.
2628 * Commands in the .vimrc might have loaded a file or split the window.
2629 * Watch out for autocommands that delete a window.
2630 */
2631#ifdef FEAT_AUTOCMD
2632 /*
2633 * Don't execute Win/Buf Enter/Leave autocommands here
2634 */
2635 ++autocmd_no_enter;
2636 ++autocmd_no_leave;
2637#endif
2638#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002639 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002640 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002641 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002642 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002643 {
2644 if (parmp->window_layout == WIN_TABS)
2645 goto_tabpage(1);
2646 else
2647 curwin = firstwin;
2648 }
2649 else if (parmp->window_layout == WIN_TABS)
2650 {
2651 if (curtab->tp_next == NULL)
2652 break;
2653 goto_tabpage(0);
2654 }
2655 else
2656 {
2657 if (curwin->w_next == NULL)
2658 break;
2659 curwin = curwin->w_next;
2660 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002661 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002662#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002663 curbuf = curwin->w_buffer;
2664 if (curbuf->b_ml.ml_mfp == NULL)
2665 {
2666#ifdef FEAT_FOLDING
2667 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2668 if (p_fdls >= 0)
2669 curwin->w_p_fdl = p_fdls;
2670#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002671#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002672 /* When getting the ATTENTION prompt here, use a dialog */
2673 swap_exists_action = SEA_DIALOG;
2674#endif
2675 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002676
2677 /* create memfile, read file */
2678 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002679
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002680#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002681 if (swap_exists_action == SEA_QUIT)
2682 {
2683 if (got_int || only_one_window())
2684 {
2685 /* abort selected or quit and only one window */
2686 did_emsg = FALSE; /* avoid hit-enter prompt */
2687 getout(1);
2688 }
2689 /* We can't close the window, it would disturb what
2690 * happens next. Clear the file name and set the arg
2691 * index to -1 to delete it later. */
2692 setfname(curbuf, NULL, NULL, FALSE);
2693 curwin->w_arg_idx = -1;
2694 swap_exists_action = SEA_NONE;
2695 }
2696 else
2697 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002698#endif
2699#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002700 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002701#endif
2702 }
2703#ifdef FEAT_WINDOWS
2704 ui_breakcheck();
2705 if (got_int)
2706 {
2707 (void)vgetc(); /* only break the file loading, not the rest */
2708 break;
2709 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002710 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002711#endif
2712#ifdef FEAT_WINDOWS
2713 if (parmp->window_layout == WIN_TABS)
2714 goto_tabpage(1);
2715 else
2716 curwin = firstwin;
2717 curbuf = curwin->w_buffer;
2718#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719#ifdef FEAT_AUTOCMD
2720 --autocmd_no_enter;
2721 --autocmd_no_leave;
2722#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002723 }
2724}
2725
2726#ifdef FEAT_WINDOWS
2727 /*
2728 * If opened more than one window, start editing files in the other
2729 * windows. make_windows() has already opened the windows.
2730 */
2731 static void
2732edit_buffers(parmp)
2733 mparm_T *parmp;
2734{
2735 int arg_idx; /* index in argument list */
2736 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002737 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002738 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739
2740# ifdef FEAT_AUTOCMD
2741 /*
2742 * Don't execute Win/Buf Enter/Leave autocommands here
2743 */
2744 ++autocmd_no_enter;
2745 ++autocmd_no_leave;
2746# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002747
2748 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2749 if (curwin->w_arg_idx == -1)
2750 {
2751 win_close(curwin, TRUE);
2752 advance = FALSE;
2753 }
2754
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002755 arg_idx = 1;
2756 for (i = 1; i < parmp->window_count; ++i)
2757 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002758 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2759 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002760 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002761 ++arg_idx;
2762 win_close(curwin, TRUE);
2763 advance = FALSE;
2764 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002765 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002766
Bram Moolenaar84212822006-11-07 21:59:47 +00002767 if (advance)
2768 {
2769 if (parmp->window_layout == WIN_TABS)
2770 {
2771 if (curtab->tp_next == NULL) /* just checking */
2772 break;
2773 goto_tabpage(0);
2774 }
2775 else
2776 {
2777 if (curwin->w_next == NULL) /* just checking */
2778 break;
2779 win_enter(curwin->w_next, FALSE);
2780 }
2781 }
2782 advance = TRUE;
2783
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002784 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002785 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002786 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2787 {
2788 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002789 /* Edit file from arg list, if there is one. When "Quit" selected
2790 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002791# ifdef HAS_SWAP_EXISTS_ACTION
2792 swap_exists_did_quit = FALSE;
2793# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002794 (void)do_ecmd(0, arg_idx < GARGCOUNT
2795 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002796 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002797# ifdef HAS_SWAP_EXISTS_ACTION
2798 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002799 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002800 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002801 if (got_int || only_one_window())
2802 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002803 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002804 did_emsg = FALSE; /* avoid hit-enter prompt */
2805 getout(1);
2806 }
2807 win_close(curwin, TRUE);
2808 advance = FALSE;
2809 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002810# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002811 if (arg_idx == GARGCOUNT - 1)
2812 arg_had_last = TRUE;
2813 ++arg_idx;
2814 }
2815 ui_breakcheck();
2816 if (got_int)
2817 {
2818 (void)vgetc(); /* only break the file loading, not the rest */
2819 break;
2820 }
2821 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002822
2823 if (parmp->window_layout == WIN_TABS)
2824 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002825# ifdef FEAT_AUTOCMD
2826 --autocmd_no_enter;
2827# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002828
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002829 /* make the first window the current window */
2830 win = firstwin;
2831#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2832 /* Avoid making a preview window the current window. */
2833 while (win->w_p_pvw)
2834 {
2835 win = win->w_next;
2836 if (win == NULL)
2837 {
2838 win = firstwin;
2839 break;
2840 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002841 }
2842#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002843 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002844
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002845# ifdef FEAT_AUTOCMD
2846 --autocmd_no_leave;
2847# endif
2848 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002849 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002850 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2851}
2852#endif /* FEAT_WINDOWS */
2853
2854/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002855 * Execute the commands from --cmd arguments "cmds[cnt]".
2856 */
2857 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002858exe_pre_commands(parmp)
2859 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002860{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002861 char_u **cmds = parmp->pre_commands;
2862 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002863 int i;
2864
2865 if (cnt > 0)
2866 {
2867 curwin->w_cursor.lnum = 0; /* just in case.. */
2868 sourcing_name = (char_u *)_("pre-vimrc command line");
2869# ifdef FEAT_EVAL
2870 current_SID = SID_CMDARG;
2871# endif
2872 for (i = 0; i < cnt; ++i)
2873 do_cmdline_cmd(cmds[i]);
2874 sourcing_name = NULL;
2875# ifdef FEAT_EVAL
2876 current_SID = 0;
2877# endif
2878 TIME_MSG("--cmd commands");
2879 }
2880}
2881
2882/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002883 * Execute "+", "-c" and "-S" arguments.
2884 */
2885 static void
2886exe_commands(parmp)
2887 mparm_T *parmp;
2888{
2889 int i;
2890
2891 /*
2892 * We start commands on line 0, make "vim +/pat file" match a
2893 * pattern on line 1. But don't move the cursor when an autocommand
2894 * with g`" was used.
2895 */
2896 msg_scroll = TRUE;
2897 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2898 curwin->w_cursor.lnum = 0;
2899 sourcing_name = (char_u *)"command line";
2900#ifdef FEAT_EVAL
2901 current_SID = SID_CARG;
2902#endif
2903 for (i = 0; i < parmp->n_commands; ++i)
2904 {
2905 do_cmdline_cmd(parmp->commands[i]);
2906 if (parmp->cmds_tofree[i])
2907 vim_free(parmp->commands[i]);
2908 }
2909 sourcing_name = NULL;
2910#ifdef FEAT_EVAL
2911 current_SID = 0;
2912#endif
2913 if (curwin->w_cursor.lnum == 0)
2914 curwin->w_cursor.lnum = 1;
2915
2916 if (!exmode_active)
2917 msg_scroll = FALSE;
2918
2919#ifdef FEAT_QUICKFIX
2920 /* When started with "-q errorfile" jump to first error again. */
2921 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002922 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002923#endif
2924 TIME_MSG("executing command arguments");
2925}
2926
2927/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002928 * Source startup scripts.
2929 */
2930 static void
2931source_startup_scripts(parmp)
2932 mparm_T *parmp;
2933{
2934 int i;
2935
2936 /*
2937 * For "evim" source evim.vim first of all, so that the user can overrule
2938 * any things he doesn't like.
2939 */
2940 if (parmp->evim_mode)
2941 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002942 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002943 TIME_MSG("source evim file");
2944 }
2945
2946 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002947 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002948 * nothing else.
2949 */
2950 if (parmp->use_vimrc != NULL)
2951 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002952 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2953 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002954 {
2955#ifdef FEAT_GUI
2956 if (use_gvimrc == NULL) /* don't load gvimrc either */
2957 use_gvimrc = parmp->use_vimrc;
2958#endif
2959 if (parmp->use_vimrc[2] == 'N')
2960 p_lpl = FALSE; /* don't load plugins either */
2961 }
2962 else
2963 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002964 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002965 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2966 }
2967 }
2968 else if (!silent_mode)
2969 {
2970#ifdef AMIGA
2971 struct Process *proc = (struct Process *)FindTask(0L);
2972 APTR save_winptr = proc->pr_WindowPtr;
2973
2974 /* Avoid a requester here for a volume that doesn't exist. */
2975 proc->pr_WindowPtr = (APTR)-1L;
2976#endif
2977
2978 /*
2979 * Get system wide defaults, if the file name is defined.
2980 */
2981#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002982 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002983#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002984#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002985 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002986#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002987
2988 /*
2989 * Try to read initialization commands from the following places:
2990 * - environment variable VIMINIT
2991 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2992 * - second user vimrc file ($VIM/.vimrc for Dos)
2993 * - environment variable EXINIT
2994 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2995 * - second user exrc file ($VIM/.exrc for Dos)
2996 * The first that exists is used, the rest is ignored.
2997 */
2998 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2999 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003000 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003002 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3003 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004#endif
3005#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003006 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3007 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003009#ifdef USR_VIMRC_FILE4
3010 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3011 DOSO_VIMRC) == FAIL
3012#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003013 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003014 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003015 {
3016#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003017 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003018#endif
3019 }
3020 }
3021
3022 /*
3023 * Read initialization commands from ".vimrc" or ".exrc" in current
3024 * directory. This is only done if the 'exrc' option is set.
3025 * Because of security reasons we disallow shell and write commands
3026 * now, except for unix if the file is owned by the user or 'secure'
3027 * option has been reset in environment of global ".exrc" or ".vimrc".
3028 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3029 * SYS_VIMRC_FILE.
3030 */
3031 if (p_exrc)
3032 {
3033#if defined(UNIX) || defined(VMS)
3034 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3035 if (!file_owned(VIMRC_FILE))
3036#endif
3037 secure = p_secure;
3038
3039 i = FAIL;
3040 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3041 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3042#ifdef USR_VIMRC_FILE2
3043 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3044 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3045#endif
3046#ifdef USR_VIMRC_FILE3
3047 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3048 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3049#endif
3050#ifdef SYS_VIMRC_FILE
3051 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3052 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3053#endif
3054 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003055 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003056
3057 if (i == FAIL)
3058 {
3059#if defined(UNIX) || defined(VMS)
3060 /* if ".exrc" is not owned by user set 'secure' mode */
3061 if (!file_owned(EXRC_FILE))
3062 secure = p_secure;
3063 else
3064 secure = 0;
3065#endif
3066 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3067 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3068#ifdef USR_EXRC_FILE2
3069 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3070 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3071#endif
3072 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003073 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003074 }
3075 }
3076 if (secure == 2)
3077 need_wait_return = TRUE;
3078 secure = 0;
3079#ifdef AMIGA
3080 proc->pr_WindowPtr = save_winptr;
3081#endif
3082 }
3083 TIME_MSG("sourcing vimrc file(s)");
3084}
3085
3086/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003087 * Setup to start using the GUI. Exit with an error when not available.
3088 */
3089 static void
3090main_start_gui()
3091{
3092#ifdef FEAT_GUI
3093 gui.starting = TRUE; /* start GUI a bit later */
3094#else
3095 mch_errmsg(_(e_nogvim));
3096 mch_errmsg("\n");
3097 mch_exit(2);
3098#endif
3099}
3100
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003101#endif /* NO_VIM_MAIN */
3102
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003103/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003104 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003105 * Returns FAIL if the environment variable was not executed, OK otherwise.
3106 */
3107 int
3108process_env(env, is_viminit)
3109 char_u *env;
3110 int is_viminit; /* when TRUE, called for VIMINIT */
3111{
3112 char_u *initstr;
3113 char_u *save_sourcing_name;
3114 linenr_T save_sourcing_lnum;
3115#ifdef FEAT_EVAL
3116 scid_T save_sid;
3117#endif
3118
3119 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3120 {
3121 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003122 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003123 save_sourcing_name = sourcing_name;
3124 save_sourcing_lnum = sourcing_lnum;
3125 sourcing_name = env;
3126 sourcing_lnum = 0;
3127#ifdef FEAT_EVAL
3128 save_sid = current_SID;
3129 current_SID = SID_ENV;
3130#endif
3131 do_cmdline_cmd(initstr);
3132 sourcing_name = save_sourcing_name;
3133 sourcing_lnum = save_sourcing_lnum;
3134#ifdef FEAT_EVAL
3135 current_SID = save_sid;;
3136#endif
3137 return OK;
3138 }
3139 return FAIL;
3140}
3141
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003142#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003143/*
3144 * Return TRUE if we are certain the user owns the file "fname".
3145 * Used for ".vimrc" and ".exrc".
3146 * Use both stat() and lstat() for extra security.
3147 */
3148 static int
3149file_owned(fname)
3150 char *fname;
3151{
3152 struct stat s;
3153# ifdef UNIX
3154 uid_t uid = getuid();
3155# else /* VMS */
3156 uid_t uid = ((getgid() << 16) | getuid());
3157# endif
3158
3159 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3160# ifdef HAVE_LSTAT
3161 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3162# endif
3163 );
3164}
3165#endif
3166
3167/*
3168 * Give an error message main_errors["n"] and exit.
3169 */
3170 static void
3171mainerr(n, str)
3172 int n; /* one of the ME_ defines */
3173 char_u *str; /* extra argument or NULL */
3174{
3175#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3176 reset_signals(); /* kill us with CTRL-C here, if you like */
3177#endif
3178
3179 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003180 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003181 mch_errmsg(_(main_errors[n]));
3182 if (str != NULL)
3183 {
3184 mch_errmsg(": \"");
3185 mch_errmsg((char *)str);
3186 mch_errmsg("\"");
3187 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003188 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003189
3190 mch_exit(1);
3191}
3192
3193 void
3194mainerr_arg_missing(str)
3195 char_u *str;
3196{
3197 mainerr(ME_ARG_MISSING, str);
3198}
3199
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003200#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003201/*
3202 * print a message with three spaces prepended and '\n' appended.
3203 */
3204 static void
3205main_msg(s)
3206 char *s;
3207{
3208 mch_msg(" ");
3209 mch_msg(s);
3210 mch_msg("\n");
3211}
3212
3213/*
3214 * Print messages for "vim -h" or "vim --help" and exit.
3215 */
3216 static void
3217usage()
3218{
3219 int i;
3220 static char *(use[]) =
3221 {
3222 N_("[file ..] edit specified file(s)"),
3223 N_("- read text from stdin"),
3224 N_("-t tag edit file where tag is defined"),
3225#ifdef FEAT_QUICKFIX
3226 N_("-q [errorfile] edit file with first error")
3227#endif
3228 };
3229
3230#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3231 reset_signals(); /* kill us with CTRL-C here, if you like */
3232#endif
3233
3234 mch_msg(longVersion);
3235 mch_msg(_("\n\nusage:"));
3236 for (i = 0; ; ++i)
3237 {
3238 mch_msg(_(" vim [arguments] "));
3239 mch_msg(_(use[i]));
3240 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3241 break;
3242 mch_msg(_("\n or:"));
3243 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003244#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003245 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003246#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003247
3248 mch_msg(_("\n\nArguments:\n"));
3249 main_msg(_("--\t\t\tOnly file names after this"));
3250#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3251 main_msg(_("--literal\t\tDon't expand wildcards"));
3252#endif
3253#ifdef FEAT_OLE
3254 main_msg(_("-register\t\tRegister this gvim for OLE"));
3255 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3256#endif
3257#ifdef FEAT_GUI
3258 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3259 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3260#endif
3261 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3262 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003263 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003264 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3265#ifdef FEAT_DIFF
3266 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3267#endif
3268 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3269 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3270 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3271 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3272 main_msg(_("-M\t\t\tModifications in text not allowed"));
3273 main_msg(_("-b\t\t\tBinary mode"));
3274#ifdef FEAT_LISP
3275 main_msg(_("-l\t\t\tLisp mode"));
3276#endif
3277 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3278 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003279 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3280#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003281 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003282#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003283 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3284 main_msg(_("-r\t\t\tList swap files and exit"));
3285 main_msg(_("-r (with file name)\tRecover crashed session"));
3286 main_msg(_("-L\t\t\tSame as -r"));
3287#ifdef AMIGA
3288 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3289 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3290#endif
3291#ifdef FEAT_ARABIC
3292 main_msg(_("-A\t\t\tstart in Arabic mode"));
3293#endif
3294#ifdef FEAT_RIGHTLEFT
3295 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3296#endif
3297#ifdef FEAT_FKMAP
3298 main_msg(_("-F\t\t\tStart in Farsi mode"));
3299#endif
3300 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3301 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3302#ifdef FEAT_GUI
3303 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3304#endif
3305 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003306#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003307 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003308 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3309 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003310#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003311 main_msg(_("+\t\t\tStart at end of file"));
3312 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003313 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003314 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3315 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3316 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3317 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3318 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3319#ifdef FEAT_CRYPT
3320 main_msg(_("-x\t\t\tEdit encrypted files"));
3321#endif
3322#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3323# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3324 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3325# endif
3326 main_msg(_("-X\t\t\tDo not connect to X server"));
3327#endif
3328#ifdef FEAT_CLIENTSERVER
3329 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3330 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3331 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3332 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003333# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003334 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003335# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003336 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3337 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3338 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3339 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3340#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003341#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003342 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003343#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003344#ifdef FEAT_VIMINFO
3345 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3346#endif
3347 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3348 main_msg(_("--version\t\tPrint version information and exit"));
3349
3350#ifdef FEAT_GUI_X11
3351# ifdef FEAT_GUI_MOTIF
3352 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3353# else
3354# ifdef FEAT_GUI_ATHENA
3355# ifdef FEAT_GUI_NEXTAW
3356 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3357# else
3358 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3359# endif
3360# endif
3361# endif
3362 main_msg(_("-display <display>\tRun vim on <display>"));
3363 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003364 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3365 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3366 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3367 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3368 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3369 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3370 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3371 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3372# ifdef FEAT_GUI_ATHENA
3373 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3374# endif
3375 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3376 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3377 main_msg(_("-xrm <resource>\tSet the specified resource"));
3378#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003379#ifdef FEAT_GUI_GTK
3380 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3381 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3382 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3383 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3384 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003385 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003386 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003387 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003388#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003389#ifdef FEAT_GUI_W32
3390 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003391 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003392#endif
3393
3394#ifdef FEAT_GUI_GNOME
3395 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3396 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003397 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003398 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003399 gui.dofork = FALSE;
3400 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003401 else
3402#endif
3403 mch_exit(0);
3404}
3405
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003406#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003407/*
3408 * Check the result of the ATTENTION dialog:
3409 * When "Quit" selected, exit Vim.
3410 * When "Recover" selected, recover the file.
3411 */
3412 static void
3413check_swap_exists_action()
3414{
3415 if (swap_exists_action == SEA_QUIT)
3416 getout(1);
3417 handle_swap_exists(NULL);
3418}
3419#endif
3420
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003421#endif
3422
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003423#if defined(STARTUPTIME) || defined(PROTO)
3424static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3425
3426static struct timeval prev_timeval;
3427
Bram Moolenaar3f269672009-11-03 11:11:11 +00003428# ifdef WIN3264
3429/*
3430 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3431 */
3432 static int
3433gettimeofday(struct timeval *tv, char *dummy)
3434{
3435 long t = clock();
3436 tv->tv_sec = t / CLOCKS_PER_SEC;
3437 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3438 return 0;
3439}
3440# endif
3441
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003442/*
3443 * Save the previous time before doing something that could nest.
3444 * set "*tv_rel" to the time elapsed so far.
3445 */
3446 void
3447time_push(tv_rel, tv_start)
3448 void *tv_rel, *tv_start;
3449{
3450 *((struct timeval *)tv_rel) = prev_timeval;
3451 gettimeofday(&prev_timeval, NULL);
3452 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3453 - ((struct timeval *)tv_rel)->tv_usec;
3454 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3455 - ((struct timeval *)tv_rel)->tv_sec;
3456 if (((struct timeval *)tv_rel)->tv_usec < 0)
3457 {
3458 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3459 --((struct timeval *)tv_rel)->tv_sec;
3460 }
3461 *(struct timeval *)tv_start = prev_timeval;
3462}
3463
3464/*
3465 * Compute the previous time after doing something that could nest.
3466 * Subtract "*tp" from prev_timeval;
3467 * Note: The arguments are (void *) to avoid trouble with systems that don't
3468 * have struct timeval.
3469 */
3470 void
3471time_pop(tp)
3472 void *tp; /* actually (struct timeval *) */
3473{
3474 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3475 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3476 if (prev_timeval.tv_usec < 0)
3477 {
3478 prev_timeval.tv_usec += 1000000;
3479 --prev_timeval.tv_sec;
3480 }
3481}
3482
3483 static void
3484time_diff(then, now)
3485 struct timeval *then;
3486 struct timeval *now;
3487{
3488 long usec;
3489 long msec;
3490
3491 usec = now->tv_usec - then->tv_usec;
3492 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3493 usec = usec % 1000L;
3494 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3495}
3496
3497 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003498time_msg(mesg, tv_start)
3499 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003500 void *tv_start; /* only for do_source: start time; actually
3501 (struct timeval *) */
3502{
3503 static struct timeval start;
3504 struct timeval now;
3505
3506 if (time_fd != NULL)
3507 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003508 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003509 {
3510 gettimeofday(&start, NULL);
3511 prev_timeval = start;
3512 fprintf(time_fd, "\n\ntimes in msec\n");
3513 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3514 fprintf(time_fd, " clock elapsed: other lines\n\n");
3515 }
3516 gettimeofday(&now, NULL);
3517 time_diff(&start, &now);
3518 if (((struct timeval *)tv_start) != NULL)
3519 {
3520 fprintf(time_fd, " ");
3521 time_diff(((struct timeval *)tv_start), &now);
3522 }
3523 fprintf(time_fd, " ");
3524 time_diff(&prev_timeval, &now);
3525 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003526 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003527 }
3528}
3529
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003530#endif
3531
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003532#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003533
3534/*
3535 * Common code for the X command server and the Win32 command server.
3536 */
3537
Bram Moolenaareb94e552006-03-11 21:35:11 +00003538static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003539
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003540/*
3541 * Do the client-server stuff, unless "--servername ''" was used.
3542 */
3543 static void
3544exec_on_server(parmp)
3545 mparm_T *parmp;
3546{
3547 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3548 {
3549# ifdef WIN32
3550 /* Initialise the client/server messaging infrastructure. */
3551 serverInitMessaging();
3552# endif
3553
3554 /*
3555 * When a command server argument was found, execute it. This may
3556 * exit Vim when it was successful. Otherwise it's executed further
3557 * on. Remember the encoding used here in "serverStrEnc".
3558 */
3559 if (parmp->serverArg)
3560 {
3561 cmdsrv_main(&parmp->argc, parmp->argv,
3562 parmp->serverName_arg, &parmp->serverStr);
3563# ifdef FEAT_MBYTE
3564 parmp->serverStrEnc = vim_strsave(p_enc);
3565# endif
3566 }
3567
3568 /* If we're still running, get the name to register ourselves.
3569 * On Win32 can register right now, for X11 need to setup the
3570 * clipboard first, it's further down. */
3571 parmp->servername = serverMakeName(parmp->serverName_arg,
3572 parmp->argv[0]);
3573# ifdef WIN32
3574 if (parmp->servername != NULL)
3575 {
3576 serverSetName(parmp->servername);
3577 vim_free(parmp->servername);
3578 }
3579# endif
3580 }
3581}
3582
3583/*
3584 * Prepare for running as a Vim server.
3585 */
3586 static void
3587prepare_server(parmp)
3588 mparm_T *parmp;
3589{
3590# if defined(FEAT_X11)
3591 /*
3592 * Register for remote command execution with :serversend and --remote
3593 * unless there was a -X or a --servername '' on the command line.
3594 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003595 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003596 */
3597 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3598# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003599 (gui.in_use
3600# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003601 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003602# endif
3603 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003604# endif
3605 parmp->serverName_arg != NULL))
3606 {
3607 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3608 vim_free(parmp->servername);
3609 TIME_MSG("register server name");
3610 }
3611 else
3612 serverDelayedStartName = parmp->servername;
3613# endif
3614
3615 /*
3616 * Execute command ourselves if we're here because the send failed (or
3617 * else we would have exited above).
3618 */
3619 if (parmp->serverStr != NULL)
3620 {
3621 char_u *p;
3622
3623 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3624 parmp->serverStr, &p));
3625 vim_free(p);
3626 }
3627}
3628
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003629 static void
3630cmdsrv_main(argc, argv, serverName_arg, serverStr)
3631 int *argc;
3632 char **argv;
3633 char_u *serverName_arg;
3634 char_u **serverStr;
3635{
3636 char_u *res;
3637 int i;
3638 char_u *sname;
3639 int ret;
3640 int didone = FALSE;
3641 int exiterr = 0;
3642 char **newArgV = argv + 1;
3643 int newArgC = 1,
3644 Argc = *argc;
3645 int argtype;
3646#define ARGTYPE_OTHER 0
3647#define ARGTYPE_EDIT 1
3648#define ARGTYPE_EDIT_WAIT 2
3649#define ARGTYPE_SEND 3
3650 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003651 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003652# ifndef FEAT_X11
3653 HWND srv;
3654# else
3655 Window srv;
3656
3657 setup_term_clip();
3658# endif
3659
3660 sname = serverMakeName(serverName_arg, argv[0]);
3661 if (sname == NULL)
3662 return;
3663
3664 /*
3665 * Execute the command server related arguments and remove them
3666 * from the argc/argv array; We may have to return into main()
3667 */
3668 for (i = 1; i < Argc; i++)
3669 {
3670 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003671 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003672 {
3673 for (; i < *argc; i++)
3674 {
3675 *newArgV++ = argv[i];
3676 newArgC++;
3677 }
3678 break;
3679 }
3680
Bram Moolenaareb94e552006-03-11 21:35:11 +00003681 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003682 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003683 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3684 {
3685 char *p = argv[i] + 8;
3686
3687 argtype = ARGTYPE_EDIT;
3688 while (*p != NUL)
3689 {
3690 if (STRNICMP(p, "-wait", 5) == 0)
3691 {
3692 argtype = ARGTYPE_EDIT_WAIT;
3693 p += 5;
3694 }
3695 else if (STRNICMP(p, "-silent", 7) == 0)
3696 {
3697 silent = TRUE;
3698 p += 7;
3699 }
3700 else if (STRNICMP(p, "-tab", 4) == 0)
3701 {
3702 tabs = TRUE;
3703 p += 4;
3704 }
3705 else
3706 {
3707 argtype = ARGTYPE_OTHER;
3708 break;
3709 }
3710 }
3711 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003712 else
3713 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003714
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003715 if (argtype != ARGTYPE_OTHER)
3716 {
3717 if (i == *argc - 1)
3718 mainerr_arg_missing((char_u *)argv[i]);
3719 if (argtype == ARGTYPE_SEND)
3720 {
3721 *serverStr = (char_u *)argv[i + 1];
3722 i++;
3723 }
3724 else
3725 {
3726 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003727 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003728 if (*serverStr == NULL)
3729 {
3730 /* Probably out of memory, exit. */
3731 didone = TRUE;
3732 exiterr = 1;
3733 break;
3734 }
3735 Argc = i;
3736 }
3737# ifdef FEAT_X11
3738 if (xterm_dpy == NULL)
3739 {
3740 mch_errmsg(_("No display"));
3741 ret = -1;
3742 }
3743 else
3744 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3745 NULL, &srv, 0, 0, silent);
3746# else
3747 /* Win32 always works? */
3748 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3749# endif
3750 if (ret < 0)
3751 {
3752 if (argtype == ARGTYPE_SEND)
3753 {
3754 /* Failed to send, abort. */
3755 mch_errmsg(_(": Send failed.\n"));
3756 didone = TRUE;
3757 exiterr = 1;
3758 }
3759 else if (!silent)
3760 /* Let vim start normally. */
3761 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3762 break;
3763 }
3764
3765# ifdef FEAT_GUI_W32
3766 /* Guess that when the server name starts with "g" it's a GUI
3767 * server, which we can bring to the foreground here.
3768 * Foreground() in the server doesn't work very well. */
3769 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3770 SetForegroundWindow(srv);
3771# endif
3772
3773 /*
3774 * For --remote-wait: Wait until the server did edit each
3775 * file. Also detect that the server no longer runs.
3776 */
3777 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3778 {
3779 int numFiles = *argc - i - 1;
3780 int j;
3781 char_u *done = alloc(numFiles);
3782 char_u *p;
3783# ifdef FEAT_GUI_W32
3784 NOTIFYICONDATA ni;
3785 int count = 0;
3786 extern HWND message_window;
3787# endif
3788
3789 if (numFiles > 0 && argv[i + 1][0] == '+')
3790 /* Skip "+cmd" argument, don't wait for it to be edited. */
3791 --numFiles;
3792
3793# ifdef FEAT_GUI_W32
3794 ni.cbSize = sizeof(ni);
3795 ni.hWnd = message_window;
3796 ni.uID = 0;
3797 ni.uFlags = NIF_ICON|NIF_TIP;
3798 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3799 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3800 Shell_NotifyIcon(NIM_ADD, &ni);
3801# endif
3802
3803 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003804 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003805 while (memchr(done, 0, numFiles) != NULL)
3806 {
3807# ifdef WIN32
3808 p = serverGetReply(srv, NULL, TRUE, TRUE);
3809 if (p == NULL)
3810 break;
3811# else
3812 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3813 break;
3814# endif
3815 j = atoi((char *)p);
3816 if (j >= 0 && j < numFiles)
3817 {
3818# ifdef FEAT_GUI_W32
3819 ++count;
3820 sprintf(ni.szTip, _("%d of %d edited"),
3821 count, numFiles);
3822 Shell_NotifyIcon(NIM_MODIFY, &ni);
3823# endif
3824 done[j] = 1;
3825 }
3826 }
3827# ifdef FEAT_GUI_W32
3828 Shell_NotifyIcon(NIM_DELETE, &ni);
3829# endif
3830 }
3831 }
3832 else if (STRICMP(argv[i], "--remote-expr") == 0)
3833 {
3834 if (i == *argc - 1)
3835 mainerr_arg_missing((char_u *)argv[i]);
3836# ifdef WIN32
3837 /* Win32 always works? */
3838 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3839 &res, NULL, 1, FALSE) < 0)
3840# else
3841 if (xterm_dpy == NULL)
3842 mch_errmsg(_("No display: Send expression failed.\n"));
3843 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3844 &res, NULL, 1, 1, FALSE) < 0)
3845# endif
3846 {
3847 if (res != NULL && *res != NUL)
3848 {
3849 /* Output error from remote */
3850 mch_errmsg((char *)res);
3851 vim_free(res);
3852 res = NULL;
3853 }
3854 mch_errmsg(_(": Send expression failed.\n"));
3855 }
3856 }
3857 else if (STRICMP(argv[i], "--serverlist") == 0)
3858 {
3859# ifdef WIN32
3860 /* Win32 always works? */
3861 res = serverGetVimNames();
3862# else
3863 if (xterm_dpy != NULL)
3864 res = serverGetVimNames(xterm_dpy);
3865# endif
3866 if (called_emsg)
3867 mch_errmsg("\n");
3868 }
3869 else if (STRICMP(argv[i], "--servername") == 0)
3870 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003871 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003872 i++;
3873 continue;
3874 }
3875 else
3876 {
3877 *newArgV++ = argv[i];
3878 newArgC++;
3879 continue;
3880 }
3881 didone = TRUE;
3882 if (res != NULL && *res != NUL)
3883 {
3884 mch_msg((char *)res);
3885 if (res[STRLEN(res) - 1] != '\n')
3886 mch_msg("\n");
3887 }
3888 vim_free(res);
3889 }
3890
3891 if (didone)
3892 {
3893 display_errors(); /* display any collected messages */
3894 exit(exiterr); /* Mission accomplished - get out */
3895 }
3896
3897 /* Return back into main() */
3898 *argc = newArgC;
3899 vim_free(sname);
3900}
3901
3902/*
3903 * Build a ":drop" command to send to a Vim server.
3904 */
3905 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003906build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003907 int filec;
3908 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003909 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003910 int sendReply;
3911{
3912 garray_T ga;
3913 int i;
3914 char_u *inicmd = NULL;
3915 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003916 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003917
3918 if (filec > 0 && filev[0][0] == '+')
3919 {
3920 inicmd = (char_u *)filev[0] + 1;
3921 filev++;
3922 filec--;
3923 }
3924 /* Check if we have at least one argument. */
3925 if (filec <= 0)
3926 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003927
3928 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003929 cwd = alloc(MAXPATHL);
3930 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003931 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003932 if (mch_dirname(cwd, MAXPATHL) != OK)
3933 {
3934 vim_free(cwd);
3935 return NULL;
3936 }
3937 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003938#ifdef BACKSLASH_IN_FILENAME
3939 "", /* rem_backslash() will tell what chars to escape */
3940#else
3941 PATH_ESC_CHARS,
3942#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003943 '\\', TRUE);
3944 vim_free(cwd);
3945 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003946 return NULL;
3947 ga_init2(&ga, 1, 100);
3948 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3949 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003950 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003951
3952 /* Call inputsave() so that a prompt for an encryption key works. */
3953 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3954 if (tabs)
3955 ga_concat(&ga, (char_u *)"tab ");
3956 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003957 for (i = 0; i < filec; i++)
3958 {
3959 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003960 * do it again in the Vim server. On MS-Windows only escape
3961 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003962 p = vim_strsave_escaped((char_u *)filev[i],
3963#ifdef UNIX
3964 PATH_ESC_CHARS
3965#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003966 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003967#endif
3968 );
3969 if (p == NULL)
3970 {
3971 vim_free(ga.ga_data);
3972 return NULL;
3973 }
3974 ga_concat(&ga, (char_u *)" ");
3975 ga_concat(&ga, p);
3976 vim_free(p);
3977 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003978 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3979
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003980 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3981 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003982 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3983
3984 /* Switch back to the correct current directory (prior to temporary path
3985 * switch) unless 'autochdir' is set, in which case it will already be
3986 * correct after the :drop command. */
3987 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3988
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003989 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003990 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3991 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003992 if (inicmd != NULL)
3993 {
3994 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3995 * the following commands to be inserted as text. Use a "|",
3996 * hopefully "inicmd" does allow this... */
3997 ga_concat(&ga, inicmd);
3998 ga_concat(&ga, (char_u *)"|");
3999 }
4000 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4001 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004002 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004003 ga_append(&ga, NUL);
4004 return ga.ga_data;
4005}
4006
4007/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004008 * Make our basic server name: use the specified "arg" if given, otherwise use
4009 * the tail of the command "cmd" we were started with.
4010 * Return the name in allocated memory. This doesn't include a serial number.
4011 */
4012 static char_u *
4013serverMakeName(arg, cmd)
4014 char_u *arg;
4015 char *cmd;
4016{
4017 char_u *p;
4018
4019 if (arg != NULL && *arg != NUL)
4020 p = vim_strsave_up(arg);
4021 else
4022 {
4023 p = vim_strsave_up(gettail((char_u *)cmd));
4024 /* Remove .exe or .bat from the name. */
4025 if (p != NULL && vim_strchr(p, '.') != NULL)
4026 *vim_strchr(p, '.') = NUL;
4027 }
4028 return p;
4029}
4030#endif /* FEAT_CLIENTSERVER */
4031
4032#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4033/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004034 * Replace termcodes such as <CR> and insert as key presses if there is room.
4035 */
4036 void
4037server_to_input_buf(str)
4038 char_u *str;
4039{
4040 char_u *ptr = NULL;
4041 char_u *cpo_save = p_cpo;
4042
4043 /* Set 'cpoptions' the way we want it.
4044 * B set - backslashes are *not* treated specially
4045 * k set - keycodes are *not* reverse-engineered
4046 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004047 * The last but one parameter of replace_termcodes() is TRUE so that the
4048 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004049 */
4050 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004051 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004052 p_cpo = cpo_save;
4053
4054 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4055 {
4056 /*
4057 * Add the string to the input stream.
4058 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4059 *
4060 * First clear typed characters from the typeahead buffer, there could
4061 * be half a mapping there. Then append to the existing string, so
4062 * that multiple commands from a client are concatenated.
4063 */
4064 if (typebuf.tb_maplen < typebuf.tb_len)
4065 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4066 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4067
4068 /* Let input_available() know we inserted text in the typeahead
4069 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004070 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004071 }
4072 vim_free((char_u *)ptr);
4073}
4074
4075/*
4076 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004077 */
4078 char_u *
4079eval_client_expr_to_string(expr)
4080 char_u *expr;
4081{
4082 char_u *res;
4083 int save_dbl = debug_break_level;
4084 int save_ro = redir_off;
4085
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004086 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4087 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004088 debug_break_level = -1;
4089 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004090 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4091 * to be typed. Do generate errors so that try/catch works. */
4092 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004093
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004094 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004095
4096 debug_break_level = save_dbl;
4097 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004098 --emsg_silent;
4099 if (emsg_silent < 0)
4100 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004101
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004102 /* A client can tell us to redraw, but not to display the cursor, so do
4103 * that here. */
4104 setcursor();
4105 out_flush();
4106#ifdef FEAT_GUI
4107 if (gui.in_use)
4108 gui_update_cursor(FALSE, FALSE);
4109#endif
4110
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004111 return res;
4112}
4113
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004114/*
4115 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4116 * return an allocated string. Otherwise return "data".
4117 * "*tofree" is set to the result when it needs to be freed later.
4118 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004119 char_u *
4120serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004121 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004122 char_u *data;
4123 char_u **tofree;
4124{
4125 char_u *res = data;
4126
4127 *tofree = NULL;
4128# ifdef FEAT_MBYTE
4129 if (client_enc != NULL && p_enc != NULL)
4130 {
4131 vimconv_T vimconv;
4132
4133 vimconv.vc_type = CONV_NONE;
4134 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4135 && vimconv.vc_type != CONV_NONE)
4136 {
4137 res = string_convert(&vimconv, data, NULL);
4138 if (res == NULL)
4139 res = data;
4140 else
4141 *tofree = res;
4142 }
4143 convert_setup(&vimconv, NULL, NULL);
4144 }
4145# endif
4146 return res;
4147}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004148#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004149
4150/*
4151 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4152 */
4153#if defined(FEAT_FKMAP) || defined(PROTO)
4154# include "farsi.c"
4155#endif
4156
4157/*
4158 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4159 */
4160#if defined(FEAT_ARABIC) || defined(PROTO)
4161# include "arabic.c"
4162#endif