blob: 58062c3925b64f16b7f507ab989080b93b311e55 [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
13#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000014# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000015#endif
16
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017#ifdef __CYGWIN__
18# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000019# include <cygwin/version.h>
20# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
21 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000022# endif
23# include <limits.h>
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Maximum number of commands from + or -c arguments. */
27#define MAX_ARG_CMDS 10
28
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000029/* values for "window_layout" */
30#define WIN_HOR 1 /* "-o" horizontally split windows */
31#define WIN_VER 2 /* "-O" vertically split windows */
32#define WIN_TABS 3 /* "-p" windows on tab pages */
33
Bram Moolenaar58d98232005-07-23 22:25:46 +000034/* Struct for various parameters passed between main() and other functions. */
35typedef struct
36{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 int argc;
38 char **argv;
39
40 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000041 char_u *use_vimrc; /* vimrc from -u argument */
42
43 int n_commands; /* no. of commands from + or -c */
44 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
45 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
46 int n_pre_commands; /* no. of commands from --cmd */
47 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
48
49 int edit_type; /* type of editing to do */
50 char_u *tagname; /* tag from -t argument */
51#ifdef FEAT_QUICKFIX
52 char_u *use_ef; /* 'errorfile' from -q argument */
53#endif
54
55 int want_full_screen;
56 int stdout_isatty; /* is stdout a terminal? */
57 char_u *term; /* specified terminal name */
58#ifdef FEAT_CRYPT
59 int ask_for_key; /* -x argument */
60#endif
61 int no_swap_file; /* "-n" argument used */
62#ifdef FEAT_EVAL
63 int use_debug_break_level;
64#endif
65#ifdef FEAT_WINDOWS
66 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000067 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000068#endif
69
70#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000071 int serverArg; /* TRUE when argument for a server */
72 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000073 char_u *serverStr; /* remote server command */
74 char_u *serverStrEnc; /* encoding of serverStr */
75 char_u *servername; /* allocated name for our server */
76#endif
77#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
78 int literal; /* don't expand file names */
79#endif
80#ifdef MSWIN
81 int full_path; /* file name argument was full path */
82#endif
83#ifdef FEAT_DIFF
84 int diff_mode; /* start with 'diff' set */
85#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000086} mparm_T;
87
Bram Moolenaarc013cb62005-07-24 21:18:31 +000088/* Values for edit_type. */
89#define EDIT_NONE 0 /* no edit type yet */
90#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
91#define EDIT_STDIN 2 /* read file from stdin */
92#define EDIT_TAG 3 /* tag name argument given, use tagname */
93#define EDIT_QF 4 /* start in quickfix mode */
94
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010095#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000096static int file_owned __ARGS((char *fname));
97#endif
98static void mainerr __ARGS((int, char_u *));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010099#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100static void main_msg __ARGS((char *s));
101static void usage __ARGS((void));
102static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100103# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000104static void init_locale __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100105# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000106static void parse_command_name __ARGS((mparm_T *parmp));
107static void early_arg_scan __ARGS((mparm_T *parmp));
108static void command_line_scan __ARGS((mparm_T *parmp));
109static void check_tty __ARGS((mparm_T *parmp));
110static void read_stdin __ARGS((void));
111static void create_windows __ARGS((mparm_T *parmp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100112# ifdef FEAT_WINDOWS
Bram Moolenaard87c36e2015-04-03 14:56:49 +0200113static void edit_buffers __ARGS((mparm_T *parmp, char_u *cwd));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100114# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000115static void exe_pre_commands __ARGS((mparm_T *parmp));
116static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000117static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118static void main_start_gui __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100119# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120static void check_swap_exists_action __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100121# endif
122# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000123static void exec_on_server __ARGS((mparm_T *parmp));
124static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
126static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100127# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000128#endif
129
130
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000131/*
132 * Different types of error messages.
133 */
134static char *(main_errors[]) =
135{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000137#define ME_UNKNOWN_OPTION 0
138 N_("Too many edit arguments"),
139#define ME_TOO_MANY_ARGS 1
140 N_("Argument missing after"),
141#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000143#define ME_GARBAGE 3
144 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
145#define ME_EXTRA_CMD 4
146 N_("Invalid argument for"),
147#define ME_INVALID_ARG 5
148};
149
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100150#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100151#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram 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 Moolenaard87c36e2015-04-03 14:56:49 +0200171 char_u *cwd = NULL; /* current workding dir on startup */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000172#ifdef STARTUPTIME
173 int i;
174#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176 /*
177 * Do any system-specific initialisations. These can NOT use IObuff or
178 * NameBuff. Thus emsg2() cannot be called!
179 */
180 mch_early_init();
181
Bram Moolenaar14993322014-09-09 12:25:33 +0200182#if defined(WIN32) && defined(FEAT_MBYTE)
183 /*
184 * MingW expands command line arguments, which confuses our code to
185 * convert when 'encoding' changes. Get the unexpanded arguments.
186 */
187 argc = get_cmd_argsW(&argv);
188#endif
189
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000190 /* Many variables are in "params" so that we can pass them to invoked
191 * functions without a lot of arguments. "argc" and "argv" are also
192 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000193 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000194 params.argc = argc;
195 params.argv = argv;
196 params.want_full_screen = TRUE;
197#ifdef FEAT_EVAL
198 params.use_debug_break_level = -1;
199#endif
200#ifdef FEAT_WINDOWS
201 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000202#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000203
Bram Moolenaar99685e62013-05-11 13:56:18 +0200204#ifdef FEAT_RUBY
205 {
206 int ruby_stack_start;
207 vim_ruby_init((void *)&ruby_stack_start);
208 }
209#endif
210
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000211#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000212 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213#endif
214
215#ifdef MEM_PROFILE
216 atexit(vim_mem_profile_dump);
217#endif
218
219#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000220 for (i = 1; i < argc; ++i)
221 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000222 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000223 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000224 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000225 TIME_MSG("--- VIM STARTING ---");
226 break;
227 }
228 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000229#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000230 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231
232#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000233 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234#endif
235
236#ifdef FEAT_MBYTE
237 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
238#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000239#ifdef FEAT_EVAL
240 eval_init(); /* init global variables */
241#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000242
243#ifdef __QNXNTO__
244 qnx_init(); /* PhAttach() for clipboard, (and gui) */
245#endif
246
247#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000248 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000249 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000250 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000251 TIME_MSG("GUI prepared");
252#endif
253
254 /* Init the table of Normal mode commands. */
255 init_normal_cmds();
256
257#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000258 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259#endif
260
261 /*
262 * Allocate space for the generic buffers (needed for set_init_1() and
263 * EMSG2()).
264 */
265 if ((IObuff = alloc(IOSIZE)) == NULL
266 || (NameBuff = alloc(MAXPATHL)) == NULL)
267 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000268 TIME_MSG("Allocated generic buffers");
269
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000270#ifdef NBDEBUG
271 /* Wait a moment for debugging NetBeans. Must be after allocating
272 * NameBuff. */
273 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
274 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000275 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000276#endif
277
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000278#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
279 /*
280 * Setup to use the current locale (for ctype() and many other things).
281 * NOTE: Translated messages with encodings other than latin1 will not
282 * work until set_init_1() has been called!
283 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000284 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000285 TIME_MSG("locale set");
286#endif
287
288#ifdef FEAT_GUI
289 gui.dofork = TRUE; /* default is to use fork() */
290#endif
291
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000292 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000293 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000294 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000295 * --server...
296 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000297 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000299 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300
301#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000302 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000303#endif
304#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000305 /* Prepare for possibly starting GUI sometime */
306 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000307 TIME_MSG("GUI prepared");
308#endif
309
310#ifdef FEAT_CLIPBOARD
311 clip_init(FALSE); /* Initialise clipboard stuff */
312 TIME_MSG("clipboard setup");
313#endif
314
315 /*
316 * Check if we have an interactive window.
317 * On the Amiga: If there is no window, we open one with a newcli command
318 * (needed for :! to * work). mch_check_win() will also handle the -d or
319 * -dev argument.
320 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000321 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000322 TIME_MSG("window checked");
323
324 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000325 * Allocate the first window and buffer.
326 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000327 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000328 if (win_alloc_first() == FAIL)
329 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000330
331 init_yank(); /* init yank buffers */
332
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000333 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200334 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000335
336 /*
337 * Set the default values for the options.
338 * NOTE: Non-latin1 translated messages are working only after this,
339 * because this is where "has_mbyte" will be set, which is used by
340 * msg_outtrans_len_attr().
341 * First find out the home directory, needed to expand "~" in options.
342 */
343 init_homedir(); /* find real value of $HOME */
344 set_init_1();
345 TIME_MSG("inits 1");
346
347#ifdef FEAT_EVAL
348 set_lang_var(); /* set v:lang and v:ctype */
349#endif
350
351#ifdef FEAT_CLIENTSERVER
352 /*
353 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000354 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000355 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000356 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000357#endif
358
359 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000360 * Figure out the way to work from the command name argv[0].
361 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000362 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000363 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364
365 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000366 * Process the command line arguments. File names are put in the global
367 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000368 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000369 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000370 TIME_MSG("parsing arguments");
371
372 /*
373 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000374 * there is no terminal version, and on Windows we can't fork one off with
375 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000376 */
377#ifdef ALWAYS_USE_GUI
378 gui.starting = TRUE;
379#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000380# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000381 /*
382 * Check if the GUI can be started. Reset gui.starting if not.
383 * Don't know about other systems, stay on the safe side and don't check.
384 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000385 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000386 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000387 if (gui_init_check() == FAIL)
388 {
389 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000390
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000391 /* When running "evim" or "gvim -y" we need the menus, exit if we
392 * don't have them. */
393 if (params.evim_mode)
394 mch_exit(1);
395 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000396 }
397# endif
398#endif
399
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400 if (GARGCOUNT > 0)
401 {
402#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
403 /*
404 * Expand wildcards in file names.
405 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000406 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000407 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +0200408 cwd = alloc(MAXPATHL);
409 if (cwd != NULL)
410 mch_dirname(cwd, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000411 /* Temporarily add '(' and ')' to 'isfname'. These are valid
412 * filename characters but are excluded from 'isfname' to make
413 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
414 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000415 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000416 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaard87c36e2015-04-03 14:56:49 +0200417 if (cwd != NULL)
418 mch_chdir((char *)cwd);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000419 }
420#endif
421 fname = alist_name(&GARGLIST[0]);
422 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000423
424#if defined(WIN32) && defined(FEAT_MBYTE)
425 {
426 extern void set_alist_count(void);
427
428 /* Remember the number of entries in the argument list. If it changes
429 * we don't react on setting 'encoding'. */
430 set_alist_count();
431 }
432#endif
433
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000434#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000435 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000436 {
437 /*
438 * If there is one filename, fully qualified, we have very probably
439 * been invoked from explorer, so change to the file's directory.
440 * Hint: to avoid this when typing a command use a forward slash.
441 * If the cd fails, it doesn't matter.
442 */
443 (void)vim_chdirfile(fname);
Bram Moolenaard87c36e2015-04-03 14:56:49 +0200444 if (cwd != NULL)
445 mch_dirnamem(cwd, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000446 }
447#endif
448 TIME_MSG("expanding arguments");
449
450#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000451 if (params.diff_mode && params.window_count == -1)
452 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000453#endif
454
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000455 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000456 ++RedrawingDisabled;
457
458 /*
459 * When listing swap file names, don't do cursor positioning et. al.
460 */
461 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000462 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000463
464 /*
465 * When certain to start the GUI, don't check capabilities of terminal.
466 * For GTK we can't be sure, but when started from the desktop it doesn't
467 * make sense to try using a terminal.
468 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000469#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000470 if (gui.starting
471# ifdef FEAT_GUI_GTK
472 && !isatty(2)
473# endif
474 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000475 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000476#endif
477
478#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
479 /* When the GUI is started from Finder, need to display messages in a
480 * message box. isatty(2) returns TRUE anyway, thus we need to check the
481 * name to know we're not started from a terminal. */
482 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000483 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000484 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000485
486 /* Avoid always using "/" as the current directory. Note that when
487 * started from Finder the arglist will be filled later in
488 * HandleODocAE() and "fname" will be NULL. */
489 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
490 && STRCMP(NameBuff, "/") == 0)
491 {
492 if (fname != NULL)
493 (void)vim_chdirfile(fname);
494 else
495 {
496 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
497 vim_chdir(NameBuff);
498 }
Bram Moolenaard87c36e2015-04-03 14:56:49 +0200499 if (cwd != NULL)
500 mch_dirname(cwd, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000501 }
502 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000503#endif
504
505 /*
506 * mch_init() sets up the terminal (window) for use. This must be
507 * done after resetting full_screen, otherwise it may move the cursor
508 * (MSDOS).
509 * Note that we may use mch_exit() before mch_init()!
510 */
511 mch_init();
512 TIME_MSG("shell init");
513
514#ifdef USE_XSMP
515 /*
516 * For want of anywhere else to do it, try to connect to xsmp here.
517 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
518 * Hijacking -X 'no X connection' to also disable XSMP connection as that
519 * has a similar delay upon failure.
520 * Only try if SESSION_MANAGER is set to something non-null.
521 */
522 if (!x_no_connect)
523 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000524 char *p = getenv("SESSION_MANAGER");
525
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000526 if (p != NULL && *p != NUL)
527 {
528 xsmp_init();
529 TIME_MSG("xsmp init");
530 }
531 }
532#endif
533
534 /*
535 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000536 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000537 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539 /* This message comes before term inits, but after setting "silent_mode"
540 * when the input is not a tty. */
541 if (GARGCOUNT > 1 && !silent_mode)
542 printf(_("%d files to edit\n"), GARGCOUNT);
543
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000544 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000545 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000546 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000547 capabilities (will set full_screen) */
548 screen_start(); /* don't know where cursor is now */
549 TIME_MSG("Termcap init");
550 }
551
552 /*
553 * Set the default values for the options that use Rows and Columns.
554 */
555 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000556 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557#ifdef FEAT_DIFF
558 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
559 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000560 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000561 diff_win_options(firstwin, FALSE);
562#endif
563
564 cmdline_row = Rows - p_ch;
565 msg_row = cmdline_row;
566 screenalloc(FALSE); /* allocate screen buffers */
567 set_init_2();
568 TIME_MSG("inits 2");
569
570 msg_scroll = TRUE;
571 no_wait_return = TRUE;
572
573 init_mappings(); /* set up initial mappings */
574
575 init_highlight(TRUE, FALSE); /* set the default highlight groups */
576 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000577
578#ifdef FEAT_EVAL
579 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000580 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000581#endif
582
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100583#ifdef FEAT_MZSCHEME
584 /*
585 * Newer version of MzScheme (Racket) require earlier (trampolined)
586 * initialisation via scheme_main_setup.
587 * Implement this by initialising it as early as possible
588 * and splitting off remaining Vim main into vim_main2
589 */
590 {
591 /* Pack up preprocessed command line arguments.
592 * It is safe because Scheme does not access argc/argv. */
593 char *args[2];
594 args[0] = (char *)fname;
595 args[1] = (char *)&params;
596 return mzscheme_main(2, args);
597 }
598}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100599#endif
600#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100601
Bram Moolenaar8866d272012-11-28 15:55:42 +0100602/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
603 * NO_VIM_MAIN is defined. */
604#ifdef FEAT_MZSCHEME
605 int
606vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100607{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100608# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100609 char_u *fname = (char_u *)argv[0];
610 mparm_T params;
611
612 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100613# else
614 return 0;
615}
616# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100617#endif
618
Bram Moolenaar8866d272012-11-28 15:55:42 +0100619#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000620 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000621 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000622
Bram Moolenaar58d98232005-07-23 22:25:46 +0000623 /* Source startup scripts. */
624 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000625
626#ifdef FEAT_EVAL
627 /*
628 * Read all the plugin files.
629 * Only when compiled with +eval, since most plugins need it.
630 */
631 if (p_lpl)
632 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000633# ifdef VMS /* Somehow VMS doesn't handle the "**". */
634 source_runtime((char_u *)"plugin/*.vim", TRUE);
635# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000636 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000637# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000638 TIME_MSG("loading plugins");
639 }
640#endif
641
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000642#ifdef FEAT_DIFF
643 /* Decide about window layout for diff mode after reading vimrc. */
644 if (params.diff_mode && params.window_layout == 0)
645 {
646 if (diffopt_horizontal())
647 params.window_layout = WIN_HOR; /* use horizontal split */
648 else
649 params.window_layout = WIN_VER; /* use vertical split */
650 }
651#endif
652
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000653 /*
654 * Recovery mode without a file name: List swap files.
655 * This uses the 'dir' option, therefore it must be after the
656 * initializations.
657 */
658 if (recoverymode && fname == NULL)
659 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200660 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000661 mch_exit(0);
662 }
663
664 /*
665 * Set a few option defaults after reading .vimrc files:
666 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
667 */
668 set_init_3();
669 TIME_MSG("inits 3");
670
671 /*
672 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
673 * Note that this overrides anything from a vimrc file.
674 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000675 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000676 p_uc = 0;
677
678#ifdef FEAT_FKMAP
679 if (curwin->w_p_rl && p_altkeymap)
680 {
681 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
682# ifdef FEAT_ARABIC
683 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
684# endif
685 p_fkmap = TRUE; /* Set the Farsi keymap mode */
686 }
687#endif
688
689#ifdef FEAT_GUI
690 if (gui.starting)
691 {
692#if defined(UNIX) || defined(VMS)
693 /* When something caused a message from a vimrc script, need to output
694 * an extra newline before the shell prompt. */
695 if (did_emsg || msg_didout)
696 putchar('\n');
697#endif
698
699 gui_start(); /* will set full_screen to TRUE */
700 TIME_MSG("starting GUI");
701
702 /* When running "evim" or "gvim -y" we need the menus, exit if we
703 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000704 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000705 mch_exit(1);
706 }
707#endif
708
709#ifdef SPAWNO /* special MSDOS swapping library */
710 init_SPAWNO("", SWAP_ANY);
711#endif
712
713#ifdef FEAT_VIMINFO
714 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000715 * Read in registers, history etc, but not marks, from the viminfo file.
716 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000717 */
718 if (*p_viminfo != NUL)
719 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000720 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 TIME_MSG("reading viminfo");
722 }
723#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100724#ifdef FEAT_EVAL
725 /* It's better to make v:oldfiles an empty list than NULL. */
726 if (get_vim_var_list(VV_OLDFILES) == NULL)
727 set_vim_var_list(VV_OLDFILES, list_alloc());
728#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000729
730#ifdef FEAT_QUICKFIX
731 /*
732 * "-q errorfile": Load the error file now.
733 * If the error file can't be read, exit before doing anything else.
734 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000735 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000736 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000737 if (params.use_ef != NULL)
738 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000739 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200740 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
741 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000742 {
743 out_char('\n');
744 mch_exit(3);
745 }
746 TIME_MSG("reading errorfile");
747 }
748#endif
749
750 /*
751 * Start putting things on the screen.
752 * Scroll screen down before drawing over it
753 * Clear screen now, so file message will not be cleared.
754 */
755 starting = NO_BUFFERS;
756 no_wait_return = FALSE;
757 if (!exmode_active)
758 msg_scroll = FALSE;
759
760#ifdef FEAT_GUI
761 /*
762 * This seems to be required to make callbacks to be called now, instead
763 * of after things have been put on the screen, which then may be deleted
764 * when getting a resize callback.
765 * For the Mac this handles putting files dropped on the Vim icon to
766 * global_alist.
767 */
768 if (gui.in_use)
769 {
770# ifdef FEAT_SUN_WORKSHOP
771 if (!usingSunWorkShop)
772# endif
773 gui_wait_for_chars(50L);
774 TIME_MSG("GUI delay");
775 }
776#endif
777
778#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
779 qnx_clip_init();
780#endif
781
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200782#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
783 clip_init(TRUE);
784#endif
785
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000786#ifdef FEAT_XCLIPBOARD
787 /* Start using the X clipboard, unless the GUI was started. */
788# ifdef FEAT_GUI
789 if (!gui.in_use)
790# endif
791 {
792 setup_term_clip();
793 TIME_MSG("setup clipboard");
794 }
795#endif
796
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000797#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000798 /* Prepare for being a Vim server. */
799 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800#endif
801
802 /*
803 * If "-" argument given: Read file from stdin.
804 * Do this before starting Raw mode, because it may change things that the
805 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
806 * are the same terminal: "cat | vim -".
807 * Using autocommands here may cause trouble...
808 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000809 if (params.edit_type == EDIT_STDIN && !recoverymode)
810 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000811
812#if defined(UNIX) || defined(VMS)
813 /* When switching screens and something caused a message from a vimrc
814 * script, need to output an extra newline on exit. */
815 if ((did_emsg || msg_didout) && *T_TI != NUL)
816 newline_on_exit = TRUE;
817#endif
818
819 /*
820 * When done something that is not allowed or error message call
821 * wait_return. This must be done before starttermcap(), because it may
822 * switch to another screen. It must be done after settmode(TMODE_RAW),
823 * because we want to react on a single key stroke.
824 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000825 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000826 */
827 settmode(TMODE_RAW);
828 TIME_MSG("setting raw mode");
829
830 if (need_wait_return || msg_didany)
831 {
832 wait_return(TRUE);
833 TIME_MSG("waiting for return");
834 }
835
836 starttermcap(); /* start termcap if not done by wait_return() */
837 TIME_MSG("start termcap");
Bram Moolenaar9584b312013-03-13 19:29:28 +0100838#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200839 may_req_ambiguous_char_width();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100840#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841
842#ifdef FEAT_MOUSE
843 setmouse(); /* may start using the mouse */
844#endif
845 if (scroll_region)
846 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000847 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000848
849 /*
850 * Don't clear the screen when starting in Ex mode, unless using the GUI.
851 */
852 if (exmode_active
853#ifdef FEAT_GUI
854 && !gui.in_use
855#endif
856 )
857 must_redraw = CLEAR;
858 else
859 {
860 screenclear(); /* clear screen */
861 TIME_MSG("clearing screen");
862 }
863
864#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000865 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000866 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100867 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200868 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000869 TIME_MSG("getting crypt key");
870 }
871#endif
872
873 no_wait_return = TRUE;
874
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000876 * Create the requested number of windows and edit buffers in them.
877 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000879 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880 TIME_MSG("opening buffers");
881
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000882#ifdef FEAT_EVAL
883 /* clear v:swapcommand */
884 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
885#endif
886
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000887 /* Ex starts at last line of the file */
888 if (exmode_active)
889 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
890
891#ifdef FEAT_AUTOCMD
892 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
893 TIME_MSG("BufEnter autocommands");
894#endif
895 setpcmark();
896
897#ifdef FEAT_QUICKFIX
898 /*
899 * When started with "-q errorfile" jump to first error now.
900 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000901 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000902 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000903 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000904 TIME_MSG("jump to first error");
905 }
906#endif
907
908#ifdef FEAT_WINDOWS
909 /*
910 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000911 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000912 */
Bram Moolenaard87c36e2015-04-03 14:56:49 +0200913 edit_buffers(&params, cwd);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000914#endif
Bram Moolenaard87c36e2015-04-03 14:56:49 +0200915 vim_free(cwd);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000916
917#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000918 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000919 {
920 win_T *wp;
921
922 /* set options in each window for "vimdiff". */
923 for (wp = firstwin; wp != NULL; wp = wp->w_next)
924 diff_win_options(wp, TRUE);
925 }
926#endif
927
928 /*
929 * Shorten any of the filenames, but only when absolute.
930 */
931 shorten_fnames(FALSE);
932
933 /*
934 * Need to jump to the tag before executing the '-c command'.
935 * Makes "vim -c '/return' -t main" work.
936 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000937 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000938 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000939#if defined(HAS_SWAP_EXISTS_ACTION)
940 swap_exists_did_quit = FALSE;
941#endif
942
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000943 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000944 do_cmdline_cmd(IObuff);
945 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000946
947#if defined(HAS_SWAP_EXISTS_ACTION)
948 /* If the user doesn't want to edit the file then we quit here. */
949 if (swap_exists_did_quit)
950 getout(1);
951#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000952 }
953
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000954 /* Execute any "+", "-c" and "-S" arguments. */
955 if (params.n_commands > 0)
956 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000957
958 RedrawingDisabled = 0;
959 redraw_all_later(NOT_VALID);
960 no_wait_return = FALSE;
961 starting = 0;
962
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000963#ifdef FEAT_TERMRESPONSE
964 /* Requesting the termresponse is postponed until here, so that a "-c q"
965 * argument doesn't make it appear in the shell Vim was started from. */
966 may_req_termresponse();
967#endif
968
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000969 /* start in insert mode */
970 if (p_im)
971 need_start_insertmode = TRUE;
972
973#ifdef FEAT_AUTOCMD
974 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
975 TIME_MSG("VimEnter autocommands");
976#endif
977
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200978#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
979 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
980 * done after the clipboard is available and all initial commands that may
981 * modify the 'clipboard' setting have run; i.e. just before entering the
982 * main loop. */
983 {
984 int default_regname = 0;
985 adjust_clip_reg(&default_regname);
986 set_reg_var(default_regname);
987 }
988#endif
989
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000990#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
991 /* When a startup script or session file setup for diff'ing and
992 * scrollbind, sync the scrollbind now. */
993 if (curwin->w_p_diff && curwin->w_p_scb)
994 {
995 update_topline();
996 check_scrollbind((linenr_T)0, 0L);
997 TIME_MSG("diff scrollbinding");
998 }
999#endif
1000
1001#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1002 mch_set_winsize_now(); /* Allow winsize changes from now on */
1003#endif
1004
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001005#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1006 /* When tab pages were created, may need to update the tab pages line and
1007 * scrollbars. This is skipped while creating them. */
1008 if (first_tabpage->tp_next != NULL)
1009 {
1010 out_flush();
1011 gui_init_which_components(NULL);
1012 gui_update_scrollbars(TRUE);
1013 }
1014 need_mouse_correct = TRUE;
1015#endif
1016
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001017 /* If ":startinsert" command used, stuff a dummy command to be able to
1018 * call normal_cmd(), which will then start Insert mode. */
1019 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001020 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001021
1022#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001023 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001024 {
1025# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001026# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001027 && !defined(FEAT_GUI_W32)
1028 if (gui.in_use)
1029 {
1030 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1031 mch_exit(2);
1032 }
1033# endif
1034# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001035 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001036 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001037 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038#endif
1039
1040 TIME_MSG("before starting main loop");
1041
1042 /*
1043 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001044 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046
1047 return 0;
1048}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001049#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001050#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001051
1052/*
1053 * Main loop: Execute Normal mode commands until exiting Vim.
1054 * Also used to handle commands in the command-line window, until the window
1055 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 * Also used to handle ":visual" command after ":global": execute Normal mode
1057 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001058 */
1059 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060main_loop(cmdwin, noexmode)
1061 int cmdwin; /* TRUE when working in the command-line window */
1062 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001063{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001064 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001065 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001066#ifdef FEAT_CONCEAL
1067 linenr_T conceal_old_cursor_line = 0;
1068 linenr_T conceal_new_cursor_line = 0;
1069 int conceal_update_lines = FALSE;
1070#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001071
1072#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1073 /* Setup to catch a terminating error from the X server. Just ignore
1074 * it, restore the state and continue. This might not always work
1075 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001076 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001078 {
1079 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001080 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001081 got_int = TRUE;
1082 need_wait_return = FALSE;
1083 global_busy = FALSE;
1084 exmode_active = 0;
1085 skip_redraw = FALSE;
1086 RedrawingDisabled = 0;
1087 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001088 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001089# ifdef FEAT_EVAL
1090 emsg_skip = 0;
1091# endif
1092 emsg_off = 0;
1093# ifdef FEAT_MOUSE
1094 setmouse();
1095# endif
1096 settmode(TMODE_RAW);
1097 starttermcap();
1098 scroll_start();
1099 redraw_later_clear();
1100 }
1101#endif
1102
1103 clear_oparg(&oa);
1104 while (!cmdwin
1105#ifdef FEAT_CMDWIN
1106 || cmdwin_result == 0
1107#endif
1108 )
1109 {
1110 if (stuff_empty())
1111 {
1112 did_check_timestamps = FALSE;
1113 if (need_check_timestamps)
1114 check_timestamps(FALSE);
1115 if (need_wait_return) /* if wait_return still needed ... */
1116 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001117 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001118 {
1119 need_start_insertmode = FALSE;
1120 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1121 /* skip the fileinfo message now, because it would be shown
1122 * after insert mode finishes! */
1123 need_fileinfo = FALSE;
1124 }
1125 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001126
1127 /* Reset "got_int" now that we got back to the main loop. Except when
1128 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1129 * the ":g" command.
1130 * For ":g/pat/vi" we reset "got_int" when used once. When used
1131 * a second time we go back to Ex mode and abort the ":g" command. */
1132 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001133 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001134 if (noexmode && global_busy && !exmode_active && previous_got_int)
1135 {
1136 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1137 * used and keep "got_int" set, so that it aborts ":g". */
1138 exmode_active = EXMODE_NORMAL;
1139 State = NORMAL;
1140 }
1141 else if (!global_busy || !exmode_active)
1142 {
1143 if (!quit_more)
1144 (void)vgetc(); /* flush all buffers */
1145 got_int = FALSE;
1146 }
1147 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001148 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001149 else
1150 previous_got_int = FALSE;
1151
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001152 if (!exmode_active)
1153 msg_scroll = FALSE;
1154 quit_more = FALSE;
1155
1156 /*
1157 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1158 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1159 * update cursor and redraw.
1160 */
1161 if (skip_redraw || exmode_active)
1162 skip_redraw = FALSE;
1163 else if (do_redraw || stuff_empty())
1164 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001165#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001166 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167 if (!finish_op && (
1168# ifdef FEAT_AUTOCMD
1169 has_cursormoved()
1170# endif
1171# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1172 ||
1173# endif
1174# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001175 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001176# endif
1177 )
1178 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001179 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001180# ifdef FEAT_AUTOCMD
1181 if (has_cursormoved())
1182 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1183 FALSE, curbuf);
1184# endif
1185# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001186 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001187 {
1188 conceal_old_cursor_line = last_cursormoved.lnum;
1189 conceal_new_cursor_line = curwin->w_cursor.lnum;
1190 conceal_update_lines = TRUE;
1191 }
1192# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001193 last_cursormoved = curwin->w_cursor;
1194 }
1195#endif
1196
Bram Moolenaar186628f2013-03-19 13:33:23 +01001197#ifdef FEAT_AUTOCMD
1198 /* Trigger TextChanged if b_changedtick differs. */
1199 if (!finish_op && has_textchanged()
1200 && last_changedtick != curbuf->b_changedtick)
1201 {
1202 if (last_changedtick_buf == curbuf)
1203 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1204 FALSE, curbuf);
1205 last_changedtick_buf = curbuf;
1206 last_changedtick = curbuf->b_changedtick;
1207 }
1208#endif
1209
Bram Moolenaar33aec762006-01-22 23:30:12 +00001210#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1211 /* Scroll-binding for diff mode may have been postponed until
1212 * here. Avoids doing it for every change. */
1213 if (diff_need_scrollbind)
1214 {
1215 check_scrollbind((linenr_T)0, 0L);
1216 diff_need_scrollbind = FALSE;
1217 }
1218#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001219#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001220 /* Include a closed fold completely in the Visual area. */
1221 foldAdjustVisual();
1222#endif
1223#ifdef FEAT_FOLDING
1224 /*
1225 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1226 * contain the cursor.
1227 * When 'foldopen' is "all", open the fold(s) under the cursor.
1228 * This may mark the window for redrawing.
1229 */
1230 if (hasAnyFolding(curwin) && !char_avail())
1231 {
1232 foldCheckClose();
1233 if (fdo_flags & FDO_ALL)
1234 foldOpenCursor();
1235 }
1236#endif
1237
1238 /*
1239 * Before redrawing, make sure w_topline is correct, and w_leftcol
1240 * if lines don't wrap, and w_skipcol if lines wrap.
1241 */
1242 update_topline();
1243 validate_cursor();
1244
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001245 if (VIsual_active)
1246 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001247 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001248 update_screen(0);
1249 else if (redraw_cmdline || clear_cmdline)
1250 showmode();
1251#ifdef FEAT_WINDOWS
1252 redraw_statuslines();
1253#endif
1254#ifdef FEAT_TITLE
1255 if (need_maketitle)
1256 maketitle();
1257#endif
1258 /* display message after redraw */
1259 if (keep_msg != NULL)
1260 {
1261 char_u *p;
1262
1263 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001264 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1265 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001266 p = keep_msg;
1267 msg_attr(p, keep_msg_attr);
1268 vim_free(p);
1269 }
1270 if (need_fileinfo) /* show file info after redraw */
1271 {
1272 fileinfo(FALSE, TRUE, FALSE);
1273 need_fileinfo = FALSE;
1274 }
1275
1276 emsg_on_display = FALSE; /* can delete error message now */
1277 did_emsg = FALSE;
1278 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001279 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 showruler(FALSE);
1281
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001282# if defined(FEAT_CONCEAL)
1283 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001284 && (conceal_old_cursor_line != conceal_new_cursor_line
1285 || conceal_cursor_line(curwin)
1286 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001287 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001288 if (conceal_old_cursor_line != conceal_new_cursor_line
1289 && conceal_old_cursor_line
1290 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001291 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001292 update_single_line(curwin, conceal_new_cursor_line);
1293 curwin->w_valid &= ~VALID_CROW;
1294 }
1295# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001296 setcursor();
1297 cursor_on();
1298
1299 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001300
1301#ifdef STARTUPTIME
1302 /* Now that we have drawn the first screen all the startup stuff
1303 * has been done, close any file for startup messages. */
1304 if (time_fd != NULL)
1305 {
1306 TIME_MSG("first screen update");
1307 TIME_MSG("--- VIM STARTED ---");
1308 fclose(time_fd);
1309 time_fd = NULL;
1310 }
1311#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001312 }
1313#ifdef FEAT_GUI
1314 if (need_mouse_correct)
1315 gui_mouse_correct();
1316#endif
1317
1318 /*
1319 * Update w_curswant if w_set_curswant has been set.
1320 * Postponed until here to avoid computing w_virtcol too often.
1321 */
1322 update_curswant();
1323
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001324#ifdef FEAT_EVAL
1325 /*
1326 * May perform garbage collection when waiting for a character, but
1327 * only at the very toplevel. Otherwise we may be using a List or
1328 * Dict internally somewhere.
1329 * "may_garbage_collect" is reset in vgetc() which is invoked through
1330 * do_exmode() and normal_cmd().
1331 */
1332 may_garbage_collect = (!cmdwin && !noexmode);
1333#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001334 /*
1335 * If we're invoked as ex, do a round of ex commands.
1336 * Otherwise, get and execute a normal mode command.
1337 */
1338 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001339 {
1340 if (noexmode) /* End of ":global/path/visual" commands */
1341 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001343 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001344 else
1345 normal_cmd(&oa, TRUE);
1346 }
1347}
1348
1349
1350#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1351/*
1352 * Exit, but leave behind swap files for modified buffers.
1353 */
1354 void
1355getout_preserve_modified(exitval)
1356 int exitval;
1357{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001358# if defined(SIGHUP) && defined(SIG_IGN)
1359 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1360 * makes Vim exit and then handling SIGHUP causes various reentrance
1361 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001362 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001363# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001364
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365 ml_close_notmod(); /* close all not-modified buffers */
1366 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1367 ml_close_all(FALSE); /* close all memfiles, without deleting */
1368 getout(exitval); /* exit Vim properly */
1369}
1370#endif
1371
1372
1373/* Exit properly */
1374 void
1375getout(exitval)
1376 int exitval;
1377{
1378#ifdef FEAT_AUTOCMD
1379 buf_T *buf;
1380 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001381 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001382#endif
1383
1384 exiting = TRUE;
1385
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001386 /* When running in Ex mode an error causes us to exit with a non-zero exit
1387 * code. POSIX requires this, although it's not 100% clear from the
1388 * standard. */
1389 if (exmode_active)
1390 exitval += ex_exitval;
1391
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001392 /* Position the cursor on the last screen line, below all the text */
1393#ifdef FEAT_GUI
1394 if (!gui.in_use)
1395#endif
1396 windgoto((int)Rows - 1, 0);
1397
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001398#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1399 /* Optionally print hashtable efficiency. */
1400 hash_debug_results();
1401#endif
1402
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001403#ifdef FEAT_GUI
1404 msg_didany = FALSE;
1405#endif
1406
1407#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001408 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001410 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1411# if defined FEAT_WINDOWS
1412 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001414 next_tp = tp->tp_next;
1415 for (wp = (tp == curtab)
1416 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001417 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001418 if (wp->w_buffer == NULL)
1419 /* Autocmd must have close the buffer already, skip. */
1420 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001421 buf = wp->w_buffer;
1422 if (buf->b_changedtick != -1)
1423 {
1424 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1425 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001426 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001427 /* start all over, autocommands may mess up the lists */
1428 next_tp = first_tabpage;
1429 break;
1430 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001431 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001432 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001433# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001434 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1435 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001436# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001437
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001438 /* Trigger BufUnload for buffers that are loaded */
1439 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1440 if (buf->b_ml.ml_mfp != NULL)
1441 {
1442 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001443 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001444 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1445 break;
1446 }
1447 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1448 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001449#endif
1450
1451#ifdef FEAT_VIMINFO
1452 if (*p_viminfo != NUL)
1453 /* Write out the registers, history, marks etc, to the viminfo file */
1454 write_viminfo(NULL, FALSE);
1455#endif
1456
1457#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001458 if (get_vim_var_nr(VV_DYING) <= 1)
1459 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001460#endif
1461
Bram Moolenaar05159a02005-02-26 23:04:13 +00001462#ifdef FEAT_PROFILE
1463 profile_dump();
1464#endif
1465
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001466 if (did_emsg
1467#ifdef FEAT_GUI
1468 || (gui.in_use && msg_didany && p_verbose > 0)
1469#endif
1470 )
1471 {
1472 /* give the user a chance to read the (error) message */
1473 no_wait_return = FALSE;
1474 wait_return(FALSE);
1475 }
1476
1477#ifdef FEAT_AUTOCMD
1478 /* Position the cursor again, the autocommands may have moved it */
1479# ifdef FEAT_GUI
1480 if (!gui.in_use)
1481# endif
1482 windgoto((int)Rows - 1, 0);
1483#endif
1484
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001485#ifdef FEAT_LUA
1486 lua_end();
1487#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001488#ifdef FEAT_MZSCHEME
1489 mzscheme_end();
1490#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001491#ifdef FEAT_TCL
1492 tcl_end();
1493#endif
1494#ifdef FEAT_RUBY
1495 ruby_end();
1496#endif
1497#ifdef FEAT_PYTHON
1498 python_end();
1499#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001500#ifdef FEAT_PYTHON3
1501 python3_end();
1502#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001503#ifdef FEAT_PERL
1504 perl_end();
1505#endif
1506#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1507 iconv_end();
1508#endif
1509#ifdef FEAT_NETBEANS_INTG
1510 netbeans_end();
1511#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001512#ifdef FEAT_CSCOPE
1513 cs_end();
1514#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001515#ifdef FEAT_EVAL
1516 if (garbage_collect_at_exit)
1517 garbage_collect();
1518#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001519#if defined(WIN32) && defined(FEAT_MBYTE)
1520 free_cmd_argsW();
1521#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001522
1523 mch_exit(exitval);
1524}
1525
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001526#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001527/*
1528 * Get a (optional) count for a Vim argument.
1529 */
1530 static int
1531get_number_arg(p, idx, def)
1532 char_u *p; /* pointer to argument */
1533 int *idx; /* index in argument, is incremented */
1534 int def; /* default value */
1535{
1536 if (vim_isdigit(p[*idx]))
1537 {
1538 def = atoi((char *)&(p[*idx]));
1539 while (vim_isdigit(p[*idx]))
1540 *idx = *idx + 1;
1541 }
1542 return def;
1543}
1544
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001545#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1546/*
1547 * Setup to use the current locale (for ctype() and many other things).
1548 */
1549 static void
1550init_locale()
1551{
1552 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001553
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001554# ifdef FEAT_GUI_GTK
1555 /* Tell Gtk not to change our locale settings. */
1556 gtk_disable_setlocale();
1557# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001558# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1559 /* Make sure strtod() uses a decimal point, not a comma. */
1560 setlocale(LC_NUMERIC, "C");
1561# endif
1562
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001563# ifdef WIN32
1564 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1565 * text while it's expecting text in the current locale. This call avoids
1566 * that. */
1567 setlocale(LC_CTYPE, "C");
1568# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001569
1570# ifdef FEAT_GETTEXT
1571 {
1572 int mustfree = FALSE;
1573 char_u *p;
1574
1575# ifdef DYNAMIC_GETTEXT
1576 /* Initialize the gettext library */
1577 dyn_libintl_init(NULL);
1578# endif
1579 /* expand_env() doesn't work yet, because chartab[] is not initialized
1580 * yet, call vim_getenv() directly */
1581 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1582 if (p != NULL && *p != NUL)
1583 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001584 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001585 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1586 }
1587 if (mustfree)
1588 vim_free(p);
1589 textdomain(VIMPACKAGE);
1590 }
1591# endif
1592}
1593#endif
1594
1595/*
1596 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1597 * If the executable name starts with "r" we disable shell commands.
1598 * If the next character is "e" we run in Easy mode.
1599 * If the next character is "g" we run the GUI version.
1600 * If the next characters are "view" we start in readonly mode.
1601 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1602 * If the next characters are "ex" we start in Ex mode. If it's followed
1603 * by "im" use improved Ex mode.
1604 */
1605 static void
1606parse_command_name(parmp)
1607 mparm_T *parmp;
1608{
1609 char_u *initstr;
1610
1611 initstr = gettail((char_u *)parmp->argv[0]);
1612
1613#ifdef MACOS_X_UNIX
1614 /* An issue has been seen when launching Vim in such a way that
1615 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1616 * executable or a symbolic link of it. Until this issue is resolved
1617 * we prohibit the GUI from being used.
1618 */
1619 if (STRCMP(initstr, parmp->argv[0]) == 0)
1620 disallow_gui = TRUE;
1621
1622 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001623 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001624#endif
1625
1626#ifdef FEAT_EVAL
1627 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001628 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001629#endif
1630
1631 if (TOLOWER_ASC(initstr[0]) == 'r')
1632 {
1633 restricted = TRUE;
1634 ++initstr;
1635 }
1636
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001637 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001638 if (TOLOWER_ASC(initstr[0]) == 'e'
1639 && (TOLOWER_ASC(initstr[1]) == 'v'
1640 || TOLOWER_ASC(initstr[1]) == 'g'))
1641 {
1642#ifdef FEAT_GUI
1643 gui.starting = TRUE;
1644#endif
1645 parmp->evim_mode = TRUE;
1646 ++initstr;
1647 }
1648
Bram Moolenaar806875d2008-09-18 18:57:10 +00001649 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1650 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001651 {
1652 main_start_gui();
1653#ifdef FEAT_GUI
1654 ++initstr;
1655#endif
1656 }
1657
1658 if (STRNICMP(initstr, "view", 4) == 0)
1659 {
1660 readonlymode = TRUE;
1661 curbuf->b_p_ro = TRUE;
1662 p_uc = 10000; /* don't update very often */
1663 initstr += 4;
1664 }
1665 else if (STRNICMP(initstr, "vim", 3) == 0)
1666 initstr += 3;
1667
1668 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1669 if (STRICMP(initstr, "diff") == 0)
1670 {
1671#ifdef FEAT_DIFF
1672 parmp->diff_mode = TRUE;
1673#else
1674 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1675 mch_errmsg("\n");
1676 mch_exit(2);
1677#endif
1678 }
1679
1680 if (STRNICMP(initstr, "ex", 2) == 0)
1681 {
1682 if (STRNICMP(initstr + 2, "im", 2) == 0)
1683 exmode_active = EXMODE_VIM;
1684 else
1685 exmode_active = EXMODE_NORMAL;
1686 change_compatible(TRUE); /* set 'compatible' */
1687 }
1688}
1689
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001690/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001691 * Get the name of the display, before gui_prepare() removes it from
1692 * argv[]. Used for the xterm-clipboard display.
1693 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001694 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001695 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001696 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001697early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001698 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001699{
Bram Moolenaar03005972008-11-20 13:12:36 +00001700#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1701 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001702 int argc = parmp->argc;
1703 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001704 int i;
1705
1706 for (i = 1; i < argc; i++)
1707 {
1708 if (STRCMP(argv[i], "--") == 0)
1709 break;
1710# ifdef FEAT_XCLIPBOARD
1711 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001712# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001713 || STRICMP(argv[i], "--display") == 0
1714# endif
1715 )
1716 {
1717 if (i == argc - 1)
1718 mainerr_arg_missing((char_u *)argv[i]);
1719 xterm_display = argv[++i];
1720 }
1721# endif
1722# ifdef FEAT_CLIENTSERVER
1723 else if (STRICMP(argv[i], "--servername") == 0)
1724 {
1725 if (i == argc - 1)
1726 mainerr_arg_missing((char_u *)argv[i]);
1727 parmp->serverName_arg = (char_u *)argv[++i];
1728 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001729 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001730 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001731 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001732 {
1733 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001734# ifdef FEAT_GUI
1735 if (strstr(argv[i], "-wait") != 0)
1736 /* don't fork() when starting the GUI to edit files ourself */
1737 gui.dofork = FALSE;
1738# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001739 }
1740# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001741
1742# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1743# ifdef FEAT_GUI_W32
1744 else if (STRICMP(argv[i], "--windowid") == 0)
1745# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001746 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001747# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001748 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001749 long_u id;
1750 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001751
1752 if (i == argc - 1)
1753 mainerr_arg_missing((char_u *)argv[i]);
1754 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001755 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001756 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001757 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001758 if (count != 1)
1759 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1760 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001761# ifdef FEAT_GUI_W32
1762 win_socket_id = id;
1763# else
1764 gtk_socket_id = id;
1765# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001766 i++;
1767 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001768# endif
1769# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001770 else if (STRICMP(argv[i], "--echo-wid") == 0)
1771 echo_wid_arg = TRUE;
1772# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001773# ifndef FEAT_NETBEANS_INTG
1774 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001775 {
1776 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1777 mch_exit(2);
1778 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001779# endif
1780
Bram Moolenaar58d98232005-07-23 22:25:46 +00001781 }
1782#endif
1783}
1784
1785/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001786 * Scan the command line arguments.
1787 */
1788 static void
1789command_line_scan(parmp)
1790 mparm_T *parmp;
1791{
1792 int argc = parmp->argc;
1793 char **argv = parmp->argv;
1794 int argv_idx; /* index in argv[n][] */
1795 int had_minmin = FALSE; /* found "--" argument */
1796 int want_argument; /* option argument with argument */
1797 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001798 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001799 long n;
1800
1801 --argc;
1802 ++argv;
1803 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1804 while (argc > 0)
1805 {
1806 /*
1807 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1808 */
1809 if (argv[0][0] == '+' && !had_minmin)
1810 {
1811 if (parmp->n_commands >= MAX_ARG_CMDS)
1812 mainerr(ME_EXTRA_CMD, NULL);
1813 argv_idx = -1; /* skip to next argument */
1814 if (argv[0][1] == NUL)
1815 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1816 else
1817 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1818 }
1819
1820 /*
1821 * Optional argument.
1822 */
1823 else if (argv[0][0] == '-' && !had_minmin)
1824 {
1825 want_argument = FALSE;
1826 c = argv[0][argv_idx++];
1827#ifdef VMS
1828 /*
1829 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1830 * and "-/X" as "-X".
1831 */
1832 if (c == '/')
1833 {
1834 c = argv[0][argv_idx++];
1835 c = TOUPPER_ASC(c);
1836 }
1837 else
1838 c = TOLOWER_ASC(c);
1839#endif
1840 switch (c)
1841 {
1842 case NUL: /* "vim -" read from stdin */
1843 /* "ex -" silent mode */
1844 if (exmode_active)
1845 silent_mode = TRUE;
1846 else
1847 {
1848 if (parmp->edit_type != EDIT_NONE)
1849 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1850 parmp->edit_type = EDIT_STDIN;
1851 read_cmd_fd = 2; /* read from stderr instead of stdin */
1852 }
1853 argv_idx = -1; /* skip to next argument */
1854 break;
1855
1856 case '-': /* "--" don't take any more option arguments */
1857 /* "--help" give help message */
1858 /* "--version" give version message */
1859 /* "--literal" take files literally */
1860 /* "--nofork" don't fork */
1861 /* "--noplugin[s]" skip plugins */
1862 /* "--cmd <cmd>" execute cmd before vimrc */
1863 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1864 usage();
1865 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1866 {
1867 Columns = 80; /* need to init Columns */
1868 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1869 list_version();
1870 msg_putchar('\n');
1871 msg_didout = FALSE;
1872 mch_exit(0);
1873 }
1874 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1875 {
1876#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1877 parmp->literal = TRUE;
1878#endif
1879 }
1880 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1881 {
1882#ifdef FEAT_GUI
1883 gui.dofork = FALSE; /* don't fork() when starting GUI */
1884#endif
1885 }
1886 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1887 p_lpl = FALSE;
1888 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1889 {
1890 want_argument = TRUE;
1891 argv_idx += 3;
1892 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001893 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1894 {
1895 want_argument = TRUE;
1896 argv_idx += 11;
1897 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001898#ifdef FEAT_CLIENTSERVER
1899 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1900 ; /* already processed -- no arg */
1901 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1902 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1903 {
1904 /* already processed -- snatch the following arg */
1905 if (argc > 1)
1906 {
1907 --argc;
1908 ++argv;
1909 }
1910 }
1911#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001912#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1913# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001914 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001915# else
1916 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1917# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001918 {
1919 /* already processed -- snatch the following arg */
1920 if (argc > 1)
1921 {
1922 --argc;
1923 ++argv;
1924 }
1925 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001926#endif
1927#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001928 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1929 {
1930 /* already processed, skip */
1931 }
1932#endif
1933 else
1934 {
1935 if (argv[0][argv_idx])
1936 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1937 had_minmin = TRUE;
1938 }
1939 if (!want_argument)
1940 argv_idx = -1; /* skip to next argument */
1941 break;
1942
1943 case 'A': /* "-A" start in Arabic mode */
1944#ifdef FEAT_ARABIC
1945 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1946#else
1947 mch_errmsg(_(e_noarabic));
1948 mch_exit(2);
1949#endif
1950 break;
1951
1952 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001953 /* Needs to be effective before expanding file names, because
1954 * for Win32 this makes us edit a shortcut file itself,
1955 * instead of the file it links to. */
1956 set_options_bin(curbuf->b_p_bin, 1, 0);
1957 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001958 break;
1959
1960 case 'C': /* "-C" Compatible */
1961 change_compatible(TRUE);
1962 break;
1963
1964 case 'e': /* "-e" Ex mode */
1965 exmode_active = EXMODE_NORMAL;
1966 break;
1967
1968 case 'E': /* "-E" Improved Ex mode */
1969 exmode_active = EXMODE_VIM;
1970 break;
1971
1972 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1973 window directly, not with newcli */
1974#ifdef FEAT_GUI
1975 gui.dofork = FALSE; /* don't fork() when starting GUI */
1976#endif
1977 break;
1978
1979 case 'g': /* "-g" start GUI */
1980 main_start_gui();
1981 break;
1982
1983 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1984#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001985 p_fkmap = TRUE;
1986 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001987#else
1988 mch_errmsg(_(e_nofarsi));
1989 mch_exit(2);
1990#endif
1991 break;
1992
1993 case 'h': /* "-h" give help message */
1994#ifdef FEAT_GUI_GNOME
1995 /* Tell usage() to exit for "gvim". */
1996 gui.starting = FALSE;
1997#endif
1998 usage();
1999 break;
2000
2001 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2002#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002003 p_hkmap = TRUE;
2004 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002005#else
2006 mch_errmsg(_(e_nohebrew));
2007 mch_exit(2);
2008#endif
2009 break;
2010
2011 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2012#ifdef FEAT_LISP
2013 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2014 p_sm = TRUE;
2015#endif
2016 break;
2017
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002018 case 'M': /* "-M" no changes or writing of files */
2019 reset_modifiable();
2020 /* FALLTHROUGH */
2021
2022 case 'm': /* "-m" no writing of files */
2023 p_write = FALSE;
2024 break;
2025
2026 case 'y': /* "-y" easy mode */
2027#ifdef FEAT_GUI
2028 gui.starting = TRUE; /* start GUI a bit later */
2029#endif
2030 parmp->evim_mode = TRUE;
2031 break;
2032
2033 case 'N': /* "-N" Nocompatible */
2034 change_compatible(FALSE);
2035 break;
2036
2037 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002038#ifdef FEAT_NETBEANS_INTG
2039 /* checking for "-nb", netbeans parameters */
2040 if (argv[0][argv_idx] == 'b')
2041 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002042 netbeansArg = argv[0];
2043 argv_idx = -1; /* skip to next argument */
2044 }
2045 else
2046#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002047 parmp->no_swap_file = TRUE;
2048 break;
2049
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002050 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002051#ifdef TARGET_API_MAC_OSX
2052 /* For some reason on MacOS X, an argument like:
2053 -psn_0_10223617 is passed in when invoke from Finder
2054 or with the 'open' command */
2055 if (argv[0][argv_idx] == 's')
2056 {
2057 argv_idx = -1; /* bypass full -psn */
2058 main_start_gui();
2059 break;
2060 }
2061#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002062#ifdef FEAT_WINDOWS
2063 /* default is 0: open window for each file */
2064 parmp->window_count = get_number_arg((char_u *)argv[0],
2065 &argv_idx, 0);
2066 parmp->window_layout = WIN_TABS;
2067#endif
2068 break;
2069
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002070 case 'o': /* "-o[N]" open N horizontal split windows */
2071#ifdef FEAT_WINDOWS
2072 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002073 parmp->window_count = get_number_arg((char_u *)argv[0],
2074 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002075 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002076#endif
2077 break;
2078
2079 case 'O': /* "-O[N]" open N vertical split windows */
2080#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2081 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002082 parmp->window_count = get_number_arg((char_u *)argv[0],
2083 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002084 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002085#endif
2086 break;
2087
2088#ifdef FEAT_QUICKFIX
2089 case 'q': /* "-q" QuickFix mode */
2090 if (parmp->edit_type != EDIT_NONE)
2091 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2092 parmp->edit_type = EDIT_QF;
2093 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2094 {
2095 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2096 argv_idx = -1;
2097 }
2098 else if (argc > 1) /* "-q {errorfile}" */
2099 want_argument = TRUE;
2100 break;
2101#endif
2102
2103 case 'R': /* "-R" readonly mode */
2104 readonlymode = TRUE;
2105 curbuf->b_p_ro = TRUE;
2106 p_uc = 10000; /* don't update very often */
2107 break;
2108
2109 case 'r': /* "-r" recovery mode */
2110 case 'L': /* "-L" recovery mode */
2111 recoverymode = 1;
2112 break;
2113
2114 case 's':
2115 if (exmode_active) /* "-s" silent (batch) mode */
2116 silent_mode = TRUE;
2117 else /* "-s {scriptin}" read from script file */
2118 want_argument = TRUE;
2119 break;
2120
2121 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2122 if (parmp->edit_type != EDIT_NONE)
2123 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2124 parmp->edit_type = EDIT_TAG;
2125 if (argv[0][argv_idx]) /* "-t{tag}" */
2126 {
2127 parmp->tagname = (char_u *)argv[0] + argv_idx;
2128 argv_idx = -1;
2129 }
2130 else /* "-t {tag}" */
2131 want_argument = TRUE;
2132 break;
2133
2134#ifdef FEAT_EVAL
2135 case 'D': /* "-D" Debugging */
2136 parmp->use_debug_break_level = 9999;
2137 break;
2138#endif
2139#ifdef FEAT_DIFF
2140 case 'd': /* "-d" 'diff' */
2141# ifdef AMIGA
2142 /* check for "-dev {device}" */
2143 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2144 want_argument = TRUE;
2145 else
2146# endif
2147 parmp->diff_mode = TRUE;
2148 break;
2149#endif
2150 case 'V': /* "-V{N}" Verbose level */
2151 /* default is 10: a little bit verbose */
2152 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2153 if (argv[0][argv_idx] != NUL)
2154 {
2155 set_option_value((char_u *)"verbosefile", 0L,
2156 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002157 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002158 }
2159 break;
2160
2161 case 'v': /* "-v" Vi-mode (as if called "vi") */
2162 exmode_active = 0;
2163#ifdef FEAT_GUI
2164 gui.starting = FALSE; /* don't start GUI */
2165#endif
2166 break;
2167
2168 case 'w': /* "-w{number}" set window height */
2169 /* "-w {scriptout}" write to script */
2170 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2171 {
2172 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2173 set_option_value((char_u *)"window", n, NULL, 0);
2174 break;
2175 }
2176 want_argument = TRUE;
2177 break;
2178
2179#ifdef FEAT_CRYPT
2180 case 'x': /* "-x" encrypted reading/writing of files */
2181 parmp->ask_for_key = TRUE;
2182 break;
2183#endif
2184
2185 case 'X': /* "-X" don't connect to X server */
2186#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2187 x_no_connect = TRUE;
2188#endif
2189 break;
2190
2191 case 'Z': /* "-Z" restricted mode */
2192 restricted = TRUE;
2193 break;
2194
2195 case 'c': /* "-c{command}" or "-c {command}" execute
2196 command */
2197 if (argv[0][argv_idx] != NUL)
2198 {
2199 if (parmp->n_commands >= MAX_ARG_CMDS)
2200 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002201 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2202 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002203 argv_idx = -1;
2204 break;
2205 }
2206 /*FALLTHROUGH*/
2207 case 'S': /* "-S {file}" execute Vim script */
2208 case 'i': /* "-i {viminfo}" use for viminfo */
2209#ifndef FEAT_DIFF
2210 case 'd': /* "-d {device}" device (for Amiga) */
2211#endif
2212 case 'T': /* "-T {terminal}" terminal name */
2213 case 'u': /* "-u {vimrc}" vim inits file */
2214 case 'U': /* "-U {gvimrc}" gvim inits file */
2215 case 'W': /* "-W {scriptout}" overwrite */
2216#ifdef FEAT_GUI_W32
2217 case 'P': /* "-P {parent title}" MDI parent */
2218#endif
2219 want_argument = TRUE;
2220 break;
2221
2222 default:
2223 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2224 }
2225
2226 /*
2227 * Handle option arguments with argument.
2228 */
2229 if (want_argument)
2230 {
2231 /*
2232 * Check for garbage immediately after the option letter.
2233 */
2234 if (argv[0][argv_idx] != NUL)
2235 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2236
2237 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002238 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002239 mainerr_arg_missing((char_u *)argv[0]);
2240 ++argv;
2241 argv_idx = -1;
2242
2243 switch (c)
2244 {
2245 case 'c': /* "-c {command}" execute command */
2246 case 'S': /* "-S {file}" execute Vim script */
2247 if (parmp->n_commands >= MAX_ARG_CMDS)
2248 mainerr(ME_EXTRA_CMD, NULL);
2249 if (c == 'S')
2250 {
2251 char *a;
2252
2253 if (argc < 1)
2254 /* "-S" without argument: use default session file
2255 * name. */
2256 a = SESSION_FILE;
2257 else if (argv[0][0] == '-')
2258 {
2259 /* "-S" followed by another option: use default
2260 * session file name. */
2261 a = SESSION_FILE;
2262 ++argc;
2263 --argv;
2264 }
2265 else
2266 a = argv[0];
2267 p = alloc((unsigned)(STRLEN(a) + 4));
2268 if (p == NULL)
2269 mch_exit(2);
2270 sprintf((char *)p, "so %s", a);
2271 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2272 parmp->commands[parmp->n_commands++] = p;
2273 }
2274 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002275 parmp->commands[parmp->n_commands++] =
2276 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002277 break;
2278
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002279 case '-':
2280 if (argv[-1][2] == 'c')
2281 {
2282 /* "--cmd {command}" execute command */
2283 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2284 mainerr(ME_EXTRA_CMD, NULL);
2285 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002286 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002287 }
2288 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002289 break;
2290
2291 /* case 'd': -d {device} is handled in mch_check_win() for the
2292 * Amiga */
2293
2294#ifdef FEAT_QUICKFIX
2295 case 'q': /* "-q {errorfile}" QuickFix mode */
2296 parmp->use_ef = (char_u *)argv[0];
2297 break;
2298#endif
2299
2300 case 'i': /* "-i {viminfo}" use for viminfo */
2301 use_viminfo = (char_u *)argv[0];
2302 break;
2303
2304 case 's': /* "-s {scriptin}" read from script file */
2305 if (scriptin[0] != NULL)
2306 {
2307scripterror:
2308 mch_errmsg(_("Attempt to open script file again: \""));
2309 mch_errmsg(argv[-1]);
2310 mch_errmsg(" ");
2311 mch_errmsg(argv[0]);
2312 mch_errmsg("\"\n");
2313 mch_exit(2);
2314 }
2315 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2316 {
2317 mch_errmsg(_("Cannot open for reading: \""));
2318 mch_errmsg(argv[0]);
2319 mch_errmsg("\"\n");
2320 mch_exit(2);
2321 }
2322 if (save_typebuf() == FAIL)
2323 mch_exit(2); /* out of memory */
2324 break;
2325
2326 case 't': /* "-t {tag}" */
2327 parmp->tagname = (char_u *)argv[0];
2328 break;
2329
2330 case 'T': /* "-T {terminal}" terminal name */
2331 /*
2332 * The -T term argument is always available and when
2333 * HAVE_TERMLIB is supported it overrides the environment
2334 * variable TERM.
2335 */
2336#ifdef FEAT_GUI
2337 if (term_is_gui((char_u *)argv[0]))
2338 gui.starting = TRUE; /* start GUI a bit later */
2339 else
2340#endif
2341 parmp->term = (char_u *)argv[0];
2342 break;
2343
2344 case 'u': /* "-u {vimrc}" vim inits file */
2345 parmp->use_vimrc = (char_u *)argv[0];
2346 break;
2347
2348 case 'U': /* "-U {gvimrc}" gvim inits file */
2349#ifdef FEAT_GUI
2350 use_gvimrc = (char_u *)argv[0];
2351#endif
2352 break;
2353
2354 case 'w': /* "-w {nr}" 'window' value */
2355 /* "-w {scriptout}" append to script file */
2356 if (vim_isdigit(*((char_u *)argv[0])))
2357 {
2358 argv_idx = 0;
2359 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2360 set_option_value((char_u *)"window", n, NULL, 0);
2361 argv_idx = -1;
2362 break;
2363 }
2364 /*FALLTHROUGH*/
2365 case 'W': /* "-W {scriptout}" overwrite script file */
2366 if (scriptout != NULL)
2367 goto scripterror;
2368 if ((scriptout = mch_fopen(argv[0],
2369 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2370 {
2371 mch_errmsg(_("Cannot open for script output: \""));
2372 mch_errmsg(argv[0]);
2373 mch_errmsg("\"\n");
2374 mch_exit(2);
2375 }
2376 break;
2377
2378#ifdef FEAT_GUI_W32
2379 case 'P': /* "-P {parent title}" MDI parent */
2380 gui_mch_set_parent(argv[0]);
2381 break;
2382#endif
2383 }
2384 }
2385 }
2386
2387 /*
2388 * File name argument.
2389 */
2390 else
2391 {
2392 argv_idx = -1; /* skip to next argument */
2393
2394 /* Check for only one type of editing. */
2395 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2396 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2397 parmp->edit_type = EDIT_FILE;
2398
2399#ifdef MSWIN
2400 /* Remember if the argument was a full path before changing
2401 * slashes to backslashes. */
2402 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2403 parmp->full_path = TRUE;
2404#endif
2405
2406 /* Add the file to the global argument list. */
2407 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2408 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2409 mch_exit(2);
2410#ifdef FEAT_DIFF
2411 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2412 && !mch_isdir(alist_name(&GARGLIST[0])))
2413 {
2414 char_u *r;
2415
2416 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2417 if (r != NULL)
2418 {
2419 vim_free(p);
2420 p = r;
2421 }
2422 }
2423#endif
2424#if defined(__CYGWIN32__) && !defined(WIN32)
2425 /*
2426 * If vim is invoked by non-Cygwin tools, convert away any
2427 * DOS paths, so things like .swp files are created correctly.
2428 * Look for evidence of non-Cygwin paths before we bother.
2429 * This is only for when using the Unix files.
2430 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002431 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002432 {
2433 char posix_path[PATH_MAX];
2434
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002435# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2436 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2437# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002438 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002439# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002441 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002442 if (p == NULL)
2443 mch_exit(2);
2444 }
2445#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002446
2447#ifdef USE_FNAME_CASE
2448 /* Make the case of the file name match the actual file. */
2449 fname_case(p, 0);
2450#endif
2451
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002452 alist_add(&global_alist, p,
2453#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002454 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002455#else
2456 2 /* add buffer number now and use curbuf */
2457#endif
2458 );
2459
2460#if defined(FEAT_MBYTE) && defined(WIN32)
2461 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462 /* Remember this argument has been added to the argument list.
2463 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002464 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002465# ifdef FEAT_DIFF
2466 parmp->diff_mode
2467# else
2468 FALSE
2469# endif
2470 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002471 }
2472#endif
2473 }
2474
2475 /*
2476 * If there are no more letters after the current "-", go to next
2477 * argument. argv_idx is set to -1 when the current argument is to be
2478 * skipped.
2479 */
2480 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2481 {
2482 --argc;
2483 ++argv;
2484 argv_idx = 1;
2485 }
2486 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002487
2488#ifdef FEAT_EVAL
2489 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2490 * one. */
2491 if (parmp->n_commands > 0)
2492 {
2493 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2494 if (p != NULL)
2495 {
2496 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2497 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2498 vim_free(p);
2499 }
2500 }
2501#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002502}
2503
2504/*
2505 * Print a warning if stdout is not a terminal.
2506 * When starting in Ex mode and commands come from a file, set Silent mode.
2507 */
2508 static void
2509check_tty(parmp)
2510 mparm_T *parmp;
2511{
2512 int input_isatty; /* is active input a terminal? */
2513
2514 input_isatty = mch_input_isatty();
2515 if (exmode_active)
2516 {
2517 if (!input_isatty)
2518 silent_mode = TRUE;
2519 }
2520 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2521#ifdef FEAT_GUI
2522 /* don't want the delay when started from the desktop */
2523 && !gui.starting
2524#endif
2525 )
2526 {
2527#ifdef NBDEBUG
2528 /*
2529 * This shouldn't be necessary. But if I run netbeans with the log
2530 * output coming to the console and XOpenDisplay fails, I get vim
2531 * trying to start with input/output to my console tty. This fills my
2532 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002533 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002534 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002535 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002536 {
2537 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2538 exit(1);
2539 }
2540#endif
2541 if (!parmp->stdout_isatty)
2542 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2543 if (!input_isatty)
2544 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2545 out_flush();
2546 if (scriptin[0] == NULL)
2547 ui_delay(2000L, TRUE);
2548 TIME_MSG("Warning delay");
2549 }
2550}
2551
2552/*
2553 * Read text from stdin.
2554 */
2555 static void
2556read_stdin()
2557{
2558 int i;
2559
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002560#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002561 /* When getting the ATTENTION prompt here, use a dialog */
2562 swap_exists_action = SEA_DIALOG;
2563#endif
2564 no_wait_return = TRUE;
2565 i = msg_didany;
2566 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002567 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002568 no_wait_return = FALSE;
2569 msg_didany = i;
2570 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002571#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002572 check_swap_exists_action();
2573#endif
2574#if !(defined(AMIGA) || defined(MACOS))
2575 /*
2576 * Close stdin and dup it from stderr. Required for GPM to work
2577 * properly, and for running external commands.
2578 * Is there any other system that cannot do this?
2579 */
2580 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002581 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002582#endif
2583}
2584
2585/*
2586 * Create the requested number of windows and edit buffers in them.
2587 * Also does recovery if "recoverymode" set.
2588 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 static void
2590create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002591 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592{
2593#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002594 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002595 int done = 0;
2596
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597 /*
2598 * Create the number of windows that was requested.
2599 */
2600 if (parmp->window_count == -1) /* was not set */
2601 parmp->window_count = 1;
2602 if (parmp->window_count == 0)
2603 parmp->window_count = GARGCOUNT;
2604 if (parmp->window_count > 1)
2605 {
2606 /* Don't change the windows if there was a command in .vimrc that
2607 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002608 if (parmp->window_layout == 0)
2609 parmp->window_layout = WIN_HOR;
2610 if (parmp->window_layout == WIN_TABS)
2611 {
2612 parmp->window_count = make_tabpages(parmp->window_count);
2613 TIME_MSG("making tab pages");
2614 }
2615 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002616 {
2617 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002618 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002619 TIME_MSG("making windows");
2620 }
2621 else
2622 parmp->window_count = win_count();
2623 }
2624 else
2625 parmp->window_count = 1;
2626#endif
2627
2628 if (recoverymode) /* do recover */
2629 {
2630 msg_scroll = TRUE; /* scroll message up */
2631 ml_recover();
2632 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2633 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002634 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002635 }
2636 else
2637 {
2638 /*
2639 * Open a buffer for windows that don't have one yet.
2640 * Commands in the .vimrc might have loaded a file or split the window.
2641 * Watch out for autocommands that delete a window.
2642 */
2643#ifdef FEAT_AUTOCMD
2644 /*
2645 * Don't execute Win/Buf Enter/Leave autocommands here
2646 */
2647 ++autocmd_no_enter;
2648 ++autocmd_no_leave;
2649#endif
2650#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002651 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002652 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002653 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002654 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002655 {
2656 if (parmp->window_layout == WIN_TABS)
2657 goto_tabpage(1);
2658 else
2659 curwin = firstwin;
2660 }
2661 else if (parmp->window_layout == WIN_TABS)
2662 {
2663 if (curtab->tp_next == NULL)
2664 break;
2665 goto_tabpage(0);
2666 }
2667 else
2668 {
2669 if (curwin->w_next == NULL)
2670 break;
2671 curwin = curwin->w_next;
2672 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002673 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002674#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002675 curbuf = curwin->w_buffer;
2676 if (curbuf->b_ml.ml_mfp == NULL)
2677 {
2678#ifdef FEAT_FOLDING
2679 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2680 if (p_fdls >= 0)
2681 curwin->w_p_fdl = p_fdls;
2682#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002683#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002684 /* When getting the ATTENTION prompt here, use a dialog */
2685 swap_exists_action = SEA_DIALOG;
2686#endif
2687 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002688
2689 /* create memfile, read file */
2690 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002691
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002692#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002693 if (swap_exists_action == SEA_QUIT)
2694 {
2695 if (got_int || only_one_window())
2696 {
2697 /* abort selected or quit and only one window */
2698 did_emsg = FALSE; /* avoid hit-enter prompt */
2699 getout(1);
2700 }
2701 /* We can't close the window, it would disturb what
2702 * happens next. Clear the file name and set the arg
2703 * index to -1 to delete it later. */
2704 setfname(curbuf, NULL, NULL, FALSE);
2705 curwin->w_arg_idx = -1;
2706 swap_exists_action = SEA_NONE;
2707 }
2708 else
2709 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002710#endif
2711#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002712 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002713#endif
2714 }
2715#ifdef FEAT_WINDOWS
2716 ui_breakcheck();
2717 if (got_int)
2718 {
2719 (void)vgetc(); /* only break the file loading, not the rest */
2720 break;
2721 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002722 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002723#endif
2724#ifdef FEAT_WINDOWS
2725 if (parmp->window_layout == WIN_TABS)
2726 goto_tabpage(1);
2727 else
2728 curwin = firstwin;
2729 curbuf = curwin->w_buffer;
2730#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002731#ifdef FEAT_AUTOCMD
2732 --autocmd_no_enter;
2733 --autocmd_no_leave;
2734#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002735 }
2736}
2737
2738#ifdef FEAT_WINDOWS
2739 /*
2740 * If opened more than one window, start editing files in the other
2741 * windows. make_windows() has already opened the windows.
2742 */
2743 static void
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002744edit_buffers(parmp, cwd)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002745 mparm_T *parmp;
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002746 char_u *cwd; /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002747{
2748 int arg_idx; /* index in argument list */
2749 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002750 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002751 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752
2753# ifdef FEAT_AUTOCMD
2754 /*
2755 * Don't execute Win/Buf Enter/Leave autocommands here
2756 */
2757 ++autocmd_no_enter;
2758 ++autocmd_no_leave;
2759# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002760
2761 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2762 if (curwin->w_arg_idx == -1)
2763 {
2764 win_close(curwin, TRUE);
2765 advance = FALSE;
2766 }
2767
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002768 arg_idx = 1;
2769 for (i = 1; i < parmp->window_count; ++i)
2770 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002771 if (cwd != NULL)
2772 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002773 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2774 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002775 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002776 ++arg_idx;
2777 win_close(curwin, TRUE);
2778 advance = FALSE;
2779 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002780 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002781
Bram Moolenaar84212822006-11-07 21:59:47 +00002782 if (advance)
2783 {
2784 if (parmp->window_layout == WIN_TABS)
2785 {
2786 if (curtab->tp_next == NULL) /* just checking */
2787 break;
2788 goto_tabpage(0);
2789 }
2790 else
2791 {
2792 if (curwin->w_next == NULL) /* just checking */
2793 break;
2794 win_enter(curwin->w_next, FALSE);
2795 }
2796 }
2797 advance = TRUE;
2798
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002799 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002800 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002801 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2802 {
2803 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002804 /* Edit file from arg list, if there is one. When "Quit" selected
2805 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002806# ifdef HAS_SWAP_EXISTS_ACTION
2807 swap_exists_did_quit = FALSE;
2808# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002809 (void)do_ecmd(0, arg_idx < GARGCOUNT
2810 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002811 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002812# ifdef HAS_SWAP_EXISTS_ACTION
2813 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002814 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002815 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002816 if (got_int || only_one_window())
2817 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002818 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002819 did_emsg = FALSE; /* avoid hit-enter prompt */
2820 getout(1);
2821 }
2822 win_close(curwin, TRUE);
2823 advance = FALSE;
2824 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002825# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002826 if (arg_idx == GARGCOUNT - 1)
2827 arg_had_last = TRUE;
2828 ++arg_idx;
2829 }
2830 ui_breakcheck();
2831 if (got_int)
2832 {
2833 (void)vgetc(); /* only break the file loading, not the rest */
2834 break;
2835 }
2836 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002837
2838 if (parmp->window_layout == WIN_TABS)
2839 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002840# ifdef FEAT_AUTOCMD
2841 --autocmd_no_enter;
2842# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002843
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002844 /* make the first window the current window */
2845 win = firstwin;
2846#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2847 /* Avoid making a preview window the current window. */
2848 while (win->w_p_pvw)
2849 {
2850 win = win->w_next;
2851 if (win == NULL)
2852 {
2853 win = firstwin;
2854 break;
2855 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002856 }
2857#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002858 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002859
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002860# ifdef FEAT_AUTOCMD
2861 --autocmd_no_leave;
2862# endif
2863 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002864 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002865 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2866}
2867#endif /* FEAT_WINDOWS */
2868
2869/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002870 * Execute the commands from --cmd arguments "cmds[cnt]".
2871 */
2872 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002873exe_pre_commands(parmp)
2874 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002875{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002876 char_u **cmds = parmp->pre_commands;
2877 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002878 int i;
2879
2880 if (cnt > 0)
2881 {
2882 curwin->w_cursor.lnum = 0; /* just in case.. */
2883 sourcing_name = (char_u *)_("pre-vimrc command line");
2884# ifdef FEAT_EVAL
2885 current_SID = SID_CMDARG;
2886# endif
2887 for (i = 0; i < cnt; ++i)
2888 do_cmdline_cmd(cmds[i]);
2889 sourcing_name = NULL;
2890# ifdef FEAT_EVAL
2891 current_SID = 0;
2892# endif
2893 TIME_MSG("--cmd commands");
2894 }
2895}
2896
2897/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002898 * Execute "+", "-c" and "-S" arguments.
2899 */
2900 static void
2901exe_commands(parmp)
2902 mparm_T *parmp;
2903{
2904 int i;
2905
2906 /*
2907 * We start commands on line 0, make "vim +/pat file" match a
2908 * pattern on line 1. But don't move the cursor when an autocommand
2909 * with g`" was used.
2910 */
2911 msg_scroll = TRUE;
2912 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2913 curwin->w_cursor.lnum = 0;
2914 sourcing_name = (char_u *)"command line";
2915#ifdef FEAT_EVAL
2916 current_SID = SID_CARG;
2917#endif
2918 for (i = 0; i < parmp->n_commands; ++i)
2919 {
2920 do_cmdline_cmd(parmp->commands[i]);
2921 if (parmp->cmds_tofree[i])
2922 vim_free(parmp->commands[i]);
2923 }
2924 sourcing_name = NULL;
2925#ifdef FEAT_EVAL
2926 current_SID = 0;
2927#endif
2928 if (curwin->w_cursor.lnum == 0)
2929 curwin->w_cursor.lnum = 1;
2930
2931 if (!exmode_active)
2932 msg_scroll = FALSE;
2933
2934#ifdef FEAT_QUICKFIX
2935 /* When started with "-q errorfile" jump to first error again. */
2936 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002937 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002938#endif
2939 TIME_MSG("executing command arguments");
2940}
2941
2942/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002943 * Source startup scripts.
2944 */
2945 static void
2946source_startup_scripts(parmp)
2947 mparm_T *parmp;
2948{
2949 int i;
2950
2951 /*
2952 * For "evim" source evim.vim first of all, so that the user can overrule
2953 * any things he doesn't like.
2954 */
2955 if (parmp->evim_mode)
2956 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002957 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002958 TIME_MSG("source evim file");
2959 }
2960
2961 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002962 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002963 * nothing else.
2964 */
2965 if (parmp->use_vimrc != NULL)
2966 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002967 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2968 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002969 {
2970#ifdef FEAT_GUI
2971 if (use_gvimrc == NULL) /* don't load gvimrc either */
2972 use_gvimrc = parmp->use_vimrc;
2973#endif
2974 if (parmp->use_vimrc[2] == 'N')
2975 p_lpl = FALSE; /* don't load plugins either */
2976 }
2977 else
2978 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002979 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002980 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2981 }
2982 }
2983 else if (!silent_mode)
2984 {
2985#ifdef AMIGA
2986 struct Process *proc = (struct Process *)FindTask(0L);
2987 APTR save_winptr = proc->pr_WindowPtr;
2988
2989 /* Avoid a requester here for a volume that doesn't exist. */
2990 proc->pr_WindowPtr = (APTR)-1L;
2991#endif
2992
2993 /*
2994 * Get system wide defaults, if the file name is defined.
2995 */
2996#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002997 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002998#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002999#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003000 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003001#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003002
3003 /*
3004 * Try to read initialization commands from the following places:
3005 * - environment variable VIMINIT
3006 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3007 * - second user vimrc file ($VIM/.vimrc for Dos)
3008 * - environment variable EXINIT
3009 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3010 * - second user exrc file ($VIM/.exrc for Dos)
3011 * The first that exists is used, the rest is ignored.
3012 */
3013 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3014 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003015 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003016#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003017 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3018 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003019#endif
3020#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003021 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3022 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003023#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003024#ifdef USR_VIMRC_FILE4
3025 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3026 DOSO_VIMRC) == FAIL
3027#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003028 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003029 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003030 {
3031#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003032 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003033#endif
3034 }
3035 }
3036
3037 /*
3038 * Read initialization commands from ".vimrc" or ".exrc" in current
3039 * directory. This is only done if the 'exrc' option is set.
3040 * Because of security reasons we disallow shell and write commands
3041 * now, except for unix if the file is owned by the user or 'secure'
3042 * option has been reset in environment of global ".exrc" or ".vimrc".
3043 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3044 * SYS_VIMRC_FILE.
3045 */
3046 if (p_exrc)
3047 {
3048#if defined(UNIX) || defined(VMS)
3049 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3050 if (!file_owned(VIMRC_FILE))
3051#endif
3052 secure = p_secure;
3053
3054 i = FAIL;
3055 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3056 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3057#ifdef USR_VIMRC_FILE2
3058 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3059 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3060#endif
3061#ifdef USR_VIMRC_FILE3
3062 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3063 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3064#endif
3065#ifdef SYS_VIMRC_FILE
3066 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3067 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3068#endif
3069 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003070 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003071
3072 if (i == FAIL)
3073 {
3074#if defined(UNIX) || defined(VMS)
3075 /* if ".exrc" is not owned by user set 'secure' mode */
3076 if (!file_owned(EXRC_FILE))
3077 secure = p_secure;
3078 else
3079 secure = 0;
3080#endif
3081 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3082 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3083#ifdef USR_EXRC_FILE2
3084 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3085 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3086#endif
3087 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003088 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003089 }
3090 }
3091 if (secure == 2)
3092 need_wait_return = TRUE;
3093 secure = 0;
3094#ifdef AMIGA
3095 proc->pr_WindowPtr = save_winptr;
3096#endif
3097 }
3098 TIME_MSG("sourcing vimrc file(s)");
3099}
3100
3101/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003102 * Setup to start using the GUI. Exit with an error when not available.
3103 */
3104 static void
3105main_start_gui()
3106{
3107#ifdef FEAT_GUI
3108 gui.starting = TRUE; /* start GUI a bit later */
3109#else
3110 mch_errmsg(_(e_nogvim));
3111 mch_errmsg("\n");
3112 mch_exit(2);
3113#endif
3114}
3115
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003116#endif /* NO_VIM_MAIN */
3117
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003118/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003119 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003120 * Returns FAIL if the environment variable was not executed, OK otherwise.
3121 */
3122 int
3123process_env(env, is_viminit)
3124 char_u *env;
3125 int is_viminit; /* when TRUE, called for VIMINIT */
3126{
3127 char_u *initstr;
3128 char_u *save_sourcing_name;
3129 linenr_T save_sourcing_lnum;
3130#ifdef FEAT_EVAL
3131 scid_T save_sid;
3132#endif
3133
3134 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3135 {
3136 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003137 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003138 save_sourcing_name = sourcing_name;
3139 save_sourcing_lnum = sourcing_lnum;
3140 sourcing_name = env;
3141 sourcing_lnum = 0;
3142#ifdef FEAT_EVAL
3143 save_sid = current_SID;
3144 current_SID = SID_ENV;
3145#endif
3146 do_cmdline_cmd(initstr);
3147 sourcing_name = save_sourcing_name;
3148 sourcing_lnum = save_sourcing_lnum;
3149#ifdef FEAT_EVAL
3150 current_SID = save_sid;;
3151#endif
3152 return OK;
3153 }
3154 return FAIL;
3155}
3156
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003157#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003158/*
3159 * Return TRUE if we are certain the user owns the file "fname".
3160 * Used for ".vimrc" and ".exrc".
3161 * Use both stat() and lstat() for extra security.
3162 */
3163 static int
3164file_owned(fname)
3165 char *fname;
3166{
3167 struct stat s;
3168# ifdef UNIX
3169 uid_t uid = getuid();
3170# else /* VMS */
3171 uid_t uid = ((getgid() << 16) | getuid());
3172# endif
3173
3174 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3175# ifdef HAVE_LSTAT
3176 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3177# endif
3178 );
3179}
3180#endif
3181
3182/*
3183 * Give an error message main_errors["n"] and exit.
3184 */
3185 static void
3186mainerr(n, str)
3187 int n; /* one of the ME_ defines */
3188 char_u *str; /* extra argument or NULL */
3189{
3190#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3191 reset_signals(); /* kill us with CTRL-C here, if you like */
3192#endif
3193
3194 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003195 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003196 mch_errmsg(_(main_errors[n]));
3197 if (str != NULL)
3198 {
3199 mch_errmsg(": \"");
3200 mch_errmsg((char *)str);
3201 mch_errmsg("\"");
3202 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003203 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003204
3205 mch_exit(1);
3206}
3207
3208 void
3209mainerr_arg_missing(str)
3210 char_u *str;
3211{
3212 mainerr(ME_ARG_MISSING, str);
3213}
3214
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003215#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003216/*
3217 * print a message with three spaces prepended and '\n' appended.
3218 */
3219 static void
3220main_msg(s)
3221 char *s;
3222{
3223 mch_msg(" ");
3224 mch_msg(s);
3225 mch_msg("\n");
3226}
3227
3228/*
3229 * Print messages for "vim -h" or "vim --help" and exit.
3230 */
3231 static void
3232usage()
3233{
3234 int i;
3235 static char *(use[]) =
3236 {
3237 N_("[file ..] edit specified file(s)"),
3238 N_("- read text from stdin"),
3239 N_("-t tag edit file where tag is defined"),
3240#ifdef FEAT_QUICKFIX
3241 N_("-q [errorfile] edit file with first error")
3242#endif
3243 };
3244
3245#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3246 reset_signals(); /* kill us with CTRL-C here, if you like */
3247#endif
3248
3249 mch_msg(longVersion);
3250 mch_msg(_("\n\nusage:"));
3251 for (i = 0; ; ++i)
3252 {
3253 mch_msg(_(" vim [arguments] "));
3254 mch_msg(_(use[i]));
3255 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3256 break;
3257 mch_msg(_("\n or:"));
3258 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003259#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003260 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003261#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003262
3263 mch_msg(_("\n\nArguments:\n"));
3264 main_msg(_("--\t\t\tOnly file names after this"));
3265#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3266 main_msg(_("--literal\t\tDon't expand wildcards"));
3267#endif
3268#ifdef FEAT_OLE
3269 main_msg(_("-register\t\tRegister this gvim for OLE"));
3270 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3271#endif
3272#ifdef FEAT_GUI
3273 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3274 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3275#endif
3276 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3277 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003278 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003279 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3280#ifdef FEAT_DIFF
3281 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3282#endif
3283 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3284 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3285 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3286 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3287 main_msg(_("-M\t\t\tModifications in text not allowed"));
3288 main_msg(_("-b\t\t\tBinary mode"));
3289#ifdef FEAT_LISP
3290 main_msg(_("-l\t\t\tLisp mode"));
3291#endif
3292 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3293 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003294 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3295#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003296 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003297#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003298 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3299 main_msg(_("-r\t\t\tList swap files and exit"));
3300 main_msg(_("-r (with file name)\tRecover crashed session"));
3301 main_msg(_("-L\t\t\tSame as -r"));
3302#ifdef AMIGA
3303 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3304 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3305#endif
3306#ifdef FEAT_ARABIC
3307 main_msg(_("-A\t\t\tstart in Arabic mode"));
3308#endif
3309#ifdef FEAT_RIGHTLEFT
3310 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3311#endif
3312#ifdef FEAT_FKMAP
3313 main_msg(_("-F\t\t\tStart in Farsi mode"));
3314#endif
3315 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3316 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3317#ifdef FEAT_GUI
3318 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3319#endif
3320 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003321#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003322 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003323 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3324 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003325#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003326 main_msg(_("+\t\t\tStart at end of file"));
3327 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003329 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3330 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3331 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3332 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3333 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3334#ifdef FEAT_CRYPT
3335 main_msg(_("-x\t\t\tEdit encrypted files"));
3336#endif
3337#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3338# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3339 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3340# endif
3341 main_msg(_("-X\t\t\tDo not connect to X server"));
3342#endif
3343#ifdef FEAT_CLIENTSERVER
3344 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3345 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3346 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3347 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003348# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003349 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003350# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003351 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3352 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3353 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3354 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3355#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003356#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003357 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003358#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003359#ifdef FEAT_VIMINFO
3360 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3361#endif
3362 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3363 main_msg(_("--version\t\tPrint version information and exit"));
3364
3365#ifdef FEAT_GUI_X11
3366# ifdef FEAT_GUI_MOTIF
3367 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3368# else
3369# ifdef FEAT_GUI_ATHENA
3370# ifdef FEAT_GUI_NEXTAW
3371 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3372# else
3373 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3374# endif
3375# endif
3376# endif
3377 main_msg(_("-display <display>\tRun vim on <display>"));
3378 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003379 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3380 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3381 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3382 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3383 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3384 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3385 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3386 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3387# ifdef FEAT_GUI_ATHENA
3388 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3389# endif
3390 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3391 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3392 main_msg(_("-xrm <resource>\tSet the specified resource"));
3393#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003394#ifdef FEAT_GUI_GTK
3395 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3396 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3397 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3398 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3399 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003401 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003402 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003404#ifdef FEAT_GUI_W32
3405 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003406 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003407#endif
3408
3409#ifdef FEAT_GUI_GNOME
3410 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3411 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003412 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003414 gui.dofork = FALSE;
3415 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003416 else
3417#endif
3418 mch_exit(0);
3419}
3420
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003421#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003422/*
3423 * Check the result of the ATTENTION dialog:
3424 * When "Quit" selected, exit Vim.
3425 * When "Recover" selected, recover the file.
3426 */
3427 static void
3428check_swap_exists_action()
3429{
3430 if (swap_exists_action == SEA_QUIT)
3431 getout(1);
3432 handle_swap_exists(NULL);
3433}
3434#endif
3435
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003436#endif
3437
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003438#if defined(STARTUPTIME) || defined(PROTO)
3439static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3440
3441static struct timeval prev_timeval;
3442
Bram Moolenaar3f269672009-11-03 11:11:11 +00003443# ifdef WIN3264
3444/*
3445 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3446 */
3447 static int
3448gettimeofday(struct timeval *tv, char *dummy)
3449{
3450 long t = clock();
3451 tv->tv_sec = t / CLOCKS_PER_SEC;
3452 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3453 return 0;
3454}
3455# endif
3456
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003457/*
3458 * Save the previous time before doing something that could nest.
3459 * set "*tv_rel" to the time elapsed so far.
3460 */
3461 void
3462time_push(tv_rel, tv_start)
3463 void *tv_rel, *tv_start;
3464{
3465 *((struct timeval *)tv_rel) = prev_timeval;
3466 gettimeofday(&prev_timeval, NULL);
3467 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3468 - ((struct timeval *)tv_rel)->tv_usec;
3469 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3470 - ((struct timeval *)tv_rel)->tv_sec;
3471 if (((struct timeval *)tv_rel)->tv_usec < 0)
3472 {
3473 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3474 --((struct timeval *)tv_rel)->tv_sec;
3475 }
3476 *(struct timeval *)tv_start = prev_timeval;
3477}
3478
3479/*
3480 * Compute the previous time after doing something that could nest.
3481 * Subtract "*tp" from prev_timeval;
3482 * Note: The arguments are (void *) to avoid trouble with systems that don't
3483 * have struct timeval.
3484 */
3485 void
3486time_pop(tp)
3487 void *tp; /* actually (struct timeval *) */
3488{
3489 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3490 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3491 if (prev_timeval.tv_usec < 0)
3492 {
3493 prev_timeval.tv_usec += 1000000;
3494 --prev_timeval.tv_sec;
3495 }
3496}
3497
3498 static void
3499time_diff(then, now)
3500 struct timeval *then;
3501 struct timeval *now;
3502{
3503 long usec;
3504 long msec;
3505
3506 usec = now->tv_usec - then->tv_usec;
3507 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3508 usec = usec % 1000L;
3509 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3510}
3511
3512 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003513time_msg(mesg, tv_start)
3514 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003515 void *tv_start; /* only for do_source: start time; actually
3516 (struct timeval *) */
3517{
3518 static struct timeval start;
3519 struct timeval now;
3520
3521 if (time_fd != NULL)
3522 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003523 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003524 {
3525 gettimeofday(&start, NULL);
3526 prev_timeval = start;
3527 fprintf(time_fd, "\n\ntimes in msec\n");
3528 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3529 fprintf(time_fd, " clock elapsed: other lines\n\n");
3530 }
3531 gettimeofday(&now, NULL);
3532 time_diff(&start, &now);
3533 if (((struct timeval *)tv_start) != NULL)
3534 {
3535 fprintf(time_fd, " ");
3536 time_diff(((struct timeval *)tv_start), &now);
3537 }
3538 fprintf(time_fd, " ");
3539 time_diff(&prev_timeval, &now);
3540 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003541 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003542 }
3543}
3544
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003545#endif
3546
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003547#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003548
3549/*
3550 * Common code for the X command server and the Win32 command server.
3551 */
3552
Bram Moolenaareb94e552006-03-11 21:35:11 +00003553static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003554
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003555/*
3556 * Do the client-server stuff, unless "--servername ''" was used.
3557 */
3558 static void
3559exec_on_server(parmp)
3560 mparm_T *parmp;
3561{
3562 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3563 {
3564# ifdef WIN32
3565 /* Initialise the client/server messaging infrastructure. */
3566 serverInitMessaging();
3567# endif
3568
3569 /*
3570 * When a command server argument was found, execute it. This may
3571 * exit Vim when it was successful. Otherwise it's executed further
3572 * on. Remember the encoding used here in "serverStrEnc".
3573 */
3574 if (parmp->serverArg)
3575 {
3576 cmdsrv_main(&parmp->argc, parmp->argv,
3577 parmp->serverName_arg, &parmp->serverStr);
3578# ifdef FEAT_MBYTE
3579 parmp->serverStrEnc = vim_strsave(p_enc);
3580# endif
3581 }
3582
3583 /* If we're still running, get the name to register ourselves.
3584 * On Win32 can register right now, for X11 need to setup the
3585 * clipboard first, it's further down. */
3586 parmp->servername = serverMakeName(parmp->serverName_arg,
3587 parmp->argv[0]);
3588# ifdef WIN32
3589 if (parmp->servername != NULL)
3590 {
3591 serverSetName(parmp->servername);
3592 vim_free(parmp->servername);
3593 }
3594# endif
3595 }
3596}
3597
3598/*
3599 * Prepare for running as a Vim server.
3600 */
3601 static void
3602prepare_server(parmp)
3603 mparm_T *parmp;
3604{
3605# if defined(FEAT_X11)
3606 /*
3607 * Register for remote command execution with :serversend and --remote
3608 * unless there was a -X or a --servername '' on the command line.
3609 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003610 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003611 */
3612 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3613# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003614 (gui.in_use
3615# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003616 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003617# endif
3618 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003619# endif
3620 parmp->serverName_arg != NULL))
3621 {
3622 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3623 vim_free(parmp->servername);
3624 TIME_MSG("register server name");
3625 }
3626 else
3627 serverDelayedStartName = parmp->servername;
3628# endif
3629
3630 /*
3631 * Execute command ourselves if we're here because the send failed (or
3632 * else we would have exited above).
3633 */
3634 if (parmp->serverStr != NULL)
3635 {
3636 char_u *p;
3637
3638 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3639 parmp->serverStr, &p));
3640 vim_free(p);
3641 }
3642}
3643
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003644 static void
3645cmdsrv_main(argc, argv, serverName_arg, serverStr)
3646 int *argc;
3647 char **argv;
3648 char_u *serverName_arg;
3649 char_u **serverStr;
3650{
3651 char_u *res;
3652 int i;
3653 char_u *sname;
3654 int ret;
3655 int didone = FALSE;
3656 int exiterr = 0;
3657 char **newArgV = argv + 1;
3658 int newArgC = 1,
3659 Argc = *argc;
3660 int argtype;
3661#define ARGTYPE_OTHER 0
3662#define ARGTYPE_EDIT 1
3663#define ARGTYPE_EDIT_WAIT 2
3664#define ARGTYPE_SEND 3
3665 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003666 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003667# ifndef FEAT_X11
3668 HWND srv;
3669# else
3670 Window srv;
3671
3672 setup_term_clip();
3673# endif
3674
3675 sname = serverMakeName(serverName_arg, argv[0]);
3676 if (sname == NULL)
3677 return;
3678
3679 /*
3680 * Execute the command server related arguments and remove them
3681 * from the argc/argv array; We may have to return into main()
3682 */
3683 for (i = 1; i < Argc; i++)
3684 {
3685 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003686 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003687 {
3688 for (; i < *argc; i++)
3689 {
3690 *newArgV++ = argv[i];
3691 newArgC++;
3692 }
3693 break;
3694 }
3695
Bram Moolenaareb94e552006-03-11 21:35:11 +00003696 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003697 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003698 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3699 {
3700 char *p = argv[i] + 8;
3701
3702 argtype = ARGTYPE_EDIT;
3703 while (*p != NUL)
3704 {
3705 if (STRNICMP(p, "-wait", 5) == 0)
3706 {
3707 argtype = ARGTYPE_EDIT_WAIT;
3708 p += 5;
3709 }
3710 else if (STRNICMP(p, "-silent", 7) == 0)
3711 {
3712 silent = TRUE;
3713 p += 7;
3714 }
3715 else if (STRNICMP(p, "-tab", 4) == 0)
3716 {
3717 tabs = TRUE;
3718 p += 4;
3719 }
3720 else
3721 {
3722 argtype = ARGTYPE_OTHER;
3723 break;
3724 }
3725 }
3726 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003727 else
3728 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003729
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003730 if (argtype != ARGTYPE_OTHER)
3731 {
3732 if (i == *argc - 1)
3733 mainerr_arg_missing((char_u *)argv[i]);
3734 if (argtype == ARGTYPE_SEND)
3735 {
3736 *serverStr = (char_u *)argv[i + 1];
3737 i++;
3738 }
3739 else
3740 {
3741 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003742 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003743 if (*serverStr == NULL)
3744 {
3745 /* Probably out of memory, exit. */
3746 didone = TRUE;
3747 exiterr = 1;
3748 break;
3749 }
3750 Argc = i;
3751 }
3752# ifdef FEAT_X11
3753 if (xterm_dpy == NULL)
3754 {
3755 mch_errmsg(_("No display"));
3756 ret = -1;
3757 }
3758 else
3759 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3760 NULL, &srv, 0, 0, silent);
3761# else
3762 /* Win32 always works? */
3763 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3764# endif
3765 if (ret < 0)
3766 {
3767 if (argtype == ARGTYPE_SEND)
3768 {
3769 /* Failed to send, abort. */
3770 mch_errmsg(_(": Send failed.\n"));
3771 didone = TRUE;
3772 exiterr = 1;
3773 }
3774 else if (!silent)
3775 /* Let vim start normally. */
3776 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3777 break;
3778 }
3779
3780# ifdef FEAT_GUI_W32
3781 /* Guess that when the server name starts with "g" it's a GUI
3782 * server, which we can bring to the foreground here.
3783 * Foreground() in the server doesn't work very well. */
3784 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3785 SetForegroundWindow(srv);
3786# endif
3787
3788 /*
3789 * For --remote-wait: Wait until the server did edit each
3790 * file. Also detect that the server no longer runs.
3791 */
3792 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3793 {
3794 int numFiles = *argc - i - 1;
3795 int j;
3796 char_u *done = alloc(numFiles);
3797 char_u *p;
3798# ifdef FEAT_GUI_W32
3799 NOTIFYICONDATA ni;
3800 int count = 0;
3801 extern HWND message_window;
3802# endif
3803
3804 if (numFiles > 0 && argv[i + 1][0] == '+')
3805 /* Skip "+cmd" argument, don't wait for it to be edited. */
3806 --numFiles;
3807
3808# ifdef FEAT_GUI_W32
3809 ni.cbSize = sizeof(ni);
3810 ni.hWnd = message_window;
3811 ni.uID = 0;
3812 ni.uFlags = NIF_ICON|NIF_TIP;
3813 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3814 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3815 Shell_NotifyIcon(NIM_ADD, &ni);
3816# endif
3817
3818 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003819 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003820 while (memchr(done, 0, numFiles) != NULL)
3821 {
3822# ifdef WIN32
3823 p = serverGetReply(srv, NULL, TRUE, TRUE);
3824 if (p == NULL)
3825 break;
3826# else
3827 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3828 break;
3829# endif
3830 j = atoi((char *)p);
3831 if (j >= 0 && j < numFiles)
3832 {
3833# ifdef FEAT_GUI_W32
3834 ++count;
3835 sprintf(ni.szTip, _("%d of %d edited"),
3836 count, numFiles);
3837 Shell_NotifyIcon(NIM_MODIFY, &ni);
3838# endif
3839 done[j] = 1;
3840 }
3841 }
3842# ifdef FEAT_GUI_W32
3843 Shell_NotifyIcon(NIM_DELETE, &ni);
3844# endif
3845 }
3846 }
3847 else if (STRICMP(argv[i], "--remote-expr") == 0)
3848 {
3849 if (i == *argc - 1)
3850 mainerr_arg_missing((char_u *)argv[i]);
3851# ifdef WIN32
3852 /* Win32 always works? */
3853 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3854 &res, NULL, 1, FALSE) < 0)
3855# else
3856 if (xterm_dpy == NULL)
3857 mch_errmsg(_("No display: Send expression failed.\n"));
3858 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3859 &res, NULL, 1, 1, FALSE) < 0)
3860# endif
3861 {
3862 if (res != NULL && *res != NUL)
3863 {
3864 /* Output error from remote */
3865 mch_errmsg((char *)res);
3866 vim_free(res);
3867 res = NULL;
3868 }
3869 mch_errmsg(_(": Send expression failed.\n"));
3870 }
3871 }
3872 else if (STRICMP(argv[i], "--serverlist") == 0)
3873 {
3874# ifdef WIN32
3875 /* Win32 always works? */
3876 res = serverGetVimNames();
3877# else
3878 if (xterm_dpy != NULL)
3879 res = serverGetVimNames(xterm_dpy);
3880# endif
3881 if (called_emsg)
3882 mch_errmsg("\n");
3883 }
3884 else if (STRICMP(argv[i], "--servername") == 0)
3885 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003886 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003887 i++;
3888 continue;
3889 }
3890 else
3891 {
3892 *newArgV++ = argv[i];
3893 newArgC++;
3894 continue;
3895 }
3896 didone = TRUE;
3897 if (res != NULL && *res != NUL)
3898 {
3899 mch_msg((char *)res);
3900 if (res[STRLEN(res) - 1] != '\n')
3901 mch_msg("\n");
3902 }
3903 vim_free(res);
3904 }
3905
3906 if (didone)
3907 {
3908 display_errors(); /* display any collected messages */
3909 exit(exiterr); /* Mission accomplished - get out */
3910 }
3911
3912 /* Return back into main() */
3913 *argc = newArgC;
3914 vim_free(sname);
3915}
3916
3917/*
3918 * Build a ":drop" command to send to a Vim server.
3919 */
3920 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003921build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003922 int filec;
3923 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003924 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003925 int sendReply;
3926{
3927 garray_T ga;
3928 int i;
3929 char_u *inicmd = NULL;
3930 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003931 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003932 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003933
3934 if (filec > 0 && filev[0][0] == '+')
3935 {
3936 inicmd = (char_u *)filev[0] + 1;
3937 filev++;
3938 filec--;
3939 }
3940 /* Check if we have at least one argument. */
3941 if (filec <= 0)
3942 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003943
3944 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003945 cwd = alloc(MAXPATHL);
3946 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003947 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003948 if (mch_dirname(cwd, MAXPATHL) != OK)
3949 {
3950 vim_free(cwd);
3951 return NULL;
3952 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003953 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003954#ifdef BACKSLASH_IN_FILENAME
3955 "", /* rem_backslash() will tell what chars to escape */
3956#else
3957 PATH_ESC_CHARS,
3958#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003959 '\\', TRUE);
3960 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003961 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003962 return NULL;
3963 ga_init2(&ga, 1, 100);
3964 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003965 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003966
3967 /* Call inputsave() so that a prompt for an encryption key works. */
3968 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3969 if (tabs)
3970 ga_concat(&ga, (char_u *)"tab ");
3971 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003972 for (i = 0; i < filec; i++)
3973 {
3974 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003975 * do it again in the Vim server. On MS-Windows only escape
3976 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003977 p = vim_strsave_escaped((char_u *)filev[i],
3978#ifdef UNIX
3979 PATH_ESC_CHARS
3980#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003981 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003982#endif
3983 );
3984 if (p == NULL)
3985 {
3986 vim_free(ga.ga_data);
3987 return NULL;
3988 }
3989 ga_concat(&ga, (char_u *)" ");
3990 ga_concat(&ga, p);
3991 vim_free(p);
3992 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003993 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3994
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003995 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3996 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003997 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3998
3999 /* Switch back to the correct current directory (prior to temporary path
4000 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004001 * correct after the :drop command. With line breaks and spaces:
4002 * if !exists('+acd') || !&acd
4003 * if haslocaldir()
4004 * cd -
4005 * lcd -
4006 * elseif getcwd() ==# "current path"
4007 * cd -
4008 * endif
4009 * endif
4010 */
4011 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
4012 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# \"");
4013 ga_concat(&ga, cdp);
4014 ga_concat(&ga, (char_u *)"\"|cd -|endif|endif<CR>");
4015 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004016
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004017 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004018 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4019 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004020 if (inicmd != NULL)
4021 {
4022 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4023 * the following commands to be inserted as text. Use a "|",
4024 * hopefully "inicmd" does allow this... */
4025 ga_concat(&ga, inicmd);
4026 ga_concat(&ga, (char_u *)"|");
4027 }
4028 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4029 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004030 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004031 ga_append(&ga, NUL);
4032 return ga.ga_data;
4033}
4034
4035/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004036 * Make our basic server name: use the specified "arg" if given, otherwise use
4037 * the tail of the command "cmd" we were started with.
4038 * Return the name in allocated memory. This doesn't include a serial number.
4039 */
4040 static char_u *
4041serverMakeName(arg, cmd)
4042 char_u *arg;
4043 char *cmd;
4044{
4045 char_u *p;
4046
4047 if (arg != NULL && *arg != NUL)
4048 p = vim_strsave_up(arg);
4049 else
4050 {
4051 p = vim_strsave_up(gettail((char_u *)cmd));
4052 /* Remove .exe or .bat from the name. */
4053 if (p != NULL && vim_strchr(p, '.') != NULL)
4054 *vim_strchr(p, '.') = NUL;
4055 }
4056 return p;
4057}
4058#endif /* FEAT_CLIENTSERVER */
4059
4060#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4061/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004062 * Replace termcodes such as <CR> and insert as key presses if there is room.
4063 */
4064 void
4065server_to_input_buf(str)
4066 char_u *str;
4067{
4068 char_u *ptr = NULL;
4069 char_u *cpo_save = p_cpo;
4070
4071 /* Set 'cpoptions' the way we want it.
4072 * B set - backslashes are *not* treated specially
4073 * k set - keycodes are *not* reverse-engineered
4074 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004075 * The last but one parameter of replace_termcodes() is TRUE so that the
4076 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004077 */
4078 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004079 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004080 p_cpo = cpo_save;
4081
4082 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4083 {
4084 /*
4085 * Add the string to the input stream.
4086 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4087 *
4088 * First clear typed characters from the typeahead buffer, there could
4089 * be half a mapping there. Then append to the existing string, so
4090 * that multiple commands from a client are concatenated.
4091 */
4092 if (typebuf.tb_maplen < typebuf.tb_len)
4093 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4094 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4095
4096 /* Let input_available() know we inserted text in the typeahead
4097 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004098 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004099 }
4100 vim_free((char_u *)ptr);
4101}
4102
4103/*
4104 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004105 */
4106 char_u *
4107eval_client_expr_to_string(expr)
4108 char_u *expr;
4109{
4110 char_u *res;
4111 int save_dbl = debug_break_level;
4112 int save_ro = redir_off;
4113
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004114 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4115 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004116 debug_break_level = -1;
4117 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004118 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4119 * to be typed. Do generate errors so that try/catch works. */
4120 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004121
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004122 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004123
4124 debug_break_level = save_dbl;
4125 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004126 --emsg_silent;
4127 if (emsg_silent < 0)
4128 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004129
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004130 /* A client can tell us to redraw, but not to display the cursor, so do
4131 * that here. */
4132 setcursor();
4133 out_flush();
4134#ifdef FEAT_GUI
4135 if (gui.in_use)
4136 gui_update_cursor(FALSE, FALSE);
4137#endif
4138
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004139 return res;
4140}
4141
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004142/*
4143 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4144 * return an allocated string. Otherwise return "data".
4145 * "*tofree" is set to the result when it needs to be freed later.
4146 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004147 char_u *
4148serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004149 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004150 char_u *data;
4151 char_u **tofree;
4152{
4153 char_u *res = data;
4154
4155 *tofree = NULL;
4156# ifdef FEAT_MBYTE
4157 if (client_enc != NULL && p_enc != NULL)
4158 {
4159 vimconv_T vimconv;
4160
4161 vimconv.vc_type = CONV_NONE;
4162 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4163 && vimconv.vc_type != CONV_NONE)
4164 {
4165 res = string_convert(&vimconv, data, NULL);
4166 if (res == NULL)
4167 res = data;
4168 else
4169 *tofree = res;
4170 }
4171 convert_setup(&vimconv, NULL, NULL);
4172 }
4173# endif
4174 return res;
4175}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004176#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004177
4178/*
4179 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4180 */
4181#if defined(FEAT_FKMAP) || defined(PROTO)
4182# include "farsi.c"
4183#endif
4184
4185/*
4186 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4187 */
4188#if defined(FEAT_ARABIC) || defined(PROTO)
4189# include "arabic.c"
4190#endif