blob: dcc8274fd9af2ad0479c1e703ff4f47d25e78eb0 [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
Bram Moolenaar53076832015-12-31 19:53:21 +010077#if !defined(UNIX) && !defined(__EMX__)
78# define EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +000079 int literal; /* don't expand file names */
80#endif
81#ifdef MSWIN
82 int full_path; /* file name argument was full path */
83#endif
84#ifdef FEAT_DIFF
85 int diff_mode; /* start with 'diff' set */
86#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000087} mparm_T;
88
Bram Moolenaarc013cb62005-07-24 21:18:31 +000089/* Values for edit_type. */
90#define EDIT_NONE 0 /* no edit type yet */
91#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
92#define EDIT_STDIN 2 /* read file from stdin */
93#define EDIT_TAG 3 /* tag name argument given, use tagname */
94#define EDIT_QF 4 /* start in quickfix mode */
95
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010096#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010097static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000098#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010099static void mainerr(int, char_u *);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100100#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100101static void main_msg(char *s);
102static void usage(void);
103static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100104# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100105static void init_locale(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100106# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100107static void parse_command_name(mparm_T *parmp);
108static void early_arg_scan(mparm_T *parmp);
109static void command_line_scan(mparm_T *parmp);
110static void check_tty(mparm_T *parmp);
111static void read_stdin(void);
112static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100113# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100114static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100115# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100116static void exe_pre_commands(mparm_T *parmp);
117static void exe_commands(mparm_T *parmp);
118static void source_startup_scripts(mparm_T *parmp);
119static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100120# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100121static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100122# endif
123# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100124static void exec_on_server(mparm_T *parmp);
125static void prepare_server(mparm_T *parmp);
126static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
127static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100128# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000129#endif
130
131
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000132/*
133 * Different types of error messages.
134 */
135static char *(main_errors[]) =
136{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000137 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000138#define ME_UNKNOWN_OPTION 0
139 N_("Too many edit arguments"),
140#define ME_TOO_MANY_ARGS 1
141 N_("Argument missing after"),
142#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000143 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000144#define ME_GARBAGE 3
145 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
146#define ME_EXTRA_CMD 4
147 N_("Invalid argument for"),
148#define ME_INVALID_ARG 5
149};
150
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100151#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100152#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +0200153
154static char_u *start_dir = NULL; /* current working dir on startup */
155
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000156 int
157# ifdef VIMDLL
158_export
159# endif
160# ifdef FEAT_GUI_MSWIN
161# ifdef __BORLANDC__
162_cdecl
163# endif
164VimMain
165# else
166main
167# endif
168(argc, argv)
169 int argc;
170 char **argv;
171{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000172 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000173 mparm_T params; /* various parameters passed between
174 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000175#ifdef STARTUPTIME
176 int i;
177#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000178
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000179 /*
180 * Do any system-specific initialisations. These can NOT use IObuff or
181 * NameBuff. Thus emsg2() cannot be called!
182 */
183 mch_early_init();
184
Bram Moolenaar14993322014-09-09 12:25:33 +0200185#if defined(WIN32) && defined(FEAT_MBYTE)
186 /*
187 * MingW expands command line arguments, which confuses our code to
188 * convert when 'encoding' changes. Get the unexpanded arguments.
189 */
190 argc = get_cmd_argsW(&argv);
191#endif
192
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193 /* Many variables are in "params" so that we can pass them to invoked
194 * functions without a lot of arguments. "argc" and "argv" are also
195 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000196 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000197 params.argc = argc;
198 params.argv = argv;
199 params.want_full_screen = TRUE;
200#ifdef FEAT_EVAL
201 params.use_debug_break_level = -1;
202#endif
203#ifdef FEAT_WINDOWS
204 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000205#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000206
Bram Moolenaar99685e62013-05-11 13:56:18 +0200207#ifdef FEAT_RUBY
208 {
209 int ruby_stack_start;
210 vim_ruby_init((void *)&ruby_stack_start);
211 }
212#endif
213
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000215 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000216#endif
217
218#ifdef MEM_PROFILE
219 atexit(vim_mem_profile_dump);
220#endif
221
222#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000223 for (i = 1; i < argc; ++i)
224 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000225 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000226 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000227 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000228 TIME_MSG("--- VIM STARTING ---");
229 break;
230 }
231 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000233 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000234
235#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000236 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000237#endif
238
239#ifdef FEAT_MBYTE
240 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
241#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000242#ifdef FEAT_EVAL
243 eval_init(); /* init global variables */
244#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000245
246#ifdef __QNXNTO__
247 qnx_init(); /* PhAttach() for clipboard, (and gui) */
248#endif
249
250#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000251 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000252 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000253 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000254 TIME_MSG("GUI prepared");
255#endif
256
257 /* Init the table of Normal mode commands. */
258 init_normal_cmds();
259
260#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000261 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262#endif
263
264 /*
265 * Allocate space for the generic buffers (needed for set_init_1() and
266 * EMSG2()).
267 */
268 if ((IObuff = alloc(IOSIZE)) == NULL
269 || (NameBuff = alloc(MAXPATHL)) == NULL)
270 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000271 TIME_MSG("Allocated generic buffers");
272
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000273#ifdef NBDEBUG
274 /* Wait a moment for debugging NetBeans. Must be after allocating
275 * NameBuff. */
276 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
277 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000278 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000279#endif
280
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000281#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
282 /*
283 * Setup to use the current locale (for ctype() and many other things).
284 * NOTE: Translated messages with encodings other than latin1 will not
285 * work until set_init_1() has been called!
286 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000287 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000288 TIME_MSG("locale set");
289#endif
290
291#ifdef FEAT_GUI
292 gui.dofork = TRUE; /* default is to use fork() */
293#endif
294
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000295 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000296 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000297 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000298 * --server...
299 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000300 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000301 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000302 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000303
304#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000305 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000306#endif
307#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000308 /* Prepare for possibly starting GUI sometime */
309 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000310 TIME_MSG("GUI prepared");
311#endif
312
313#ifdef FEAT_CLIPBOARD
314 clip_init(FALSE); /* Initialise clipboard stuff */
315 TIME_MSG("clipboard setup");
316#endif
317
318 /*
319 * Check if we have an interactive window.
320 * On the Amiga: If there is no window, we open one with a newcli command
321 * (needed for :! to * work). mch_check_win() will also handle the -d or
322 * -dev argument.
323 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000324 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000325 TIME_MSG("window checked");
326
327 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000328 * Allocate the first window and buffer.
329 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000330 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000331 if (win_alloc_first() == FAIL)
332 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000333
334 init_yank(); /* init yank buffers */
335
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000336 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200337 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000338
339 /*
340 * Set the default values for the options.
341 * NOTE: Non-latin1 translated messages are working only after this,
342 * because this is where "has_mbyte" will be set, which is used by
343 * msg_outtrans_len_attr().
344 * First find out the home directory, needed to expand "~" in options.
345 */
346 init_homedir(); /* find real value of $HOME */
347 set_init_1();
348 TIME_MSG("inits 1");
349
350#ifdef FEAT_EVAL
351 set_lang_var(); /* set v:lang and v:ctype */
352#endif
353
354#ifdef FEAT_CLIENTSERVER
355 /*
356 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000358 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000359 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360#endif
361
362 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000363 * Figure out the way to work from the command name argv[0].
364 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000366 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367
368 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000369 * Process the command line arguments. File names are put in the global
370 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000372 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000373 TIME_MSG("parsing arguments");
374
375 /*
376 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000377 * there is no terminal version, and on Windows we can't fork one off with
378 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379 */
380#ifdef ALWAYS_USE_GUI
381 gui.starting = TRUE;
382#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000383# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000384 /*
385 * Check if the GUI can be started. Reset gui.starting if not.
386 * Don't know about other systems, stay on the safe side and don't check.
387 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000388 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000389 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000390 if (gui_init_check() == FAIL)
391 {
392 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000393
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000394 /* When running "evim" or "gvim -y" we need the menus, exit if we
395 * don't have them. */
396 if (params.evim_mode)
397 mch_exit(1);
398 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000399 }
400# endif
401#endif
402
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000403 if (GARGCOUNT > 0)
404 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100405#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000406 /*
407 * Expand wildcards in file names.
408 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000409 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000410 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200411 start_dir = alloc(MAXPATHL);
412 if (start_dir != NULL)
413 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000414 /* Temporarily add '(' and ')' to 'isfname'. These are valid
415 * filename characters but are excluded from 'isfname' to make
416 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
417 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000418 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000419 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200420 if (start_dir != NULL)
421 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000422 }
423#endif
424 fname = alist_name(&GARGLIST[0]);
425 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000426
427#if defined(WIN32) && defined(FEAT_MBYTE)
428 {
429 extern void set_alist_count(void);
430
431 /* Remember the number of entries in the argument list. If it changes
432 * we don't react on setting 'encoding'. */
433 set_alist_count();
434 }
435#endif
436
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000437#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000438 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000439 {
440 /*
441 * If there is one filename, fully qualified, we have very probably
442 * been invoked from explorer, so change to the file's directory.
443 * Hint: to avoid this when typing a command use a forward slash.
444 * If the cd fails, it doesn't matter.
445 */
446 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200447 if (start_dir != NULL)
448 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000449 }
450#endif
451 TIME_MSG("expanding arguments");
452
453#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000454 if (params.diff_mode && params.window_count == -1)
455 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000456#endif
457
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000458 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000459 ++RedrawingDisabled;
460
461 /*
462 * When listing swap file names, don't do cursor positioning et. al.
463 */
464 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000465 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000466
467 /*
468 * When certain to start the GUI, don't check capabilities of terminal.
469 * For GTK we can't be sure, but when started from the desktop it doesn't
470 * make sense to try using a terminal.
471 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000472#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000473 if (gui.starting
474# ifdef FEAT_GUI_GTK
475 && !isatty(2)
476# endif
477 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000478 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000479#endif
480
481#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
482 /* When the GUI is started from Finder, need to display messages in a
483 * message box. isatty(2) returns TRUE anyway, thus we need to check the
484 * name to know we're not started from a terminal. */
485 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000486 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000487 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000488
489 /* Avoid always using "/" as the current directory. Note that when
490 * started from Finder the arglist will be filled later in
491 * HandleODocAE() and "fname" will be NULL. */
492 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
493 && STRCMP(NameBuff, "/") == 0)
494 {
495 if (fname != NULL)
496 (void)vim_chdirfile(fname);
497 else
498 {
499 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
500 vim_chdir(NameBuff);
501 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200502 if (start_dir != NULL)
503 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000504 }
505 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000506#endif
507
508 /*
509 * mch_init() sets up the terminal (window) for use. This must be
510 * done after resetting full_screen, otherwise it may move the cursor
511 * (MSDOS).
512 * Note that we may use mch_exit() before mch_init()!
513 */
514 mch_init();
515 TIME_MSG("shell init");
516
517#ifdef USE_XSMP
518 /*
519 * For want of anywhere else to do it, try to connect to xsmp here.
520 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
521 * Hijacking -X 'no X connection' to also disable XSMP connection as that
522 * has a similar delay upon failure.
523 * Only try if SESSION_MANAGER is set to something non-null.
524 */
525 if (!x_no_connect)
526 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000527 char *p = getenv("SESSION_MANAGER");
528
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000529 if (p != NULL && *p != NUL)
530 {
531 xsmp_init();
532 TIME_MSG("xsmp init");
533 }
534 }
535#endif
536
537 /*
538 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000540 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000541
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000542 /* This message comes before term inits, but after setting "silent_mode"
543 * when the input is not a tty. */
544 if (GARGCOUNT > 1 && !silent_mode)
545 printf(_("%d files to edit\n"), GARGCOUNT);
546
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000547 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000549 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000550 capabilities (will set full_screen) */
551 screen_start(); /* don't know where cursor is now */
552 TIME_MSG("Termcap init");
553 }
554
555 /*
556 * Set the default values for the options that use Rows and Columns.
557 */
558 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000559 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000560#ifdef FEAT_DIFF
561 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
562 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000563 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000564 diff_win_options(firstwin, FALSE);
565#endif
566
567 cmdline_row = Rows - p_ch;
568 msg_row = cmdline_row;
569 screenalloc(FALSE); /* allocate screen buffers */
570 set_init_2();
571 TIME_MSG("inits 2");
572
573 msg_scroll = TRUE;
574 no_wait_return = TRUE;
575
576 init_mappings(); /* set up initial mappings */
577
578 init_highlight(TRUE, FALSE); /* set the default highlight groups */
579 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000580
581#ifdef FEAT_EVAL
582 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000583 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000584#endif
585
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100586#ifdef FEAT_MZSCHEME
587 /*
588 * Newer version of MzScheme (Racket) require earlier (trampolined)
589 * initialisation via scheme_main_setup.
590 * Implement this by initialising it as early as possible
591 * and splitting off remaining Vim main into vim_main2
592 */
593 {
594 /* Pack up preprocessed command line arguments.
595 * It is safe because Scheme does not access argc/argv. */
596 char *args[2];
597 args[0] = (char *)fname;
598 args[1] = (char *)&params;
599 return mzscheme_main(2, args);
600 }
601}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100602#endif
603#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100604
Bram Moolenaar8866d272012-11-28 15:55:42 +0100605/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
606 * NO_VIM_MAIN is defined. */
607#ifdef FEAT_MZSCHEME
608 int
609vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100610{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100611# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100612 char_u *fname = (char_u *)argv[0];
613 mparm_T params;
614
615 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100616# else
617 return 0;
618}
619# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100620#endif
621
Bram Moolenaar8866d272012-11-28 15:55:42 +0100622#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000623 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000624 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000625
Bram Moolenaar58d98232005-07-23 22:25:46 +0000626 /* Source startup scripts. */
627 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000628
629#ifdef FEAT_EVAL
630 /*
631 * Read all the plugin files.
632 * Only when compiled with +eval, since most plugins need it.
633 */
634 if (p_lpl)
635 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000636# ifdef VMS /* Somehow VMS doesn't handle the "**". */
637 source_runtime((char_u *)"plugin/*.vim", TRUE);
638# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000639 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000640# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000641 TIME_MSG("loading plugins");
642 }
643#endif
644
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000645#ifdef FEAT_DIFF
646 /* Decide about window layout for diff mode after reading vimrc. */
647 if (params.diff_mode && params.window_layout == 0)
648 {
649 if (diffopt_horizontal())
650 params.window_layout = WIN_HOR; /* use horizontal split */
651 else
652 params.window_layout = WIN_VER; /* use vertical split */
653 }
654#endif
655
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000656 /*
657 * Recovery mode without a file name: List swap files.
658 * This uses the 'dir' option, therefore it must be after the
659 * initializations.
660 */
661 if (recoverymode && fname == NULL)
662 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200663 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000664 mch_exit(0);
665 }
666
667 /*
668 * Set a few option defaults after reading .vimrc files:
669 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
670 */
671 set_init_3();
672 TIME_MSG("inits 3");
673
674 /*
675 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
676 * Note that this overrides anything from a vimrc file.
677 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000678 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000679 p_uc = 0;
680
681#ifdef FEAT_FKMAP
682 if (curwin->w_p_rl && p_altkeymap)
683 {
684 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
685# ifdef FEAT_ARABIC
686 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
687# endif
688 p_fkmap = TRUE; /* Set the Farsi keymap mode */
689 }
690#endif
691
692#ifdef FEAT_GUI
693 if (gui.starting)
694 {
695#if defined(UNIX) || defined(VMS)
696 /* When something caused a message from a vimrc script, need to output
697 * an extra newline before the shell prompt. */
698 if (did_emsg || msg_didout)
699 putchar('\n');
700#endif
701
702 gui_start(); /* will set full_screen to TRUE */
703 TIME_MSG("starting GUI");
704
705 /* When running "evim" or "gvim -y" we need the menus, exit if we
706 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000707 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708 mch_exit(1);
709 }
710#endif
711
712#ifdef SPAWNO /* special MSDOS swapping library */
713 init_SPAWNO("", SWAP_ANY);
714#endif
715
716#ifdef FEAT_VIMINFO
717 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000718 * Read in registers, history etc, but not marks, from the viminfo file.
719 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000720 */
721 if (*p_viminfo != NUL)
722 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000723 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724 TIME_MSG("reading viminfo");
725 }
726#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100727#ifdef FEAT_EVAL
728 /* It's better to make v:oldfiles an empty list than NULL. */
729 if (get_vim_var_list(VV_OLDFILES) == NULL)
730 set_vim_var_list(VV_OLDFILES, list_alloc());
731#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000732
733#ifdef FEAT_QUICKFIX
734 /*
735 * "-q errorfile": Load the error file now.
736 * If the error file can't be read, exit before doing anything else.
737 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000738 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000739 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000740 if (params.use_ef != NULL)
741 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000742 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200743 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
744 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000745 {
746 out_char('\n');
747 mch_exit(3);
748 }
749 TIME_MSG("reading errorfile");
750 }
751#endif
752
753 /*
754 * Start putting things on the screen.
755 * Scroll screen down before drawing over it
756 * Clear screen now, so file message will not be cleared.
757 */
758 starting = NO_BUFFERS;
759 no_wait_return = FALSE;
760 if (!exmode_active)
761 msg_scroll = FALSE;
762
763#ifdef FEAT_GUI
764 /*
765 * This seems to be required to make callbacks to be called now, instead
766 * of after things have been put on the screen, which then may be deleted
767 * when getting a resize callback.
768 * For the Mac this handles putting files dropped on the Vim icon to
769 * global_alist.
770 */
771 if (gui.in_use)
772 {
773# ifdef FEAT_SUN_WORKSHOP
774 if (!usingSunWorkShop)
775# endif
776 gui_wait_for_chars(50L);
777 TIME_MSG("GUI delay");
778 }
779#endif
780
781#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
782 qnx_clip_init();
783#endif
784
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200785#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
786 clip_init(TRUE);
787#endif
788
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000789#ifdef FEAT_XCLIPBOARD
790 /* Start using the X clipboard, unless the GUI was started. */
791# ifdef FEAT_GUI
792 if (!gui.in_use)
793# endif
794 {
795 setup_term_clip();
796 TIME_MSG("setup clipboard");
797 }
798#endif
799
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000800#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000801 /* Prepare for being a Vim server. */
802 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000803#endif
804
805 /*
806 * If "-" argument given: Read file from stdin.
807 * Do this before starting Raw mode, because it may change things that the
808 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
809 * are the same terminal: "cat | vim -".
810 * Using autocommands here may cause trouble...
811 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000812 if (params.edit_type == EDIT_STDIN && !recoverymode)
813 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814
815#if defined(UNIX) || defined(VMS)
816 /* When switching screens and something caused a message from a vimrc
817 * script, need to output an extra newline on exit. */
818 if ((did_emsg || msg_didout) && *T_TI != NUL)
819 newline_on_exit = TRUE;
820#endif
821
822 /*
823 * When done something that is not allowed or error message call
824 * wait_return. This must be done before starttermcap(), because it may
825 * switch to another screen. It must be done after settmode(TMODE_RAW),
826 * because we want to react on a single key stroke.
827 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000828 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000829 */
830 settmode(TMODE_RAW);
831 TIME_MSG("setting raw mode");
832
833 if (need_wait_return || msg_didany)
834 {
835 wait_return(TRUE);
836 TIME_MSG("waiting for return");
837 }
838
839 starttermcap(); /* start termcap if not done by wait_return() */
840 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200841#if defined(FEAT_TERMRESPONSE)
842# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200843 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200844# endif
845 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100846#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000847
848#ifdef FEAT_MOUSE
849 setmouse(); /* may start using the mouse */
850#endif
851 if (scroll_region)
852 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000853 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000854
855 /*
856 * Don't clear the screen when starting in Ex mode, unless using the GUI.
857 */
858 if (exmode_active
859#ifdef FEAT_GUI
860 && !gui.in_use
861#endif
862 )
863 must_redraw = CLEAR;
864 else
865 {
866 screenclear(); /* clear screen */
867 TIME_MSG("clearing screen");
868 }
869
870#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000871 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000872 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100873 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200874 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000875 TIME_MSG("getting crypt key");
876 }
877#endif
878
879 no_wait_return = TRUE;
880
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000882 * Create the requested number of windows and edit buffers in them.
883 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000885 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000886 TIME_MSG("opening buffers");
887
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000888#ifdef FEAT_EVAL
889 /* clear v:swapcommand */
890 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
891#endif
892
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000893 /* Ex starts at last line of the file */
894 if (exmode_active)
895 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
896
897#ifdef FEAT_AUTOCMD
898 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
899 TIME_MSG("BufEnter autocommands");
900#endif
901 setpcmark();
902
903#ifdef FEAT_QUICKFIX
904 /*
905 * When started with "-q errorfile" jump to first error now.
906 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000907 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000908 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000909 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000910 TIME_MSG("jump to first error");
911 }
912#endif
913
914#ifdef FEAT_WINDOWS
915 /*
916 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000917 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000918 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200919 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000920#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200921 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000922
923#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000924 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000925 {
926 win_T *wp;
927
928 /* set options in each window for "vimdiff". */
929 for (wp = firstwin; wp != NULL; wp = wp->w_next)
930 diff_win_options(wp, TRUE);
931 }
932#endif
933
934 /*
935 * Shorten any of the filenames, but only when absolute.
936 */
937 shorten_fnames(FALSE);
938
939 /*
940 * Need to jump to the tag before executing the '-c command'.
941 * Makes "vim -c '/return' -t main" work.
942 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000943 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000944 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000945#if defined(HAS_SWAP_EXISTS_ACTION)
946 swap_exists_did_quit = FALSE;
947#endif
948
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000949 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000950 do_cmdline_cmd(IObuff);
951 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000952
953#if defined(HAS_SWAP_EXISTS_ACTION)
954 /* If the user doesn't want to edit the file then we quit here. */
955 if (swap_exists_did_quit)
956 getout(1);
957#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000958 }
959
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000960 /* Execute any "+", "-c" and "-S" arguments. */
961 if (params.n_commands > 0)
962 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000963
964 RedrawingDisabled = 0;
965 redraw_all_later(NOT_VALID);
966 no_wait_return = FALSE;
967 starting = 0;
968
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000969#ifdef FEAT_TERMRESPONSE
970 /* Requesting the termresponse is postponed until here, so that a "-c q"
971 * argument doesn't make it appear in the shell Vim was started from. */
972 may_req_termresponse();
973#endif
974
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000975 /* start in insert mode */
976 if (p_im)
977 need_start_insertmode = TRUE;
978
979#ifdef FEAT_AUTOCMD
980 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
981 TIME_MSG("VimEnter autocommands");
982#endif
983
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200984#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
985 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
986 * done after the clipboard is available and all initial commands that may
987 * modify the 'clipboard' setting have run; i.e. just before entering the
988 * main loop. */
989 {
990 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100991
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200992 adjust_clip_reg(&default_regname);
993 set_reg_var(default_regname);
994 }
995#endif
996
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000997#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
998 /* When a startup script or session file setup for diff'ing and
999 * scrollbind, sync the scrollbind now. */
1000 if (curwin->w_p_diff && curwin->w_p_scb)
1001 {
1002 update_topline();
1003 check_scrollbind((linenr_T)0, 0L);
1004 TIME_MSG("diff scrollbinding");
1005 }
1006#endif
1007
1008#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1009 mch_set_winsize_now(); /* Allow winsize changes from now on */
1010#endif
1011
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001012#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1013 /* When tab pages were created, may need to update the tab pages line and
1014 * scrollbars. This is skipped while creating them. */
1015 if (first_tabpage->tp_next != NULL)
1016 {
1017 out_flush();
1018 gui_init_which_components(NULL);
1019 gui_update_scrollbars(TRUE);
1020 }
1021 need_mouse_correct = TRUE;
1022#endif
1023
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001024 /* If ":startinsert" command used, stuff a dummy command to be able to
1025 * call normal_cmd(), which will then start Insert mode. */
1026 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001027 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001028
1029#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001030 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001031 {
1032# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001033# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001034 && !defined(FEAT_GUI_W32)
1035 if (gui.in_use)
1036 {
1037 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1038 mch_exit(2);
1039 }
1040# endif
1041# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001042 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001043 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001044 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001045#endif
1046
1047 TIME_MSG("before starting main loop");
1048
1049 /*
1050 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001051 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001053
1054 return 0;
1055}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001056#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001057#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001058
1059/*
1060 * Main loop: Execute Normal mode commands until exiting Vim.
1061 * Also used to handle commands in the command-line window, until the window
1062 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 * Also used to handle ":visual" command after ":global": execute Normal mode
1064 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001065 */
1066 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001067main_loop(
1068 int cmdwin, /* TRUE when working in the command-line window */
1069 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001070{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001071 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001072 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001073#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001074 /* these are static to avoid a compiler warning */
1075 static linenr_T conceal_old_cursor_line = 0;
1076 static linenr_T conceal_new_cursor_line = 0;
1077 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001078#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001079
1080#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1081 /* Setup to catch a terminating error from the X server. Just ignore
1082 * it, restore the state and continue. This might not always work
1083 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001084 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001086 {
1087 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001088 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001089 got_int = TRUE;
1090 need_wait_return = FALSE;
1091 global_busy = FALSE;
1092 exmode_active = 0;
1093 skip_redraw = FALSE;
1094 RedrawingDisabled = 0;
1095 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001096 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001097# ifdef FEAT_EVAL
1098 emsg_skip = 0;
1099# endif
1100 emsg_off = 0;
1101# ifdef FEAT_MOUSE
1102 setmouse();
1103# endif
1104 settmode(TMODE_RAW);
1105 starttermcap();
1106 scroll_start();
1107 redraw_later_clear();
1108 }
1109#endif
1110
1111 clear_oparg(&oa);
1112 while (!cmdwin
1113#ifdef FEAT_CMDWIN
1114 || cmdwin_result == 0
1115#endif
1116 )
1117 {
1118 if (stuff_empty())
1119 {
1120 did_check_timestamps = FALSE;
1121 if (need_check_timestamps)
1122 check_timestamps(FALSE);
1123 if (need_wait_return) /* if wait_return still needed ... */
1124 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001125 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001126 {
1127 need_start_insertmode = FALSE;
1128 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1129 /* skip the fileinfo message now, because it would be shown
1130 * after insert mode finishes! */
1131 need_fileinfo = FALSE;
1132 }
1133 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001134
1135 /* Reset "got_int" now that we got back to the main loop. Except when
1136 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1137 * the ":g" command.
1138 * For ":g/pat/vi" we reset "got_int" when used once. When used
1139 * a second time we go back to Ex mode and abort the ":g" command. */
1140 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001141 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001142 if (noexmode && global_busy && !exmode_active && previous_got_int)
1143 {
1144 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1145 * used and keep "got_int" set, so that it aborts ":g". */
1146 exmode_active = EXMODE_NORMAL;
1147 State = NORMAL;
1148 }
1149 else if (!global_busy || !exmode_active)
1150 {
1151 if (!quit_more)
1152 (void)vgetc(); /* flush all buffers */
1153 got_int = FALSE;
1154 }
1155 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001156 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001157 else
1158 previous_got_int = FALSE;
1159
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001160 if (!exmode_active)
1161 msg_scroll = FALSE;
1162 quit_more = FALSE;
1163
1164 /*
1165 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1166 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1167 * update cursor and redraw.
1168 */
1169 if (skip_redraw || exmode_active)
1170 skip_redraw = FALSE;
1171 else if (do_redraw || stuff_empty())
1172 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001173#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001174 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001175 if (!finish_op && (
1176# ifdef FEAT_AUTOCMD
1177 has_cursormoved()
1178# endif
1179# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1180 ||
1181# endif
1182# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001183 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001184# endif
1185 )
1186 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001187 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001188# ifdef FEAT_AUTOCMD
1189 if (has_cursormoved())
1190 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1191 FALSE, curbuf);
1192# endif
1193# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001194 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001195 {
1196 conceal_old_cursor_line = last_cursormoved.lnum;
1197 conceal_new_cursor_line = curwin->w_cursor.lnum;
1198 conceal_update_lines = TRUE;
1199 }
1200# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001201 last_cursormoved = curwin->w_cursor;
1202 }
1203#endif
1204
Bram Moolenaar186628f2013-03-19 13:33:23 +01001205#ifdef FEAT_AUTOCMD
1206 /* Trigger TextChanged if b_changedtick differs. */
1207 if (!finish_op && has_textchanged()
1208 && last_changedtick != curbuf->b_changedtick)
1209 {
1210 if (last_changedtick_buf == curbuf)
1211 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1212 FALSE, curbuf);
1213 last_changedtick_buf = curbuf;
1214 last_changedtick = curbuf->b_changedtick;
1215 }
1216#endif
1217
Bram Moolenaar33aec762006-01-22 23:30:12 +00001218#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1219 /* Scroll-binding for diff mode may have been postponed until
1220 * here. Avoids doing it for every change. */
1221 if (diff_need_scrollbind)
1222 {
1223 check_scrollbind((linenr_T)0, 0L);
1224 diff_need_scrollbind = FALSE;
1225 }
1226#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001227#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001228 /* Include a closed fold completely in the Visual area. */
1229 foldAdjustVisual();
1230#endif
1231#ifdef FEAT_FOLDING
1232 /*
1233 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1234 * contain the cursor.
1235 * When 'foldopen' is "all", open the fold(s) under the cursor.
1236 * This may mark the window for redrawing.
1237 */
1238 if (hasAnyFolding(curwin) && !char_avail())
1239 {
1240 foldCheckClose();
1241 if (fdo_flags & FDO_ALL)
1242 foldOpenCursor();
1243 }
1244#endif
1245
1246 /*
1247 * Before redrawing, make sure w_topline is correct, and w_leftcol
1248 * if lines don't wrap, and w_skipcol if lines wrap.
1249 */
1250 update_topline();
1251 validate_cursor();
1252
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001253 if (VIsual_active)
1254 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001255 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001256 update_screen(0);
1257 else if (redraw_cmdline || clear_cmdline)
1258 showmode();
1259#ifdef FEAT_WINDOWS
1260 redraw_statuslines();
1261#endif
1262#ifdef FEAT_TITLE
1263 if (need_maketitle)
1264 maketitle();
1265#endif
1266 /* display message after redraw */
1267 if (keep_msg != NULL)
1268 {
1269 char_u *p;
1270
1271 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001272 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1273 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001274 p = keep_msg;
1275 msg_attr(p, keep_msg_attr);
1276 vim_free(p);
1277 }
1278 if (need_fileinfo) /* show file info after redraw */
1279 {
1280 fileinfo(FALSE, TRUE, FALSE);
1281 need_fileinfo = FALSE;
1282 }
1283
1284 emsg_on_display = FALSE; /* can delete error message now */
1285 did_emsg = FALSE;
1286 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001287 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001288 showruler(FALSE);
1289
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001290# if defined(FEAT_CONCEAL)
1291 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001292 && (conceal_old_cursor_line != conceal_new_cursor_line
1293 || conceal_cursor_line(curwin)
1294 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001295 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001296 if (conceal_old_cursor_line != conceal_new_cursor_line
1297 && conceal_old_cursor_line
1298 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001299 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001300 update_single_line(curwin, conceal_new_cursor_line);
1301 curwin->w_valid &= ~VALID_CROW;
1302 }
1303# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001304 setcursor();
1305 cursor_on();
1306
1307 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001308
1309#ifdef STARTUPTIME
1310 /* Now that we have drawn the first screen all the startup stuff
1311 * has been done, close any file for startup messages. */
1312 if (time_fd != NULL)
1313 {
1314 TIME_MSG("first screen update");
1315 TIME_MSG("--- VIM STARTED ---");
1316 fclose(time_fd);
1317 time_fd = NULL;
1318 }
1319#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001320 }
1321#ifdef FEAT_GUI
1322 if (need_mouse_correct)
1323 gui_mouse_correct();
1324#endif
1325
1326 /*
1327 * Update w_curswant if w_set_curswant has been set.
1328 * Postponed until here to avoid computing w_virtcol too often.
1329 */
1330 update_curswant();
1331
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001332#ifdef FEAT_EVAL
1333 /*
1334 * May perform garbage collection when waiting for a character, but
1335 * only at the very toplevel. Otherwise we may be using a List or
1336 * Dict internally somewhere.
1337 * "may_garbage_collect" is reset in vgetc() which is invoked through
1338 * do_exmode() and normal_cmd().
1339 */
1340 may_garbage_collect = (!cmdwin && !noexmode);
1341#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 /*
1343 * If we're invoked as ex, do a round of ex commands.
1344 * Otherwise, get and execute a normal mode command.
1345 */
1346 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001347 {
1348 if (noexmode) /* End of ":global/path/visual" commands */
1349 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001350 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001351 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001352 else
1353 normal_cmd(&oa, TRUE);
1354 }
1355}
1356
1357
1358#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1359/*
1360 * Exit, but leave behind swap files for modified buffers.
1361 */
1362 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001363getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001365# if defined(SIGHUP) && defined(SIG_IGN)
1366 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1367 * makes Vim exit and then handling SIGHUP causes various reentrance
1368 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001369 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001370# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001371
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001372 ml_close_notmod(); /* close all not-modified buffers */
1373 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1374 ml_close_all(FALSE); /* close all memfiles, without deleting */
1375 getout(exitval); /* exit Vim properly */
1376}
1377#endif
1378
1379
1380/* Exit properly */
1381 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001382getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001383{
1384#ifdef FEAT_AUTOCMD
1385 buf_T *buf;
1386 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001387 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001388#endif
1389
1390 exiting = TRUE;
1391
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001392 /* When running in Ex mode an error causes us to exit with a non-zero exit
1393 * code. POSIX requires this, although it's not 100% clear from the
1394 * standard. */
1395 if (exmode_active)
1396 exitval += ex_exitval;
1397
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001398 /* Position the cursor on the last screen line, below all the text */
1399#ifdef FEAT_GUI
1400 if (!gui.in_use)
1401#endif
1402 windgoto((int)Rows - 1, 0);
1403
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001404#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1405 /* Optionally print hashtable efficiency. */
1406 hash_debug_results();
1407#endif
1408
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409#ifdef FEAT_GUI
1410 msg_didany = FALSE;
1411#endif
1412
1413#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001414 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001415 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001416 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1417# if defined FEAT_WINDOWS
1418 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001419 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001420 next_tp = tp->tp_next;
1421 for (wp = (tp == curtab)
1422 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001423 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001424 if (wp->w_buffer == NULL)
1425 /* Autocmd must have close the buffer already, skip. */
1426 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001427 buf = wp->w_buffer;
1428 if (buf->b_changedtick != -1)
1429 {
1430 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1431 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001432 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001433 /* start all over, autocommands may mess up the lists */
1434 next_tp = first_tabpage;
1435 break;
1436 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001437 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001438 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001439# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001440 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1441 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001442# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001443
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001444 /* Trigger BufUnload for buffers that are loaded */
1445 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1446 if (buf->b_ml.ml_mfp != NULL)
1447 {
1448 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001449 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001450 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1451 break;
1452 }
1453 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1454 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001455#endif
1456
1457#ifdef FEAT_VIMINFO
1458 if (*p_viminfo != NUL)
1459 /* Write out the registers, history, marks etc, to the viminfo file */
1460 write_viminfo(NULL, FALSE);
1461#endif
1462
1463#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001464 if (get_vim_var_nr(VV_DYING) <= 1)
1465 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001466#endif
1467
Bram Moolenaar05159a02005-02-26 23:04:13 +00001468#ifdef FEAT_PROFILE
1469 profile_dump();
1470#endif
1471
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001472 if (did_emsg
1473#ifdef FEAT_GUI
1474 || (gui.in_use && msg_didany && p_verbose > 0)
1475#endif
1476 )
1477 {
1478 /* give the user a chance to read the (error) message */
1479 no_wait_return = FALSE;
1480 wait_return(FALSE);
1481 }
1482
1483#ifdef FEAT_AUTOCMD
1484 /* Position the cursor again, the autocommands may have moved it */
1485# ifdef FEAT_GUI
1486 if (!gui.in_use)
1487# endif
1488 windgoto((int)Rows - 1, 0);
1489#endif
1490
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001491#ifdef FEAT_LUA
1492 lua_end();
1493#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001494#ifdef FEAT_MZSCHEME
1495 mzscheme_end();
1496#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001497#ifdef FEAT_TCL
1498 tcl_end();
1499#endif
1500#ifdef FEAT_RUBY
1501 ruby_end();
1502#endif
1503#ifdef FEAT_PYTHON
1504 python_end();
1505#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001506#ifdef FEAT_PYTHON3
1507 python3_end();
1508#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001509#ifdef FEAT_PERL
1510 perl_end();
1511#endif
1512#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1513 iconv_end();
1514#endif
1515#ifdef FEAT_NETBEANS_INTG
1516 netbeans_end();
1517#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001518#ifdef FEAT_CSCOPE
1519 cs_end();
1520#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001521#ifdef FEAT_EVAL
1522 if (garbage_collect_at_exit)
1523 garbage_collect();
1524#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001525#if defined(WIN32) && defined(FEAT_MBYTE)
1526 free_cmd_argsW();
1527#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001528
1529 mch_exit(exitval);
1530}
1531
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001532#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001533/*
1534 * Get a (optional) count for a Vim argument.
1535 */
1536 static int
1537get_number_arg(p, idx, def)
1538 char_u *p; /* pointer to argument */
1539 int *idx; /* index in argument, is incremented */
1540 int def; /* default value */
1541{
1542 if (vim_isdigit(p[*idx]))
1543 {
1544 def = atoi((char *)&(p[*idx]));
1545 while (vim_isdigit(p[*idx]))
1546 *idx = *idx + 1;
1547 }
1548 return def;
1549}
1550
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001551#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1552/*
1553 * Setup to use the current locale (for ctype() and many other things).
1554 */
1555 static void
1556init_locale()
1557{
1558 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001560# ifdef FEAT_GUI_GTK
1561 /* Tell Gtk not to change our locale settings. */
1562 gtk_disable_setlocale();
1563# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001564# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1565 /* Make sure strtod() uses a decimal point, not a comma. */
1566 setlocale(LC_NUMERIC, "C");
1567# endif
1568
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001569# ifdef WIN32
1570 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1571 * text while it's expecting text in the current locale. This call avoids
1572 * that. */
1573 setlocale(LC_CTYPE, "C");
1574# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001575
1576# ifdef FEAT_GETTEXT
1577 {
1578 int mustfree = FALSE;
1579 char_u *p;
1580
1581# ifdef DYNAMIC_GETTEXT
1582 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001583 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001584# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001585 /* expand_env() doesn't work yet, because g_chartab[] is not
1586 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001587 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1588 if (p != NULL && *p != NUL)
1589 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001590 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001591 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1592 }
1593 if (mustfree)
1594 vim_free(p);
1595 textdomain(VIMPACKAGE);
1596 }
1597# endif
1598}
1599#endif
1600
1601/*
1602 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1603 * If the executable name starts with "r" we disable shell commands.
1604 * If the next character is "e" we run in Easy mode.
1605 * If the next character is "g" we run the GUI version.
1606 * If the next characters are "view" we start in readonly mode.
1607 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1608 * If the next characters are "ex" we start in Ex mode. If it's followed
1609 * by "im" use improved Ex mode.
1610 */
1611 static void
1612parse_command_name(parmp)
1613 mparm_T *parmp;
1614{
1615 char_u *initstr;
1616
1617 initstr = gettail((char_u *)parmp->argv[0]);
1618
1619#ifdef MACOS_X_UNIX
1620 /* An issue has been seen when launching Vim in such a way that
1621 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1622 * executable or a symbolic link of it. Until this issue is resolved
1623 * we prohibit the GUI from being used.
1624 */
1625 if (STRCMP(initstr, parmp->argv[0]) == 0)
1626 disallow_gui = TRUE;
1627
1628 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001629 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001630#endif
1631
1632#ifdef FEAT_EVAL
1633 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001634 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001635#endif
1636
1637 if (TOLOWER_ASC(initstr[0]) == 'r')
1638 {
1639 restricted = TRUE;
1640 ++initstr;
1641 }
1642
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001643 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001644 if (TOLOWER_ASC(initstr[0]) == 'e'
1645 && (TOLOWER_ASC(initstr[1]) == 'v'
1646 || TOLOWER_ASC(initstr[1]) == 'g'))
1647 {
1648#ifdef FEAT_GUI
1649 gui.starting = TRUE;
1650#endif
1651 parmp->evim_mode = TRUE;
1652 ++initstr;
1653 }
1654
Bram Moolenaar806875d2008-09-18 18:57:10 +00001655 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1656 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001657 {
1658 main_start_gui();
1659#ifdef FEAT_GUI
1660 ++initstr;
1661#endif
1662 }
1663
1664 if (STRNICMP(initstr, "view", 4) == 0)
1665 {
1666 readonlymode = TRUE;
1667 curbuf->b_p_ro = TRUE;
1668 p_uc = 10000; /* don't update very often */
1669 initstr += 4;
1670 }
1671 else if (STRNICMP(initstr, "vim", 3) == 0)
1672 initstr += 3;
1673
1674 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1675 if (STRICMP(initstr, "diff") == 0)
1676 {
1677#ifdef FEAT_DIFF
1678 parmp->diff_mode = TRUE;
1679#else
1680 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1681 mch_errmsg("\n");
1682 mch_exit(2);
1683#endif
1684 }
1685
1686 if (STRNICMP(initstr, "ex", 2) == 0)
1687 {
1688 if (STRNICMP(initstr + 2, "im", 2) == 0)
1689 exmode_active = EXMODE_VIM;
1690 else
1691 exmode_active = EXMODE_NORMAL;
1692 change_compatible(TRUE); /* set 'compatible' */
1693 }
1694}
1695
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001696/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001697 * Get the name of the display, before gui_prepare() removes it from
1698 * argv[]. Used for the xterm-clipboard display.
1699 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001700 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001701 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001702 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001703early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001704 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001705{
Bram Moolenaar03005972008-11-20 13:12:36 +00001706#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1707 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001708 int argc = parmp->argc;
1709 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001710 int i;
1711
1712 for (i = 1; i < argc; i++)
1713 {
1714 if (STRCMP(argv[i], "--") == 0)
1715 break;
1716# ifdef FEAT_XCLIPBOARD
1717 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001718# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001719 || STRICMP(argv[i], "--display") == 0
1720# endif
1721 )
1722 {
1723 if (i == argc - 1)
1724 mainerr_arg_missing((char_u *)argv[i]);
1725 xterm_display = argv[++i];
1726 }
1727# endif
1728# ifdef FEAT_CLIENTSERVER
1729 else if (STRICMP(argv[i], "--servername") == 0)
1730 {
1731 if (i == argc - 1)
1732 mainerr_arg_missing((char_u *)argv[i]);
1733 parmp->serverName_arg = (char_u *)argv[++i];
1734 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001735 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001736 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001737 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001738 {
1739 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001740# ifdef FEAT_GUI
1741 if (strstr(argv[i], "-wait") != 0)
1742 /* don't fork() when starting the GUI to edit files ourself */
1743 gui.dofork = FALSE;
1744# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001745 }
1746# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001747
1748# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1749# ifdef FEAT_GUI_W32
1750 else if (STRICMP(argv[i], "--windowid") == 0)
1751# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001752 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001753# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001754 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001755 long_u id;
1756 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001757
1758 if (i == argc - 1)
1759 mainerr_arg_missing((char_u *)argv[i]);
1760 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001761 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001762 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001763 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001764 if (count != 1)
1765 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1766 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001767# ifdef FEAT_GUI_W32
1768 win_socket_id = id;
1769# else
1770 gtk_socket_id = id;
1771# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001772 i++;
1773 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001774# endif
1775# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001776 else if (STRICMP(argv[i], "--echo-wid") == 0)
1777 echo_wid_arg = TRUE;
1778# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001779# ifndef FEAT_NETBEANS_INTG
1780 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001781 {
1782 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1783 mch_exit(2);
1784 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001785# endif
1786
Bram Moolenaar58d98232005-07-23 22:25:46 +00001787 }
1788#endif
1789}
1790
1791/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001792 * Scan the command line arguments.
1793 */
1794 static void
1795command_line_scan(parmp)
1796 mparm_T *parmp;
1797{
1798 int argc = parmp->argc;
1799 char **argv = parmp->argv;
1800 int argv_idx; /* index in argv[n][] */
1801 int had_minmin = FALSE; /* found "--" argument */
1802 int want_argument; /* option argument with argument */
1803 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001804 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001805 long n;
1806
1807 --argc;
1808 ++argv;
1809 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1810 while (argc > 0)
1811 {
1812 /*
1813 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1814 */
1815 if (argv[0][0] == '+' && !had_minmin)
1816 {
1817 if (parmp->n_commands >= MAX_ARG_CMDS)
1818 mainerr(ME_EXTRA_CMD, NULL);
1819 argv_idx = -1; /* skip to next argument */
1820 if (argv[0][1] == NUL)
1821 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1822 else
1823 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1824 }
1825
1826 /*
1827 * Optional argument.
1828 */
1829 else if (argv[0][0] == '-' && !had_minmin)
1830 {
1831 want_argument = FALSE;
1832 c = argv[0][argv_idx++];
1833#ifdef VMS
1834 /*
1835 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1836 * and "-/X" as "-X".
1837 */
1838 if (c == '/')
1839 {
1840 c = argv[0][argv_idx++];
1841 c = TOUPPER_ASC(c);
1842 }
1843 else
1844 c = TOLOWER_ASC(c);
1845#endif
1846 switch (c)
1847 {
1848 case NUL: /* "vim -" read from stdin */
1849 /* "ex -" silent mode */
1850 if (exmode_active)
1851 silent_mode = TRUE;
1852 else
1853 {
1854 if (parmp->edit_type != EDIT_NONE)
1855 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1856 parmp->edit_type = EDIT_STDIN;
1857 read_cmd_fd = 2; /* read from stderr instead of stdin */
1858 }
1859 argv_idx = -1; /* skip to next argument */
1860 break;
1861
1862 case '-': /* "--" don't take any more option arguments */
1863 /* "--help" give help message */
1864 /* "--version" give version message */
1865 /* "--literal" take files literally */
1866 /* "--nofork" don't fork */
1867 /* "--noplugin[s]" skip plugins */
1868 /* "--cmd <cmd>" execute cmd before vimrc */
1869 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1870 usage();
1871 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1872 {
1873 Columns = 80; /* need to init Columns */
1874 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1875 list_version();
1876 msg_putchar('\n');
1877 msg_didout = FALSE;
1878 mch_exit(0);
1879 }
1880 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1881 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001882#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001883 parmp->literal = TRUE;
1884#endif
1885 }
1886 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1887 {
1888#ifdef FEAT_GUI
1889 gui.dofork = FALSE; /* don't fork() when starting GUI */
1890#endif
1891 }
1892 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1893 p_lpl = FALSE;
1894 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1895 {
1896 want_argument = TRUE;
1897 argv_idx += 3;
1898 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001899 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1900 {
1901 want_argument = TRUE;
1902 argv_idx += 11;
1903 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001904#ifdef FEAT_CLIENTSERVER
1905 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1906 ; /* already processed -- no arg */
1907 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1908 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1909 {
1910 /* already processed -- snatch the following arg */
1911 if (argc > 1)
1912 {
1913 --argc;
1914 ++argv;
1915 }
1916 }
1917#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001918#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1919# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001920 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001921# else
1922 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1923# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001924 {
1925 /* already processed -- snatch the following arg */
1926 if (argc > 1)
1927 {
1928 --argc;
1929 ++argv;
1930 }
1931 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001932#endif
1933#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001934 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1935 {
1936 /* already processed, skip */
1937 }
1938#endif
1939 else
1940 {
1941 if (argv[0][argv_idx])
1942 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1943 had_minmin = TRUE;
1944 }
1945 if (!want_argument)
1946 argv_idx = -1; /* skip to next argument */
1947 break;
1948
1949 case 'A': /* "-A" start in Arabic mode */
1950#ifdef FEAT_ARABIC
1951 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1952#else
1953 mch_errmsg(_(e_noarabic));
1954 mch_exit(2);
1955#endif
1956 break;
1957
1958 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001959 /* Needs to be effective before expanding file names, because
1960 * for Win32 this makes us edit a shortcut file itself,
1961 * instead of the file it links to. */
1962 set_options_bin(curbuf->b_p_bin, 1, 0);
1963 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001964 break;
1965
1966 case 'C': /* "-C" Compatible */
1967 change_compatible(TRUE);
1968 break;
1969
1970 case 'e': /* "-e" Ex mode */
1971 exmode_active = EXMODE_NORMAL;
1972 break;
1973
1974 case 'E': /* "-E" Improved Ex mode */
1975 exmode_active = EXMODE_VIM;
1976 break;
1977
1978 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1979 window directly, not with newcli */
1980#ifdef FEAT_GUI
1981 gui.dofork = FALSE; /* don't fork() when starting GUI */
1982#endif
1983 break;
1984
1985 case 'g': /* "-g" start GUI */
1986 main_start_gui();
1987 break;
1988
1989 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1990#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001991 p_fkmap = TRUE;
1992 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001993#else
1994 mch_errmsg(_(e_nofarsi));
1995 mch_exit(2);
1996#endif
1997 break;
1998
1999 case 'h': /* "-h" give help message */
2000#ifdef FEAT_GUI_GNOME
2001 /* Tell usage() to exit for "gvim". */
2002 gui.starting = FALSE;
2003#endif
2004 usage();
2005 break;
2006
2007 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2008#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002009 p_hkmap = TRUE;
2010 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002011#else
2012 mch_errmsg(_(e_nohebrew));
2013 mch_exit(2);
2014#endif
2015 break;
2016
2017 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2018#ifdef FEAT_LISP
2019 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2020 p_sm = TRUE;
2021#endif
2022 break;
2023
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002024 case 'M': /* "-M" no changes or writing of files */
2025 reset_modifiable();
2026 /* FALLTHROUGH */
2027
2028 case 'm': /* "-m" no writing of files */
2029 p_write = FALSE;
2030 break;
2031
2032 case 'y': /* "-y" easy mode */
2033#ifdef FEAT_GUI
2034 gui.starting = TRUE; /* start GUI a bit later */
2035#endif
2036 parmp->evim_mode = TRUE;
2037 break;
2038
2039 case 'N': /* "-N" Nocompatible */
2040 change_compatible(FALSE);
2041 break;
2042
2043 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002044#ifdef FEAT_NETBEANS_INTG
2045 /* checking for "-nb", netbeans parameters */
2046 if (argv[0][argv_idx] == 'b')
2047 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002048 netbeansArg = argv[0];
2049 argv_idx = -1; /* skip to next argument */
2050 }
2051 else
2052#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002053 parmp->no_swap_file = TRUE;
2054 break;
2055
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002056 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002057#ifdef TARGET_API_MAC_OSX
2058 /* For some reason on MacOS X, an argument like:
2059 -psn_0_10223617 is passed in when invoke from Finder
2060 or with the 'open' command */
2061 if (argv[0][argv_idx] == 's')
2062 {
2063 argv_idx = -1; /* bypass full -psn */
2064 main_start_gui();
2065 break;
2066 }
2067#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002068#ifdef FEAT_WINDOWS
2069 /* default is 0: open window for each file */
2070 parmp->window_count = get_number_arg((char_u *)argv[0],
2071 &argv_idx, 0);
2072 parmp->window_layout = WIN_TABS;
2073#endif
2074 break;
2075
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002076 case 'o': /* "-o[N]" open N horizontal split windows */
2077#ifdef FEAT_WINDOWS
2078 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002079 parmp->window_count = get_number_arg((char_u *)argv[0],
2080 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002081 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002082#endif
2083 break;
2084
2085 case 'O': /* "-O[N]" open N vertical split windows */
2086#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2087 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002088 parmp->window_count = get_number_arg((char_u *)argv[0],
2089 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002090 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002091#endif
2092 break;
2093
2094#ifdef FEAT_QUICKFIX
2095 case 'q': /* "-q" QuickFix mode */
2096 if (parmp->edit_type != EDIT_NONE)
2097 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2098 parmp->edit_type = EDIT_QF;
2099 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2100 {
2101 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2102 argv_idx = -1;
2103 }
2104 else if (argc > 1) /* "-q {errorfile}" */
2105 want_argument = TRUE;
2106 break;
2107#endif
2108
2109 case 'R': /* "-R" readonly mode */
2110 readonlymode = TRUE;
2111 curbuf->b_p_ro = TRUE;
2112 p_uc = 10000; /* don't update very often */
2113 break;
2114
2115 case 'r': /* "-r" recovery mode */
2116 case 'L': /* "-L" recovery mode */
2117 recoverymode = 1;
2118 break;
2119
2120 case 's':
2121 if (exmode_active) /* "-s" silent (batch) mode */
2122 silent_mode = TRUE;
2123 else /* "-s {scriptin}" read from script file */
2124 want_argument = TRUE;
2125 break;
2126
2127 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2128 if (parmp->edit_type != EDIT_NONE)
2129 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2130 parmp->edit_type = EDIT_TAG;
2131 if (argv[0][argv_idx]) /* "-t{tag}" */
2132 {
2133 parmp->tagname = (char_u *)argv[0] + argv_idx;
2134 argv_idx = -1;
2135 }
2136 else /* "-t {tag}" */
2137 want_argument = TRUE;
2138 break;
2139
2140#ifdef FEAT_EVAL
2141 case 'D': /* "-D" Debugging */
2142 parmp->use_debug_break_level = 9999;
2143 break;
2144#endif
2145#ifdef FEAT_DIFF
2146 case 'd': /* "-d" 'diff' */
2147# ifdef AMIGA
2148 /* check for "-dev {device}" */
2149 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2150 want_argument = TRUE;
2151 else
2152# endif
2153 parmp->diff_mode = TRUE;
2154 break;
2155#endif
2156 case 'V': /* "-V{N}" Verbose level */
2157 /* default is 10: a little bit verbose */
2158 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2159 if (argv[0][argv_idx] != NUL)
2160 {
2161 set_option_value((char_u *)"verbosefile", 0L,
2162 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002163 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002164 }
2165 break;
2166
2167 case 'v': /* "-v" Vi-mode (as if called "vi") */
2168 exmode_active = 0;
2169#ifdef FEAT_GUI
2170 gui.starting = FALSE; /* don't start GUI */
2171#endif
2172 break;
2173
2174 case 'w': /* "-w{number}" set window height */
2175 /* "-w {scriptout}" write to script */
2176 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2177 {
2178 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2179 set_option_value((char_u *)"window", n, NULL, 0);
2180 break;
2181 }
2182 want_argument = TRUE;
2183 break;
2184
2185#ifdef FEAT_CRYPT
2186 case 'x': /* "-x" encrypted reading/writing of files */
2187 parmp->ask_for_key = TRUE;
2188 break;
2189#endif
2190
2191 case 'X': /* "-X" don't connect to X server */
2192#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2193 x_no_connect = TRUE;
2194#endif
2195 break;
2196
2197 case 'Z': /* "-Z" restricted mode */
2198 restricted = TRUE;
2199 break;
2200
2201 case 'c': /* "-c{command}" or "-c {command}" execute
2202 command */
2203 if (argv[0][argv_idx] != NUL)
2204 {
2205 if (parmp->n_commands >= MAX_ARG_CMDS)
2206 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002207 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2208 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002209 argv_idx = -1;
2210 break;
2211 }
2212 /*FALLTHROUGH*/
2213 case 'S': /* "-S {file}" execute Vim script */
2214 case 'i': /* "-i {viminfo}" use for viminfo */
2215#ifndef FEAT_DIFF
2216 case 'd': /* "-d {device}" device (for Amiga) */
2217#endif
2218 case 'T': /* "-T {terminal}" terminal name */
2219 case 'u': /* "-u {vimrc}" vim inits file */
2220 case 'U': /* "-U {gvimrc}" gvim inits file */
2221 case 'W': /* "-W {scriptout}" overwrite */
2222#ifdef FEAT_GUI_W32
2223 case 'P': /* "-P {parent title}" MDI parent */
2224#endif
2225 want_argument = TRUE;
2226 break;
2227
2228 default:
2229 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2230 }
2231
2232 /*
2233 * Handle option arguments with argument.
2234 */
2235 if (want_argument)
2236 {
2237 /*
2238 * Check for garbage immediately after the option letter.
2239 */
2240 if (argv[0][argv_idx] != NUL)
2241 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2242
2243 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002244 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002245 mainerr_arg_missing((char_u *)argv[0]);
2246 ++argv;
2247 argv_idx = -1;
2248
2249 switch (c)
2250 {
2251 case 'c': /* "-c {command}" execute command */
2252 case 'S': /* "-S {file}" execute Vim script */
2253 if (parmp->n_commands >= MAX_ARG_CMDS)
2254 mainerr(ME_EXTRA_CMD, NULL);
2255 if (c == 'S')
2256 {
2257 char *a;
2258
2259 if (argc < 1)
2260 /* "-S" without argument: use default session file
2261 * name. */
2262 a = SESSION_FILE;
2263 else if (argv[0][0] == '-')
2264 {
2265 /* "-S" followed by another option: use default
2266 * session file name. */
2267 a = SESSION_FILE;
2268 ++argc;
2269 --argv;
2270 }
2271 else
2272 a = argv[0];
2273 p = alloc((unsigned)(STRLEN(a) + 4));
2274 if (p == NULL)
2275 mch_exit(2);
2276 sprintf((char *)p, "so %s", a);
2277 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2278 parmp->commands[parmp->n_commands++] = p;
2279 }
2280 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002281 parmp->commands[parmp->n_commands++] =
2282 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002283 break;
2284
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002285 case '-':
2286 if (argv[-1][2] == 'c')
2287 {
2288 /* "--cmd {command}" execute command */
2289 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2290 mainerr(ME_EXTRA_CMD, NULL);
2291 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002292 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002293 }
2294 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002295 break;
2296
2297 /* case 'd': -d {device} is handled in mch_check_win() for the
2298 * Amiga */
2299
2300#ifdef FEAT_QUICKFIX
2301 case 'q': /* "-q {errorfile}" QuickFix mode */
2302 parmp->use_ef = (char_u *)argv[0];
2303 break;
2304#endif
2305
2306 case 'i': /* "-i {viminfo}" use for viminfo */
2307 use_viminfo = (char_u *)argv[0];
2308 break;
2309
2310 case 's': /* "-s {scriptin}" read from script file */
2311 if (scriptin[0] != NULL)
2312 {
2313scripterror:
2314 mch_errmsg(_("Attempt to open script file again: \""));
2315 mch_errmsg(argv[-1]);
2316 mch_errmsg(" ");
2317 mch_errmsg(argv[0]);
2318 mch_errmsg("\"\n");
2319 mch_exit(2);
2320 }
2321 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2322 {
2323 mch_errmsg(_("Cannot open for reading: \""));
2324 mch_errmsg(argv[0]);
2325 mch_errmsg("\"\n");
2326 mch_exit(2);
2327 }
2328 if (save_typebuf() == FAIL)
2329 mch_exit(2); /* out of memory */
2330 break;
2331
2332 case 't': /* "-t {tag}" */
2333 parmp->tagname = (char_u *)argv[0];
2334 break;
2335
2336 case 'T': /* "-T {terminal}" terminal name */
2337 /*
2338 * The -T term argument is always available and when
2339 * HAVE_TERMLIB is supported it overrides the environment
2340 * variable TERM.
2341 */
2342#ifdef FEAT_GUI
2343 if (term_is_gui((char_u *)argv[0]))
2344 gui.starting = TRUE; /* start GUI a bit later */
2345 else
2346#endif
2347 parmp->term = (char_u *)argv[0];
2348 break;
2349
2350 case 'u': /* "-u {vimrc}" vim inits file */
2351 parmp->use_vimrc = (char_u *)argv[0];
2352 break;
2353
2354 case 'U': /* "-U {gvimrc}" gvim inits file */
2355#ifdef FEAT_GUI
2356 use_gvimrc = (char_u *)argv[0];
2357#endif
2358 break;
2359
2360 case 'w': /* "-w {nr}" 'window' value */
2361 /* "-w {scriptout}" append to script file */
2362 if (vim_isdigit(*((char_u *)argv[0])))
2363 {
2364 argv_idx = 0;
2365 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2366 set_option_value((char_u *)"window", n, NULL, 0);
2367 argv_idx = -1;
2368 break;
2369 }
2370 /*FALLTHROUGH*/
2371 case 'W': /* "-W {scriptout}" overwrite script file */
2372 if (scriptout != NULL)
2373 goto scripterror;
2374 if ((scriptout = mch_fopen(argv[0],
2375 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2376 {
2377 mch_errmsg(_("Cannot open for script output: \""));
2378 mch_errmsg(argv[0]);
2379 mch_errmsg("\"\n");
2380 mch_exit(2);
2381 }
2382 break;
2383
2384#ifdef FEAT_GUI_W32
2385 case 'P': /* "-P {parent title}" MDI parent */
2386 gui_mch_set_parent(argv[0]);
2387 break;
2388#endif
2389 }
2390 }
2391 }
2392
2393 /*
2394 * File name argument.
2395 */
2396 else
2397 {
2398 argv_idx = -1; /* skip to next argument */
2399
2400 /* Check for only one type of editing. */
2401 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2402 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2403 parmp->edit_type = EDIT_FILE;
2404
2405#ifdef MSWIN
2406 /* Remember if the argument was a full path before changing
2407 * slashes to backslashes. */
2408 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2409 parmp->full_path = TRUE;
2410#endif
2411
2412 /* Add the file to the global argument list. */
2413 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2414 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2415 mch_exit(2);
2416#ifdef FEAT_DIFF
2417 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2418 && !mch_isdir(alist_name(&GARGLIST[0])))
2419 {
2420 char_u *r;
2421
2422 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2423 if (r != NULL)
2424 {
2425 vim_free(p);
2426 p = r;
2427 }
2428 }
2429#endif
2430#if defined(__CYGWIN32__) && !defined(WIN32)
2431 /*
2432 * If vim is invoked by non-Cygwin tools, convert away any
2433 * DOS paths, so things like .swp files are created correctly.
2434 * Look for evidence of non-Cygwin paths before we bother.
2435 * This is only for when using the Unix files.
2436 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002437 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002438 {
2439 char posix_path[PATH_MAX];
2440
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002441# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2442 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2443# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002444 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002445# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002446 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002447 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002448 if (p == NULL)
2449 mch_exit(2);
2450 }
2451#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002452
2453#ifdef USE_FNAME_CASE
2454 /* Make the case of the file name match the actual file. */
2455 fname_case(p, 0);
2456#endif
2457
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002458 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002459#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002460 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002461#else
2462 2 /* add buffer number now and use curbuf */
2463#endif
2464 );
2465
2466#if defined(FEAT_MBYTE) && defined(WIN32)
2467 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002468 /* Remember this argument has been added to the argument list.
2469 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002470 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002471# ifdef FEAT_DIFF
2472 parmp->diff_mode
2473# else
2474 FALSE
2475# endif
2476 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002477 }
2478#endif
2479 }
2480
2481 /*
2482 * If there are no more letters after the current "-", go to next
2483 * argument. argv_idx is set to -1 when the current argument is to be
2484 * skipped.
2485 */
2486 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2487 {
2488 --argc;
2489 ++argv;
2490 argv_idx = 1;
2491 }
2492 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002493
2494#ifdef FEAT_EVAL
2495 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2496 * one. */
2497 if (parmp->n_commands > 0)
2498 {
2499 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2500 if (p != NULL)
2501 {
2502 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2503 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2504 vim_free(p);
2505 }
2506 }
2507#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508}
2509
2510/*
2511 * Print a warning if stdout is not a terminal.
2512 * When starting in Ex mode and commands come from a file, set Silent mode.
2513 */
2514 static void
2515check_tty(parmp)
2516 mparm_T *parmp;
2517{
2518 int input_isatty; /* is active input a terminal? */
2519
2520 input_isatty = mch_input_isatty();
2521 if (exmode_active)
2522 {
2523 if (!input_isatty)
2524 silent_mode = TRUE;
2525 }
2526 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2527#ifdef FEAT_GUI
2528 /* don't want the delay when started from the desktop */
2529 && !gui.starting
2530#endif
2531 )
2532 {
2533#ifdef NBDEBUG
2534 /*
2535 * This shouldn't be necessary. But if I run netbeans with the log
2536 * output coming to the console and XOpenDisplay fails, I get vim
2537 * trying to start with input/output to my console tty. This fills my
2538 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002539 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002540 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002541 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002542 {
2543 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2544 exit(1);
2545 }
2546#endif
2547 if (!parmp->stdout_isatty)
2548 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2549 if (!input_isatty)
2550 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2551 out_flush();
2552 if (scriptin[0] == NULL)
2553 ui_delay(2000L, TRUE);
2554 TIME_MSG("Warning delay");
2555 }
2556}
2557
2558/*
2559 * Read text from stdin.
2560 */
2561 static void
2562read_stdin()
2563{
2564 int i;
2565
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002566#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002567 /* When getting the ATTENTION prompt here, use a dialog */
2568 swap_exists_action = SEA_DIALOG;
2569#endif
2570 no_wait_return = TRUE;
2571 i = msg_didany;
2572 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002573 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002574 no_wait_return = FALSE;
2575 msg_didany = i;
2576 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002577#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002578 check_swap_exists_action();
2579#endif
2580#if !(defined(AMIGA) || defined(MACOS))
2581 /*
2582 * Close stdin and dup it from stderr. Required for GPM to work
2583 * properly, and for running external commands.
2584 * Is there any other system that cannot do this?
2585 */
2586 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002587 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002588#endif
2589}
2590
2591/*
2592 * Create the requested number of windows and edit buffers in them.
2593 * Also does recovery if "recoverymode" set.
2594 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 static void
2596create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002597 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002598{
2599#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002600 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002601 int done = 0;
2602
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002603 /*
2604 * Create the number of windows that was requested.
2605 */
2606 if (parmp->window_count == -1) /* was not set */
2607 parmp->window_count = 1;
2608 if (parmp->window_count == 0)
2609 parmp->window_count = GARGCOUNT;
2610 if (parmp->window_count > 1)
2611 {
2612 /* Don't change the windows if there was a command in .vimrc that
2613 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002614 if (parmp->window_layout == 0)
2615 parmp->window_layout = WIN_HOR;
2616 if (parmp->window_layout == WIN_TABS)
2617 {
2618 parmp->window_count = make_tabpages(parmp->window_count);
2619 TIME_MSG("making tab pages");
2620 }
2621 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002622 {
2623 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002624 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002625 TIME_MSG("making windows");
2626 }
2627 else
2628 parmp->window_count = win_count();
2629 }
2630 else
2631 parmp->window_count = 1;
2632#endif
2633
2634 if (recoverymode) /* do recover */
2635 {
2636 msg_scroll = TRUE; /* scroll message up */
2637 ml_recover();
2638 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2639 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002640 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002641 }
2642 else
2643 {
2644 /*
2645 * Open a buffer for windows that don't have one yet.
2646 * Commands in the .vimrc might have loaded a file or split the window.
2647 * Watch out for autocommands that delete a window.
2648 */
2649#ifdef FEAT_AUTOCMD
2650 /*
2651 * Don't execute Win/Buf Enter/Leave autocommands here
2652 */
2653 ++autocmd_no_enter;
2654 ++autocmd_no_leave;
2655#endif
2656#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002657 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002658 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002659 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002660 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002661 {
2662 if (parmp->window_layout == WIN_TABS)
2663 goto_tabpage(1);
2664 else
2665 curwin = firstwin;
2666 }
2667 else if (parmp->window_layout == WIN_TABS)
2668 {
2669 if (curtab->tp_next == NULL)
2670 break;
2671 goto_tabpage(0);
2672 }
2673 else
2674 {
2675 if (curwin->w_next == NULL)
2676 break;
2677 curwin = curwin->w_next;
2678 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002679 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002680#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002681 curbuf = curwin->w_buffer;
2682 if (curbuf->b_ml.ml_mfp == NULL)
2683 {
2684#ifdef FEAT_FOLDING
2685 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2686 if (p_fdls >= 0)
2687 curwin->w_p_fdl = p_fdls;
2688#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002689#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690 /* When getting the ATTENTION prompt here, use a dialog */
2691 swap_exists_action = SEA_DIALOG;
2692#endif
2693 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002694
2695 /* create memfile, read file */
2696 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002697
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002698#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002699 if (swap_exists_action == SEA_QUIT)
2700 {
2701 if (got_int || only_one_window())
2702 {
2703 /* abort selected or quit and only one window */
2704 did_emsg = FALSE; /* avoid hit-enter prompt */
2705 getout(1);
2706 }
2707 /* We can't close the window, it would disturb what
2708 * happens next. Clear the file name and set the arg
2709 * index to -1 to delete it later. */
2710 setfname(curbuf, NULL, NULL, FALSE);
2711 curwin->w_arg_idx = -1;
2712 swap_exists_action = SEA_NONE;
2713 }
2714 else
2715 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002716#endif
2717#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002718 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002719#endif
2720 }
2721#ifdef FEAT_WINDOWS
2722 ui_breakcheck();
2723 if (got_int)
2724 {
2725 (void)vgetc(); /* only break the file loading, not the rest */
2726 break;
2727 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002728 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002729#endif
2730#ifdef FEAT_WINDOWS
2731 if (parmp->window_layout == WIN_TABS)
2732 goto_tabpage(1);
2733 else
2734 curwin = firstwin;
2735 curbuf = curwin->w_buffer;
2736#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737#ifdef FEAT_AUTOCMD
2738 --autocmd_no_enter;
2739 --autocmd_no_leave;
2740#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002741 }
2742}
2743
2744#ifdef FEAT_WINDOWS
2745 /*
2746 * If opened more than one window, start editing files in the other
2747 * windows. make_windows() has already opened the windows.
2748 */
2749 static void
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002750edit_buffers(parmp, cwd)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002751 mparm_T *parmp;
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002752 char_u *cwd; /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002753{
2754 int arg_idx; /* index in argument list */
2755 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002756 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002757 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002758
2759# ifdef FEAT_AUTOCMD
2760 /*
2761 * Don't execute Win/Buf Enter/Leave autocommands here
2762 */
2763 ++autocmd_no_enter;
2764 ++autocmd_no_leave;
2765# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002766
2767 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2768 if (curwin->w_arg_idx == -1)
2769 {
2770 win_close(curwin, TRUE);
2771 advance = FALSE;
2772 }
2773
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002774 arg_idx = 1;
2775 for (i = 1; i < parmp->window_count; ++i)
2776 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002777 if (cwd != NULL)
2778 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002779 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2780 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002781 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002782 ++arg_idx;
2783 win_close(curwin, TRUE);
2784 advance = FALSE;
2785 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002786 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002787
Bram Moolenaar84212822006-11-07 21:59:47 +00002788 if (advance)
2789 {
2790 if (parmp->window_layout == WIN_TABS)
2791 {
2792 if (curtab->tp_next == NULL) /* just checking */
2793 break;
2794 goto_tabpage(0);
2795 }
2796 else
2797 {
2798 if (curwin->w_next == NULL) /* just checking */
2799 break;
2800 win_enter(curwin->w_next, FALSE);
2801 }
2802 }
2803 advance = TRUE;
2804
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002805 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002806 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002807 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2808 {
2809 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002810 /* Edit file from arg list, if there is one. When "Quit" selected
2811 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002812# ifdef HAS_SWAP_EXISTS_ACTION
2813 swap_exists_did_quit = FALSE;
2814# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002815 (void)do_ecmd(0, arg_idx < GARGCOUNT
2816 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002817 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002818# ifdef HAS_SWAP_EXISTS_ACTION
2819 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002820 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002821 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002822 if (got_int || only_one_window())
2823 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002824 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002825 did_emsg = FALSE; /* avoid hit-enter prompt */
2826 getout(1);
2827 }
2828 win_close(curwin, TRUE);
2829 advance = FALSE;
2830 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002831# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002832 if (arg_idx == GARGCOUNT - 1)
2833 arg_had_last = TRUE;
2834 ++arg_idx;
2835 }
2836 ui_breakcheck();
2837 if (got_int)
2838 {
2839 (void)vgetc(); /* only break the file loading, not the rest */
2840 break;
2841 }
2842 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002843
2844 if (parmp->window_layout == WIN_TABS)
2845 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002846# ifdef FEAT_AUTOCMD
2847 --autocmd_no_enter;
2848# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002849
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002850 /* make the first window the current window */
2851 win = firstwin;
2852#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2853 /* Avoid making a preview window the current window. */
2854 while (win->w_p_pvw)
2855 {
2856 win = win->w_next;
2857 if (win == NULL)
2858 {
2859 win = firstwin;
2860 break;
2861 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002862 }
2863#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002864 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002865
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002866# ifdef FEAT_AUTOCMD
2867 --autocmd_no_leave;
2868# endif
2869 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002870 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2872}
2873#endif /* FEAT_WINDOWS */
2874
2875/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002876 * Execute the commands from --cmd arguments "cmds[cnt]".
2877 */
2878 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002879exe_pre_commands(parmp)
2880 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002881{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002882 char_u **cmds = parmp->pre_commands;
2883 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002884 int i;
2885
2886 if (cnt > 0)
2887 {
2888 curwin->w_cursor.lnum = 0; /* just in case.. */
2889 sourcing_name = (char_u *)_("pre-vimrc command line");
2890# ifdef FEAT_EVAL
2891 current_SID = SID_CMDARG;
2892# endif
2893 for (i = 0; i < cnt; ++i)
2894 do_cmdline_cmd(cmds[i]);
2895 sourcing_name = NULL;
2896# ifdef FEAT_EVAL
2897 current_SID = 0;
2898# endif
2899 TIME_MSG("--cmd commands");
2900 }
2901}
2902
2903/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002904 * Execute "+", "-c" and "-S" arguments.
2905 */
2906 static void
2907exe_commands(parmp)
2908 mparm_T *parmp;
2909{
2910 int i;
2911
2912 /*
2913 * We start commands on line 0, make "vim +/pat file" match a
2914 * pattern on line 1. But don't move the cursor when an autocommand
2915 * with g`" was used.
2916 */
2917 msg_scroll = TRUE;
2918 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2919 curwin->w_cursor.lnum = 0;
2920 sourcing_name = (char_u *)"command line";
2921#ifdef FEAT_EVAL
2922 current_SID = SID_CARG;
2923#endif
2924 for (i = 0; i < parmp->n_commands; ++i)
2925 {
2926 do_cmdline_cmd(parmp->commands[i]);
2927 if (parmp->cmds_tofree[i])
2928 vim_free(parmp->commands[i]);
2929 }
2930 sourcing_name = NULL;
2931#ifdef FEAT_EVAL
2932 current_SID = 0;
2933#endif
2934 if (curwin->w_cursor.lnum == 0)
2935 curwin->w_cursor.lnum = 1;
2936
2937 if (!exmode_active)
2938 msg_scroll = FALSE;
2939
2940#ifdef FEAT_QUICKFIX
2941 /* When started with "-q errorfile" jump to first error again. */
2942 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002943 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002944#endif
2945 TIME_MSG("executing command arguments");
2946}
2947
2948/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002949 * Source startup scripts.
2950 */
2951 static void
2952source_startup_scripts(parmp)
2953 mparm_T *parmp;
2954{
2955 int i;
2956
2957 /*
2958 * For "evim" source evim.vim first of all, so that the user can overrule
2959 * any things he doesn't like.
2960 */
2961 if (parmp->evim_mode)
2962 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002963 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002964 TIME_MSG("source evim file");
2965 }
2966
2967 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002968 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002969 * nothing else.
2970 */
2971 if (parmp->use_vimrc != NULL)
2972 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002973 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2974 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002975 {
2976#ifdef FEAT_GUI
2977 if (use_gvimrc == NULL) /* don't load gvimrc either */
2978 use_gvimrc = parmp->use_vimrc;
2979#endif
2980 if (parmp->use_vimrc[2] == 'N')
2981 p_lpl = FALSE; /* don't load plugins either */
2982 }
2983 else
2984 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002985 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002986 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2987 }
2988 }
2989 else if (!silent_mode)
2990 {
2991#ifdef AMIGA
2992 struct Process *proc = (struct Process *)FindTask(0L);
2993 APTR save_winptr = proc->pr_WindowPtr;
2994
2995 /* Avoid a requester here for a volume that doesn't exist. */
2996 proc->pr_WindowPtr = (APTR)-1L;
2997#endif
2998
2999 /*
3000 * Get system wide defaults, if the file name is defined.
3001 */
3002#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003003 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003005#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003006 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003007#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003008
3009 /*
3010 * Try to read initialization commands from the following places:
3011 * - environment variable VIMINIT
3012 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3013 * - second user vimrc file ($VIM/.vimrc for Dos)
3014 * - environment variable EXINIT
3015 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3016 * - second user exrc file ($VIM/.exrc for Dos)
3017 * The first that exists is used, the rest is ignored.
3018 */
3019 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3020 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003021 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003022#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003023 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3024 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003025#endif
3026#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003027 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3028 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003029#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003030#ifdef USR_VIMRC_FILE4
3031 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3032 DOSO_VIMRC) == FAIL
3033#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003034 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003035 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003036 {
3037#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003038 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003039#endif
3040 }
3041 }
3042
3043 /*
3044 * Read initialization commands from ".vimrc" or ".exrc" in current
3045 * directory. This is only done if the 'exrc' option is set.
3046 * Because of security reasons we disallow shell and write commands
3047 * now, except for unix if the file is owned by the user or 'secure'
3048 * option has been reset in environment of global ".exrc" or ".vimrc".
3049 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3050 * SYS_VIMRC_FILE.
3051 */
3052 if (p_exrc)
3053 {
3054#if defined(UNIX) || defined(VMS)
3055 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3056 if (!file_owned(VIMRC_FILE))
3057#endif
3058 secure = p_secure;
3059
3060 i = FAIL;
3061 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3062 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3063#ifdef USR_VIMRC_FILE2
3064 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3065 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3066#endif
3067#ifdef USR_VIMRC_FILE3
3068 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3069 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3070#endif
3071#ifdef SYS_VIMRC_FILE
3072 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3073 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3074#endif
3075 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003076 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003077
3078 if (i == FAIL)
3079 {
3080#if defined(UNIX) || defined(VMS)
3081 /* if ".exrc" is not owned by user set 'secure' mode */
3082 if (!file_owned(EXRC_FILE))
3083 secure = p_secure;
3084 else
3085 secure = 0;
3086#endif
3087 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3088 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3089#ifdef USR_EXRC_FILE2
3090 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3091 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3092#endif
3093 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003094 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003095 }
3096 }
3097 if (secure == 2)
3098 need_wait_return = TRUE;
3099 secure = 0;
3100#ifdef AMIGA
3101 proc->pr_WindowPtr = save_winptr;
3102#endif
3103 }
3104 TIME_MSG("sourcing vimrc file(s)");
3105}
3106
3107/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003108 * Setup to start using the GUI. Exit with an error when not available.
3109 */
3110 static void
3111main_start_gui()
3112{
3113#ifdef FEAT_GUI
3114 gui.starting = TRUE; /* start GUI a bit later */
3115#else
3116 mch_errmsg(_(e_nogvim));
3117 mch_errmsg("\n");
3118 mch_exit(2);
3119#endif
3120}
3121
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003122#endif /* NO_VIM_MAIN */
3123
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003124/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003125 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003126 * Returns FAIL if the environment variable was not executed, OK otherwise.
3127 */
3128 int
3129process_env(env, is_viminit)
3130 char_u *env;
3131 int is_viminit; /* when TRUE, called for VIMINIT */
3132{
3133 char_u *initstr;
3134 char_u *save_sourcing_name;
3135 linenr_T save_sourcing_lnum;
3136#ifdef FEAT_EVAL
3137 scid_T save_sid;
3138#endif
3139
3140 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3141 {
3142 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003143 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003144 save_sourcing_name = sourcing_name;
3145 save_sourcing_lnum = sourcing_lnum;
3146 sourcing_name = env;
3147 sourcing_lnum = 0;
3148#ifdef FEAT_EVAL
3149 save_sid = current_SID;
3150 current_SID = SID_ENV;
3151#endif
3152 do_cmdline_cmd(initstr);
3153 sourcing_name = save_sourcing_name;
3154 sourcing_lnum = save_sourcing_lnum;
3155#ifdef FEAT_EVAL
3156 current_SID = save_sid;;
3157#endif
3158 return OK;
3159 }
3160 return FAIL;
3161}
3162
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003163#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003164/*
3165 * Return TRUE if we are certain the user owns the file "fname".
3166 * Used for ".vimrc" and ".exrc".
3167 * Use both stat() and lstat() for extra security.
3168 */
3169 static int
3170file_owned(fname)
3171 char *fname;
3172{
3173 struct stat s;
3174# ifdef UNIX
3175 uid_t uid = getuid();
3176# else /* VMS */
3177 uid_t uid = ((getgid() << 16) | getuid());
3178# endif
3179
3180 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3181# ifdef HAVE_LSTAT
3182 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3183# endif
3184 );
3185}
3186#endif
3187
3188/*
3189 * Give an error message main_errors["n"] and exit.
3190 */
3191 static void
3192mainerr(n, str)
3193 int n; /* one of the ME_ defines */
3194 char_u *str; /* extra argument or NULL */
3195{
3196#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3197 reset_signals(); /* kill us with CTRL-C here, if you like */
3198#endif
3199
3200 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003201 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202 mch_errmsg(_(main_errors[n]));
3203 if (str != NULL)
3204 {
3205 mch_errmsg(": \"");
3206 mch_errmsg((char *)str);
3207 mch_errmsg("\"");
3208 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003209 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003210
3211 mch_exit(1);
3212}
3213
3214 void
3215mainerr_arg_missing(str)
3216 char_u *str;
3217{
3218 mainerr(ME_ARG_MISSING, str);
3219}
3220
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003221#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003222/*
3223 * print a message with three spaces prepended and '\n' appended.
3224 */
3225 static void
3226main_msg(s)
3227 char *s;
3228{
3229 mch_msg(" ");
3230 mch_msg(s);
3231 mch_msg("\n");
3232}
3233
3234/*
3235 * Print messages for "vim -h" or "vim --help" and exit.
3236 */
3237 static void
3238usage()
3239{
3240 int i;
3241 static char *(use[]) =
3242 {
3243 N_("[file ..] edit specified file(s)"),
3244 N_("- read text from stdin"),
3245 N_("-t tag edit file where tag is defined"),
3246#ifdef FEAT_QUICKFIX
3247 N_("-q [errorfile] edit file with first error")
3248#endif
3249 };
3250
3251#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3252 reset_signals(); /* kill us with CTRL-C here, if you like */
3253#endif
3254
3255 mch_msg(longVersion);
3256 mch_msg(_("\n\nusage:"));
3257 for (i = 0; ; ++i)
3258 {
3259 mch_msg(_(" vim [arguments] "));
3260 mch_msg(_(use[i]));
3261 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3262 break;
3263 mch_msg(_("\n or:"));
3264 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003265#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003266 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003267#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003268
3269 mch_msg(_("\n\nArguments:\n"));
3270 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003271#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003272 main_msg(_("--literal\t\tDon't expand wildcards"));
3273#endif
3274#ifdef FEAT_OLE
3275 main_msg(_("-register\t\tRegister this gvim for OLE"));
3276 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3277#endif
3278#ifdef FEAT_GUI
3279 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3280 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3281#endif
3282 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3283 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003284 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003285 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3286#ifdef FEAT_DIFF
3287 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3288#endif
3289 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3290 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3291 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3292 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3293 main_msg(_("-M\t\t\tModifications in text not allowed"));
3294 main_msg(_("-b\t\t\tBinary mode"));
3295#ifdef FEAT_LISP
3296 main_msg(_("-l\t\t\tLisp mode"));
3297#endif
3298 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3299 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003300 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3301#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003302 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003303#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003304 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3305 main_msg(_("-r\t\t\tList swap files and exit"));
3306 main_msg(_("-r (with file name)\tRecover crashed session"));
3307 main_msg(_("-L\t\t\tSame as -r"));
3308#ifdef AMIGA
3309 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3310 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3311#endif
3312#ifdef FEAT_ARABIC
3313 main_msg(_("-A\t\t\tstart in Arabic mode"));
3314#endif
3315#ifdef FEAT_RIGHTLEFT
3316 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3317#endif
3318#ifdef FEAT_FKMAP
3319 main_msg(_("-F\t\t\tStart in Farsi mode"));
3320#endif
3321 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3322 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3323#ifdef FEAT_GUI
3324 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3325#endif
3326 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003327#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003328 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003329 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3330 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003331#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003332 main_msg(_("+\t\t\tStart at end of file"));
3333 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003334 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003335 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3336 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3337 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3338 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3339 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3340#ifdef FEAT_CRYPT
3341 main_msg(_("-x\t\t\tEdit encrypted files"));
3342#endif
3343#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3344# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3345 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3346# endif
3347 main_msg(_("-X\t\t\tDo not connect to X server"));
3348#endif
3349#ifdef FEAT_CLIENTSERVER
3350 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3351 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3352 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3353 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003354# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003355 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003356# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003357 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3358 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3359 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3360 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3361#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003362#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003363 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003364#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003365#ifdef FEAT_VIMINFO
3366 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3367#endif
3368 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3369 main_msg(_("--version\t\tPrint version information and exit"));
3370
3371#ifdef FEAT_GUI_X11
3372# ifdef FEAT_GUI_MOTIF
3373 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3374# else
3375# ifdef FEAT_GUI_ATHENA
3376# ifdef FEAT_GUI_NEXTAW
3377 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3378# else
3379 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3380# endif
3381# endif
3382# endif
3383 main_msg(_("-display <display>\tRun vim on <display>"));
3384 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003385 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3386 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3387 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3388 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3389 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3390 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3391 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3392 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3393# ifdef FEAT_GUI_ATHENA
3394 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3395# endif
3396 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3397 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3398 main_msg(_("-xrm <resource>\tSet the specified resource"));
3399#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400#ifdef FEAT_GUI_GTK
3401 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3402 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3403 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3404 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3405 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003407 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003408 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003409#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003410#ifdef FEAT_GUI_W32
3411 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003412 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413#endif
3414
3415#ifdef FEAT_GUI_GNOME
3416 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3417 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003418 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003419 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003420 gui.dofork = FALSE;
3421 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003422 else
3423#endif
3424 mch_exit(0);
3425}
3426
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003427#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428/*
3429 * Check the result of the ATTENTION dialog:
3430 * When "Quit" selected, exit Vim.
3431 * When "Recover" selected, recover the file.
3432 */
3433 static void
3434check_swap_exists_action()
3435{
3436 if (swap_exists_action == SEA_QUIT)
3437 getout(1);
3438 handle_swap_exists(NULL);
3439}
3440#endif
3441
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003442#endif
3443
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003444#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003445static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003446
3447static struct timeval prev_timeval;
3448
Bram Moolenaar3f269672009-11-03 11:11:11 +00003449# ifdef WIN3264
3450/*
3451 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3452 */
3453 static int
3454gettimeofday(struct timeval *tv, char *dummy)
3455{
3456 long t = clock();
3457 tv->tv_sec = t / CLOCKS_PER_SEC;
3458 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3459 return 0;
3460}
3461# endif
3462
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003463/*
3464 * Save the previous time before doing something that could nest.
3465 * set "*tv_rel" to the time elapsed so far.
3466 */
3467 void
3468time_push(tv_rel, tv_start)
3469 void *tv_rel, *tv_start;
3470{
3471 *((struct timeval *)tv_rel) = prev_timeval;
3472 gettimeofday(&prev_timeval, NULL);
3473 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3474 - ((struct timeval *)tv_rel)->tv_usec;
3475 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3476 - ((struct timeval *)tv_rel)->tv_sec;
3477 if (((struct timeval *)tv_rel)->tv_usec < 0)
3478 {
3479 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3480 --((struct timeval *)tv_rel)->tv_sec;
3481 }
3482 *(struct timeval *)tv_start = prev_timeval;
3483}
3484
3485/*
3486 * Compute the previous time after doing something that could nest.
3487 * Subtract "*tp" from prev_timeval;
3488 * Note: The arguments are (void *) to avoid trouble with systems that don't
3489 * have struct timeval.
3490 */
3491 void
3492time_pop(tp)
3493 void *tp; /* actually (struct timeval *) */
3494{
3495 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3496 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3497 if (prev_timeval.tv_usec < 0)
3498 {
3499 prev_timeval.tv_usec += 1000000;
3500 --prev_timeval.tv_sec;
3501 }
3502}
3503
3504 static void
3505time_diff(then, now)
3506 struct timeval *then;
3507 struct timeval *now;
3508{
3509 long usec;
3510 long msec;
3511
3512 usec = now->tv_usec - then->tv_usec;
3513 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3514 usec = usec % 1000L;
3515 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3516}
3517
3518 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003519time_msg(mesg, tv_start)
3520 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003521 void *tv_start; /* only for do_source: start time; actually
3522 (struct timeval *) */
3523{
3524 static struct timeval start;
3525 struct timeval now;
3526
3527 if (time_fd != NULL)
3528 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003529 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003530 {
3531 gettimeofday(&start, NULL);
3532 prev_timeval = start;
3533 fprintf(time_fd, "\n\ntimes in msec\n");
3534 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3535 fprintf(time_fd, " clock elapsed: other lines\n\n");
3536 }
3537 gettimeofday(&now, NULL);
3538 time_diff(&start, &now);
3539 if (((struct timeval *)tv_start) != NULL)
3540 {
3541 fprintf(time_fd, " ");
3542 time_diff(((struct timeval *)tv_start), &now);
3543 }
3544 fprintf(time_fd, " ");
3545 time_diff(&prev_timeval, &now);
3546 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003547 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003548 }
3549}
3550
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003551#endif
3552
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003553#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003554
3555/*
3556 * Common code for the X command server and the Win32 command server.
3557 */
3558
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003559static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003560
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003561/*
3562 * Do the client-server stuff, unless "--servername ''" was used.
3563 */
3564 static void
3565exec_on_server(parmp)
3566 mparm_T *parmp;
3567{
3568 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3569 {
3570# ifdef WIN32
3571 /* Initialise the client/server messaging infrastructure. */
3572 serverInitMessaging();
3573# endif
3574
3575 /*
3576 * When a command server argument was found, execute it. This may
3577 * exit Vim when it was successful. Otherwise it's executed further
3578 * on. Remember the encoding used here in "serverStrEnc".
3579 */
3580 if (parmp->serverArg)
3581 {
3582 cmdsrv_main(&parmp->argc, parmp->argv,
3583 parmp->serverName_arg, &parmp->serverStr);
3584# ifdef FEAT_MBYTE
3585 parmp->serverStrEnc = vim_strsave(p_enc);
3586# endif
3587 }
3588
3589 /* If we're still running, get the name to register ourselves.
3590 * On Win32 can register right now, for X11 need to setup the
3591 * clipboard first, it's further down. */
3592 parmp->servername = serverMakeName(parmp->serverName_arg,
3593 parmp->argv[0]);
3594# ifdef WIN32
3595 if (parmp->servername != NULL)
3596 {
3597 serverSetName(parmp->servername);
3598 vim_free(parmp->servername);
3599 }
3600# endif
3601 }
3602}
3603
3604/*
3605 * Prepare for running as a Vim server.
3606 */
3607 static void
3608prepare_server(parmp)
3609 mparm_T *parmp;
3610{
3611# if defined(FEAT_X11)
3612 /*
3613 * Register for remote command execution with :serversend and --remote
3614 * unless there was a -X or a --servername '' on the command line.
3615 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003616 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003617 */
3618 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3619# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003620 (gui.in_use
3621# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003622 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003623# endif
3624 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003625# endif
3626 parmp->serverName_arg != NULL))
3627 {
3628 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3629 vim_free(parmp->servername);
3630 TIME_MSG("register server name");
3631 }
3632 else
3633 serverDelayedStartName = parmp->servername;
3634# endif
3635
3636 /*
3637 * Execute command ourselves if we're here because the send failed (or
3638 * else we would have exited above).
3639 */
3640 if (parmp->serverStr != NULL)
3641 {
3642 char_u *p;
3643
3644 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3645 parmp->serverStr, &p));
3646 vim_free(p);
3647 }
3648}
3649
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003650 static void
3651cmdsrv_main(argc, argv, serverName_arg, serverStr)
3652 int *argc;
3653 char **argv;
3654 char_u *serverName_arg;
3655 char_u **serverStr;
3656{
3657 char_u *res;
3658 int i;
3659 char_u *sname;
3660 int ret;
3661 int didone = FALSE;
3662 int exiterr = 0;
3663 char **newArgV = argv + 1;
3664 int newArgC = 1,
3665 Argc = *argc;
3666 int argtype;
3667#define ARGTYPE_OTHER 0
3668#define ARGTYPE_EDIT 1
3669#define ARGTYPE_EDIT_WAIT 2
3670#define ARGTYPE_SEND 3
3671 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003672 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003673# ifndef FEAT_X11
3674 HWND srv;
3675# else
3676 Window srv;
3677
3678 setup_term_clip();
3679# endif
3680
3681 sname = serverMakeName(serverName_arg, argv[0]);
3682 if (sname == NULL)
3683 return;
3684
3685 /*
3686 * Execute the command server related arguments and remove them
3687 * from the argc/argv array; We may have to return into main()
3688 */
3689 for (i = 1; i < Argc; i++)
3690 {
3691 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003692 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003693 {
3694 for (; i < *argc; i++)
3695 {
3696 *newArgV++ = argv[i];
3697 newArgC++;
3698 }
3699 break;
3700 }
3701
Bram Moolenaareb94e552006-03-11 21:35:11 +00003702 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003703 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003704 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3705 {
3706 char *p = argv[i] + 8;
3707
3708 argtype = ARGTYPE_EDIT;
3709 while (*p != NUL)
3710 {
3711 if (STRNICMP(p, "-wait", 5) == 0)
3712 {
3713 argtype = ARGTYPE_EDIT_WAIT;
3714 p += 5;
3715 }
3716 else if (STRNICMP(p, "-silent", 7) == 0)
3717 {
3718 silent = TRUE;
3719 p += 7;
3720 }
3721 else if (STRNICMP(p, "-tab", 4) == 0)
3722 {
3723 tabs = TRUE;
3724 p += 4;
3725 }
3726 else
3727 {
3728 argtype = ARGTYPE_OTHER;
3729 break;
3730 }
3731 }
3732 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003733 else
3734 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003735
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003736 if (argtype != ARGTYPE_OTHER)
3737 {
3738 if (i == *argc - 1)
3739 mainerr_arg_missing((char_u *)argv[i]);
3740 if (argtype == ARGTYPE_SEND)
3741 {
3742 *serverStr = (char_u *)argv[i + 1];
3743 i++;
3744 }
3745 else
3746 {
3747 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003748 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003749 if (*serverStr == NULL)
3750 {
3751 /* Probably out of memory, exit. */
3752 didone = TRUE;
3753 exiterr = 1;
3754 break;
3755 }
3756 Argc = i;
3757 }
3758# ifdef FEAT_X11
3759 if (xterm_dpy == NULL)
3760 {
3761 mch_errmsg(_("No display"));
3762 ret = -1;
3763 }
3764 else
3765 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3766 NULL, &srv, 0, 0, silent);
3767# else
3768 /* Win32 always works? */
3769 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3770# endif
3771 if (ret < 0)
3772 {
3773 if (argtype == ARGTYPE_SEND)
3774 {
3775 /* Failed to send, abort. */
3776 mch_errmsg(_(": Send failed.\n"));
3777 didone = TRUE;
3778 exiterr = 1;
3779 }
3780 else if (!silent)
3781 /* Let vim start normally. */
3782 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3783 break;
3784 }
3785
3786# ifdef FEAT_GUI_W32
3787 /* Guess that when the server name starts with "g" it's a GUI
3788 * server, which we can bring to the foreground here.
3789 * Foreground() in the server doesn't work very well. */
3790 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3791 SetForegroundWindow(srv);
3792# endif
3793
3794 /*
3795 * For --remote-wait: Wait until the server did edit each
3796 * file. Also detect that the server no longer runs.
3797 */
3798 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3799 {
3800 int numFiles = *argc - i - 1;
3801 int j;
3802 char_u *done = alloc(numFiles);
3803 char_u *p;
3804# ifdef FEAT_GUI_W32
3805 NOTIFYICONDATA ni;
3806 int count = 0;
3807 extern HWND message_window;
3808# endif
3809
3810 if (numFiles > 0 && argv[i + 1][0] == '+')
3811 /* Skip "+cmd" argument, don't wait for it to be edited. */
3812 --numFiles;
3813
3814# ifdef FEAT_GUI_W32
3815 ni.cbSize = sizeof(ni);
3816 ni.hWnd = message_window;
3817 ni.uID = 0;
3818 ni.uFlags = NIF_ICON|NIF_TIP;
3819 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3820 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3821 Shell_NotifyIcon(NIM_ADD, &ni);
3822# endif
3823
3824 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003825 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003826 while (memchr(done, 0, numFiles) != NULL)
3827 {
3828# ifdef WIN32
3829 p = serverGetReply(srv, NULL, TRUE, TRUE);
3830 if (p == NULL)
3831 break;
3832# else
3833 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3834 break;
3835# endif
3836 j = atoi((char *)p);
3837 if (j >= 0 && j < numFiles)
3838 {
3839# ifdef FEAT_GUI_W32
3840 ++count;
3841 sprintf(ni.szTip, _("%d of %d edited"),
3842 count, numFiles);
3843 Shell_NotifyIcon(NIM_MODIFY, &ni);
3844# endif
3845 done[j] = 1;
3846 }
3847 }
3848# ifdef FEAT_GUI_W32
3849 Shell_NotifyIcon(NIM_DELETE, &ni);
3850# endif
3851 }
3852 }
3853 else if (STRICMP(argv[i], "--remote-expr") == 0)
3854 {
3855 if (i == *argc - 1)
3856 mainerr_arg_missing((char_u *)argv[i]);
3857# ifdef WIN32
3858 /* Win32 always works? */
3859 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3860 &res, NULL, 1, FALSE) < 0)
3861# else
3862 if (xterm_dpy == NULL)
3863 mch_errmsg(_("No display: Send expression failed.\n"));
3864 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3865 &res, NULL, 1, 1, FALSE) < 0)
3866# endif
3867 {
3868 if (res != NULL && *res != NUL)
3869 {
3870 /* Output error from remote */
3871 mch_errmsg((char *)res);
3872 vim_free(res);
3873 res = NULL;
3874 }
3875 mch_errmsg(_(": Send expression failed.\n"));
3876 }
3877 }
3878 else if (STRICMP(argv[i], "--serverlist") == 0)
3879 {
3880# ifdef WIN32
3881 /* Win32 always works? */
3882 res = serverGetVimNames();
3883# else
3884 if (xterm_dpy != NULL)
3885 res = serverGetVimNames(xterm_dpy);
3886# endif
3887 if (called_emsg)
3888 mch_errmsg("\n");
3889 }
3890 else if (STRICMP(argv[i], "--servername") == 0)
3891 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003892 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003893 i++;
3894 continue;
3895 }
3896 else
3897 {
3898 *newArgV++ = argv[i];
3899 newArgC++;
3900 continue;
3901 }
3902 didone = TRUE;
3903 if (res != NULL && *res != NUL)
3904 {
3905 mch_msg((char *)res);
3906 if (res[STRLEN(res) - 1] != '\n')
3907 mch_msg("\n");
3908 }
3909 vim_free(res);
3910 }
3911
3912 if (didone)
3913 {
3914 display_errors(); /* display any collected messages */
3915 exit(exiterr); /* Mission accomplished - get out */
3916 }
3917
3918 /* Return back into main() */
3919 *argc = newArgC;
3920 vim_free(sname);
3921}
3922
3923/*
3924 * Build a ":drop" command to send to a Vim server.
3925 */
3926 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003927build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003928 int filec;
3929 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003930 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003931 int sendReply;
3932{
3933 garray_T ga;
3934 int i;
3935 char_u *inicmd = NULL;
3936 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003937 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003938 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003939
3940 if (filec > 0 && filev[0][0] == '+')
3941 {
3942 inicmd = (char_u *)filev[0] + 1;
3943 filev++;
3944 filec--;
3945 }
3946 /* Check if we have at least one argument. */
3947 if (filec <= 0)
3948 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003949
3950 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003951 cwd = alloc(MAXPATHL);
3952 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003953 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003954 if (mch_dirname(cwd, MAXPATHL) != OK)
3955 {
3956 vim_free(cwd);
3957 return NULL;
3958 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003959 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003960#ifdef BACKSLASH_IN_FILENAME
3961 "", /* rem_backslash() will tell what chars to escape */
3962#else
3963 PATH_ESC_CHARS,
3964#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003965 '\\', TRUE);
3966 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003967 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003968 return NULL;
3969 ga_init2(&ga, 1, 100);
3970 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003971 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003972
3973 /* Call inputsave() so that a prompt for an encryption key works. */
3974 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3975 if (tabs)
3976 ga_concat(&ga, (char_u *)"tab ");
3977 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003978 for (i = 0; i < filec; i++)
3979 {
3980 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003981 * do it again in the Vim server. On MS-Windows only escape
3982 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003983 p = vim_strsave_escaped((char_u *)filev[i],
3984#ifdef UNIX
3985 PATH_ESC_CHARS
3986#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003987 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003988#endif
3989 );
3990 if (p == NULL)
3991 {
3992 vim_free(ga.ga_data);
3993 return NULL;
3994 }
3995 ga_concat(&ga, (char_u *)" ");
3996 ga_concat(&ga, p);
3997 vim_free(p);
3998 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003999 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
4000
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004001 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
4002 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004003 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
4004
4005 /* Switch back to the correct current directory (prior to temporary path
4006 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004007 * correct after the :drop command. With line breaks and spaces:
4008 * if !exists('+acd') || !&acd
4009 * if haslocaldir()
4010 * cd -
4011 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004012 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004013 * cd -
4014 * endif
4015 * endif
4016 */
4017 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004018 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004019 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004020 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004021 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004022
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004023 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004024 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4025 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004026 if (inicmd != NULL)
4027 {
4028 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4029 * the following commands to be inserted as text. Use a "|",
4030 * hopefully "inicmd" does allow this... */
4031 ga_concat(&ga, inicmd);
4032 ga_concat(&ga, (char_u *)"|");
4033 }
4034 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4035 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004036 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004037 ga_append(&ga, NUL);
4038 return ga.ga_data;
4039}
4040
4041/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004042 * Make our basic server name: use the specified "arg" if given, otherwise use
4043 * the tail of the command "cmd" we were started with.
4044 * Return the name in allocated memory. This doesn't include a serial number.
4045 */
4046 static char_u *
4047serverMakeName(arg, cmd)
4048 char_u *arg;
4049 char *cmd;
4050{
4051 char_u *p;
4052
4053 if (arg != NULL && *arg != NUL)
4054 p = vim_strsave_up(arg);
4055 else
4056 {
4057 p = vim_strsave_up(gettail((char_u *)cmd));
4058 /* Remove .exe or .bat from the name. */
4059 if (p != NULL && vim_strchr(p, '.') != NULL)
4060 *vim_strchr(p, '.') = NUL;
4061 }
4062 return p;
4063}
4064#endif /* FEAT_CLIENTSERVER */
4065
4066#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4067/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004068 * Replace termcodes such as <CR> and insert as key presses if there is room.
4069 */
4070 void
4071server_to_input_buf(str)
4072 char_u *str;
4073{
4074 char_u *ptr = NULL;
4075 char_u *cpo_save = p_cpo;
4076
4077 /* Set 'cpoptions' the way we want it.
4078 * B set - backslashes are *not* treated specially
4079 * k set - keycodes are *not* reverse-engineered
4080 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004081 * The last but one parameter of replace_termcodes() is TRUE so that the
4082 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004083 */
4084 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004085 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004086 p_cpo = cpo_save;
4087
4088 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4089 {
4090 /*
4091 * Add the string to the input stream.
4092 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4093 *
4094 * First clear typed characters from the typeahead buffer, there could
4095 * be half a mapping there. Then append to the existing string, so
4096 * that multiple commands from a client are concatenated.
4097 */
4098 if (typebuf.tb_maplen < typebuf.tb_len)
4099 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4100 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4101
4102 /* Let input_available() know we inserted text in the typeahead
4103 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004104 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004105 }
4106 vim_free((char_u *)ptr);
4107}
4108
4109/*
4110 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004111 */
4112 char_u *
4113eval_client_expr_to_string(expr)
4114 char_u *expr;
4115{
4116 char_u *res;
4117 int save_dbl = debug_break_level;
4118 int save_ro = redir_off;
4119
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004120 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4121 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004122 debug_break_level = -1;
4123 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004124 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4125 * to be typed. Do generate errors so that try/catch works. */
4126 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004127
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004128 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004129
4130 debug_break_level = save_dbl;
4131 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004132 --emsg_silent;
4133 if (emsg_silent < 0)
4134 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004135
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004136 /* A client can tell us to redraw, but not to display the cursor, so do
4137 * that here. */
4138 setcursor();
4139 out_flush();
4140#ifdef FEAT_GUI
4141 if (gui.in_use)
4142 gui_update_cursor(FALSE, FALSE);
4143#endif
4144
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004145 return res;
4146}
4147
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004148/*
4149 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4150 * return an allocated string. Otherwise return "data".
4151 * "*tofree" is set to the result when it needs to be freed later.
4152 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004153 char_u *
4154serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004155 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004156 char_u *data;
4157 char_u **tofree;
4158{
4159 char_u *res = data;
4160
4161 *tofree = NULL;
4162# ifdef FEAT_MBYTE
4163 if (client_enc != NULL && p_enc != NULL)
4164 {
4165 vimconv_T vimconv;
4166
4167 vimconv.vc_type = CONV_NONE;
4168 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4169 && vimconv.vc_type != CONV_NONE)
4170 {
4171 res = string_convert(&vimconv, data, NULL);
4172 if (res == NULL)
4173 res = data;
4174 else
4175 *tofree = res;
4176 }
4177 convert_setup(&vimconv, NULL, NULL);
4178 }
4179# endif
4180 return res;
4181}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004182#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004183
4184/*
4185 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4186 */
4187#if defined(FEAT_FKMAP) || defined(PROTO)
4188# include "farsi.c"
4189#endif
4190
4191/*
4192 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4193 */
4194#if defined(FEAT_ARABIC) || defined(PROTO)
4195# include "arabic.c"
4196#endif