blob: 66726d2790bfd2b843d45997e2f75d7414e5fad5 [file] [log] [blame]
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
Bram Moolenaarb4210b32004-06-13 14:51:16 +000010#define EXTERN
11#include "vim.h"
12
13#ifdef SPAWNO
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +000014# include <spawno.h> /* special MS-DOS swapping library */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000015#endif
16
Bram Moolenaarb4210b32004-06-13 14:51:16 +000017#ifdef __CYGWIN__
18# ifndef WIN32
Bram Moolenaar0d1498e2008-06-29 12:00:49 +000019# include <cygwin/version.h>
20# include <sys/cygwin.h> /* for cygwin_conv_to_posix_path() and/or
21 * cygwin_conv_path() */
Bram Moolenaarb4210b32004-06-13 14:51:16 +000022# endif
23# include <limits.h>
24#endif
25
Bram Moolenaarc013cb62005-07-24 21:18:31 +000026/* Maximum number of commands from + or -c arguments. */
27#define MAX_ARG_CMDS 10
28
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000029/* values for "window_layout" */
30#define WIN_HOR 1 /* "-o" horizontally split windows */
31#define WIN_VER 2 /* "-O" vertically split windows */
32#define WIN_TABS 3 /* "-p" windows on tab pages */
33
Bram Moolenaar58d98232005-07-23 22:25:46 +000034/* Struct for various parameters passed between main() and other functions. */
35typedef struct
36{
Bram Moolenaarc013cb62005-07-24 21:18:31 +000037 int argc;
38 char **argv;
39
40 int evim_mode; /* started as "evim" */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000041 char_u *use_vimrc; /* vimrc from -u argument */
42
43 int n_commands; /* no. of commands from + or -c */
44 char_u *commands[MAX_ARG_CMDS]; /* commands from + or -c arg. */
45 char_u cmds_tofree[MAX_ARG_CMDS]; /* commands that need free() */
46 int n_pre_commands; /* no. of commands from --cmd */
47 char_u *pre_commands[MAX_ARG_CMDS]; /* commands from --cmd argument */
48
49 int edit_type; /* type of editing to do */
50 char_u *tagname; /* tag from -t argument */
51#ifdef FEAT_QUICKFIX
52 char_u *use_ef; /* 'errorfile' from -q argument */
53#endif
54
55 int want_full_screen;
56 int stdout_isatty; /* is stdout a terminal? */
57 char_u *term; /* specified terminal name */
58#ifdef FEAT_CRYPT
59 int ask_for_key; /* -x argument */
60#endif
61 int no_swap_file; /* "-n" argument used */
62#ifdef FEAT_EVAL
63 int use_debug_break_level;
64#endif
65#ifdef FEAT_WINDOWS
66 int window_count; /* number of windows to use */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000067 int window_layout; /* 0, WIN_HOR, WIN_VER or WIN_TABS */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000068#endif
69
70#ifdef FEAT_CLIENTSERVER
Bram Moolenaar58d98232005-07-23 22:25:46 +000071 int serverArg; /* TRUE when argument for a server */
72 char_u *serverName_arg; /* cmdline arg for server name */
Bram Moolenaarc013cb62005-07-24 21:18:31 +000073 char_u *serverStr; /* remote server command */
74 char_u *serverStrEnc; /* encoding of serverStr */
75 char_u *servername; /* allocated name for our server */
76#endif
77#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
78 int literal; /* don't expand file names */
79#endif
80#ifdef MSWIN
81 int full_path; /* file name argument was full path */
82#endif
83#ifdef FEAT_DIFF
84 int diff_mode; /* start with 'diff' set */
85#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +000086} mparm_T;
87
Bram Moolenaarc013cb62005-07-24 21:18:31 +000088/* Values for edit_type. */
89#define EDIT_NONE 0 /* no edit type yet */
90#define EDIT_FILE 1 /* file name argument[s] given, use argument list */
91#define EDIT_STDIN 2 /* read file from stdin */
92#define EDIT_TAG 3 /* tag name argument given, use tagname */
93#define EDIT_QF 4 /* start in quickfix mode */
94
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010095#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +000096static int file_owned __ARGS((char *fname));
97#endif
98static void mainerr __ARGS((int, char_u *));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +010099#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000100static void main_msg __ARGS((char *s));
101static void usage __ARGS((void));
102static int get_number_arg __ARGS((char_u *p, int *idx, int def));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100103# if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000104static void init_locale __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100105# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000106static void parse_command_name __ARGS((mparm_T *parmp));
107static void early_arg_scan __ARGS((mparm_T *parmp));
108static void command_line_scan __ARGS((mparm_T *parmp));
109static void check_tty __ARGS((mparm_T *parmp));
110static void read_stdin __ARGS((void));
111static void create_windows __ARGS((mparm_T *parmp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100112# ifdef FEAT_WINDOWS
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000113static void edit_buffers __ARGS((mparm_T *parmp));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100114# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000115static void exe_pre_commands __ARGS((mparm_T *parmp));
116static void exe_commands __ARGS((mparm_T *parmp));
Bram Moolenaar58d98232005-07-23 22:25:46 +0000117static void source_startup_scripts __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000118static void main_start_gui __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100119# if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000120static void check_swap_exists_action __ARGS((void));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100121# endif
122# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000123static void exec_on_server __ARGS((mparm_T *parmp));
124static void prepare_server __ARGS((mparm_T *parmp));
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000125static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
126static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100127# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000128#endif
129
130
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000131/*
132 * Different types of error messages.
133 */
134static char *(main_errors[]) =
135{
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000136 N_("Unknown option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000137#define ME_UNKNOWN_OPTION 0
138 N_("Too many edit arguments"),
139#define ME_TOO_MANY_ARGS 1
140 N_("Argument missing after"),
141#define ME_ARG_MISSING 2
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000142 N_("Garbage after option argument"),
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000143#define ME_GARBAGE 3
144 N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
145#define ME_EXTRA_CMD 4
146 N_("Invalid argument for"),
147#define ME_INVALID_ARG 5
148};
149
Bram Moolenaarb05b10a2011-03-22 18:10:45 +0100150#ifndef PROTO /* don't want a prototype for main() */
Bram Moolenaar8866d272012-11-28 15:55:42 +0100151#ifndef NO_VIM_MAIN /* skip this for unittests */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000152 int
153# ifdef VIMDLL
154_export
155# endif
156# ifdef FEAT_GUI_MSWIN
157# ifdef __BORLANDC__
158_cdecl
159# endif
160VimMain
161# else
162main
163# endif
164(argc, argv)
165 int argc;
166 char **argv;
167{
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000168 char_u *fname = NULL; /* file name from command line */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000169 mparm_T params; /* various parameters passed between
170 * main() and other functions. */
Bram Moolenaar3f269672009-11-03 11:11:11 +0000171#ifdef STARTUPTIME
172 int i;
173#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000174
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000175 /*
176 * Do any system-specific initialisations. These can NOT use IObuff or
177 * NameBuff. Thus emsg2() cannot be called!
178 */
179 mch_early_init();
180
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000181 /* Many variables are in "params" so that we can pass them to invoked
182 * functions without a lot of arguments. "argc" and "argv" are also
183 * copied, so that they can be changed. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000184 vim_memset(&params, 0, sizeof(params));
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000185 params.argc = argc;
186 params.argv = argv;
187 params.want_full_screen = TRUE;
188#ifdef FEAT_EVAL
189 params.use_debug_break_level = -1;
190#endif
191#ifdef FEAT_WINDOWS
192 params.window_count = -1;
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000193#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +0000194
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000195#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000196 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000197#endif
198
199#ifdef MEM_PROFILE
200 atexit(vim_mem_profile_dump);
201#endif
202
203#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000204 for (i = 1; i < argc; ++i)
205 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000206 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000207 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000208 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000209 TIME_MSG("--- VIM STARTING ---");
210 break;
211 }
212 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000213#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000214 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000215
216#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000217 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000218#endif
219
220#ifdef FEAT_MBYTE
221 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
222#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000223#ifdef FEAT_EVAL
224 eval_init(); /* init global variables */
225#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000226
227#ifdef __QNXNTO__
228 qnx_init(); /* PhAttach() for clipboard, (and gui) */
229#endif
230
231#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000232 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000234 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000235 TIME_MSG("GUI prepared");
236#endif
237
238 /* Init the table of Normal mode commands. */
239 init_normal_cmds();
240
241#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000242 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000243#endif
244
245 /*
246 * Allocate space for the generic buffers (needed for set_init_1() and
247 * EMSG2()).
248 */
249 if ((IObuff = alloc(IOSIZE)) == NULL
250 || (NameBuff = alloc(MAXPATHL)) == NULL)
251 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000252 TIME_MSG("Allocated generic buffers");
253
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000254#ifdef NBDEBUG
255 /* Wait a moment for debugging NetBeans. Must be after allocating
256 * NameBuff. */
257 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
258 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000259 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000260#endif
261
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000262#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
263 /*
264 * Setup to use the current locale (for ctype() and many other things).
265 * NOTE: Translated messages with encodings other than latin1 will not
266 * work until set_init_1() has been called!
267 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000268 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269 TIME_MSG("locale set");
270#endif
271
272#ifdef FEAT_GUI
273 gui.dofork = TRUE; /* default is to use fork() */
274#endif
275
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000276 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000277 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000278 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000279 * --server...
280 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000281 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000282 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000283 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000284
285#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000286 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000287#endif
288#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000289 /* Prepare for possibly starting GUI sometime */
290 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291 TIME_MSG("GUI prepared");
292#endif
293
294#ifdef FEAT_CLIPBOARD
295 clip_init(FALSE); /* Initialise clipboard stuff */
296 TIME_MSG("clipboard setup");
297#endif
298
299 /*
300 * Check if we have an interactive window.
301 * On the Amiga: If there is no window, we open one with a newcli command
302 * (needed for :! to * work). mch_check_win() will also handle the -d or
303 * -dev argument.
304 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000305 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000306 TIME_MSG("window checked");
307
308 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000309 * Allocate the first window and buffer.
310 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000311 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000312 if (win_alloc_first() == FAIL)
313 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000314
315 init_yank(); /* init yank buffers */
316
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000317 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000318
319 /*
320 * Set the default values for the options.
321 * NOTE: Non-latin1 translated messages are working only after this,
322 * because this is where "has_mbyte" will be set, which is used by
323 * msg_outtrans_len_attr().
324 * First find out the home directory, needed to expand "~" in options.
325 */
326 init_homedir(); /* find real value of $HOME */
327 set_init_1();
328 TIME_MSG("inits 1");
329
330#ifdef FEAT_EVAL
331 set_lang_var(); /* set v:lang and v:ctype */
332#endif
333
334#ifdef FEAT_CLIENTSERVER
335 /*
336 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000337 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000338 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000339 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000340#endif
341
342 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000343 * Figure out the way to work from the command name argv[0].
344 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000345 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000346 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000347
348 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000349 * Process the command line arguments. File names are put in the global
350 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000351 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000352 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000353 TIME_MSG("parsing arguments");
354
355 /*
356 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000357 * there is no terminal version, and on Windows we can't fork one off with
358 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000359 */
360#ifdef ALWAYS_USE_GUI
361 gui.starting = TRUE;
362#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000363# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000364 /*
365 * Check if the GUI can be started. Reset gui.starting if not.
366 * Don't know about other systems, stay on the safe side and don't check.
367 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000368 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000369 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000370 if (gui_init_check() == FAIL)
371 {
372 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000373
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000374 /* When running "evim" or "gvim -y" we need the menus, exit if we
375 * don't have them. */
376 if (params.evim_mode)
377 mch_exit(1);
378 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000379 }
380# endif
381#endif
382
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000383 if (GARGCOUNT > 0)
384 {
385#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
386 /*
387 * Expand wildcards in file names.
388 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000389 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000390 {
391 /* Temporarily add '(' and ')' to 'isfname'. These are valid
392 * filename characters but are excluded from 'isfname' to make
393 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
394 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000395 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000396 do_cmdline_cmd((char_u *)":set isf&");
397 }
398#endif
399 fname = alist_name(&GARGLIST[0]);
400 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000401
402#if defined(WIN32) && defined(FEAT_MBYTE)
403 {
404 extern void set_alist_count(void);
405
406 /* Remember the number of entries in the argument list. If it changes
407 * we don't react on setting 'encoding'. */
408 set_alist_count();
409 }
410#endif
411
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000412#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000413 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000414 {
415 /*
416 * If there is one filename, fully qualified, we have very probably
417 * been invoked from explorer, so change to the file's directory.
418 * Hint: to avoid this when typing a command use a forward slash.
419 * If the cd fails, it doesn't matter.
420 */
421 (void)vim_chdirfile(fname);
422 }
423#endif
424 TIME_MSG("expanding arguments");
425
426#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000427 if (params.diff_mode && params.window_count == -1)
428 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000429#endif
430
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000431 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000432 ++RedrawingDisabled;
433
434 /*
435 * When listing swap file names, don't do cursor positioning et. al.
436 */
437 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000438 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000439
440 /*
441 * When certain to start the GUI, don't check capabilities of terminal.
442 * For GTK we can't be sure, but when started from the desktop it doesn't
443 * make sense to try using a terminal.
444 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000445#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000446 if (gui.starting
447# ifdef FEAT_GUI_GTK
448 && !isatty(2)
449# endif
450 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000451 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000452#endif
453
454#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
455 /* When the GUI is started from Finder, need to display messages in a
456 * message box. isatty(2) returns TRUE anyway, thus we need to check the
457 * name to know we're not started from a terminal. */
458 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000459 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000460 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000461
462 /* Avoid always using "/" as the current directory. Note that when
463 * started from Finder the arglist will be filled later in
464 * HandleODocAE() and "fname" will be NULL. */
465 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
466 && STRCMP(NameBuff, "/") == 0)
467 {
468 if (fname != NULL)
469 (void)vim_chdirfile(fname);
470 else
471 {
472 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
473 vim_chdir(NameBuff);
474 }
475 }
476 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000477#endif
478
479 /*
480 * mch_init() sets up the terminal (window) for use. This must be
481 * done after resetting full_screen, otherwise it may move the cursor
482 * (MSDOS).
483 * Note that we may use mch_exit() before mch_init()!
484 */
485 mch_init();
486 TIME_MSG("shell init");
487
488#ifdef USE_XSMP
489 /*
490 * For want of anywhere else to do it, try to connect to xsmp here.
491 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
492 * Hijacking -X 'no X connection' to also disable XSMP connection as that
493 * has a similar delay upon failure.
494 * Only try if SESSION_MANAGER is set to something non-null.
495 */
496 if (!x_no_connect)
497 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000498 char *p = getenv("SESSION_MANAGER");
499
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000500 if (p != NULL && *p != NUL)
501 {
502 xsmp_init();
503 TIME_MSG("xsmp init");
504 }
505 }
506#endif
507
508 /*
509 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000510 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000511 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000512
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 /* This message comes before term inits, but after setting "silent_mode"
514 * when the input is not a tty. */
515 if (GARGCOUNT > 1 && !silent_mode)
516 printf(_("%d files to edit\n"), GARGCOUNT);
517
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000518 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000520 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000521 capabilities (will set full_screen) */
522 screen_start(); /* don't know where cursor is now */
523 TIME_MSG("Termcap init");
524 }
525
526 /*
527 * Set the default values for the options that use Rows and Columns.
528 */
529 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000530 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000531#ifdef FEAT_DIFF
532 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
533 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000534 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000535 diff_win_options(firstwin, FALSE);
536#endif
537
538 cmdline_row = Rows - p_ch;
539 msg_row = cmdline_row;
540 screenalloc(FALSE); /* allocate screen buffers */
541 set_init_2();
542 TIME_MSG("inits 2");
543
544 msg_scroll = TRUE;
545 no_wait_return = TRUE;
546
547 init_mappings(); /* set up initial mappings */
548
549 init_highlight(TRUE, FALSE); /* set the default highlight groups */
550 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000551
552#ifdef FEAT_EVAL
553 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000554 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000555#endif
556
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100557#ifdef FEAT_MZSCHEME
558 /*
559 * Newer version of MzScheme (Racket) require earlier (trampolined)
560 * initialisation via scheme_main_setup.
561 * Implement this by initialising it as early as possible
562 * and splitting off remaining Vim main into vim_main2
563 */
564 {
565 /* Pack up preprocessed command line arguments.
566 * It is safe because Scheme does not access argc/argv. */
567 char *args[2];
568 args[0] = (char *)fname;
569 args[1] = (char *)&params;
570 return mzscheme_main(2, args);
571 }
572}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100573#endif
574#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100575
Bram Moolenaar8866d272012-11-28 15:55:42 +0100576/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
577 * NO_VIM_MAIN is defined. */
578#ifdef FEAT_MZSCHEME
579 int
580vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100581{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100582# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100583 char_u *fname = (char_u *)argv[0];
584 mparm_T params;
585
586 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100587# else
588 return 0;
589}
590# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100591#endif
592
Bram Moolenaar8866d272012-11-28 15:55:42 +0100593#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000594 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000595 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000596
Bram Moolenaar58d98232005-07-23 22:25:46 +0000597 /* Source startup scripts. */
598 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000599
600#ifdef FEAT_EVAL
601 /*
602 * Read all the plugin files.
603 * Only when compiled with +eval, since most plugins need it.
604 */
605 if (p_lpl)
606 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000607# ifdef VMS /* Somehow VMS doesn't handle the "**". */
608 source_runtime((char_u *)"plugin/*.vim", TRUE);
609# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000610 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000611# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000612 TIME_MSG("loading plugins");
613 }
614#endif
615
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000616#ifdef FEAT_DIFF
617 /* Decide about window layout for diff mode after reading vimrc. */
618 if (params.diff_mode && params.window_layout == 0)
619 {
620 if (diffopt_horizontal())
621 params.window_layout = WIN_HOR; /* use horizontal split */
622 else
623 params.window_layout = WIN_VER; /* use vertical split */
624 }
625#endif
626
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000627 /*
628 * Recovery mode without a file name: List swap files.
629 * This uses the 'dir' option, therefore it must be after the
630 * initializations.
631 */
632 if (recoverymode && fname == NULL)
633 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200634 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000635 mch_exit(0);
636 }
637
638 /*
639 * Set a few option defaults after reading .vimrc files:
640 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
641 */
642 set_init_3();
643 TIME_MSG("inits 3");
644
645 /*
646 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
647 * Note that this overrides anything from a vimrc file.
648 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000649 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000650 p_uc = 0;
651
652#ifdef FEAT_FKMAP
653 if (curwin->w_p_rl && p_altkeymap)
654 {
655 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
656# ifdef FEAT_ARABIC
657 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
658# endif
659 p_fkmap = TRUE; /* Set the Farsi keymap mode */
660 }
661#endif
662
663#ifdef FEAT_GUI
664 if (gui.starting)
665 {
666#if defined(UNIX) || defined(VMS)
667 /* When something caused a message from a vimrc script, need to output
668 * an extra newline before the shell prompt. */
669 if (did_emsg || msg_didout)
670 putchar('\n');
671#endif
672
673 gui_start(); /* will set full_screen to TRUE */
674 TIME_MSG("starting GUI");
675
676 /* When running "evim" or "gvim -y" we need the menus, exit if we
677 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000678 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000679 mch_exit(1);
680 }
681#endif
682
683#ifdef SPAWNO /* special MSDOS swapping library */
684 init_SPAWNO("", SWAP_ANY);
685#endif
686
687#ifdef FEAT_VIMINFO
688 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000689 * Read in registers, history etc, but not marks, from the viminfo file.
690 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000691 */
692 if (*p_viminfo != NUL)
693 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000694 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000695 TIME_MSG("reading viminfo");
696 }
697#endif
698
699#ifdef FEAT_QUICKFIX
700 /*
701 * "-q errorfile": Load the error file now.
702 * If the error file can't be read, exit before doing anything else.
703 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000704 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000705 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000706 if (params.use_ef != NULL)
707 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000708 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200709 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
710 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000711 {
712 out_char('\n');
713 mch_exit(3);
714 }
715 TIME_MSG("reading errorfile");
716 }
717#endif
718
719 /*
720 * Start putting things on the screen.
721 * Scroll screen down before drawing over it
722 * Clear screen now, so file message will not be cleared.
723 */
724 starting = NO_BUFFERS;
725 no_wait_return = FALSE;
726 if (!exmode_active)
727 msg_scroll = FALSE;
728
729#ifdef FEAT_GUI
730 /*
731 * This seems to be required to make callbacks to be called now, instead
732 * of after things have been put on the screen, which then may be deleted
733 * when getting a resize callback.
734 * For the Mac this handles putting files dropped on the Vim icon to
735 * global_alist.
736 */
737 if (gui.in_use)
738 {
739# ifdef FEAT_SUN_WORKSHOP
740 if (!usingSunWorkShop)
741# endif
742 gui_wait_for_chars(50L);
743 TIME_MSG("GUI delay");
744 }
745#endif
746
747#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
748 qnx_clip_init();
749#endif
750
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200751#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
752 clip_init(TRUE);
753#endif
754
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000755#ifdef FEAT_XCLIPBOARD
756 /* Start using the X clipboard, unless the GUI was started. */
757# ifdef FEAT_GUI
758 if (!gui.in_use)
759# endif
760 {
761 setup_term_clip();
762 TIME_MSG("setup clipboard");
763 }
764#endif
765
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000766#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000767 /* Prepare for being a Vim server. */
768 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000769#endif
770
771 /*
772 * If "-" argument given: Read file from stdin.
773 * Do this before starting Raw mode, because it may change things that the
774 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
775 * are the same terminal: "cat | vim -".
776 * Using autocommands here may cause trouble...
777 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000778 if (params.edit_type == EDIT_STDIN && !recoverymode)
779 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000780
781#if defined(UNIX) || defined(VMS)
782 /* When switching screens and something caused a message from a vimrc
783 * script, need to output an extra newline on exit. */
784 if ((did_emsg || msg_didout) && *T_TI != NUL)
785 newline_on_exit = TRUE;
786#endif
787
788 /*
789 * When done something that is not allowed or error message call
790 * wait_return. This must be done before starttermcap(), because it may
791 * switch to another screen. It must be done after settmode(TMODE_RAW),
792 * because we want to react on a single key stroke.
793 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000794 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000795 */
796 settmode(TMODE_RAW);
797 TIME_MSG("setting raw mode");
798
799 if (need_wait_return || msg_didany)
800 {
801 wait_return(TRUE);
802 TIME_MSG("waiting for return");
803 }
804
805 starttermcap(); /* start termcap if not done by wait_return() */
806 TIME_MSG("start termcap");
807
808#ifdef FEAT_MOUSE
809 setmouse(); /* may start using the mouse */
810#endif
811 if (scroll_region)
812 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000813 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000814
815 /*
816 * Don't clear the screen when starting in Ex mode, unless using the GUI.
817 */
818 if (exmode_active
819#ifdef FEAT_GUI
820 && !gui.in_use
821#endif
822 )
823 must_redraw = CLEAR;
824 else
825 {
826 screenclear(); /* clear screen */
827 TIME_MSG("clearing screen");
828 }
829
830#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000831 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000832 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200833 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000834 (void)get_crypt_key(TRUE, TRUE);
835 TIME_MSG("getting crypt key");
836 }
837#endif
838
839 no_wait_return = TRUE;
840
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000841 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000842 * Create the requested number of windows and edit buffers in them.
843 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000845 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000846 TIME_MSG("opening buffers");
847
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000848#ifdef FEAT_EVAL
849 /* clear v:swapcommand */
850 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
851#endif
852
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000853 /* Ex starts at last line of the file */
854 if (exmode_active)
855 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
856
857#ifdef FEAT_AUTOCMD
858 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
859 TIME_MSG("BufEnter autocommands");
860#endif
861 setpcmark();
862
863#ifdef FEAT_QUICKFIX
864 /*
865 * When started with "-q errorfile" jump to first error now.
866 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000867 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000868 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000869 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000870 TIME_MSG("jump to first error");
871 }
872#endif
873
874#ifdef FEAT_WINDOWS
875 /*
876 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000877 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000879 edit_buffers(&params);
880#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881
882#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000883 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884 {
885 win_T *wp;
886
887 /* set options in each window for "vimdiff". */
888 for (wp = firstwin; wp != NULL; wp = wp->w_next)
889 diff_win_options(wp, TRUE);
890 }
891#endif
892
893 /*
894 * Shorten any of the filenames, but only when absolute.
895 */
896 shorten_fnames(FALSE);
897
898 /*
899 * Need to jump to the tag before executing the '-c command'.
900 * Makes "vim -c '/return' -t main" work.
901 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000902 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000903 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000904#if defined(HAS_SWAP_EXISTS_ACTION)
905 swap_exists_did_quit = FALSE;
906#endif
907
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000908 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000909 do_cmdline_cmd(IObuff);
910 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000911
912#if defined(HAS_SWAP_EXISTS_ACTION)
913 /* If the user doesn't want to edit the file then we quit here. */
914 if (swap_exists_did_quit)
915 getout(1);
916#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000917 }
918
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000919 /* Execute any "+", "-c" and "-S" arguments. */
920 if (params.n_commands > 0)
921 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000922
923 RedrawingDisabled = 0;
924 redraw_all_later(NOT_VALID);
925 no_wait_return = FALSE;
926 starting = 0;
927
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000928#ifdef FEAT_TERMRESPONSE
929 /* Requesting the termresponse is postponed until here, so that a "-c q"
930 * argument doesn't make it appear in the shell Vim was started from. */
931 may_req_termresponse();
932#endif
933
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000934 /* start in insert mode */
935 if (p_im)
936 need_start_insertmode = TRUE;
937
938#ifdef FEAT_AUTOCMD
939 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
940 TIME_MSG("VimEnter autocommands");
941#endif
942
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200943#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
944 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
945 * done after the clipboard is available and all initial commands that may
946 * modify the 'clipboard' setting have run; i.e. just before entering the
947 * main loop. */
948 {
949 int default_regname = 0;
950 adjust_clip_reg(&default_regname);
951 set_reg_var(default_regname);
952 }
953#endif
954
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000955#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
956 /* When a startup script or session file setup for diff'ing and
957 * scrollbind, sync the scrollbind now. */
958 if (curwin->w_p_diff && curwin->w_p_scb)
959 {
960 update_topline();
961 check_scrollbind((linenr_T)0, 0L);
962 TIME_MSG("diff scrollbinding");
963 }
964#endif
965
966#if defined(WIN3264) && !defined(FEAT_GUI_W32)
967 mch_set_winsize_now(); /* Allow winsize changes from now on */
968#endif
969
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000970#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
971 /* When tab pages were created, may need to update the tab pages line and
972 * scrollbars. This is skipped while creating them. */
973 if (first_tabpage->tp_next != NULL)
974 {
975 out_flush();
976 gui_init_which_components(NULL);
977 gui_update_scrollbars(TRUE);
978 }
979 need_mouse_correct = TRUE;
980#endif
981
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000982 /* If ":startinsert" command used, stuff a dummy command to be able to
983 * call normal_cmd(), which will then start Insert mode. */
984 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000985 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000986
987#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200988 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200989 {
990# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200991# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200992 && !defined(FEAT_GUI_W32)
993 if (gui.in_use)
994 {
995 mch_errmsg(_("netbeans is not supported with this GUI\n"));
996 mch_exit(2);
997 }
998# endif
999# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001000 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001001 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001002 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001003#endif
1004
1005 TIME_MSG("before starting main loop");
1006
1007 /*
1008 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001009 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001010 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001011
1012 return 0;
1013}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001014#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001015#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001016
1017/*
1018 * Main loop: Execute Normal mode commands until exiting Vim.
1019 * Also used to handle commands in the command-line window, until the window
1020 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021 * Also used to handle ":visual" command after ":global": execute Normal mode
1022 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001023 */
1024 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001025main_loop(cmdwin, noexmode)
1026 int cmdwin; /* TRUE when working in the command-line window */
1027 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001028{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001029 oparg_T oa; /* operator arguments */
1030 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001031#ifdef FEAT_CONCEAL
1032 linenr_T conceal_old_cursor_line = 0;
1033 linenr_T conceal_new_cursor_line = 0;
1034 int conceal_update_lines = FALSE;
1035#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001036
1037#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1038 /* Setup to catch a terminating error from the X server. Just ignore
1039 * it, restore the state and continue. This might not always work
1040 * properly, but at least we don't exit unexpectedly when the X server
1041 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001043 {
1044 State = NORMAL;
1045# ifdef FEAT_VISUAL
1046 VIsual_active = FALSE;
1047# endif
1048 got_int = TRUE;
1049 need_wait_return = FALSE;
1050 global_busy = FALSE;
1051 exmode_active = 0;
1052 skip_redraw = FALSE;
1053 RedrawingDisabled = 0;
1054 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001055 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001056# ifdef FEAT_EVAL
1057 emsg_skip = 0;
1058# endif
1059 emsg_off = 0;
1060# ifdef FEAT_MOUSE
1061 setmouse();
1062# endif
1063 settmode(TMODE_RAW);
1064 starttermcap();
1065 scroll_start();
1066 redraw_later_clear();
1067 }
1068#endif
1069
1070 clear_oparg(&oa);
1071 while (!cmdwin
1072#ifdef FEAT_CMDWIN
1073 || cmdwin_result == 0
1074#endif
1075 )
1076 {
1077 if (stuff_empty())
1078 {
1079 did_check_timestamps = FALSE;
1080 if (need_check_timestamps)
1081 check_timestamps(FALSE);
1082 if (need_wait_return) /* if wait_return still needed ... */
1083 wait_return(FALSE); /* ... call it now */
1084 if (need_start_insertmode && goto_im()
1085#ifdef FEAT_VISUAL
1086 && !VIsual_active
1087#endif
1088 )
1089 {
1090 need_start_insertmode = FALSE;
1091 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1092 /* skip the fileinfo message now, because it would be shown
1093 * after insert mode finishes! */
1094 need_fileinfo = FALSE;
1095 }
1096 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001097
1098 /* Reset "got_int" now that we got back to the main loop. Except when
1099 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1100 * the ":g" command.
1101 * For ":g/pat/vi" we reset "got_int" when used once. When used
1102 * a second time we go back to Ex mode and abort the ":g" command. */
1103 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001104 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001105 if (noexmode && global_busy && !exmode_active && previous_got_int)
1106 {
1107 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1108 * used and keep "got_int" set, so that it aborts ":g". */
1109 exmode_active = EXMODE_NORMAL;
1110 State = NORMAL;
1111 }
1112 else if (!global_busy || !exmode_active)
1113 {
1114 if (!quit_more)
1115 (void)vgetc(); /* flush all buffers */
1116 got_int = FALSE;
1117 }
1118 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001119 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001120 else
1121 previous_got_int = FALSE;
1122
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001123 if (!exmode_active)
1124 msg_scroll = FALSE;
1125 quit_more = FALSE;
1126
1127 /*
1128 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1129 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1130 * update cursor and redraw.
1131 */
1132 if (skip_redraw || exmode_active)
1133 skip_redraw = FALSE;
1134 else if (do_redraw || stuff_empty())
1135 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001136#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001137 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001138 if (!finish_op && (
1139# ifdef FEAT_AUTOCMD
1140 has_cursormoved()
1141# endif
1142# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1143 ||
1144# endif
1145# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001146 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001147# endif
1148 )
1149 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001150 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001151# ifdef FEAT_AUTOCMD
1152 if (has_cursormoved())
1153 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1154 FALSE, curbuf);
1155# endif
1156# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001157 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001158 {
1159 conceal_old_cursor_line = last_cursormoved.lnum;
1160 conceal_new_cursor_line = curwin->w_cursor.lnum;
1161 conceal_update_lines = TRUE;
1162 }
1163# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001164 last_cursormoved = curwin->w_cursor;
1165 }
1166#endif
1167
Bram Moolenaar33aec762006-01-22 23:30:12 +00001168#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1169 /* Scroll-binding for diff mode may have been postponed until
1170 * here. Avoids doing it for every change. */
1171 if (diff_need_scrollbind)
1172 {
1173 check_scrollbind((linenr_T)0, 0L);
1174 diff_need_scrollbind = FALSE;
1175 }
1176#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001177#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1178 /* Include a closed fold completely in the Visual area. */
1179 foldAdjustVisual();
1180#endif
1181#ifdef FEAT_FOLDING
1182 /*
1183 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1184 * contain the cursor.
1185 * When 'foldopen' is "all", open the fold(s) under the cursor.
1186 * This may mark the window for redrawing.
1187 */
1188 if (hasAnyFolding(curwin) && !char_avail())
1189 {
1190 foldCheckClose();
1191 if (fdo_flags & FDO_ALL)
1192 foldOpenCursor();
1193 }
1194#endif
1195
1196 /*
1197 * Before redrawing, make sure w_topline is correct, and w_leftcol
1198 * if lines don't wrap, and w_skipcol if lines wrap.
1199 */
1200 update_topline();
1201 validate_cursor();
1202
1203#ifdef FEAT_VISUAL
1204 if (VIsual_active)
1205 update_curbuf(INVERTED);/* update inverted part */
1206 else
1207#endif
1208 if (must_redraw)
1209 update_screen(0);
1210 else if (redraw_cmdline || clear_cmdline)
1211 showmode();
1212#ifdef FEAT_WINDOWS
1213 redraw_statuslines();
1214#endif
1215#ifdef FEAT_TITLE
1216 if (need_maketitle)
1217 maketitle();
1218#endif
1219 /* display message after redraw */
1220 if (keep_msg != NULL)
1221 {
1222 char_u *p;
1223
1224 /* msg_attr_keep() will set keep_msg to NULL, must free the
1225 * string here. */
1226 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001227 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001228 msg_attr(p, keep_msg_attr);
1229 vim_free(p);
1230 }
1231 if (need_fileinfo) /* show file info after redraw */
1232 {
1233 fileinfo(FALSE, TRUE, FALSE);
1234 need_fileinfo = FALSE;
1235 }
1236
1237 emsg_on_display = FALSE; /* can delete error message now */
1238 did_emsg = FALSE;
1239 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001240 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001241 showruler(FALSE);
1242
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001243# if defined(FEAT_CONCEAL)
1244 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001245 && (conceal_old_cursor_line != conceal_new_cursor_line
1246 || conceal_cursor_line(curwin)
1247 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001248 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001249 if (conceal_old_cursor_line != conceal_new_cursor_line
1250 && conceal_old_cursor_line
1251 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001252 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001253 update_single_line(curwin, conceal_new_cursor_line);
1254 curwin->w_valid &= ~VALID_CROW;
1255 }
1256# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001257 setcursor();
1258 cursor_on();
1259
1260 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001261
1262#ifdef STARTUPTIME
1263 /* Now that we have drawn the first screen all the startup stuff
1264 * has been done, close any file for startup messages. */
1265 if (time_fd != NULL)
1266 {
1267 TIME_MSG("first screen update");
1268 TIME_MSG("--- VIM STARTED ---");
1269 fclose(time_fd);
1270 time_fd = NULL;
1271 }
1272#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001273 }
1274#ifdef FEAT_GUI
1275 if (need_mouse_correct)
1276 gui_mouse_correct();
1277#endif
1278
1279 /*
1280 * Update w_curswant if w_set_curswant has been set.
1281 * Postponed until here to avoid computing w_virtcol too often.
1282 */
1283 update_curswant();
1284
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001285#ifdef FEAT_EVAL
1286 /*
1287 * May perform garbage collection when waiting for a character, but
1288 * only at the very toplevel. Otherwise we may be using a List or
1289 * Dict internally somewhere.
1290 * "may_garbage_collect" is reset in vgetc() which is invoked through
1291 * do_exmode() and normal_cmd().
1292 */
1293 may_garbage_collect = (!cmdwin && !noexmode);
1294#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001295 /*
1296 * If we're invoked as ex, do a round of ex commands.
1297 * Otherwise, get and execute a normal mode command.
1298 */
1299 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001300 {
1301 if (noexmode) /* End of ":global/path/visual" commands */
1302 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001303 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001304 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001305 else
1306 normal_cmd(&oa, TRUE);
1307 }
1308}
1309
1310
1311#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1312/*
1313 * Exit, but leave behind swap files for modified buffers.
1314 */
1315 void
1316getout_preserve_modified(exitval)
1317 int exitval;
1318{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001319# if defined(SIGHUP) && defined(SIG_IGN)
1320 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1321 * makes Vim exit and then handling SIGHUP causes various reentrance
1322 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001323 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001324# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001325
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001326 ml_close_notmod(); /* close all not-modified buffers */
1327 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1328 ml_close_all(FALSE); /* close all memfiles, without deleting */
1329 getout(exitval); /* exit Vim properly */
1330}
1331#endif
1332
1333
1334/* Exit properly */
1335 void
1336getout(exitval)
1337 int exitval;
1338{
1339#ifdef FEAT_AUTOCMD
1340 buf_T *buf;
1341 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001342 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001343#endif
1344
1345 exiting = TRUE;
1346
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001347 /* When running in Ex mode an error causes us to exit with a non-zero exit
1348 * code. POSIX requires this, although it's not 100% clear from the
1349 * standard. */
1350 if (exmode_active)
1351 exitval += ex_exitval;
1352
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001353 /* Position the cursor on the last screen line, below all the text */
1354#ifdef FEAT_GUI
1355 if (!gui.in_use)
1356#endif
1357 windgoto((int)Rows - 1, 0);
1358
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001359#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1360 /* Optionally print hashtable efficiency. */
1361 hash_debug_results();
1362#endif
1363
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001364#ifdef FEAT_GUI
1365 msg_didany = FALSE;
1366#endif
1367
1368#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001369 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001370 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001371 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1372# if defined FEAT_WINDOWS
1373 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001374 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001375 next_tp = tp->tp_next;
1376 for (wp = (tp == curtab)
1377 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001378 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001379 buf = wp->w_buffer;
1380 if (buf->b_changedtick != -1)
1381 {
1382 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1383 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001384 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001385 /* start all over, autocommands may mess up the lists */
1386 next_tp = first_tabpage;
1387 break;
1388 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001389 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001390 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001391# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001392 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1393 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001394# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001395
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001396 /* Trigger BufUnload for buffers that are loaded */
1397 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1398 if (buf->b_ml.ml_mfp != NULL)
1399 {
1400 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001401 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001402 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1403 break;
1404 }
1405 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1406 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001407#endif
1408
1409#ifdef FEAT_VIMINFO
1410 if (*p_viminfo != NUL)
1411 /* Write out the registers, history, marks etc, to the viminfo file */
1412 write_viminfo(NULL, FALSE);
1413#endif
1414
1415#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001416 if (get_vim_var_nr(VV_DYING) <= 1)
1417 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001418#endif
1419
Bram Moolenaar05159a02005-02-26 23:04:13 +00001420#ifdef FEAT_PROFILE
1421 profile_dump();
1422#endif
1423
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001424 if (did_emsg
1425#ifdef FEAT_GUI
1426 || (gui.in_use && msg_didany && p_verbose > 0)
1427#endif
1428 )
1429 {
1430 /* give the user a chance to read the (error) message */
1431 no_wait_return = FALSE;
1432 wait_return(FALSE);
1433 }
1434
1435#ifdef FEAT_AUTOCMD
1436 /* Position the cursor again, the autocommands may have moved it */
1437# ifdef FEAT_GUI
1438 if (!gui.in_use)
1439# endif
1440 windgoto((int)Rows - 1, 0);
1441#endif
1442
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001443#ifdef FEAT_LUA
1444 lua_end();
1445#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001446#ifdef FEAT_MZSCHEME
1447 mzscheme_end();
1448#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001449#ifdef FEAT_TCL
1450 tcl_end();
1451#endif
1452#ifdef FEAT_RUBY
1453 ruby_end();
1454#endif
1455#ifdef FEAT_PYTHON
1456 python_end();
1457#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001458#ifdef FEAT_PYTHON3
1459 python3_end();
1460#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001461#ifdef FEAT_PERL
1462 perl_end();
1463#endif
1464#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1465 iconv_end();
1466#endif
1467#ifdef FEAT_NETBEANS_INTG
1468 netbeans_end();
1469#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001470#ifdef FEAT_CSCOPE
1471 cs_end();
1472#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001473#ifdef FEAT_EVAL
1474 if (garbage_collect_at_exit)
1475 garbage_collect();
1476#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001477
1478 mch_exit(exitval);
1479}
1480
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001481#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001482/*
1483 * Get a (optional) count for a Vim argument.
1484 */
1485 static int
1486get_number_arg(p, idx, def)
1487 char_u *p; /* pointer to argument */
1488 int *idx; /* index in argument, is incremented */
1489 int def; /* default value */
1490{
1491 if (vim_isdigit(p[*idx]))
1492 {
1493 def = atoi((char *)&(p[*idx]));
1494 while (vim_isdigit(p[*idx]))
1495 *idx = *idx + 1;
1496 }
1497 return def;
1498}
1499
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001500#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1501/*
1502 * Setup to use the current locale (for ctype() and many other things).
1503 */
1504 static void
1505init_locale()
1506{
1507 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001508
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001509# ifdef FEAT_GUI_GTK
1510 /* Tell Gtk not to change our locale settings. */
1511 gtk_disable_setlocale();
1512# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001513# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1514 /* Make sure strtod() uses a decimal point, not a comma. */
1515 setlocale(LC_NUMERIC, "C");
1516# endif
1517
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001518# ifdef WIN32
1519 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1520 * text while it's expecting text in the current locale. This call avoids
1521 * that. */
1522 setlocale(LC_CTYPE, "C");
1523# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001524
1525# ifdef FEAT_GETTEXT
1526 {
1527 int mustfree = FALSE;
1528 char_u *p;
1529
1530# ifdef DYNAMIC_GETTEXT
1531 /* Initialize the gettext library */
1532 dyn_libintl_init(NULL);
1533# endif
1534 /* expand_env() doesn't work yet, because chartab[] is not initialized
1535 * yet, call vim_getenv() directly */
1536 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1537 if (p != NULL && *p != NUL)
1538 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001539 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001540 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1541 }
1542 if (mustfree)
1543 vim_free(p);
1544 textdomain(VIMPACKAGE);
1545 }
1546# endif
1547}
1548#endif
1549
1550/*
1551 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1552 * If the executable name starts with "r" we disable shell commands.
1553 * If the next character is "e" we run in Easy mode.
1554 * If the next character is "g" we run the GUI version.
1555 * If the next characters are "view" we start in readonly mode.
1556 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1557 * If the next characters are "ex" we start in Ex mode. If it's followed
1558 * by "im" use improved Ex mode.
1559 */
1560 static void
1561parse_command_name(parmp)
1562 mparm_T *parmp;
1563{
1564 char_u *initstr;
1565
1566 initstr = gettail((char_u *)parmp->argv[0]);
1567
1568#ifdef MACOS_X_UNIX
1569 /* An issue has been seen when launching Vim in such a way that
1570 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1571 * executable or a symbolic link of it. Until this issue is resolved
1572 * we prohibit the GUI from being used.
1573 */
1574 if (STRCMP(initstr, parmp->argv[0]) == 0)
1575 disallow_gui = TRUE;
1576
1577 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001578 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001579#endif
1580
1581#ifdef FEAT_EVAL
1582 set_vim_var_string(VV_PROGNAME, initstr, -1);
1583#endif
1584
1585 if (TOLOWER_ASC(initstr[0]) == 'r')
1586 {
1587 restricted = TRUE;
1588 ++initstr;
1589 }
1590
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001591 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001592 if (TOLOWER_ASC(initstr[0]) == 'e'
1593 && (TOLOWER_ASC(initstr[1]) == 'v'
1594 || TOLOWER_ASC(initstr[1]) == 'g'))
1595 {
1596#ifdef FEAT_GUI
1597 gui.starting = TRUE;
1598#endif
1599 parmp->evim_mode = TRUE;
1600 ++initstr;
1601 }
1602
Bram Moolenaar806875d2008-09-18 18:57:10 +00001603 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1604 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001605 {
1606 main_start_gui();
1607#ifdef FEAT_GUI
1608 ++initstr;
1609#endif
1610 }
1611
1612 if (STRNICMP(initstr, "view", 4) == 0)
1613 {
1614 readonlymode = TRUE;
1615 curbuf->b_p_ro = TRUE;
1616 p_uc = 10000; /* don't update very often */
1617 initstr += 4;
1618 }
1619 else if (STRNICMP(initstr, "vim", 3) == 0)
1620 initstr += 3;
1621
1622 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1623 if (STRICMP(initstr, "diff") == 0)
1624 {
1625#ifdef FEAT_DIFF
1626 parmp->diff_mode = TRUE;
1627#else
1628 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1629 mch_errmsg("\n");
1630 mch_exit(2);
1631#endif
1632 }
1633
1634 if (STRNICMP(initstr, "ex", 2) == 0)
1635 {
1636 if (STRNICMP(initstr + 2, "im", 2) == 0)
1637 exmode_active = EXMODE_VIM;
1638 else
1639 exmode_active = EXMODE_NORMAL;
1640 change_compatible(TRUE); /* set 'compatible' */
1641 }
1642}
1643
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001644/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001645 * Get the name of the display, before gui_prepare() removes it from
1646 * argv[]. Used for the xterm-clipboard display.
1647 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001648 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001649 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001650 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001651early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001652 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001653{
Bram Moolenaar03005972008-11-20 13:12:36 +00001654#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1655 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001656 int argc = parmp->argc;
1657 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001658 int i;
1659
1660 for (i = 1; i < argc; i++)
1661 {
1662 if (STRCMP(argv[i], "--") == 0)
1663 break;
1664# ifdef FEAT_XCLIPBOARD
1665 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001666# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001667 || STRICMP(argv[i], "--display") == 0
1668# endif
1669 )
1670 {
1671 if (i == argc - 1)
1672 mainerr_arg_missing((char_u *)argv[i]);
1673 xterm_display = argv[++i];
1674 }
1675# endif
1676# ifdef FEAT_CLIENTSERVER
1677 else if (STRICMP(argv[i], "--servername") == 0)
1678 {
1679 if (i == argc - 1)
1680 mainerr_arg_missing((char_u *)argv[i]);
1681 parmp->serverName_arg = (char_u *)argv[++i];
1682 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001683 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001684 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001685 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001686 {
1687 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001688# ifdef FEAT_GUI
1689 if (strstr(argv[i], "-wait") != 0)
1690 /* don't fork() when starting the GUI to edit files ourself */
1691 gui.dofork = FALSE;
1692# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001693 }
1694# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001695
1696# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1697# ifdef FEAT_GUI_W32
1698 else if (STRICMP(argv[i], "--windowid") == 0)
1699# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001700 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001701# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001702 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001703 long_u id;
1704 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001705
1706 if (i == argc - 1)
1707 mainerr_arg_missing((char_u *)argv[i]);
1708 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001709 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001710 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001711 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001712 if (count != 1)
1713 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1714 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001715# ifdef FEAT_GUI_W32
1716 win_socket_id = id;
1717# else
1718 gtk_socket_id = id;
1719# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001720 i++;
1721 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001722# endif
1723# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001724 else if (STRICMP(argv[i], "--echo-wid") == 0)
1725 echo_wid_arg = TRUE;
1726# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001727# ifndef FEAT_NETBEANS_INTG
1728 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001729 {
1730 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1731 mch_exit(2);
1732 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001733# endif
1734
Bram Moolenaar58d98232005-07-23 22:25:46 +00001735 }
1736#endif
1737}
1738
1739/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001740 * Scan the command line arguments.
1741 */
1742 static void
1743command_line_scan(parmp)
1744 mparm_T *parmp;
1745{
1746 int argc = parmp->argc;
1747 char **argv = parmp->argv;
1748 int argv_idx; /* index in argv[n][] */
1749 int had_minmin = FALSE; /* found "--" argument */
1750 int want_argument; /* option argument with argument */
1751 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001752 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001753 long n;
1754
1755 --argc;
1756 ++argv;
1757 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1758 while (argc > 0)
1759 {
1760 /*
1761 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1762 */
1763 if (argv[0][0] == '+' && !had_minmin)
1764 {
1765 if (parmp->n_commands >= MAX_ARG_CMDS)
1766 mainerr(ME_EXTRA_CMD, NULL);
1767 argv_idx = -1; /* skip to next argument */
1768 if (argv[0][1] == NUL)
1769 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1770 else
1771 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1772 }
1773
1774 /*
1775 * Optional argument.
1776 */
1777 else if (argv[0][0] == '-' && !had_minmin)
1778 {
1779 want_argument = FALSE;
1780 c = argv[0][argv_idx++];
1781#ifdef VMS
1782 /*
1783 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1784 * and "-/X" as "-X".
1785 */
1786 if (c == '/')
1787 {
1788 c = argv[0][argv_idx++];
1789 c = TOUPPER_ASC(c);
1790 }
1791 else
1792 c = TOLOWER_ASC(c);
1793#endif
1794 switch (c)
1795 {
1796 case NUL: /* "vim -" read from stdin */
1797 /* "ex -" silent mode */
1798 if (exmode_active)
1799 silent_mode = TRUE;
1800 else
1801 {
1802 if (parmp->edit_type != EDIT_NONE)
1803 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1804 parmp->edit_type = EDIT_STDIN;
1805 read_cmd_fd = 2; /* read from stderr instead of stdin */
1806 }
1807 argv_idx = -1; /* skip to next argument */
1808 break;
1809
1810 case '-': /* "--" don't take any more option arguments */
1811 /* "--help" give help message */
1812 /* "--version" give version message */
1813 /* "--literal" take files literally */
1814 /* "--nofork" don't fork */
1815 /* "--noplugin[s]" skip plugins */
1816 /* "--cmd <cmd>" execute cmd before vimrc */
1817 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1818 usage();
1819 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1820 {
1821 Columns = 80; /* need to init Columns */
1822 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1823 list_version();
1824 msg_putchar('\n');
1825 msg_didout = FALSE;
1826 mch_exit(0);
1827 }
1828 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1829 {
1830#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1831 parmp->literal = TRUE;
1832#endif
1833 }
1834 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1835 {
1836#ifdef FEAT_GUI
1837 gui.dofork = FALSE; /* don't fork() when starting GUI */
1838#endif
1839 }
1840 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1841 p_lpl = FALSE;
1842 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1843 {
1844 want_argument = TRUE;
1845 argv_idx += 3;
1846 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001847 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1848 {
1849 want_argument = TRUE;
1850 argv_idx += 11;
1851 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001852#ifdef FEAT_CLIENTSERVER
1853 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1854 ; /* already processed -- no arg */
1855 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1856 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1857 {
1858 /* already processed -- snatch the following arg */
1859 if (argc > 1)
1860 {
1861 --argc;
1862 ++argv;
1863 }
1864 }
1865#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001866#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1867# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001868 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001869# else
1870 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1871# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001872 {
1873 /* already processed -- snatch the following arg */
1874 if (argc > 1)
1875 {
1876 --argc;
1877 ++argv;
1878 }
1879 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001880#endif
1881#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001882 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1883 {
1884 /* already processed, skip */
1885 }
1886#endif
1887 else
1888 {
1889 if (argv[0][argv_idx])
1890 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1891 had_minmin = TRUE;
1892 }
1893 if (!want_argument)
1894 argv_idx = -1; /* skip to next argument */
1895 break;
1896
1897 case 'A': /* "-A" start in Arabic mode */
1898#ifdef FEAT_ARABIC
1899 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1900#else
1901 mch_errmsg(_(e_noarabic));
1902 mch_exit(2);
1903#endif
1904 break;
1905
1906 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001907 /* Needs to be effective before expanding file names, because
1908 * for Win32 this makes us edit a shortcut file itself,
1909 * instead of the file it links to. */
1910 set_options_bin(curbuf->b_p_bin, 1, 0);
1911 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001912 break;
1913
1914 case 'C': /* "-C" Compatible */
1915 change_compatible(TRUE);
1916 break;
1917
1918 case 'e': /* "-e" Ex mode */
1919 exmode_active = EXMODE_NORMAL;
1920 break;
1921
1922 case 'E': /* "-E" Improved Ex mode */
1923 exmode_active = EXMODE_VIM;
1924 break;
1925
1926 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1927 window directly, not with newcli */
1928#ifdef FEAT_GUI
1929 gui.dofork = FALSE; /* don't fork() when starting GUI */
1930#endif
1931 break;
1932
1933 case 'g': /* "-g" start GUI */
1934 main_start_gui();
1935 break;
1936
1937 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1938#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001939 p_fkmap = TRUE;
1940 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001941#else
1942 mch_errmsg(_(e_nofarsi));
1943 mch_exit(2);
1944#endif
1945 break;
1946
1947 case 'h': /* "-h" give help message */
1948#ifdef FEAT_GUI_GNOME
1949 /* Tell usage() to exit for "gvim". */
1950 gui.starting = FALSE;
1951#endif
1952 usage();
1953 break;
1954
1955 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1956#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001957 p_hkmap = TRUE;
1958 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001959#else
1960 mch_errmsg(_(e_nohebrew));
1961 mch_exit(2);
1962#endif
1963 break;
1964
1965 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1966#ifdef FEAT_LISP
1967 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1968 p_sm = TRUE;
1969#endif
1970 break;
1971
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001972 case 'M': /* "-M" no changes or writing of files */
1973 reset_modifiable();
1974 /* FALLTHROUGH */
1975
1976 case 'm': /* "-m" no writing of files */
1977 p_write = FALSE;
1978 break;
1979
1980 case 'y': /* "-y" easy mode */
1981#ifdef FEAT_GUI
1982 gui.starting = TRUE; /* start GUI a bit later */
1983#endif
1984 parmp->evim_mode = TRUE;
1985 break;
1986
1987 case 'N': /* "-N" Nocompatible */
1988 change_compatible(FALSE);
1989 break;
1990
1991 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001992#ifdef FEAT_NETBEANS_INTG
1993 /* checking for "-nb", netbeans parameters */
1994 if (argv[0][argv_idx] == 'b')
1995 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02001996 netbeansArg = argv[0];
1997 argv_idx = -1; /* skip to next argument */
1998 }
1999 else
2000#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002001 parmp->no_swap_file = TRUE;
2002 break;
2003
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002004 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002005#ifdef TARGET_API_MAC_OSX
2006 /* For some reason on MacOS X, an argument like:
2007 -psn_0_10223617 is passed in when invoke from Finder
2008 or with the 'open' command */
2009 if (argv[0][argv_idx] == 's')
2010 {
2011 argv_idx = -1; /* bypass full -psn */
2012 main_start_gui();
2013 break;
2014 }
2015#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002016#ifdef FEAT_WINDOWS
2017 /* default is 0: open window for each file */
2018 parmp->window_count = get_number_arg((char_u *)argv[0],
2019 &argv_idx, 0);
2020 parmp->window_layout = WIN_TABS;
2021#endif
2022 break;
2023
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002024 case 'o': /* "-o[N]" open N horizontal split windows */
2025#ifdef FEAT_WINDOWS
2026 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002027 parmp->window_count = get_number_arg((char_u *)argv[0],
2028 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002029 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002030#endif
2031 break;
2032
2033 case 'O': /* "-O[N]" open N vertical split windows */
2034#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2035 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002036 parmp->window_count = get_number_arg((char_u *)argv[0],
2037 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002038 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002039#endif
2040 break;
2041
2042#ifdef FEAT_QUICKFIX
2043 case 'q': /* "-q" QuickFix mode */
2044 if (parmp->edit_type != EDIT_NONE)
2045 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2046 parmp->edit_type = EDIT_QF;
2047 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2048 {
2049 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2050 argv_idx = -1;
2051 }
2052 else if (argc > 1) /* "-q {errorfile}" */
2053 want_argument = TRUE;
2054 break;
2055#endif
2056
2057 case 'R': /* "-R" readonly mode */
2058 readonlymode = TRUE;
2059 curbuf->b_p_ro = TRUE;
2060 p_uc = 10000; /* don't update very often */
2061 break;
2062
2063 case 'r': /* "-r" recovery mode */
2064 case 'L': /* "-L" recovery mode */
2065 recoverymode = 1;
2066 break;
2067
2068 case 's':
2069 if (exmode_active) /* "-s" silent (batch) mode */
2070 silent_mode = TRUE;
2071 else /* "-s {scriptin}" read from script file */
2072 want_argument = TRUE;
2073 break;
2074
2075 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2076 if (parmp->edit_type != EDIT_NONE)
2077 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2078 parmp->edit_type = EDIT_TAG;
2079 if (argv[0][argv_idx]) /* "-t{tag}" */
2080 {
2081 parmp->tagname = (char_u *)argv[0] + argv_idx;
2082 argv_idx = -1;
2083 }
2084 else /* "-t {tag}" */
2085 want_argument = TRUE;
2086 break;
2087
2088#ifdef FEAT_EVAL
2089 case 'D': /* "-D" Debugging */
2090 parmp->use_debug_break_level = 9999;
2091 break;
2092#endif
2093#ifdef FEAT_DIFF
2094 case 'd': /* "-d" 'diff' */
2095# ifdef AMIGA
2096 /* check for "-dev {device}" */
2097 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2098 want_argument = TRUE;
2099 else
2100# endif
2101 parmp->diff_mode = TRUE;
2102 break;
2103#endif
2104 case 'V': /* "-V{N}" Verbose level */
2105 /* default is 10: a little bit verbose */
2106 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2107 if (argv[0][argv_idx] != NUL)
2108 {
2109 set_option_value((char_u *)"verbosefile", 0L,
2110 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002111 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002112 }
2113 break;
2114
2115 case 'v': /* "-v" Vi-mode (as if called "vi") */
2116 exmode_active = 0;
2117#ifdef FEAT_GUI
2118 gui.starting = FALSE; /* don't start GUI */
2119#endif
2120 break;
2121
2122 case 'w': /* "-w{number}" set window height */
2123 /* "-w {scriptout}" write to script */
2124 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2125 {
2126 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2127 set_option_value((char_u *)"window", n, NULL, 0);
2128 break;
2129 }
2130 want_argument = TRUE;
2131 break;
2132
2133#ifdef FEAT_CRYPT
2134 case 'x': /* "-x" encrypted reading/writing of files */
2135 parmp->ask_for_key = TRUE;
2136 break;
2137#endif
2138
2139 case 'X': /* "-X" don't connect to X server */
2140#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2141 x_no_connect = TRUE;
2142#endif
2143 break;
2144
2145 case 'Z': /* "-Z" restricted mode */
2146 restricted = TRUE;
2147 break;
2148
2149 case 'c': /* "-c{command}" or "-c {command}" execute
2150 command */
2151 if (argv[0][argv_idx] != NUL)
2152 {
2153 if (parmp->n_commands >= MAX_ARG_CMDS)
2154 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002155 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2156 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002157 argv_idx = -1;
2158 break;
2159 }
2160 /*FALLTHROUGH*/
2161 case 'S': /* "-S {file}" execute Vim script */
2162 case 'i': /* "-i {viminfo}" use for viminfo */
2163#ifndef FEAT_DIFF
2164 case 'd': /* "-d {device}" device (for Amiga) */
2165#endif
2166 case 'T': /* "-T {terminal}" terminal name */
2167 case 'u': /* "-u {vimrc}" vim inits file */
2168 case 'U': /* "-U {gvimrc}" gvim inits file */
2169 case 'W': /* "-W {scriptout}" overwrite */
2170#ifdef FEAT_GUI_W32
2171 case 'P': /* "-P {parent title}" MDI parent */
2172#endif
2173 want_argument = TRUE;
2174 break;
2175
2176 default:
2177 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2178 }
2179
2180 /*
2181 * Handle option arguments with argument.
2182 */
2183 if (want_argument)
2184 {
2185 /*
2186 * Check for garbage immediately after the option letter.
2187 */
2188 if (argv[0][argv_idx] != NUL)
2189 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2190
2191 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002192 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002193 mainerr_arg_missing((char_u *)argv[0]);
2194 ++argv;
2195 argv_idx = -1;
2196
2197 switch (c)
2198 {
2199 case 'c': /* "-c {command}" execute command */
2200 case 'S': /* "-S {file}" execute Vim script */
2201 if (parmp->n_commands >= MAX_ARG_CMDS)
2202 mainerr(ME_EXTRA_CMD, NULL);
2203 if (c == 'S')
2204 {
2205 char *a;
2206
2207 if (argc < 1)
2208 /* "-S" without argument: use default session file
2209 * name. */
2210 a = SESSION_FILE;
2211 else if (argv[0][0] == '-')
2212 {
2213 /* "-S" followed by another option: use default
2214 * session file name. */
2215 a = SESSION_FILE;
2216 ++argc;
2217 --argv;
2218 }
2219 else
2220 a = argv[0];
2221 p = alloc((unsigned)(STRLEN(a) + 4));
2222 if (p == NULL)
2223 mch_exit(2);
2224 sprintf((char *)p, "so %s", a);
2225 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2226 parmp->commands[parmp->n_commands++] = p;
2227 }
2228 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002229 parmp->commands[parmp->n_commands++] =
2230 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002231 break;
2232
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002233 case '-':
2234 if (argv[-1][2] == 'c')
2235 {
2236 /* "--cmd {command}" execute command */
2237 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2238 mainerr(ME_EXTRA_CMD, NULL);
2239 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002240 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002241 }
2242 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002243 break;
2244
2245 /* case 'd': -d {device} is handled in mch_check_win() for the
2246 * Amiga */
2247
2248#ifdef FEAT_QUICKFIX
2249 case 'q': /* "-q {errorfile}" QuickFix mode */
2250 parmp->use_ef = (char_u *)argv[0];
2251 break;
2252#endif
2253
2254 case 'i': /* "-i {viminfo}" use for viminfo */
2255 use_viminfo = (char_u *)argv[0];
2256 break;
2257
2258 case 's': /* "-s {scriptin}" read from script file */
2259 if (scriptin[0] != NULL)
2260 {
2261scripterror:
2262 mch_errmsg(_("Attempt to open script file again: \""));
2263 mch_errmsg(argv[-1]);
2264 mch_errmsg(" ");
2265 mch_errmsg(argv[0]);
2266 mch_errmsg("\"\n");
2267 mch_exit(2);
2268 }
2269 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2270 {
2271 mch_errmsg(_("Cannot open for reading: \""));
2272 mch_errmsg(argv[0]);
2273 mch_errmsg("\"\n");
2274 mch_exit(2);
2275 }
2276 if (save_typebuf() == FAIL)
2277 mch_exit(2); /* out of memory */
2278 break;
2279
2280 case 't': /* "-t {tag}" */
2281 parmp->tagname = (char_u *)argv[0];
2282 break;
2283
2284 case 'T': /* "-T {terminal}" terminal name */
2285 /*
2286 * The -T term argument is always available and when
2287 * HAVE_TERMLIB is supported it overrides the environment
2288 * variable TERM.
2289 */
2290#ifdef FEAT_GUI
2291 if (term_is_gui((char_u *)argv[0]))
2292 gui.starting = TRUE; /* start GUI a bit later */
2293 else
2294#endif
2295 parmp->term = (char_u *)argv[0];
2296 break;
2297
2298 case 'u': /* "-u {vimrc}" vim inits file */
2299 parmp->use_vimrc = (char_u *)argv[0];
2300 break;
2301
2302 case 'U': /* "-U {gvimrc}" gvim inits file */
2303#ifdef FEAT_GUI
2304 use_gvimrc = (char_u *)argv[0];
2305#endif
2306 break;
2307
2308 case 'w': /* "-w {nr}" 'window' value */
2309 /* "-w {scriptout}" append to script file */
2310 if (vim_isdigit(*((char_u *)argv[0])))
2311 {
2312 argv_idx = 0;
2313 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2314 set_option_value((char_u *)"window", n, NULL, 0);
2315 argv_idx = -1;
2316 break;
2317 }
2318 /*FALLTHROUGH*/
2319 case 'W': /* "-W {scriptout}" overwrite script file */
2320 if (scriptout != NULL)
2321 goto scripterror;
2322 if ((scriptout = mch_fopen(argv[0],
2323 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2324 {
2325 mch_errmsg(_("Cannot open for script output: \""));
2326 mch_errmsg(argv[0]);
2327 mch_errmsg("\"\n");
2328 mch_exit(2);
2329 }
2330 break;
2331
2332#ifdef FEAT_GUI_W32
2333 case 'P': /* "-P {parent title}" MDI parent */
2334 gui_mch_set_parent(argv[0]);
2335 break;
2336#endif
2337 }
2338 }
2339 }
2340
2341 /*
2342 * File name argument.
2343 */
2344 else
2345 {
2346 argv_idx = -1; /* skip to next argument */
2347
2348 /* Check for only one type of editing. */
2349 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2350 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2351 parmp->edit_type = EDIT_FILE;
2352
2353#ifdef MSWIN
2354 /* Remember if the argument was a full path before changing
2355 * slashes to backslashes. */
2356 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2357 parmp->full_path = TRUE;
2358#endif
2359
2360 /* Add the file to the global argument list. */
2361 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2362 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2363 mch_exit(2);
2364#ifdef FEAT_DIFF
2365 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2366 && !mch_isdir(alist_name(&GARGLIST[0])))
2367 {
2368 char_u *r;
2369
2370 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2371 if (r != NULL)
2372 {
2373 vim_free(p);
2374 p = r;
2375 }
2376 }
2377#endif
2378#if defined(__CYGWIN32__) && !defined(WIN32)
2379 /*
2380 * If vim is invoked by non-Cygwin tools, convert away any
2381 * DOS paths, so things like .swp files are created correctly.
2382 * Look for evidence of non-Cygwin paths before we bother.
2383 * This is only for when using the Unix files.
2384 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002385 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002386 {
2387 char posix_path[PATH_MAX];
2388
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002389# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2390 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2391# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002392 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002393# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002394 vim_free(p);
2395 p = vim_strsave(posix_path);
2396 if (p == NULL)
2397 mch_exit(2);
2398 }
2399#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002400
2401#ifdef USE_FNAME_CASE
2402 /* Make the case of the file name match the actual file. */
2403 fname_case(p, 0);
2404#endif
2405
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002406 alist_add(&global_alist, p,
2407#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002408 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002409#else
2410 2 /* add buffer number now and use curbuf */
2411#endif
2412 );
2413
2414#if defined(FEAT_MBYTE) && defined(WIN32)
2415 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002416 /* Remember this argument has been added to the argument list.
2417 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002418 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002419# ifdef FEAT_DIFF
2420 parmp->diff_mode
2421# else
2422 FALSE
2423# endif
2424 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002425 }
2426#endif
2427 }
2428
2429 /*
2430 * If there are no more letters after the current "-", go to next
2431 * argument. argv_idx is set to -1 when the current argument is to be
2432 * skipped.
2433 */
2434 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2435 {
2436 --argc;
2437 ++argv;
2438 argv_idx = 1;
2439 }
2440 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002441
2442#ifdef FEAT_EVAL
2443 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2444 * one. */
2445 if (parmp->n_commands > 0)
2446 {
2447 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2448 if (p != NULL)
2449 {
2450 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2451 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2452 vim_free(p);
2453 }
2454 }
2455#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002456}
2457
2458/*
2459 * Print a warning if stdout is not a terminal.
2460 * When starting in Ex mode and commands come from a file, set Silent mode.
2461 */
2462 static void
2463check_tty(parmp)
2464 mparm_T *parmp;
2465{
2466 int input_isatty; /* is active input a terminal? */
2467
2468 input_isatty = mch_input_isatty();
2469 if (exmode_active)
2470 {
2471 if (!input_isatty)
2472 silent_mode = TRUE;
2473 }
2474 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2475#ifdef FEAT_GUI
2476 /* don't want the delay when started from the desktop */
2477 && !gui.starting
2478#endif
2479 )
2480 {
2481#ifdef NBDEBUG
2482 /*
2483 * This shouldn't be necessary. But if I run netbeans with the log
2484 * output coming to the console and XOpenDisplay fails, I get vim
2485 * trying to start with input/output to my console tty. This fills my
2486 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002487 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002488 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002489 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002490 {
2491 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2492 exit(1);
2493 }
2494#endif
2495 if (!parmp->stdout_isatty)
2496 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2497 if (!input_isatty)
2498 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2499 out_flush();
2500 if (scriptin[0] == NULL)
2501 ui_delay(2000L, TRUE);
2502 TIME_MSG("Warning delay");
2503 }
2504}
2505
2506/*
2507 * Read text from stdin.
2508 */
2509 static void
2510read_stdin()
2511{
2512 int i;
2513
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002514#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002515 /* When getting the ATTENTION prompt here, use a dialog */
2516 swap_exists_action = SEA_DIALOG;
2517#endif
2518 no_wait_return = TRUE;
2519 i = msg_didany;
2520 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002521 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002522 no_wait_return = FALSE;
2523 msg_didany = i;
2524 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002525#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002526 check_swap_exists_action();
2527#endif
2528#if !(defined(AMIGA) || defined(MACOS))
2529 /*
2530 * Close stdin and dup it from stderr. Required for GPM to work
2531 * properly, and for running external commands.
2532 * Is there any other system that cannot do this?
2533 */
2534 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002535 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002536#endif
2537}
2538
2539/*
2540 * Create the requested number of windows and edit buffers in them.
2541 * Also does recovery if "recoverymode" set.
2542 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002543 static void
2544create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002545 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002546{
2547#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002548 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002549 int done = 0;
2550
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002551 /*
2552 * Create the number of windows that was requested.
2553 */
2554 if (parmp->window_count == -1) /* was not set */
2555 parmp->window_count = 1;
2556 if (parmp->window_count == 0)
2557 parmp->window_count = GARGCOUNT;
2558 if (parmp->window_count > 1)
2559 {
2560 /* Don't change the windows if there was a command in .vimrc that
2561 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002562 if (parmp->window_layout == 0)
2563 parmp->window_layout = WIN_HOR;
2564 if (parmp->window_layout == WIN_TABS)
2565 {
2566 parmp->window_count = make_tabpages(parmp->window_count);
2567 TIME_MSG("making tab pages");
2568 }
2569 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002570 {
2571 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002572 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002573 TIME_MSG("making windows");
2574 }
2575 else
2576 parmp->window_count = win_count();
2577 }
2578 else
2579 parmp->window_count = 1;
2580#endif
2581
2582 if (recoverymode) /* do recover */
2583 {
2584 msg_scroll = TRUE; /* scroll message up */
2585 ml_recover();
2586 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2587 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002588 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 }
2590 else
2591 {
2592 /*
2593 * Open a buffer for windows that don't have one yet.
2594 * Commands in the .vimrc might have loaded a file or split the window.
2595 * Watch out for autocommands that delete a window.
2596 */
2597#ifdef FEAT_AUTOCMD
2598 /*
2599 * Don't execute Win/Buf Enter/Leave autocommands here
2600 */
2601 ++autocmd_no_enter;
2602 ++autocmd_no_leave;
2603#endif
2604#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002605 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002606 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002607 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002608 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002609 {
2610 if (parmp->window_layout == WIN_TABS)
2611 goto_tabpage(1);
2612 else
2613 curwin = firstwin;
2614 }
2615 else if (parmp->window_layout == WIN_TABS)
2616 {
2617 if (curtab->tp_next == NULL)
2618 break;
2619 goto_tabpage(0);
2620 }
2621 else
2622 {
2623 if (curwin->w_next == NULL)
2624 break;
2625 curwin = curwin->w_next;
2626 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002627 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002628#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002629 curbuf = curwin->w_buffer;
2630 if (curbuf->b_ml.ml_mfp == NULL)
2631 {
2632#ifdef FEAT_FOLDING
2633 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2634 if (p_fdls >= 0)
2635 curwin->w_p_fdl = p_fdls;
2636#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002637#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002638 /* When getting the ATTENTION prompt here, use a dialog */
2639 swap_exists_action = SEA_DIALOG;
2640#endif
2641 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002642
2643 /* create memfile, read file */
2644 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002645
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002646#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002647 if (swap_exists_action == SEA_QUIT)
2648 {
2649 if (got_int || only_one_window())
2650 {
2651 /* abort selected or quit and only one window */
2652 did_emsg = FALSE; /* avoid hit-enter prompt */
2653 getout(1);
2654 }
2655 /* We can't close the window, it would disturb what
2656 * happens next. Clear the file name and set the arg
2657 * index to -1 to delete it later. */
2658 setfname(curbuf, NULL, NULL, FALSE);
2659 curwin->w_arg_idx = -1;
2660 swap_exists_action = SEA_NONE;
2661 }
2662 else
2663 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664#endif
2665#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002666 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002667#endif
2668 }
2669#ifdef FEAT_WINDOWS
2670 ui_breakcheck();
2671 if (got_int)
2672 {
2673 (void)vgetc(); /* only break the file loading, not the rest */
2674 break;
2675 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002676 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002677#endif
2678#ifdef FEAT_WINDOWS
2679 if (parmp->window_layout == WIN_TABS)
2680 goto_tabpage(1);
2681 else
2682 curwin = firstwin;
2683 curbuf = curwin->w_buffer;
2684#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002685#ifdef FEAT_AUTOCMD
2686 --autocmd_no_enter;
2687 --autocmd_no_leave;
2688#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002689 }
2690}
2691
2692#ifdef FEAT_WINDOWS
2693 /*
2694 * If opened more than one window, start editing files in the other
2695 * windows. make_windows() has already opened the windows.
2696 */
2697 static void
2698edit_buffers(parmp)
2699 mparm_T *parmp;
2700{
2701 int arg_idx; /* index in argument list */
2702 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002703 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002704
2705# ifdef FEAT_AUTOCMD
2706 /*
2707 * Don't execute Win/Buf Enter/Leave autocommands here
2708 */
2709 ++autocmd_no_enter;
2710 ++autocmd_no_leave;
2711# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002712
2713 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2714 if (curwin->w_arg_idx == -1)
2715 {
2716 win_close(curwin, TRUE);
2717 advance = FALSE;
2718 }
2719
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002720 arg_idx = 1;
2721 for (i = 1; i < parmp->window_count; ++i)
2722 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002723 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2724 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002725 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002726 ++arg_idx;
2727 win_close(curwin, TRUE);
2728 advance = FALSE;
2729 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002730 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002731
Bram Moolenaar84212822006-11-07 21:59:47 +00002732 if (advance)
2733 {
2734 if (parmp->window_layout == WIN_TABS)
2735 {
2736 if (curtab->tp_next == NULL) /* just checking */
2737 break;
2738 goto_tabpage(0);
2739 }
2740 else
2741 {
2742 if (curwin->w_next == NULL) /* just checking */
2743 break;
2744 win_enter(curwin->w_next, FALSE);
2745 }
2746 }
2747 advance = TRUE;
2748
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002749 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002750 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002751 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2752 {
2753 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002754 /* Edit file from arg list, if there is one. When "Quit" selected
2755 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002756# ifdef HAS_SWAP_EXISTS_ACTION
2757 swap_exists_did_quit = FALSE;
2758# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002759 (void)do_ecmd(0, arg_idx < GARGCOUNT
2760 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002761 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002762# ifdef HAS_SWAP_EXISTS_ACTION
2763 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002764 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002765 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002766 if (got_int || only_one_window())
2767 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002768 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002769 did_emsg = FALSE; /* avoid hit-enter prompt */
2770 getout(1);
2771 }
2772 win_close(curwin, TRUE);
2773 advance = FALSE;
2774 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002775# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002776 if (arg_idx == GARGCOUNT - 1)
2777 arg_had_last = TRUE;
2778 ++arg_idx;
2779 }
2780 ui_breakcheck();
2781 if (got_int)
2782 {
2783 (void)vgetc(); /* only break the file loading, not the rest */
2784 break;
2785 }
2786 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002787
2788 if (parmp->window_layout == WIN_TABS)
2789 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002790# ifdef FEAT_AUTOCMD
2791 --autocmd_no_enter;
2792# endif
2793 win_enter(firstwin, FALSE); /* back to first window */
2794# ifdef FEAT_AUTOCMD
2795 --autocmd_no_leave;
2796# endif
2797 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002798 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002799 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2800}
2801#endif /* FEAT_WINDOWS */
2802
2803/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002804 * Execute the commands from --cmd arguments "cmds[cnt]".
2805 */
2806 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002807exe_pre_commands(parmp)
2808 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002809{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002810 char_u **cmds = parmp->pre_commands;
2811 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002812 int i;
2813
2814 if (cnt > 0)
2815 {
2816 curwin->w_cursor.lnum = 0; /* just in case.. */
2817 sourcing_name = (char_u *)_("pre-vimrc command line");
2818# ifdef FEAT_EVAL
2819 current_SID = SID_CMDARG;
2820# endif
2821 for (i = 0; i < cnt; ++i)
2822 do_cmdline_cmd(cmds[i]);
2823 sourcing_name = NULL;
2824# ifdef FEAT_EVAL
2825 current_SID = 0;
2826# endif
2827 TIME_MSG("--cmd commands");
2828 }
2829}
2830
2831/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002832 * Execute "+", "-c" and "-S" arguments.
2833 */
2834 static void
2835exe_commands(parmp)
2836 mparm_T *parmp;
2837{
2838 int i;
2839
2840 /*
2841 * We start commands on line 0, make "vim +/pat file" match a
2842 * pattern on line 1. But don't move the cursor when an autocommand
2843 * with g`" was used.
2844 */
2845 msg_scroll = TRUE;
2846 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2847 curwin->w_cursor.lnum = 0;
2848 sourcing_name = (char_u *)"command line";
2849#ifdef FEAT_EVAL
2850 current_SID = SID_CARG;
2851#endif
2852 for (i = 0; i < parmp->n_commands; ++i)
2853 {
2854 do_cmdline_cmd(parmp->commands[i]);
2855 if (parmp->cmds_tofree[i])
2856 vim_free(parmp->commands[i]);
2857 }
2858 sourcing_name = NULL;
2859#ifdef FEAT_EVAL
2860 current_SID = 0;
2861#endif
2862 if (curwin->w_cursor.lnum == 0)
2863 curwin->w_cursor.lnum = 1;
2864
2865 if (!exmode_active)
2866 msg_scroll = FALSE;
2867
2868#ifdef FEAT_QUICKFIX
2869 /* When started with "-q errorfile" jump to first error again. */
2870 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002871 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002872#endif
2873 TIME_MSG("executing command arguments");
2874}
2875
2876/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002877 * Source startup scripts.
2878 */
2879 static void
2880source_startup_scripts(parmp)
2881 mparm_T *parmp;
2882{
2883 int i;
2884
2885 /*
2886 * For "evim" source evim.vim first of all, so that the user can overrule
2887 * any things he doesn't like.
2888 */
2889 if (parmp->evim_mode)
2890 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002891 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002892 TIME_MSG("source evim file");
2893 }
2894
2895 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002896 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002897 * nothing else.
2898 */
2899 if (parmp->use_vimrc != NULL)
2900 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002901 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2902 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002903 {
2904#ifdef FEAT_GUI
2905 if (use_gvimrc == NULL) /* don't load gvimrc either */
2906 use_gvimrc = parmp->use_vimrc;
2907#endif
2908 if (parmp->use_vimrc[2] == 'N')
2909 p_lpl = FALSE; /* don't load plugins either */
2910 }
2911 else
2912 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002913 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002914 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2915 }
2916 }
2917 else if (!silent_mode)
2918 {
2919#ifdef AMIGA
2920 struct Process *proc = (struct Process *)FindTask(0L);
2921 APTR save_winptr = proc->pr_WindowPtr;
2922
2923 /* Avoid a requester here for a volume that doesn't exist. */
2924 proc->pr_WindowPtr = (APTR)-1L;
2925#endif
2926
2927 /*
2928 * Get system wide defaults, if the file name is defined.
2929 */
2930#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002931 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002932#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002933#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002934 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002935#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002936
2937 /*
2938 * Try to read initialization commands from the following places:
2939 * - environment variable VIMINIT
2940 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2941 * - second user vimrc file ($VIM/.vimrc for Dos)
2942 * - environment variable EXINIT
2943 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2944 * - second user exrc file ($VIM/.exrc for Dos)
2945 * The first that exists is used, the rest is ignored.
2946 */
2947 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2948 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002949 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002950#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002951 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2952 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002953#endif
2954#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002955 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2956 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002957#endif
2958 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002959 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002960 {
2961#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002962 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002963#endif
2964 }
2965 }
2966
2967 /*
2968 * Read initialization commands from ".vimrc" or ".exrc" in current
2969 * directory. This is only done if the 'exrc' option is set.
2970 * Because of security reasons we disallow shell and write commands
2971 * now, except for unix if the file is owned by the user or 'secure'
2972 * option has been reset in environment of global ".exrc" or ".vimrc".
2973 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2974 * SYS_VIMRC_FILE.
2975 */
2976 if (p_exrc)
2977 {
2978#if defined(UNIX) || defined(VMS)
2979 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2980 if (!file_owned(VIMRC_FILE))
2981#endif
2982 secure = p_secure;
2983
2984 i = FAIL;
2985 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2986 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2987#ifdef USR_VIMRC_FILE2
2988 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2989 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2990#endif
2991#ifdef USR_VIMRC_FILE3
2992 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2993 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2994#endif
2995#ifdef SYS_VIMRC_FILE
2996 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
2997 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2998#endif
2999 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003000 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003001
3002 if (i == FAIL)
3003 {
3004#if defined(UNIX) || defined(VMS)
3005 /* if ".exrc" is not owned by user set 'secure' mode */
3006 if (!file_owned(EXRC_FILE))
3007 secure = p_secure;
3008 else
3009 secure = 0;
3010#endif
3011 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3012 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3013#ifdef USR_EXRC_FILE2
3014 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3015 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3016#endif
3017 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003018 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003019 }
3020 }
3021 if (secure == 2)
3022 need_wait_return = TRUE;
3023 secure = 0;
3024#ifdef AMIGA
3025 proc->pr_WindowPtr = save_winptr;
3026#endif
3027 }
3028 TIME_MSG("sourcing vimrc file(s)");
3029}
3030
3031/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003032 * Setup to start using the GUI. Exit with an error when not available.
3033 */
3034 static void
3035main_start_gui()
3036{
3037#ifdef FEAT_GUI
3038 gui.starting = TRUE; /* start GUI a bit later */
3039#else
3040 mch_errmsg(_(e_nogvim));
3041 mch_errmsg("\n");
3042 mch_exit(2);
3043#endif
3044}
3045
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003046#endif /* NO_VIM_MAIN */
3047
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003048/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003049 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003050 * Returns FAIL if the environment variable was not executed, OK otherwise.
3051 */
3052 int
3053process_env(env, is_viminit)
3054 char_u *env;
3055 int is_viminit; /* when TRUE, called for VIMINIT */
3056{
3057 char_u *initstr;
3058 char_u *save_sourcing_name;
3059 linenr_T save_sourcing_lnum;
3060#ifdef FEAT_EVAL
3061 scid_T save_sid;
3062#endif
3063
3064 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3065 {
3066 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003067 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003068 save_sourcing_name = sourcing_name;
3069 save_sourcing_lnum = sourcing_lnum;
3070 sourcing_name = env;
3071 sourcing_lnum = 0;
3072#ifdef FEAT_EVAL
3073 save_sid = current_SID;
3074 current_SID = SID_ENV;
3075#endif
3076 do_cmdline_cmd(initstr);
3077 sourcing_name = save_sourcing_name;
3078 sourcing_lnum = save_sourcing_lnum;
3079#ifdef FEAT_EVAL
3080 current_SID = save_sid;;
3081#endif
3082 return OK;
3083 }
3084 return FAIL;
3085}
3086
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003087#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003088/*
3089 * Return TRUE if we are certain the user owns the file "fname".
3090 * Used for ".vimrc" and ".exrc".
3091 * Use both stat() and lstat() for extra security.
3092 */
3093 static int
3094file_owned(fname)
3095 char *fname;
3096{
3097 struct stat s;
3098# ifdef UNIX
3099 uid_t uid = getuid();
3100# else /* VMS */
3101 uid_t uid = ((getgid() << 16) | getuid());
3102# endif
3103
3104 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3105# ifdef HAVE_LSTAT
3106 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3107# endif
3108 );
3109}
3110#endif
3111
3112/*
3113 * Give an error message main_errors["n"] and exit.
3114 */
3115 static void
3116mainerr(n, str)
3117 int n; /* one of the ME_ defines */
3118 char_u *str; /* extra argument or NULL */
3119{
3120#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3121 reset_signals(); /* kill us with CTRL-C here, if you like */
3122#endif
3123
3124 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003125 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003126 mch_errmsg(_(main_errors[n]));
3127 if (str != NULL)
3128 {
3129 mch_errmsg(": \"");
3130 mch_errmsg((char *)str);
3131 mch_errmsg("\"");
3132 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003133 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003134
3135 mch_exit(1);
3136}
3137
3138 void
3139mainerr_arg_missing(str)
3140 char_u *str;
3141{
3142 mainerr(ME_ARG_MISSING, str);
3143}
3144
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003145#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003146/*
3147 * print a message with three spaces prepended and '\n' appended.
3148 */
3149 static void
3150main_msg(s)
3151 char *s;
3152{
3153 mch_msg(" ");
3154 mch_msg(s);
3155 mch_msg("\n");
3156}
3157
3158/*
3159 * Print messages for "vim -h" or "vim --help" and exit.
3160 */
3161 static void
3162usage()
3163{
3164 int i;
3165 static char *(use[]) =
3166 {
3167 N_("[file ..] edit specified file(s)"),
3168 N_("- read text from stdin"),
3169 N_("-t tag edit file where tag is defined"),
3170#ifdef FEAT_QUICKFIX
3171 N_("-q [errorfile] edit file with first error")
3172#endif
3173 };
3174
3175#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3176 reset_signals(); /* kill us with CTRL-C here, if you like */
3177#endif
3178
3179 mch_msg(longVersion);
3180 mch_msg(_("\n\nusage:"));
3181 for (i = 0; ; ++i)
3182 {
3183 mch_msg(_(" vim [arguments] "));
3184 mch_msg(_(use[i]));
3185 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3186 break;
3187 mch_msg(_("\n or:"));
3188 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003189#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003190 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003191#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003192
3193 mch_msg(_("\n\nArguments:\n"));
3194 main_msg(_("--\t\t\tOnly file names after this"));
3195#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3196 main_msg(_("--literal\t\tDon't expand wildcards"));
3197#endif
3198#ifdef FEAT_OLE
3199 main_msg(_("-register\t\tRegister this gvim for OLE"));
3200 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3201#endif
3202#ifdef FEAT_GUI
3203 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3204 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3205#endif
3206 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3207 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003208 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003209 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3210#ifdef FEAT_DIFF
3211 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3212#endif
3213 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3214 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3215 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3216 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3217 main_msg(_("-M\t\t\tModifications in text not allowed"));
3218 main_msg(_("-b\t\t\tBinary mode"));
3219#ifdef FEAT_LISP
3220 main_msg(_("-l\t\t\tLisp mode"));
3221#endif
3222 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3223 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003224 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3225#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003226 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003227#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003228 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3229 main_msg(_("-r\t\t\tList swap files and exit"));
3230 main_msg(_("-r (with file name)\tRecover crashed session"));
3231 main_msg(_("-L\t\t\tSame as -r"));
3232#ifdef AMIGA
3233 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3234 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3235#endif
3236#ifdef FEAT_ARABIC
3237 main_msg(_("-A\t\t\tstart in Arabic mode"));
3238#endif
3239#ifdef FEAT_RIGHTLEFT
3240 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3241#endif
3242#ifdef FEAT_FKMAP
3243 main_msg(_("-F\t\t\tStart in Farsi mode"));
3244#endif
3245 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3246 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3247#ifdef FEAT_GUI
3248 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3249#endif
3250 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003251#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003252 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003253 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3254 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003255#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003256 main_msg(_("+\t\t\tStart at end of file"));
3257 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003258 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003259 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3260 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3261 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3262 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3263 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3264#ifdef FEAT_CRYPT
3265 main_msg(_("-x\t\t\tEdit encrypted files"));
3266#endif
3267#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3268# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3269 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3270# endif
3271 main_msg(_("-X\t\t\tDo not connect to X server"));
3272#endif
3273#ifdef FEAT_CLIENTSERVER
3274 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3275 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3276 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3277 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003278# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003279 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003280# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003281 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3282 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3283 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3284 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3285#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003286#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003287 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003288#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003289#ifdef FEAT_VIMINFO
3290 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3291#endif
3292 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3293 main_msg(_("--version\t\tPrint version information and exit"));
3294
3295#ifdef FEAT_GUI_X11
3296# ifdef FEAT_GUI_MOTIF
3297 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3298# else
3299# ifdef FEAT_GUI_ATHENA
3300# ifdef FEAT_GUI_NEXTAW
3301 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3302# else
3303 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3304# endif
3305# endif
3306# endif
3307 main_msg(_("-display <display>\tRun vim on <display>"));
3308 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003309 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3310 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3311 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3312 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3313 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3314 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3315 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3316 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3317# ifdef FEAT_GUI_ATHENA
3318 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3319# endif
3320 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3321 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3322 main_msg(_("-xrm <resource>\tSet the specified resource"));
3323#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003324#ifdef FEAT_GUI_GTK
3325 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3326 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3327 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3328 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3329 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003331 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003332 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003333#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003334#ifdef FEAT_GUI_W32
3335 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003336 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003337#endif
3338
3339#ifdef FEAT_GUI_GNOME
3340 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3341 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003342 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003343 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003344 gui.dofork = FALSE;
3345 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003346 else
3347#endif
3348 mch_exit(0);
3349}
3350
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003351#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352/*
3353 * Check the result of the ATTENTION dialog:
3354 * When "Quit" selected, exit Vim.
3355 * When "Recover" selected, recover the file.
3356 */
3357 static void
3358check_swap_exists_action()
3359{
3360 if (swap_exists_action == SEA_QUIT)
3361 getout(1);
3362 handle_swap_exists(NULL);
3363}
3364#endif
3365
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003366#endif
3367
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003368#if defined(STARTUPTIME) || defined(PROTO)
3369static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3370
3371static struct timeval prev_timeval;
3372
Bram Moolenaar3f269672009-11-03 11:11:11 +00003373# ifdef WIN3264
3374/*
3375 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3376 */
3377 static int
3378gettimeofday(struct timeval *tv, char *dummy)
3379{
3380 long t = clock();
3381 tv->tv_sec = t / CLOCKS_PER_SEC;
3382 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3383 return 0;
3384}
3385# endif
3386
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387/*
3388 * Save the previous time before doing something that could nest.
3389 * set "*tv_rel" to the time elapsed so far.
3390 */
3391 void
3392time_push(tv_rel, tv_start)
3393 void *tv_rel, *tv_start;
3394{
3395 *((struct timeval *)tv_rel) = prev_timeval;
3396 gettimeofday(&prev_timeval, NULL);
3397 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3398 - ((struct timeval *)tv_rel)->tv_usec;
3399 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3400 - ((struct timeval *)tv_rel)->tv_sec;
3401 if (((struct timeval *)tv_rel)->tv_usec < 0)
3402 {
3403 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3404 --((struct timeval *)tv_rel)->tv_sec;
3405 }
3406 *(struct timeval *)tv_start = prev_timeval;
3407}
3408
3409/*
3410 * Compute the previous time after doing something that could nest.
3411 * Subtract "*tp" from prev_timeval;
3412 * Note: The arguments are (void *) to avoid trouble with systems that don't
3413 * have struct timeval.
3414 */
3415 void
3416time_pop(tp)
3417 void *tp; /* actually (struct timeval *) */
3418{
3419 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3420 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3421 if (prev_timeval.tv_usec < 0)
3422 {
3423 prev_timeval.tv_usec += 1000000;
3424 --prev_timeval.tv_sec;
3425 }
3426}
3427
3428 static void
3429time_diff(then, now)
3430 struct timeval *then;
3431 struct timeval *now;
3432{
3433 long usec;
3434 long msec;
3435
3436 usec = now->tv_usec - then->tv_usec;
3437 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3438 usec = usec % 1000L;
3439 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3440}
3441
3442 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003443time_msg(mesg, tv_start)
3444 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003445 void *tv_start; /* only for do_source: start time; actually
3446 (struct timeval *) */
3447{
3448 static struct timeval start;
3449 struct timeval now;
3450
3451 if (time_fd != NULL)
3452 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003453 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003454 {
3455 gettimeofday(&start, NULL);
3456 prev_timeval = start;
3457 fprintf(time_fd, "\n\ntimes in msec\n");
3458 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3459 fprintf(time_fd, " clock elapsed: other lines\n\n");
3460 }
3461 gettimeofday(&now, NULL);
3462 time_diff(&start, &now);
3463 if (((struct timeval *)tv_start) != NULL)
3464 {
3465 fprintf(time_fd, " ");
3466 time_diff(((struct timeval *)tv_start), &now);
3467 }
3468 fprintf(time_fd, " ");
3469 time_diff(&prev_timeval, &now);
3470 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003471 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003472 }
3473}
3474
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003475#endif
3476
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003477#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003478
3479/*
3480 * Common code for the X command server and the Win32 command server.
3481 */
3482
Bram Moolenaareb94e552006-03-11 21:35:11 +00003483static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003484
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003485/*
3486 * Do the client-server stuff, unless "--servername ''" was used.
3487 */
3488 static void
3489exec_on_server(parmp)
3490 mparm_T *parmp;
3491{
3492 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3493 {
3494# ifdef WIN32
3495 /* Initialise the client/server messaging infrastructure. */
3496 serverInitMessaging();
3497# endif
3498
3499 /*
3500 * When a command server argument was found, execute it. This may
3501 * exit Vim when it was successful. Otherwise it's executed further
3502 * on. Remember the encoding used here in "serverStrEnc".
3503 */
3504 if (parmp->serverArg)
3505 {
3506 cmdsrv_main(&parmp->argc, parmp->argv,
3507 parmp->serverName_arg, &parmp->serverStr);
3508# ifdef FEAT_MBYTE
3509 parmp->serverStrEnc = vim_strsave(p_enc);
3510# endif
3511 }
3512
3513 /* If we're still running, get the name to register ourselves.
3514 * On Win32 can register right now, for X11 need to setup the
3515 * clipboard first, it's further down. */
3516 parmp->servername = serverMakeName(parmp->serverName_arg,
3517 parmp->argv[0]);
3518# ifdef WIN32
3519 if (parmp->servername != NULL)
3520 {
3521 serverSetName(parmp->servername);
3522 vim_free(parmp->servername);
3523 }
3524# endif
3525 }
3526}
3527
3528/*
3529 * Prepare for running as a Vim server.
3530 */
3531 static void
3532prepare_server(parmp)
3533 mparm_T *parmp;
3534{
3535# if defined(FEAT_X11)
3536 /*
3537 * Register for remote command execution with :serversend and --remote
3538 * unless there was a -X or a --servername '' on the command line.
3539 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003540 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003541 */
3542 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3543# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003544 (gui.in_use
3545# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003546 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003547# endif
3548 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003549# endif
3550 parmp->serverName_arg != NULL))
3551 {
3552 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3553 vim_free(parmp->servername);
3554 TIME_MSG("register server name");
3555 }
3556 else
3557 serverDelayedStartName = parmp->servername;
3558# endif
3559
3560 /*
3561 * Execute command ourselves if we're here because the send failed (or
3562 * else we would have exited above).
3563 */
3564 if (parmp->serverStr != NULL)
3565 {
3566 char_u *p;
3567
3568 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3569 parmp->serverStr, &p));
3570 vim_free(p);
3571 }
3572}
3573
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003574 static void
3575cmdsrv_main(argc, argv, serverName_arg, serverStr)
3576 int *argc;
3577 char **argv;
3578 char_u *serverName_arg;
3579 char_u **serverStr;
3580{
3581 char_u *res;
3582 int i;
3583 char_u *sname;
3584 int ret;
3585 int didone = FALSE;
3586 int exiterr = 0;
3587 char **newArgV = argv + 1;
3588 int newArgC = 1,
3589 Argc = *argc;
3590 int argtype;
3591#define ARGTYPE_OTHER 0
3592#define ARGTYPE_EDIT 1
3593#define ARGTYPE_EDIT_WAIT 2
3594#define ARGTYPE_SEND 3
3595 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003596 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003597# ifndef FEAT_X11
3598 HWND srv;
3599# else
3600 Window srv;
3601
3602 setup_term_clip();
3603# endif
3604
3605 sname = serverMakeName(serverName_arg, argv[0]);
3606 if (sname == NULL)
3607 return;
3608
3609 /*
3610 * Execute the command server related arguments and remove them
3611 * from the argc/argv array; We may have to return into main()
3612 */
3613 for (i = 1; i < Argc; i++)
3614 {
3615 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003616 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003617 {
3618 for (; i < *argc; i++)
3619 {
3620 *newArgV++ = argv[i];
3621 newArgC++;
3622 }
3623 break;
3624 }
3625
Bram Moolenaareb94e552006-03-11 21:35:11 +00003626 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003627 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003628 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3629 {
3630 char *p = argv[i] + 8;
3631
3632 argtype = ARGTYPE_EDIT;
3633 while (*p != NUL)
3634 {
3635 if (STRNICMP(p, "-wait", 5) == 0)
3636 {
3637 argtype = ARGTYPE_EDIT_WAIT;
3638 p += 5;
3639 }
3640 else if (STRNICMP(p, "-silent", 7) == 0)
3641 {
3642 silent = TRUE;
3643 p += 7;
3644 }
3645 else if (STRNICMP(p, "-tab", 4) == 0)
3646 {
3647 tabs = TRUE;
3648 p += 4;
3649 }
3650 else
3651 {
3652 argtype = ARGTYPE_OTHER;
3653 break;
3654 }
3655 }
3656 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003657 else
3658 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003659
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003660 if (argtype != ARGTYPE_OTHER)
3661 {
3662 if (i == *argc - 1)
3663 mainerr_arg_missing((char_u *)argv[i]);
3664 if (argtype == ARGTYPE_SEND)
3665 {
3666 *serverStr = (char_u *)argv[i + 1];
3667 i++;
3668 }
3669 else
3670 {
3671 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003672 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003673 if (*serverStr == NULL)
3674 {
3675 /* Probably out of memory, exit. */
3676 didone = TRUE;
3677 exiterr = 1;
3678 break;
3679 }
3680 Argc = i;
3681 }
3682# ifdef FEAT_X11
3683 if (xterm_dpy == NULL)
3684 {
3685 mch_errmsg(_("No display"));
3686 ret = -1;
3687 }
3688 else
3689 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3690 NULL, &srv, 0, 0, silent);
3691# else
3692 /* Win32 always works? */
3693 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3694# endif
3695 if (ret < 0)
3696 {
3697 if (argtype == ARGTYPE_SEND)
3698 {
3699 /* Failed to send, abort. */
3700 mch_errmsg(_(": Send failed.\n"));
3701 didone = TRUE;
3702 exiterr = 1;
3703 }
3704 else if (!silent)
3705 /* Let vim start normally. */
3706 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3707 break;
3708 }
3709
3710# ifdef FEAT_GUI_W32
3711 /* Guess that when the server name starts with "g" it's a GUI
3712 * server, which we can bring to the foreground here.
3713 * Foreground() in the server doesn't work very well. */
3714 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3715 SetForegroundWindow(srv);
3716# endif
3717
3718 /*
3719 * For --remote-wait: Wait until the server did edit each
3720 * file. Also detect that the server no longer runs.
3721 */
3722 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3723 {
3724 int numFiles = *argc - i - 1;
3725 int j;
3726 char_u *done = alloc(numFiles);
3727 char_u *p;
3728# ifdef FEAT_GUI_W32
3729 NOTIFYICONDATA ni;
3730 int count = 0;
3731 extern HWND message_window;
3732# endif
3733
3734 if (numFiles > 0 && argv[i + 1][0] == '+')
3735 /* Skip "+cmd" argument, don't wait for it to be edited. */
3736 --numFiles;
3737
3738# ifdef FEAT_GUI_W32
3739 ni.cbSize = sizeof(ni);
3740 ni.hWnd = message_window;
3741 ni.uID = 0;
3742 ni.uFlags = NIF_ICON|NIF_TIP;
3743 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3744 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3745 Shell_NotifyIcon(NIM_ADD, &ni);
3746# endif
3747
3748 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003749 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003750 while (memchr(done, 0, numFiles) != NULL)
3751 {
3752# ifdef WIN32
3753 p = serverGetReply(srv, NULL, TRUE, TRUE);
3754 if (p == NULL)
3755 break;
3756# else
3757 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3758 break;
3759# endif
3760 j = atoi((char *)p);
3761 if (j >= 0 && j < numFiles)
3762 {
3763# ifdef FEAT_GUI_W32
3764 ++count;
3765 sprintf(ni.szTip, _("%d of %d edited"),
3766 count, numFiles);
3767 Shell_NotifyIcon(NIM_MODIFY, &ni);
3768# endif
3769 done[j] = 1;
3770 }
3771 }
3772# ifdef FEAT_GUI_W32
3773 Shell_NotifyIcon(NIM_DELETE, &ni);
3774# endif
3775 }
3776 }
3777 else if (STRICMP(argv[i], "--remote-expr") == 0)
3778 {
3779 if (i == *argc - 1)
3780 mainerr_arg_missing((char_u *)argv[i]);
3781# ifdef WIN32
3782 /* Win32 always works? */
3783 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3784 &res, NULL, 1, FALSE) < 0)
3785# else
3786 if (xterm_dpy == NULL)
3787 mch_errmsg(_("No display: Send expression failed.\n"));
3788 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3789 &res, NULL, 1, 1, FALSE) < 0)
3790# endif
3791 {
3792 if (res != NULL && *res != NUL)
3793 {
3794 /* Output error from remote */
3795 mch_errmsg((char *)res);
3796 vim_free(res);
3797 res = NULL;
3798 }
3799 mch_errmsg(_(": Send expression failed.\n"));
3800 }
3801 }
3802 else if (STRICMP(argv[i], "--serverlist") == 0)
3803 {
3804# ifdef WIN32
3805 /* Win32 always works? */
3806 res = serverGetVimNames();
3807# else
3808 if (xterm_dpy != NULL)
3809 res = serverGetVimNames(xterm_dpy);
3810# endif
3811 if (called_emsg)
3812 mch_errmsg("\n");
3813 }
3814 else if (STRICMP(argv[i], "--servername") == 0)
3815 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003816 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003817 i++;
3818 continue;
3819 }
3820 else
3821 {
3822 *newArgV++ = argv[i];
3823 newArgC++;
3824 continue;
3825 }
3826 didone = TRUE;
3827 if (res != NULL && *res != NUL)
3828 {
3829 mch_msg((char *)res);
3830 if (res[STRLEN(res) - 1] != '\n')
3831 mch_msg("\n");
3832 }
3833 vim_free(res);
3834 }
3835
3836 if (didone)
3837 {
3838 display_errors(); /* display any collected messages */
3839 exit(exiterr); /* Mission accomplished - get out */
3840 }
3841
3842 /* Return back into main() */
3843 *argc = newArgC;
3844 vim_free(sname);
3845}
3846
3847/*
3848 * Build a ":drop" command to send to a Vim server.
3849 */
3850 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003851build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003852 int filec;
3853 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003854 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003855 int sendReply;
3856{
3857 garray_T ga;
3858 int i;
3859 char_u *inicmd = NULL;
3860 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003861 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003862
3863 if (filec > 0 && filev[0][0] == '+')
3864 {
3865 inicmd = (char_u *)filev[0] + 1;
3866 filev++;
3867 filec--;
3868 }
3869 /* Check if we have at least one argument. */
3870 if (filec <= 0)
3871 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003872
3873 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003874 cwd = alloc(MAXPATHL);
3875 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003876 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003877 if (mch_dirname(cwd, MAXPATHL) != OK)
3878 {
3879 vim_free(cwd);
3880 return NULL;
3881 }
3882 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003883#ifdef BACKSLASH_IN_FILENAME
3884 "", /* rem_backslash() will tell what chars to escape */
3885#else
3886 PATH_ESC_CHARS,
3887#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003888 '\\', TRUE);
3889 vim_free(cwd);
3890 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003891 return NULL;
3892 ga_init2(&ga, 1, 100);
3893 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3894 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003895 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003896
3897 /* Call inputsave() so that a prompt for an encryption key works. */
3898 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3899 if (tabs)
3900 ga_concat(&ga, (char_u *)"tab ");
3901 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003902 for (i = 0; i < filec; i++)
3903 {
3904 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003905 * do it again in the Vim server. On MS-Windows only escape
3906 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003907 p = vim_strsave_escaped((char_u *)filev[i],
3908#ifdef UNIX
3909 PATH_ESC_CHARS
3910#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003911 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003912#endif
3913 );
3914 if (p == NULL)
3915 {
3916 vim_free(ga.ga_data);
3917 return NULL;
3918 }
3919 ga_concat(&ga, (char_u *)" ");
3920 ga_concat(&ga, p);
3921 vim_free(p);
3922 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003923 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3924
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003925 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3926 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003927 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3928
3929 /* Switch back to the correct current directory (prior to temporary path
3930 * switch) unless 'autochdir' is set, in which case it will already be
3931 * correct after the :drop command. */
3932 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3933
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003934 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003935 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3936 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003937 if (inicmd != NULL)
3938 {
3939 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3940 * the following commands to be inserted as text. Use a "|",
3941 * hopefully "inicmd" does allow this... */
3942 ga_concat(&ga, inicmd);
3943 ga_concat(&ga, (char_u *)"|");
3944 }
3945 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3946 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003947 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003948 ga_append(&ga, NUL);
3949 return ga.ga_data;
3950}
3951
3952/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003953 * Make our basic server name: use the specified "arg" if given, otherwise use
3954 * the tail of the command "cmd" we were started with.
3955 * Return the name in allocated memory. This doesn't include a serial number.
3956 */
3957 static char_u *
3958serverMakeName(arg, cmd)
3959 char_u *arg;
3960 char *cmd;
3961{
3962 char_u *p;
3963
3964 if (arg != NULL && *arg != NUL)
3965 p = vim_strsave_up(arg);
3966 else
3967 {
3968 p = vim_strsave_up(gettail((char_u *)cmd));
3969 /* Remove .exe or .bat from the name. */
3970 if (p != NULL && vim_strchr(p, '.') != NULL)
3971 *vim_strchr(p, '.') = NUL;
3972 }
3973 return p;
3974}
3975#endif /* FEAT_CLIENTSERVER */
3976
3977#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3978/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003979 * Replace termcodes such as <CR> and insert as key presses if there is room.
3980 */
3981 void
3982server_to_input_buf(str)
3983 char_u *str;
3984{
3985 char_u *ptr = NULL;
3986 char_u *cpo_save = p_cpo;
3987
3988 /* Set 'cpoptions' the way we want it.
3989 * B set - backslashes are *not* treated specially
3990 * k set - keycodes are *not* reverse-engineered
3991 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003992 * The last but one parameter of replace_termcodes() is TRUE so that the
3993 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003994 */
3995 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003996 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003997 p_cpo = cpo_save;
3998
3999 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4000 {
4001 /*
4002 * Add the string to the input stream.
4003 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4004 *
4005 * First clear typed characters from the typeahead buffer, there could
4006 * be half a mapping there. Then append to the existing string, so
4007 * that multiple commands from a client are concatenated.
4008 */
4009 if (typebuf.tb_maplen < typebuf.tb_len)
4010 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4011 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4012
4013 /* Let input_available() know we inserted text in the typeahead
4014 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004015 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004016 }
4017 vim_free((char_u *)ptr);
4018}
4019
4020/*
4021 * Evaluate an expression that the client sent to a string.
4022 * Handles disabling error messages and disables debugging, otherwise Vim
4023 * hangs, waiting for "cont" to be typed.
4024 */
4025 char_u *
4026eval_client_expr_to_string(expr)
4027 char_u *expr;
4028{
4029 char_u *res;
4030 int save_dbl = debug_break_level;
4031 int save_ro = redir_off;
4032
4033 debug_break_level = -1;
4034 redir_off = 0;
4035 ++emsg_skip;
4036
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004037 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004038
4039 debug_break_level = save_dbl;
4040 redir_off = save_ro;
4041 --emsg_skip;
4042
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004043 /* A client can tell us to redraw, but not to display the cursor, so do
4044 * that here. */
4045 setcursor();
4046 out_flush();
4047#ifdef FEAT_GUI
4048 if (gui.in_use)
4049 gui_update_cursor(FALSE, FALSE);
4050#endif
4051
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004052 return res;
4053}
4054
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004055/*
4056 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4057 * return an allocated string. Otherwise return "data".
4058 * "*tofree" is set to the result when it needs to be freed later.
4059 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004060 char_u *
4061serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004062 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004063 char_u *data;
4064 char_u **tofree;
4065{
4066 char_u *res = data;
4067
4068 *tofree = NULL;
4069# ifdef FEAT_MBYTE
4070 if (client_enc != NULL && p_enc != NULL)
4071 {
4072 vimconv_T vimconv;
4073
4074 vimconv.vc_type = CONV_NONE;
4075 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4076 && vimconv.vc_type != CONV_NONE)
4077 {
4078 res = string_convert(&vimconv, data, NULL);
4079 if (res == NULL)
4080 res = data;
4081 else
4082 *tofree = res;
4083 }
4084 convert_setup(&vimconv, NULL, NULL);
4085 }
4086# endif
4087 return res;
4088}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004089#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004090
4091/*
4092 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4093 */
4094#if defined(FEAT_FKMAP) || defined(PROTO)
4095# include "farsi.c"
4096#endif
4097
4098/*
4099 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4100 */
4101#if defined(FEAT_ARABIC) || defined(PROTO)
4102# include "arabic.c"
4103#endif