blob: ac4c7a6d2540fec8379a6352c6914db82de84b6c [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
Bram Moolenaarb4210b32004-06-13 14:51:16 +000013#ifdef __CYGWIN__
14# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000015# include <cygwin/version.h>
16# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
17 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000018# endif
19# include <limits.h>
20#endif
21
Bram Moolenaarc013cb62005-07-24 21:18:31 +000022/* Maximum number of commands from + or -c arguments. */
23#define MAX_ARG_CMDS 10
24
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000025/* values for "window_layout" */
26#define WIN_HOR 1 /* "-o" horizontally split windows */
27#define WIN_VER 2 /* "-O" vertically split windows */
28#define WIN_TABS 3 /* "-p" windows on tab pages */
29
Bram Moolenaar58d98232005-07-23 22:25:46 +000030/* Struct for various parameters passed between main() and other functions. */
31typedef struct
32{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000033 int argc;
34 char **argv;
35
36 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 char_u *use_vimrc; /* vimrc from -u argument */
38
39 int n_commands; /* no. of commands from + or -c */
40 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
41 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
42 int n_pre_commands; /* no. of commands from --cmd */
43 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
44
45 int edit_type; /* type of editing to do */
46 char_u *tagname; /* tag from -t argument */
47#ifdef FEAT_QUICKFIX
48 char_u *use_ef; /* 'errorfile' from -q argument */
49#endif
50
51 int want_full_screen;
52 int stdout_isatty; /* is stdout a terminal? */
53 char_u *term; /* specified terminal name */
54#ifdef FEAT_CRYPT
55 int ask_for_key; /* -x argument */
56#endif
57 int no_swap_file; /* "-n" argument used */
58#ifdef FEAT_EVAL
59 int use_debug_break_level;
60#endif
61#ifdef FEAT_WINDOWS
62 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000063 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000064#endif
65
66#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000067 int serverArg; /* TRUE when argument for a server */
68 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000069 char_u *serverStr; /* remote server command */
70 char_u *serverStrEnc; /* encoding of serverStr */
71 char_u *servername; /* allocated name for our server */
72#endif
Bram Moolenaar53076832015-12-31 19:53:21 +010073#if !defined(UNIX) && !defined(__EMX__)
74# define EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +000075 int literal; /* don't expand file names */
76#endif
77#ifdef MSWIN
78 int full_path; /* file name argument was full path */
79#endif
80#ifdef FEAT_DIFF
81 int diff_mode; /* start with 'diff' set */
82#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000083} mparm_T;
84
Bram Moolenaarc013cb62005-07-24 21:18:31 +000085/* Values for edit_type. */
86#define EDIT_NONE 0 /* no edit type yet */
87#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
88#define EDIT_STDIN 2 /* read file from stdin */
89#define EDIT_TAG 3 /* tag name argument given, use tagname */
90#define EDIT_QF 4 /* start in quickfix mode */
91
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010092#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010093static int file_owned(char *fname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +000094#endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010095static void mainerr(int, char_u *);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010096#ifndef NO_VIM_MAIN
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +010097static void main_msg(char *s);
98static void usage(void);
99static int get_number_arg(char_u *p, int *idx, int def);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100100# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100101static void init_locale(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100102# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100103static void parse_command_name(mparm_T *parmp);
104static void early_arg_scan(mparm_T *parmp);
105static void command_line_scan(mparm_T *parmp);
106static void check_tty(mparm_T *parmp);
107static void read_stdin(void);
108static void create_windows(mparm_T *parmp);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100109# ifdef FEAT_WINDOWS
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100110static void edit_buffers(mparm_T *parmp, char_u *cwd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100111# endif
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100112static void exe_pre_commands(mparm_T *parmp);
113static void exe_commands(mparm_T *parmp);
114static void source_startup_scripts(mparm_T *parmp);
115static void main_start_gui(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100116# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100117static void check_swap_exists_action(void);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100118# endif
119# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +0100120static void exec_on_server(mparm_T *parmp);
121static void prepare_server(mparm_T *parmp);
122static void cmdsrv_main(int *argc, char **argv, char_u *serverName_arg, char_u **serverStr);
123static char_u *serverMakeName(char_u *arg, char *cmd);
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100124# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125#endif
126
127
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000128/*
129 * Different types of error messages.
130 */
131static char *(main_errors[]) =
132{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000133 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000134#define ME_UNKNOWN_OPTION 0
135 N_("Too many edit arguments"),
136#define ME_TOO_MANY_ARGS 1
137 N_("Argument missing after"),
138#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000139 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000140#define ME_GARBAGE 3
141 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
142#define ME_EXTRA_CMD 4
143 N_("Invalid argument for"),
144#define ME_INVALID_ARG 5
145};
146
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100147#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100148#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarf9bde2b2015-04-17 22:08:16 +0200149
150static char_u *start_dir = NULL; /* current working dir on startup */
151
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000152 int
153# ifdef VIMDLL
154_export
155# endif
156# ifdef FEAT_GUI_MSWIN
157# ifdef __BORLANDC__
158_cdecl
159# endif
160VimMain
161# else
162main
163# endif
Bram Moolenaar52ea13d2016-01-30 18:51:09 +0100164(int argc, char **argv)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000165{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000166 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000167 mparm_T params; /* various parameters passed between
168 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000169#ifdef STARTUPTIME
170 int i;
171#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000172
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000173 /*
174 * Do any system-specific initialisations. These can NOT use IObuff or
175 * NameBuff. Thus emsg2() cannot be called!
176 */
177 mch_early_init();
178
Bram Moolenaar14993322014-09-09 12:25:33 +0200179#if defined(WIN32) && defined(FEAT_MBYTE)
180 /*
181 * MingW expands command line arguments, which confuses our code to
182 * convert when 'encoding' changes. Get the unexpanded arguments.
183 */
184 argc = get_cmd_argsW(&argv);
185#endif
186
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000187 /* Many variables are in "params" so that we can pass them to invoked
188 * functions without a lot of arguments. "argc" and "argv" are also
189 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000190 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000191 params.argc = argc;
192 params.argv = argv;
193 params.want_full_screen = TRUE;
194#ifdef FEAT_EVAL
195 params.use_debug_break_level = -1;
196#endif
197#ifdef FEAT_WINDOWS
198 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000199#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000200
Bram Moolenaar99685e62013-05-11 13:56:18 +0200201#ifdef FEAT_RUBY
202 {
203 int ruby_stack_start;
204 vim_ruby_init((void *)&ruby_stack_start);
205 }
206#endif
207
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000208#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000209 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000210#endif
211
212#ifdef MEM_PROFILE
213 atexit(vim_mem_profile_dump);
214#endif
215
216#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000217 for (i = 1; i < argc; ++i)
218 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000219 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000220 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000221 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000222 TIME_MSG("--- VIM STARTING ---");
223 break;
224 }
225 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000226#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000227 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000228
229#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000230 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000231#endif
232
233#ifdef FEAT_MBYTE
234 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
235#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000236#ifdef FEAT_EVAL
237 eval_init(); /* init global variables */
238#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000239
240#ifdef __QNXNTO__
241 qnx_init(); /* PhAttach() for clipboard, (and gui) */
242#endif
243
244#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000245 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000246 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000247 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000248 TIME_MSG("GUI prepared");
249#endif
250
251 /* Init the table of Normal mode commands. */
252 init_normal_cmds();
253
254#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000255 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000256#endif
257
258 /*
259 * Allocate space for the generic buffers (needed for set_init_1() and
260 * EMSG2()).
261 */
262 if ((IObuff = alloc(IOSIZE)) == NULL
263 || (NameBuff = alloc(MAXPATHL)) == NULL)
264 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000265 TIME_MSG("Allocated generic buffers");
266
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000267#ifdef NBDEBUG
268 /* Wait a moment for debugging NetBeans. Must be after allocating
269 * NameBuff. */
270 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
271 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000272 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000273#endif
274
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000275#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
276 /*
277 * Setup to use the current locale (for ctype() and many other things).
278 * NOTE: Translated messages with encodings other than latin1 will not
279 * work until set_init_1() has been called!
280 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000281 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000282 TIME_MSG("locale set");
283#endif
284
285#ifdef FEAT_GUI
286 gui.dofork = TRUE; /* default is to use fork() */
287#endif
288
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000289 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000290 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000291 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000292 * --server...
293 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000294 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000295 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000297
298#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000299 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000300#endif
301#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000302 /* Prepare for possibly starting GUI sometime */
303 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000304 TIME_MSG("GUI prepared");
305#endif
306
307#ifdef FEAT_CLIPBOARD
308 clip_init(FALSE); /* Initialise clipboard stuff */
309 TIME_MSG("clipboard setup");
310#endif
311
312 /*
313 * Check if we have an interactive window.
314 * On the Amiga: If there is no window, we open one with a newcli command
315 * (needed for :! to * work). mch_check_win() will also handle the -d or
316 * -dev argument.
317 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000318 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000319 TIME_MSG("window checked");
320
321 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000322 * Allocate the first window and buffer.
323 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000324 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000325 if (win_alloc_first() == FAIL)
326 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000327
328 init_yank(); /* init yank buffers */
329
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000330 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaar2d1fe052014-05-28 18:22:57 +0200331 global_alist.id = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000332
333 /*
334 * Set the default values for the options.
335 * NOTE: Non-latin1 translated messages are working only after this,
336 * because this is where "has_mbyte" will be set, which is used by
337 * msg_outtrans_len_attr().
338 * First find out the home directory, needed to expand "~" in options.
339 */
340 init_homedir(); /* find real value of $HOME */
341 set_init_1();
342 TIME_MSG("inits 1");
343
344#ifdef FEAT_EVAL
345 set_lang_var(); /* set v:lang and v:ctype */
346#endif
347
348#ifdef FEAT_CLIENTSERVER
349 /*
350 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000351 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000352 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000354#endif
355
356 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 * Figure out the way to work from the command name argv[0].
358 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000360 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000361
362 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000363 * Process the command line arguments. File names are put in the global
364 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000365 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000366 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000367 TIME_MSG("parsing arguments");
368
369 /*
370 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000371 * there is no terminal version, and on Windows we can't fork one off with
372 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000373 */
374#ifdef ALWAYS_USE_GUI
375 gui.starting = TRUE;
376#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000377# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000378 /*
379 * Check if the GUI can be started. Reset gui.starting if not.
380 * Don't know about other systems, stay on the safe side and don't check.
381 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000382 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000384 if (gui_init_check() == FAIL)
385 {
386 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000387
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000388 /* When running "evim" or "gvim -y" we need the menus, exit if we
389 * don't have them. */
390 if (params.evim_mode)
391 mch_exit(1);
392 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000393 }
394# endif
395#endif
396
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397 if (GARGCOUNT > 0)
398 {
Bram Moolenaar53076832015-12-31 19:53:21 +0100399#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000400 /*
401 * Expand wildcards in file names.
402 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000403 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000404 {
Bram Moolenaarf6303872015-04-03 17:59:43 +0200405 start_dir = alloc(MAXPATHL);
406 if (start_dir != NULL)
407 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000408 /* Temporarily add '(' and ')' to 'isfname'. These are valid
409 * filename characters but are excluded from 'isfname' to make
410 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
411 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000412 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000413 do_cmdline_cmd((char_u *)":set isf&");
Bram Moolenaarf6303872015-04-03 17:59:43 +0200414 if (start_dir != NULL)
415 mch_chdir((char *)start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000416 }
417#endif
418 fname = alist_name(&GARGLIST[0]);
419 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000420
421#if defined(WIN32) && defined(FEAT_MBYTE)
422 {
423 extern void set_alist_count(void);
424
425 /* Remember the number of entries in the argument list. If it changes
426 * we don't react on setting 'encoding'. */
427 set_alist_count();
428 }
429#endif
430
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000431#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000432 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000433 {
434 /*
435 * If there is one filename, fully qualified, we have very probably
436 * been invoked from explorer, so change to the file's directory.
437 * Hint: to avoid this when typing a command use a forward slash.
438 * If the cd fails, it doesn't matter.
439 */
440 (void)vim_chdirfile(fname);
Bram Moolenaarf6303872015-04-03 17:59:43 +0200441 if (start_dir != NULL)
442 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000443 }
444#endif
445 TIME_MSG("expanding arguments");
446
447#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000448 if (params.diff_mode && params.window_count == -1)
449 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000450#endif
451
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000452 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000453 ++RedrawingDisabled;
454
455 /*
456 * When listing swap file names, don't do cursor positioning et. al.
457 */
458 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000459 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000460
461 /*
462 * When certain to start the GUI, don't check capabilities of terminal.
463 * For GTK we can't be sure, but when started from the desktop it doesn't
464 * make sense to try using a terminal.
465 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000466#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000467 if (gui.starting
468# ifdef FEAT_GUI_GTK
469 && !isatty(2)
470# endif
471 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000472 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000473#endif
474
475#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
476 /* When the GUI is started from Finder, need to display messages in a
477 * message box. isatty(2) returns TRUE anyway, thus we need to check the
478 * name to know we're not started from a terminal. */
479 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000480 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000481 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000482
483 /* Avoid always using "/" as the current directory. Note that when
484 * started from Finder the arglist will be filled later in
485 * HandleODocAE() and "fname" will be NULL. */
486 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
487 && STRCMP(NameBuff, "/") == 0)
488 {
489 if (fname != NULL)
490 (void)vim_chdirfile(fname);
491 else
492 {
493 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
494 vim_chdir(NameBuff);
495 }
Bram Moolenaarf6303872015-04-03 17:59:43 +0200496 if (start_dir != NULL)
497 mch_dirname(start_dir, MAXPATHL);
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000498 }
499 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000500#endif
501
502 /*
503 * mch_init() sets up the terminal (window) for use. This must be
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100504 * done after resetting full_screen, otherwise it may move the cursor.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000505 * Note that we may use mch_exit() before mch_init()!
506 */
507 mch_init();
508 TIME_MSG("shell init");
509
510#ifdef USE_XSMP
511 /*
512 * For want of anywhere else to do it, try to connect to xsmp here.
513 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
514 * Hijacking -X 'no X connection' to also disable XSMP connection as that
515 * has a similar delay upon failure.
516 * Only try if SESSION_MANAGER is set to something non-null.
517 */
518 if (!x_no_connect)
519 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000520 char *p = getenv("SESSION_MANAGER");
521
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000522 if (p != NULL && *p != NUL)
523 {
524 xsmp_init();
525 TIME_MSG("xsmp init");
526 }
527 }
528#endif
529
530 /*
531 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000532 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000533 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000534
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000535 /* This message comes before term inits, but after setting "silent_mode"
536 * when the input is not a tty. */
537 if (GARGCOUNT > 1 && !silent_mode)
538 printf(_("%d files to edit\n"), GARGCOUNT);
539
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000540 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000541 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000542 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000543 capabilities (will set full_screen) */
544 screen_start(); /* don't know where cursor is now */
545 TIME_MSG("Termcap init");
546 }
547
548 /*
549 * Set the default values for the options that use Rows and Columns.
550 */
551 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000552 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000553#ifdef FEAT_DIFF
554 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
555 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000556 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000557 diff_win_options(firstwin, FALSE);
558#endif
559
560 cmdline_row = Rows - p_ch;
561 msg_row = cmdline_row;
562 screenalloc(FALSE); /* allocate screen buffers */
563 set_init_2();
564 TIME_MSG("inits 2");
565
566 msg_scroll = TRUE;
567 no_wait_return = TRUE;
568
569 init_mappings(); /* set up initial mappings */
570
571 init_highlight(TRUE, FALSE); /* set the default highlight groups */
572 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000573
574#ifdef FEAT_EVAL
575 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000576 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000577#endif
578
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100579#ifdef FEAT_MZSCHEME
580 /*
581 * Newer version of MzScheme (Racket) require earlier (trampolined)
582 * initialisation via scheme_main_setup.
583 * Implement this by initialising it as early as possible
584 * and splitting off remaining Vim main into vim_main2
585 */
586 {
587 /* Pack up preprocessed command line arguments.
588 * It is safe because Scheme does not access argc/argv. */
589 char *args[2];
590 args[0] = (char *)fname;
591 args[1] = (char *)&params;
592 return mzscheme_main(2, args);
593 }
594}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100595#endif
596#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100597
Bram Moolenaar8866d272012-11-28 15:55:42 +0100598/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
599 * NO_VIM_MAIN is defined. */
600#ifdef FEAT_MZSCHEME
601 int
602vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100603{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100604# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100605 char_u *fname = (char_u *)argv[0];
606 mparm_T params;
607
608 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100609# else
610 return 0;
611}
612# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100613#endif
614
Bram Moolenaar8866d272012-11-28 15:55:42 +0100615#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000616 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000617 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000618
Bram Moolenaar58d98232005-07-23 22:25:46 +0000619 /* Source startup scripts. */
620 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000621
622#ifdef FEAT_EVAL
623 /*
624 * Read all the plugin files.
625 * Only when compiled with +eval, since most plugins need it.
626 */
627 if (p_lpl)
628 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000629# ifdef VMS /* Somehow VMS doesn't handle the "**". */
630 source_runtime((char_u *)"plugin/*.vim", TRUE);
631# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000632 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000633# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000634 TIME_MSG("loading plugins");
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +0100635
636 source_packages();
637 TIME_MSG("loading packages");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000638 }
639#endif
640
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000641#ifdef FEAT_DIFF
642 /* Decide about window layout for diff mode after reading vimrc. */
643 if (params.diff_mode && params.window_layout == 0)
644 {
645 if (diffopt_horizontal())
646 params.window_layout = WIN_HOR; /* use horizontal split */
647 else
648 params.window_layout = WIN_VER; /* use vertical split */
649 }
650#endif
651
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000652 /*
653 * Recovery mode without a file name: List swap files.
654 * This uses the 'dir' option, therefore it must be after the
655 * initializations.
656 */
657 if (recoverymode && fname == NULL)
658 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200659 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000660 mch_exit(0);
661 }
662
663 /*
664 * Set a few option defaults after reading .vimrc files:
665 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
666 */
667 set_init_3();
668 TIME_MSG("inits 3");
669
670 /*
671 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
672 * Note that this overrides anything from a vimrc file.
673 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000674 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000675 p_uc = 0;
676
677#ifdef FEAT_FKMAP
678 if (curwin->w_p_rl && p_altkeymap)
679 {
680 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
681# ifdef FEAT_ARABIC
682 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
683# endif
684 p_fkmap = TRUE; /* Set the Farsi keymap mode */
685 }
686#endif
687
688#ifdef FEAT_GUI
689 if (gui.starting)
690 {
691#if defined(UNIX) || defined(VMS)
692 /* When something caused a message from a vimrc script, need to output
693 * an extra newline before the shell prompt. */
694 if (did_emsg || msg_didout)
695 putchar('\n');
696#endif
697
698 gui_start(); /* will set full_screen to TRUE */
699 TIME_MSG("starting GUI");
700
701 /* When running "evim" or "gvim -y" we need the menus, exit if we
702 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000703 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000704 mch_exit(1);
705 }
706#endif
707
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000708#ifdef FEAT_VIMINFO
709 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000710 * Read in registers, history etc, but not marks, from the viminfo file.
711 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 */
713 if (*p_viminfo != NUL)
714 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000715 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000716 TIME_MSG("reading viminfo");
717 }
718#endif
Bram Moolenaar2cd36962014-01-14 12:57:05 +0100719#ifdef FEAT_EVAL
720 /* It's better to make v:oldfiles an empty list than NULL. */
721 if (get_vim_var_list(VV_OLDFILES) == NULL)
722 set_vim_var_list(VV_OLDFILES, list_alloc());
723#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000724
725#ifdef FEAT_QUICKFIX
726 /*
727 * "-q errorfile": Load the error file now.
728 * If the error file can't be read, exit before doing anything else.
729 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000730 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000731 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000732 if (params.use_ef != NULL)
733 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000734 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200735 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
736 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000737 {
738 out_char('\n');
739 mch_exit(3);
740 }
741 TIME_MSG("reading errorfile");
742 }
743#endif
744
745 /*
746 * Start putting things on the screen.
747 * Scroll screen down before drawing over it
748 * Clear screen now, so file message will not be cleared.
749 */
750 starting = NO_BUFFERS;
751 no_wait_return = FALSE;
752 if (!exmode_active)
753 msg_scroll = FALSE;
754
755#ifdef FEAT_GUI
756 /*
757 * This seems to be required to make callbacks to be called now, instead
758 * of after things have been put on the screen, which then may be deleted
759 * when getting a resize callback.
760 * For the Mac this handles putting files dropped on the Vim icon to
761 * global_alist.
762 */
763 if (gui.in_use)
764 {
765# ifdef FEAT_SUN_WORKSHOP
766 if (!usingSunWorkShop)
767# endif
768 gui_wait_for_chars(50L);
769 TIME_MSG("GUI delay");
770 }
771#endif
772
773#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
774 qnx_clip_init();
775#endif
776
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200777#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
778 clip_init(TRUE);
779#endif
780
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000781#ifdef FEAT_XCLIPBOARD
782 /* Start using the X clipboard, unless the GUI was started. */
783# ifdef FEAT_GUI
784 if (!gui.in_use)
785# endif
786 {
787 setup_term_clip();
788 TIME_MSG("setup clipboard");
789 }
790#endif
791
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000792#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000793 /* Prepare for being a Vim server. */
794 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795#endif
796
797 /*
798 * If "-" argument given: Read file from stdin.
799 * Do this before starting Raw mode, because it may change things that the
800 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
801 * are the same terminal: "cat | vim -".
802 * Using autocommands here may cause trouble...
803 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000804 if (params.edit_type == EDIT_STDIN && !recoverymode)
805 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000806
807#if defined(UNIX) || defined(VMS)
808 /* When switching screens and something caused a message from a vimrc
809 * script, need to output an extra newline on exit. */
810 if ((did_emsg || msg_didout) && *T_TI != NUL)
811 newline_on_exit = TRUE;
812#endif
813
814 /*
815 * When done something that is not allowed or error message call
816 * wait_return. This must be done before starttermcap(), because it may
817 * switch to another screen. It must be done after settmode(TMODE_RAW),
818 * because we want to react on a single key stroke.
819 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000820 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000821 */
822 settmode(TMODE_RAW);
823 TIME_MSG("setting raw mode");
824
825 if (need_wait_return || msg_didany)
826 {
827 wait_return(TRUE);
828 TIME_MSG("waiting for return");
829 }
830
831 starttermcap(); /* start termcap if not done by wait_return() */
832 TIME_MSG("start termcap");
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200833#if defined(FEAT_TERMRESPONSE)
834# if defined(FEAT_MBYTE)
Bram Moolenaar386dcde2013-09-29 16:27:47 +0200835 may_req_ambiguous_char_width();
Bram Moolenaarb5c32652015-06-25 17:03:36 +0200836# endif
837 may_req_bg_color();
Bram Moolenaar9584b312013-03-13 19:29:28 +0100838#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000839
840#ifdef FEAT_MOUSE
841 setmouse(); /* may start using the mouse */
842#endif
843 if (scroll_region)
844 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000845 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000846
847 /*
848 * Don't clear the screen when starting in Ex mode, unless using the GUI.
849 */
850 if (exmode_active
851#ifdef FEAT_GUI
852 && !gui.in_use
853#endif
854 )
855 must_redraw = CLEAR;
856 else
857 {
858 screenclear(); /* clear screen */
859 TIME_MSG("clearing screen");
860 }
861
862#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000863 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000864 {
Bram Moolenaar3a0c9082014-11-12 15:15:42 +0100865 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +0200866 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000867 TIME_MSG("getting crypt key");
868 }
869#endif
870
871 no_wait_return = TRUE;
872
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000873 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000874 * Create the requested number of windows and edit buffers in them.
875 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000876 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000877 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878 TIME_MSG("opening buffers");
879
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000880#ifdef FEAT_EVAL
881 /* clear v:swapcommand */
882 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
883#endif
884
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000885 /* Ex starts at last line of the file */
886 if (exmode_active)
887 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
888
889#ifdef FEAT_AUTOCMD
890 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
891 TIME_MSG("BufEnter autocommands");
892#endif
893 setpcmark();
894
895#ifdef FEAT_QUICKFIX
896 /*
897 * When started with "-q errorfile" jump to first error now.
898 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000899 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000900 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000901 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000902 TIME_MSG("jump to first error");
903 }
904#endif
905
906#ifdef FEAT_WINDOWS
907 /*
908 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000909 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000910 */
Bram Moolenaarf6303872015-04-03 17:59:43 +0200911 edit_buffers(&params, start_dir);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000912#endif
Bram Moolenaarf6303872015-04-03 17:59:43 +0200913 vim_free(start_dir);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000914
915#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000916 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000917 {
918 win_T *wp;
919
920 /* set options in each window for "vimdiff". */
921 for (wp = firstwin; wp != NULL; wp = wp->w_next)
922 diff_win_options(wp, TRUE);
923 }
924#endif
925
926 /*
927 * Shorten any of the filenames, but only when absolute.
928 */
929 shorten_fnames(FALSE);
930
931 /*
932 * Need to jump to the tag before executing the '-c command'.
933 * Makes "vim -c '/return' -t main" work.
934 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000935 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000936 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000937#if defined(HAS_SWAP_EXISTS_ACTION)
938 swap_exists_did_quit = FALSE;
939#endif
940
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000941 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000942 do_cmdline_cmd(IObuff);
943 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000944
945#if defined(HAS_SWAP_EXISTS_ACTION)
946 /* If the user doesn't want to edit the file then we quit here. */
947 if (swap_exists_did_quit)
948 getout(1);
949#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000950 }
951
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000952 /* Execute any "+", "-c" and "-S" arguments. */
953 if (params.n_commands > 0)
954 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000955
956 RedrawingDisabled = 0;
957 redraw_all_later(NOT_VALID);
958 no_wait_return = FALSE;
959 starting = 0;
960
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000961#ifdef FEAT_TERMRESPONSE
962 /* Requesting the termresponse is postponed until here, so that a "-c q"
963 * argument doesn't make it appear in the shell Vim was started from. */
964 may_req_termresponse();
965#endif
966
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000967 /* start in insert mode */
968 if (p_im)
969 need_start_insertmode = TRUE;
970
971#ifdef FEAT_AUTOCMD
972 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
973 TIME_MSG("VimEnter autocommands");
974#endif
975
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200976#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
977 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
978 * done after the clipboard is available and all initial commands that may
979 * modify the 'clipboard' setting have run; i.e. just before entering the
980 * main loop. */
981 {
982 int default_regname = 0;
Bram Moolenaar53076832015-12-31 19:53:21 +0100983
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200984 adjust_clip_reg(&default_regname);
985 set_reg_var(default_regname);
986 }
987#endif
988
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000989#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
990 /* When a startup script or session file setup for diff'ing and
991 * scrollbind, sync the scrollbind now. */
992 if (curwin->w_p_diff && curwin->w_p_scb)
993 {
994 update_topline();
995 check_scrollbind((linenr_T)0, 0L);
996 TIME_MSG("diff scrollbinding");
997 }
998#endif
999
1000#if defined(WIN3264) && !defined(FEAT_GUI_W32)
1001 mch_set_winsize_now(); /* Allow winsize changes from now on */
1002#endif
1003
Bram Moolenaar32466aa2006-02-24 23:53:04 +00001004#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
1005 /* When tab pages were created, may need to update the tab pages line and
1006 * scrollbars. This is skipped while creating them. */
1007 if (first_tabpage->tp_next != NULL)
1008 {
1009 out_flush();
1010 gui_init_which_components(NULL);
1011 gui_update_scrollbars(TRUE);
1012 }
1013 need_mouse_correct = TRUE;
1014#endif
1015
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001016 /* If ":startinsert" command used, stuff a dummy command to be able to
1017 * call normal_cmd(), which will then start Insert mode. */
1018 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +00001019 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001020
1021#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001022 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001023 {
1024# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001025# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001026 && !defined(FEAT_GUI_W32)
1027 if (gui.in_use)
1028 {
1029 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1030 mch_exit(2);
1031 }
1032# endif
1033# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001034 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001035 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001036 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001037#endif
1038
1039 TIME_MSG("before starting main loop");
1040
1041 /*
1042 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001043 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001044 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001045
1046 return 0;
1047}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001048#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001049#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001050
1051/*
1052 * Main loop: Execute Normal mode commands until exiting Vim.
1053 * Also used to handle commands in the command-line window, until the window
1054 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001055 * Also used to handle ":visual" command after ":global": execute Normal mode
1056 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001057 */
1058 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001059main_loop(
1060 int cmdwin, /* TRUE when working in the command-line window */
1061 int noexmode) /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001062{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001063 oparg_T oa; /* operator arguments */
Bram Moolenaar8872ef12015-02-10 19:27:05 +01001064 volatile int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001065#ifdef FEAT_CONCEAL
Bram Moolenaar1db43b12015-07-12 16:21:23 +02001066 /* these are static to avoid a compiler warning */
1067 static linenr_T conceal_old_cursor_line = 0;
1068 static linenr_T conceal_new_cursor_line = 0;
1069 static int conceal_update_lines = FALSE;
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001070#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001071
1072#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1073 /* Setup to catch a terminating error from the X server. Just ignore
1074 * it, restore the state and continue. This might not always work
1075 * properly, but at least we don't exit unexpectedly when the X server
Bram Moolenaar2cd36962014-01-14 12:57:05 +01001076 * exits while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001077 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001078 {
1079 State = NORMAL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001080 VIsual_active = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001081 got_int = TRUE;
1082 need_wait_return = FALSE;
1083 global_busy = FALSE;
1084 exmode_active = 0;
1085 skip_redraw = FALSE;
1086 RedrawingDisabled = 0;
1087 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001088 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001089# ifdef FEAT_EVAL
1090 emsg_skip = 0;
1091# endif
1092 emsg_off = 0;
1093# ifdef FEAT_MOUSE
1094 setmouse();
1095# endif
1096 settmode(TMODE_RAW);
1097 starttermcap();
1098 scroll_start();
1099 redraw_later_clear();
1100 }
1101#endif
1102
1103 clear_oparg(&oa);
1104 while (!cmdwin
1105#ifdef FEAT_CMDWIN
1106 || cmdwin_result == 0
1107#endif
1108 )
1109 {
1110 if (stuff_empty())
1111 {
1112 did_check_timestamps = FALSE;
1113 if (need_check_timestamps)
1114 check_timestamps(FALSE);
1115 if (need_wait_return) /* if wait_return still needed ... */
1116 wait_return(FALSE); /* ... call it now */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001117 if (need_start_insertmode && goto_im() && !VIsual_active)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001118 {
1119 need_start_insertmode = FALSE;
1120 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1121 /* skip the fileinfo message now, because it would be shown
1122 * after insert mode finishes! */
1123 need_fileinfo = FALSE;
1124 }
1125 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001126
1127 /* Reset "got_int" now that we got back to the main loop. Except when
1128 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1129 * the ":g" command.
1130 * For ":g/pat/vi" we reset "got_int" when used once. When used
1131 * a second time we go back to Ex mode and abort the ":g" command. */
1132 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001133 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001134 if (noexmode && global_busy && !exmode_active && previous_got_int)
1135 {
1136 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1137 * used and keep "got_int" set, so that it aborts ":g". */
1138 exmode_active = EXMODE_NORMAL;
1139 State = NORMAL;
1140 }
1141 else if (!global_busy || !exmode_active)
1142 {
1143 if (!quit_more)
1144 (void)vgetc(); /* flush all buffers */
1145 got_int = FALSE;
1146 }
1147 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001148 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001149 else
1150 previous_got_int = FALSE;
1151
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001152 if (!exmode_active)
1153 msg_scroll = FALSE;
1154 quit_more = FALSE;
1155
1156 /*
1157 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1158 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1159 * update cursor and redraw.
1160 */
1161 if (skip_redraw || exmode_active)
1162 skip_redraw = FALSE;
1163 else if (do_redraw || stuff_empty())
1164 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001165#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001166 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001167 if (!finish_op && (
1168# ifdef FEAT_AUTOCMD
1169 has_cursormoved()
1170# endif
1171# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1172 ||
1173# endif
1174# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001175 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001176# endif
1177 )
1178 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001179 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001180# ifdef FEAT_AUTOCMD
1181 if (has_cursormoved())
1182 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1183 FALSE, curbuf);
1184# endif
1185# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001186 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001187 {
1188 conceal_old_cursor_line = last_cursormoved.lnum;
1189 conceal_new_cursor_line = curwin->w_cursor.lnum;
1190 conceal_update_lines = TRUE;
1191 }
1192# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001193 last_cursormoved = curwin->w_cursor;
1194 }
1195#endif
1196
Bram Moolenaar186628f2013-03-19 13:33:23 +01001197#ifdef FEAT_AUTOCMD
1198 /* Trigger TextChanged if b_changedtick differs. */
1199 if (!finish_op && has_textchanged()
1200 && last_changedtick != curbuf->b_changedtick)
1201 {
1202 if (last_changedtick_buf == curbuf)
1203 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1204 FALSE, curbuf);
1205 last_changedtick_buf = curbuf;
1206 last_changedtick = curbuf->b_changedtick;
1207 }
1208#endif
1209
Bram Moolenaar33aec762006-01-22 23:30:12 +00001210#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1211 /* Scroll-binding for diff mode may have been postponed until
1212 * here. Avoids doing it for every change. */
1213 if (diff_need_scrollbind)
1214 {
1215 check_scrollbind((linenr_T)0, 0L);
1216 diff_need_scrollbind = FALSE;
1217 }
1218#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001219#if defined(FEAT_FOLDING)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001220 /* Include a closed fold completely in the Visual area. */
1221 foldAdjustVisual();
1222#endif
1223#ifdef FEAT_FOLDING
1224 /*
1225 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1226 * contain the cursor.
1227 * When 'foldopen' is "all", open the fold(s) under the cursor.
1228 * This may mark the window for redrawing.
1229 */
1230 if (hasAnyFolding(curwin) && !char_avail())
1231 {
1232 foldCheckClose();
1233 if (fdo_flags & FDO_ALL)
1234 foldOpenCursor();
1235 }
1236#endif
1237
1238 /*
1239 * Before redrawing, make sure w_topline is correct, and w_leftcol
1240 * if lines don't wrap, and w_skipcol if lines wrap.
1241 */
1242 update_topline();
1243 validate_cursor();
1244
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001245 if (VIsual_active)
1246 update_curbuf(INVERTED);/* update inverted part */
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01001247 else if (must_redraw)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001248 update_screen(0);
1249 else if (redraw_cmdline || clear_cmdline)
1250 showmode();
1251#ifdef FEAT_WINDOWS
1252 redraw_statuslines();
1253#endif
1254#ifdef FEAT_TITLE
1255 if (need_maketitle)
1256 maketitle();
1257#endif
1258 /* display message after redraw */
1259 if (keep_msg != NULL)
1260 {
1261 char_u *p;
1262
1263 /* msg_attr_keep() will set keep_msg to NULL, must free the
Bram Moolenaarf638cbc2014-09-09 17:47:38 +02001264 * string here. Don't reset keep_msg, msg_attr_keep() uses it
1265 * to check for duplicates. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001266 p = keep_msg;
1267 msg_attr(p, keep_msg_attr);
1268 vim_free(p);
1269 }
1270 if (need_fileinfo) /* show file info after redraw */
1271 {
1272 fileinfo(FALSE, TRUE, FALSE);
1273 need_fileinfo = FALSE;
1274 }
1275
1276 emsg_on_display = FALSE; /* can delete error message now */
1277 did_emsg = FALSE;
1278 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001279 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 showruler(FALSE);
1281
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001282# if defined(FEAT_CONCEAL)
1283 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001284 && (conceal_old_cursor_line != conceal_new_cursor_line
1285 || conceal_cursor_line(curwin)
1286 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001287 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001288 if (conceal_old_cursor_line != conceal_new_cursor_line
1289 && conceal_old_cursor_line
1290 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001291 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001292 update_single_line(curwin, conceal_new_cursor_line);
1293 curwin->w_valid &= ~VALID_CROW;
1294 }
1295# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001296 setcursor();
1297 cursor_on();
1298
1299 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001300
1301#ifdef STARTUPTIME
1302 /* Now that we have drawn the first screen all the startup stuff
1303 * has been done, close any file for startup messages. */
1304 if (time_fd != NULL)
1305 {
1306 TIME_MSG("first screen update");
1307 TIME_MSG("--- VIM STARTED ---");
1308 fclose(time_fd);
1309 time_fd = NULL;
1310 }
1311#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001312 }
1313#ifdef FEAT_GUI
1314 if (need_mouse_correct)
1315 gui_mouse_correct();
1316#endif
1317
1318 /*
1319 * Update w_curswant if w_set_curswant has been set.
1320 * Postponed until here to avoid computing w_virtcol too often.
1321 */
1322 update_curswant();
1323
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001324#ifdef FEAT_EVAL
1325 /*
1326 * May perform garbage collection when waiting for a character, but
1327 * only at the very toplevel. Otherwise we may be using a List or
1328 * Dict internally somewhere.
1329 * "may_garbage_collect" is reset in vgetc() which is invoked through
1330 * do_exmode() and normal_cmd().
1331 */
1332 may_garbage_collect = (!cmdwin && !noexmode);
1333#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001334 /*
1335 * If we're invoked as ex, do a round of ex commands.
1336 * Otherwise, get and execute a normal mode command.
1337 */
1338 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001339 {
1340 if (noexmode) /* End of ":global/path/visual" commands */
1341 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001343 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001344 else
1345 normal_cmd(&oa, TRUE);
1346 }
1347}
1348
1349
1350#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1351/*
1352 * Exit, but leave behind swap files for modified buffers.
1353 */
1354 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001355getout_preserve_modified(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001356{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001357# if defined(SIGHUP) && defined(SIG_IGN)
1358 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1359 * makes Vim exit and then handling SIGHUP causes various reentrance
1360 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001361 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001362# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001363
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364 ml_close_notmod(); /* close all not-modified buffers */
1365 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1366 ml_close_all(FALSE); /* close all memfiles, without deleting */
1367 getout(exitval); /* exit Vim properly */
1368}
1369#endif
1370
1371
1372/* Exit properly */
1373 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001374getout(int exitval)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001375{
1376#ifdef FEAT_AUTOCMD
1377 buf_T *buf;
1378 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001379 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001380#endif
1381
1382 exiting = TRUE;
1383
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001384 /* When running in Ex mode an error causes us to exit with a non-zero exit
1385 * code. POSIX requires this, although it's not 100% clear from the
1386 * standard. */
1387 if (exmode_active)
1388 exitval += ex_exitval;
1389
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001390 /* Position the cursor on the last screen line, below all the text */
1391#ifdef FEAT_GUI
1392 if (!gui.in_use)
1393#endif
1394 windgoto((int)Rows - 1, 0);
1395
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001396#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1397 /* Optionally print hashtable efficiency. */
1398 hash_debug_results();
1399#endif
1400
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401#ifdef FEAT_GUI
1402 msg_didany = FALSE;
1403#endif
1404
1405#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001406 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001407 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001408 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1409# if defined FEAT_WINDOWS
1410 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001411 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001412 next_tp = tp->tp_next;
1413 for (wp = (tp == curtab)
1414 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001415 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001416 if (wp->w_buffer == NULL)
1417 /* Autocmd must have close the buffer already, skip. */
1418 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001419 buf = wp->w_buffer;
1420 if (buf->b_changedtick != -1)
1421 {
1422 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1423 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001424 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001425 /* start all over, autocommands may mess up the lists */
1426 next_tp = first_tabpage;
1427 break;
1428 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001429 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001430 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001431# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001432 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1433 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001434# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001435
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001436 /* Trigger BufUnload for buffers that are loaded */
1437 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1438 if (buf->b_ml.ml_mfp != NULL)
1439 {
1440 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001441 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001442 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1443 break;
1444 }
1445 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1446 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001447#endif
1448
1449#ifdef FEAT_VIMINFO
1450 if (*p_viminfo != NUL)
1451 /* Write out the registers, history, marks etc, to the viminfo file */
1452 write_viminfo(NULL, FALSE);
1453#endif
1454
1455#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001456 if (get_vim_var_nr(VV_DYING) <= 1)
1457 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001458#endif
1459
Bram Moolenaar05159a02005-02-26 23:04:13 +00001460#ifdef FEAT_PROFILE
1461 profile_dump();
1462#endif
1463
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001464 if (did_emsg
1465#ifdef FEAT_GUI
1466 || (gui.in_use && msg_didany && p_verbose > 0)
1467#endif
1468 )
1469 {
1470 /* give the user a chance to read the (error) message */
1471 no_wait_return = FALSE;
1472 wait_return(FALSE);
1473 }
1474
1475#ifdef FEAT_AUTOCMD
1476 /* Position the cursor again, the autocommands may have moved it */
1477# ifdef FEAT_GUI
1478 if (!gui.in_use)
1479# endif
1480 windgoto((int)Rows - 1, 0);
1481#endif
1482
Bram Moolenaar65edff82016-02-21 16:40:11 +01001483#ifdef FEAT_JOB
1484 job_stop_on_exit();
1485#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001486#ifdef FEAT_LUA
1487 lua_end();
1488#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001489#ifdef FEAT_MZSCHEME
1490 mzscheme_end();
1491#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001492#ifdef FEAT_TCL
1493 tcl_end();
1494#endif
1495#ifdef FEAT_RUBY
1496 ruby_end();
1497#endif
1498#ifdef FEAT_PYTHON
1499 python_end();
1500#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001501#ifdef FEAT_PYTHON3
1502 python3_end();
1503#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001504#ifdef FEAT_PERL
1505 perl_end();
1506#endif
1507#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1508 iconv_end();
1509#endif
1510#ifdef FEAT_NETBEANS_INTG
1511 netbeans_end();
1512#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001513#ifdef FEAT_CSCOPE
1514 cs_end();
1515#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001516#ifdef FEAT_EVAL
1517 if (garbage_collect_at_exit)
1518 garbage_collect();
1519#endif
Bram Moolenaar14993322014-09-09 12:25:33 +02001520#if defined(WIN32) && defined(FEAT_MBYTE)
1521 free_cmd_argsW();
1522#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001523
1524 mch_exit(exitval);
1525}
1526
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001527#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001528/*
1529 * Get a (optional) count for a Vim argument.
1530 */
1531 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001532get_number_arg(
1533 char_u *p, /* pointer to argument */
1534 int *idx, /* index in argument, is incremented */
1535 int def) /* default value */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001536{
1537 if (vim_isdigit(p[*idx]))
1538 {
1539 def = atoi((char *)&(p[*idx]));
1540 while (vim_isdigit(p[*idx]))
1541 *idx = *idx + 1;
1542 }
1543 return def;
1544}
1545
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001546#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1547/*
1548 * Setup to use the current locale (for ctype() and many other things).
1549 */
1550 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001551init_locale(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001552{
1553 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001554
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001555# ifdef FEAT_GUI_GTK
1556 /* Tell Gtk not to change our locale settings. */
1557 gtk_disable_setlocale();
1558# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1560 /* Make sure strtod() uses a decimal point, not a comma. */
1561 setlocale(LC_NUMERIC, "C");
1562# endif
1563
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001564# ifdef WIN32
1565 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1566 * text while it's expecting text in the current locale. This call avoids
1567 * that. */
1568 setlocale(LC_CTYPE, "C");
1569# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001570
1571# ifdef FEAT_GETTEXT
1572 {
1573 int mustfree = FALSE;
1574 char_u *p;
1575
1576# ifdef DYNAMIC_GETTEXT
1577 /* Initialize the gettext library */
Bram Moolenaar286eacd2016-01-16 18:05:50 +01001578 dyn_libintl_init();
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001579# endif
Bram Moolenaar88e8f9f2016-01-20 22:48:02 +01001580 /* expand_env() doesn't work yet, because g_chartab[] is not
1581 * initialized yet, call vim_getenv() directly */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001582 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1583 if (p != NULL && *p != NUL)
1584 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001585 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001586 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1587 }
1588 if (mustfree)
1589 vim_free(p);
1590 textdomain(VIMPACKAGE);
1591 }
1592# endif
1593}
1594#endif
1595
1596/*
1597 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1598 * If the executable name starts with "r" we disable shell commands.
1599 * If the next character is "e" we run in Easy mode.
1600 * If the next character is "g" we run the GUI version.
1601 * If the next characters are "view" we start in readonly mode.
1602 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1603 * If the next characters are "ex" we start in Ex mode. If it's followed
1604 * by "im" use improved Ex mode.
1605 */
1606 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001607parse_command_name(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001608{
1609 char_u *initstr;
1610
1611 initstr = gettail((char_u *)parmp->argv[0]);
1612
1613#ifdef MACOS_X_UNIX
1614 /* An issue has been seen when launching Vim in such a way that
1615 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1616 * executable or a symbolic link of it. Until this issue is resolved
1617 * we prohibit the GUI from being used.
1618 */
1619 if (STRCMP(initstr, parmp->argv[0]) == 0)
1620 disallow_gui = TRUE;
1621
1622 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001623 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001624#endif
1625
1626#ifdef FEAT_EVAL
1627 set_vim_var_string(VV_PROGNAME, initstr, -1);
Bram Moolenaara1706c92014-04-01 19:55:49 +02001628 set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001629#endif
1630
1631 if (TOLOWER_ASC(initstr[0]) == 'r')
1632 {
1633 restricted = TRUE;
1634 ++initstr;
1635 }
1636
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001637 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001638 if (TOLOWER_ASC(initstr[0]) == 'e'
1639 && (TOLOWER_ASC(initstr[1]) == 'v'
1640 || TOLOWER_ASC(initstr[1]) == 'g'))
1641 {
1642#ifdef FEAT_GUI
1643 gui.starting = TRUE;
1644#endif
1645 parmp->evim_mode = TRUE;
1646 ++initstr;
1647 }
1648
Bram Moolenaar806875d2008-09-18 18:57:10 +00001649 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1650 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001651 {
1652 main_start_gui();
1653#ifdef FEAT_GUI
1654 ++initstr;
1655#endif
1656 }
1657
1658 if (STRNICMP(initstr, "view", 4) == 0)
1659 {
1660 readonlymode = TRUE;
1661 curbuf->b_p_ro = TRUE;
1662 p_uc = 10000; /* don't update very often */
1663 initstr += 4;
1664 }
1665 else if (STRNICMP(initstr, "vim", 3) == 0)
1666 initstr += 3;
1667
1668 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1669 if (STRICMP(initstr, "diff") == 0)
1670 {
1671#ifdef FEAT_DIFF
1672 parmp->diff_mode = TRUE;
1673#else
1674 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1675 mch_errmsg("\n");
1676 mch_exit(2);
1677#endif
1678 }
1679
1680 if (STRNICMP(initstr, "ex", 2) == 0)
1681 {
1682 if (STRNICMP(initstr + 2, "im", 2) == 0)
1683 exmode_active = EXMODE_VIM;
1684 else
1685 exmode_active = EXMODE_NORMAL;
1686 change_compatible(TRUE); /* set 'compatible' */
1687 }
1688}
1689
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001690/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001691 * Get the name of the display, before gui_prepare() removes it from
1692 * argv[]. Used for the xterm-clipboard display.
1693 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001694 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001695 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001696 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001697early_arg_scan(mparm_T *parmp UNUSED)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001698{
Bram Moolenaar03005972008-11-20 13:12:36 +00001699#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1700 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001701 int argc = parmp->argc;
1702 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001703 int i;
1704
1705 for (i = 1; i < argc; i++)
1706 {
1707 if (STRCMP(argv[i], "--") == 0)
1708 break;
1709# ifdef FEAT_XCLIPBOARD
1710 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001711# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001712 || STRICMP(argv[i], "--display") == 0
1713# endif
1714 )
1715 {
1716 if (i == argc - 1)
1717 mainerr_arg_missing((char_u *)argv[i]);
1718 xterm_display = argv[++i];
1719 }
1720# endif
1721# ifdef FEAT_CLIENTSERVER
1722 else if (STRICMP(argv[i], "--servername") == 0)
1723 {
1724 if (i == argc - 1)
1725 mainerr_arg_missing((char_u *)argv[i]);
1726 parmp->serverName_arg = (char_u *)argv[++i];
1727 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001728 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001729 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001730 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001731 {
1732 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001733# ifdef FEAT_GUI
1734 if (strstr(argv[i], "-wait") != 0)
1735 /* don't fork() when starting the GUI to edit files ourself */
1736 gui.dofork = FALSE;
1737# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001738 }
1739# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001740
1741# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1742# ifdef FEAT_GUI_W32
1743 else if (STRICMP(argv[i], "--windowid") == 0)
1744# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001745 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001746# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001747 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001748 long_u id;
1749 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001750
1751 if (i == argc - 1)
1752 mainerr_arg_missing((char_u *)argv[i]);
1753 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001754 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001755 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001756 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001757 if (count != 1)
1758 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1759 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001760# ifdef FEAT_GUI_W32
1761 win_socket_id = id;
1762# else
1763 gtk_socket_id = id;
1764# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001765 i++;
1766 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001767# endif
1768# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001769 else if (STRICMP(argv[i], "--echo-wid") == 0)
1770 echo_wid_arg = TRUE;
1771# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001772# ifndef FEAT_NETBEANS_INTG
1773 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001774 {
1775 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1776 mch_exit(2);
1777 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001778# endif
1779
Bram Moolenaar58d98232005-07-23 22:25:46 +00001780 }
1781#endif
1782}
1783
1784/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001785 * Scan the command line arguments.
1786 */
1787 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01001788command_line_scan(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001789{
1790 int argc = parmp->argc;
1791 char **argv = parmp->argv;
1792 int argv_idx; /* index in argv[n][] */
1793 int had_minmin = FALSE; /* found "--" argument */
1794 int want_argument; /* option argument with argument */
1795 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001796 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001797 long n;
1798
1799 --argc;
1800 ++argv;
1801 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1802 while (argc > 0)
1803 {
1804 /*
1805 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1806 */
1807 if (argv[0][0] == '+' && !had_minmin)
1808 {
1809 if (parmp->n_commands >= MAX_ARG_CMDS)
1810 mainerr(ME_EXTRA_CMD, NULL);
1811 argv_idx = -1; /* skip to next argument */
1812 if (argv[0][1] == NUL)
1813 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1814 else
1815 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1816 }
1817
1818 /*
1819 * Optional argument.
1820 */
1821 else if (argv[0][0] == '-' && !had_minmin)
1822 {
1823 want_argument = FALSE;
1824 c = argv[0][argv_idx++];
1825#ifdef VMS
1826 /*
1827 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1828 * and "-/X" as "-X".
1829 */
1830 if (c == '/')
1831 {
1832 c = argv[0][argv_idx++];
1833 c = TOUPPER_ASC(c);
1834 }
1835 else
1836 c = TOLOWER_ASC(c);
1837#endif
1838 switch (c)
1839 {
1840 case NUL: /* "vim -" read from stdin */
1841 /* "ex -" silent mode */
1842 if (exmode_active)
1843 silent_mode = TRUE;
1844 else
1845 {
1846 if (parmp->edit_type != EDIT_NONE)
1847 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1848 parmp->edit_type = EDIT_STDIN;
1849 read_cmd_fd = 2; /* read from stderr instead of stdin */
1850 }
1851 argv_idx = -1; /* skip to next argument */
1852 break;
1853
1854 case '-': /* "--" don't take any more option arguments */
1855 /* "--help" give help message */
1856 /* "--version" give version message */
1857 /* "--literal" take files literally */
1858 /* "--nofork" don't fork */
1859 /* "--noplugin[s]" skip plugins */
1860 /* "--cmd <cmd>" execute cmd before vimrc */
1861 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1862 usage();
1863 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1864 {
1865 Columns = 80; /* need to init Columns */
1866 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1867 list_version();
1868 msg_putchar('\n');
1869 msg_didout = FALSE;
1870 mch_exit(0);
1871 }
1872 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1873 {
Bram Moolenaar53076832015-12-31 19:53:21 +01001874#ifdef EXPAND_FILENAMES
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001875 parmp->literal = TRUE;
1876#endif
1877 }
1878 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1879 {
1880#ifdef FEAT_GUI
1881 gui.dofork = FALSE; /* don't fork() when starting GUI */
1882#endif
1883 }
1884 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1885 p_lpl = FALSE;
1886 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1887 {
1888 want_argument = TRUE;
1889 argv_idx += 3;
1890 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001891 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1892 {
1893 want_argument = TRUE;
1894 argv_idx += 11;
1895 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001896#ifdef FEAT_CLIENTSERVER
1897 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1898 ; /* already processed -- no arg */
1899 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1900 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1901 {
1902 /* already processed -- snatch the following arg */
1903 if (argc > 1)
1904 {
1905 --argc;
1906 ++argv;
1907 }
1908 }
1909#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001910#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1911# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001912 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001913# else
1914 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1915# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001916 {
1917 /* already processed -- snatch the following arg */
1918 if (argc > 1)
1919 {
1920 --argc;
1921 ++argv;
1922 }
1923 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001924#endif
1925#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001926 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1927 {
1928 /* already processed, skip */
1929 }
1930#endif
1931 else
1932 {
1933 if (argv[0][argv_idx])
1934 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1935 had_minmin = TRUE;
1936 }
1937 if (!want_argument)
1938 argv_idx = -1; /* skip to next argument */
1939 break;
1940
1941 case 'A': /* "-A" start in Arabic mode */
1942#ifdef FEAT_ARABIC
1943 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1944#else
1945 mch_errmsg(_(e_noarabic));
1946 mch_exit(2);
1947#endif
1948 break;
1949
1950 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001951 /* Needs to be effective before expanding file names, because
1952 * for Win32 this makes us edit a shortcut file itself,
1953 * instead of the file it links to. */
1954 set_options_bin(curbuf->b_p_bin, 1, 0);
1955 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001956 break;
1957
1958 case 'C': /* "-C" Compatible */
1959 change_compatible(TRUE);
1960 break;
1961
1962 case 'e': /* "-e" Ex mode */
1963 exmode_active = EXMODE_NORMAL;
1964 break;
1965
1966 case 'E': /* "-E" Improved Ex mode */
1967 exmode_active = EXMODE_VIM;
1968 break;
1969
1970 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1971 window directly, not with newcli */
1972#ifdef FEAT_GUI
1973 gui.dofork = FALSE; /* don't fork() when starting GUI */
1974#endif
1975 break;
1976
1977 case 'g': /* "-g" start GUI */
1978 main_start_gui();
1979 break;
1980
1981 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1982#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001983 p_fkmap = TRUE;
1984 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001985#else
1986 mch_errmsg(_(e_nofarsi));
1987 mch_exit(2);
1988#endif
1989 break;
1990
1991 case 'h': /* "-h" give help message */
1992#ifdef FEAT_GUI_GNOME
1993 /* Tell usage() to exit for "gvim". */
1994 gui.starting = FALSE;
1995#endif
1996 usage();
1997 break;
1998
1999 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
2000#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00002001 p_hkmap = TRUE;
2002 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002003#else
2004 mch_errmsg(_(e_nohebrew));
2005 mch_exit(2);
2006#endif
2007 break;
2008
2009 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
2010#ifdef FEAT_LISP
2011 set_option_value((char_u *)"lisp", 1L, NULL, 0);
2012 p_sm = TRUE;
2013#endif
2014 break;
2015
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002016 case 'M': /* "-M" no changes or writing of files */
2017 reset_modifiable();
2018 /* FALLTHROUGH */
2019
2020 case 'm': /* "-m" no writing of files */
2021 p_write = FALSE;
2022 break;
2023
2024 case 'y': /* "-y" easy mode */
2025#ifdef FEAT_GUI
2026 gui.starting = TRUE; /* start GUI a bit later */
2027#endif
2028 parmp->evim_mode = TRUE;
2029 break;
2030
2031 case 'N': /* "-N" Nocompatible */
2032 change_compatible(FALSE);
2033 break;
2034
2035 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002036#ifdef FEAT_NETBEANS_INTG
2037 /* checking for "-nb", netbeans parameters */
2038 if (argv[0][argv_idx] == 'b')
2039 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002040 netbeansArg = argv[0];
2041 argv_idx = -1; /* skip to next argument */
2042 }
2043 else
2044#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002045 parmp->no_swap_file = TRUE;
2046 break;
2047
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002048 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002049#ifdef TARGET_API_MAC_OSX
2050 /* For some reason on MacOS X, an argument like:
2051 -psn_0_10223617 is passed in when invoke from Finder
2052 or with the 'open' command */
2053 if (argv[0][argv_idx] == 's')
2054 {
2055 argv_idx = -1; /* bypass full -psn */
2056 main_start_gui();
2057 break;
2058 }
2059#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002060#ifdef FEAT_WINDOWS
2061 /* default is 0: open window for each file */
2062 parmp->window_count = get_number_arg((char_u *)argv[0],
2063 &argv_idx, 0);
2064 parmp->window_layout = WIN_TABS;
2065#endif
2066 break;
2067
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002068 case 'o': /* "-o[N]" open N horizontal split windows */
2069#ifdef FEAT_WINDOWS
2070 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002071 parmp->window_count = get_number_arg((char_u *)argv[0],
2072 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002073 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002074#endif
2075 break;
2076
2077 case 'O': /* "-O[N]" open N vertical split windows */
2078#if defined(FEAT_VERTSPLIT) && defined(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_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002083#endif
2084 break;
2085
2086#ifdef FEAT_QUICKFIX
2087 case 'q': /* "-q" QuickFix mode */
2088 if (parmp->edit_type != EDIT_NONE)
2089 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2090 parmp->edit_type = EDIT_QF;
2091 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2092 {
2093 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2094 argv_idx = -1;
2095 }
2096 else if (argc > 1) /* "-q {errorfile}" */
2097 want_argument = TRUE;
2098 break;
2099#endif
2100
2101 case 'R': /* "-R" readonly mode */
2102 readonlymode = TRUE;
2103 curbuf->b_p_ro = TRUE;
2104 p_uc = 10000; /* don't update very often */
2105 break;
2106
2107 case 'r': /* "-r" recovery mode */
2108 case 'L': /* "-L" recovery mode */
2109 recoverymode = 1;
2110 break;
2111
2112 case 's':
2113 if (exmode_active) /* "-s" silent (batch) mode */
2114 silent_mode = TRUE;
2115 else /* "-s {scriptin}" read from script file */
2116 want_argument = TRUE;
2117 break;
2118
2119 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2120 if (parmp->edit_type != EDIT_NONE)
2121 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2122 parmp->edit_type = EDIT_TAG;
2123 if (argv[0][argv_idx]) /* "-t{tag}" */
2124 {
2125 parmp->tagname = (char_u *)argv[0] + argv_idx;
2126 argv_idx = -1;
2127 }
2128 else /* "-t {tag}" */
2129 want_argument = TRUE;
2130 break;
2131
2132#ifdef FEAT_EVAL
2133 case 'D': /* "-D" Debugging */
2134 parmp->use_debug_break_level = 9999;
2135 break;
2136#endif
2137#ifdef FEAT_DIFF
2138 case 'd': /* "-d" 'diff' */
2139# ifdef AMIGA
2140 /* check for "-dev {device}" */
2141 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2142 want_argument = TRUE;
2143 else
2144# endif
2145 parmp->diff_mode = TRUE;
2146 break;
2147#endif
2148 case 'V': /* "-V{N}" Verbose level */
2149 /* default is 10: a little bit verbose */
2150 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2151 if (argv[0][argv_idx] != NUL)
2152 {
2153 set_option_value((char_u *)"verbosefile", 0L,
2154 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002155 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002156 }
2157 break;
2158
2159 case 'v': /* "-v" Vi-mode (as if called "vi") */
2160 exmode_active = 0;
2161#ifdef FEAT_GUI
2162 gui.starting = FALSE; /* don't start GUI */
2163#endif
2164 break;
2165
2166 case 'w': /* "-w{number}" set window height */
2167 /* "-w {scriptout}" write to script */
2168 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2169 {
2170 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2171 set_option_value((char_u *)"window", n, NULL, 0);
2172 break;
2173 }
2174 want_argument = TRUE;
2175 break;
2176
2177#ifdef FEAT_CRYPT
2178 case 'x': /* "-x" encrypted reading/writing of files */
2179 parmp->ask_for_key = TRUE;
2180 break;
2181#endif
2182
2183 case 'X': /* "-X" don't connect to X server */
2184#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2185 x_no_connect = TRUE;
2186#endif
2187 break;
2188
2189 case 'Z': /* "-Z" restricted mode */
2190 restricted = TRUE;
2191 break;
2192
2193 case 'c': /* "-c{command}" or "-c {command}" execute
2194 command */
2195 if (argv[0][argv_idx] != NUL)
2196 {
2197 if (parmp->n_commands >= MAX_ARG_CMDS)
2198 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002199 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2200 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002201 argv_idx = -1;
2202 break;
2203 }
2204 /*FALLTHROUGH*/
2205 case 'S': /* "-S {file}" execute Vim script */
2206 case 'i': /* "-i {viminfo}" use for viminfo */
2207#ifndef FEAT_DIFF
2208 case 'd': /* "-d {device}" device (for Amiga) */
2209#endif
2210 case 'T': /* "-T {terminal}" terminal name */
2211 case 'u': /* "-u {vimrc}" vim inits file */
2212 case 'U': /* "-U {gvimrc}" gvim inits file */
2213 case 'W': /* "-W {scriptout}" overwrite */
2214#ifdef FEAT_GUI_W32
2215 case 'P': /* "-P {parent title}" MDI parent */
2216#endif
2217 want_argument = TRUE;
2218 break;
2219
2220 default:
2221 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2222 }
2223
2224 /*
2225 * Handle option arguments with argument.
2226 */
2227 if (want_argument)
2228 {
2229 /*
2230 * Check for garbage immediately after the option letter.
2231 */
2232 if (argv[0][argv_idx] != NUL)
2233 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2234
2235 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002236 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002237 mainerr_arg_missing((char_u *)argv[0]);
2238 ++argv;
2239 argv_idx = -1;
2240
2241 switch (c)
2242 {
2243 case 'c': /* "-c {command}" execute command */
2244 case 'S': /* "-S {file}" execute Vim script */
2245 if (parmp->n_commands >= MAX_ARG_CMDS)
2246 mainerr(ME_EXTRA_CMD, NULL);
2247 if (c == 'S')
2248 {
2249 char *a;
2250
2251 if (argc < 1)
2252 /* "-S" without argument: use default session file
2253 * name. */
2254 a = SESSION_FILE;
2255 else if (argv[0][0] == '-')
2256 {
2257 /* "-S" followed by another option: use default
2258 * session file name. */
2259 a = SESSION_FILE;
2260 ++argc;
2261 --argv;
2262 }
2263 else
2264 a = argv[0];
2265 p = alloc((unsigned)(STRLEN(a) + 4));
2266 if (p == NULL)
2267 mch_exit(2);
2268 sprintf((char *)p, "so %s", a);
2269 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2270 parmp->commands[parmp->n_commands++] = p;
2271 }
2272 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002273 parmp->commands[parmp->n_commands++] =
2274 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002275 break;
2276
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002277 case '-':
2278 if (argv[-1][2] == 'c')
2279 {
2280 /* "--cmd {command}" execute command */
2281 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2282 mainerr(ME_EXTRA_CMD, NULL);
2283 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002284 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002285 }
2286 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002287 break;
2288
2289 /* case 'd': -d {device} is handled in mch_check_win() for the
2290 * Amiga */
2291
2292#ifdef FEAT_QUICKFIX
2293 case 'q': /* "-q {errorfile}" QuickFix mode */
2294 parmp->use_ef = (char_u *)argv[0];
2295 break;
2296#endif
2297
2298 case 'i': /* "-i {viminfo}" use for viminfo */
2299 use_viminfo = (char_u *)argv[0];
2300 break;
2301
2302 case 's': /* "-s {scriptin}" read from script file */
2303 if (scriptin[0] != NULL)
2304 {
2305scripterror:
2306 mch_errmsg(_("Attempt to open script file again: \""));
2307 mch_errmsg(argv[-1]);
2308 mch_errmsg(" ");
2309 mch_errmsg(argv[0]);
2310 mch_errmsg("\"\n");
2311 mch_exit(2);
2312 }
2313 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2314 {
2315 mch_errmsg(_("Cannot open for reading: \""));
2316 mch_errmsg(argv[0]);
2317 mch_errmsg("\"\n");
2318 mch_exit(2);
2319 }
2320 if (save_typebuf() == FAIL)
2321 mch_exit(2); /* out of memory */
2322 break;
2323
2324 case 't': /* "-t {tag}" */
2325 parmp->tagname = (char_u *)argv[0];
2326 break;
2327
2328 case 'T': /* "-T {terminal}" terminal name */
2329 /*
2330 * The -T term argument is always available and when
2331 * HAVE_TERMLIB is supported it overrides the environment
2332 * variable TERM.
2333 */
2334#ifdef FEAT_GUI
2335 if (term_is_gui((char_u *)argv[0]))
2336 gui.starting = TRUE; /* start GUI a bit later */
2337 else
2338#endif
2339 parmp->term = (char_u *)argv[0];
2340 break;
2341
2342 case 'u': /* "-u {vimrc}" vim inits file */
2343 parmp->use_vimrc = (char_u *)argv[0];
2344 break;
2345
2346 case 'U': /* "-U {gvimrc}" gvim inits file */
2347#ifdef FEAT_GUI
2348 use_gvimrc = (char_u *)argv[0];
2349#endif
2350 break;
2351
2352 case 'w': /* "-w {nr}" 'window' value */
2353 /* "-w {scriptout}" append to script file */
2354 if (vim_isdigit(*((char_u *)argv[0])))
2355 {
2356 argv_idx = 0;
2357 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2358 set_option_value((char_u *)"window", n, NULL, 0);
2359 argv_idx = -1;
2360 break;
2361 }
2362 /*FALLTHROUGH*/
2363 case 'W': /* "-W {scriptout}" overwrite script file */
2364 if (scriptout != NULL)
2365 goto scripterror;
2366 if ((scriptout = mch_fopen(argv[0],
2367 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2368 {
2369 mch_errmsg(_("Cannot open for script output: \""));
2370 mch_errmsg(argv[0]);
2371 mch_errmsg("\"\n");
2372 mch_exit(2);
2373 }
2374 break;
2375
2376#ifdef FEAT_GUI_W32
2377 case 'P': /* "-P {parent title}" MDI parent */
2378 gui_mch_set_parent(argv[0]);
2379 break;
2380#endif
2381 }
2382 }
2383 }
2384
2385 /*
2386 * File name argument.
2387 */
2388 else
2389 {
2390 argv_idx = -1; /* skip to next argument */
2391
2392 /* Check for only one type of editing. */
2393 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2394 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2395 parmp->edit_type = EDIT_FILE;
2396
2397#ifdef MSWIN
2398 /* Remember if the argument was a full path before changing
2399 * slashes to backslashes. */
2400 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2401 parmp->full_path = TRUE;
2402#endif
2403
2404 /* Add the file to the global argument list. */
2405 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2406 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2407 mch_exit(2);
2408#ifdef FEAT_DIFF
2409 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2410 && !mch_isdir(alist_name(&GARGLIST[0])))
2411 {
2412 char_u *r;
2413
2414 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2415 if (r != NULL)
2416 {
2417 vim_free(p);
2418 p = r;
2419 }
2420 }
2421#endif
2422#if defined(__CYGWIN32__) && !defined(WIN32)
2423 /*
2424 * If vim is invoked by non-Cygwin tools, convert away any
2425 * DOS paths, so things like .swp files are created correctly.
2426 * Look for evidence of non-Cygwin paths before we bother.
2427 * This is only for when using the Unix files.
2428 */
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002429 if (vim_strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002430 {
2431 char posix_path[PATH_MAX];
2432
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002433# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2434 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2435# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002436 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002437# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002438 vim_free(p);
Bram Moolenaarfe17e762013-06-29 14:17:02 +02002439 p = vim_strsave((char_u *)posix_path);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002440 if (p == NULL)
2441 mch_exit(2);
2442 }
2443#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002444
2445#ifdef USE_FNAME_CASE
2446 /* Make the case of the file name match the actual file. */
2447 fname_case(p, 0);
2448#endif
2449
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002450 alist_add(&global_alist, p,
Bram Moolenaar53076832015-12-31 19:53:21 +01002451#ifdef EXPAND_FILENAMES
Bram Moolenaar231334e2005-07-25 20:46:57 +00002452 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002453#else
2454 2 /* add buffer number now and use curbuf */
2455#endif
2456 );
2457
2458#if defined(FEAT_MBYTE) && defined(WIN32)
2459 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002460 /* Remember this argument has been added to the argument list.
2461 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002462 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002463# ifdef FEAT_DIFF
2464 parmp->diff_mode
2465# else
2466 FALSE
2467# endif
2468 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002469 }
2470#endif
2471 }
2472
2473 /*
2474 * If there are no more letters after the current "-", go to next
2475 * argument. argv_idx is set to -1 when the current argument is to be
2476 * skipped.
2477 */
2478 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2479 {
2480 --argc;
2481 ++argv;
2482 argv_idx = 1;
2483 }
2484 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002485
2486#ifdef FEAT_EVAL
2487 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2488 * one. */
2489 if (parmp->n_commands > 0)
2490 {
2491 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2492 if (p != NULL)
2493 {
2494 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2495 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2496 vim_free(p);
2497 }
2498 }
2499#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002500}
2501
2502/*
2503 * Print a warning if stdout is not a terminal.
2504 * When starting in Ex mode and commands come from a file, set Silent mode.
2505 */
2506 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002507check_tty(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002508{
2509 int input_isatty; /* is active input a terminal? */
2510
2511 input_isatty = mch_input_isatty();
2512 if (exmode_active)
2513 {
2514 if (!input_isatty)
2515 silent_mode = TRUE;
2516 }
2517 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2518#ifdef FEAT_GUI
2519 /* don't want the delay when started from the desktop */
2520 && !gui.starting
2521#endif
2522 )
2523 {
2524#ifdef NBDEBUG
2525 /*
2526 * This shouldn't be necessary. But if I run netbeans with the log
2527 * output coming to the console and XOpenDisplay fails, I get vim
2528 * trying to start with input/output to my console tty. This fills my
2529 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002530 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002531 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002532 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002533 {
2534 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2535 exit(1);
2536 }
2537#endif
2538 if (!parmp->stdout_isatty)
2539 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2540 if (!input_isatty)
2541 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2542 out_flush();
2543 if (scriptin[0] == NULL)
2544 ui_delay(2000L, TRUE);
2545 TIME_MSG("Warning delay");
2546 }
2547}
2548
2549/*
2550 * Read text from stdin.
2551 */
2552 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002553read_stdin(void)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002554{
2555 int i;
2556
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002557#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002558 /* When getting the ATTENTION prompt here, use a dialog */
2559 swap_exists_action = SEA_DIALOG;
2560#endif
2561 no_wait_return = TRUE;
2562 i = msg_didany;
2563 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002564 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002565 no_wait_return = FALSE;
2566 msg_didany = i;
2567 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002568#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002569 check_swap_exists_action();
2570#endif
2571#if !(defined(AMIGA) || defined(MACOS))
2572 /*
2573 * Close stdin and dup it from stderr. Required for GPM to work
2574 * properly, and for running external commands.
2575 * Is there any other system that cannot do this?
2576 */
2577 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002578 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002579#endif
2580}
2581
2582/*
2583 * Create the requested number of windows and edit buffers in them.
2584 * Also does recovery if "recoverymode" set.
2585 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002586 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002587create_windows(mparm_T *parmp UNUSED)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002588{
2589#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002590 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002591 int done = 0;
2592
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002593 /*
2594 * Create the number of windows that was requested.
2595 */
2596 if (parmp->window_count == -1) /* was not set */
2597 parmp->window_count = 1;
2598 if (parmp->window_count == 0)
2599 parmp->window_count = GARGCOUNT;
2600 if (parmp->window_count > 1)
2601 {
2602 /* Don't change the windows if there was a command in .vimrc that
2603 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002604 if (parmp->window_layout == 0)
2605 parmp->window_layout = WIN_HOR;
2606 if (parmp->window_layout == WIN_TABS)
2607 {
2608 parmp->window_count = make_tabpages(parmp->window_count);
2609 TIME_MSG("making tab pages");
2610 }
2611 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002612 {
2613 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002614 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002615 TIME_MSG("making windows");
2616 }
2617 else
2618 parmp->window_count = win_count();
2619 }
2620 else
2621 parmp->window_count = 1;
2622#endif
2623
2624 if (recoverymode) /* do recover */
2625 {
2626 msg_scroll = TRUE; /* scroll message up */
2627 ml_recover();
2628 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2629 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002630 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002631 }
2632 else
2633 {
2634 /*
2635 * Open a buffer for windows that don't have one yet.
2636 * Commands in the .vimrc might have loaded a file or split the window.
2637 * Watch out for autocommands that delete a window.
2638 */
2639#ifdef FEAT_AUTOCMD
2640 /*
2641 * Don't execute Win/Buf Enter/Leave autocommands here
2642 */
2643 ++autocmd_no_enter;
2644 ++autocmd_no_leave;
2645#endif
2646#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002647 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002648 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002649 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002650 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002651 {
2652 if (parmp->window_layout == WIN_TABS)
2653 goto_tabpage(1);
2654 else
2655 curwin = firstwin;
2656 }
2657 else if (parmp->window_layout == WIN_TABS)
2658 {
2659 if (curtab->tp_next == NULL)
2660 break;
2661 goto_tabpage(0);
2662 }
2663 else
2664 {
2665 if (curwin->w_next == NULL)
2666 break;
2667 curwin = curwin->w_next;
2668 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002669 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002670#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002671 curbuf = curwin->w_buffer;
2672 if (curbuf->b_ml.ml_mfp == NULL)
2673 {
2674#ifdef FEAT_FOLDING
2675 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2676 if (p_fdls >= 0)
2677 curwin->w_p_fdl = p_fdls;
2678#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002679#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002680 /* When getting the ATTENTION prompt here, use a dialog */
2681 swap_exists_action = SEA_DIALOG;
2682#endif
2683 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002684
2685 /* create memfile, read file */
2686 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002687
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002688#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002689 if (swap_exists_action == SEA_QUIT)
2690 {
2691 if (got_int || only_one_window())
2692 {
2693 /* abort selected or quit and only one window */
2694 did_emsg = FALSE; /* avoid hit-enter prompt */
2695 getout(1);
2696 }
2697 /* We can't close the window, it would disturb what
2698 * happens next. Clear the file name and set the arg
2699 * index to -1 to delete it later. */
2700 setfname(curbuf, NULL, NULL, FALSE);
2701 curwin->w_arg_idx = -1;
2702 swap_exists_action = SEA_NONE;
2703 }
2704 else
2705 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002706#endif
2707#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002708 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002709#endif
2710 }
2711#ifdef FEAT_WINDOWS
2712 ui_breakcheck();
2713 if (got_int)
2714 {
2715 (void)vgetc(); /* only break the file loading, not the rest */
2716 break;
2717 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002718 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002719#endif
2720#ifdef FEAT_WINDOWS
2721 if (parmp->window_layout == WIN_TABS)
2722 goto_tabpage(1);
2723 else
2724 curwin = firstwin;
2725 curbuf = curwin->w_buffer;
2726#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002727#ifdef FEAT_AUTOCMD
2728 --autocmd_no_enter;
2729 --autocmd_no_leave;
2730#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002731 }
2732}
2733
2734#ifdef FEAT_WINDOWS
2735 /*
2736 * If opened more than one window, start editing files in the other
2737 * windows. make_windows() has already opened the windows.
2738 */
2739 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002740edit_buffers(
2741 mparm_T *parmp,
2742 char_u *cwd) /* current working dir */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002743{
2744 int arg_idx; /* index in argument list */
2745 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002746 int advance = TRUE;
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002747 win_T *win;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002748
2749# ifdef FEAT_AUTOCMD
2750 /*
2751 * Don't execute Win/Buf Enter/Leave autocommands here
2752 */
2753 ++autocmd_no_enter;
2754 ++autocmd_no_leave;
2755# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002756
2757 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2758 if (curwin->w_arg_idx == -1)
2759 {
2760 win_close(curwin, TRUE);
2761 advance = FALSE;
2762 }
2763
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002764 arg_idx = 1;
2765 for (i = 1; i < parmp->window_count; ++i)
2766 {
Bram Moolenaard87c36e2015-04-03 14:56:49 +02002767 if (cwd != NULL)
2768 mch_chdir((char *)cwd);
Bram Moolenaar84212822006-11-07 21:59:47 +00002769 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2770 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002771 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002772 ++arg_idx;
2773 win_close(curwin, TRUE);
2774 advance = FALSE;
2775 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002776 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002777
Bram Moolenaar84212822006-11-07 21:59:47 +00002778 if (advance)
2779 {
2780 if (parmp->window_layout == WIN_TABS)
2781 {
2782 if (curtab->tp_next == NULL) /* just checking */
2783 break;
2784 goto_tabpage(0);
2785 }
2786 else
2787 {
2788 if (curwin->w_next == NULL) /* just checking */
2789 break;
2790 win_enter(curwin->w_next, FALSE);
2791 }
2792 }
2793 advance = TRUE;
2794
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002795 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002796 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002797 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2798 {
2799 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002800 /* Edit file from arg list, if there is one. When "Quit" selected
2801 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002802# ifdef HAS_SWAP_EXISTS_ACTION
2803 swap_exists_did_quit = FALSE;
2804# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002805 (void)do_ecmd(0, arg_idx < GARGCOUNT
2806 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002807 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002808# ifdef HAS_SWAP_EXISTS_ACTION
2809 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002810 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002811 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002812 if (got_int || only_one_window())
2813 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002814 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002815 did_emsg = FALSE; /* avoid hit-enter prompt */
2816 getout(1);
2817 }
2818 win_close(curwin, TRUE);
2819 advance = FALSE;
2820 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002821# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002822 if (arg_idx == GARGCOUNT - 1)
2823 arg_had_last = TRUE;
2824 ++arg_idx;
2825 }
2826 ui_breakcheck();
2827 if (got_int)
2828 {
2829 (void)vgetc(); /* only break the file loading, not the rest */
2830 break;
2831 }
2832 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002833
2834 if (parmp->window_layout == WIN_TABS)
2835 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002836# ifdef FEAT_AUTOCMD
2837 --autocmd_no_enter;
2838# endif
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002839
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002840 /* make the first window the current window */
2841 win = firstwin;
2842#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
2843 /* Avoid making a preview window the current window. */
2844 while (win->w_p_pvw)
2845 {
2846 win = win->w_next;
2847 if (win == NULL)
2848 {
2849 win = firstwin;
2850 break;
2851 }
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002852 }
2853#endif
Bram Moolenaar74cd6242013-08-22 14:14:27 +02002854 win_enter(win, FALSE);
Bram Moolenaare66f06d2013-06-15 21:54:16 +02002855
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002856# ifdef FEAT_AUTOCMD
2857 --autocmd_no_leave;
2858# endif
2859 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002860 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002861 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2862}
2863#endif /* FEAT_WINDOWS */
2864
2865/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002866 * Execute the commands from --cmd arguments "cmds[cnt]".
2867 */
2868 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002869exe_pre_commands(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002870{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002871 char_u **cmds = parmp->pre_commands;
2872 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002873 int i;
2874
2875 if (cnt > 0)
2876 {
2877 curwin->w_cursor.lnum = 0; /* just in case.. */
2878 sourcing_name = (char_u *)_("pre-vimrc command line");
2879# ifdef FEAT_EVAL
2880 current_SID = SID_CMDARG;
2881# endif
2882 for (i = 0; i < cnt; ++i)
2883 do_cmdline_cmd(cmds[i]);
2884 sourcing_name = NULL;
2885# ifdef FEAT_EVAL
2886 current_SID = 0;
2887# endif
2888 TIME_MSG("--cmd commands");
2889 }
2890}
2891
2892/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002893 * Execute "+", "-c" and "-S" arguments.
2894 */
2895 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002896exe_commands(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002897{
2898 int i;
2899
2900 /*
2901 * We start commands on line 0, make "vim +/pat file" match a
2902 * pattern on line 1. But don't move the cursor when an autocommand
2903 * with g`" was used.
2904 */
2905 msg_scroll = TRUE;
2906 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2907 curwin->w_cursor.lnum = 0;
2908 sourcing_name = (char_u *)"command line";
2909#ifdef FEAT_EVAL
2910 current_SID = SID_CARG;
2911#endif
2912 for (i = 0; i < parmp->n_commands; ++i)
2913 {
2914 do_cmdline_cmd(parmp->commands[i]);
2915 if (parmp->cmds_tofree[i])
2916 vim_free(parmp->commands[i]);
2917 }
2918 sourcing_name = NULL;
2919#ifdef FEAT_EVAL
2920 current_SID = 0;
2921#endif
2922 if (curwin->w_cursor.lnum == 0)
2923 curwin->w_cursor.lnum = 1;
2924
2925 if (!exmode_active)
2926 msg_scroll = FALSE;
2927
2928#ifdef FEAT_QUICKFIX
2929 /* When started with "-q errorfile" jump to first error again. */
2930 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002931 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002932#endif
2933 TIME_MSG("executing command arguments");
2934}
2935
2936/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002937 * Source startup scripts.
2938 */
2939 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01002940source_startup_scripts(mparm_T *parmp)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002941{
2942 int i;
2943
2944 /*
2945 * For "evim" source evim.vim first of all, so that the user can overrule
2946 * any things he doesn't like.
2947 */
2948 if (parmp->evim_mode)
2949 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002950 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002951 TIME_MSG("source evim file");
2952 }
2953
2954 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002955 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002956 * nothing else.
2957 */
2958 if (parmp->use_vimrc != NULL)
2959 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002960 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2961 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002962 {
2963#ifdef FEAT_GUI
2964 if (use_gvimrc == NULL) /* don't load gvimrc either */
2965 use_gvimrc = parmp->use_vimrc;
2966#endif
2967 if (parmp->use_vimrc[2] == 'N')
2968 p_lpl = FALSE; /* don't load plugins either */
2969 }
2970 else
2971 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002972 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002973 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2974 }
2975 }
2976 else if (!silent_mode)
2977 {
2978#ifdef AMIGA
2979 struct Process *proc = (struct Process *)FindTask(0L);
2980 APTR save_winptr = proc->pr_WindowPtr;
2981
2982 /* Avoid a requester here for a volume that doesn't exist. */
2983 proc->pr_WindowPtr = (APTR)-1L;
2984#endif
2985
2986 /*
2987 * Get system wide defaults, if the file name is defined.
2988 */
2989#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002990 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002991#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002992#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002993 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002994#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002995
2996 /*
2997 * Try to read initialization commands from the following places:
2998 * - environment variable VIMINIT
2999 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
3000 * - second user vimrc file ($VIM/.vimrc for Dos)
3001 * - environment variable EXINIT
3002 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
3003 * - second user exrc file ($VIM/.exrc for Dos)
3004 * The first that exists is used, the rest is ignored.
3005 */
3006 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
3007 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003008 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003009#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003010 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
3011 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003012#endif
3013#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003014 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
3015 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00003016#endif
Bram Moolenaar22971aa2013-06-12 20:35:58 +02003017#ifdef USR_VIMRC_FILE4
3018 && do_source((char_u *)USR_VIMRC_FILE4, TRUE,
3019 DOSO_VIMRC) == FAIL
3020#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00003021 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003022 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00003023 {
3024#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003025 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003026#endif
3027 }
3028 }
3029
3030 /*
3031 * Read initialization commands from ".vimrc" or ".exrc" in current
3032 * directory. This is only done if the 'exrc' option is set.
3033 * Because of security reasons we disallow shell and write commands
3034 * now, except for unix if the file is owned by the user or 'secure'
3035 * option has been reset in environment of global ".exrc" or ".vimrc".
3036 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3037 * SYS_VIMRC_FILE.
3038 */
3039 if (p_exrc)
3040 {
3041#if defined(UNIX) || defined(VMS)
3042 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3043 if (!file_owned(VIMRC_FILE))
3044#endif
3045 secure = p_secure;
3046
3047 i = FAIL;
3048 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3049 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3050#ifdef USR_VIMRC_FILE2
3051 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3052 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3053#endif
3054#ifdef USR_VIMRC_FILE3
3055 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3056 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3057#endif
3058#ifdef SYS_VIMRC_FILE
3059 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3060 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3061#endif
3062 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003063 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003064
3065 if (i == FAIL)
3066 {
3067#if defined(UNIX) || defined(VMS)
3068 /* if ".exrc" is not owned by user set 'secure' mode */
3069 if (!file_owned(EXRC_FILE))
3070 secure = p_secure;
3071 else
3072 secure = 0;
3073#endif
3074 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3075 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3076#ifdef USR_EXRC_FILE2
3077 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3078 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3079#endif
3080 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003081 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003082 }
3083 }
3084 if (secure == 2)
3085 need_wait_return = TRUE;
3086 secure = 0;
3087#ifdef AMIGA
3088 proc->pr_WindowPtr = save_winptr;
3089#endif
3090 }
3091 TIME_MSG("sourcing vimrc file(s)");
3092}
3093
3094/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003095 * Setup to start using the GUI. Exit with an error when not available.
3096 */
3097 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003098main_start_gui(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003099{
3100#ifdef FEAT_GUI
3101 gui.starting = TRUE; /* start GUI a bit later */
3102#else
3103 mch_errmsg(_(e_nogvim));
3104 mch_errmsg("\n");
3105 mch_exit(2);
3106#endif
3107}
3108
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003109#endif /* NO_VIM_MAIN */
3110
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003111/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003112 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003113 * Returns FAIL if the environment variable was not executed, OK otherwise.
3114 */
3115 int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003116process_env(
3117 char_u *env,
3118 int is_viminit) /* when TRUE, called for VIMINIT */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003119{
3120 char_u *initstr;
3121 char_u *save_sourcing_name;
3122 linenr_T save_sourcing_lnum;
3123#ifdef FEAT_EVAL
3124 scid_T save_sid;
3125#endif
3126
3127 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3128 {
3129 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003130 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003131 save_sourcing_name = sourcing_name;
3132 save_sourcing_lnum = sourcing_lnum;
3133 sourcing_name = env;
3134 sourcing_lnum = 0;
3135#ifdef FEAT_EVAL
3136 save_sid = current_SID;
3137 current_SID = SID_ENV;
3138#endif
3139 do_cmdline_cmd(initstr);
3140 sourcing_name = save_sourcing_name;
3141 sourcing_lnum = save_sourcing_lnum;
3142#ifdef FEAT_EVAL
3143 current_SID = save_sid;;
3144#endif
3145 return OK;
3146 }
3147 return FAIL;
3148}
3149
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003150#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003151/*
3152 * Return TRUE if we are certain the user owns the file "fname".
3153 * Used for ".vimrc" and ".exrc".
3154 * Use both stat() and lstat() for extra security.
3155 */
3156 static int
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003157file_owned(char *fname)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003158{
3159 struct stat s;
3160# ifdef UNIX
3161 uid_t uid = getuid();
3162# else /* VMS */
3163 uid_t uid = ((getgid() << 16) | getuid());
3164# endif
3165
3166 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3167# ifdef HAVE_LSTAT
3168 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3169# endif
3170 );
3171}
3172#endif
3173
3174/*
3175 * Give an error message main_errors["n"] and exit.
3176 */
3177 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003178mainerr(
3179 int n, /* one of the ME_ defines */
3180 char_u *str) /* extra argument or NULL */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003181{
3182#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3183 reset_signals(); /* kill us with CTRL-C here, if you like */
3184#endif
3185
3186 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003187 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003188 mch_errmsg(_(main_errors[n]));
3189 if (str != NULL)
3190 {
3191 mch_errmsg(": \"");
3192 mch_errmsg((char *)str);
3193 mch_errmsg("\"");
3194 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003195 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003196
3197 mch_exit(1);
3198}
3199
3200 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003201mainerr_arg_missing(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003202{
3203 mainerr(ME_ARG_MISSING, str);
3204}
3205
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003206#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003207/*
3208 * print a message with three spaces prepended and '\n' appended.
3209 */
3210 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003211main_msg(char *s)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003212{
3213 mch_msg(" ");
3214 mch_msg(s);
3215 mch_msg("\n");
3216}
3217
3218/*
3219 * Print messages for "vim -h" or "vim --help" and exit.
3220 */
3221 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003222usage(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003223{
3224 int i;
3225 static char *(use[]) =
3226 {
3227 N_("[file ..] edit specified file(s)"),
3228 N_("- read text from stdin"),
3229 N_("-t tag edit file where tag is defined"),
3230#ifdef FEAT_QUICKFIX
3231 N_("-q [errorfile] edit file with first error")
3232#endif
3233 };
3234
3235#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3236 reset_signals(); /* kill us with CTRL-C here, if you like */
3237#endif
3238
3239 mch_msg(longVersion);
3240 mch_msg(_("\n\nusage:"));
3241 for (i = 0; ; ++i)
3242 {
3243 mch_msg(_(" vim [arguments] "));
3244 mch_msg(_(use[i]));
3245 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3246 break;
3247 mch_msg(_("\n or:"));
3248 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003249#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003250 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003251#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003252
3253 mch_msg(_("\n\nArguments:\n"));
3254 main_msg(_("--\t\t\tOnly file names after this"));
Bram Moolenaar53076832015-12-31 19:53:21 +01003255#ifdef EXPAND_FILENAMES
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256 main_msg(_("--literal\t\tDon't expand wildcards"));
3257#endif
3258#ifdef FEAT_OLE
3259 main_msg(_("-register\t\tRegister this gvim for OLE"));
3260 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3261#endif
3262#ifdef FEAT_GUI
3263 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3264 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3265#endif
3266 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3267 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003268 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003269 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3270#ifdef FEAT_DIFF
3271 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3272#endif
3273 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3274 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3275 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3276 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3277 main_msg(_("-M\t\t\tModifications in text not allowed"));
3278 main_msg(_("-b\t\t\tBinary mode"));
3279#ifdef FEAT_LISP
3280 main_msg(_("-l\t\t\tLisp mode"));
3281#endif
3282 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3283 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003284 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3285#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003286 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003287#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003288 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3289 main_msg(_("-r\t\t\tList swap files and exit"));
3290 main_msg(_("-r (with file name)\tRecover crashed session"));
3291 main_msg(_("-L\t\t\tSame as -r"));
3292#ifdef AMIGA
3293 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3294 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3295#endif
3296#ifdef FEAT_ARABIC
3297 main_msg(_("-A\t\t\tstart in Arabic mode"));
3298#endif
3299#ifdef FEAT_RIGHTLEFT
3300 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3301#endif
3302#ifdef FEAT_FKMAP
3303 main_msg(_("-F\t\t\tStart in Farsi mode"));
3304#endif
3305 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3306 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3307#ifdef FEAT_GUI
3308 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3309#endif
3310 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003311#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003312 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003313 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3314 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003315#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003316 main_msg(_("+\t\t\tStart at end of file"));
3317 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003318 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003319 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3320 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3321 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3322 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3323 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3324#ifdef FEAT_CRYPT
3325 main_msg(_("-x\t\t\tEdit encrypted files"));
3326#endif
3327#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3328# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3329 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3330# endif
3331 main_msg(_("-X\t\t\tDo not connect to X server"));
3332#endif
3333#ifdef FEAT_CLIENTSERVER
3334 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3335 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3336 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3337 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003338# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003339 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003340# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003341 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3342 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3343 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3344 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3345#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003346#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003347 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003348#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349#ifdef FEAT_VIMINFO
3350 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3351#endif
3352 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3353 main_msg(_("--version\t\tPrint version information and exit"));
3354
3355#ifdef FEAT_GUI_X11
3356# ifdef FEAT_GUI_MOTIF
3357 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3358# else
3359# ifdef FEAT_GUI_ATHENA
3360# ifdef FEAT_GUI_NEXTAW
3361 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3362# else
3363 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3364# endif
3365# endif
3366# endif
3367 main_msg(_("-display <display>\tRun vim on <display>"));
3368 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003369 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3370 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3371 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3372 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3373 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3374 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3375 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3376 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3377# ifdef FEAT_GUI_ATHENA
3378 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3379# endif
3380 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3381 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3382 main_msg(_("-xrm <resource>\tSet the specified resource"));
3383#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003384#ifdef FEAT_GUI_GTK
3385 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3386 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3387 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3388 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3389 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003390 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003391 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003392 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003394#ifdef FEAT_GUI_W32
3395 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003396 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003397#endif
3398
3399#ifdef FEAT_GUI_GNOME
3400 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3401 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003402 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003403 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003404 gui.dofork = FALSE;
3405 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406 else
3407#endif
3408 mch_exit(0);
3409}
3410
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003411#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003412/*
3413 * Check the result of the ATTENTION dialog:
3414 * When "Quit" selected, exit Vim.
3415 * When "Recover" selected, recover the file.
3416 */
3417 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003418check_swap_exists_action(void)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003419{
3420 if (swap_exists_action == SEA_QUIT)
3421 getout(1);
3422 handle_swap_exists(NULL);
3423}
3424#endif
3425
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003426#endif
3427
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003428#if defined(STARTUPTIME) || defined(PROTO)
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003429static void time_diff(struct timeval *then, struct timeval *now);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003430
3431static struct timeval prev_timeval;
3432
Bram Moolenaar3f269672009-11-03 11:11:11 +00003433# ifdef WIN3264
3434/*
3435 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3436 */
3437 static int
3438gettimeofday(struct timeval *tv, char *dummy)
3439{
3440 long t = clock();
3441 tv->tv_sec = t / CLOCKS_PER_SEC;
3442 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3443 return 0;
3444}
3445# endif
3446
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003447/*
3448 * Save the previous time before doing something that could nest.
3449 * set "*tv_rel" to the time elapsed so far.
3450 */
3451 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003452time_push(void *tv_rel, void *tv_start)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003453{
3454 *((struct timeval *)tv_rel) = prev_timeval;
3455 gettimeofday(&prev_timeval, NULL);
3456 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3457 - ((struct timeval *)tv_rel)->tv_usec;
3458 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3459 - ((struct timeval *)tv_rel)->tv_sec;
3460 if (((struct timeval *)tv_rel)->tv_usec < 0)
3461 {
3462 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3463 --((struct timeval *)tv_rel)->tv_sec;
3464 }
3465 *(struct timeval *)tv_start = prev_timeval;
3466}
3467
3468/*
3469 * Compute the previous time after doing something that could nest.
3470 * Subtract "*tp" from prev_timeval;
3471 * Note: The arguments are (void *) to avoid trouble with systems that don't
3472 * have struct timeval.
3473 */
3474 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003475time_pop(
3476 void *tp) /* actually (struct timeval *) */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003477{
3478 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3479 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3480 if (prev_timeval.tv_usec < 0)
3481 {
3482 prev_timeval.tv_usec += 1000000;
3483 --prev_timeval.tv_sec;
3484 }
3485}
3486
3487 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003488time_diff(struct timeval *then, struct timeval *now)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003489{
3490 long usec;
3491 long msec;
3492
3493 usec = now->tv_usec - then->tv_usec;
3494 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3495 usec = usec % 1000L;
3496 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3497}
3498
3499 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003500time_msg(
3501 char *mesg,
3502 void *tv_start) /* only for do_source: start time; actually
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003503 (struct timeval *) */
3504{
3505 static struct timeval start;
3506 struct timeval now;
3507
3508 if (time_fd != NULL)
3509 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003510 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003511 {
3512 gettimeofday(&start, NULL);
3513 prev_timeval = start;
3514 fprintf(time_fd, "\n\ntimes in msec\n");
3515 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3516 fprintf(time_fd, " clock elapsed: other lines\n\n");
3517 }
3518 gettimeofday(&now, NULL);
3519 time_diff(&start, &now);
3520 if (((struct timeval *)tv_start) != NULL)
3521 {
3522 fprintf(time_fd, " ");
3523 time_diff(((struct timeval *)tv_start), &now);
3524 }
3525 fprintf(time_fd, " ");
3526 time_diff(&prev_timeval, &now);
3527 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003528 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003529 }
3530}
3531
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003532#endif
3533
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003534#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003535
3536/*
3537 * Common code for the X command server and the Win32 command server.
3538 */
3539
Bram Moolenaar92b8b2d2016-01-29 22:36:45 +01003540static char_u *build_drop_cmd(int filec, char **filev, int tabs, int sendReply);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003541
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003542/*
3543 * Do the client-server stuff, unless "--servername ''" was used.
3544 */
3545 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003546exec_on_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003547{
3548 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3549 {
3550# ifdef WIN32
3551 /* Initialise the client/server messaging infrastructure. */
3552 serverInitMessaging();
3553# endif
3554
3555 /*
3556 * When a command server argument was found, execute it. This may
3557 * exit Vim when it was successful. Otherwise it's executed further
3558 * on. Remember the encoding used here in "serverStrEnc".
3559 */
3560 if (parmp->serverArg)
3561 {
3562 cmdsrv_main(&parmp->argc, parmp->argv,
3563 parmp->serverName_arg, &parmp->serverStr);
3564# ifdef FEAT_MBYTE
3565 parmp->serverStrEnc = vim_strsave(p_enc);
3566# endif
3567 }
3568
3569 /* If we're still running, get the name to register ourselves.
3570 * On Win32 can register right now, for X11 need to setup the
3571 * clipboard first, it's further down. */
3572 parmp->servername = serverMakeName(parmp->serverName_arg,
3573 parmp->argv[0]);
3574# ifdef WIN32
3575 if (parmp->servername != NULL)
3576 {
3577 serverSetName(parmp->servername);
3578 vim_free(parmp->servername);
3579 }
3580# endif
3581 }
3582}
3583
3584/*
3585 * Prepare for running as a Vim server.
3586 */
3587 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003588prepare_server(mparm_T *parmp)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003589{
3590# if defined(FEAT_X11)
3591 /*
3592 * Register for remote command execution with :serversend and --remote
3593 * unless there was a -X or a --servername '' on the command line.
3594 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003595 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003596 */
3597 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3598# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003599 (gui.in_use
3600# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003601 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003602# endif
3603 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003604# endif
3605 parmp->serverName_arg != NULL))
3606 {
3607 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3608 vim_free(parmp->servername);
3609 TIME_MSG("register server name");
3610 }
3611 else
3612 serverDelayedStartName = parmp->servername;
3613# endif
3614
3615 /*
3616 * Execute command ourselves if we're here because the send failed (or
3617 * else we would have exited above).
3618 */
3619 if (parmp->serverStr != NULL)
3620 {
3621 char_u *p;
3622
3623 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3624 parmp->serverStr, &p));
3625 vim_free(p);
3626 }
3627}
3628
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003629 static void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003630cmdsrv_main(
3631 int *argc,
3632 char **argv,
3633 char_u *serverName_arg,
3634 char_u **serverStr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003635{
3636 char_u *res;
3637 int i;
3638 char_u *sname;
3639 int ret;
3640 int didone = FALSE;
3641 int exiterr = 0;
3642 char **newArgV = argv + 1;
3643 int newArgC = 1,
3644 Argc = *argc;
3645 int argtype;
3646#define ARGTYPE_OTHER 0
3647#define ARGTYPE_EDIT 1
3648#define ARGTYPE_EDIT_WAIT 2
3649#define ARGTYPE_SEND 3
3650 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003651 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003652# ifndef FEAT_X11
3653 HWND srv;
3654# else
3655 Window srv;
3656
3657 setup_term_clip();
3658# endif
3659
3660 sname = serverMakeName(serverName_arg, argv[0]);
3661 if (sname == NULL)
3662 return;
3663
3664 /*
3665 * Execute the command server related arguments and remove them
3666 * from the argc/argv array; We may have to return into main()
3667 */
3668 for (i = 1; i < Argc; i++)
3669 {
3670 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003671 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003672 {
3673 for (; i < *argc; i++)
3674 {
3675 *newArgV++ = argv[i];
3676 newArgC++;
3677 }
3678 break;
3679 }
3680
Bram Moolenaareb94e552006-03-11 21:35:11 +00003681 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003682 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003683 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3684 {
3685 char *p = argv[i] + 8;
3686
3687 argtype = ARGTYPE_EDIT;
3688 while (*p != NUL)
3689 {
3690 if (STRNICMP(p, "-wait", 5) == 0)
3691 {
3692 argtype = ARGTYPE_EDIT_WAIT;
3693 p += 5;
3694 }
3695 else if (STRNICMP(p, "-silent", 7) == 0)
3696 {
3697 silent = TRUE;
3698 p += 7;
3699 }
3700 else if (STRNICMP(p, "-tab", 4) == 0)
3701 {
3702 tabs = TRUE;
3703 p += 4;
3704 }
3705 else
3706 {
3707 argtype = ARGTYPE_OTHER;
3708 break;
3709 }
3710 }
3711 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003712 else
3713 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003714
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003715 if (argtype != ARGTYPE_OTHER)
3716 {
3717 if (i == *argc - 1)
3718 mainerr_arg_missing((char_u *)argv[i]);
3719 if (argtype == ARGTYPE_SEND)
3720 {
3721 *serverStr = (char_u *)argv[i + 1];
3722 i++;
3723 }
3724 else
3725 {
3726 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003727 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003728 if (*serverStr == NULL)
3729 {
3730 /* Probably out of memory, exit. */
3731 didone = TRUE;
3732 exiterr = 1;
3733 break;
3734 }
3735 Argc = i;
3736 }
3737# ifdef FEAT_X11
3738 if (xterm_dpy == NULL)
3739 {
3740 mch_errmsg(_("No display"));
3741 ret = -1;
3742 }
3743 else
3744 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3745 NULL, &srv, 0, 0, silent);
3746# else
3747 /* Win32 always works? */
3748 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3749# endif
3750 if (ret < 0)
3751 {
3752 if (argtype == ARGTYPE_SEND)
3753 {
3754 /* Failed to send, abort. */
3755 mch_errmsg(_(": Send failed.\n"));
3756 didone = TRUE;
3757 exiterr = 1;
3758 }
3759 else if (!silent)
3760 /* Let vim start normally. */
3761 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3762 break;
3763 }
3764
3765# ifdef FEAT_GUI_W32
3766 /* Guess that when the server name starts with "g" it's a GUI
3767 * server, which we can bring to the foreground here.
3768 * Foreground() in the server doesn't work very well. */
3769 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3770 SetForegroundWindow(srv);
3771# endif
3772
3773 /*
3774 * For --remote-wait: Wait until the server did edit each
3775 * file. Also detect that the server no longer runs.
3776 */
3777 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3778 {
3779 int numFiles = *argc - i - 1;
3780 int j;
3781 char_u *done = alloc(numFiles);
3782 char_u *p;
3783# ifdef FEAT_GUI_W32
3784 NOTIFYICONDATA ni;
3785 int count = 0;
3786 extern HWND message_window;
3787# endif
3788
3789 if (numFiles > 0 && argv[i + 1][0] == '+')
3790 /* Skip "+cmd" argument, don't wait for it to be edited. */
3791 --numFiles;
3792
3793# ifdef FEAT_GUI_W32
3794 ni.cbSize = sizeof(ni);
3795 ni.hWnd = message_window;
3796 ni.uID = 0;
3797 ni.uFlags = NIF_ICON|NIF_TIP;
3798 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3799 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3800 Shell_NotifyIcon(NIM_ADD, &ni);
3801# endif
3802
3803 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003804 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003805 while (memchr(done, 0, numFiles) != NULL)
3806 {
3807# ifdef WIN32
3808 p = serverGetReply(srv, NULL, TRUE, TRUE);
3809 if (p == NULL)
3810 break;
3811# else
3812 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3813 break;
3814# endif
3815 j = atoi((char *)p);
3816 if (j >= 0 && j < numFiles)
3817 {
3818# ifdef FEAT_GUI_W32
3819 ++count;
3820 sprintf(ni.szTip, _("%d of %d edited"),
3821 count, numFiles);
3822 Shell_NotifyIcon(NIM_MODIFY, &ni);
3823# endif
3824 done[j] = 1;
3825 }
3826 }
3827# ifdef FEAT_GUI_W32
3828 Shell_NotifyIcon(NIM_DELETE, &ni);
3829# endif
3830 }
3831 }
3832 else if (STRICMP(argv[i], "--remote-expr") == 0)
3833 {
3834 if (i == *argc - 1)
3835 mainerr_arg_missing((char_u *)argv[i]);
3836# ifdef WIN32
3837 /* Win32 always works? */
3838 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3839 &res, NULL, 1, FALSE) < 0)
3840# else
3841 if (xterm_dpy == NULL)
3842 mch_errmsg(_("No display: Send expression failed.\n"));
3843 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3844 &res, NULL, 1, 1, FALSE) < 0)
3845# endif
3846 {
3847 if (res != NULL && *res != NUL)
3848 {
3849 /* Output error from remote */
3850 mch_errmsg((char *)res);
3851 vim_free(res);
3852 res = NULL;
3853 }
3854 mch_errmsg(_(": Send expression failed.\n"));
3855 }
3856 }
3857 else if (STRICMP(argv[i], "--serverlist") == 0)
3858 {
3859# ifdef WIN32
3860 /* Win32 always works? */
3861 res = serverGetVimNames();
3862# else
3863 if (xterm_dpy != NULL)
3864 res = serverGetVimNames(xterm_dpy);
3865# endif
3866 if (called_emsg)
3867 mch_errmsg("\n");
3868 }
3869 else if (STRICMP(argv[i], "--servername") == 0)
3870 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003871 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003872 i++;
3873 continue;
3874 }
3875 else
3876 {
3877 *newArgV++ = argv[i];
3878 newArgC++;
3879 continue;
3880 }
3881 didone = TRUE;
3882 if (res != NULL && *res != NUL)
3883 {
3884 mch_msg((char *)res);
3885 if (res[STRLEN(res) - 1] != '\n')
3886 mch_msg("\n");
3887 }
3888 vim_free(res);
3889 }
3890
3891 if (didone)
3892 {
3893 display_errors(); /* display any collected messages */
3894 exit(exiterr); /* Mission accomplished - get out */
3895 }
3896
3897 /* Return back into main() */
3898 *argc = newArgC;
3899 vim_free(sname);
3900}
3901
3902/*
3903 * Build a ":drop" command to send to a Vim server.
3904 */
3905 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01003906build_drop_cmd(
3907 int filec,
3908 char **filev,
3909 int tabs, /* Use ":tab drop" instead of ":drop". */
3910 int sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003911{
3912 garray_T ga;
3913 int i;
3914 char_u *inicmd = NULL;
3915 char_u *p;
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003916 char_u *cdp;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003917 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003918
3919 if (filec > 0 && filev[0][0] == '+')
3920 {
3921 inicmd = (char_u *)filev[0] + 1;
3922 filev++;
3923 filec--;
3924 }
3925 /* Check if we have at least one argument. */
3926 if (filec <= 0)
3927 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003928
3929 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003930 cwd = alloc(MAXPATHL);
3931 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003932 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003933 if (mch_dirname(cwd, MAXPATHL) != OK)
3934 {
3935 vim_free(cwd);
3936 return NULL;
3937 }
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003938 cdp = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003939#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01003940 (char_u *)"", /* rem_backslash() will tell what chars to escape */
Bram Moolenaar02b06312007-09-06 15:39:22 +00003941#else
3942 PATH_ESC_CHARS,
3943#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003944 '\\', TRUE);
3945 vim_free(cwd);
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003946 if (cdp == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003947 return NULL;
3948 ga_init2(&ga, 1, 100);
3949 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003950 ga_concat(&ga, cdp);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003951
3952 /* Call inputsave() so that a prompt for an encryption key works. */
3953 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3954 if (tabs)
3955 ga_concat(&ga, (char_u *)"tab ");
3956 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003957 for (i = 0; i < filec; i++)
3958 {
3959 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003960 * do it again in the Vim server. On MS-Windows only escape
3961 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003962 p = vim_strsave_escaped((char_u *)filev[i],
3963#ifdef UNIX
3964 PATH_ESC_CHARS
3965#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003966 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003967#endif
3968 );
3969 if (p == NULL)
3970 {
3971 vim_free(ga.ga_data);
3972 return NULL;
3973 }
3974 ga_concat(&ga, (char_u *)" ");
3975 ga_concat(&ga, p);
3976 vim_free(p);
3977 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003978 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3979
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003980 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3981 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003982 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3983
3984 /* Switch back to the correct current directory (prior to temporary path
3985 * switch) unless 'autochdir' is set, in which case it will already be
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003986 * correct after the :drop command. With line breaks and spaces:
3987 * if !exists('+acd') || !&acd
3988 * if haslocaldir()
3989 * cd -
3990 * lcd -
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003991 * elseif getcwd() ==# 'current path'
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003992 * cd -
3993 * endif
3994 * endif
3995 */
3996 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|if haslocaldir()|");
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003997 ga_concat(&ga, (char_u *)"cd -|lcd -|elseif getcwd() ==# '");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01003998 ga_concat(&ga, cdp);
Bram Moolenaarfafeee62015-07-03 13:33:01 +02003999 ga_concat(&ga, (char_u *)"'|cd -|endif|endif<CR>");
Bram Moolenaarf11ce662015-03-24 16:48:58 +01004000 vim_free(cdp);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004001
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004002 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01004003 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
4004 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004005 if (inicmd != NULL)
4006 {
4007 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
4008 * the following commands to be inserted as text. Use a "|",
4009 * hopefully "inicmd" does allow this... */
4010 ga_concat(&ga, inicmd);
4011 ga_concat(&ga, (char_u *)"|");
4012 }
4013 /* Bring the window to the foreground, goto Insert mode when 'im' set and
4014 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00004015 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004016 ga_append(&ga, NUL);
4017 return ga.ga_data;
4018}
4019
4020/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004021 * Make our basic server name: use the specified "arg" if given, otherwise use
4022 * the tail of the command "cmd" we were started with.
4023 * Return the name in allocated memory. This doesn't include a serial number.
4024 */
4025 static char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004026serverMakeName(char_u *arg, char *cmd)
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004027{
4028 char_u *p;
4029
4030 if (arg != NULL && *arg != NUL)
4031 p = vim_strsave_up(arg);
4032 else
4033 {
4034 p = vim_strsave_up(gettail((char_u *)cmd));
4035 /* Remove .exe or .bat from the name. */
4036 if (p != NULL && vim_strchr(p, '.') != NULL)
4037 *vim_strchr(p, '.') = NUL;
4038 }
4039 return p;
4040}
4041#endif /* FEAT_CLIENTSERVER */
4042
4043#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4044/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004045 * Replace termcodes such as <CR> and insert as key presses if there is room.
4046 */
4047 void
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004048server_to_input_buf(char_u *str)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004049{
4050 char_u *ptr = NULL;
4051 char_u *cpo_save = p_cpo;
4052
4053 /* Set 'cpoptions' the way we want it.
4054 * B set - backslashes are *not* treated specially
4055 * k set - keycodes are *not* reverse-engineered
4056 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004057 * The last but one parameter of replace_termcodes() is TRUE so that the
4058 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004059 */
4060 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004061 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004062 p_cpo = cpo_save;
4063
4064 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4065 {
4066 /*
4067 * Add the string to the input stream.
4068 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4069 *
4070 * First clear typed characters from the typeahead buffer, there could
4071 * be half a mapping there. Then append to the existing string, so
4072 * that multiple commands from a client are concatenated.
4073 */
4074 if (typebuf.tb_maplen < typebuf.tb_len)
4075 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4076 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4077
4078 /* Let input_available() know we inserted text in the typeahead
4079 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004080 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004081 }
4082 vim_free((char_u *)ptr);
4083}
4084
4085/*
4086 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004087 */
4088 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004089eval_client_expr_to_string(char_u *expr)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090{
4091 char_u *res;
4092 int save_dbl = debug_break_level;
4093 int save_ro = redir_off;
4094
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004095 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4096 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004097 debug_break_level = -1;
4098 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004099 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4100 * to be typed. Do generate errors so that try/catch works. */
4101 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004102
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004103 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004104
4105 debug_break_level = save_dbl;
4106 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004107 --emsg_silent;
4108 if (emsg_silent < 0)
4109 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004110
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004111 /* A client can tell us to redraw, but not to display the cursor, so do
4112 * that here. */
4113 setcursor();
4114 out_flush();
4115#ifdef FEAT_GUI
4116 if (gui.in_use)
4117 gui_update_cursor(FALSE, FALSE);
4118#endif
4119
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004120 return res;
4121}
4122
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004123/*
4124 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4125 * return an allocated string. Otherwise return "data".
4126 * "*tofree" is set to the result when it needs to be freed later.
4127 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004128 char_u *
Bram Moolenaar52ea13d2016-01-30 18:51:09 +01004129serverConvert(
4130 char_u *client_enc UNUSED,
4131 char_u *data,
4132 char_u **tofree)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004133{
4134 char_u *res = data;
4135
4136 *tofree = NULL;
4137# ifdef FEAT_MBYTE
4138 if (client_enc != NULL && p_enc != NULL)
4139 {
4140 vimconv_T vimconv;
4141
4142 vimconv.vc_type = CONV_NONE;
4143 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4144 && vimconv.vc_type != CONV_NONE)
4145 {
4146 res = string_convert(&vimconv, data, NULL);
4147 if (res == NULL)
4148 res = data;
4149 else
4150 *tofree = res;
4151 }
4152 convert_setup(&vimconv, NULL, NULL);
4153 }
4154# endif
4155 return res;
4156}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004157#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004158
4159/*
4160 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4161 */
4162#if defined(FEAT_FKMAP) || defined(PROTO)
4163# include "farsi.c"
4164#endif
4165
4166/*
4167 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4168 */
4169#if defined(FEAT_ARABIC) || defined(PROTO)
4170# include "arabic.c"
4171#endif