blob: cba8dce75da05e63b06848624e36cac0279d2955 [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");
Bram Moolenaar9584b312013-03-13 19:29:28 +0100807#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
808 may_req_ambiguous_character_width();
809#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000810
811#ifdef FEAT_MOUSE
812 setmouse(); /* may start using the mouse */
813#endif
814 if (scroll_region)
815 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000816 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000817
818 /*
819 * Don't clear the screen when starting in Ex mode, unless using the GUI.
820 */
821 if (exmode_active
822#ifdef FEAT_GUI
823 && !gui.in_use
824#endif
825 )
826 must_redraw = CLEAR;
827 else
828 {
829 screenclear(); /* clear screen */
830 TIME_MSG("clearing screen");
831 }
832
833#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000834 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000835 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200836 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000837 (void)get_crypt_key(TRUE, TRUE);
838 TIME_MSG("getting crypt key");
839 }
840#endif
841
842 no_wait_return = TRUE;
843
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000845 * Create the requested number of windows and edit buffers in them.
846 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000847 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000848 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000849 TIME_MSG("opening buffers");
850
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000851#ifdef FEAT_EVAL
852 /* clear v:swapcommand */
853 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
854#endif
855
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856 /* Ex starts at last line of the file */
857 if (exmode_active)
858 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
859
860#ifdef FEAT_AUTOCMD
861 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
862 TIME_MSG("BufEnter autocommands");
863#endif
864 setpcmark();
865
866#ifdef FEAT_QUICKFIX
867 /*
868 * When started with "-q errorfile" jump to first error now.
869 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000870 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000871 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000872 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000873 TIME_MSG("jump to first error");
874 }
875#endif
876
877#ifdef FEAT_WINDOWS
878 /*
879 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000880 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000881 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000882 edit_buffers(&params);
883#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000884
885#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000886 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000887 {
888 win_T *wp;
889
890 /* set options in each window for "vimdiff". */
891 for (wp = firstwin; wp != NULL; wp = wp->w_next)
892 diff_win_options(wp, TRUE);
893 }
894#endif
895
896 /*
897 * Shorten any of the filenames, but only when absolute.
898 */
899 shorten_fnames(FALSE);
900
901 /*
902 * Need to jump to the tag before executing the '-c command'.
903 * Makes "vim -c '/return' -t main" work.
904 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000905 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000906 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000907#if defined(HAS_SWAP_EXISTS_ACTION)
908 swap_exists_did_quit = FALSE;
909#endif
910
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000911 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000912 do_cmdline_cmd(IObuff);
913 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000914
915#if defined(HAS_SWAP_EXISTS_ACTION)
916 /* If the user doesn't want to edit the file then we quit here. */
917 if (swap_exists_did_quit)
918 getout(1);
919#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000920 }
921
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000922 /* Execute any "+", "-c" and "-S" arguments. */
923 if (params.n_commands > 0)
924 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000925
926 RedrawingDisabled = 0;
927 redraw_all_later(NOT_VALID);
928 no_wait_return = FALSE;
929 starting = 0;
930
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000931#ifdef FEAT_TERMRESPONSE
932 /* Requesting the termresponse is postponed until here, so that a "-c q"
933 * argument doesn't make it appear in the shell Vim was started from. */
934 may_req_termresponse();
935#endif
936
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000937 /* start in insert mode */
938 if (p_im)
939 need_start_insertmode = TRUE;
940
941#ifdef FEAT_AUTOCMD
942 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
943 TIME_MSG("VimEnter autocommands");
944#endif
945
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200946#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
947 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
948 * done after the clipboard is available and all initial commands that may
949 * modify the 'clipboard' setting have run; i.e. just before entering the
950 * main loop. */
951 {
952 int default_regname = 0;
953 adjust_clip_reg(&default_regname);
954 set_reg_var(default_regname);
955 }
956#endif
957
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000958#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
959 /* When a startup script or session file setup for diff'ing and
960 * scrollbind, sync the scrollbind now. */
961 if (curwin->w_p_diff && curwin->w_p_scb)
962 {
963 update_topline();
964 check_scrollbind((linenr_T)0, 0L);
965 TIME_MSG("diff scrollbinding");
966 }
967#endif
968
969#if defined(WIN3264) && !defined(FEAT_GUI_W32)
970 mch_set_winsize_now(); /* Allow winsize changes from now on */
971#endif
972
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000973#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
974 /* When tab pages were created, may need to update the tab pages line and
975 * scrollbars. This is skipped while creating them. */
976 if (first_tabpage->tp_next != NULL)
977 {
978 out_flush();
979 gui_init_which_components(NULL);
980 gui_update_scrollbars(TRUE);
981 }
982 need_mouse_correct = TRUE;
983#endif
984
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000985 /* If ":startinsert" command used, stuff a dummy command to be able to
986 * call normal_cmd(), which will then start Insert mode. */
987 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000988 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000989
990#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200991 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200992 {
993# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +0200994# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +0200995 && !defined(FEAT_GUI_W32)
996 if (gui.in_use)
997 {
998 mch_errmsg(_("netbeans is not supported with this GUI\n"));
999 mch_exit(2);
1000 }
1001# endif
1002# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001003 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001004 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001005 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001006#endif
1007
1008 TIME_MSG("before starting main loop");
1009
1010 /*
1011 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001012 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001013 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001014
1015 return 0;
1016}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001017#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001018#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001019
1020/*
1021 * Main loop: Execute Normal mode commands until exiting Vim.
1022 * Also used to handle commands in the command-line window, until the window
1023 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001024 * Also used to handle ":visual" command after ":global": execute Normal mode
1025 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026 */
1027 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001028main_loop(cmdwin, noexmode)
1029 int cmdwin; /* TRUE when working in the command-line window */
1030 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001031{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001032 oparg_T oa; /* operator arguments */
1033 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001034#ifdef FEAT_CONCEAL
1035 linenr_T conceal_old_cursor_line = 0;
1036 linenr_T conceal_new_cursor_line = 0;
1037 int conceal_update_lines = FALSE;
1038#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001039
1040#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1041 /* Setup to catch a terminating error from the X server. Just ignore
1042 * it, restore the state and continue. This might not always work
1043 * properly, but at least we don't exit unexpectedly when the X server
1044 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046 {
1047 State = NORMAL;
1048# ifdef FEAT_VISUAL
1049 VIsual_active = FALSE;
1050# endif
1051 got_int = TRUE;
1052 need_wait_return = FALSE;
1053 global_busy = FALSE;
1054 exmode_active = 0;
1055 skip_redraw = FALSE;
1056 RedrawingDisabled = 0;
1057 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001058 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001059# ifdef FEAT_EVAL
1060 emsg_skip = 0;
1061# endif
1062 emsg_off = 0;
1063# ifdef FEAT_MOUSE
1064 setmouse();
1065# endif
1066 settmode(TMODE_RAW);
1067 starttermcap();
1068 scroll_start();
1069 redraw_later_clear();
1070 }
1071#endif
1072
1073 clear_oparg(&oa);
1074 while (!cmdwin
1075#ifdef FEAT_CMDWIN
1076 || cmdwin_result == 0
1077#endif
1078 )
1079 {
1080 if (stuff_empty())
1081 {
1082 did_check_timestamps = FALSE;
1083 if (need_check_timestamps)
1084 check_timestamps(FALSE);
1085 if (need_wait_return) /* if wait_return still needed ... */
1086 wait_return(FALSE); /* ... call it now */
1087 if (need_start_insertmode && goto_im()
1088#ifdef FEAT_VISUAL
1089 && !VIsual_active
1090#endif
1091 )
1092 {
1093 need_start_insertmode = FALSE;
1094 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1095 /* skip the fileinfo message now, because it would be shown
1096 * after insert mode finishes! */
1097 need_fileinfo = FALSE;
1098 }
1099 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001100
1101 /* Reset "got_int" now that we got back to the main loop. Except when
1102 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1103 * the ":g" command.
1104 * For ":g/pat/vi" we reset "got_int" when used once. When used
1105 * a second time we go back to Ex mode and abort the ":g" command. */
1106 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001107 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001108 if (noexmode && global_busy && !exmode_active && previous_got_int)
1109 {
1110 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1111 * used and keep "got_int" set, so that it aborts ":g". */
1112 exmode_active = EXMODE_NORMAL;
1113 State = NORMAL;
1114 }
1115 else if (!global_busy || !exmode_active)
1116 {
1117 if (!quit_more)
1118 (void)vgetc(); /* flush all buffers */
1119 got_int = FALSE;
1120 }
1121 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001122 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001123 else
1124 previous_got_int = FALSE;
1125
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001126 if (!exmode_active)
1127 msg_scroll = FALSE;
1128 quit_more = FALSE;
1129
1130 /*
1131 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1132 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1133 * update cursor and redraw.
1134 */
1135 if (skip_redraw || exmode_active)
1136 skip_redraw = FALSE;
1137 else if (do_redraw || stuff_empty())
1138 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001139#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001140 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001141 if (!finish_op && (
1142# ifdef FEAT_AUTOCMD
1143 has_cursormoved()
1144# endif
1145# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1146 ||
1147# endif
1148# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001149 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001150# endif
1151 )
1152 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001153 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001154# ifdef FEAT_AUTOCMD
1155 if (has_cursormoved())
1156 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1157 FALSE, curbuf);
1158# endif
1159# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001160 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001161 {
1162 conceal_old_cursor_line = last_cursormoved.lnum;
1163 conceal_new_cursor_line = curwin->w_cursor.lnum;
1164 conceal_update_lines = TRUE;
1165 }
1166# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001167 last_cursormoved = curwin->w_cursor;
1168 }
1169#endif
1170
Bram Moolenaar186628f2013-03-19 13:33:23 +01001171#ifdef FEAT_AUTOCMD
1172 /* Trigger TextChanged if b_changedtick differs. */
1173 if (!finish_op && has_textchanged()
1174 && last_changedtick != curbuf->b_changedtick)
1175 {
1176 if (last_changedtick_buf == curbuf)
1177 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1178 FALSE, curbuf);
1179 last_changedtick_buf = curbuf;
1180 last_changedtick = curbuf->b_changedtick;
1181 }
1182#endif
1183
Bram Moolenaar33aec762006-01-22 23:30:12 +00001184#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1185 /* Scroll-binding for diff mode may have been postponed until
1186 * here. Avoids doing it for every change. */
1187 if (diff_need_scrollbind)
1188 {
1189 check_scrollbind((linenr_T)0, 0L);
1190 diff_need_scrollbind = FALSE;
1191 }
1192#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001193#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1194 /* Include a closed fold completely in the Visual area. */
1195 foldAdjustVisual();
1196#endif
1197#ifdef FEAT_FOLDING
1198 /*
1199 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1200 * contain the cursor.
1201 * When 'foldopen' is "all", open the fold(s) under the cursor.
1202 * This may mark the window for redrawing.
1203 */
1204 if (hasAnyFolding(curwin) && !char_avail())
1205 {
1206 foldCheckClose();
1207 if (fdo_flags & FDO_ALL)
1208 foldOpenCursor();
1209 }
1210#endif
1211
1212 /*
1213 * Before redrawing, make sure w_topline is correct, and w_leftcol
1214 * if lines don't wrap, and w_skipcol if lines wrap.
1215 */
1216 update_topline();
1217 validate_cursor();
1218
1219#ifdef FEAT_VISUAL
1220 if (VIsual_active)
1221 update_curbuf(INVERTED);/* update inverted part */
1222 else
1223#endif
1224 if (must_redraw)
1225 update_screen(0);
1226 else if (redraw_cmdline || clear_cmdline)
1227 showmode();
1228#ifdef FEAT_WINDOWS
1229 redraw_statuslines();
1230#endif
1231#ifdef FEAT_TITLE
1232 if (need_maketitle)
1233 maketitle();
1234#endif
1235 /* display message after redraw */
1236 if (keep_msg != NULL)
1237 {
1238 char_u *p;
1239
1240 /* msg_attr_keep() will set keep_msg to NULL, must free the
1241 * string here. */
1242 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001243 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001244 msg_attr(p, keep_msg_attr);
1245 vim_free(p);
1246 }
1247 if (need_fileinfo) /* show file info after redraw */
1248 {
1249 fileinfo(FALSE, TRUE, FALSE);
1250 need_fileinfo = FALSE;
1251 }
1252
1253 emsg_on_display = FALSE; /* can delete error message now */
1254 did_emsg = FALSE;
1255 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001256 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001257 showruler(FALSE);
1258
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001259# if defined(FEAT_CONCEAL)
1260 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001261 && (conceal_old_cursor_line != conceal_new_cursor_line
1262 || conceal_cursor_line(curwin)
1263 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001264 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001265 if (conceal_old_cursor_line != conceal_new_cursor_line
1266 && conceal_old_cursor_line
1267 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001268 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001269 update_single_line(curwin, conceal_new_cursor_line);
1270 curwin->w_valid &= ~VALID_CROW;
1271 }
1272# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001273 setcursor();
1274 cursor_on();
1275
1276 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001277
1278#ifdef STARTUPTIME
1279 /* Now that we have drawn the first screen all the startup stuff
1280 * has been done, close any file for startup messages. */
1281 if (time_fd != NULL)
1282 {
1283 TIME_MSG("first screen update");
1284 TIME_MSG("--- VIM STARTED ---");
1285 fclose(time_fd);
1286 time_fd = NULL;
1287 }
1288#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001289 }
1290#ifdef FEAT_GUI
1291 if (need_mouse_correct)
1292 gui_mouse_correct();
1293#endif
1294
1295 /*
1296 * Update w_curswant if w_set_curswant has been set.
1297 * Postponed until here to avoid computing w_virtcol too often.
1298 */
1299 update_curswant();
1300
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001301#ifdef FEAT_EVAL
1302 /*
1303 * May perform garbage collection when waiting for a character, but
1304 * only at the very toplevel. Otherwise we may be using a List or
1305 * Dict internally somewhere.
1306 * "may_garbage_collect" is reset in vgetc() which is invoked through
1307 * do_exmode() and normal_cmd().
1308 */
1309 may_garbage_collect = (!cmdwin && !noexmode);
1310#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001311 /*
1312 * If we're invoked as ex, do a round of ex commands.
1313 * Otherwise, get and execute a normal mode command.
1314 */
1315 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001316 {
1317 if (noexmode) /* End of ":global/path/visual" commands */
1318 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001319 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001320 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001321 else
1322 normal_cmd(&oa, TRUE);
1323 }
1324}
1325
1326
1327#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1328/*
1329 * Exit, but leave behind swap files for modified buffers.
1330 */
1331 void
1332getout_preserve_modified(exitval)
1333 int exitval;
1334{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001335# if defined(SIGHUP) && defined(SIG_IGN)
1336 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1337 * makes Vim exit and then handling SIGHUP causes various reentrance
1338 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001339 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001340# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001341
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001342 ml_close_notmod(); /* close all not-modified buffers */
1343 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1344 ml_close_all(FALSE); /* close all memfiles, without deleting */
1345 getout(exitval); /* exit Vim properly */
1346}
1347#endif
1348
1349
1350/* Exit properly */
1351 void
1352getout(exitval)
1353 int exitval;
1354{
1355#ifdef FEAT_AUTOCMD
1356 buf_T *buf;
1357 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001358 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001359#endif
1360
1361 exiting = TRUE;
1362
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001363 /* When running in Ex mode an error causes us to exit with a non-zero exit
1364 * code. POSIX requires this, although it's not 100% clear from the
1365 * standard. */
1366 if (exmode_active)
1367 exitval += ex_exitval;
1368
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001369 /* Position the cursor on the last screen line, below all the text */
1370#ifdef FEAT_GUI
1371 if (!gui.in_use)
1372#endif
1373 windgoto((int)Rows - 1, 0);
1374
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001375#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1376 /* Optionally print hashtable efficiency. */
1377 hash_debug_results();
1378#endif
1379
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001380#ifdef FEAT_GUI
1381 msg_didany = FALSE;
1382#endif
1383
1384#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001385 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001386 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001387 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1388# if defined FEAT_WINDOWS
1389 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001390 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001391 next_tp = tp->tp_next;
1392 for (wp = (tp == curtab)
1393 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001394 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001395 if (wp->w_buffer == NULL)
1396 /* Autocmd must have close the buffer already, skip. */
1397 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001398 buf = wp->w_buffer;
1399 if (buf->b_changedtick != -1)
1400 {
1401 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1402 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001403 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001404 /* start all over, autocommands may mess up the lists */
1405 next_tp = first_tabpage;
1406 break;
1407 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001408 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001409 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001410# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001411 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1412 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001413# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001414
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001415 /* Trigger BufUnload for buffers that are loaded */
1416 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1417 if (buf->b_ml.ml_mfp != NULL)
1418 {
1419 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001420 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001421 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1422 break;
1423 }
1424 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1425 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001426#endif
1427
1428#ifdef FEAT_VIMINFO
1429 if (*p_viminfo != NUL)
1430 /* Write out the registers, history, marks etc, to the viminfo file */
1431 write_viminfo(NULL, FALSE);
1432#endif
1433
1434#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001435 if (get_vim_var_nr(VV_DYING) <= 1)
1436 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001437#endif
1438
Bram Moolenaar05159a02005-02-26 23:04:13 +00001439#ifdef FEAT_PROFILE
1440 profile_dump();
1441#endif
1442
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001443 if (did_emsg
1444#ifdef FEAT_GUI
1445 || (gui.in_use && msg_didany && p_verbose > 0)
1446#endif
1447 )
1448 {
1449 /* give the user a chance to read the (error) message */
1450 no_wait_return = FALSE;
1451 wait_return(FALSE);
1452 }
1453
1454#ifdef FEAT_AUTOCMD
1455 /* Position the cursor again, the autocommands may have moved it */
1456# ifdef FEAT_GUI
1457 if (!gui.in_use)
1458# endif
1459 windgoto((int)Rows - 1, 0);
1460#endif
1461
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001462#ifdef FEAT_LUA
1463 lua_end();
1464#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001465#ifdef FEAT_MZSCHEME
1466 mzscheme_end();
1467#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001468#ifdef FEAT_TCL
1469 tcl_end();
1470#endif
1471#ifdef FEAT_RUBY
1472 ruby_end();
1473#endif
1474#ifdef FEAT_PYTHON
1475 python_end();
1476#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001477#ifdef FEAT_PYTHON3
1478 python3_end();
1479#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001480#ifdef FEAT_PERL
1481 perl_end();
1482#endif
1483#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1484 iconv_end();
1485#endif
1486#ifdef FEAT_NETBEANS_INTG
1487 netbeans_end();
1488#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001489#ifdef FEAT_CSCOPE
1490 cs_end();
1491#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001492#ifdef FEAT_EVAL
1493 if (garbage_collect_at_exit)
1494 garbage_collect();
1495#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001496
1497 mch_exit(exitval);
1498}
1499
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001500#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001501/*
1502 * Get a (optional) count for a Vim argument.
1503 */
1504 static int
1505get_number_arg(p, idx, def)
1506 char_u *p; /* pointer to argument */
1507 int *idx; /* index in argument, is incremented */
1508 int def; /* default value */
1509{
1510 if (vim_isdigit(p[*idx]))
1511 {
1512 def = atoi((char *)&(p[*idx]));
1513 while (vim_isdigit(p[*idx]))
1514 *idx = *idx + 1;
1515 }
1516 return def;
1517}
1518
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001519#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1520/*
1521 * Setup to use the current locale (for ctype() and many other things).
1522 */
1523 static void
1524init_locale()
1525{
1526 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001527
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001528# ifdef FEAT_GUI_GTK
1529 /* Tell Gtk not to change our locale settings. */
1530 gtk_disable_setlocale();
1531# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001532# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1533 /* Make sure strtod() uses a decimal point, not a comma. */
1534 setlocale(LC_NUMERIC, "C");
1535# endif
1536
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001537# ifdef WIN32
1538 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1539 * text while it's expecting text in the current locale. This call avoids
1540 * that. */
1541 setlocale(LC_CTYPE, "C");
1542# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001543
1544# ifdef FEAT_GETTEXT
1545 {
1546 int mustfree = FALSE;
1547 char_u *p;
1548
1549# ifdef DYNAMIC_GETTEXT
1550 /* Initialize the gettext library */
1551 dyn_libintl_init(NULL);
1552# endif
1553 /* expand_env() doesn't work yet, because chartab[] is not initialized
1554 * yet, call vim_getenv() directly */
1555 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1556 if (p != NULL && *p != NUL)
1557 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001558 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001559 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1560 }
1561 if (mustfree)
1562 vim_free(p);
1563 textdomain(VIMPACKAGE);
1564 }
1565# endif
1566}
1567#endif
1568
1569/*
1570 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1571 * If the executable name starts with "r" we disable shell commands.
1572 * If the next character is "e" we run in Easy mode.
1573 * If the next character is "g" we run the GUI version.
1574 * If the next characters are "view" we start in readonly mode.
1575 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1576 * If the next characters are "ex" we start in Ex mode. If it's followed
1577 * by "im" use improved Ex mode.
1578 */
1579 static void
1580parse_command_name(parmp)
1581 mparm_T *parmp;
1582{
1583 char_u *initstr;
1584
1585 initstr = gettail((char_u *)parmp->argv[0]);
1586
1587#ifdef MACOS_X_UNIX
1588 /* An issue has been seen when launching Vim in such a way that
1589 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1590 * executable or a symbolic link of it. Until this issue is resolved
1591 * we prohibit the GUI from being used.
1592 */
1593 if (STRCMP(initstr, parmp->argv[0]) == 0)
1594 disallow_gui = TRUE;
1595
1596 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001597 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001598#endif
1599
1600#ifdef FEAT_EVAL
1601 set_vim_var_string(VV_PROGNAME, initstr, -1);
1602#endif
1603
1604 if (TOLOWER_ASC(initstr[0]) == 'r')
1605 {
1606 restricted = TRUE;
1607 ++initstr;
1608 }
1609
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001610 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001611 if (TOLOWER_ASC(initstr[0]) == 'e'
1612 && (TOLOWER_ASC(initstr[1]) == 'v'
1613 || TOLOWER_ASC(initstr[1]) == 'g'))
1614 {
1615#ifdef FEAT_GUI
1616 gui.starting = TRUE;
1617#endif
1618 parmp->evim_mode = TRUE;
1619 ++initstr;
1620 }
1621
Bram Moolenaar806875d2008-09-18 18:57:10 +00001622 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1623 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001624 {
1625 main_start_gui();
1626#ifdef FEAT_GUI
1627 ++initstr;
1628#endif
1629 }
1630
1631 if (STRNICMP(initstr, "view", 4) == 0)
1632 {
1633 readonlymode = TRUE;
1634 curbuf->b_p_ro = TRUE;
1635 p_uc = 10000; /* don't update very often */
1636 initstr += 4;
1637 }
1638 else if (STRNICMP(initstr, "vim", 3) == 0)
1639 initstr += 3;
1640
1641 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1642 if (STRICMP(initstr, "diff") == 0)
1643 {
1644#ifdef FEAT_DIFF
1645 parmp->diff_mode = TRUE;
1646#else
1647 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1648 mch_errmsg("\n");
1649 mch_exit(2);
1650#endif
1651 }
1652
1653 if (STRNICMP(initstr, "ex", 2) == 0)
1654 {
1655 if (STRNICMP(initstr + 2, "im", 2) == 0)
1656 exmode_active = EXMODE_VIM;
1657 else
1658 exmode_active = EXMODE_NORMAL;
1659 change_compatible(TRUE); /* set 'compatible' */
1660 }
1661}
1662
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001663/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001664 * Get the name of the display, before gui_prepare() removes it from
1665 * argv[]. Used for the xterm-clipboard display.
1666 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001667 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001668 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001669 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001670early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001671 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001672{
Bram Moolenaar03005972008-11-20 13:12:36 +00001673#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1674 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001675 int argc = parmp->argc;
1676 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001677 int i;
1678
1679 for (i = 1; i < argc; i++)
1680 {
1681 if (STRCMP(argv[i], "--") == 0)
1682 break;
1683# ifdef FEAT_XCLIPBOARD
1684 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001685# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001686 || STRICMP(argv[i], "--display") == 0
1687# endif
1688 )
1689 {
1690 if (i == argc - 1)
1691 mainerr_arg_missing((char_u *)argv[i]);
1692 xterm_display = argv[++i];
1693 }
1694# endif
1695# ifdef FEAT_CLIENTSERVER
1696 else if (STRICMP(argv[i], "--servername") == 0)
1697 {
1698 if (i == argc - 1)
1699 mainerr_arg_missing((char_u *)argv[i]);
1700 parmp->serverName_arg = (char_u *)argv[++i];
1701 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001702 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001703 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001704 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001705 {
1706 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001707# ifdef FEAT_GUI
1708 if (strstr(argv[i], "-wait") != 0)
1709 /* don't fork() when starting the GUI to edit files ourself */
1710 gui.dofork = FALSE;
1711# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001712 }
1713# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001714
1715# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1716# ifdef FEAT_GUI_W32
1717 else if (STRICMP(argv[i], "--windowid") == 0)
1718# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001719 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001720# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001721 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001722 long_u id;
1723 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001724
1725 if (i == argc - 1)
1726 mainerr_arg_missing((char_u *)argv[i]);
1727 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001728 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001729 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001730 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001731 if (count != 1)
1732 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1733 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001734# ifdef FEAT_GUI_W32
1735 win_socket_id = id;
1736# else
1737 gtk_socket_id = id;
1738# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001739 i++;
1740 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001741# endif
1742# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001743 else if (STRICMP(argv[i], "--echo-wid") == 0)
1744 echo_wid_arg = TRUE;
1745# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001746# ifndef FEAT_NETBEANS_INTG
1747 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001748 {
1749 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1750 mch_exit(2);
1751 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001752# endif
1753
Bram Moolenaar58d98232005-07-23 22:25:46 +00001754 }
1755#endif
1756}
1757
1758/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001759 * Scan the command line arguments.
1760 */
1761 static void
1762command_line_scan(parmp)
1763 mparm_T *parmp;
1764{
1765 int argc = parmp->argc;
1766 char **argv = parmp->argv;
1767 int argv_idx; /* index in argv[n][] */
1768 int had_minmin = FALSE; /* found "--" argument */
1769 int want_argument; /* option argument with argument */
1770 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001771 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001772 long n;
1773
1774 --argc;
1775 ++argv;
1776 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1777 while (argc > 0)
1778 {
1779 /*
1780 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1781 */
1782 if (argv[0][0] == '+' && !had_minmin)
1783 {
1784 if (parmp->n_commands >= MAX_ARG_CMDS)
1785 mainerr(ME_EXTRA_CMD, NULL);
1786 argv_idx = -1; /* skip to next argument */
1787 if (argv[0][1] == NUL)
1788 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1789 else
1790 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1791 }
1792
1793 /*
1794 * Optional argument.
1795 */
1796 else if (argv[0][0] == '-' && !had_minmin)
1797 {
1798 want_argument = FALSE;
1799 c = argv[0][argv_idx++];
1800#ifdef VMS
1801 /*
1802 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1803 * and "-/X" as "-X".
1804 */
1805 if (c == '/')
1806 {
1807 c = argv[0][argv_idx++];
1808 c = TOUPPER_ASC(c);
1809 }
1810 else
1811 c = TOLOWER_ASC(c);
1812#endif
1813 switch (c)
1814 {
1815 case NUL: /* "vim -" read from stdin */
1816 /* "ex -" silent mode */
1817 if (exmode_active)
1818 silent_mode = TRUE;
1819 else
1820 {
1821 if (parmp->edit_type != EDIT_NONE)
1822 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1823 parmp->edit_type = EDIT_STDIN;
1824 read_cmd_fd = 2; /* read from stderr instead of stdin */
1825 }
1826 argv_idx = -1; /* skip to next argument */
1827 break;
1828
1829 case '-': /* "--" don't take any more option arguments */
1830 /* "--help" give help message */
1831 /* "--version" give version message */
1832 /* "--literal" take files literally */
1833 /* "--nofork" don't fork */
1834 /* "--noplugin[s]" skip plugins */
1835 /* "--cmd <cmd>" execute cmd before vimrc */
1836 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1837 usage();
1838 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1839 {
1840 Columns = 80; /* need to init Columns */
1841 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1842 list_version();
1843 msg_putchar('\n');
1844 msg_didout = FALSE;
1845 mch_exit(0);
1846 }
1847 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1848 {
1849#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1850 parmp->literal = TRUE;
1851#endif
1852 }
1853 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1854 {
1855#ifdef FEAT_GUI
1856 gui.dofork = FALSE; /* don't fork() when starting GUI */
1857#endif
1858 }
1859 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1860 p_lpl = FALSE;
1861 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1862 {
1863 want_argument = TRUE;
1864 argv_idx += 3;
1865 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001866 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1867 {
1868 want_argument = TRUE;
1869 argv_idx += 11;
1870 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001871#ifdef FEAT_CLIENTSERVER
1872 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1873 ; /* already processed -- no arg */
1874 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1875 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1876 {
1877 /* already processed -- snatch the following arg */
1878 if (argc > 1)
1879 {
1880 --argc;
1881 ++argv;
1882 }
1883 }
1884#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001885#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1886# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001887 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001888# else
1889 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1890# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001891 {
1892 /* already processed -- snatch the following arg */
1893 if (argc > 1)
1894 {
1895 --argc;
1896 ++argv;
1897 }
1898 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001899#endif
1900#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001901 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1902 {
1903 /* already processed, skip */
1904 }
1905#endif
1906 else
1907 {
1908 if (argv[0][argv_idx])
1909 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1910 had_minmin = TRUE;
1911 }
1912 if (!want_argument)
1913 argv_idx = -1; /* skip to next argument */
1914 break;
1915
1916 case 'A': /* "-A" start in Arabic mode */
1917#ifdef FEAT_ARABIC
1918 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1919#else
1920 mch_errmsg(_(e_noarabic));
1921 mch_exit(2);
1922#endif
1923 break;
1924
1925 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001926 /* Needs to be effective before expanding file names, because
1927 * for Win32 this makes us edit a shortcut file itself,
1928 * instead of the file it links to. */
1929 set_options_bin(curbuf->b_p_bin, 1, 0);
1930 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001931 break;
1932
1933 case 'C': /* "-C" Compatible */
1934 change_compatible(TRUE);
1935 break;
1936
1937 case 'e': /* "-e" Ex mode */
1938 exmode_active = EXMODE_NORMAL;
1939 break;
1940
1941 case 'E': /* "-E" Improved Ex mode */
1942 exmode_active = EXMODE_VIM;
1943 break;
1944
1945 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1946 window directly, not with newcli */
1947#ifdef FEAT_GUI
1948 gui.dofork = FALSE; /* don't fork() when starting GUI */
1949#endif
1950 break;
1951
1952 case 'g': /* "-g" start GUI */
1953 main_start_gui();
1954 break;
1955
1956 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1957#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001958 p_fkmap = TRUE;
1959 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001960#else
1961 mch_errmsg(_(e_nofarsi));
1962 mch_exit(2);
1963#endif
1964 break;
1965
1966 case 'h': /* "-h" give help message */
1967#ifdef FEAT_GUI_GNOME
1968 /* Tell usage() to exit for "gvim". */
1969 gui.starting = FALSE;
1970#endif
1971 usage();
1972 break;
1973
1974 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1975#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001976 p_hkmap = TRUE;
1977 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001978#else
1979 mch_errmsg(_(e_nohebrew));
1980 mch_exit(2);
1981#endif
1982 break;
1983
1984 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1985#ifdef FEAT_LISP
1986 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1987 p_sm = TRUE;
1988#endif
1989 break;
1990
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001991 case 'M': /* "-M" no changes or writing of files */
1992 reset_modifiable();
1993 /* FALLTHROUGH */
1994
1995 case 'm': /* "-m" no writing of files */
1996 p_write = FALSE;
1997 break;
1998
1999 case 'y': /* "-y" easy mode */
2000#ifdef FEAT_GUI
2001 gui.starting = TRUE; /* start GUI a bit later */
2002#endif
2003 parmp->evim_mode = TRUE;
2004 break;
2005
2006 case 'N': /* "-N" Nocompatible */
2007 change_compatible(FALSE);
2008 break;
2009
2010 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002011#ifdef FEAT_NETBEANS_INTG
2012 /* checking for "-nb", netbeans parameters */
2013 if (argv[0][argv_idx] == 'b')
2014 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002015 netbeansArg = argv[0];
2016 argv_idx = -1; /* skip to next argument */
2017 }
2018 else
2019#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002020 parmp->no_swap_file = TRUE;
2021 break;
2022
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002023 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002024#ifdef TARGET_API_MAC_OSX
2025 /* For some reason on MacOS X, an argument like:
2026 -psn_0_10223617 is passed in when invoke from Finder
2027 or with the 'open' command */
2028 if (argv[0][argv_idx] == 's')
2029 {
2030 argv_idx = -1; /* bypass full -psn */
2031 main_start_gui();
2032 break;
2033 }
2034#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002035#ifdef FEAT_WINDOWS
2036 /* default is 0: open window for each file */
2037 parmp->window_count = get_number_arg((char_u *)argv[0],
2038 &argv_idx, 0);
2039 parmp->window_layout = WIN_TABS;
2040#endif
2041 break;
2042
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002043 case 'o': /* "-o[N]" open N horizontal split windows */
2044#ifdef FEAT_WINDOWS
2045 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002046 parmp->window_count = get_number_arg((char_u *)argv[0],
2047 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002048 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002049#endif
2050 break;
2051
2052 case 'O': /* "-O[N]" open N vertical split windows */
2053#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2054 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002055 parmp->window_count = get_number_arg((char_u *)argv[0],
2056 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002057 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002058#endif
2059 break;
2060
2061#ifdef FEAT_QUICKFIX
2062 case 'q': /* "-q" QuickFix mode */
2063 if (parmp->edit_type != EDIT_NONE)
2064 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2065 parmp->edit_type = EDIT_QF;
2066 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2067 {
2068 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2069 argv_idx = -1;
2070 }
2071 else if (argc > 1) /* "-q {errorfile}" */
2072 want_argument = TRUE;
2073 break;
2074#endif
2075
2076 case 'R': /* "-R" readonly mode */
2077 readonlymode = TRUE;
2078 curbuf->b_p_ro = TRUE;
2079 p_uc = 10000; /* don't update very often */
2080 break;
2081
2082 case 'r': /* "-r" recovery mode */
2083 case 'L': /* "-L" recovery mode */
2084 recoverymode = 1;
2085 break;
2086
2087 case 's':
2088 if (exmode_active) /* "-s" silent (batch) mode */
2089 silent_mode = TRUE;
2090 else /* "-s {scriptin}" read from script file */
2091 want_argument = TRUE;
2092 break;
2093
2094 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2095 if (parmp->edit_type != EDIT_NONE)
2096 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2097 parmp->edit_type = EDIT_TAG;
2098 if (argv[0][argv_idx]) /* "-t{tag}" */
2099 {
2100 parmp->tagname = (char_u *)argv[0] + argv_idx;
2101 argv_idx = -1;
2102 }
2103 else /* "-t {tag}" */
2104 want_argument = TRUE;
2105 break;
2106
2107#ifdef FEAT_EVAL
2108 case 'D': /* "-D" Debugging */
2109 parmp->use_debug_break_level = 9999;
2110 break;
2111#endif
2112#ifdef FEAT_DIFF
2113 case 'd': /* "-d" 'diff' */
2114# ifdef AMIGA
2115 /* check for "-dev {device}" */
2116 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2117 want_argument = TRUE;
2118 else
2119# endif
2120 parmp->diff_mode = TRUE;
2121 break;
2122#endif
2123 case 'V': /* "-V{N}" Verbose level */
2124 /* default is 10: a little bit verbose */
2125 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2126 if (argv[0][argv_idx] != NUL)
2127 {
2128 set_option_value((char_u *)"verbosefile", 0L,
2129 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002130 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002131 }
2132 break;
2133
2134 case 'v': /* "-v" Vi-mode (as if called "vi") */
2135 exmode_active = 0;
2136#ifdef FEAT_GUI
2137 gui.starting = FALSE; /* don't start GUI */
2138#endif
2139 break;
2140
2141 case 'w': /* "-w{number}" set window height */
2142 /* "-w {scriptout}" write to script */
2143 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2144 {
2145 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2146 set_option_value((char_u *)"window", n, NULL, 0);
2147 break;
2148 }
2149 want_argument = TRUE;
2150 break;
2151
2152#ifdef FEAT_CRYPT
2153 case 'x': /* "-x" encrypted reading/writing of files */
2154 parmp->ask_for_key = TRUE;
2155 break;
2156#endif
2157
2158 case 'X': /* "-X" don't connect to X server */
2159#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2160 x_no_connect = TRUE;
2161#endif
2162 break;
2163
2164 case 'Z': /* "-Z" restricted mode */
2165 restricted = TRUE;
2166 break;
2167
2168 case 'c': /* "-c{command}" or "-c {command}" execute
2169 command */
2170 if (argv[0][argv_idx] != NUL)
2171 {
2172 if (parmp->n_commands >= MAX_ARG_CMDS)
2173 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002174 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2175 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002176 argv_idx = -1;
2177 break;
2178 }
2179 /*FALLTHROUGH*/
2180 case 'S': /* "-S {file}" execute Vim script */
2181 case 'i': /* "-i {viminfo}" use for viminfo */
2182#ifndef FEAT_DIFF
2183 case 'd': /* "-d {device}" device (for Amiga) */
2184#endif
2185 case 'T': /* "-T {terminal}" terminal name */
2186 case 'u': /* "-u {vimrc}" vim inits file */
2187 case 'U': /* "-U {gvimrc}" gvim inits file */
2188 case 'W': /* "-W {scriptout}" overwrite */
2189#ifdef FEAT_GUI_W32
2190 case 'P': /* "-P {parent title}" MDI parent */
2191#endif
2192 want_argument = TRUE;
2193 break;
2194
2195 default:
2196 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2197 }
2198
2199 /*
2200 * Handle option arguments with argument.
2201 */
2202 if (want_argument)
2203 {
2204 /*
2205 * Check for garbage immediately after the option letter.
2206 */
2207 if (argv[0][argv_idx] != NUL)
2208 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2209
2210 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002211 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002212 mainerr_arg_missing((char_u *)argv[0]);
2213 ++argv;
2214 argv_idx = -1;
2215
2216 switch (c)
2217 {
2218 case 'c': /* "-c {command}" execute command */
2219 case 'S': /* "-S {file}" execute Vim script */
2220 if (parmp->n_commands >= MAX_ARG_CMDS)
2221 mainerr(ME_EXTRA_CMD, NULL);
2222 if (c == 'S')
2223 {
2224 char *a;
2225
2226 if (argc < 1)
2227 /* "-S" without argument: use default session file
2228 * name. */
2229 a = SESSION_FILE;
2230 else if (argv[0][0] == '-')
2231 {
2232 /* "-S" followed by another option: use default
2233 * session file name. */
2234 a = SESSION_FILE;
2235 ++argc;
2236 --argv;
2237 }
2238 else
2239 a = argv[0];
2240 p = alloc((unsigned)(STRLEN(a) + 4));
2241 if (p == NULL)
2242 mch_exit(2);
2243 sprintf((char *)p, "so %s", a);
2244 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2245 parmp->commands[parmp->n_commands++] = p;
2246 }
2247 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002248 parmp->commands[parmp->n_commands++] =
2249 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002250 break;
2251
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002252 case '-':
2253 if (argv[-1][2] == 'c')
2254 {
2255 /* "--cmd {command}" execute command */
2256 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2257 mainerr(ME_EXTRA_CMD, NULL);
2258 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002259 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002260 }
2261 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002262 break;
2263
2264 /* case 'd': -d {device} is handled in mch_check_win() for the
2265 * Amiga */
2266
2267#ifdef FEAT_QUICKFIX
2268 case 'q': /* "-q {errorfile}" QuickFix mode */
2269 parmp->use_ef = (char_u *)argv[0];
2270 break;
2271#endif
2272
2273 case 'i': /* "-i {viminfo}" use for viminfo */
2274 use_viminfo = (char_u *)argv[0];
2275 break;
2276
2277 case 's': /* "-s {scriptin}" read from script file */
2278 if (scriptin[0] != NULL)
2279 {
2280scripterror:
2281 mch_errmsg(_("Attempt to open script file again: \""));
2282 mch_errmsg(argv[-1]);
2283 mch_errmsg(" ");
2284 mch_errmsg(argv[0]);
2285 mch_errmsg("\"\n");
2286 mch_exit(2);
2287 }
2288 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2289 {
2290 mch_errmsg(_("Cannot open for reading: \""));
2291 mch_errmsg(argv[0]);
2292 mch_errmsg("\"\n");
2293 mch_exit(2);
2294 }
2295 if (save_typebuf() == FAIL)
2296 mch_exit(2); /* out of memory */
2297 break;
2298
2299 case 't': /* "-t {tag}" */
2300 parmp->tagname = (char_u *)argv[0];
2301 break;
2302
2303 case 'T': /* "-T {terminal}" terminal name */
2304 /*
2305 * The -T term argument is always available and when
2306 * HAVE_TERMLIB is supported it overrides the environment
2307 * variable TERM.
2308 */
2309#ifdef FEAT_GUI
2310 if (term_is_gui((char_u *)argv[0]))
2311 gui.starting = TRUE; /* start GUI a bit later */
2312 else
2313#endif
2314 parmp->term = (char_u *)argv[0];
2315 break;
2316
2317 case 'u': /* "-u {vimrc}" vim inits file */
2318 parmp->use_vimrc = (char_u *)argv[0];
2319 break;
2320
2321 case 'U': /* "-U {gvimrc}" gvim inits file */
2322#ifdef FEAT_GUI
2323 use_gvimrc = (char_u *)argv[0];
2324#endif
2325 break;
2326
2327 case 'w': /* "-w {nr}" 'window' value */
2328 /* "-w {scriptout}" append to script file */
2329 if (vim_isdigit(*((char_u *)argv[0])))
2330 {
2331 argv_idx = 0;
2332 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2333 set_option_value((char_u *)"window", n, NULL, 0);
2334 argv_idx = -1;
2335 break;
2336 }
2337 /*FALLTHROUGH*/
2338 case 'W': /* "-W {scriptout}" overwrite script file */
2339 if (scriptout != NULL)
2340 goto scripterror;
2341 if ((scriptout = mch_fopen(argv[0],
2342 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2343 {
2344 mch_errmsg(_("Cannot open for script output: \""));
2345 mch_errmsg(argv[0]);
2346 mch_errmsg("\"\n");
2347 mch_exit(2);
2348 }
2349 break;
2350
2351#ifdef FEAT_GUI_W32
2352 case 'P': /* "-P {parent title}" MDI parent */
2353 gui_mch_set_parent(argv[0]);
2354 break;
2355#endif
2356 }
2357 }
2358 }
2359
2360 /*
2361 * File name argument.
2362 */
2363 else
2364 {
2365 argv_idx = -1; /* skip to next argument */
2366
2367 /* Check for only one type of editing. */
2368 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2369 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2370 parmp->edit_type = EDIT_FILE;
2371
2372#ifdef MSWIN
2373 /* Remember if the argument was a full path before changing
2374 * slashes to backslashes. */
2375 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2376 parmp->full_path = TRUE;
2377#endif
2378
2379 /* Add the file to the global argument list. */
2380 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2381 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2382 mch_exit(2);
2383#ifdef FEAT_DIFF
2384 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2385 && !mch_isdir(alist_name(&GARGLIST[0])))
2386 {
2387 char_u *r;
2388
2389 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2390 if (r != NULL)
2391 {
2392 vim_free(p);
2393 p = r;
2394 }
2395 }
2396#endif
2397#if defined(__CYGWIN32__) && !defined(WIN32)
2398 /*
2399 * If vim is invoked by non-Cygwin tools, convert away any
2400 * DOS paths, so things like .swp files are created correctly.
2401 * Look for evidence of non-Cygwin paths before we bother.
2402 * This is only for when using the Unix files.
2403 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002404 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002405 {
2406 char posix_path[PATH_MAX];
2407
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002408# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2409 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2410# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002411 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002412# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002413 vim_free(p);
2414 p = vim_strsave(posix_path);
2415 if (p == NULL)
2416 mch_exit(2);
2417 }
2418#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002419
2420#ifdef USE_FNAME_CASE
2421 /* Make the case of the file name match the actual file. */
2422 fname_case(p, 0);
2423#endif
2424
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002425 alist_add(&global_alist, p,
2426#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002427 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002428#else
2429 2 /* add buffer number now and use curbuf */
2430#endif
2431 );
2432
2433#if defined(FEAT_MBYTE) && defined(WIN32)
2434 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002435 /* Remember this argument has been added to the argument list.
2436 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002437 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002438# ifdef FEAT_DIFF
2439 parmp->diff_mode
2440# else
2441 FALSE
2442# endif
2443 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002444 }
2445#endif
2446 }
2447
2448 /*
2449 * If there are no more letters after the current "-", go to next
2450 * argument. argv_idx is set to -1 when the current argument is to be
2451 * skipped.
2452 */
2453 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2454 {
2455 --argc;
2456 ++argv;
2457 argv_idx = 1;
2458 }
2459 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002460
2461#ifdef FEAT_EVAL
2462 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2463 * one. */
2464 if (parmp->n_commands > 0)
2465 {
2466 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2467 if (p != NULL)
2468 {
2469 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2470 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2471 vim_free(p);
2472 }
2473 }
2474#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002475}
2476
2477/*
2478 * Print a warning if stdout is not a terminal.
2479 * When starting in Ex mode and commands come from a file, set Silent mode.
2480 */
2481 static void
2482check_tty(parmp)
2483 mparm_T *parmp;
2484{
2485 int input_isatty; /* is active input a terminal? */
2486
2487 input_isatty = mch_input_isatty();
2488 if (exmode_active)
2489 {
2490 if (!input_isatty)
2491 silent_mode = TRUE;
2492 }
2493 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2494#ifdef FEAT_GUI
2495 /* don't want the delay when started from the desktop */
2496 && !gui.starting
2497#endif
2498 )
2499 {
2500#ifdef NBDEBUG
2501 /*
2502 * This shouldn't be necessary. But if I run netbeans with the log
2503 * output coming to the console and XOpenDisplay fails, I get vim
2504 * trying to start with input/output to my console tty. This fills my
2505 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002506 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002507 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002508 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002509 {
2510 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2511 exit(1);
2512 }
2513#endif
2514 if (!parmp->stdout_isatty)
2515 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2516 if (!input_isatty)
2517 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2518 out_flush();
2519 if (scriptin[0] == NULL)
2520 ui_delay(2000L, TRUE);
2521 TIME_MSG("Warning delay");
2522 }
2523}
2524
2525/*
2526 * Read text from stdin.
2527 */
2528 static void
2529read_stdin()
2530{
2531 int i;
2532
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002533#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002534 /* When getting the ATTENTION prompt here, use a dialog */
2535 swap_exists_action = SEA_DIALOG;
2536#endif
2537 no_wait_return = TRUE;
2538 i = msg_didany;
2539 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002540 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 no_wait_return = FALSE;
2542 msg_didany = i;
2543 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002544#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002545 check_swap_exists_action();
2546#endif
2547#if !(defined(AMIGA) || defined(MACOS))
2548 /*
2549 * Close stdin and dup it from stderr. Required for GPM to work
2550 * properly, and for running external commands.
2551 * Is there any other system that cannot do this?
2552 */
2553 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002554 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002555#endif
2556}
2557
2558/*
2559 * Create the requested number of windows and edit buffers in them.
2560 * Also does recovery if "recoverymode" set.
2561 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562 static void
2563create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002564 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002565{
2566#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002567 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002568 int done = 0;
2569
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002570 /*
2571 * Create the number of windows that was requested.
2572 */
2573 if (parmp->window_count == -1) /* was not set */
2574 parmp->window_count = 1;
2575 if (parmp->window_count == 0)
2576 parmp->window_count = GARGCOUNT;
2577 if (parmp->window_count > 1)
2578 {
2579 /* Don't change the windows if there was a command in .vimrc that
2580 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002581 if (parmp->window_layout == 0)
2582 parmp->window_layout = WIN_HOR;
2583 if (parmp->window_layout == WIN_TABS)
2584 {
2585 parmp->window_count = make_tabpages(parmp->window_count);
2586 TIME_MSG("making tab pages");
2587 }
2588 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002589 {
2590 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002591 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002592 TIME_MSG("making windows");
2593 }
2594 else
2595 parmp->window_count = win_count();
2596 }
2597 else
2598 parmp->window_count = 1;
2599#endif
2600
2601 if (recoverymode) /* do recover */
2602 {
2603 msg_scroll = TRUE; /* scroll message up */
2604 ml_recover();
2605 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2606 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002607 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002608 }
2609 else
2610 {
2611 /*
2612 * Open a buffer for windows that don't have one yet.
2613 * Commands in the .vimrc might have loaded a file or split the window.
2614 * Watch out for autocommands that delete a window.
2615 */
2616#ifdef FEAT_AUTOCMD
2617 /*
2618 * Don't execute Win/Buf Enter/Leave autocommands here
2619 */
2620 ++autocmd_no_enter;
2621 ++autocmd_no_leave;
2622#endif
2623#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002624 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002625 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002626 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002627 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002628 {
2629 if (parmp->window_layout == WIN_TABS)
2630 goto_tabpage(1);
2631 else
2632 curwin = firstwin;
2633 }
2634 else if (parmp->window_layout == WIN_TABS)
2635 {
2636 if (curtab->tp_next == NULL)
2637 break;
2638 goto_tabpage(0);
2639 }
2640 else
2641 {
2642 if (curwin->w_next == NULL)
2643 break;
2644 curwin = curwin->w_next;
2645 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002646 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002647#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002648 curbuf = curwin->w_buffer;
2649 if (curbuf->b_ml.ml_mfp == NULL)
2650 {
2651#ifdef FEAT_FOLDING
2652 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2653 if (p_fdls >= 0)
2654 curwin->w_p_fdl = p_fdls;
2655#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002656#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002657 /* When getting the ATTENTION prompt here, use a dialog */
2658 swap_exists_action = SEA_DIALOG;
2659#endif
2660 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002661
2662 /* create memfile, read file */
2663 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002665#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002666 if (swap_exists_action == SEA_QUIT)
2667 {
2668 if (got_int || only_one_window())
2669 {
2670 /* abort selected or quit and only one window */
2671 did_emsg = FALSE; /* avoid hit-enter prompt */
2672 getout(1);
2673 }
2674 /* We can't close the window, it would disturb what
2675 * happens next. Clear the file name and set the arg
2676 * index to -1 to delete it later. */
2677 setfname(curbuf, NULL, NULL, FALSE);
2678 curwin->w_arg_idx = -1;
2679 swap_exists_action = SEA_NONE;
2680 }
2681 else
2682 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002683#endif
2684#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002685 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002686#endif
2687 }
2688#ifdef FEAT_WINDOWS
2689 ui_breakcheck();
2690 if (got_int)
2691 {
2692 (void)vgetc(); /* only break the file loading, not the rest */
2693 break;
2694 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002695 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002696#endif
2697#ifdef FEAT_WINDOWS
2698 if (parmp->window_layout == WIN_TABS)
2699 goto_tabpage(1);
2700 else
2701 curwin = firstwin;
2702 curbuf = curwin->w_buffer;
2703#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002704#ifdef FEAT_AUTOCMD
2705 --autocmd_no_enter;
2706 --autocmd_no_leave;
2707#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002708 }
2709}
2710
2711#ifdef FEAT_WINDOWS
2712 /*
2713 * If opened more than one window, start editing files in the other
2714 * windows. make_windows() has already opened the windows.
2715 */
2716 static void
2717edit_buffers(parmp)
2718 mparm_T *parmp;
2719{
2720 int arg_idx; /* index in argument list */
2721 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002722 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002723
2724# ifdef FEAT_AUTOCMD
2725 /*
2726 * Don't execute Win/Buf Enter/Leave autocommands here
2727 */
2728 ++autocmd_no_enter;
2729 ++autocmd_no_leave;
2730# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002731
2732 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2733 if (curwin->w_arg_idx == -1)
2734 {
2735 win_close(curwin, TRUE);
2736 advance = FALSE;
2737 }
2738
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002739 arg_idx = 1;
2740 for (i = 1; i < parmp->window_count; ++i)
2741 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002742 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2743 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002744 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002745 ++arg_idx;
2746 win_close(curwin, TRUE);
2747 advance = FALSE;
2748 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002749 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002750
Bram Moolenaar84212822006-11-07 21:59:47 +00002751 if (advance)
2752 {
2753 if (parmp->window_layout == WIN_TABS)
2754 {
2755 if (curtab->tp_next == NULL) /* just checking */
2756 break;
2757 goto_tabpage(0);
2758 }
2759 else
2760 {
2761 if (curwin->w_next == NULL) /* just checking */
2762 break;
2763 win_enter(curwin->w_next, FALSE);
2764 }
2765 }
2766 advance = TRUE;
2767
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002768 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002769 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002770 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2771 {
2772 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002773 /* Edit file from arg list, if there is one. When "Quit" selected
2774 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002775# ifdef HAS_SWAP_EXISTS_ACTION
2776 swap_exists_did_quit = FALSE;
2777# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002778 (void)do_ecmd(0, arg_idx < GARGCOUNT
2779 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002780 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002781# ifdef HAS_SWAP_EXISTS_ACTION
2782 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002783 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002784 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002785 if (got_int || only_one_window())
2786 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002787 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002788 did_emsg = FALSE; /* avoid hit-enter prompt */
2789 getout(1);
2790 }
2791 win_close(curwin, TRUE);
2792 advance = FALSE;
2793 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002794# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002795 if (arg_idx == GARGCOUNT - 1)
2796 arg_had_last = TRUE;
2797 ++arg_idx;
2798 }
2799 ui_breakcheck();
2800 if (got_int)
2801 {
2802 (void)vgetc(); /* only break the file loading, not the rest */
2803 break;
2804 }
2805 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002806
2807 if (parmp->window_layout == WIN_TABS)
2808 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002809# ifdef FEAT_AUTOCMD
2810 --autocmd_no_enter;
2811# endif
2812 win_enter(firstwin, FALSE); /* back to first window */
2813# ifdef FEAT_AUTOCMD
2814 --autocmd_no_leave;
2815# endif
2816 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002817 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002818 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2819}
2820#endif /* FEAT_WINDOWS */
2821
2822/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002823 * Execute the commands from --cmd arguments "cmds[cnt]".
2824 */
2825 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002826exe_pre_commands(parmp)
2827 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002828{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002829 char_u **cmds = parmp->pre_commands;
2830 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002831 int i;
2832
2833 if (cnt > 0)
2834 {
2835 curwin->w_cursor.lnum = 0; /* just in case.. */
2836 sourcing_name = (char_u *)_("pre-vimrc command line");
2837# ifdef FEAT_EVAL
2838 current_SID = SID_CMDARG;
2839# endif
2840 for (i = 0; i < cnt; ++i)
2841 do_cmdline_cmd(cmds[i]);
2842 sourcing_name = NULL;
2843# ifdef FEAT_EVAL
2844 current_SID = 0;
2845# endif
2846 TIME_MSG("--cmd commands");
2847 }
2848}
2849
2850/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002851 * Execute "+", "-c" and "-S" arguments.
2852 */
2853 static void
2854exe_commands(parmp)
2855 mparm_T *parmp;
2856{
2857 int i;
2858
2859 /*
2860 * We start commands on line 0, make "vim +/pat file" match a
2861 * pattern on line 1. But don't move the cursor when an autocommand
2862 * with g`" was used.
2863 */
2864 msg_scroll = TRUE;
2865 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2866 curwin->w_cursor.lnum = 0;
2867 sourcing_name = (char_u *)"command line";
2868#ifdef FEAT_EVAL
2869 current_SID = SID_CARG;
2870#endif
2871 for (i = 0; i < parmp->n_commands; ++i)
2872 {
2873 do_cmdline_cmd(parmp->commands[i]);
2874 if (parmp->cmds_tofree[i])
2875 vim_free(parmp->commands[i]);
2876 }
2877 sourcing_name = NULL;
2878#ifdef FEAT_EVAL
2879 current_SID = 0;
2880#endif
2881 if (curwin->w_cursor.lnum == 0)
2882 curwin->w_cursor.lnum = 1;
2883
2884 if (!exmode_active)
2885 msg_scroll = FALSE;
2886
2887#ifdef FEAT_QUICKFIX
2888 /* When started with "-q errorfile" jump to first error again. */
2889 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002890 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002891#endif
2892 TIME_MSG("executing command arguments");
2893}
2894
2895/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002896 * Source startup scripts.
2897 */
2898 static void
2899source_startup_scripts(parmp)
2900 mparm_T *parmp;
2901{
2902 int i;
2903
2904 /*
2905 * For "evim" source evim.vim first of all, so that the user can overrule
2906 * any things he doesn't like.
2907 */
2908 if (parmp->evim_mode)
2909 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002910 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002911 TIME_MSG("source evim file");
2912 }
2913
2914 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002915 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002916 * nothing else.
2917 */
2918 if (parmp->use_vimrc != NULL)
2919 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002920 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2921 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002922 {
2923#ifdef FEAT_GUI
2924 if (use_gvimrc == NULL) /* don't load gvimrc either */
2925 use_gvimrc = parmp->use_vimrc;
2926#endif
2927 if (parmp->use_vimrc[2] == 'N')
2928 p_lpl = FALSE; /* don't load plugins either */
2929 }
2930 else
2931 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002932 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002933 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2934 }
2935 }
2936 else if (!silent_mode)
2937 {
2938#ifdef AMIGA
2939 struct Process *proc = (struct Process *)FindTask(0L);
2940 APTR save_winptr = proc->pr_WindowPtr;
2941
2942 /* Avoid a requester here for a volume that doesn't exist. */
2943 proc->pr_WindowPtr = (APTR)-1L;
2944#endif
2945
2946 /*
2947 * Get system wide defaults, if the file name is defined.
2948 */
2949#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002950 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002951#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002952#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002953 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002954#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002955
2956 /*
2957 * Try to read initialization commands from the following places:
2958 * - environment variable VIMINIT
2959 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2960 * - second user vimrc file ($VIM/.vimrc for Dos)
2961 * - environment variable EXINIT
2962 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2963 * - second user exrc file ($VIM/.exrc for Dos)
2964 * The first that exists is used, the rest is ignored.
2965 */
2966 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2967 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002968 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002969#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002970 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2971 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002972#endif
2973#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002974 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2975 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002976#endif
2977 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002978 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002979 {
2980#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002981 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002982#endif
2983 }
2984 }
2985
2986 /*
2987 * Read initialization commands from ".vimrc" or ".exrc" in current
2988 * directory. This is only done if the 'exrc' option is set.
2989 * Because of security reasons we disallow shell and write commands
2990 * now, except for unix if the file is owned by the user or 'secure'
2991 * option has been reset in environment of global ".exrc" or ".vimrc".
2992 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2993 * SYS_VIMRC_FILE.
2994 */
2995 if (p_exrc)
2996 {
2997#if defined(UNIX) || defined(VMS)
2998 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2999 if (!file_owned(VIMRC_FILE))
3000#endif
3001 secure = p_secure;
3002
3003 i = FAIL;
3004 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3005 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3006#ifdef USR_VIMRC_FILE2
3007 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3008 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3009#endif
3010#ifdef USR_VIMRC_FILE3
3011 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3012 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3013#endif
3014#ifdef SYS_VIMRC_FILE
3015 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3016 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3017#endif
3018 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003019 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003020
3021 if (i == FAIL)
3022 {
3023#if defined(UNIX) || defined(VMS)
3024 /* if ".exrc" is not owned by user set 'secure' mode */
3025 if (!file_owned(EXRC_FILE))
3026 secure = p_secure;
3027 else
3028 secure = 0;
3029#endif
3030 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3031 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3032#ifdef USR_EXRC_FILE2
3033 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3034 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3035#endif
3036 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003037 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003038 }
3039 }
3040 if (secure == 2)
3041 need_wait_return = TRUE;
3042 secure = 0;
3043#ifdef AMIGA
3044 proc->pr_WindowPtr = save_winptr;
3045#endif
3046 }
3047 TIME_MSG("sourcing vimrc file(s)");
3048}
3049
3050/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003051 * Setup to start using the GUI. Exit with an error when not available.
3052 */
3053 static void
3054main_start_gui()
3055{
3056#ifdef FEAT_GUI
3057 gui.starting = TRUE; /* start GUI a bit later */
3058#else
3059 mch_errmsg(_(e_nogvim));
3060 mch_errmsg("\n");
3061 mch_exit(2);
3062#endif
3063}
3064
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003065#endif /* NO_VIM_MAIN */
3066
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003067/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003068 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003069 * Returns FAIL if the environment variable was not executed, OK otherwise.
3070 */
3071 int
3072process_env(env, is_viminit)
3073 char_u *env;
3074 int is_viminit; /* when TRUE, called for VIMINIT */
3075{
3076 char_u *initstr;
3077 char_u *save_sourcing_name;
3078 linenr_T save_sourcing_lnum;
3079#ifdef FEAT_EVAL
3080 scid_T save_sid;
3081#endif
3082
3083 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3084 {
3085 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003086 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003087 save_sourcing_name = sourcing_name;
3088 save_sourcing_lnum = sourcing_lnum;
3089 sourcing_name = env;
3090 sourcing_lnum = 0;
3091#ifdef FEAT_EVAL
3092 save_sid = current_SID;
3093 current_SID = SID_ENV;
3094#endif
3095 do_cmdline_cmd(initstr);
3096 sourcing_name = save_sourcing_name;
3097 sourcing_lnum = save_sourcing_lnum;
3098#ifdef FEAT_EVAL
3099 current_SID = save_sid;;
3100#endif
3101 return OK;
3102 }
3103 return FAIL;
3104}
3105
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003106#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003107/*
3108 * Return TRUE if we are certain the user owns the file "fname".
3109 * Used for ".vimrc" and ".exrc".
3110 * Use both stat() and lstat() for extra security.
3111 */
3112 static int
3113file_owned(fname)
3114 char *fname;
3115{
3116 struct stat s;
3117# ifdef UNIX
3118 uid_t uid = getuid();
3119# else /* VMS */
3120 uid_t uid = ((getgid() << 16) | getuid());
3121# endif
3122
3123 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3124# ifdef HAVE_LSTAT
3125 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3126# endif
3127 );
3128}
3129#endif
3130
3131/*
3132 * Give an error message main_errors["n"] and exit.
3133 */
3134 static void
3135mainerr(n, str)
3136 int n; /* one of the ME_ defines */
3137 char_u *str; /* extra argument or NULL */
3138{
3139#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3140 reset_signals(); /* kill us with CTRL-C here, if you like */
3141#endif
3142
3143 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003144 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003145 mch_errmsg(_(main_errors[n]));
3146 if (str != NULL)
3147 {
3148 mch_errmsg(": \"");
3149 mch_errmsg((char *)str);
3150 mch_errmsg("\"");
3151 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003152 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003153
3154 mch_exit(1);
3155}
3156
3157 void
3158mainerr_arg_missing(str)
3159 char_u *str;
3160{
3161 mainerr(ME_ARG_MISSING, str);
3162}
3163
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003164#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003165/*
3166 * print a message with three spaces prepended and '\n' appended.
3167 */
3168 static void
3169main_msg(s)
3170 char *s;
3171{
3172 mch_msg(" ");
3173 mch_msg(s);
3174 mch_msg("\n");
3175}
3176
3177/*
3178 * Print messages for "vim -h" or "vim --help" and exit.
3179 */
3180 static void
3181usage()
3182{
3183 int i;
3184 static char *(use[]) =
3185 {
3186 N_("[file ..] edit specified file(s)"),
3187 N_("- read text from stdin"),
3188 N_("-t tag edit file where tag is defined"),
3189#ifdef FEAT_QUICKFIX
3190 N_("-q [errorfile] edit file with first error")
3191#endif
3192 };
3193
3194#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3195 reset_signals(); /* kill us with CTRL-C here, if you like */
3196#endif
3197
3198 mch_msg(longVersion);
3199 mch_msg(_("\n\nusage:"));
3200 for (i = 0; ; ++i)
3201 {
3202 mch_msg(_(" vim [arguments] "));
3203 mch_msg(_(use[i]));
3204 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3205 break;
3206 mch_msg(_("\n or:"));
3207 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003208#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003209 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003210#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003211
3212 mch_msg(_("\n\nArguments:\n"));
3213 main_msg(_("--\t\t\tOnly file names after this"));
3214#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3215 main_msg(_("--literal\t\tDon't expand wildcards"));
3216#endif
3217#ifdef FEAT_OLE
3218 main_msg(_("-register\t\tRegister this gvim for OLE"));
3219 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3220#endif
3221#ifdef FEAT_GUI
3222 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3223 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3224#endif
3225 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3226 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003227 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003228 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3229#ifdef FEAT_DIFF
3230 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3231#endif
3232 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3233 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3234 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3235 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3236 main_msg(_("-M\t\t\tModifications in text not allowed"));
3237 main_msg(_("-b\t\t\tBinary mode"));
3238#ifdef FEAT_LISP
3239 main_msg(_("-l\t\t\tLisp mode"));
3240#endif
3241 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3242 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003243 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3244#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003245 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003246#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003247 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3248 main_msg(_("-r\t\t\tList swap files and exit"));
3249 main_msg(_("-r (with file name)\tRecover crashed session"));
3250 main_msg(_("-L\t\t\tSame as -r"));
3251#ifdef AMIGA
3252 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3253 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3254#endif
3255#ifdef FEAT_ARABIC
3256 main_msg(_("-A\t\t\tstart in Arabic mode"));
3257#endif
3258#ifdef FEAT_RIGHTLEFT
3259 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3260#endif
3261#ifdef FEAT_FKMAP
3262 main_msg(_("-F\t\t\tStart in Farsi mode"));
3263#endif
3264 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3265 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3266#ifdef FEAT_GUI
3267 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3268#endif
3269 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003270#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003271 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003272 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3273 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003274#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003275 main_msg(_("+\t\t\tStart at end of file"));
3276 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003277 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003278 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3279 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3280 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3281 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3282 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3283#ifdef FEAT_CRYPT
3284 main_msg(_("-x\t\t\tEdit encrypted files"));
3285#endif
3286#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3287# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3288 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3289# endif
3290 main_msg(_("-X\t\t\tDo not connect to X server"));
3291#endif
3292#ifdef FEAT_CLIENTSERVER
3293 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3294 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3295 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3296 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003297# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003298 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003299# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003300 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3301 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3302 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3303 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3304#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003305#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003306 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003307#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003308#ifdef FEAT_VIMINFO
3309 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3310#endif
3311 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3312 main_msg(_("--version\t\tPrint version information and exit"));
3313
3314#ifdef FEAT_GUI_X11
3315# ifdef FEAT_GUI_MOTIF
3316 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3317# else
3318# ifdef FEAT_GUI_ATHENA
3319# ifdef FEAT_GUI_NEXTAW
3320 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3321# else
3322 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3323# endif
3324# endif
3325# endif
3326 main_msg(_("-display <display>\tRun vim on <display>"));
3327 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003328 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3329 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3330 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3331 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3332 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3333 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3334 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3335 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3336# ifdef FEAT_GUI_ATHENA
3337 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3338# endif
3339 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3340 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3341 main_msg(_("-xrm <resource>\tSet the specified resource"));
3342#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003343#ifdef FEAT_GUI_GTK
3344 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3345 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3346 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3347 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3348 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003351 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003353#ifdef FEAT_GUI_W32
3354 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003355 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003356#endif
3357
3358#ifdef FEAT_GUI_GNOME
3359 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3360 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003361 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003362 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003363 gui.dofork = FALSE;
3364 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003365 else
3366#endif
3367 mch_exit(0);
3368}
3369
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003370#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003371/*
3372 * Check the result of the ATTENTION dialog:
3373 * When "Quit" selected, exit Vim.
3374 * When "Recover" selected, recover the file.
3375 */
3376 static void
3377check_swap_exists_action()
3378{
3379 if (swap_exists_action == SEA_QUIT)
3380 getout(1);
3381 handle_swap_exists(NULL);
3382}
3383#endif
3384
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003385#endif
3386
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003387#if defined(STARTUPTIME) || defined(PROTO)
3388static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3389
3390static struct timeval prev_timeval;
3391
Bram Moolenaar3f269672009-11-03 11:11:11 +00003392# ifdef WIN3264
3393/*
3394 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3395 */
3396 static int
3397gettimeofday(struct timeval *tv, char *dummy)
3398{
3399 long t = clock();
3400 tv->tv_sec = t / CLOCKS_PER_SEC;
3401 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3402 return 0;
3403}
3404# endif
3405
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003406/*
3407 * Save the previous time before doing something that could nest.
3408 * set "*tv_rel" to the time elapsed so far.
3409 */
3410 void
3411time_push(tv_rel, tv_start)
3412 void *tv_rel, *tv_start;
3413{
3414 *((struct timeval *)tv_rel) = prev_timeval;
3415 gettimeofday(&prev_timeval, NULL);
3416 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3417 - ((struct timeval *)tv_rel)->tv_usec;
3418 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3419 - ((struct timeval *)tv_rel)->tv_sec;
3420 if (((struct timeval *)tv_rel)->tv_usec < 0)
3421 {
3422 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3423 --((struct timeval *)tv_rel)->tv_sec;
3424 }
3425 *(struct timeval *)tv_start = prev_timeval;
3426}
3427
3428/*
3429 * Compute the previous time after doing something that could nest.
3430 * Subtract "*tp" from prev_timeval;
3431 * Note: The arguments are (void *) to avoid trouble with systems that don't
3432 * have struct timeval.
3433 */
3434 void
3435time_pop(tp)
3436 void *tp; /* actually (struct timeval *) */
3437{
3438 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3439 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3440 if (prev_timeval.tv_usec < 0)
3441 {
3442 prev_timeval.tv_usec += 1000000;
3443 --prev_timeval.tv_sec;
3444 }
3445}
3446
3447 static void
3448time_diff(then, now)
3449 struct timeval *then;
3450 struct timeval *now;
3451{
3452 long usec;
3453 long msec;
3454
3455 usec = now->tv_usec - then->tv_usec;
3456 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3457 usec = usec % 1000L;
3458 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3459}
3460
3461 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003462time_msg(mesg, tv_start)
3463 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003464 void *tv_start; /* only for do_source: start time; actually
3465 (struct timeval *) */
3466{
3467 static struct timeval start;
3468 struct timeval now;
3469
3470 if (time_fd != NULL)
3471 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003472 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003473 {
3474 gettimeofday(&start, NULL);
3475 prev_timeval = start;
3476 fprintf(time_fd, "\n\ntimes in msec\n");
3477 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3478 fprintf(time_fd, " clock elapsed: other lines\n\n");
3479 }
3480 gettimeofday(&now, NULL);
3481 time_diff(&start, &now);
3482 if (((struct timeval *)tv_start) != NULL)
3483 {
3484 fprintf(time_fd, " ");
3485 time_diff(((struct timeval *)tv_start), &now);
3486 }
3487 fprintf(time_fd, " ");
3488 time_diff(&prev_timeval, &now);
3489 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003490 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003491 }
3492}
3493
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003494#endif
3495
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003496#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003497
3498/*
3499 * Common code for the X command server and the Win32 command server.
3500 */
3501
Bram Moolenaareb94e552006-03-11 21:35:11 +00003502static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003503
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003504/*
3505 * Do the client-server stuff, unless "--servername ''" was used.
3506 */
3507 static void
3508exec_on_server(parmp)
3509 mparm_T *parmp;
3510{
3511 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3512 {
3513# ifdef WIN32
3514 /* Initialise the client/server messaging infrastructure. */
3515 serverInitMessaging();
3516# endif
3517
3518 /*
3519 * When a command server argument was found, execute it. This may
3520 * exit Vim when it was successful. Otherwise it's executed further
3521 * on. Remember the encoding used here in "serverStrEnc".
3522 */
3523 if (parmp->serverArg)
3524 {
3525 cmdsrv_main(&parmp->argc, parmp->argv,
3526 parmp->serverName_arg, &parmp->serverStr);
3527# ifdef FEAT_MBYTE
3528 parmp->serverStrEnc = vim_strsave(p_enc);
3529# endif
3530 }
3531
3532 /* If we're still running, get the name to register ourselves.
3533 * On Win32 can register right now, for X11 need to setup the
3534 * clipboard first, it's further down. */
3535 parmp->servername = serverMakeName(parmp->serverName_arg,
3536 parmp->argv[0]);
3537# ifdef WIN32
3538 if (parmp->servername != NULL)
3539 {
3540 serverSetName(parmp->servername);
3541 vim_free(parmp->servername);
3542 }
3543# endif
3544 }
3545}
3546
3547/*
3548 * Prepare for running as a Vim server.
3549 */
3550 static void
3551prepare_server(parmp)
3552 mparm_T *parmp;
3553{
3554# if defined(FEAT_X11)
3555 /*
3556 * Register for remote command execution with :serversend and --remote
3557 * unless there was a -X or a --servername '' on the command line.
3558 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003559 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003560 */
3561 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3562# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003563 (gui.in_use
3564# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003565 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003566# endif
3567 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003568# endif
3569 parmp->serverName_arg != NULL))
3570 {
3571 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3572 vim_free(parmp->servername);
3573 TIME_MSG("register server name");
3574 }
3575 else
3576 serverDelayedStartName = parmp->servername;
3577# endif
3578
3579 /*
3580 * Execute command ourselves if we're here because the send failed (or
3581 * else we would have exited above).
3582 */
3583 if (parmp->serverStr != NULL)
3584 {
3585 char_u *p;
3586
3587 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3588 parmp->serverStr, &p));
3589 vim_free(p);
3590 }
3591}
3592
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003593 static void
3594cmdsrv_main(argc, argv, serverName_arg, serverStr)
3595 int *argc;
3596 char **argv;
3597 char_u *serverName_arg;
3598 char_u **serverStr;
3599{
3600 char_u *res;
3601 int i;
3602 char_u *sname;
3603 int ret;
3604 int didone = FALSE;
3605 int exiterr = 0;
3606 char **newArgV = argv + 1;
3607 int newArgC = 1,
3608 Argc = *argc;
3609 int argtype;
3610#define ARGTYPE_OTHER 0
3611#define ARGTYPE_EDIT 1
3612#define ARGTYPE_EDIT_WAIT 2
3613#define ARGTYPE_SEND 3
3614 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003615 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003616# ifndef FEAT_X11
3617 HWND srv;
3618# else
3619 Window srv;
3620
3621 setup_term_clip();
3622# endif
3623
3624 sname = serverMakeName(serverName_arg, argv[0]);
3625 if (sname == NULL)
3626 return;
3627
3628 /*
3629 * Execute the command server related arguments and remove them
3630 * from the argc/argv array; We may have to return into main()
3631 */
3632 for (i = 1; i < Argc; i++)
3633 {
3634 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003635 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003636 {
3637 for (; i < *argc; i++)
3638 {
3639 *newArgV++ = argv[i];
3640 newArgC++;
3641 }
3642 break;
3643 }
3644
Bram Moolenaareb94e552006-03-11 21:35:11 +00003645 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003646 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003647 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3648 {
3649 char *p = argv[i] + 8;
3650
3651 argtype = ARGTYPE_EDIT;
3652 while (*p != NUL)
3653 {
3654 if (STRNICMP(p, "-wait", 5) == 0)
3655 {
3656 argtype = ARGTYPE_EDIT_WAIT;
3657 p += 5;
3658 }
3659 else if (STRNICMP(p, "-silent", 7) == 0)
3660 {
3661 silent = TRUE;
3662 p += 7;
3663 }
3664 else if (STRNICMP(p, "-tab", 4) == 0)
3665 {
3666 tabs = TRUE;
3667 p += 4;
3668 }
3669 else
3670 {
3671 argtype = ARGTYPE_OTHER;
3672 break;
3673 }
3674 }
3675 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003676 else
3677 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003678
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003679 if (argtype != ARGTYPE_OTHER)
3680 {
3681 if (i == *argc - 1)
3682 mainerr_arg_missing((char_u *)argv[i]);
3683 if (argtype == ARGTYPE_SEND)
3684 {
3685 *serverStr = (char_u *)argv[i + 1];
3686 i++;
3687 }
3688 else
3689 {
3690 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003691 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003692 if (*serverStr == NULL)
3693 {
3694 /* Probably out of memory, exit. */
3695 didone = TRUE;
3696 exiterr = 1;
3697 break;
3698 }
3699 Argc = i;
3700 }
3701# ifdef FEAT_X11
3702 if (xterm_dpy == NULL)
3703 {
3704 mch_errmsg(_("No display"));
3705 ret = -1;
3706 }
3707 else
3708 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3709 NULL, &srv, 0, 0, silent);
3710# else
3711 /* Win32 always works? */
3712 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3713# endif
3714 if (ret < 0)
3715 {
3716 if (argtype == ARGTYPE_SEND)
3717 {
3718 /* Failed to send, abort. */
3719 mch_errmsg(_(": Send failed.\n"));
3720 didone = TRUE;
3721 exiterr = 1;
3722 }
3723 else if (!silent)
3724 /* Let vim start normally. */
3725 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3726 break;
3727 }
3728
3729# ifdef FEAT_GUI_W32
3730 /* Guess that when the server name starts with "g" it's a GUI
3731 * server, which we can bring to the foreground here.
3732 * Foreground() in the server doesn't work very well. */
3733 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3734 SetForegroundWindow(srv);
3735# endif
3736
3737 /*
3738 * For --remote-wait: Wait until the server did edit each
3739 * file. Also detect that the server no longer runs.
3740 */
3741 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3742 {
3743 int numFiles = *argc - i - 1;
3744 int j;
3745 char_u *done = alloc(numFiles);
3746 char_u *p;
3747# ifdef FEAT_GUI_W32
3748 NOTIFYICONDATA ni;
3749 int count = 0;
3750 extern HWND message_window;
3751# endif
3752
3753 if (numFiles > 0 && argv[i + 1][0] == '+')
3754 /* Skip "+cmd" argument, don't wait for it to be edited. */
3755 --numFiles;
3756
3757# ifdef FEAT_GUI_W32
3758 ni.cbSize = sizeof(ni);
3759 ni.hWnd = message_window;
3760 ni.uID = 0;
3761 ni.uFlags = NIF_ICON|NIF_TIP;
3762 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3763 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3764 Shell_NotifyIcon(NIM_ADD, &ni);
3765# endif
3766
3767 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003768 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003769 while (memchr(done, 0, numFiles) != NULL)
3770 {
3771# ifdef WIN32
3772 p = serverGetReply(srv, NULL, TRUE, TRUE);
3773 if (p == NULL)
3774 break;
3775# else
3776 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3777 break;
3778# endif
3779 j = atoi((char *)p);
3780 if (j >= 0 && j < numFiles)
3781 {
3782# ifdef FEAT_GUI_W32
3783 ++count;
3784 sprintf(ni.szTip, _("%d of %d edited"),
3785 count, numFiles);
3786 Shell_NotifyIcon(NIM_MODIFY, &ni);
3787# endif
3788 done[j] = 1;
3789 }
3790 }
3791# ifdef FEAT_GUI_W32
3792 Shell_NotifyIcon(NIM_DELETE, &ni);
3793# endif
3794 }
3795 }
3796 else if (STRICMP(argv[i], "--remote-expr") == 0)
3797 {
3798 if (i == *argc - 1)
3799 mainerr_arg_missing((char_u *)argv[i]);
3800# ifdef WIN32
3801 /* Win32 always works? */
3802 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3803 &res, NULL, 1, FALSE) < 0)
3804# else
3805 if (xterm_dpy == NULL)
3806 mch_errmsg(_("No display: Send expression failed.\n"));
3807 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3808 &res, NULL, 1, 1, FALSE) < 0)
3809# endif
3810 {
3811 if (res != NULL && *res != NUL)
3812 {
3813 /* Output error from remote */
3814 mch_errmsg((char *)res);
3815 vim_free(res);
3816 res = NULL;
3817 }
3818 mch_errmsg(_(": Send expression failed.\n"));
3819 }
3820 }
3821 else if (STRICMP(argv[i], "--serverlist") == 0)
3822 {
3823# ifdef WIN32
3824 /* Win32 always works? */
3825 res = serverGetVimNames();
3826# else
3827 if (xterm_dpy != NULL)
3828 res = serverGetVimNames(xterm_dpy);
3829# endif
3830 if (called_emsg)
3831 mch_errmsg("\n");
3832 }
3833 else if (STRICMP(argv[i], "--servername") == 0)
3834 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003835 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003836 i++;
3837 continue;
3838 }
3839 else
3840 {
3841 *newArgV++ = argv[i];
3842 newArgC++;
3843 continue;
3844 }
3845 didone = TRUE;
3846 if (res != NULL && *res != NUL)
3847 {
3848 mch_msg((char *)res);
3849 if (res[STRLEN(res) - 1] != '\n')
3850 mch_msg("\n");
3851 }
3852 vim_free(res);
3853 }
3854
3855 if (didone)
3856 {
3857 display_errors(); /* display any collected messages */
3858 exit(exiterr); /* Mission accomplished - get out */
3859 }
3860
3861 /* Return back into main() */
3862 *argc = newArgC;
3863 vim_free(sname);
3864}
3865
3866/*
3867 * Build a ":drop" command to send to a Vim server.
3868 */
3869 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003870build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003871 int filec;
3872 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003873 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003874 int sendReply;
3875{
3876 garray_T ga;
3877 int i;
3878 char_u *inicmd = NULL;
3879 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003880 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003881
3882 if (filec > 0 && filev[0][0] == '+')
3883 {
3884 inicmd = (char_u *)filev[0] + 1;
3885 filev++;
3886 filec--;
3887 }
3888 /* Check if we have at least one argument. */
3889 if (filec <= 0)
3890 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003891
3892 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003893 cwd = alloc(MAXPATHL);
3894 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003895 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003896 if (mch_dirname(cwd, MAXPATHL) != OK)
3897 {
3898 vim_free(cwd);
3899 return NULL;
3900 }
3901 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003902#ifdef BACKSLASH_IN_FILENAME
3903 "", /* rem_backslash() will tell what chars to escape */
3904#else
3905 PATH_ESC_CHARS,
3906#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003907 '\\', TRUE);
3908 vim_free(cwd);
3909 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003910 return NULL;
3911 ga_init2(&ga, 1, 100);
3912 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3913 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003914 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003915
3916 /* Call inputsave() so that a prompt for an encryption key works. */
3917 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3918 if (tabs)
3919 ga_concat(&ga, (char_u *)"tab ");
3920 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003921 for (i = 0; i < filec; i++)
3922 {
3923 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003924 * do it again in the Vim server. On MS-Windows only escape
3925 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003926 p = vim_strsave_escaped((char_u *)filev[i],
3927#ifdef UNIX
3928 PATH_ESC_CHARS
3929#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003930 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003931#endif
3932 );
3933 if (p == NULL)
3934 {
3935 vim_free(ga.ga_data);
3936 return NULL;
3937 }
3938 ga_concat(&ga, (char_u *)" ");
3939 ga_concat(&ga, p);
3940 vim_free(p);
3941 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003942 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3943
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003944 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3945 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003946 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3947
3948 /* Switch back to the correct current directory (prior to temporary path
3949 * switch) unless 'autochdir' is set, in which case it will already be
3950 * correct after the :drop command. */
3951 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3952
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003953 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003954 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3955 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003956 if (inicmd != NULL)
3957 {
3958 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3959 * the following commands to be inserted as text. Use a "|",
3960 * hopefully "inicmd" does allow this... */
3961 ga_concat(&ga, inicmd);
3962 ga_concat(&ga, (char_u *)"|");
3963 }
3964 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3965 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003966 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003967 ga_append(&ga, NUL);
3968 return ga.ga_data;
3969}
3970
3971/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003972 * Make our basic server name: use the specified "arg" if given, otherwise use
3973 * the tail of the command "cmd" we were started with.
3974 * Return the name in allocated memory. This doesn't include a serial number.
3975 */
3976 static char_u *
3977serverMakeName(arg, cmd)
3978 char_u *arg;
3979 char *cmd;
3980{
3981 char_u *p;
3982
3983 if (arg != NULL && *arg != NUL)
3984 p = vim_strsave_up(arg);
3985 else
3986 {
3987 p = vim_strsave_up(gettail((char_u *)cmd));
3988 /* Remove .exe or .bat from the name. */
3989 if (p != NULL && vim_strchr(p, '.') != NULL)
3990 *vim_strchr(p, '.') = NUL;
3991 }
3992 return p;
3993}
3994#endif /* FEAT_CLIENTSERVER */
3995
3996#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3997/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003998 * Replace termcodes such as <CR> and insert as key presses if there is room.
3999 */
4000 void
4001server_to_input_buf(str)
4002 char_u *str;
4003{
4004 char_u *ptr = NULL;
4005 char_u *cpo_save = p_cpo;
4006
4007 /* Set 'cpoptions' the way we want it.
4008 * B set - backslashes are *not* treated specially
4009 * k set - keycodes are *not* reverse-engineered
4010 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004011 * The last but one parameter of replace_termcodes() is TRUE so that the
4012 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004013 */
4014 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004015 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004016 p_cpo = cpo_save;
4017
4018 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4019 {
4020 /*
4021 * Add the string to the input stream.
4022 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4023 *
4024 * First clear typed characters from the typeahead buffer, there could
4025 * be half a mapping there. Then append to the existing string, so
4026 * that multiple commands from a client are concatenated.
4027 */
4028 if (typebuf.tb_maplen < typebuf.tb_len)
4029 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4030 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4031
4032 /* Let input_available() know we inserted text in the typeahead
4033 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004034 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004035 }
4036 vim_free((char_u *)ptr);
4037}
4038
4039/*
4040 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004041 */
4042 char_u *
4043eval_client_expr_to_string(expr)
4044 char_u *expr;
4045{
4046 char_u *res;
4047 int save_dbl = debug_break_level;
4048 int save_ro = redir_off;
4049
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004050 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4051 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004052 debug_break_level = -1;
4053 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004054 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4055 * to be typed. Do generate errors so that try/catch works. */
4056 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004057
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004058 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004059
4060 debug_break_level = save_dbl;
4061 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004062 --emsg_silent;
4063 if (emsg_silent < 0)
4064 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004065
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004066 /* A client can tell us to redraw, but not to display the cursor, so do
4067 * that here. */
4068 setcursor();
4069 out_flush();
4070#ifdef FEAT_GUI
4071 if (gui.in_use)
4072 gui_update_cursor(FALSE, FALSE);
4073#endif
4074
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004075 return res;
4076}
4077
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004078/*
4079 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4080 * return an allocated string. Otherwise return "data".
4081 * "*tofree" is set to the result when it needs to be freed later.
4082 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004083 char_u *
4084serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004085 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004086 char_u *data;
4087 char_u **tofree;
4088{
4089 char_u *res = data;
4090
4091 *tofree = NULL;
4092# ifdef FEAT_MBYTE
4093 if (client_enc != NULL && p_enc != NULL)
4094 {
4095 vimconv_T vimconv;
4096
4097 vimconv.vc_type = CONV_NONE;
4098 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4099 && vimconv.vc_type != CONV_NONE)
4100 {
4101 res = string_convert(&vimconv, data, NULL);
4102 if (res == NULL)
4103 res = data;
4104 else
4105 *tofree = res;
4106 }
4107 convert_setup(&vimconv, NULL, NULL);
4108 }
4109# endif
4110 return res;
4111}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004112#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004113
4114/*
4115 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4116 */
4117#if defined(FEAT_FKMAP) || defined(PROTO)
4118# include "farsi.c"
4119#endif
4120
4121/*
4122 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4123 */
4124#if defined(FEAT_ARABIC) || defined(PROTO)
4125# include "arabic.c"
4126#endif