blob: f19ea1d122c40fb80d8e671f49d47437f35cd583 [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
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100168(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000169{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000170 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000171 mparm_T params; /* various parameters passed between
172 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000173#ifdef STARTUPTIME
174 int i;
175#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000176
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000177 /*
178 * Do any system-specific initialisations. These can NOT use IObuff or
179 * NameBuff. Thus emsg2() cannot be called!
180 */
181 mch_early_init();
182
Bram Moolenaar14993322014-09-09 12:25:33 +0200183#if defined(WIN32) && defined(FEAT_MBYTE)
184 /*
185 * MingW expands command line arguments, which confuses our code to
186 * convert when 'encoding' changes. Get the unexpanded arguments.
187 */
188 argc = get_cmd_argsW(&argv);
189#endif
190
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 /* Many variables are in "params" so that we can pass them to invoked
192 * functions without a lot of arguments. "argc" and "argv" are also
193 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000194 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000195 params.argc = argc;
196 params.argv = argv;
197 params.want_full_screen = TRUE;
198#ifdef FEAT_EVAL
199 params.use_debug_break_level = -1;
200#endif
201#ifdef FEAT_WINDOWS
202 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000204
Bram Moolenaar99685e62013-05-11 13:56:18 +0200205#ifdef FEAT_RUBY
206 {
207 int ruby_stack_start;
208 vim_ruby_init((void *)&ruby_stack_start);
209 }
210#endif
211
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000212#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000213 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000214#endif
215
216#ifdef MEM_PROFILE
217 atexit(vim_mem_profile_dump);
218#endif
219
220#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000221 for (i = 1; i < argc; ++i)
222 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000223 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000224 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000225 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000226 TIME_MSG("--- VIM STARTING ---");
227 break;
228 }
229 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000230#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000231 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000232
233#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000234 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235#endif
236
237#ifdef FEAT_MBYTE
238 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
239#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000240#ifdef FEAT_EVAL
241 eval_init(); /* init global variables */
242#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243
244#ifdef __QNXNTO__
245 qnx_init(); /* PhAttach() for clipboard, (and gui) */
246#endif
247
248#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000249 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000251 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000252 TIME_MSG("GUI prepared");
253#endif
254
255 /* Init the table of Normal mode commands. */
256 init_normal_cmds();
257
258#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000259 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000260#endif
261
262 /*
263 * Allocate space for the generic buffers (needed for set_init_1() and
264 * EMSG2()).
265 */
266 if ((IObuff = alloc(IOSIZE)) == NULL
267 || (NameBuff = alloc(MAXPATHL)) == NULL)
268 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269 TIME_MSG("Allocated generic buffers");
270
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000271#ifdef NBDEBUG
272 /* Wait a moment for debugging NetBeans. Must be after allocating
273 * NameBuff. */
274 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
275 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000276 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000277#endif
278
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000279#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
280 /*
281 * Setup to use the current locale (for ctype() and many other things).
282 * NOTE: Translated messages with encodings other than latin1 will not
283 * work until set_init_1() has been called!
284 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000285 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000286 TIME_MSG("locale set");
287#endif
288
289#ifdef FEAT_GUI
290 gui.dofork = TRUE; /* default is to use fork() */
291#endif
292
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000293 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000294 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000295 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000296 * --server...
297 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000298 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000299 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000300 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000301
302#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000303 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000304#endif
305#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000306 /* Prepare for possibly starting GUI sometime */
307 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000308 TIME_MSG("GUI prepared");
309#endif
310
311#ifdef FEAT_CLIPBOARD
312 clip_init(FALSE); /* Initialise clipboard stuff */
313 TIME_MSG("clipboard setup");
314#endif
315
316 /*
317 * Check if we have an interactive window.
318 * On the Amiga: If there is no window, we open one with a newcli command
319 * (needed for :! to * work). mch_check_win() will also handle the -d or
320 * -dev argument.
321 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000322 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000323 TIME_MSG("window checked");
324
325 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000326 * Allocate the first window and buffer.
327 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000328 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000329 if (win_alloc_first() == FAIL)
330 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000331
332 init_yank(); /* init yank buffers */
333
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000334 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200335 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000336
337 /*
338 * Set the default values for the options.
339 * NOTE: Non-latin1 translated messages are working only after this,
340 * because this is where "has_mbyte" will be set, which is used by
341 * msg_outtrans_len_attr().
342 * First find out the home directory, needed to expand "~" in options.
343 */
344 init_homedir(); /* find real value of $HOME */
345 set_init_1();
346 TIME_MSG("inits 1");
347
348#ifdef FEAT_EVAL
349 set_lang_var(); /* set v:lang and v:ctype */
350#endif
351
352#ifdef FEAT_CLIENTSERVER
353 /*
354 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000355 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000356 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000358#endif
359
360 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000361 * Figure out the way to work from the command name argv[0].
362 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000363 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365
366 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000367 * Process the command line arguments. File names are put in the global
368 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000369 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000370 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 TIME_MSG("parsing arguments");
372
373 /*
374 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000375 * there is no terminal version, and on Windows we can't fork one off with
376 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000377 */
378#ifdef ALWAYS_USE_GUI
379 gui.starting = TRUE;
380#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000381# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000382 /*
383 * Check if the GUI can be started. Reset gui.starting if not.
384 * Don't know about other systems, stay on the safe side and don't check.
385 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000386 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000387 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000388 if (gui_init_check() == FAIL)
389 {
390 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000391
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000392 /* When running "evim" or "gvim -y" we need the menus, exit if we
393 * don't have them. */
394 if (params.evim_mode)
395 mch_exit(1);
396 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397 }
398# endif
399#endif
400
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000401 if (GARGCOUNT > 0)
402 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100403#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404 /*
405 * Expand wildcards in file names.
406 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000407 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200409 start_dir = alloc(MAXPATHL);
410 if (start_dir != NULL)
411 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000412 /* Temporarily add '(' and ')' to 'isfname'. These are valid
413 * filename characters but are excluded from 'isfname' to make
414 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
415 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000416 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000417 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200418 if (start_dir != NULL)
419 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000420 }
421#endif
422 fname = alist_name(&GARGLIST[0]);
423 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000424
425#if defined(WIN32) && defined(FEAT_MBYTE)
426 {
427 extern void set_alist_count(void);
428
429 /* Remember the number of entries in the argument list. If it changes
430 * we don't react on setting 'encoding'. */
431 set_alist_count();
432 }
433#endif
434
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000435#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000436 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000437 {
438 /*
439 * If there is one filename, fully qualified, we have very probably
440 * been invoked from explorer, so change to the file's directory.
441 * Hint: to avoid this when typing a command use a forward slash.
442 * If the cd fails, it doesn't matter.
443 */
444 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200445 if (start_dir != NULL)
446 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000447 }
448#endif
449 TIME_MSG("expanding arguments");
450
451#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000452 if (params.diff_mode && params.window_count == -1)
453 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000454#endif
455
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000456 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000457 ++RedrawingDisabled;
458
459 /*
460 * When listing swap file names, don't do cursor positioning et. al.
461 */
462 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000463 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000464
465 /*
466 * When certain to start the GUI, don't check capabilities of terminal.
467 * For GTK we can't be sure, but when started from the desktop it doesn't
468 * make sense to try using a terminal.
469 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000470#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000471 if (gui.starting
472# ifdef FEAT_GUI_GTK
473 && !isatty(2)
474# endif
475 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000476 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000477#endif
478
479#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
480 /* When the GUI is started from Finder, need to display messages in a
481 * message box. isatty(2) returns TRUE anyway, thus we need to check the
482 * name to know we're not started from a terminal. */
483 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000484 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000485 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000486
487 /* Avoid always using "/" as the current directory. Note that when
488 * started from Finder the arglist will be filled later in
489 * HandleODocAE() and "fname" will be NULL. */
490 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
491 && STRCMP(NameBuff, "/") == 0)
492 {
493 if (fname != NULL)
494 (void)vim_chdirfile(fname);
495 else
496 {
497 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
498 vim_chdir(NameBuff);
499 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200500 if (start_dir != NULL)
501 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000502 }
503 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000504#endif
505
506 /*
507 * mch_init() sets up the terminal (window) for use. This must be
508 * done after resetting full_screen, otherwise it may move the cursor
509 * (MSDOS).
510 * Note that we may use mch_exit() before mch_init()!
511 */
512 mch_init();
513 TIME_MSG("shell init");
514
515#ifdef USE_XSMP
516 /*
517 * For want of anywhere else to do it, try to connect to xsmp here.
518 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
519 * Hijacking -X 'no X connection' to also disable XSMP connection as that
520 * has a similar delay upon failure.
521 * Only try if SESSION_MANAGER is set to something non-null.
522 */
523 if (!x_no_connect)
524 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000525 char *p = getenv("SESSION_MANAGER");
526
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000527 if (p != NULL && *p != NUL)
528 {
529 xsmp_init();
530 TIME_MSG("xsmp init");
531 }
532 }
533#endif
534
535 /*
536 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000537 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000538 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000539
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000540 /* This message comes before term inits, but after setting "silent_mode"
541 * when the input is not a tty. */
542 if (GARGCOUNT > 1 && !silent_mode)
543 printf(_("%d files to edit\n"), GARGCOUNT);
544
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000545 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000546 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000547 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000548 capabilities (will set full_screen) */
549 screen_start(); /* don't know where cursor is now */
550 TIME_MSG("Termcap init");
551 }
552
553 /*
554 * Set the default values for the options that use Rows and Columns.
555 */
556 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000557 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000558#ifdef FEAT_DIFF
559 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
560 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000561 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562 diff_win_options(firstwin, FALSE);
563#endif
564
565 cmdline_row = Rows - p_ch;
566 msg_row = cmdline_row;
567 screenalloc(FALSE); /* allocate screen buffers */
568 set_init_2();
569 TIME_MSG("inits 2");
570
571 msg_scroll = TRUE;
572 no_wait_return = TRUE;
573
574 init_mappings(); /* set up initial mappings */
575
576 init_highlight(TRUE, FALSE); /* set the default highlight groups */
577 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000578
579#ifdef FEAT_EVAL
580 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000581 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000582#endif
583
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100584#ifdef FEAT_MZSCHEME
585 /*
586 * Newer version of MzScheme (Racket) require earlier (trampolined)
587 * initialisation via scheme_main_setup.
588 * Implement this by initialising it as early as possible
589 * and splitting off remaining Vim main into vim_main2
590 */
591 {
592 /* Pack up preprocessed command line arguments.
593 * It is safe because Scheme does not access argc/argv. */
594 char *args[2];
595 args[0] = (char *)fname;
596 args[1] = (char *)&params;
597 return mzscheme_main(2, args);
598 }
599}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100600#endif
601#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100602
Bram Moolenaar8866d272012-11-28 15:55:42 +0100603/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
604 * NO_VIM_MAIN is defined. */
605#ifdef FEAT_MZSCHEME
606 int
607vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100608{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100609# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100610 char_u *fname = (char_u *)argv[0];
611 mparm_T params;
612
613 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100614# else
615 return 0;
616}
617# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100618#endif
619
Bram Moolenaar8866d272012-11-28 15:55:42 +0100620#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000621 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000622 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000623
Bram Moolenaar58d98232005-07-23 22:25:46 +0000624 /* Source startup scripts. */
625 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000626
627#ifdef FEAT_EVAL
628 /*
629 * Read all the plugin files.
630 * Only when compiled with +eval, since most plugins need it.
631 */
632 if (p_lpl)
633 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000634# ifdef VMS /* Somehow VMS doesn't handle the "**". */
635 source_runtime((char_u *)"plugin/*.vim", TRUE);
636# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000637 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000638# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000639 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100640
641 source_packages();
642 TIME_MSG("loading packages");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000643 }
644#endif
645
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000646#ifdef FEAT_DIFF
647 /* Decide about window layout for diff mode after reading vimrc. */
648 if (params.diff_mode && params.window_layout == 0)
649 {
650 if (diffopt_horizontal())
651 params.window_layout = WIN_HOR; /* use horizontal split */
652 else
653 params.window_layout = WIN_VER; /* use vertical split */
654 }
655#endif
656
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000657 /*
658 * Recovery mode without a file name: List swap files.
659 * This uses the 'dir' option, therefore it must be after the
660 * initializations.
661 */
662 if (recoverymode && fname == NULL)
663 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200664 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000665 mch_exit(0);
666 }
667
668 /*
669 * Set a few option defaults after reading .vimrc files:
670 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
671 */
672 set_init_3();
673 TIME_MSG("inits 3");
674
675 /*
676 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
677 * Note that this overrides anything from a vimrc file.
678 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000679 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000680 p_uc = 0;
681
682#ifdef FEAT_FKMAP
683 if (curwin->w_p_rl && p_altkeymap)
684 {
685 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
686# ifdef FEAT_ARABIC
687 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
688# endif
689 p_fkmap = TRUE; /* Set the Farsi keymap mode */
690 }
691#endif
692
693#ifdef FEAT_GUI
694 if (gui.starting)
695 {
696#if defined(UNIX) || defined(VMS)
697 /* When something caused a message from a vimrc script, need to output
698 * an extra newline before the shell prompt. */
699 if (did_emsg || msg_didout)
700 putchar('\n');
701#endif
702
703 gui_start(); /* will set full_screen to TRUE */
704 TIME_MSG("starting GUI");
705
706 /* When running "evim" or "gvim -y" we need the menus, exit if we
707 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000708 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000709 mch_exit(1);
710 }
711#endif
712
713#ifdef SPAWNO /* special MSDOS swapping library */
714 init_SPAWNO("", SWAP_ANY);
715#endif
716
717#ifdef FEAT_VIMINFO
718 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000719 * Read in registers, history etc, but not marks, from the viminfo file.
720 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000721 */
722 if (*p_viminfo != NUL)
723 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000724 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000725 TIME_MSG("reading viminfo");
726 }
727#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100728#ifdef FEAT_EVAL
729 /* It's better to make v:oldfiles an empty list than NULL. */
730 if (get_vim_var_list(VV_OLDFILES) == NULL)
731 set_vim_var_list(VV_OLDFILES, list_alloc());
732#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000733
734#ifdef FEAT_QUICKFIX
735 /*
736 * "-q errorfile": Load the error file now.
737 * If the error file can't be read, exit before doing anything else.
738 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000739 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000740 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000741 if (params.use_ef != NULL)
742 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000743 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200744 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
745 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000746 {
747 out_char('\n');
748 mch_exit(3);
749 }
750 TIME_MSG("reading errorfile");
751 }
752#endif
753
754 /*
755 * Start putting things on the screen.
756 * Scroll screen down before drawing over it
757 * Clear screen now, so file message will not be cleared.
758 */
759 starting = NO_BUFFERS;
760 no_wait_return = FALSE;
761 if (!exmode_active)
762 msg_scroll = FALSE;
763
764#ifdef FEAT_GUI
765 /*
766 * This seems to be required to make callbacks to be called now, instead
767 * of after things have been put on the screen, which then may be deleted
768 * when getting a resize callback.
769 * For the Mac this handles putting files dropped on the Vim icon to
770 * global_alist.
771 */
772 if (gui.in_use)
773 {
774# ifdef FEAT_SUN_WORKSHOP
775 if (!usingSunWorkShop)
776# endif
777 gui_wait_for_chars(50L);
778 TIME_MSG("GUI delay");
779 }
780#endif
781
782#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
783 qnx_clip_init();
784#endif
785
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200786#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
787 clip_init(TRUE);
788#endif
789
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000790#ifdef FEAT_XCLIPBOARD
791 /* Start using the X clipboard, unless the GUI was started. */
792# ifdef FEAT_GUI
793 if (!gui.in_use)
794# endif
795 {
796 setup_term_clip();
797 TIME_MSG("setup clipboard");
798 }
799#endif
800
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000801#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000802 /* Prepare for being a Vim server. */
803 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000804#endif
805
806 /*
807 * If "-" argument given: Read file from stdin.
808 * Do this before starting Raw mode, because it may change things that the
809 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
810 * are the same terminal: "cat | vim -".
811 * Using autocommands here may cause trouble...
812 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000813 if (params.edit_type == EDIT_STDIN && !recoverymode)
814 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000815
816#if defined(UNIX) || defined(VMS)
817 /* When switching screens and something caused a message from a vimrc
818 * script, need to output an extra newline on exit. */
819 if ((did_emsg || msg_didout) && *T_TI != NUL)
820 newline_on_exit = TRUE;
821#endif
822
823 /*
824 * When done something that is not allowed or error message call
825 * wait_return. This must be done before starttermcap(), because it may
826 * switch to another screen. It must be done after settmode(TMODE_RAW),
827 * because we want to react on a single key stroke.
828 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000829 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000830 */
831 settmode(TMODE_RAW);
832 TIME_MSG("setting raw mode");
833
834 if (need_wait_return || msg_didany)
835 {
836 wait_return(TRUE);
837 TIME_MSG("waiting for return");
838 }
839
840 starttermcap(); /* start termcap if not done by wait_return() */
841 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200842#if defined(FEAT_TERMRESPONSE)
843# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200844 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200845# endif
846 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100847#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000848
849#ifdef FEAT_MOUSE
850 setmouse(); /* may start using the mouse */
851#endif
852 if (scroll_region)
853 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000854 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000855
856 /*
857 * Don't clear the screen when starting in Ex mode, unless using the GUI.
858 */
859 if (exmode_active
860#ifdef FEAT_GUI
861 && !gui.in_use
862#endif
863 )
864 must_redraw = CLEAR;
865 else
866 {
867 screenclear(); /* clear screen */
868 TIME_MSG("clearing screen");
869 }
870
871#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000872 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000873 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100874 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200875 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000876 TIME_MSG("getting crypt key");
877 }
878#endif
879
880 no_wait_return = TRUE;
881
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000882 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000883 * Create the requested number of windows and edit buffers in them.
884 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000885 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000886 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000887 TIME_MSG("opening buffers");
888
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000889#ifdef FEAT_EVAL
890 /* clear v:swapcommand */
891 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
892#endif
893
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000894 /* Ex starts at last line of the file */
895 if (exmode_active)
896 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
897
898#ifdef FEAT_AUTOCMD
899 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
900 TIME_MSG("BufEnter autocommands");
901#endif
902 setpcmark();
903
904#ifdef FEAT_QUICKFIX
905 /*
906 * When started with "-q errorfile" jump to first error now.
907 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000908 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000909 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000910 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000911 TIME_MSG("jump to first error");
912 }
913#endif
914
915#ifdef FEAT_WINDOWS
916 /*
917 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000918 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000919 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200920 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000921#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200922 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000923
924#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000925 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000926 {
927 win_T *wp;
928
929 /* set options in each window for "vimdiff". */
930 for (wp = firstwin; wp != NULL; wp = wp->w_next)
931 diff_win_options(wp, TRUE);
932 }
933#endif
934
935 /*
936 * Shorten any of the filenames, but only when absolute.
937 */
938 shorten_fnames(FALSE);
939
940 /*
941 * Need to jump to the tag before executing the '-c command'.
942 * Makes "vim -c '/return' -t main" work.
943 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000944 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000945 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000946#if defined(HAS_SWAP_EXISTS_ACTION)
947 swap_exists_did_quit = FALSE;
948#endif
949
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000950 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000951 do_cmdline_cmd(IObuff);
952 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000953
954#if defined(HAS_SWAP_EXISTS_ACTION)
955 /* If the user doesn't want to edit the file then we quit here. */
956 if (swap_exists_did_quit)
957 getout(1);
958#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000959 }
960
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000961 /* Execute any "+", "-c" and "-S" arguments. */
962 if (params.n_commands > 0)
963 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000964
965 RedrawingDisabled = 0;
966 redraw_all_later(NOT_VALID);
967 no_wait_return = FALSE;
968 starting = 0;
969
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000970#ifdef FEAT_TERMRESPONSE
971 /* Requesting the termresponse is postponed until here, so that a "-c q"
972 * argument doesn't make it appear in the shell Vim was started from. */
973 may_req_termresponse();
974#endif
975
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000976 /* start in insert mode */
977 if (p_im)
978 need_start_insertmode = TRUE;
979
980#ifdef FEAT_AUTOCMD
981 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
982 TIME_MSG("VimEnter autocommands");
983#endif
984
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200985#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
986 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
987 * done after the clipboard is available and all initial commands that may
988 * modify the 'clipboard' setting have run; i.e. just before entering the
989 * main loop. */
990 {
991 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100992
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200993 adjust_clip_reg(&default_regname);
994 set_reg_var(default_regname);
995 }
996#endif
997
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000998#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
999 /* When a startup script or session file setup for diff'ing and
1000 * scrollbind, sync the scrollbind now. */
1001 if (curwin->w_p_diff && curwin->w_p_scb)
1002 {
1003 update_topline();
1004 check_scrollbind((linenr_T)0, 0L);
1005 TIME_MSG("diff scrollbinding");
1006 }
1007#endif
1008
1009#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1010 mch_set_winsize_now(); /* Allow winsize changes from now on */
1011#endif
1012
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001013#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1014 /* When tab pages were created, may need to update the tab pages line and
1015 * scrollbars. This is skipped while creating them. */
1016 if (first_tabpage->tp_next != NULL)
1017 {
1018 out_flush();
1019 gui_init_which_components(NULL);
1020 gui_update_scrollbars(TRUE);
1021 }
1022 need_mouse_correct = TRUE;
1023#endif
1024
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001025 /* If ":startinsert" command used, stuff a dummy command to be able to
1026 * call normal_cmd(), which will then start Insert mode. */
1027 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001028 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001029
1030#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001031 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001032 {
1033# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001034# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001035 && !defined(FEAT_GUI_W32)
1036 if (gui.in_use)
1037 {
1038 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1039 mch_exit(2);
1040 }
1041# endif
1042# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001044 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001045 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046#endif
1047
1048 TIME_MSG("before starting main loop");
1049
1050 /*
1051 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001052 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001053 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001054
1055 return 0;
1056}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001057#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001058#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001059
1060/*
1061 * Main loop: Execute Normal mode commands until exiting Vim.
1062 * Also used to handle commands in the command-line window, until the window
1063 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001064 * Also used to handle ":visual" command after ":global": execute Normal mode
1065 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001066 */
1067 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001068main_loop(
1069 int cmdwin, /* TRUE when working in the command-line window */
1070 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001071{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001072 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001073 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001074#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001075 /* these are static to avoid a compiler warning */
1076 static linenr_T conceal_old_cursor_line = 0;
1077 static linenr_T conceal_new_cursor_line = 0;
1078 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001079#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001080
1081#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1082 /* Setup to catch a terminating error from the X server. Just ignore
1083 * it, restore the state and continue. This might not always work
1084 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001085 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001087 {
1088 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001089 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001090 got_int = TRUE;
1091 need_wait_return = FALSE;
1092 global_busy = FALSE;
1093 exmode_active = 0;
1094 skip_redraw = FALSE;
1095 RedrawingDisabled = 0;
1096 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001097 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001098# ifdef FEAT_EVAL
1099 emsg_skip = 0;
1100# endif
1101 emsg_off = 0;
1102# ifdef FEAT_MOUSE
1103 setmouse();
1104# endif
1105 settmode(TMODE_RAW);
1106 starttermcap();
1107 scroll_start();
1108 redraw_later_clear();
1109 }
1110#endif
1111
1112 clear_oparg(&oa);
1113 while (!cmdwin
1114#ifdef FEAT_CMDWIN
1115 || cmdwin_result == 0
1116#endif
1117 )
1118 {
1119 if (stuff_empty())
1120 {
1121 did_check_timestamps = FALSE;
1122 if (need_check_timestamps)
1123 check_timestamps(FALSE);
1124 if (need_wait_return) /* if wait_return still needed ... */
1125 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001126 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001127 {
1128 need_start_insertmode = FALSE;
1129 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1130 /* skip the fileinfo message now, because it would be shown
1131 * after insert mode finishes! */
1132 need_fileinfo = FALSE;
1133 }
1134 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001135
1136 /* Reset "got_int" now that we got back to the main loop. Except when
1137 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1138 * the ":g" command.
1139 * For ":g/pat/vi" we reset "got_int" when used once. When used
1140 * a second time we go back to Ex mode and abort the ":g" command. */
1141 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001142 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001143 if (noexmode && global_busy && !exmode_active && previous_got_int)
1144 {
1145 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1146 * used and keep "got_int" set, so that it aborts ":g". */
1147 exmode_active = EXMODE_NORMAL;
1148 State = NORMAL;
1149 }
1150 else if (!global_busy || !exmode_active)
1151 {
1152 if (!quit_more)
1153 (void)vgetc(); /* flush all buffers */
1154 got_int = FALSE;
1155 }
1156 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001157 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001158 else
1159 previous_got_int = FALSE;
1160
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001161 if (!exmode_active)
1162 msg_scroll = FALSE;
1163 quit_more = FALSE;
1164
1165 /*
1166 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1167 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1168 * update cursor and redraw.
1169 */
1170 if (skip_redraw || exmode_active)
1171 skip_redraw = FALSE;
1172 else if (do_redraw || stuff_empty())
1173 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001174#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001175 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001176 if (!finish_op && (
1177# ifdef FEAT_AUTOCMD
1178 has_cursormoved()
1179# endif
1180# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1181 ||
1182# endif
1183# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001184 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001185# endif
1186 )
1187 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001188 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001189# ifdef FEAT_AUTOCMD
1190 if (has_cursormoved())
1191 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1192 FALSE, curbuf);
1193# endif
1194# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001195 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001196 {
1197 conceal_old_cursor_line = last_cursormoved.lnum;
1198 conceal_new_cursor_line = curwin->w_cursor.lnum;
1199 conceal_update_lines = TRUE;
1200 }
1201# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001202 last_cursormoved = curwin->w_cursor;
1203 }
1204#endif
1205
Bram Moolenaar186628f2013-03-19 13:33:23 +01001206#ifdef FEAT_AUTOCMD
1207 /* Trigger TextChanged if b_changedtick differs. */
1208 if (!finish_op && has_textchanged()
1209 && last_changedtick != curbuf->b_changedtick)
1210 {
1211 if (last_changedtick_buf == curbuf)
1212 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1213 FALSE, curbuf);
1214 last_changedtick_buf = curbuf;
1215 last_changedtick = curbuf->b_changedtick;
1216 }
1217#endif
1218
Bram Moolenaar33aec762006-01-22 23:30:12 +00001219#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1220 /* Scroll-binding for diff mode may have been postponed until
1221 * here. Avoids doing it for every change. */
1222 if (diff_need_scrollbind)
1223 {
1224 check_scrollbind((linenr_T)0, 0L);
1225 diff_need_scrollbind = FALSE;
1226 }
1227#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001228#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001229 /* Include a closed fold completely in the Visual area. */
1230 foldAdjustVisual();
1231#endif
1232#ifdef FEAT_FOLDING
1233 /*
1234 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1235 * contain the cursor.
1236 * When 'foldopen' is "all", open the fold(s) under the cursor.
1237 * This may mark the window for redrawing.
1238 */
1239 if (hasAnyFolding(curwin) && !char_avail())
1240 {
1241 foldCheckClose();
1242 if (fdo_flags & FDO_ALL)
1243 foldOpenCursor();
1244 }
1245#endif
1246
1247 /*
1248 * Before redrawing, make sure w_topline is correct, and w_leftcol
1249 * if lines don't wrap, and w_skipcol if lines wrap.
1250 */
1251 update_topline();
1252 validate_cursor();
1253
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001254 if (VIsual_active)
1255 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001256 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001257 update_screen(0);
1258 else if (redraw_cmdline || clear_cmdline)
1259 showmode();
1260#ifdef FEAT_WINDOWS
1261 redraw_statuslines();
1262#endif
1263#ifdef FEAT_TITLE
1264 if (need_maketitle)
1265 maketitle();
1266#endif
1267 /* display message after redraw */
1268 if (keep_msg != NULL)
1269 {
1270 char_u *p;
1271
1272 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001273 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1274 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001275 p = keep_msg;
1276 msg_attr(p, keep_msg_attr);
1277 vim_free(p);
1278 }
1279 if (need_fileinfo) /* show file info after redraw */
1280 {
1281 fileinfo(FALSE, TRUE, FALSE);
1282 need_fileinfo = FALSE;
1283 }
1284
1285 emsg_on_display = FALSE; /* can delete error message now */
1286 did_emsg = FALSE;
1287 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001288 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001289 showruler(FALSE);
1290
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001291# if defined(FEAT_CONCEAL)
1292 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001293 && (conceal_old_cursor_line != conceal_new_cursor_line
1294 || conceal_cursor_line(curwin)
1295 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001296 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001297 if (conceal_old_cursor_line != conceal_new_cursor_line
1298 && conceal_old_cursor_line
1299 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001300 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001301 update_single_line(curwin, conceal_new_cursor_line);
1302 curwin->w_valid &= ~VALID_CROW;
1303 }
1304# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001305 setcursor();
1306 cursor_on();
1307
1308 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001309
1310#ifdef STARTUPTIME
1311 /* Now that we have drawn the first screen all the startup stuff
1312 * has been done, close any file for startup messages. */
1313 if (time_fd != NULL)
1314 {
1315 TIME_MSG("first screen update");
1316 TIME_MSG("--- VIM STARTED ---");
1317 fclose(time_fd);
1318 time_fd = NULL;
1319 }
1320#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001321 }
1322#ifdef FEAT_GUI
1323 if (need_mouse_correct)
1324 gui_mouse_correct();
1325#endif
1326
1327 /*
1328 * Update w_curswant if w_set_curswant has been set.
1329 * Postponed until here to avoid computing w_virtcol too often.
1330 */
1331 update_curswant();
1332
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001333#ifdef FEAT_EVAL
1334 /*
1335 * May perform garbage collection when waiting for a character, but
1336 * only at the very toplevel. Otherwise we may be using a List or
1337 * Dict internally somewhere.
1338 * "may_garbage_collect" is reset in vgetc() which is invoked through
1339 * do_exmode() and normal_cmd().
1340 */
1341 may_garbage_collect = (!cmdwin && !noexmode);
1342#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001343 /*
1344 * If we're invoked as ex, do a round of ex commands.
1345 * Otherwise, get and execute a normal mode command.
1346 */
1347 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001348 {
1349 if (noexmode) /* End of ":global/path/visual" commands */
1350 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001351 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001352 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001353 else
1354 normal_cmd(&oa, TRUE);
1355 }
1356}
1357
1358
1359#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1360/*
1361 * Exit, but leave behind swap files for modified buffers.
1362 */
1363 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001364getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001365{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001366# if defined(SIGHUP) && defined(SIG_IGN)
1367 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1368 * makes Vim exit and then handling SIGHUP causes various reentrance
1369 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001370 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001371# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001372
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001373 ml_close_notmod(); /* close all not-modified buffers */
1374 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1375 ml_close_all(FALSE); /* close all memfiles, without deleting */
1376 getout(exitval); /* exit Vim properly */
1377}
1378#endif
1379
1380
1381/* Exit properly */
1382 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001383getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001384{
1385#ifdef FEAT_AUTOCMD
1386 buf_T *buf;
1387 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001388 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001389#endif
1390
1391 exiting = TRUE;
1392
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001393 /* When running in Ex mode an error causes us to exit with a non-zero exit
1394 * code. POSIX requires this, although it's not 100% clear from the
1395 * standard. */
1396 if (exmode_active)
1397 exitval += ex_exitval;
1398
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001399 /* Position the cursor on the last screen line, below all the text */
1400#ifdef FEAT_GUI
1401 if (!gui.in_use)
1402#endif
1403 windgoto((int)Rows - 1, 0);
1404
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001405#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1406 /* Optionally print hashtable efficiency. */
1407 hash_debug_results();
1408#endif
1409
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001410#ifdef FEAT_GUI
1411 msg_didany = FALSE;
1412#endif
1413
1414#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001415 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001416 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001417 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1418# if defined FEAT_WINDOWS
1419 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001420 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001421 next_tp = tp->tp_next;
1422 for (wp = (tp == curtab)
1423 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001424 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001425 if (wp->w_buffer == NULL)
1426 /* Autocmd must have close the buffer already, skip. */
1427 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001428 buf = wp->w_buffer;
1429 if (buf->b_changedtick != -1)
1430 {
1431 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1432 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001433 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001434 /* start all over, autocommands may mess up the lists */
1435 next_tp = first_tabpage;
1436 break;
1437 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001438 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001439 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001440# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001441 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1442 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001443# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001444
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001445 /* Trigger BufUnload for buffers that are loaded */
1446 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1447 if (buf->b_ml.ml_mfp != NULL)
1448 {
1449 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001451 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1452 break;
1453 }
1454 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1455 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001456#endif
1457
1458#ifdef FEAT_VIMINFO
1459 if (*p_viminfo != NUL)
1460 /* Write out the registers, history, marks etc, to the viminfo file */
1461 write_viminfo(NULL, FALSE);
1462#endif
1463
1464#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001465 if (get_vim_var_nr(VV_DYING) <= 1)
1466 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001467#endif
1468
Bram Moolenaar05159a02005-02-26 23:04:13 +00001469#ifdef FEAT_PROFILE
1470 profile_dump();
1471#endif
1472
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001473 if (did_emsg
1474#ifdef FEAT_GUI
1475 || (gui.in_use && msg_didany && p_verbose > 0)
1476#endif
1477 )
1478 {
1479 /* give the user a chance to read the (error) message */
1480 no_wait_return = FALSE;
1481 wait_return(FALSE);
1482 }
1483
1484#ifdef FEAT_AUTOCMD
1485 /* Position the cursor again, the autocommands may have moved it */
1486# ifdef FEAT_GUI
1487 if (!gui.in_use)
1488# endif
1489 windgoto((int)Rows - 1, 0);
1490#endif
1491
Bram Moolenaar65edff82016-02-21 16:40:11 +01001492#ifdef FEAT_JOB
1493 job_stop_on_exit();
1494#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001495#ifdef FEAT_LUA
1496 lua_end();
1497#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001498#ifdef FEAT_MZSCHEME
1499 mzscheme_end();
1500#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001501#ifdef FEAT_TCL
1502 tcl_end();
1503#endif
1504#ifdef FEAT_RUBY
1505 ruby_end();
1506#endif
1507#ifdef FEAT_PYTHON
1508 python_end();
1509#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001510#ifdef FEAT_PYTHON3
1511 python3_end();
1512#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001513#ifdef FEAT_PERL
1514 perl_end();
1515#endif
1516#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1517 iconv_end();
1518#endif
1519#ifdef FEAT_NETBEANS_INTG
1520 netbeans_end();
1521#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001522#ifdef FEAT_CSCOPE
1523 cs_end();
1524#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001525#ifdef FEAT_EVAL
1526 if (garbage_collect_at_exit)
1527 garbage_collect();
1528#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001529#if defined(WIN32) && defined(FEAT_MBYTE)
1530 free_cmd_argsW();
1531#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001532
1533 mch_exit(exitval);
1534}
1535
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001536#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001537/*
1538 * Get a (optional) count for a Vim argument.
1539 */
1540 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001541get_number_arg(
1542 char_u *p, /* pointer to argument */
1543 int *idx, /* index in argument, is incremented */
1544 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001545{
1546 if (vim_isdigit(p[*idx]))
1547 {
1548 def = atoi((char *)&(p[*idx]));
1549 while (vim_isdigit(p[*idx]))
1550 *idx = *idx + 1;
1551 }
1552 return def;
1553}
1554
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001555#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1556/*
1557 * Setup to use the current locale (for ctype() and many other things).
1558 */
1559 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001560init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001561{
1562 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001563
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001564# ifdef FEAT_GUI_GTK
1565 /* Tell Gtk not to change our locale settings. */
1566 gtk_disable_setlocale();
1567# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001568# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1569 /* Make sure strtod() uses a decimal point, not a comma. */
1570 setlocale(LC_NUMERIC, "C");
1571# endif
1572
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001573# ifdef WIN32
1574 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1575 * text while it's expecting text in the current locale. This call avoids
1576 * that. */
1577 setlocale(LC_CTYPE, "C");
1578# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001579
1580# ifdef FEAT_GETTEXT
1581 {
1582 int mustfree = FALSE;
1583 char_u *p;
1584
1585# ifdef DYNAMIC_GETTEXT
1586 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001587 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001588# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001589 /* expand_env() doesn't work yet, because g_chartab[] is not
1590 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001591 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1592 if (p != NULL && *p != NUL)
1593 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001594 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001595 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1596 }
1597 if (mustfree)
1598 vim_free(p);
1599 textdomain(VIMPACKAGE);
1600 }
1601# endif
1602}
1603#endif
1604
1605/*
1606 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1607 * If the executable name starts with "r" we disable shell commands.
1608 * If the next character is "e" we run in Easy mode.
1609 * If the next character is "g" we run the GUI version.
1610 * If the next characters are "view" we start in readonly mode.
1611 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1612 * If the next characters are "ex" we start in Ex mode. If it's followed
1613 * by "im" use improved Ex mode.
1614 */
1615 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001616parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001617{
1618 char_u *initstr;
1619
1620 initstr = gettail((char_u *)parmp->argv[0]);
1621
1622#ifdef MACOS_X_UNIX
1623 /* An issue has been seen when launching Vim in such a way that
1624 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1625 * executable or a symbolic link of it. Until this issue is resolved
1626 * we prohibit the GUI from being used.
1627 */
1628 if (STRCMP(initstr, parmp->argv[0]) == 0)
1629 disallow_gui = TRUE;
1630
1631 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001632 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001633#endif
1634
1635#ifdef FEAT_EVAL
1636 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001637 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001638#endif
1639
1640 if (TOLOWER_ASC(initstr[0]) == 'r')
1641 {
1642 restricted = TRUE;
1643 ++initstr;
1644 }
1645
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001646 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001647 if (TOLOWER_ASC(initstr[0]) == 'e'
1648 && (TOLOWER_ASC(initstr[1]) == 'v'
1649 || TOLOWER_ASC(initstr[1]) == 'g'))
1650 {
1651#ifdef FEAT_GUI
1652 gui.starting = TRUE;
1653#endif
1654 parmp->evim_mode = TRUE;
1655 ++initstr;
1656 }
1657
Bram Moolenaar806875d2008-09-18 18:57:10 +00001658 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1659 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001660 {
1661 main_start_gui();
1662#ifdef FEAT_GUI
1663 ++initstr;
1664#endif
1665 }
1666
1667 if (STRNICMP(initstr, "view", 4) == 0)
1668 {
1669 readonlymode = TRUE;
1670 curbuf->b_p_ro = TRUE;
1671 p_uc = 10000; /* don't update very often */
1672 initstr += 4;
1673 }
1674 else if (STRNICMP(initstr, "vim", 3) == 0)
1675 initstr += 3;
1676
1677 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1678 if (STRICMP(initstr, "diff") == 0)
1679 {
1680#ifdef FEAT_DIFF
1681 parmp->diff_mode = TRUE;
1682#else
1683 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1684 mch_errmsg("\n");
1685 mch_exit(2);
1686#endif
1687 }
1688
1689 if (STRNICMP(initstr, "ex", 2) == 0)
1690 {
1691 if (STRNICMP(initstr + 2, "im", 2) == 0)
1692 exmode_active = EXMODE_VIM;
1693 else
1694 exmode_active = EXMODE_NORMAL;
1695 change_compatible(TRUE); /* set 'compatible' */
1696 }
1697}
1698
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001699/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001700 * Get the name of the display, before gui_prepare() removes it from
1701 * argv[]. Used for the xterm-clipboard display.
1702 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001703 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001704 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001705 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001706early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001707{
Bram Moolenaar03005972008-11-20 13:12:36 +00001708#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1709 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001710 int argc = parmp->argc;
1711 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001712 int i;
1713
1714 for (i = 1; i < argc; i++)
1715 {
1716 if (STRCMP(argv[i], "--") == 0)
1717 break;
1718# ifdef FEAT_XCLIPBOARD
1719 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001720# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001721 || STRICMP(argv[i], "--display") == 0
1722# endif
1723 )
1724 {
1725 if (i == argc - 1)
1726 mainerr_arg_missing((char_u *)argv[i]);
1727 xterm_display = argv[++i];
1728 }
1729# endif
1730# ifdef FEAT_CLIENTSERVER
1731 else if (STRICMP(argv[i], "--servername") == 0)
1732 {
1733 if (i == argc - 1)
1734 mainerr_arg_missing((char_u *)argv[i]);
1735 parmp->serverName_arg = (char_u *)argv[++i];
1736 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001737 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001738 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001739 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001740 {
1741 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001742# ifdef FEAT_GUI
1743 if (strstr(argv[i], "-wait") != 0)
1744 /* don't fork() when starting the GUI to edit files ourself */
1745 gui.dofork = FALSE;
1746# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001747 }
1748# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001749
1750# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1751# ifdef FEAT_GUI_W32
1752 else if (STRICMP(argv[i], "--windowid") == 0)
1753# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001754 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001755# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001756 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001757 long_u id;
1758 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001759
1760 if (i == argc - 1)
1761 mainerr_arg_missing((char_u *)argv[i]);
1762 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001763 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001764 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001765 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001766 if (count != 1)
1767 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1768 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001769# ifdef FEAT_GUI_W32
1770 win_socket_id = id;
1771# else
1772 gtk_socket_id = id;
1773# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001774 i++;
1775 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001776# endif
1777# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001778 else if (STRICMP(argv[i], "--echo-wid") == 0)
1779 echo_wid_arg = TRUE;
1780# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001781# ifndef FEAT_NETBEANS_INTG
1782 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001783 {
1784 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1785 mch_exit(2);
1786 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001787# endif
1788
Bram Moolenaar58d98232005-07-23 22:25:46 +00001789 }
1790#endif
1791}
1792
1793/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001794 * Scan the command line arguments.
1795 */
1796 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001797command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001798{
1799 int argc = parmp->argc;
1800 char **argv = parmp->argv;
1801 int argv_idx; /* index in argv[n][] */
1802 int had_minmin = FALSE; /* found "--" argument */
1803 int want_argument; /* option argument with argument */
1804 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001805 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001806 long n;
1807
1808 --argc;
1809 ++argv;
1810 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1811 while (argc > 0)
1812 {
1813 /*
1814 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1815 */
1816 if (argv[0][0] == '+' && !had_minmin)
1817 {
1818 if (parmp->n_commands >= MAX_ARG_CMDS)
1819 mainerr(ME_EXTRA_CMD, NULL);
1820 argv_idx = -1; /* skip to next argument */
1821 if (argv[0][1] == NUL)
1822 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1823 else
1824 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1825 }
1826
1827 /*
1828 * Optional argument.
1829 */
1830 else if (argv[0][0] == '-' && !had_minmin)
1831 {
1832 want_argument = FALSE;
1833 c = argv[0][argv_idx++];
1834#ifdef VMS
1835 /*
1836 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1837 * and "-/X" as "-X".
1838 */
1839 if (c == '/')
1840 {
1841 c = argv[0][argv_idx++];
1842 c = TOUPPER_ASC(c);
1843 }
1844 else
1845 c = TOLOWER_ASC(c);
1846#endif
1847 switch (c)
1848 {
1849 case NUL: /* "vim -" read from stdin */
1850 /* "ex -" silent mode */
1851 if (exmode_active)
1852 silent_mode = TRUE;
1853 else
1854 {
1855 if (parmp->edit_type != EDIT_NONE)
1856 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1857 parmp->edit_type = EDIT_STDIN;
1858 read_cmd_fd = 2; /* read from stderr instead of stdin */
1859 }
1860 argv_idx = -1; /* skip to next argument */
1861 break;
1862
1863 case '-': /* "--" don't take any more option arguments */
1864 /* "--help" give help message */
1865 /* "--version" give version message */
1866 /* "--literal" take files literally */
1867 /* "--nofork" don't fork */
1868 /* "--noplugin[s]" skip plugins */
1869 /* "--cmd <cmd>" execute cmd before vimrc */
1870 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1871 usage();
1872 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1873 {
1874 Columns = 80; /* need to init Columns */
1875 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1876 list_version();
1877 msg_putchar('\n');
1878 msg_didout = FALSE;
1879 mch_exit(0);
1880 }
1881 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1882 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001883#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001884 parmp->literal = TRUE;
1885#endif
1886 }
1887 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1888 {
1889#ifdef FEAT_GUI
1890 gui.dofork = FALSE; /* don't fork() when starting GUI */
1891#endif
1892 }
1893 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1894 p_lpl = FALSE;
1895 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1896 {
1897 want_argument = TRUE;
1898 argv_idx += 3;
1899 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001900 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1901 {
1902 want_argument = TRUE;
1903 argv_idx += 11;
1904 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001905#ifdef FEAT_CLIENTSERVER
1906 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1907 ; /* already processed -- no arg */
1908 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1909 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1910 {
1911 /* already processed -- snatch the following arg */
1912 if (argc > 1)
1913 {
1914 --argc;
1915 ++argv;
1916 }
1917 }
1918#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001919#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1920# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001921 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001922# else
1923 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1924# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001925 {
1926 /* already processed -- snatch the following arg */
1927 if (argc > 1)
1928 {
1929 --argc;
1930 ++argv;
1931 }
1932 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001933#endif
1934#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001935 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1936 {
1937 /* already processed, skip */
1938 }
1939#endif
1940 else
1941 {
1942 if (argv[0][argv_idx])
1943 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1944 had_minmin = TRUE;
1945 }
1946 if (!want_argument)
1947 argv_idx = -1; /* skip to next argument */
1948 break;
1949
1950 case 'A': /* "-A" start in Arabic mode */
1951#ifdef FEAT_ARABIC
1952 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1953#else
1954 mch_errmsg(_(e_noarabic));
1955 mch_exit(2);
1956#endif
1957 break;
1958
1959 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001960 /* Needs to be effective before expanding file names, because
1961 * for Win32 this makes us edit a shortcut file itself,
1962 * instead of the file it links to. */
1963 set_options_bin(curbuf->b_p_bin, 1, 0);
1964 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001965 break;
1966
1967 case 'C': /* "-C" Compatible */
1968 change_compatible(TRUE);
1969 break;
1970
1971 case 'e': /* "-e" Ex mode */
1972 exmode_active = EXMODE_NORMAL;
1973 break;
1974
1975 case 'E': /* "-E" Improved Ex mode */
1976 exmode_active = EXMODE_VIM;
1977 break;
1978
1979 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1980 window directly, not with newcli */
1981#ifdef FEAT_GUI
1982 gui.dofork = FALSE; /* don't fork() when starting GUI */
1983#endif
1984 break;
1985
1986 case 'g': /* "-g" start GUI */
1987 main_start_gui();
1988 break;
1989
1990 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1991#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001992 p_fkmap = TRUE;
1993 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001994#else
1995 mch_errmsg(_(e_nofarsi));
1996 mch_exit(2);
1997#endif
1998 break;
1999
2000 case 'h': /* "-h" give help message */
2001#ifdef FEAT_GUI_GNOME
2002 /* Tell usage() to exit for "gvim". */
2003 gui.starting = FALSE;
2004#endif
2005 usage();
2006 break;
2007
2008 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2009#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002010 p_hkmap = TRUE;
2011 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002012#else
2013 mch_errmsg(_(e_nohebrew));
2014 mch_exit(2);
2015#endif
2016 break;
2017
2018 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2019#ifdef FEAT_LISP
2020 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2021 p_sm = TRUE;
2022#endif
2023 break;
2024
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002025 case 'M': /* "-M" no changes or writing of files */
2026 reset_modifiable();
2027 /* FALLTHROUGH */
2028
2029 case 'm': /* "-m" no writing of files */
2030 p_write = FALSE;
2031 break;
2032
2033 case 'y': /* "-y" easy mode */
2034#ifdef FEAT_GUI
2035 gui.starting = TRUE; /* start GUI a bit later */
2036#endif
2037 parmp->evim_mode = TRUE;
2038 break;
2039
2040 case 'N': /* "-N" Nocompatible */
2041 change_compatible(FALSE);
2042 break;
2043
2044 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002045#ifdef FEAT_NETBEANS_INTG
2046 /* checking for "-nb", netbeans parameters */
2047 if (argv[0][argv_idx] == 'b')
2048 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002049 netbeansArg = argv[0];
2050 argv_idx = -1; /* skip to next argument */
2051 }
2052 else
2053#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002054 parmp->no_swap_file = TRUE;
2055 break;
2056
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002057 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002058#ifdef TARGET_API_MAC_OSX
2059 /* For some reason on MacOS X, an argument like:
2060 -psn_0_10223617 is passed in when invoke from Finder
2061 or with the 'open' command */
2062 if (argv[0][argv_idx] == 's')
2063 {
2064 argv_idx = -1; /* bypass full -psn */
2065 main_start_gui();
2066 break;
2067 }
2068#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002069#ifdef FEAT_WINDOWS
2070 /* default is 0: open window for each file */
2071 parmp->window_count = get_number_arg((char_u *)argv[0],
2072 &argv_idx, 0);
2073 parmp->window_layout = WIN_TABS;
2074#endif
2075 break;
2076
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002077 case 'o': /* "-o[N]" open N horizontal split windows */
2078#ifdef FEAT_WINDOWS
2079 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002080 parmp->window_count = get_number_arg((char_u *)argv[0],
2081 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002082 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002083#endif
2084 break;
2085
2086 case 'O': /* "-O[N]" open N vertical split windows */
2087#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2088 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002089 parmp->window_count = get_number_arg((char_u *)argv[0],
2090 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002091 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002092#endif
2093 break;
2094
2095#ifdef FEAT_QUICKFIX
2096 case 'q': /* "-q" QuickFix mode */
2097 if (parmp->edit_type != EDIT_NONE)
2098 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2099 parmp->edit_type = EDIT_QF;
2100 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2101 {
2102 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2103 argv_idx = -1;
2104 }
2105 else if (argc > 1) /* "-q {errorfile}" */
2106 want_argument = TRUE;
2107 break;
2108#endif
2109
2110 case 'R': /* "-R" readonly mode */
2111 readonlymode = TRUE;
2112 curbuf->b_p_ro = TRUE;
2113 p_uc = 10000; /* don't update very often */
2114 break;
2115
2116 case 'r': /* "-r" recovery mode */
2117 case 'L': /* "-L" recovery mode */
2118 recoverymode = 1;
2119 break;
2120
2121 case 's':
2122 if (exmode_active) /* "-s" silent (batch) mode */
2123 silent_mode = TRUE;
2124 else /* "-s {scriptin}" read from script file */
2125 want_argument = TRUE;
2126 break;
2127
2128 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2129 if (parmp->edit_type != EDIT_NONE)
2130 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2131 parmp->edit_type = EDIT_TAG;
2132 if (argv[0][argv_idx]) /* "-t{tag}" */
2133 {
2134 parmp->tagname = (char_u *)argv[0] + argv_idx;
2135 argv_idx = -1;
2136 }
2137 else /* "-t {tag}" */
2138 want_argument = TRUE;
2139 break;
2140
2141#ifdef FEAT_EVAL
2142 case 'D': /* "-D" Debugging */
2143 parmp->use_debug_break_level = 9999;
2144 break;
2145#endif
2146#ifdef FEAT_DIFF
2147 case 'd': /* "-d" 'diff' */
2148# ifdef AMIGA
2149 /* check for "-dev {device}" */
2150 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2151 want_argument = TRUE;
2152 else
2153# endif
2154 parmp->diff_mode = TRUE;
2155 break;
2156#endif
2157 case 'V': /* "-V{N}" Verbose level */
2158 /* default is 10: a little bit verbose */
2159 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2160 if (argv[0][argv_idx] != NUL)
2161 {
2162 set_option_value((char_u *)"verbosefile", 0L,
2163 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002164 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002165 }
2166 break;
2167
2168 case 'v': /* "-v" Vi-mode (as if called "vi") */
2169 exmode_active = 0;
2170#ifdef FEAT_GUI
2171 gui.starting = FALSE; /* don't start GUI */
2172#endif
2173 break;
2174
2175 case 'w': /* "-w{number}" set window height */
2176 /* "-w {scriptout}" write to script */
2177 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2178 {
2179 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2180 set_option_value((char_u *)"window", n, NULL, 0);
2181 break;
2182 }
2183 want_argument = TRUE;
2184 break;
2185
2186#ifdef FEAT_CRYPT
2187 case 'x': /* "-x" encrypted reading/writing of files */
2188 parmp->ask_for_key = TRUE;
2189 break;
2190#endif
2191
2192 case 'X': /* "-X" don't connect to X server */
2193#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2194 x_no_connect = TRUE;
2195#endif
2196 break;
2197
2198 case 'Z': /* "-Z" restricted mode */
2199 restricted = TRUE;
2200 break;
2201
2202 case 'c': /* "-c{command}" or "-c {command}" execute
2203 command */
2204 if (argv[0][argv_idx] != NUL)
2205 {
2206 if (parmp->n_commands >= MAX_ARG_CMDS)
2207 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002208 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2209 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002210 argv_idx = -1;
2211 break;
2212 }
2213 /*FALLTHROUGH*/
2214 case 'S': /* "-S {file}" execute Vim script */
2215 case 'i': /* "-i {viminfo}" use for viminfo */
2216#ifndef FEAT_DIFF
2217 case 'd': /* "-d {device}" device (for Amiga) */
2218#endif
2219 case 'T': /* "-T {terminal}" terminal name */
2220 case 'u': /* "-u {vimrc}" vim inits file */
2221 case 'U': /* "-U {gvimrc}" gvim inits file */
2222 case 'W': /* "-W {scriptout}" overwrite */
2223#ifdef FEAT_GUI_W32
2224 case 'P': /* "-P {parent title}" MDI parent */
2225#endif
2226 want_argument = TRUE;
2227 break;
2228
2229 default:
2230 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2231 }
2232
2233 /*
2234 * Handle option arguments with argument.
2235 */
2236 if (want_argument)
2237 {
2238 /*
2239 * Check for garbage immediately after the option letter.
2240 */
2241 if (argv[0][argv_idx] != NUL)
2242 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2243
2244 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002245 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002246 mainerr_arg_missing((char_u *)argv[0]);
2247 ++argv;
2248 argv_idx = -1;
2249
2250 switch (c)
2251 {
2252 case 'c': /* "-c {command}" execute command */
2253 case 'S': /* "-S {file}" execute Vim script */
2254 if (parmp->n_commands >= MAX_ARG_CMDS)
2255 mainerr(ME_EXTRA_CMD, NULL);
2256 if (c == 'S')
2257 {
2258 char *a;
2259
2260 if (argc < 1)
2261 /* "-S" without argument: use default session file
2262 * name. */
2263 a = SESSION_FILE;
2264 else if (argv[0][0] == '-')
2265 {
2266 /* "-S" followed by another option: use default
2267 * session file name. */
2268 a = SESSION_FILE;
2269 ++argc;
2270 --argv;
2271 }
2272 else
2273 a = argv[0];
2274 p = alloc((unsigned)(STRLEN(a) + 4));
2275 if (p == NULL)
2276 mch_exit(2);
2277 sprintf((char *)p, "so %s", a);
2278 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2279 parmp->commands[parmp->n_commands++] = p;
2280 }
2281 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002282 parmp->commands[parmp->n_commands++] =
2283 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002284 break;
2285
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002286 case '-':
2287 if (argv[-1][2] == 'c')
2288 {
2289 /* "--cmd {command}" execute command */
2290 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2291 mainerr(ME_EXTRA_CMD, NULL);
2292 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002293 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002294 }
2295 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002296 break;
2297
2298 /* case 'd': -d {device} is handled in mch_check_win() for the
2299 * Amiga */
2300
2301#ifdef FEAT_QUICKFIX
2302 case 'q': /* "-q {errorfile}" QuickFix mode */
2303 parmp->use_ef = (char_u *)argv[0];
2304 break;
2305#endif
2306
2307 case 'i': /* "-i {viminfo}" use for viminfo */
2308 use_viminfo = (char_u *)argv[0];
2309 break;
2310
2311 case 's': /* "-s {scriptin}" read from script file */
2312 if (scriptin[0] != NULL)
2313 {
2314scripterror:
2315 mch_errmsg(_("Attempt to open script file again: \""));
2316 mch_errmsg(argv[-1]);
2317 mch_errmsg(" ");
2318 mch_errmsg(argv[0]);
2319 mch_errmsg("\"\n");
2320 mch_exit(2);
2321 }
2322 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2323 {
2324 mch_errmsg(_("Cannot open for reading: \""));
2325 mch_errmsg(argv[0]);
2326 mch_errmsg("\"\n");
2327 mch_exit(2);
2328 }
2329 if (save_typebuf() == FAIL)
2330 mch_exit(2); /* out of memory */
2331 break;
2332
2333 case 't': /* "-t {tag}" */
2334 parmp->tagname = (char_u *)argv[0];
2335 break;
2336
2337 case 'T': /* "-T {terminal}" terminal name */
2338 /*
2339 * The -T term argument is always available and when
2340 * HAVE_TERMLIB is supported it overrides the environment
2341 * variable TERM.
2342 */
2343#ifdef FEAT_GUI
2344 if (term_is_gui((char_u *)argv[0]))
2345 gui.starting = TRUE; /* start GUI a bit later */
2346 else
2347#endif
2348 parmp->term = (char_u *)argv[0];
2349 break;
2350
2351 case 'u': /* "-u {vimrc}" vim inits file */
2352 parmp->use_vimrc = (char_u *)argv[0];
2353 break;
2354
2355 case 'U': /* "-U {gvimrc}" gvim inits file */
2356#ifdef FEAT_GUI
2357 use_gvimrc = (char_u *)argv[0];
2358#endif
2359 break;
2360
2361 case 'w': /* "-w {nr}" 'window' value */
2362 /* "-w {scriptout}" append to script file */
2363 if (vim_isdigit(*((char_u *)argv[0])))
2364 {
2365 argv_idx = 0;
2366 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2367 set_option_value((char_u *)"window", n, NULL, 0);
2368 argv_idx = -1;
2369 break;
2370 }
2371 /*FALLTHROUGH*/
2372 case 'W': /* "-W {scriptout}" overwrite script file */
2373 if (scriptout != NULL)
2374 goto scripterror;
2375 if ((scriptout = mch_fopen(argv[0],
2376 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2377 {
2378 mch_errmsg(_("Cannot open for script output: \""));
2379 mch_errmsg(argv[0]);
2380 mch_errmsg("\"\n");
2381 mch_exit(2);
2382 }
2383 break;
2384
2385#ifdef FEAT_GUI_W32
2386 case 'P': /* "-P {parent title}" MDI parent */
2387 gui_mch_set_parent(argv[0]);
2388 break;
2389#endif
2390 }
2391 }
2392 }
2393
2394 /*
2395 * File name argument.
2396 */
2397 else
2398 {
2399 argv_idx = -1; /* skip to next argument */
2400
2401 /* Check for only one type of editing. */
2402 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2403 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2404 parmp->edit_type = EDIT_FILE;
2405
2406#ifdef MSWIN
2407 /* Remember if the argument was a full path before changing
2408 * slashes to backslashes. */
2409 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2410 parmp->full_path = TRUE;
2411#endif
2412
2413 /* Add the file to the global argument list. */
2414 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2415 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2416 mch_exit(2);
2417#ifdef FEAT_DIFF
2418 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2419 && !mch_isdir(alist_name(&GARGLIST[0])))
2420 {
2421 char_u *r;
2422
2423 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2424 if (r != NULL)
2425 {
2426 vim_free(p);
2427 p = r;
2428 }
2429 }
2430#endif
2431#if defined(__CYGWIN32__) && !defined(WIN32)
2432 /*
2433 * If vim is invoked by non-Cygwin tools, convert away any
2434 * DOS paths, so things like .swp files are created correctly.
2435 * Look for evidence of non-Cygwin paths before we bother.
2436 * This is only for when using the Unix files.
2437 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002438 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002439 {
2440 char posix_path[PATH_MAX];
2441
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002442# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2443 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2444# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002445 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002446# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002447 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002448 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002449 if (p == NULL)
2450 mch_exit(2);
2451 }
2452#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002453
2454#ifdef USE_FNAME_CASE
2455 /* Make the case of the file name match the actual file. */
2456 fname_case(p, 0);
2457#endif
2458
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002459 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002460#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002461 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462#else
2463 2 /* add buffer number now and use curbuf */
2464#endif
2465 );
2466
2467#if defined(FEAT_MBYTE) && defined(WIN32)
2468 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469 /* Remember this argument has been added to the argument list.
2470 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002471 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002472# ifdef FEAT_DIFF
2473 parmp->diff_mode
2474# else
2475 FALSE
2476# endif
2477 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002478 }
2479#endif
2480 }
2481
2482 /*
2483 * If there are no more letters after the current "-", go to next
2484 * argument. argv_idx is set to -1 when the current argument is to be
2485 * skipped.
2486 */
2487 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2488 {
2489 --argc;
2490 ++argv;
2491 argv_idx = 1;
2492 }
2493 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002494
2495#ifdef FEAT_EVAL
2496 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2497 * one. */
2498 if (parmp->n_commands > 0)
2499 {
2500 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2501 if (p != NULL)
2502 {
2503 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2504 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2505 vim_free(p);
2506 }
2507 }
2508#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002509}
2510
2511/*
2512 * Print a warning if stdout is not a terminal.
2513 * When starting in Ex mode and commands come from a file, set Silent mode.
2514 */
2515 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002516check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002517{
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
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002562read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002563{
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
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002596create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002597{
2598#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002599 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002600 int done = 0;
2601
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002602 /*
2603 * Create the number of windows that was requested.
2604 */
2605 if (parmp->window_count == -1) /* was not set */
2606 parmp->window_count = 1;
2607 if (parmp->window_count == 0)
2608 parmp->window_count = GARGCOUNT;
2609 if (parmp->window_count > 1)
2610 {
2611 /* Don't change the windows if there was a command in .vimrc that
2612 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002613 if (parmp->window_layout == 0)
2614 parmp->window_layout = WIN_HOR;
2615 if (parmp->window_layout == WIN_TABS)
2616 {
2617 parmp->window_count = make_tabpages(parmp->window_count);
2618 TIME_MSG("making tab pages");
2619 }
2620 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002621 {
2622 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002623 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002624 TIME_MSG("making windows");
2625 }
2626 else
2627 parmp->window_count = win_count();
2628 }
2629 else
2630 parmp->window_count = 1;
2631#endif
2632
2633 if (recoverymode) /* do recover */
2634 {
2635 msg_scroll = TRUE; /* scroll message up */
2636 ml_recover();
2637 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2638 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002639 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002640 }
2641 else
2642 {
2643 /*
2644 * Open a buffer for windows that don't have one yet.
2645 * Commands in the .vimrc might have loaded a file or split the window.
2646 * Watch out for autocommands that delete a window.
2647 */
2648#ifdef FEAT_AUTOCMD
2649 /*
2650 * Don't execute Win/Buf Enter/Leave autocommands here
2651 */
2652 ++autocmd_no_enter;
2653 ++autocmd_no_leave;
2654#endif
2655#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002656 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002657 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002658 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002659 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002660 {
2661 if (parmp->window_layout == WIN_TABS)
2662 goto_tabpage(1);
2663 else
2664 curwin = firstwin;
2665 }
2666 else if (parmp->window_layout == WIN_TABS)
2667 {
2668 if (curtab->tp_next == NULL)
2669 break;
2670 goto_tabpage(0);
2671 }
2672 else
2673 {
2674 if (curwin->w_next == NULL)
2675 break;
2676 curwin = curwin->w_next;
2677 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002678 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002679#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002680 curbuf = curwin->w_buffer;
2681 if (curbuf->b_ml.ml_mfp == NULL)
2682 {
2683#ifdef FEAT_FOLDING
2684 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2685 if (p_fdls >= 0)
2686 curwin->w_p_fdl = p_fdls;
2687#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002688#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002689 /* When getting the ATTENTION prompt here, use a dialog */
2690 swap_exists_action = SEA_DIALOG;
2691#endif
2692 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002693
2694 /* create memfile, read file */
2695 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002696
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002697#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002698 if (swap_exists_action == SEA_QUIT)
2699 {
2700 if (got_int || only_one_window())
2701 {
2702 /* abort selected or quit and only one window */
2703 did_emsg = FALSE; /* avoid hit-enter prompt */
2704 getout(1);
2705 }
2706 /* We can't close the window, it would disturb what
2707 * happens next. Clear the file name and set the arg
2708 * index to -1 to delete it later. */
2709 setfname(curbuf, NULL, NULL, FALSE);
2710 curwin->w_arg_idx = -1;
2711 swap_exists_action = SEA_NONE;
2712 }
2713 else
2714 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002715#endif
2716#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002717 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718#endif
2719 }
2720#ifdef FEAT_WINDOWS
2721 ui_breakcheck();
2722 if (got_int)
2723 {
2724 (void)vgetc(); /* only break the file loading, not the rest */
2725 break;
2726 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002728#endif
2729#ifdef FEAT_WINDOWS
2730 if (parmp->window_layout == WIN_TABS)
2731 goto_tabpage(1);
2732 else
2733 curwin = firstwin;
2734 curbuf = curwin->w_buffer;
2735#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002736#ifdef FEAT_AUTOCMD
2737 --autocmd_no_enter;
2738 --autocmd_no_leave;
2739#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002740 }
2741}
2742
2743#ifdef FEAT_WINDOWS
2744 /*
2745 * If opened more than one window, start editing files in the other
2746 * windows. make_windows() has already opened the windows.
2747 */
2748 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002749edit_buffers(
2750 mparm_T *parmp,
2751 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002752{
2753 int arg_idx; /* index in argument list */
2754 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002755 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002756 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757
2758# ifdef FEAT_AUTOCMD
2759 /*
2760 * Don't execute Win/Buf Enter/Leave autocommands here
2761 */
2762 ++autocmd_no_enter;
2763 ++autocmd_no_leave;
2764# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002765
2766 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2767 if (curwin->w_arg_idx == -1)
2768 {
2769 win_close(curwin, TRUE);
2770 advance = FALSE;
2771 }
2772
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002773 arg_idx = 1;
2774 for (i = 1; i < parmp->window_count; ++i)
2775 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002776 if (cwd != NULL)
2777 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002778 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2779 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002780 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002781 ++arg_idx;
2782 win_close(curwin, TRUE);
2783 advance = FALSE;
2784 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002785 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002786
Bram Moolenaar84212822006-11-07 21:59:47 +00002787 if (advance)
2788 {
2789 if (parmp->window_layout == WIN_TABS)
2790 {
2791 if (curtab->tp_next == NULL) /* just checking */
2792 break;
2793 goto_tabpage(0);
2794 }
2795 else
2796 {
2797 if (curwin->w_next == NULL) /* just checking */
2798 break;
2799 win_enter(curwin->w_next, FALSE);
2800 }
2801 }
2802 advance = TRUE;
2803
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002804 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002805 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002806 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2807 {
2808 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002809 /* Edit file from arg list, if there is one. When "Quit" selected
2810 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002811# ifdef HAS_SWAP_EXISTS_ACTION
2812 swap_exists_did_quit = FALSE;
2813# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002814 (void)do_ecmd(0, arg_idx < GARGCOUNT
2815 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002816 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002817# ifdef HAS_SWAP_EXISTS_ACTION
2818 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002819 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002820 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002821 if (got_int || only_one_window())
2822 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002823 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002824 did_emsg = FALSE; /* avoid hit-enter prompt */
2825 getout(1);
2826 }
2827 win_close(curwin, TRUE);
2828 advance = FALSE;
2829 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002830# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002831 if (arg_idx == GARGCOUNT - 1)
2832 arg_had_last = TRUE;
2833 ++arg_idx;
2834 }
2835 ui_breakcheck();
2836 if (got_int)
2837 {
2838 (void)vgetc(); /* only break the file loading, not the rest */
2839 break;
2840 }
2841 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002842
2843 if (parmp->window_layout == WIN_TABS)
2844 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002845# ifdef FEAT_AUTOCMD
2846 --autocmd_no_enter;
2847# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002848
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002849 /* make the first window the current window */
2850 win = firstwin;
2851#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2852 /* Avoid making a preview window the current window. */
2853 while (win->w_p_pvw)
2854 {
2855 win = win->w_next;
2856 if (win == NULL)
2857 {
2858 win = firstwin;
2859 break;
2860 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002861 }
2862#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002863 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002864
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002865# ifdef FEAT_AUTOCMD
2866 --autocmd_no_leave;
2867# endif
2868 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002869 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002870 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2871}
2872#endif /* FEAT_WINDOWS */
2873
2874/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002875 * Execute the commands from --cmd arguments "cmds[cnt]".
2876 */
2877 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002878exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002879{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002880 char_u **cmds = parmp->pre_commands;
2881 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002882 int i;
2883
2884 if (cnt > 0)
2885 {
2886 curwin->w_cursor.lnum = 0; /* just in case.. */
2887 sourcing_name = (char_u *)_("pre-vimrc command line");
2888# ifdef FEAT_EVAL
2889 current_SID = SID_CMDARG;
2890# endif
2891 for (i = 0; i < cnt; ++i)
2892 do_cmdline_cmd(cmds[i]);
2893 sourcing_name = NULL;
2894# ifdef FEAT_EVAL
2895 current_SID = 0;
2896# endif
2897 TIME_MSG("--cmd commands");
2898 }
2899}
2900
2901/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002902 * Execute "+", "-c" and "-S" arguments.
2903 */
2904 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002905exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002906{
2907 int i;
2908
2909 /*
2910 * We start commands on line 0, make "vim +/pat file" match a
2911 * pattern on line 1. But don't move the cursor when an autocommand
2912 * with g`" was used.
2913 */
2914 msg_scroll = TRUE;
2915 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2916 curwin->w_cursor.lnum = 0;
2917 sourcing_name = (char_u *)"command line";
2918#ifdef FEAT_EVAL
2919 current_SID = SID_CARG;
2920#endif
2921 for (i = 0; i < parmp->n_commands; ++i)
2922 {
2923 do_cmdline_cmd(parmp->commands[i]);
2924 if (parmp->cmds_tofree[i])
2925 vim_free(parmp->commands[i]);
2926 }
2927 sourcing_name = NULL;
2928#ifdef FEAT_EVAL
2929 current_SID = 0;
2930#endif
2931 if (curwin->w_cursor.lnum == 0)
2932 curwin->w_cursor.lnum = 1;
2933
2934 if (!exmode_active)
2935 msg_scroll = FALSE;
2936
2937#ifdef FEAT_QUICKFIX
2938 /* When started with "-q errorfile" jump to first error again. */
2939 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002940 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002941#endif
2942 TIME_MSG("executing command arguments");
2943}
2944
2945/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002946 * Source startup scripts.
2947 */
2948 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002949source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002950{
2951 int i;
2952
2953 /*
2954 * For "evim" source evim.vim first of all, so that the user can overrule
2955 * any things he doesn't like.
2956 */
2957 if (parmp->evim_mode)
2958 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002959 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002960 TIME_MSG("source evim file");
2961 }
2962
2963 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002964 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002965 * nothing else.
2966 */
2967 if (parmp->use_vimrc != NULL)
2968 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002969 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2970 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002971 {
2972#ifdef FEAT_GUI
2973 if (use_gvimrc == NULL) /* don't load gvimrc either */
2974 use_gvimrc = parmp->use_vimrc;
2975#endif
2976 if (parmp->use_vimrc[2] == 'N')
2977 p_lpl = FALSE; /* don't load plugins either */
2978 }
2979 else
2980 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002981 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002982 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2983 }
2984 }
2985 else if (!silent_mode)
2986 {
2987#ifdef AMIGA
2988 struct Process *proc = (struct Process *)FindTask(0L);
2989 APTR save_winptr = proc->pr_WindowPtr;
2990
2991 /* Avoid a requester here for a volume that doesn't exist. */
2992 proc->pr_WindowPtr = (APTR)-1L;
2993#endif
2994
2995 /*
2996 * Get system wide defaults, if the file name is defined.
2997 */
2998#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002999 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003000#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00003001#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003002 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00003003#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003004
3005 /*
3006 * Try to read initialization commands from the following places:
3007 * - environment variable VIMINIT
3008 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3009 * - second user vimrc file ($VIM/.vimrc for Dos)
3010 * - environment variable EXINIT
3011 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3012 * - second user exrc file ($VIM/.exrc for Dos)
3013 * The first that exists is used, the rest is ignored.
3014 */
3015 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3016 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003017 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003018#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003019 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3020 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003021#endif
3022#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003023 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3024 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003025#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003026#ifdef USR_VIMRC_FILE4
3027 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3028 DOSO_VIMRC) == FAIL
3029#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003030 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003031 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003032 {
3033#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003034 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003035#endif
3036 }
3037 }
3038
3039 /*
3040 * Read initialization commands from ".vimrc" or ".exrc" in current
3041 * directory. This is only done if the 'exrc' option is set.
3042 * Because of security reasons we disallow shell and write commands
3043 * now, except for unix if the file is owned by the user or 'secure'
3044 * option has been reset in environment of global ".exrc" or ".vimrc".
3045 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3046 * SYS_VIMRC_FILE.
3047 */
3048 if (p_exrc)
3049 {
3050#if defined(UNIX) || defined(VMS)
3051 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3052 if (!file_owned(VIMRC_FILE))
3053#endif
3054 secure = p_secure;
3055
3056 i = FAIL;
3057 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3058 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3059#ifdef USR_VIMRC_FILE2
3060 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3061 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3062#endif
3063#ifdef USR_VIMRC_FILE3
3064 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3065 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3066#endif
3067#ifdef SYS_VIMRC_FILE
3068 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3069 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3070#endif
3071 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003072 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003073
3074 if (i == FAIL)
3075 {
3076#if defined(UNIX) || defined(VMS)
3077 /* if ".exrc" is not owned by user set 'secure' mode */
3078 if (!file_owned(EXRC_FILE))
3079 secure = p_secure;
3080 else
3081 secure = 0;
3082#endif
3083 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3084 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3085#ifdef USR_EXRC_FILE2
3086 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3087 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3088#endif
3089 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003090 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003091 }
3092 }
3093 if (secure == 2)
3094 need_wait_return = TRUE;
3095 secure = 0;
3096#ifdef AMIGA
3097 proc->pr_WindowPtr = save_winptr;
3098#endif
3099 }
3100 TIME_MSG("sourcing vimrc file(s)");
3101}
3102
3103/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003104 * Setup to start using the GUI. Exit with an error when not available.
3105 */
3106 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003107main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003108{
3109#ifdef FEAT_GUI
3110 gui.starting = TRUE; /* start GUI a bit later */
3111#else
3112 mch_errmsg(_(e_nogvim));
3113 mch_errmsg("\n");
3114 mch_exit(2);
3115#endif
3116}
3117
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003118#endif /* NO_VIM_MAIN */
3119
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003120/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003121 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003122 * Returns FAIL if the environment variable was not executed, OK otherwise.
3123 */
3124 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003125process_env(
3126 char_u *env,
3127 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003128{
3129 char_u *initstr;
3130 char_u *save_sourcing_name;
3131 linenr_T save_sourcing_lnum;
3132#ifdef FEAT_EVAL
3133 scid_T save_sid;
3134#endif
3135
3136 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3137 {
3138 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003139 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003140 save_sourcing_name = sourcing_name;
3141 save_sourcing_lnum = sourcing_lnum;
3142 sourcing_name = env;
3143 sourcing_lnum = 0;
3144#ifdef FEAT_EVAL
3145 save_sid = current_SID;
3146 current_SID = SID_ENV;
3147#endif
3148 do_cmdline_cmd(initstr);
3149 sourcing_name = save_sourcing_name;
3150 sourcing_lnum = save_sourcing_lnum;
3151#ifdef FEAT_EVAL
3152 current_SID = save_sid;;
3153#endif
3154 return OK;
3155 }
3156 return FAIL;
3157}
3158
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003159#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003160/*
3161 * Return TRUE if we are certain the user owns the file "fname".
3162 * Used for ".vimrc" and ".exrc".
3163 * Use both stat() and lstat() for extra security.
3164 */
3165 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003166file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003167{
3168 struct stat s;
3169# ifdef UNIX
3170 uid_t uid = getuid();
3171# else /* VMS */
3172 uid_t uid = ((getgid() << 16) | getuid());
3173# endif
3174
3175 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3176# ifdef HAVE_LSTAT
3177 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3178# endif
3179 );
3180}
3181#endif
3182
3183/*
3184 * Give an error message main_errors["n"] and exit.
3185 */
3186 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003187mainerr(
3188 int n, /* one of the ME_ defines */
3189 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003190{
3191#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3192 reset_signals(); /* kill us with CTRL-C here, if you like */
3193#endif
3194
3195 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003196 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003197 mch_errmsg(_(main_errors[n]));
3198 if (str != NULL)
3199 {
3200 mch_errmsg(": \"");
3201 mch_errmsg((char *)str);
3202 mch_errmsg("\"");
3203 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003204 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003205
3206 mch_exit(1);
3207}
3208
3209 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003210mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003211{
3212 mainerr(ME_ARG_MISSING, str);
3213}
3214
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003215#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003216/*
3217 * print a message with three spaces prepended and '\n' appended.
3218 */
3219 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003220main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003221{
3222 mch_msg(" ");
3223 mch_msg(s);
3224 mch_msg("\n");
3225}
3226
3227/*
3228 * Print messages for "vim -h" or "vim --help" and exit.
3229 */
3230 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003231usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003232{
3233 int i;
3234 static char *(use[]) =
3235 {
3236 N_("[file ..] edit specified file(s)"),
3237 N_("- read text from stdin"),
3238 N_("-t tag edit file where tag is defined"),
3239#ifdef FEAT_QUICKFIX
3240 N_("-q [errorfile] edit file with first error")
3241#endif
3242 };
3243
3244#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3245 reset_signals(); /* kill us with CTRL-C here, if you like */
3246#endif
3247
3248 mch_msg(longVersion);
3249 mch_msg(_("\n\nusage:"));
3250 for (i = 0; ; ++i)
3251 {
3252 mch_msg(_(" vim [arguments] "));
3253 mch_msg(_(use[i]));
3254 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3255 break;
3256 mch_msg(_("\n or:"));
3257 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003258#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003259 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003260#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003261
3262 mch_msg(_("\n\nArguments:\n"));
3263 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003264#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265 main_msg(_("--literal\t\tDon't expand wildcards"));
3266#endif
3267#ifdef FEAT_OLE
3268 main_msg(_("-register\t\tRegister this gvim for OLE"));
3269 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3270#endif
3271#ifdef FEAT_GUI
3272 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3273 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3274#endif
3275 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3276 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003277 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003278 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3279#ifdef FEAT_DIFF
3280 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3281#endif
3282 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3283 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3284 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3285 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3286 main_msg(_("-M\t\t\tModifications in text not allowed"));
3287 main_msg(_("-b\t\t\tBinary mode"));
3288#ifdef FEAT_LISP
3289 main_msg(_("-l\t\t\tLisp mode"));
3290#endif
3291 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3292 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003293 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3294#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003295 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003296#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003297 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3298 main_msg(_("-r\t\t\tList swap files and exit"));
3299 main_msg(_("-r (with file name)\tRecover crashed session"));
3300 main_msg(_("-L\t\t\tSame as -r"));
3301#ifdef AMIGA
3302 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3303 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3304#endif
3305#ifdef FEAT_ARABIC
3306 main_msg(_("-A\t\t\tstart in Arabic mode"));
3307#endif
3308#ifdef FEAT_RIGHTLEFT
3309 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3310#endif
3311#ifdef FEAT_FKMAP
3312 main_msg(_("-F\t\t\tStart in Farsi mode"));
3313#endif
3314 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3315 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3316#ifdef FEAT_GUI
3317 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3318#endif
3319 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003320#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003321 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003322 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3323 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003324#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003325 main_msg(_("+\t\t\tStart at end of file"));
3326 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003327 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3329 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3330 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3331 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3332 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3333#ifdef FEAT_CRYPT
3334 main_msg(_("-x\t\t\tEdit encrypted files"));
3335#endif
3336#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3337# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3338 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3339# endif
3340 main_msg(_("-X\t\t\tDo not connect to X server"));
3341#endif
3342#ifdef FEAT_CLIENTSERVER
3343 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3344 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3345 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3346 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003347# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003348 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003349# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3351 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3352 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3353 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3354#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003355#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003356 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003357#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003358#ifdef FEAT_VIMINFO
3359 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3360#endif
3361 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3362 main_msg(_("--version\t\tPrint version information and exit"));
3363
3364#ifdef FEAT_GUI_X11
3365# ifdef FEAT_GUI_MOTIF
3366 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3367# else
3368# ifdef FEAT_GUI_ATHENA
3369# ifdef FEAT_GUI_NEXTAW
3370 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3371# else
3372 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3373# endif
3374# endif
3375# endif
3376 main_msg(_("-display <display>\tRun vim on <display>"));
3377 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003378 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3379 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3380 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3381 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3382 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3383 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3384 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3385 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3386# ifdef FEAT_GUI_ATHENA
3387 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3388# endif
3389 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3390 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3391 main_msg(_("-xrm <resource>\tSet the specified resource"));
3392#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393#ifdef FEAT_GUI_GTK
3394 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3395 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3396 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3397 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3398 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003399 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003400 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003401 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003402#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403#ifdef FEAT_GUI_W32
3404 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003405 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406#endif
3407
3408#ifdef FEAT_GUI_GNOME
3409 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3410 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003411 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003412 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003413 gui.dofork = FALSE;
3414 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003415 else
3416#endif
3417 mch_exit(0);
3418}
3419
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003420#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003421/*
3422 * Check the result of the ATTENTION dialog:
3423 * When "Quit" selected, exit Vim.
3424 * When "Recover" selected, recover the file.
3425 */
3426 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003427check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428{
3429 if (swap_exists_action == SEA_QUIT)
3430 getout(1);
3431 handle_swap_exists(NULL);
3432}
3433#endif
3434
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003435#endif
3436
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003437#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003438static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003439
3440static struct timeval prev_timeval;
3441
Bram Moolenaar3f269672009-11-03 11:11:11 +00003442# ifdef WIN3264
3443/*
3444 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3445 */
3446 static int
3447gettimeofday(struct timeval *tv, char *dummy)
3448{
3449 long t = clock();
3450 tv->tv_sec = t / CLOCKS_PER_SEC;
3451 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3452 return 0;
3453}
3454# endif
3455
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003456/*
3457 * Save the previous time before doing something that could nest.
3458 * set "*tv_rel" to the time elapsed so far.
3459 */
3460 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003461time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003462{
3463 *((struct timeval *)tv_rel) = prev_timeval;
3464 gettimeofday(&prev_timeval, NULL);
3465 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3466 - ((struct timeval *)tv_rel)->tv_usec;
3467 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3468 - ((struct timeval *)tv_rel)->tv_sec;
3469 if (((struct timeval *)tv_rel)->tv_usec < 0)
3470 {
3471 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3472 --((struct timeval *)tv_rel)->tv_sec;
3473 }
3474 *(struct timeval *)tv_start = prev_timeval;
3475}
3476
3477/*
3478 * Compute the previous time after doing something that could nest.
3479 * Subtract "*tp" from prev_timeval;
3480 * Note: The arguments are (void *) to avoid trouble with systems that don't
3481 * have struct timeval.
3482 */
3483 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003484time_pop(
3485 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003486{
3487 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3488 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3489 if (prev_timeval.tv_usec < 0)
3490 {
3491 prev_timeval.tv_usec += 1000000;
3492 --prev_timeval.tv_sec;
3493 }
3494}
3495
3496 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003497time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003498{
3499 long usec;
3500 long msec;
3501
3502 usec = now->tv_usec - then->tv_usec;
3503 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3504 usec = usec % 1000L;
3505 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3506}
3507
3508 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003509time_msg(
3510 char *mesg,
3511 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003512 (struct timeval *) */
3513{
3514 static struct timeval start;
3515 struct timeval now;
3516
3517 if (time_fd != NULL)
3518 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003519 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003520 {
3521 gettimeofday(&start, NULL);
3522 prev_timeval = start;
3523 fprintf(time_fd, "\n\ntimes in msec\n");
3524 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3525 fprintf(time_fd, " clock elapsed: other lines\n\n");
3526 }
3527 gettimeofday(&now, NULL);
3528 time_diff(&start, &now);
3529 if (((struct timeval *)tv_start) != NULL)
3530 {
3531 fprintf(time_fd, " ");
3532 time_diff(((struct timeval *)tv_start), &now);
3533 }
3534 fprintf(time_fd, " ");
3535 time_diff(&prev_timeval, &now);
3536 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003537 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003538 }
3539}
3540
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541#endif
3542
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003543#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003544
3545/*
3546 * Common code for the X command server and the Win32 command server.
3547 */
3548
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003549static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003550
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003551/*
3552 * Do the client-server stuff, unless "--servername ''" was used.
3553 */
3554 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003555exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003556{
3557 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3558 {
3559# ifdef WIN32
3560 /* Initialise the client/server messaging infrastructure. */
3561 serverInitMessaging();
3562# endif
3563
3564 /*
3565 * When a command server argument was found, execute it. This may
3566 * exit Vim when it was successful. Otherwise it's executed further
3567 * on. Remember the encoding used here in "serverStrEnc".
3568 */
3569 if (parmp->serverArg)
3570 {
3571 cmdsrv_main(&parmp->argc, parmp->argv,
3572 parmp->serverName_arg, &parmp->serverStr);
3573# ifdef FEAT_MBYTE
3574 parmp->serverStrEnc = vim_strsave(p_enc);
3575# endif
3576 }
3577
3578 /* If we're still running, get the name to register ourselves.
3579 * On Win32 can register right now, for X11 need to setup the
3580 * clipboard first, it's further down. */
3581 parmp->servername = serverMakeName(parmp->serverName_arg,
3582 parmp->argv[0]);
3583# ifdef WIN32
3584 if (parmp->servername != NULL)
3585 {
3586 serverSetName(parmp->servername);
3587 vim_free(parmp->servername);
3588 }
3589# endif
3590 }
3591}
3592
3593/*
3594 * Prepare for running as a Vim server.
3595 */
3596 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003597prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003598{
3599# if defined(FEAT_X11)
3600 /*
3601 * Register for remote command execution with :serversend and --remote
3602 * unless there was a -X or a --servername '' on the command line.
3603 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003604 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003605 */
3606 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3607# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003608 (gui.in_use
3609# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003610 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003611# endif
3612 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003613# endif
3614 parmp->serverName_arg != NULL))
3615 {
3616 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3617 vim_free(parmp->servername);
3618 TIME_MSG("register server name");
3619 }
3620 else
3621 serverDelayedStartName = parmp->servername;
3622# endif
3623
3624 /*
3625 * Execute command ourselves if we're here because the send failed (or
3626 * else we would have exited above).
3627 */
3628 if (parmp->serverStr != NULL)
3629 {
3630 char_u *p;
3631
3632 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3633 parmp->serverStr, &p));
3634 vim_free(p);
3635 }
3636}
3637
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003638 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003639cmdsrv_main(
3640 int *argc,
3641 char **argv,
3642 char_u *serverName_arg,
3643 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003644{
3645 char_u *res;
3646 int i;
3647 char_u *sname;
3648 int ret;
3649 int didone = FALSE;
3650 int exiterr = 0;
3651 char **newArgV = argv + 1;
3652 int newArgC = 1,
3653 Argc = *argc;
3654 int argtype;
3655#define ARGTYPE_OTHER 0
3656#define ARGTYPE_EDIT 1
3657#define ARGTYPE_EDIT_WAIT 2
3658#define ARGTYPE_SEND 3
3659 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003660 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003661# ifndef FEAT_X11
3662 HWND srv;
3663# else
3664 Window srv;
3665
3666 setup_term_clip();
3667# endif
3668
3669 sname = serverMakeName(serverName_arg, argv[0]);
3670 if (sname == NULL)
3671 return;
3672
3673 /*
3674 * Execute the command server related arguments and remove them
3675 * from the argc/argv array; We may have to return into main()
3676 */
3677 for (i = 1; i < Argc; i++)
3678 {
3679 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003680 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003681 {
3682 for (; i < *argc; i++)
3683 {
3684 *newArgV++ = argv[i];
3685 newArgC++;
3686 }
3687 break;
3688 }
3689
Bram Moolenaareb94e552006-03-11 21:35:11 +00003690 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003691 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003692 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3693 {
3694 char *p = argv[i] + 8;
3695
3696 argtype = ARGTYPE_EDIT;
3697 while (*p != NUL)
3698 {
3699 if (STRNICMP(p, "-wait", 5) == 0)
3700 {
3701 argtype = ARGTYPE_EDIT_WAIT;
3702 p += 5;
3703 }
3704 else if (STRNICMP(p, "-silent", 7) == 0)
3705 {
3706 silent = TRUE;
3707 p += 7;
3708 }
3709 else if (STRNICMP(p, "-tab", 4) == 0)
3710 {
3711 tabs = TRUE;
3712 p += 4;
3713 }
3714 else
3715 {
3716 argtype = ARGTYPE_OTHER;
3717 break;
3718 }
3719 }
3720 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003721 else
3722 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003723
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003724 if (argtype != ARGTYPE_OTHER)
3725 {
3726 if (i == *argc - 1)
3727 mainerr_arg_missing((char_u *)argv[i]);
3728 if (argtype == ARGTYPE_SEND)
3729 {
3730 *serverStr = (char_u *)argv[i + 1];
3731 i++;
3732 }
3733 else
3734 {
3735 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003736 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003737 if (*serverStr == NULL)
3738 {
3739 /* Probably out of memory, exit. */
3740 didone = TRUE;
3741 exiterr = 1;
3742 break;
3743 }
3744 Argc = i;
3745 }
3746# ifdef FEAT_X11
3747 if (xterm_dpy == NULL)
3748 {
3749 mch_errmsg(_("No display"));
3750 ret = -1;
3751 }
3752 else
3753 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3754 NULL, &srv, 0, 0, silent);
3755# else
3756 /* Win32 always works? */
3757 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3758# endif
3759 if (ret < 0)
3760 {
3761 if (argtype == ARGTYPE_SEND)
3762 {
3763 /* Failed to send, abort. */
3764 mch_errmsg(_(": Send failed.\n"));
3765 didone = TRUE;
3766 exiterr = 1;
3767 }
3768 else if (!silent)
3769 /* Let vim start normally. */
3770 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3771 break;
3772 }
3773
3774# ifdef FEAT_GUI_W32
3775 /* Guess that when the server name starts with "g" it's a GUI
3776 * server, which we can bring to the foreground here.
3777 * Foreground() in the server doesn't work very well. */
3778 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3779 SetForegroundWindow(srv);
3780# endif
3781
3782 /*
3783 * For --remote-wait: Wait until the server did edit each
3784 * file. Also detect that the server no longer runs.
3785 */
3786 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3787 {
3788 int numFiles = *argc - i - 1;
3789 int j;
3790 char_u *done = alloc(numFiles);
3791 char_u *p;
3792# ifdef FEAT_GUI_W32
3793 NOTIFYICONDATA ni;
3794 int count = 0;
3795 extern HWND message_window;
3796# endif
3797
3798 if (numFiles > 0 && argv[i + 1][0] == '+')
3799 /* Skip "+cmd" argument, don't wait for it to be edited. */
3800 --numFiles;
3801
3802# ifdef FEAT_GUI_W32
3803 ni.cbSize = sizeof(ni);
3804 ni.hWnd = message_window;
3805 ni.uID = 0;
3806 ni.uFlags = NIF_ICON|NIF_TIP;
3807 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3808 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3809 Shell_NotifyIcon(NIM_ADD, &ni);
3810# endif
3811
3812 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003813 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003814 while (memchr(done, 0, numFiles) != NULL)
3815 {
3816# ifdef WIN32
3817 p = serverGetReply(srv, NULL, TRUE, TRUE);
3818 if (p == NULL)
3819 break;
3820# else
3821 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3822 break;
3823# endif
3824 j = atoi((char *)p);
3825 if (j >= 0 && j < numFiles)
3826 {
3827# ifdef FEAT_GUI_W32
3828 ++count;
3829 sprintf(ni.szTip, _("%d of %d edited"),
3830 count, numFiles);
3831 Shell_NotifyIcon(NIM_MODIFY, &ni);
3832# endif
3833 done[j] = 1;
3834 }
3835 }
3836# ifdef FEAT_GUI_W32
3837 Shell_NotifyIcon(NIM_DELETE, &ni);
3838# endif
3839 }
3840 }
3841 else if (STRICMP(argv[i], "--remote-expr") == 0)
3842 {
3843 if (i == *argc - 1)
3844 mainerr_arg_missing((char_u *)argv[i]);
3845# ifdef WIN32
3846 /* Win32 always works? */
3847 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3848 &res, NULL, 1, FALSE) < 0)
3849# else
3850 if (xterm_dpy == NULL)
3851 mch_errmsg(_("No display: Send expression failed.\n"));
3852 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3853 &res, NULL, 1, 1, FALSE) < 0)
3854# endif
3855 {
3856 if (res != NULL && *res != NUL)
3857 {
3858 /* Output error from remote */
3859 mch_errmsg((char *)res);
3860 vim_free(res);
3861 res = NULL;
3862 }
3863 mch_errmsg(_(": Send expression failed.\n"));
3864 }
3865 }
3866 else if (STRICMP(argv[i], "--serverlist") == 0)
3867 {
3868# ifdef WIN32
3869 /* Win32 always works? */
3870 res = serverGetVimNames();
3871# else
3872 if (xterm_dpy != NULL)
3873 res = serverGetVimNames(xterm_dpy);
3874# endif
3875 if (called_emsg)
3876 mch_errmsg("\n");
3877 }
3878 else if (STRICMP(argv[i], "--servername") == 0)
3879 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003880 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003881 i++;
3882 continue;
3883 }
3884 else
3885 {
3886 *newArgV++ = argv[i];
3887 newArgC++;
3888 continue;
3889 }
3890 didone = TRUE;
3891 if (res != NULL && *res != NUL)
3892 {
3893 mch_msg((char *)res);
3894 if (res[STRLEN(res) - 1] != '\n')
3895 mch_msg("\n");
3896 }
3897 vim_free(res);
3898 }
3899
3900 if (didone)
3901 {
3902 display_errors(); /* display any collected messages */
3903 exit(exiterr); /* Mission accomplished - get out */
3904 }
3905
3906 /* Return back into main() */
3907 *argc = newArgC;
3908 vim_free(sname);
3909}
3910
3911/*
3912 * Build a ":drop" command to send to a Vim server.
3913 */
3914 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003915build_drop_cmd(
3916 int filec,
3917 char **filev,
3918 int tabs, /* Use ":tab drop" instead of ":drop". */
3919 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003920{
3921 garray_T ga;
3922 int i;
3923 char_u *inicmd = NULL;
3924 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003925 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003926 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003927
3928 if (filec > 0 && filev[0][0] == '+')
3929 {
3930 inicmd = (char_u *)filev[0] + 1;
3931 filev++;
3932 filec--;
3933 }
3934 /* Check if we have at least one argument. */
3935 if (filec <= 0)
3936 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003937
3938 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003939 cwd = alloc(MAXPATHL);
3940 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003941 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003942 if (mch_dirname(cwd, MAXPATHL) != OK)
3943 {
3944 vim_free(cwd);
3945 return NULL;
3946 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003947 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003948#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003949 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003950#else
3951 PATH_ESC_CHARS,
3952#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003953 '\\', TRUE);
3954 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003955 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003956 return NULL;
3957 ga_init2(&ga, 1, 100);
3958 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003959 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003960
3961 /* Call inputsave() so that a prompt for an encryption key works. */
3962 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3963 if (tabs)
3964 ga_concat(&ga, (char_u *)"tab ");
3965 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003966 for (i = 0; i < filec; i++)
3967 {
3968 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003969 * do it again in the Vim server. On MS-Windows only escape
3970 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003971 p = vim_strsave_escaped((char_u *)filev[i],
3972#ifdef UNIX
3973 PATH_ESC_CHARS
3974#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003975 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003976#endif
3977 );
3978 if (p == NULL)
3979 {
3980 vim_free(ga.ga_data);
3981 return NULL;
3982 }
3983 ga_concat(&ga, (char_u *)" ");
3984 ga_concat(&ga, p);
3985 vim_free(p);
3986 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003987 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3988
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003989 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3990 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003991 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3992
3993 /* Switch back to the correct current directory (prior to temporary path
3994 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003995 * correct after the :drop command. With line breaks and spaces:
3996 * if !exists('+acd') || !&acd
3997 * if haslocaldir()
3998 * cd -
3999 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004000 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004001 * cd -
4002 * endif
4003 * endif
4004 */
4005 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004006 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004007 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02004008 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004009 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004010
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004011 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004012 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4013 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004014 if (inicmd != NULL)
4015 {
4016 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4017 * the following commands to be inserted as text. Use a "|",
4018 * hopefully "inicmd" does allow this... */
4019 ga_concat(&ga, inicmd);
4020 ga_concat(&ga, (char_u *)"|");
4021 }
4022 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4023 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004024 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004025 ga_append(&ga, NUL);
4026 return ga.ga_data;
4027}
4028
4029/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004030 * Make our basic server name: use the specified "arg" if given, otherwise use
4031 * the tail of the command "cmd" we were started with.
4032 * Return the name in allocated memory. This doesn't include a serial number.
4033 */
4034 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004035serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004036{
4037 char_u *p;
4038
4039 if (arg != NULL && *arg != NUL)
4040 p = vim_strsave_up(arg);
4041 else
4042 {
4043 p = vim_strsave_up(gettail((char_u *)cmd));
4044 /* Remove .exe or .bat from the name. */
4045 if (p != NULL && vim_strchr(p, '.') != NULL)
4046 *vim_strchr(p, '.') = NUL;
4047 }
4048 return p;
4049}
4050#endif /* FEAT_CLIENTSERVER */
4051
4052#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4053/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004054 * Replace termcodes such as <CR> and insert as key presses if there is room.
4055 */
4056 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004057server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004058{
4059 char_u *ptr = NULL;
4060 char_u *cpo_save = p_cpo;
4061
4062 /* Set 'cpoptions' the way we want it.
4063 * B set - backslashes are *not* treated specially
4064 * k set - keycodes are *not* reverse-engineered
4065 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004066 * The last but one parameter of replace_termcodes() is TRUE so that the
4067 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004068 */
4069 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004070 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004071 p_cpo = cpo_save;
4072
4073 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4074 {
4075 /*
4076 * Add the string to the input stream.
4077 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4078 *
4079 * First clear typed characters from the typeahead buffer, there could
4080 * be half a mapping there. Then append to the existing string, so
4081 * that multiple commands from a client are concatenated.
4082 */
4083 if (typebuf.tb_maplen < typebuf.tb_len)
4084 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4085 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4086
4087 /* Let input_available() know we inserted text in the typeahead
4088 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004089 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090 }
4091 vim_free((char_u *)ptr);
4092}
4093
4094/*
4095 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004096 */
4097 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004098eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004099{
4100 char_u *res;
4101 int save_dbl = debug_break_level;
4102 int save_ro = redir_off;
4103
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004104 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4105 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004106 debug_break_level = -1;
4107 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004108 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4109 * to be typed. Do generate errors so that try/catch works. */
4110 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004111
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004112 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004113
4114 debug_break_level = save_dbl;
4115 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004116 --emsg_silent;
4117 if (emsg_silent < 0)
4118 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004119
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004120 /* A client can tell us to redraw, but not to display the cursor, so do
4121 * that here. */
4122 setcursor();
4123 out_flush();
4124#ifdef FEAT_GUI
4125 if (gui.in_use)
4126 gui_update_cursor(FALSE, FALSE);
4127#endif
4128
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004129 return res;
4130}
4131
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004132/*
4133 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4134 * return an allocated string. Otherwise return "data".
4135 * "*tofree" is set to the result when it needs to be freed later.
4136 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004137 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004138serverConvert(
4139 char_u *client_enc UNUSED,
4140 char_u *data,
4141 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004142{
4143 char_u *res = data;
4144
4145 *tofree = NULL;
4146# ifdef FEAT_MBYTE
4147 if (client_enc != NULL && p_enc != NULL)
4148 {
4149 vimconv_T vimconv;
4150
4151 vimconv.vc_type = CONV_NONE;
4152 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4153 && vimconv.vc_type != CONV_NONE)
4154 {
4155 res = string_convert(&vimconv, data, NULL);
4156 if (res == NULL)
4157 res = data;
4158 else
4159 *tofree = res;
4160 }
4161 convert_setup(&vimconv, NULL, NULL);
4162 }
4163# endif
4164 return res;
4165}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004166#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004167
4168/*
4169 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4170 */
4171#if defined(FEAT_FKMAP) || defined(PROTO)
4172# include "farsi.c"
4173#endif
4174
4175/*
4176 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4177 */
4178#if defined(FEAT_ARABIC) || defined(PROTO)
4179# include "arabic.c"
4180#endif