blob: ef4893458380d86928c87798fffd5fd12074eabf [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 Moolenaar33aec762006-01-22 23:30:12 +00001171#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1172 /* Scroll-binding for diff mode may have been postponed until
1173 * here. Avoids doing it for every change. */
1174 if (diff_need_scrollbind)
1175 {
1176 check_scrollbind((linenr_T)0, 0L);
1177 diff_need_scrollbind = FALSE;
1178 }
1179#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001180#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1181 /* Include a closed fold completely in the Visual area. */
1182 foldAdjustVisual();
1183#endif
1184#ifdef FEAT_FOLDING
1185 /*
1186 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1187 * contain the cursor.
1188 * When 'foldopen' is "all", open the fold(s) under the cursor.
1189 * This may mark the window for redrawing.
1190 */
1191 if (hasAnyFolding(curwin) && !char_avail())
1192 {
1193 foldCheckClose();
1194 if (fdo_flags & FDO_ALL)
1195 foldOpenCursor();
1196 }
1197#endif
1198
1199 /*
1200 * Before redrawing, make sure w_topline is correct, and w_leftcol
1201 * if lines don't wrap, and w_skipcol if lines wrap.
1202 */
1203 update_topline();
1204 validate_cursor();
1205
1206#ifdef FEAT_VISUAL
1207 if (VIsual_active)
1208 update_curbuf(INVERTED);/* update inverted part */
1209 else
1210#endif
1211 if (must_redraw)
1212 update_screen(0);
1213 else if (redraw_cmdline || clear_cmdline)
1214 showmode();
1215#ifdef FEAT_WINDOWS
1216 redraw_statuslines();
1217#endif
1218#ifdef FEAT_TITLE
1219 if (need_maketitle)
1220 maketitle();
1221#endif
1222 /* display message after redraw */
1223 if (keep_msg != NULL)
1224 {
1225 char_u *p;
1226
1227 /* msg_attr_keep() will set keep_msg to NULL, must free the
1228 * string here. */
1229 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001230 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001231 msg_attr(p, keep_msg_attr);
1232 vim_free(p);
1233 }
1234 if (need_fileinfo) /* show file info after redraw */
1235 {
1236 fileinfo(FALSE, TRUE, FALSE);
1237 need_fileinfo = FALSE;
1238 }
1239
1240 emsg_on_display = FALSE; /* can delete error message now */
1241 did_emsg = FALSE;
1242 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001243 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001244 showruler(FALSE);
1245
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001246# if defined(FEAT_CONCEAL)
1247 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001248 && (conceal_old_cursor_line != conceal_new_cursor_line
1249 || conceal_cursor_line(curwin)
1250 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001251 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001252 if (conceal_old_cursor_line != conceal_new_cursor_line
1253 && conceal_old_cursor_line
1254 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001255 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001256 update_single_line(curwin, conceal_new_cursor_line);
1257 curwin->w_valid &= ~VALID_CROW;
1258 }
1259# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001260 setcursor();
1261 cursor_on();
1262
1263 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001264
1265#ifdef STARTUPTIME
1266 /* Now that we have drawn the first screen all the startup stuff
1267 * has been done, close any file for startup messages. */
1268 if (time_fd != NULL)
1269 {
1270 TIME_MSG("first screen update");
1271 TIME_MSG("--- VIM STARTED ---");
1272 fclose(time_fd);
1273 time_fd = NULL;
1274 }
1275#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001276 }
1277#ifdef FEAT_GUI
1278 if (need_mouse_correct)
1279 gui_mouse_correct();
1280#endif
1281
1282 /*
1283 * Update w_curswant if w_set_curswant has been set.
1284 * Postponed until here to avoid computing w_virtcol too often.
1285 */
1286 update_curswant();
1287
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001288#ifdef FEAT_EVAL
1289 /*
1290 * May perform garbage collection when waiting for a character, but
1291 * only at the very toplevel. Otherwise we may be using a List or
1292 * Dict internally somewhere.
1293 * "may_garbage_collect" is reset in vgetc() which is invoked through
1294 * do_exmode() and normal_cmd().
1295 */
1296 may_garbage_collect = (!cmdwin && !noexmode);
1297#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001298 /*
1299 * If we're invoked as ex, do a round of ex commands.
1300 * Otherwise, get and execute a normal mode command.
1301 */
1302 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001303 {
1304 if (noexmode) /* End of ":global/path/visual" commands */
1305 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001306 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001307 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001308 else
1309 normal_cmd(&oa, TRUE);
1310 }
1311}
1312
1313
1314#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1315/*
1316 * Exit, but leave behind swap files for modified buffers.
1317 */
1318 void
1319getout_preserve_modified(exitval)
1320 int exitval;
1321{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001322# if defined(SIGHUP) && defined(SIG_IGN)
1323 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1324 * makes Vim exit and then handling SIGHUP causes various reentrance
1325 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001326 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001327# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001328
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001329 ml_close_notmod(); /* close all not-modified buffers */
1330 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1331 ml_close_all(FALSE); /* close all memfiles, without deleting */
1332 getout(exitval); /* exit Vim properly */
1333}
1334#endif
1335
1336
1337/* Exit properly */
1338 void
1339getout(exitval)
1340 int exitval;
1341{
1342#ifdef FEAT_AUTOCMD
1343 buf_T *buf;
1344 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001345 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001346#endif
1347
1348 exiting = TRUE;
1349
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001350 /* When running in Ex mode an error causes us to exit with a non-zero exit
1351 * code. POSIX requires this, although it's not 100% clear from the
1352 * standard. */
1353 if (exmode_active)
1354 exitval += ex_exitval;
1355
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001356 /* Position the cursor on the last screen line, below all the text */
1357#ifdef FEAT_GUI
1358 if (!gui.in_use)
1359#endif
1360 windgoto((int)Rows - 1, 0);
1361
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001362#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1363 /* Optionally print hashtable efficiency. */
1364 hash_debug_results();
1365#endif
1366
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001367#ifdef FEAT_GUI
1368 msg_didany = FALSE;
1369#endif
1370
1371#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001372 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001373 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001374 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1375# if defined FEAT_WINDOWS
1376 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001377 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001378 next_tp = tp->tp_next;
1379 for (wp = (tp == curtab)
1380 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001381 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001382 if (wp->w_buffer == NULL)
1383 /* Autocmd must have close the buffer already, skip. */
1384 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001385 buf = wp->w_buffer;
1386 if (buf->b_changedtick != -1)
1387 {
1388 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1389 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001390 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001391 /* start all over, autocommands may mess up the lists */
1392 next_tp = first_tabpage;
1393 break;
1394 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001395 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001396 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001397# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001398 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1399 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001400# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001401
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001402 /* Trigger BufUnload for buffers that are loaded */
1403 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1404 if (buf->b_ml.ml_mfp != NULL)
1405 {
1406 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001407 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001408 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1409 break;
1410 }
1411 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1412 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001413#endif
1414
1415#ifdef FEAT_VIMINFO
1416 if (*p_viminfo != NUL)
1417 /* Write out the registers, history, marks etc, to the viminfo file */
1418 write_viminfo(NULL, FALSE);
1419#endif
1420
1421#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 if (get_vim_var_nr(VV_DYING) <= 1)
1423 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001424#endif
1425
Bram Moolenaar05159a02005-02-26 23:04:13 +00001426#ifdef FEAT_PROFILE
1427 profile_dump();
1428#endif
1429
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001430 if (did_emsg
1431#ifdef FEAT_GUI
1432 || (gui.in_use && msg_didany && p_verbose > 0)
1433#endif
1434 )
1435 {
1436 /* give the user a chance to read the (error) message */
1437 no_wait_return = FALSE;
1438 wait_return(FALSE);
1439 }
1440
1441#ifdef FEAT_AUTOCMD
1442 /* Position the cursor again, the autocommands may have moved it */
1443# ifdef FEAT_GUI
1444 if (!gui.in_use)
1445# endif
1446 windgoto((int)Rows - 1, 0);
1447#endif
1448
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001449#ifdef FEAT_LUA
1450 lua_end();
1451#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001452#ifdef FEAT_MZSCHEME
1453 mzscheme_end();
1454#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001455#ifdef FEAT_TCL
1456 tcl_end();
1457#endif
1458#ifdef FEAT_RUBY
1459 ruby_end();
1460#endif
1461#ifdef FEAT_PYTHON
1462 python_end();
1463#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001464#ifdef FEAT_PYTHON3
1465 python3_end();
1466#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001467#ifdef FEAT_PERL
1468 perl_end();
1469#endif
1470#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1471 iconv_end();
1472#endif
1473#ifdef FEAT_NETBEANS_INTG
1474 netbeans_end();
1475#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001476#ifdef FEAT_CSCOPE
1477 cs_end();
1478#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001479#ifdef FEAT_EVAL
1480 if (garbage_collect_at_exit)
1481 garbage_collect();
1482#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001483
1484 mch_exit(exitval);
1485}
1486
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001487#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001488/*
1489 * Get a (optional) count for a Vim argument.
1490 */
1491 static int
1492get_number_arg(p, idx, def)
1493 char_u *p; /* pointer to argument */
1494 int *idx; /* index in argument, is incremented */
1495 int def; /* default value */
1496{
1497 if (vim_isdigit(p[*idx]))
1498 {
1499 def = atoi((char *)&(p[*idx]));
1500 while (vim_isdigit(p[*idx]))
1501 *idx = *idx + 1;
1502 }
1503 return def;
1504}
1505
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001506#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1507/*
1508 * Setup to use the current locale (for ctype() and many other things).
1509 */
1510 static void
1511init_locale()
1512{
1513 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001514
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001515# ifdef FEAT_GUI_GTK
1516 /* Tell Gtk not to change our locale settings. */
1517 gtk_disable_setlocale();
1518# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001519# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1520 /* Make sure strtod() uses a decimal point, not a comma. */
1521 setlocale(LC_NUMERIC, "C");
1522# endif
1523
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001524# ifdef WIN32
1525 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1526 * text while it's expecting text in the current locale. This call avoids
1527 * that. */
1528 setlocale(LC_CTYPE, "C");
1529# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001530
1531# ifdef FEAT_GETTEXT
1532 {
1533 int mustfree = FALSE;
1534 char_u *p;
1535
1536# ifdef DYNAMIC_GETTEXT
1537 /* Initialize the gettext library */
1538 dyn_libintl_init(NULL);
1539# endif
1540 /* expand_env() doesn't work yet, because chartab[] is not initialized
1541 * yet, call vim_getenv() directly */
1542 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1543 if (p != NULL && *p != NUL)
1544 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001545 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001546 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1547 }
1548 if (mustfree)
1549 vim_free(p);
1550 textdomain(VIMPACKAGE);
1551 }
1552# endif
1553}
1554#endif
1555
1556/*
1557 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1558 * If the executable name starts with "r" we disable shell commands.
1559 * If the next character is "e" we run in Easy mode.
1560 * If the next character is "g" we run the GUI version.
1561 * If the next characters are "view" we start in readonly mode.
1562 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1563 * If the next characters are "ex" we start in Ex mode. If it's followed
1564 * by "im" use improved Ex mode.
1565 */
1566 static void
1567parse_command_name(parmp)
1568 mparm_T *parmp;
1569{
1570 char_u *initstr;
1571
1572 initstr = gettail((char_u *)parmp->argv[0]);
1573
1574#ifdef MACOS_X_UNIX
1575 /* An issue has been seen when launching Vim in such a way that
1576 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1577 * executable or a symbolic link of it. Until this issue is resolved
1578 * we prohibit the GUI from being used.
1579 */
1580 if (STRCMP(initstr, parmp->argv[0]) == 0)
1581 disallow_gui = TRUE;
1582
1583 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001584 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001585#endif
1586
1587#ifdef FEAT_EVAL
1588 set_vim_var_string(VV_PROGNAME, initstr, -1);
1589#endif
1590
1591 if (TOLOWER_ASC(initstr[0]) == 'r')
1592 {
1593 restricted = TRUE;
1594 ++initstr;
1595 }
1596
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001597 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001598 if (TOLOWER_ASC(initstr[0]) == 'e'
1599 && (TOLOWER_ASC(initstr[1]) == 'v'
1600 || TOLOWER_ASC(initstr[1]) == 'g'))
1601 {
1602#ifdef FEAT_GUI
1603 gui.starting = TRUE;
1604#endif
1605 parmp->evim_mode = TRUE;
1606 ++initstr;
1607 }
1608
Bram Moolenaar806875d2008-09-18 18:57:10 +00001609 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1610 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001611 {
1612 main_start_gui();
1613#ifdef FEAT_GUI
1614 ++initstr;
1615#endif
1616 }
1617
1618 if (STRNICMP(initstr, "view", 4) == 0)
1619 {
1620 readonlymode = TRUE;
1621 curbuf->b_p_ro = TRUE;
1622 p_uc = 10000; /* don't update very often */
1623 initstr += 4;
1624 }
1625 else if (STRNICMP(initstr, "vim", 3) == 0)
1626 initstr += 3;
1627
1628 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1629 if (STRICMP(initstr, "diff") == 0)
1630 {
1631#ifdef FEAT_DIFF
1632 parmp->diff_mode = TRUE;
1633#else
1634 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1635 mch_errmsg("\n");
1636 mch_exit(2);
1637#endif
1638 }
1639
1640 if (STRNICMP(initstr, "ex", 2) == 0)
1641 {
1642 if (STRNICMP(initstr + 2, "im", 2) == 0)
1643 exmode_active = EXMODE_VIM;
1644 else
1645 exmode_active = EXMODE_NORMAL;
1646 change_compatible(TRUE); /* set 'compatible' */
1647 }
1648}
1649
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001650/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001651 * Get the name of the display, before gui_prepare() removes it from
1652 * argv[]. Used for the xterm-clipboard display.
1653 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001654 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001655 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001656 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001657early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001658 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001659{
Bram Moolenaar03005972008-11-20 13:12:36 +00001660#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1661 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001662 int argc = parmp->argc;
1663 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001664 int i;
1665
1666 for (i = 1; i < argc; i++)
1667 {
1668 if (STRCMP(argv[i], "--") == 0)
1669 break;
1670# ifdef FEAT_XCLIPBOARD
1671 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001672# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001673 || STRICMP(argv[i], "--display") == 0
1674# endif
1675 )
1676 {
1677 if (i == argc - 1)
1678 mainerr_arg_missing((char_u *)argv[i]);
1679 xterm_display = argv[++i];
1680 }
1681# endif
1682# ifdef FEAT_CLIENTSERVER
1683 else if (STRICMP(argv[i], "--servername") == 0)
1684 {
1685 if (i == argc - 1)
1686 mainerr_arg_missing((char_u *)argv[i]);
1687 parmp->serverName_arg = (char_u *)argv[++i];
1688 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001689 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001690 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001691 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001692 {
1693 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001694# ifdef FEAT_GUI
1695 if (strstr(argv[i], "-wait") != 0)
1696 /* don't fork() when starting the GUI to edit files ourself */
1697 gui.dofork = FALSE;
1698# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001699 }
1700# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001701
1702# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1703# ifdef FEAT_GUI_W32
1704 else if (STRICMP(argv[i], "--windowid") == 0)
1705# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001706 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001707# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001708 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001709 long_u id;
1710 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001711
1712 if (i == argc - 1)
1713 mainerr_arg_missing((char_u *)argv[i]);
1714 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001715 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001716 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001717 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001718 if (count != 1)
1719 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1720 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001721# ifdef FEAT_GUI_W32
1722 win_socket_id = id;
1723# else
1724 gtk_socket_id = id;
1725# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001726 i++;
1727 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001728# endif
1729# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001730 else if (STRICMP(argv[i], "--echo-wid") == 0)
1731 echo_wid_arg = TRUE;
1732# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001733# ifndef FEAT_NETBEANS_INTG
1734 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001735 {
1736 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1737 mch_exit(2);
1738 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001739# endif
1740
Bram Moolenaar58d98232005-07-23 22:25:46 +00001741 }
1742#endif
1743}
1744
1745/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001746 * Scan the command line arguments.
1747 */
1748 static void
1749command_line_scan(parmp)
1750 mparm_T *parmp;
1751{
1752 int argc = parmp->argc;
1753 char **argv = parmp->argv;
1754 int argv_idx; /* index in argv[n][] */
1755 int had_minmin = FALSE; /* found "--" argument */
1756 int want_argument; /* option argument with argument */
1757 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001758 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001759 long n;
1760
1761 --argc;
1762 ++argv;
1763 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1764 while (argc > 0)
1765 {
1766 /*
1767 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1768 */
1769 if (argv[0][0] == '+' && !had_minmin)
1770 {
1771 if (parmp->n_commands >= MAX_ARG_CMDS)
1772 mainerr(ME_EXTRA_CMD, NULL);
1773 argv_idx = -1; /* skip to next argument */
1774 if (argv[0][1] == NUL)
1775 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1776 else
1777 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1778 }
1779
1780 /*
1781 * Optional argument.
1782 */
1783 else if (argv[0][0] == '-' && !had_minmin)
1784 {
1785 want_argument = FALSE;
1786 c = argv[0][argv_idx++];
1787#ifdef VMS
1788 /*
1789 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1790 * and "-/X" as "-X".
1791 */
1792 if (c == '/')
1793 {
1794 c = argv[0][argv_idx++];
1795 c = TOUPPER_ASC(c);
1796 }
1797 else
1798 c = TOLOWER_ASC(c);
1799#endif
1800 switch (c)
1801 {
1802 case NUL: /* "vim -" read from stdin */
1803 /* "ex -" silent mode */
1804 if (exmode_active)
1805 silent_mode = TRUE;
1806 else
1807 {
1808 if (parmp->edit_type != EDIT_NONE)
1809 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1810 parmp->edit_type = EDIT_STDIN;
1811 read_cmd_fd = 2; /* read from stderr instead of stdin */
1812 }
1813 argv_idx = -1; /* skip to next argument */
1814 break;
1815
1816 case '-': /* "--" don't take any more option arguments */
1817 /* "--help" give help message */
1818 /* "--version" give version message */
1819 /* "--literal" take files literally */
1820 /* "--nofork" don't fork */
1821 /* "--noplugin[s]" skip plugins */
1822 /* "--cmd <cmd>" execute cmd before vimrc */
1823 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1824 usage();
1825 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1826 {
1827 Columns = 80; /* need to init Columns */
1828 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1829 list_version();
1830 msg_putchar('\n');
1831 msg_didout = FALSE;
1832 mch_exit(0);
1833 }
1834 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1835 {
1836#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1837 parmp->literal = TRUE;
1838#endif
1839 }
1840 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1841 {
1842#ifdef FEAT_GUI
1843 gui.dofork = FALSE; /* don't fork() when starting GUI */
1844#endif
1845 }
1846 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1847 p_lpl = FALSE;
1848 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1849 {
1850 want_argument = TRUE;
1851 argv_idx += 3;
1852 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001853 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1854 {
1855 want_argument = TRUE;
1856 argv_idx += 11;
1857 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001858#ifdef FEAT_CLIENTSERVER
1859 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1860 ; /* already processed -- no arg */
1861 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1862 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1863 {
1864 /* already processed -- snatch the following arg */
1865 if (argc > 1)
1866 {
1867 --argc;
1868 ++argv;
1869 }
1870 }
1871#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001872#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1873# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001874 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001875# else
1876 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1877# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001878 {
1879 /* already processed -- snatch the following arg */
1880 if (argc > 1)
1881 {
1882 --argc;
1883 ++argv;
1884 }
1885 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001886#endif
1887#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001888 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1889 {
1890 /* already processed, skip */
1891 }
1892#endif
1893 else
1894 {
1895 if (argv[0][argv_idx])
1896 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1897 had_minmin = TRUE;
1898 }
1899 if (!want_argument)
1900 argv_idx = -1; /* skip to next argument */
1901 break;
1902
1903 case 'A': /* "-A" start in Arabic mode */
1904#ifdef FEAT_ARABIC
1905 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1906#else
1907 mch_errmsg(_(e_noarabic));
1908 mch_exit(2);
1909#endif
1910 break;
1911
1912 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001913 /* Needs to be effective before expanding file names, because
1914 * for Win32 this makes us edit a shortcut file itself,
1915 * instead of the file it links to. */
1916 set_options_bin(curbuf->b_p_bin, 1, 0);
1917 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001918 break;
1919
1920 case 'C': /* "-C" Compatible */
1921 change_compatible(TRUE);
1922 break;
1923
1924 case 'e': /* "-e" Ex mode */
1925 exmode_active = EXMODE_NORMAL;
1926 break;
1927
1928 case 'E': /* "-E" Improved Ex mode */
1929 exmode_active = EXMODE_VIM;
1930 break;
1931
1932 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1933 window directly, not with newcli */
1934#ifdef FEAT_GUI
1935 gui.dofork = FALSE; /* don't fork() when starting GUI */
1936#endif
1937 break;
1938
1939 case 'g': /* "-g" start GUI */
1940 main_start_gui();
1941 break;
1942
1943 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1944#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001945 p_fkmap = TRUE;
1946 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001947#else
1948 mch_errmsg(_(e_nofarsi));
1949 mch_exit(2);
1950#endif
1951 break;
1952
1953 case 'h': /* "-h" give help message */
1954#ifdef FEAT_GUI_GNOME
1955 /* Tell usage() to exit for "gvim". */
1956 gui.starting = FALSE;
1957#endif
1958 usage();
1959 break;
1960
1961 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1962#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001963 p_hkmap = TRUE;
1964 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001965#else
1966 mch_errmsg(_(e_nohebrew));
1967 mch_exit(2);
1968#endif
1969 break;
1970
1971 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1972#ifdef FEAT_LISP
1973 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1974 p_sm = TRUE;
1975#endif
1976 break;
1977
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001978 case 'M': /* "-M" no changes or writing of files */
1979 reset_modifiable();
1980 /* FALLTHROUGH */
1981
1982 case 'm': /* "-m" no writing of files */
1983 p_write = FALSE;
1984 break;
1985
1986 case 'y': /* "-y" easy mode */
1987#ifdef FEAT_GUI
1988 gui.starting = TRUE; /* start GUI a bit later */
1989#endif
1990 parmp->evim_mode = TRUE;
1991 break;
1992
1993 case 'N': /* "-N" Nocompatible */
1994 change_compatible(FALSE);
1995 break;
1996
1997 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02001998#ifdef FEAT_NETBEANS_INTG
1999 /* checking for "-nb", netbeans parameters */
2000 if (argv[0][argv_idx] == 'b')
2001 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002002 netbeansArg = argv[0];
2003 argv_idx = -1; /* skip to next argument */
2004 }
2005 else
2006#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002007 parmp->no_swap_file = TRUE;
2008 break;
2009
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002010 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002011#ifdef TARGET_API_MAC_OSX
2012 /* For some reason on MacOS X, an argument like:
2013 -psn_0_10223617 is passed in when invoke from Finder
2014 or with the 'open' command */
2015 if (argv[0][argv_idx] == 's')
2016 {
2017 argv_idx = -1; /* bypass full -psn */
2018 main_start_gui();
2019 break;
2020 }
2021#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002022#ifdef FEAT_WINDOWS
2023 /* default is 0: open window for each file */
2024 parmp->window_count = get_number_arg((char_u *)argv[0],
2025 &argv_idx, 0);
2026 parmp->window_layout = WIN_TABS;
2027#endif
2028 break;
2029
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002030 case 'o': /* "-o[N]" open N horizontal split windows */
2031#ifdef FEAT_WINDOWS
2032 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002033 parmp->window_count = get_number_arg((char_u *)argv[0],
2034 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002035 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002036#endif
2037 break;
2038
2039 case 'O': /* "-O[N]" open N vertical split windows */
2040#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2041 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002042 parmp->window_count = get_number_arg((char_u *)argv[0],
2043 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002044 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002045#endif
2046 break;
2047
2048#ifdef FEAT_QUICKFIX
2049 case 'q': /* "-q" QuickFix mode */
2050 if (parmp->edit_type != EDIT_NONE)
2051 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2052 parmp->edit_type = EDIT_QF;
2053 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2054 {
2055 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2056 argv_idx = -1;
2057 }
2058 else if (argc > 1) /* "-q {errorfile}" */
2059 want_argument = TRUE;
2060 break;
2061#endif
2062
2063 case 'R': /* "-R" readonly mode */
2064 readonlymode = TRUE;
2065 curbuf->b_p_ro = TRUE;
2066 p_uc = 10000; /* don't update very often */
2067 break;
2068
2069 case 'r': /* "-r" recovery mode */
2070 case 'L': /* "-L" recovery mode */
2071 recoverymode = 1;
2072 break;
2073
2074 case 's':
2075 if (exmode_active) /* "-s" silent (batch) mode */
2076 silent_mode = TRUE;
2077 else /* "-s {scriptin}" read from script file */
2078 want_argument = TRUE;
2079 break;
2080
2081 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2082 if (parmp->edit_type != EDIT_NONE)
2083 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2084 parmp->edit_type = EDIT_TAG;
2085 if (argv[0][argv_idx]) /* "-t{tag}" */
2086 {
2087 parmp->tagname = (char_u *)argv[0] + argv_idx;
2088 argv_idx = -1;
2089 }
2090 else /* "-t {tag}" */
2091 want_argument = TRUE;
2092 break;
2093
2094#ifdef FEAT_EVAL
2095 case 'D': /* "-D" Debugging */
2096 parmp->use_debug_break_level = 9999;
2097 break;
2098#endif
2099#ifdef FEAT_DIFF
2100 case 'd': /* "-d" 'diff' */
2101# ifdef AMIGA
2102 /* check for "-dev {device}" */
2103 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2104 want_argument = TRUE;
2105 else
2106# endif
2107 parmp->diff_mode = TRUE;
2108 break;
2109#endif
2110 case 'V': /* "-V{N}" Verbose level */
2111 /* default is 10: a little bit verbose */
2112 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2113 if (argv[0][argv_idx] != NUL)
2114 {
2115 set_option_value((char_u *)"verbosefile", 0L,
2116 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002117 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002118 }
2119 break;
2120
2121 case 'v': /* "-v" Vi-mode (as if called "vi") */
2122 exmode_active = 0;
2123#ifdef FEAT_GUI
2124 gui.starting = FALSE; /* don't start GUI */
2125#endif
2126 break;
2127
2128 case 'w': /* "-w{number}" set window height */
2129 /* "-w {scriptout}" write to script */
2130 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2131 {
2132 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2133 set_option_value((char_u *)"window", n, NULL, 0);
2134 break;
2135 }
2136 want_argument = TRUE;
2137 break;
2138
2139#ifdef FEAT_CRYPT
2140 case 'x': /* "-x" encrypted reading/writing of files */
2141 parmp->ask_for_key = TRUE;
2142 break;
2143#endif
2144
2145 case 'X': /* "-X" don't connect to X server */
2146#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2147 x_no_connect = TRUE;
2148#endif
2149 break;
2150
2151 case 'Z': /* "-Z" restricted mode */
2152 restricted = TRUE;
2153 break;
2154
2155 case 'c': /* "-c{command}" or "-c {command}" execute
2156 command */
2157 if (argv[0][argv_idx] != NUL)
2158 {
2159 if (parmp->n_commands >= MAX_ARG_CMDS)
2160 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002161 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2162 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002163 argv_idx = -1;
2164 break;
2165 }
2166 /*FALLTHROUGH*/
2167 case 'S': /* "-S {file}" execute Vim script */
2168 case 'i': /* "-i {viminfo}" use for viminfo */
2169#ifndef FEAT_DIFF
2170 case 'd': /* "-d {device}" device (for Amiga) */
2171#endif
2172 case 'T': /* "-T {terminal}" terminal name */
2173 case 'u': /* "-u {vimrc}" vim inits file */
2174 case 'U': /* "-U {gvimrc}" gvim inits file */
2175 case 'W': /* "-W {scriptout}" overwrite */
2176#ifdef FEAT_GUI_W32
2177 case 'P': /* "-P {parent title}" MDI parent */
2178#endif
2179 want_argument = TRUE;
2180 break;
2181
2182 default:
2183 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2184 }
2185
2186 /*
2187 * Handle option arguments with argument.
2188 */
2189 if (want_argument)
2190 {
2191 /*
2192 * Check for garbage immediately after the option letter.
2193 */
2194 if (argv[0][argv_idx] != NUL)
2195 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2196
2197 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002198 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002199 mainerr_arg_missing((char_u *)argv[0]);
2200 ++argv;
2201 argv_idx = -1;
2202
2203 switch (c)
2204 {
2205 case 'c': /* "-c {command}" execute command */
2206 case 'S': /* "-S {file}" execute Vim script */
2207 if (parmp->n_commands >= MAX_ARG_CMDS)
2208 mainerr(ME_EXTRA_CMD, NULL);
2209 if (c == 'S')
2210 {
2211 char *a;
2212
2213 if (argc < 1)
2214 /* "-S" without argument: use default session file
2215 * name. */
2216 a = SESSION_FILE;
2217 else if (argv[0][0] == '-')
2218 {
2219 /* "-S" followed by another option: use default
2220 * session file name. */
2221 a = SESSION_FILE;
2222 ++argc;
2223 --argv;
2224 }
2225 else
2226 a = argv[0];
2227 p = alloc((unsigned)(STRLEN(a) + 4));
2228 if (p == NULL)
2229 mch_exit(2);
2230 sprintf((char *)p, "so %s", a);
2231 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2232 parmp->commands[parmp->n_commands++] = p;
2233 }
2234 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002235 parmp->commands[parmp->n_commands++] =
2236 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002237 break;
2238
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002239 case '-':
2240 if (argv[-1][2] == 'c')
2241 {
2242 /* "--cmd {command}" execute command */
2243 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2244 mainerr(ME_EXTRA_CMD, NULL);
2245 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002246 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002247 }
2248 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002249 break;
2250
2251 /* case 'd': -d {device} is handled in mch_check_win() for the
2252 * Amiga */
2253
2254#ifdef FEAT_QUICKFIX
2255 case 'q': /* "-q {errorfile}" QuickFix mode */
2256 parmp->use_ef = (char_u *)argv[0];
2257 break;
2258#endif
2259
2260 case 'i': /* "-i {viminfo}" use for viminfo */
2261 use_viminfo = (char_u *)argv[0];
2262 break;
2263
2264 case 's': /* "-s {scriptin}" read from script file */
2265 if (scriptin[0] != NULL)
2266 {
2267scripterror:
2268 mch_errmsg(_("Attempt to open script file again: \""));
2269 mch_errmsg(argv[-1]);
2270 mch_errmsg(" ");
2271 mch_errmsg(argv[0]);
2272 mch_errmsg("\"\n");
2273 mch_exit(2);
2274 }
2275 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2276 {
2277 mch_errmsg(_("Cannot open for reading: \""));
2278 mch_errmsg(argv[0]);
2279 mch_errmsg("\"\n");
2280 mch_exit(2);
2281 }
2282 if (save_typebuf() == FAIL)
2283 mch_exit(2); /* out of memory */
2284 break;
2285
2286 case 't': /* "-t {tag}" */
2287 parmp->tagname = (char_u *)argv[0];
2288 break;
2289
2290 case 'T': /* "-T {terminal}" terminal name */
2291 /*
2292 * The -T term argument is always available and when
2293 * HAVE_TERMLIB is supported it overrides the environment
2294 * variable TERM.
2295 */
2296#ifdef FEAT_GUI
2297 if (term_is_gui((char_u *)argv[0]))
2298 gui.starting = TRUE; /* start GUI a bit later */
2299 else
2300#endif
2301 parmp->term = (char_u *)argv[0];
2302 break;
2303
2304 case 'u': /* "-u {vimrc}" vim inits file */
2305 parmp->use_vimrc = (char_u *)argv[0];
2306 break;
2307
2308 case 'U': /* "-U {gvimrc}" gvim inits file */
2309#ifdef FEAT_GUI
2310 use_gvimrc = (char_u *)argv[0];
2311#endif
2312 break;
2313
2314 case 'w': /* "-w {nr}" 'window' value */
2315 /* "-w {scriptout}" append to script file */
2316 if (vim_isdigit(*((char_u *)argv[0])))
2317 {
2318 argv_idx = 0;
2319 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2320 set_option_value((char_u *)"window", n, NULL, 0);
2321 argv_idx = -1;
2322 break;
2323 }
2324 /*FALLTHROUGH*/
2325 case 'W': /* "-W {scriptout}" overwrite script file */
2326 if (scriptout != NULL)
2327 goto scripterror;
2328 if ((scriptout = mch_fopen(argv[0],
2329 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2330 {
2331 mch_errmsg(_("Cannot open for script output: \""));
2332 mch_errmsg(argv[0]);
2333 mch_errmsg("\"\n");
2334 mch_exit(2);
2335 }
2336 break;
2337
2338#ifdef FEAT_GUI_W32
2339 case 'P': /* "-P {parent title}" MDI parent */
2340 gui_mch_set_parent(argv[0]);
2341 break;
2342#endif
2343 }
2344 }
2345 }
2346
2347 /*
2348 * File name argument.
2349 */
2350 else
2351 {
2352 argv_idx = -1; /* skip to next argument */
2353
2354 /* Check for only one type of editing. */
2355 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2356 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2357 parmp->edit_type = EDIT_FILE;
2358
2359#ifdef MSWIN
2360 /* Remember if the argument was a full path before changing
2361 * slashes to backslashes. */
2362 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2363 parmp->full_path = TRUE;
2364#endif
2365
2366 /* Add the file to the global argument list. */
2367 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2368 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2369 mch_exit(2);
2370#ifdef FEAT_DIFF
2371 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2372 && !mch_isdir(alist_name(&GARGLIST[0])))
2373 {
2374 char_u *r;
2375
2376 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2377 if (r != NULL)
2378 {
2379 vim_free(p);
2380 p = r;
2381 }
2382 }
2383#endif
2384#if defined(__CYGWIN32__) && !defined(WIN32)
2385 /*
2386 * If vim is invoked by non-Cygwin tools, convert away any
2387 * DOS paths, so things like .swp files are created correctly.
2388 * Look for evidence of non-Cygwin paths before we bother.
2389 * This is only for when using the Unix files.
2390 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002391 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002392 {
2393 char posix_path[PATH_MAX];
2394
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002395# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2396 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2397# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002398 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002399# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002400 vim_free(p);
2401 p = vim_strsave(posix_path);
2402 if (p == NULL)
2403 mch_exit(2);
2404 }
2405#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002406
2407#ifdef USE_FNAME_CASE
2408 /* Make the case of the file name match the actual file. */
2409 fname_case(p, 0);
2410#endif
2411
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002412 alist_add(&global_alist, p,
2413#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002414 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002415#else
2416 2 /* add buffer number now and use curbuf */
2417#endif
2418 );
2419
2420#if defined(FEAT_MBYTE) && defined(WIN32)
2421 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002422 /* Remember this argument has been added to the argument list.
2423 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002424 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002425# ifdef FEAT_DIFF
2426 parmp->diff_mode
2427# else
2428 FALSE
2429# endif
2430 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002431 }
2432#endif
2433 }
2434
2435 /*
2436 * If there are no more letters after the current "-", go to next
2437 * argument. argv_idx is set to -1 when the current argument is to be
2438 * skipped.
2439 */
2440 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2441 {
2442 --argc;
2443 ++argv;
2444 argv_idx = 1;
2445 }
2446 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002447
2448#ifdef FEAT_EVAL
2449 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2450 * one. */
2451 if (parmp->n_commands > 0)
2452 {
2453 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2454 if (p != NULL)
2455 {
2456 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2457 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2458 vim_free(p);
2459 }
2460 }
2461#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002462}
2463
2464/*
2465 * Print a warning if stdout is not a terminal.
2466 * When starting in Ex mode and commands come from a file, set Silent mode.
2467 */
2468 static void
2469check_tty(parmp)
2470 mparm_T *parmp;
2471{
2472 int input_isatty; /* is active input a terminal? */
2473
2474 input_isatty = mch_input_isatty();
2475 if (exmode_active)
2476 {
2477 if (!input_isatty)
2478 silent_mode = TRUE;
2479 }
2480 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2481#ifdef FEAT_GUI
2482 /* don't want the delay when started from the desktop */
2483 && !gui.starting
2484#endif
2485 )
2486 {
2487#ifdef NBDEBUG
2488 /*
2489 * This shouldn't be necessary. But if I run netbeans with the log
2490 * output coming to the console and XOpenDisplay fails, I get vim
2491 * trying to start with input/output to my console tty. This fills my
2492 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002493 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002494 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002495 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002496 {
2497 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2498 exit(1);
2499 }
2500#endif
2501 if (!parmp->stdout_isatty)
2502 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2503 if (!input_isatty)
2504 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2505 out_flush();
2506 if (scriptin[0] == NULL)
2507 ui_delay(2000L, TRUE);
2508 TIME_MSG("Warning delay");
2509 }
2510}
2511
2512/*
2513 * Read text from stdin.
2514 */
2515 static void
2516read_stdin()
2517{
2518 int i;
2519
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002520#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002521 /* When getting the ATTENTION prompt here, use a dialog */
2522 swap_exists_action = SEA_DIALOG;
2523#endif
2524 no_wait_return = TRUE;
2525 i = msg_didany;
2526 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002527 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002528 no_wait_return = FALSE;
2529 msg_didany = i;
2530 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002531#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002532 check_swap_exists_action();
2533#endif
2534#if !(defined(AMIGA) || defined(MACOS))
2535 /*
2536 * Close stdin and dup it from stderr. Required for GPM to work
2537 * properly, and for running external commands.
2538 * Is there any other system that cannot do this?
2539 */
2540 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002541 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002542#endif
2543}
2544
2545/*
2546 * Create the requested number of windows and edit buffers in them.
2547 * Also does recovery if "recoverymode" set.
2548 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002549 static void
2550create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002551 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002552{
2553#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002554 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002555 int done = 0;
2556
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002557 /*
2558 * Create the number of windows that was requested.
2559 */
2560 if (parmp->window_count == -1) /* was not set */
2561 parmp->window_count = 1;
2562 if (parmp->window_count == 0)
2563 parmp->window_count = GARGCOUNT;
2564 if (parmp->window_count > 1)
2565 {
2566 /* Don't change the windows if there was a command in .vimrc that
2567 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002568 if (parmp->window_layout == 0)
2569 parmp->window_layout = WIN_HOR;
2570 if (parmp->window_layout == WIN_TABS)
2571 {
2572 parmp->window_count = make_tabpages(parmp->window_count);
2573 TIME_MSG("making tab pages");
2574 }
2575 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002576 {
2577 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002578 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002579 TIME_MSG("making windows");
2580 }
2581 else
2582 parmp->window_count = win_count();
2583 }
2584 else
2585 parmp->window_count = 1;
2586#endif
2587
2588 if (recoverymode) /* do recover */
2589 {
2590 msg_scroll = TRUE; /* scroll message up */
2591 ml_recover();
2592 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2593 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002594 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002595 }
2596 else
2597 {
2598 /*
2599 * Open a buffer for windows that don't have one yet.
2600 * Commands in the .vimrc might have loaded a file or split the window.
2601 * Watch out for autocommands that delete a window.
2602 */
2603#ifdef FEAT_AUTOCMD
2604 /*
2605 * Don't execute Win/Buf Enter/Leave autocommands here
2606 */
2607 ++autocmd_no_enter;
2608 ++autocmd_no_leave;
2609#endif
2610#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002611 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002612 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002613 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002614 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002615 {
2616 if (parmp->window_layout == WIN_TABS)
2617 goto_tabpage(1);
2618 else
2619 curwin = firstwin;
2620 }
2621 else if (parmp->window_layout == WIN_TABS)
2622 {
2623 if (curtab->tp_next == NULL)
2624 break;
2625 goto_tabpage(0);
2626 }
2627 else
2628 {
2629 if (curwin->w_next == NULL)
2630 break;
2631 curwin = curwin->w_next;
2632 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002633 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002634#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002635 curbuf = curwin->w_buffer;
2636 if (curbuf->b_ml.ml_mfp == NULL)
2637 {
2638#ifdef FEAT_FOLDING
2639 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2640 if (p_fdls >= 0)
2641 curwin->w_p_fdl = p_fdls;
2642#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002643#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002644 /* When getting the ATTENTION prompt here, use a dialog */
2645 swap_exists_action = SEA_DIALOG;
2646#endif
2647 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002648
2649 /* create memfile, read file */
2650 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002651
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002652#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002653 if (swap_exists_action == SEA_QUIT)
2654 {
2655 if (got_int || only_one_window())
2656 {
2657 /* abort selected or quit and only one window */
2658 did_emsg = FALSE; /* avoid hit-enter prompt */
2659 getout(1);
2660 }
2661 /* We can't close the window, it would disturb what
2662 * happens next. Clear the file name and set the arg
2663 * index to -1 to delete it later. */
2664 setfname(curbuf, NULL, NULL, FALSE);
2665 curwin->w_arg_idx = -1;
2666 swap_exists_action = SEA_NONE;
2667 }
2668 else
2669 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002670#endif
2671#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002672 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002673#endif
2674 }
2675#ifdef FEAT_WINDOWS
2676 ui_breakcheck();
2677 if (got_int)
2678 {
2679 (void)vgetc(); /* only break the file loading, not the rest */
2680 break;
2681 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002682 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002683#endif
2684#ifdef FEAT_WINDOWS
2685 if (parmp->window_layout == WIN_TABS)
2686 goto_tabpage(1);
2687 else
2688 curwin = firstwin;
2689 curbuf = curwin->w_buffer;
2690#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002691#ifdef FEAT_AUTOCMD
2692 --autocmd_no_enter;
2693 --autocmd_no_leave;
2694#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002695 }
2696}
2697
2698#ifdef FEAT_WINDOWS
2699 /*
2700 * If opened more than one window, start editing files in the other
2701 * windows. make_windows() has already opened the windows.
2702 */
2703 static void
2704edit_buffers(parmp)
2705 mparm_T *parmp;
2706{
2707 int arg_idx; /* index in argument list */
2708 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002709 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002710
2711# ifdef FEAT_AUTOCMD
2712 /*
2713 * Don't execute Win/Buf Enter/Leave autocommands here
2714 */
2715 ++autocmd_no_enter;
2716 ++autocmd_no_leave;
2717# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002718
2719 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2720 if (curwin->w_arg_idx == -1)
2721 {
2722 win_close(curwin, TRUE);
2723 advance = FALSE;
2724 }
2725
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002726 arg_idx = 1;
2727 for (i = 1; i < parmp->window_count; ++i)
2728 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002729 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2730 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002731 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002732 ++arg_idx;
2733 win_close(curwin, TRUE);
2734 advance = FALSE;
2735 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002736 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002737
Bram Moolenaar84212822006-11-07 21:59:47 +00002738 if (advance)
2739 {
2740 if (parmp->window_layout == WIN_TABS)
2741 {
2742 if (curtab->tp_next == NULL) /* just checking */
2743 break;
2744 goto_tabpage(0);
2745 }
2746 else
2747 {
2748 if (curwin->w_next == NULL) /* just checking */
2749 break;
2750 win_enter(curwin->w_next, FALSE);
2751 }
2752 }
2753 advance = TRUE;
2754
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002755 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002756 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2758 {
2759 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002760 /* Edit file from arg list, if there is one. When "Quit" selected
2761 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002762# ifdef HAS_SWAP_EXISTS_ACTION
2763 swap_exists_did_quit = FALSE;
2764# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002765 (void)do_ecmd(0, arg_idx < GARGCOUNT
2766 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002767 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002768# ifdef HAS_SWAP_EXISTS_ACTION
2769 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002770 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002771 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002772 if (got_int || only_one_window())
2773 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002774 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002775 did_emsg = FALSE; /* avoid hit-enter prompt */
2776 getout(1);
2777 }
2778 win_close(curwin, TRUE);
2779 advance = FALSE;
2780 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002781# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002782 if (arg_idx == GARGCOUNT - 1)
2783 arg_had_last = TRUE;
2784 ++arg_idx;
2785 }
2786 ui_breakcheck();
2787 if (got_int)
2788 {
2789 (void)vgetc(); /* only break the file loading, not the rest */
2790 break;
2791 }
2792 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002793
2794 if (parmp->window_layout == WIN_TABS)
2795 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002796# ifdef FEAT_AUTOCMD
2797 --autocmd_no_enter;
2798# endif
2799 win_enter(firstwin, FALSE); /* back to first window */
2800# ifdef FEAT_AUTOCMD
2801 --autocmd_no_leave;
2802# endif
2803 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002804 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002805 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2806}
2807#endif /* FEAT_WINDOWS */
2808
2809/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002810 * Execute the commands from --cmd arguments "cmds[cnt]".
2811 */
2812 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002813exe_pre_commands(parmp)
2814 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002815{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816 char_u **cmds = parmp->pre_commands;
2817 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002818 int i;
2819
2820 if (cnt > 0)
2821 {
2822 curwin->w_cursor.lnum = 0; /* just in case.. */
2823 sourcing_name = (char_u *)_("pre-vimrc command line");
2824# ifdef FEAT_EVAL
2825 current_SID = SID_CMDARG;
2826# endif
2827 for (i = 0; i < cnt; ++i)
2828 do_cmdline_cmd(cmds[i]);
2829 sourcing_name = NULL;
2830# ifdef FEAT_EVAL
2831 current_SID = 0;
2832# endif
2833 TIME_MSG("--cmd commands");
2834 }
2835}
2836
2837/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002838 * Execute "+", "-c" and "-S" arguments.
2839 */
2840 static void
2841exe_commands(parmp)
2842 mparm_T *parmp;
2843{
2844 int i;
2845
2846 /*
2847 * We start commands on line 0, make "vim +/pat file" match a
2848 * pattern on line 1. But don't move the cursor when an autocommand
2849 * with g`" was used.
2850 */
2851 msg_scroll = TRUE;
2852 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2853 curwin->w_cursor.lnum = 0;
2854 sourcing_name = (char_u *)"command line";
2855#ifdef FEAT_EVAL
2856 current_SID = SID_CARG;
2857#endif
2858 for (i = 0; i < parmp->n_commands; ++i)
2859 {
2860 do_cmdline_cmd(parmp->commands[i]);
2861 if (parmp->cmds_tofree[i])
2862 vim_free(parmp->commands[i]);
2863 }
2864 sourcing_name = NULL;
2865#ifdef FEAT_EVAL
2866 current_SID = 0;
2867#endif
2868 if (curwin->w_cursor.lnum == 0)
2869 curwin->w_cursor.lnum = 1;
2870
2871 if (!exmode_active)
2872 msg_scroll = FALSE;
2873
2874#ifdef FEAT_QUICKFIX
2875 /* When started with "-q errorfile" jump to first error again. */
2876 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002877 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002878#endif
2879 TIME_MSG("executing command arguments");
2880}
2881
2882/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002883 * Source startup scripts.
2884 */
2885 static void
2886source_startup_scripts(parmp)
2887 mparm_T *parmp;
2888{
2889 int i;
2890
2891 /*
2892 * For "evim" source evim.vim first of all, so that the user can overrule
2893 * any things he doesn't like.
2894 */
2895 if (parmp->evim_mode)
2896 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002897 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002898 TIME_MSG("source evim file");
2899 }
2900
2901 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002902 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002903 * nothing else.
2904 */
2905 if (parmp->use_vimrc != NULL)
2906 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002907 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2908 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002909 {
2910#ifdef FEAT_GUI
2911 if (use_gvimrc == NULL) /* don't load gvimrc either */
2912 use_gvimrc = parmp->use_vimrc;
2913#endif
2914 if (parmp->use_vimrc[2] == 'N')
2915 p_lpl = FALSE; /* don't load plugins either */
2916 }
2917 else
2918 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002919 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002920 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2921 }
2922 }
2923 else if (!silent_mode)
2924 {
2925#ifdef AMIGA
2926 struct Process *proc = (struct Process *)FindTask(0L);
2927 APTR save_winptr = proc->pr_WindowPtr;
2928
2929 /* Avoid a requester here for a volume that doesn't exist. */
2930 proc->pr_WindowPtr = (APTR)-1L;
2931#endif
2932
2933 /*
2934 * Get system wide defaults, if the file name is defined.
2935 */
2936#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002937 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002938#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002939#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002940 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002941#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002942
2943 /*
2944 * Try to read initialization commands from the following places:
2945 * - environment variable VIMINIT
2946 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2947 * - second user vimrc file ($VIM/.vimrc for Dos)
2948 * - environment variable EXINIT
2949 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2950 * - second user exrc file ($VIM/.exrc for Dos)
2951 * The first that exists is used, the rest is ignored.
2952 */
2953 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2954 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002955 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002956#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002957 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2958 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002959#endif
2960#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002961 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2962 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002963#endif
2964 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002965 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002966 {
2967#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002968 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002969#endif
2970 }
2971 }
2972
2973 /*
2974 * Read initialization commands from ".vimrc" or ".exrc" in current
2975 * directory. This is only done if the 'exrc' option is set.
2976 * Because of security reasons we disallow shell and write commands
2977 * now, except for unix if the file is owned by the user or 'secure'
2978 * option has been reset in environment of global ".exrc" or ".vimrc".
2979 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
2980 * SYS_VIMRC_FILE.
2981 */
2982 if (p_exrc)
2983 {
2984#if defined(UNIX) || defined(VMS)
2985 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
2986 if (!file_owned(VIMRC_FILE))
2987#endif
2988 secure = p_secure;
2989
2990 i = FAIL;
2991 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
2992 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2993#ifdef USR_VIMRC_FILE2
2994 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
2995 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
2996#endif
2997#ifdef USR_VIMRC_FILE3
2998 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
2999 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3000#endif
3001#ifdef SYS_VIMRC_FILE
3002 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3003 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3004#endif
3005 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003006 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003007
3008 if (i == FAIL)
3009 {
3010#if defined(UNIX) || defined(VMS)
3011 /* if ".exrc" is not owned by user set 'secure' mode */
3012 if (!file_owned(EXRC_FILE))
3013 secure = p_secure;
3014 else
3015 secure = 0;
3016#endif
3017 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3018 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3019#ifdef USR_EXRC_FILE2
3020 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3021 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3022#endif
3023 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003024 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003025 }
3026 }
3027 if (secure == 2)
3028 need_wait_return = TRUE;
3029 secure = 0;
3030#ifdef AMIGA
3031 proc->pr_WindowPtr = save_winptr;
3032#endif
3033 }
3034 TIME_MSG("sourcing vimrc file(s)");
3035}
3036
3037/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003038 * Setup to start using the GUI. Exit with an error when not available.
3039 */
3040 static void
3041main_start_gui()
3042{
3043#ifdef FEAT_GUI
3044 gui.starting = TRUE; /* start GUI a bit later */
3045#else
3046 mch_errmsg(_(e_nogvim));
3047 mch_errmsg("\n");
3048 mch_exit(2);
3049#endif
3050}
3051
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003052#endif /* NO_VIM_MAIN */
3053
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003054/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003055 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003056 * Returns FAIL if the environment variable was not executed, OK otherwise.
3057 */
3058 int
3059process_env(env, is_viminit)
3060 char_u *env;
3061 int is_viminit; /* when TRUE, called for VIMINIT */
3062{
3063 char_u *initstr;
3064 char_u *save_sourcing_name;
3065 linenr_T save_sourcing_lnum;
3066#ifdef FEAT_EVAL
3067 scid_T save_sid;
3068#endif
3069
3070 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3071 {
3072 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003073 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003074 save_sourcing_name = sourcing_name;
3075 save_sourcing_lnum = sourcing_lnum;
3076 sourcing_name = env;
3077 sourcing_lnum = 0;
3078#ifdef FEAT_EVAL
3079 save_sid = current_SID;
3080 current_SID = SID_ENV;
3081#endif
3082 do_cmdline_cmd(initstr);
3083 sourcing_name = save_sourcing_name;
3084 sourcing_lnum = save_sourcing_lnum;
3085#ifdef FEAT_EVAL
3086 current_SID = save_sid;;
3087#endif
3088 return OK;
3089 }
3090 return FAIL;
3091}
3092
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003093#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003094/*
3095 * Return TRUE if we are certain the user owns the file "fname".
3096 * Used for ".vimrc" and ".exrc".
3097 * Use both stat() and lstat() for extra security.
3098 */
3099 static int
3100file_owned(fname)
3101 char *fname;
3102{
3103 struct stat s;
3104# ifdef UNIX
3105 uid_t uid = getuid();
3106# else /* VMS */
3107 uid_t uid = ((getgid() << 16) | getuid());
3108# endif
3109
3110 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3111# ifdef HAVE_LSTAT
3112 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3113# endif
3114 );
3115}
3116#endif
3117
3118/*
3119 * Give an error message main_errors["n"] and exit.
3120 */
3121 static void
3122mainerr(n, str)
3123 int n; /* one of the ME_ defines */
3124 char_u *str; /* extra argument or NULL */
3125{
3126#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3127 reset_signals(); /* kill us with CTRL-C here, if you like */
3128#endif
3129
3130 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003131 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003132 mch_errmsg(_(main_errors[n]));
3133 if (str != NULL)
3134 {
3135 mch_errmsg(": \"");
3136 mch_errmsg((char *)str);
3137 mch_errmsg("\"");
3138 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003139 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003140
3141 mch_exit(1);
3142}
3143
3144 void
3145mainerr_arg_missing(str)
3146 char_u *str;
3147{
3148 mainerr(ME_ARG_MISSING, str);
3149}
3150
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003151#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003152/*
3153 * print a message with three spaces prepended and '\n' appended.
3154 */
3155 static void
3156main_msg(s)
3157 char *s;
3158{
3159 mch_msg(" ");
3160 mch_msg(s);
3161 mch_msg("\n");
3162}
3163
3164/*
3165 * Print messages for "vim -h" or "vim --help" and exit.
3166 */
3167 static void
3168usage()
3169{
3170 int i;
3171 static char *(use[]) =
3172 {
3173 N_("[file ..] edit specified file(s)"),
3174 N_("- read text from stdin"),
3175 N_("-t tag edit file where tag is defined"),
3176#ifdef FEAT_QUICKFIX
3177 N_("-q [errorfile] edit file with first error")
3178#endif
3179 };
3180
3181#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3182 reset_signals(); /* kill us with CTRL-C here, if you like */
3183#endif
3184
3185 mch_msg(longVersion);
3186 mch_msg(_("\n\nusage:"));
3187 for (i = 0; ; ++i)
3188 {
3189 mch_msg(_(" vim [arguments] "));
3190 mch_msg(_(use[i]));
3191 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3192 break;
3193 mch_msg(_("\n or:"));
3194 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003195#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003196 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003197#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003198
3199 mch_msg(_("\n\nArguments:\n"));
3200 main_msg(_("--\t\t\tOnly file names after this"));
3201#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3202 main_msg(_("--literal\t\tDon't expand wildcards"));
3203#endif
3204#ifdef FEAT_OLE
3205 main_msg(_("-register\t\tRegister this gvim for OLE"));
3206 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3207#endif
3208#ifdef FEAT_GUI
3209 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3210 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3211#endif
3212 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3213 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003214 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003215 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3216#ifdef FEAT_DIFF
3217 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3218#endif
3219 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3220 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3221 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3222 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3223 main_msg(_("-M\t\t\tModifications in text not allowed"));
3224 main_msg(_("-b\t\t\tBinary mode"));
3225#ifdef FEAT_LISP
3226 main_msg(_("-l\t\t\tLisp mode"));
3227#endif
3228 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3229 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003230 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3231#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003232 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003233#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003234 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3235 main_msg(_("-r\t\t\tList swap files and exit"));
3236 main_msg(_("-r (with file name)\tRecover crashed session"));
3237 main_msg(_("-L\t\t\tSame as -r"));
3238#ifdef AMIGA
3239 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3240 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3241#endif
3242#ifdef FEAT_ARABIC
3243 main_msg(_("-A\t\t\tstart in Arabic mode"));
3244#endif
3245#ifdef FEAT_RIGHTLEFT
3246 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3247#endif
3248#ifdef FEAT_FKMAP
3249 main_msg(_("-F\t\t\tStart in Farsi mode"));
3250#endif
3251 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3252 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3253#ifdef FEAT_GUI
3254 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3255#endif
3256 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003257#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003258 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003259 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3260 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003261#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003262 main_msg(_("+\t\t\tStart at end of file"));
3263 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003264 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003265 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3266 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3267 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3268 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3269 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3270#ifdef FEAT_CRYPT
3271 main_msg(_("-x\t\t\tEdit encrypted files"));
3272#endif
3273#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3274# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3275 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3276# endif
3277 main_msg(_("-X\t\t\tDo not connect to X server"));
3278#endif
3279#ifdef FEAT_CLIENTSERVER
3280 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3281 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3282 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3283 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003284# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003285 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003286# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003287 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3288 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3289 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3290 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3291#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003292#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003293 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003294#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003295#ifdef FEAT_VIMINFO
3296 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3297#endif
3298 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3299 main_msg(_("--version\t\tPrint version information and exit"));
3300
3301#ifdef FEAT_GUI_X11
3302# ifdef FEAT_GUI_MOTIF
3303 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3304# else
3305# ifdef FEAT_GUI_ATHENA
3306# ifdef FEAT_GUI_NEXTAW
3307 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3308# else
3309 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3310# endif
3311# endif
3312# endif
3313 main_msg(_("-display <display>\tRun vim on <display>"));
3314 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003315 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3316 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3317 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3318 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3319 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3320 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3321 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3322 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3323# ifdef FEAT_GUI_ATHENA
3324 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3325# endif
3326 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3327 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3328 main_msg(_("-xrm <resource>\tSet the specified resource"));
3329#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003330#ifdef FEAT_GUI_GTK
3331 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3332 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3333 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3334 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3335 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003336 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003337 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003338 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003339#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003340#ifdef FEAT_GUI_W32
3341 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003342 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003343#endif
3344
3345#ifdef FEAT_GUI_GNOME
3346 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3347 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003348 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003349 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003350 gui.dofork = FALSE;
3351 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003352 else
3353#endif
3354 mch_exit(0);
3355}
3356
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003357#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003358/*
3359 * Check the result of the ATTENTION dialog:
3360 * When "Quit" selected, exit Vim.
3361 * When "Recover" selected, recover the file.
3362 */
3363 static void
3364check_swap_exists_action()
3365{
3366 if (swap_exists_action == SEA_QUIT)
3367 getout(1);
3368 handle_swap_exists(NULL);
3369}
3370#endif
3371
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003372#endif
3373
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003374#if defined(STARTUPTIME) || defined(PROTO)
3375static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3376
3377static struct timeval prev_timeval;
3378
Bram Moolenaar3f269672009-11-03 11:11:11 +00003379# ifdef WIN3264
3380/*
3381 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3382 */
3383 static int
3384gettimeofday(struct timeval *tv, char *dummy)
3385{
3386 long t = clock();
3387 tv->tv_sec = t / CLOCKS_PER_SEC;
3388 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3389 return 0;
3390}
3391# endif
3392
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003393/*
3394 * Save the previous time before doing something that could nest.
3395 * set "*tv_rel" to the time elapsed so far.
3396 */
3397 void
3398time_push(tv_rel, tv_start)
3399 void *tv_rel, *tv_start;
3400{
3401 *((struct timeval *)tv_rel) = prev_timeval;
3402 gettimeofday(&prev_timeval, NULL);
3403 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3404 - ((struct timeval *)tv_rel)->tv_usec;
3405 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3406 - ((struct timeval *)tv_rel)->tv_sec;
3407 if (((struct timeval *)tv_rel)->tv_usec < 0)
3408 {
3409 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3410 --((struct timeval *)tv_rel)->tv_sec;
3411 }
3412 *(struct timeval *)tv_start = prev_timeval;
3413}
3414
3415/*
3416 * Compute the previous time after doing something that could nest.
3417 * Subtract "*tp" from prev_timeval;
3418 * Note: The arguments are (void *) to avoid trouble with systems that don't
3419 * have struct timeval.
3420 */
3421 void
3422time_pop(tp)
3423 void *tp; /* actually (struct timeval *) */
3424{
3425 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3426 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3427 if (prev_timeval.tv_usec < 0)
3428 {
3429 prev_timeval.tv_usec += 1000000;
3430 --prev_timeval.tv_sec;
3431 }
3432}
3433
3434 static void
3435time_diff(then, now)
3436 struct timeval *then;
3437 struct timeval *now;
3438{
3439 long usec;
3440 long msec;
3441
3442 usec = now->tv_usec - then->tv_usec;
3443 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3444 usec = usec % 1000L;
3445 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3446}
3447
3448 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003449time_msg(mesg, tv_start)
3450 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003451 void *tv_start; /* only for do_source: start time; actually
3452 (struct timeval *) */
3453{
3454 static struct timeval start;
3455 struct timeval now;
3456
3457 if (time_fd != NULL)
3458 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003459 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003460 {
3461 gettimeofday(&start, NULL);
3462 prev_timeval = start;
3463 fprintf(time_fd, "\n\ntimes in msec\n");
3464 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3465 fprintf(time_fd, " clock elapsed: other lines\n\n");
3466 }
3467 gettimeofday(&now, NULL);
3468 time_diff(&start, &now);
3469 if (((struct timeval *)tv_start) != NULL)
3470 {
3471 fprintf(time_fd, " ");
3472 time_diff(((struct timeval *)tv_start), &now);
3473 }
3474 fprintf(time_fd, " ");
3475 time_diff(&prev_timeval, &now);
3476 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003477 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003478 }
3479}
3480
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003481#endif
3482
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003483#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003484
3485/*
3486 * Common code for the X command server and the Win32 command server.
3487 */
3488
Bram Moolenaareb94e552006-03-11 21:35:11 +00003489static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003490
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003491/*
3492 * Do the client-server stuff, unless "--servername ''" was used.
3493 */
3494 static void
3495exec_on_server(parmp)
3496 mparm_T *parmp;
3497{
3498 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3499 {
3500# ifdef WIN32
3501 /* Initialise the client/server messaging infrastructure. */
3502 serverInitMessaging();
3503# endif
3504
3505 /*
3506 * When a command server argument was found, execute it. This may
3507 * exit Vim when it was successful. Otherwise it's executed further
3508 * on. Remember the encoding used here in "serverStrEnc".
3509 */
3510 if (parmp->serverArg)
3511 {
3512 cmdsrv_main(&parmp->argc, parmp->argv,
3513 parmp->serverName_arg, &parmp->serverStr);
3514# ifdef FEAT_MBYTE
3515 parmp->serverStrEnc = vim_strsave(p_enc);
3516# endif
3517 }
3518
3519 /* If we're still running, get the name to register ourselves.
3520 * On Win32 can register right now, for X11 need to setup the
3521 * clipboard first, it's further down. */
3522 parmp->servername = serverMakeName(parmp->serverName_arg,
3523 parmp->argv[0]);
3524# ifdef WIN32
3525 if (parmp->servername != NULL)
3526 {
3527 serverSetName(parmp->servername);
3528 vim_free(parmp->servername);
3529 }
3530# endif
3531 }
3532}
3533
3534/*
3535 * Prepare for running as a Vim server.
3536 */
3537 static void
3538prepare_server(parmp)
3539 mparm_T *parmp;
3540{
3541# if defined(FEAT_X11)
3542 /*
3543 * Register for remote command execution with :serversend and --remote
3544 * unless there was a -X or a --servername '' on the command line.
3545 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003546 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003547 */
3548 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3549# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003550 (gui.in_use
3551# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003552 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003553# endif
3554 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003555# endif
3556 parmp->serverName_arg != NULL))
3557 {
3558 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3559 vim_free(parmp->servername);
3560 TIME_MSG("register server name");
3561 }
3562 else
3563 serverDelayedStartName = parmp->servername;
3564# endif
3565
3566 /*
3567 * Execute command ourselves if we're here because the send failed (or
3568 * else we would have exited above).
3569 */
3570 if (parmp->serverStr != NULL)
3571 {
3572 char_u *p;
3573
3574 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3575 parmp->serverStr, &p));
3576 vim_free(p);
3577 }
3578}
3579
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003580 static void
3581cmdsrv_main(argc, argv, serverName_arg, serverStr)
3582 int *argc;
3583 char **argv;
3584 char_u *serverName_arg;
3585 char_u **serverStr;
3586{
3587 char_u *res;
3588 int i;
3589 char_u *sname;
3590 int ret;
3591 int didone = FALSE;
3592 int exiterr = 0;
3593 char **newArgV = argv + 1;
3594 int newArgC = 1,
3595 Argc = *argc;
3596 int argtype;
3597#define ARGTYPE_OTHER 0
3598#define ARGTYPE_EDIT 1
3599#define ARGTYPE_EDIT_WAIT 2
3600#define ARGTYPE_SEND 3
3601 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003602 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003603# ifndef FEAT_X11
3604 HWND srv;
3605# else
3606 Window srv;
3607
3608 setup_term_clip();
3609# endif
3610
3611 sname = serverMakeName(serverName_arg, argv[0]);
3612 if (sname == NULL)
3613 return;
3614
3615 /*
3616 * Execute the command server related arguments and remove them
3617 * from the argc/argv array; We may have to return into main()
3618 */
3619 for (i = 1; i < Argc; i++)
3620 {
3621 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003622 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003623 {
3624 for (; i < *argc; i++)
3625 {
3626 *newArgV++ = argv[i];
3627 newArgC++;
3628 }
3629 break;
3630 }
3631
Bram Moolenaareb94e552006-03-11 21:35:11 +00003632 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003633 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003634 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3635 {
3636 char *p = argv[i] + 8;
3637
3638 argtype = ARGTYPE_EDIT;
3639 while (*p != NUL)
3640 {
3641 if (STRNICMP(p, "-wait", 5) == 0)
3642 {
3643 argtype = ARGTYPE_EDIT_WAIT;
3644 p += 5;
3645 }
3646 else if (STRNICMP(p, "-silent", 7) == 0)
3647 {
3648 silent = TRUE;
3649 p += 7;
3650 }
3651 else if (STRNICMP(p, "-tab", 4) == 0)
3652 {
3653 tabs = TRUE;
3654 p += 4;
3655 }
3656 else
3657 {
3658 argtype = ARGTYPE_OTHER;
3659 break;
3660 }
3661 }
3662 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003663 else
3664 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003665
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003666 if (argtype != ARGTYPE_OTHER)
3667 {
3668 if (i == *argc - 1)
3669 mainerr_arg_missing((char_u *)argv[i]);
3670 if (argtype == ARGTYPE_SEND)
3671 {
3672 *serverStr = (char_u *)argv[i + 1];
3673 i++;
3674 }
3675 else
3676 {
3677 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003678 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003679 if (*serverStr == NULL)
3680 {
3681 /* Probably out of memory, exit. */
3682 didone = TRUE;
3683 exiterr = 1;
3684 break;
3685 }
3686 Argc = i;
3687 }
3688# ifdef FEAT_X11
3689 if (xterm_dpy == NULL)
3690 {
3691 mch_errmsg(_("No display"));
3692 ret = -1;
3693 }
3694 else
3695 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3696 NULL, &srv, 0, 0, silent);
3697# else
3698 /* Win32 always works? */
3699 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3700# endif
3701 if (ret < 0)
3702 {
3703 if (argtype == ARGTYPE_SEND)
3704 {
3705 /* Failed to send, abort. */
3706 mch_errmsg(_(": Send failed.\n"));
3707 didone = TRUE;
3708 exiterr = 1;
3709 }
3710 else if (!silent)
3711 /* Let vim start normally. */
3712 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3713 break;
3714 }
3715
3716# ifdef FEAT_GUI_W32
3717 /* Guess that when the server name starts with "g" it's a GUI
3718 * server, which we can bring to the foreground here.
3719 * Foreground() in the server doesn't work very well. */
3720 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3721 SetForegroundWindow(srv);
3722# endif
3723
3724 /*
3725 * For --remote-wait: Wait until the server did edit each
3726 * file. Also detect that the server no longer runs.
3727 */
3728 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3729 {
3730 int numFiles = *argc - i - 1;
3731 int j;
3732 char_u *done = alloc(numFiles);
3733 char_u *p;
3734# ifdef FEAT_GUI_W32
3735 NOTIFYICONDATA ni;
3736 int count = 0;
3737 extern HWND message_window;
3738# endif
3739
3740 if (numFiles > 0 && argv[i + 1][0] == '+')
3741 /* Skip "+cmd" argument, don't wait for it to be edited. */
3742 --numFiles;
3743
3744# ifdef FEAT_GUI_W32
3745 ni.cbSize = sizeof(ni);
3746 ni.hWnd = message_window;
3747 ni.uID = 0;
3748 ni.uFlags = NIF_ICON|NIF_TIP;
3749 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3750 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3751 Shell_NotifyIcon(NIM_ADD, &ni);
3752# endif
3753
3754 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003755 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003756 while (memchr(done, 0, numFiles) != NULL)
3757 {
3758# ifdef WIN32
3759 p = serverGetReply(srv, NULL, TRUE, TRUE);
3760 if (p == NULL)
3761 break;
3762# else
3763 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3764 break;
3765# endif
3766 j = atoi((char *)p);
3767 if (j >= 0 && j < numFiles)
3768 {
3769# ifdef FEAT_GUI_W32
3770 ++count;
3771 sprintf(ni.szTip, _("%d of %d edited"),
3772 count, numFiles);
3773 Shell_NotifyIcon(NIM_MODIFY, &ni);
3774# endif
3775 done[j] = 1;
3776 }
3777 }
3778# ifdef FEAT_GUI_W32
3779 Shell_NotifyIcon(NIM_DELETE, &ni);
3780# endif
3781 }
3782 }
3783 else if (STRICMP(argv[i], "--remote-expr") == 0)
3784 {
3785 if (i == *argc - 1)
3786 mainerr_arg_missing((char_u *)argv[i]);
3787# ifdef WIN32
3788 /* Win32 always works? */
3789 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3790 &res, NULL, 1, FALSE) < 0)
3791# else
3792 if (xterm_dpy == NULL)
3793 mch_errmsg(_("No display: Send expression failed.\n"));
3794 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3795 &res, NULL, 1, 1, FALSE) < 0)
3796# endif
3797 {
3798 if (res != NULL && *res != NUL)
3799 {
3800 /* Output error from remote */
3801 mch_errmsg((char *)res);
3802 vim_free(res);
3803 res = NULL;
3804 }
3805 mch_errmsg(_(": Send expression failed.\n"));
3806 }
3807 }
3808 else if (STRICMP(argv[i], "--serverlist") == 0)
3809 {
3810# ifdef WIN32
3811 /* Win32 always works? */
3812 res = serverGetVimNames();
3813# else
3814 if (xterm_dpy != NULL)
3815 res = serverGetVimNames(xterm_dpy);
3816# endif
3817 if (called_emsg)
3818 mch_errmsg("\n");
3819 }
3820 else if (STRICMP(argv[i], "--servername") == 0)
3821 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003822 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003823 i++;
3824 continue;
3825 }
3826 else
3827 {
3828 *newArgV++ = argv[i];
3829 newArgC++;
3830 continue;
3831 }
3832 didone = TRUE;
3833 if (res != NULL && *res != NUL)
3834 {
3835 mch_msg((char *)res);
3836 if (res[STRLEN(res) - 1] != '\n')
3837 mch_msg("\n");
3838 }
3839 vim_free(res);
3840 }
3841
3842 if (didone)
3843 {
3844 display_errors(); /* display any collected messages */
3845 exit(exiterr); /* Mission accomplished - get out */
3846 }
3847
3848 /* Return back into main() */
3849 *argc = newArgC;
3850 vim_free(sname);
3851}
3852
3853/*
3854 * Build a ":drop" command to send to a Vim server.
3855 */
3856 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003857build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003858 int filec;
3859 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003860 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003861 int sendReply;
3862{
3863 garray_T ga;
3864 int i;
3865 char_u *inicmd = NULL;
3866 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003867 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003868
3869 if (filec > 0 && filev[0][0] == '+')
3870 {
3871 inicmd = (char_u *)filev[0] + 1;
3872 filev++;
3873 filec--;
3874 }
3875 /* Check if we have at least one argument. */
3876 if (filec <= 0)
3877 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003878
3879 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003880 cwd = alloc(MAXPATHL);
3881 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003882 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003883 if (mch_dirname(cwd, MAXPATHL) != OK)
3884 {
3885 vim_free(cwd);
3886 return NULL;
3887 }
3888 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003889#ifdef BACKSLASH_IN_FILENAME
3890 "", /* rem_backslash() will tell what chars to escape */
3891#else
3892 PATH_ESC_CHARS,
3893#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003894 '\\', TRUE);
3895 vim_free(cwd);
3896 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003897 return NULL;
3898 ga_init2(&ga, 1, 100);
3899 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3900 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003901 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003902
3903 /* Call inputsave() so that a prompt for an encryption key works. */
3904 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3905 if (tabs)
3906 ga_concat(&ga, (char_u *)"tab ");
3907 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003908 for (i = 0; i < filec; i++)
3909 {
3910 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003911 * do it again in the Vim server. On MS-Windows only escape
3912 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003913 p = vim_strsave_escaped((char_u *)filev[i],
3914#ifdef UNIX
3915 PATH_ESC_CHARS
3916#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003917 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003918#endif
3919 );
3920 if (p == NULL)
3921 {
3922 vim_free(ga.ga_data);
3923 return NULL;
3924 }
3925 ga_concat(&ga, (char_u *)" ");
3926 ga_concat(&ga, p);
3927 vim_free(p);
3928 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003929 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3930
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003931 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3932 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003933 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3934
3935 /* Switch back to the correct current directory (prior to temporary path
3936 * switch) unless 'autochdir' is set, in which case it will already be
3937 * correct after the :drop command. */
3938 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3939
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003940 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003941 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3942 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003943 if (inicmd != NULL)
3944 {
3945 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3946 * the following commands to be inserted as text. Use a "|",
3947 * hopefully "inicmd" does allow this... */
3948 ga_concat(&ga, inicmd);
3949 ga_concat(&ga, (char_u *)"|");
3950 }
3951 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3952 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003953 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003954 ga_append(&ga, NUL);
3955 return ga.ga_data;
3956}
3957
3958/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003959 * Make our basic server name: use the specified "arg" if given, otherwise use
3960 * the tail of the command "cmd" we were started with.
3961 * Return the name in allocated memory. This doesn't include a serial number.
3962 */
3963 static char_u *
3964serverMakeName(arg, cmd)
3965 char_u *arg;
3966 char *cmd;
3967{
3968 char_u *p;
3969
3970 if (arg != NULL && *arg != NUL)
3971 p = vim_strsave_up(arg);
3972 else
3973 {
3974 p = vim_strsave_up(gettail((char_u *)cmd));
3975 /* Remove .exe or .bat from the name. */
3976 if (p != NULL && vim_strchr(p, '.') != NULL)
3977 *vim_strchr(p, '.') = NUL;
3978 }
3979 return p;
3980}
3981#endif /* FEAT_CLIENTSERVER */
3982
3983#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
3984/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003985 * Replace termcodes such as <CR> and insert as key presses if there is room.
3986 */
3987 void
3988server_to_input_buf(str)
3989 char_u *str;
3990{
3991 char_u *ptr = NULL;
3992 char_u *cpo_save = p_cpo;
3993
3994 /* Set 'cpoptions' the way we want it.
3995 * B set - backslashes are *not* treated specially
3996 * k set - keycodes are *not* reverse-engineered
3997 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00003998 * The last but one parameter of replace_termcodes() is TRUE so that the
3999 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004000 */
4001 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004002 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004003 p_cpo = cpo_save;
4004
4005 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4006 {
4007 /*
4008 * Add the string to the input stream.
4009 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4010 *
4011 * First clear typed characters from the typeahead buffer, there could
4012 * be half a mapping there. Then append to the existing string, so
4013 * that multiple commands from a client are concatenated.
4014 */
4015 if (typebuf.tb_maplen < typebuf.tb_len)
4016 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4017 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4018
4019 /* Let input_available() know we inserted text in the typeahead
4020 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004021 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004022 }
4023 vim_free((char_u *)ptr);
4024}
4025
4026/*
4027 * Evaluate an expression that the client sent to a string.
4028 * Handles disabling error messages and disables debugging, otherwise Vim
4029 * hangs, waiting for "cont" to be typed.
4030 */
4031 char_u *
4032eval_client_expr_to_string(expr)
4033 char_u *expr;
4034{
4035 char_u *res;
4036 int save_dbl = debug_break_level;
4037 int save_ro = redir_off;
4038
4039 debug_break_level = -1;
4040 redir_off = 0;
4041 ++emsg_skip;
4042
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004043 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004044
4045 debug_break_level = save_dbl;
4046 redir_off = save_ro;
4047 --emsg_skip;
4048
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004049 /* A client can tell us to redraw, but not to display the cursor, so do
4050 * that here. */
4051 setcursor();
4052 out_flush();
4053#ifdef FEAT_GUI
4054 if (gui.in_use)
4055 gui_update_cursor(FALSE, FALSE);
4056#endif
4057
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004058 return res;
4059}
4060
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004061/*
4062 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4063 * return an allocated string. Otherwise return "data".
4064 * "*tofree" is set to the result when it needs to be freed later.
4065 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004066 char_u *
4067serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004068 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004069 char_u *data;
4070 char_u **tofree;
4071{
4072 char_u *res = data;
4073
4074 *tofree = NULL;
4075# ifdef FEAT_MBYTE
4076 if (client_enc != NULL && p_enc != NULL)
4077 {
4078 vimconv_T vimconv;
4079
4080 vimconv.vc_type = CONV_NONE;
4081 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4082 && vimconv.vc_type != CONV_NONE)
4083 {
4084 res = string_convert(&vimconv, data, NULL);
4085 if (res == NULL)
4086 res = data;
4087 else
4088 *tofree = res;
4089 }
4090 convert_setup(&vimconv, NULL, NULL);
4091 }
4092# endif
4093 return res;
4094}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004095#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004096
4097/*
4098 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4099 */
4100#if defined(FEAT_FKMAP) || defined(PROTO)
4101# include "farsi.c"
4102#endif
4103
4104/*
4105 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4106 */
4107#if defined(FEAT_ARABIC) || defined(PROTO)
4108# include "arabic.c"
4109#endif