blob: b524c7edaac0e3964096282ed2f74e813d336aa0 [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 Moolenaar99685e62013-05-11 13:56:18 +0200195#ifdef FEAT_RUBY
196 {
197 int ruby_stack_start;
198 vim_ruby_init((void *)&ruby_stack_start);
199 }
200#endif
201
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000202#ifdef FEAT_TCL
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000203 vim_tcl_init(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000204#endif
205
206#ifdef MEM_PROFILE
207 atexit(vim_mem_profile_dump);
208#endif
209
210#ifdef STARTUPTIME
Bram Moolenaar3f269672009-11-03 11:11:11 +0000211 for (i = 1; i < argc; ++i)
212 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000213 if (STRICMP(argv[i], "--startuptime") == 0 && i + 1 < argc)
Bram Moolenaar3f269672009-11-03 11:11:11 +0000214 {
Bram Moolenaaref94eec2009-11-11 13:22:11 +0000215 time_fd = mch_fopen(argv[i + 1], "a");
Bram Moolenaar3f269672009-11-03 11:11:11 +0000216 TIME_MSG("--- VIM STARTING ---");
217 break;
218 }
219 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000220#endif
Bram Moolenaarca003e12006-03-17 23:19:38 +0000221 starttime = time(NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000222
223#ifdef __EMX__
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000224 _wildcard(&params.argc, &params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000225#endif
226
227#ifdef FEAT_MBYTE
228 (void)mb_init(); /* init mb_bytelen_tab[] to ones */
229#endif
Bram Moolenaardcaf10e2005-01-21 11:55:25 +0000230#ifdef FEAT_EVAL
231 eval_init(); /* init global variables */
232#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000233
234#ifdef __QNXNTO__
235 qnx_init(); /* PhAttach() for clipboard, (and gui) */
236#endif
237
238#ifdef MAC_OS_CLASSIC
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000239 /* Prepare for possibly starting GUI sometime */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000240 /* Macintosh needs this before any memory is allocated. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000241 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000242 TIME_MSG("GUI prepared");
243#endif
244
245 /* Init the table of Normal mode commands. */
246 init_normal_cmds();
247
248#if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
Bram Moolenaar58d98232005-07-23 22:25:46 +0000249 make_version(); /* Construct the long version string. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000250#endif
251
252 /*
253 * Allocate space for the generic buffers (needed for set_init_1() and
254 * EMSG2()).
255 */
256 if ((IObuff = alloc(IOSIZE)) == NULL
257 || (NameBuff = alloc(MAXPATHL)) == NULL)
258 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000259 TIME_MSG("Allocated generic buffers");
260
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000261#ifdef NBDEBUG
262 /* Wait a moment for debugging NetBeans. Must be after allocating
263 * NameBuff. */
264 nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
265 nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000266 TIME_MSG("NetBeans debug wait");
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000267#endif
268
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000269#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
270 /*
271 * Setup to use the current locale (for ctype() and many other things).
272 * NOTE: Translated messages with encodings other than latin1 will not
273 * work until set_init_1() has been called!
274 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000275 init_locale();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000276 TIME_MSG("locale set");
277#endif
278
279#ifdef FEAT_GUI
280 gui.dofork = TRUE; /* default is to use fork() */
281#endif
282
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000283 /*
Bram Moolenaar58d98232005-07-23 22:25:46 +0000284 * Do a first scan of the arguments in "argv[]":
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000285 * -display or --display
Bram Moolenaar58d98232005-07-23 22:25:46 +0000286 * --server...
287 * --socketid
Bram Moolenaar78e17622007-08-30 10:26:19 +0000288 * --windowid
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000289 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000290 early_arg_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000291
292#ifdef FEAT_SUN_WORKSHOP
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000293 findYourself(params.argv[0]);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000294#endif
295#if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000296 /* Prepare for possibly starting GUI sometime */
297 gui_prepare(&params.argc, params.argv);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000298 TIME_MSG("GUI prepared");
299#endif
300
301#ifdef FEAT_CLIPBOARD
302 clip_init(FALSE); /* Initialise clipboard stuff */
303 TIME_MSG("clipboard setup");
304#endif
305
306 /*
307 * Check if we have an interactive window.
308 * On the Amiga: If there is no window, we open one with a newcli command
309 * (needed for :! to * work). mch_check_win() will also handle the -d or
310 * -dev argument.
311 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000312 params.stdout_isatty = (mch_check_win(params.argc, params.argv) != FAIL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000313 TIME_MSG("window checked");
314
315 /*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000316 * Allocate the first window and buffer.
317 * Can't do anything without it, exit when it fails.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000318 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000319 if (win_alloc_first() == FAIL)
320 mch_exit(0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000321
322 init_yank(); /* init yank buffers */
323
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000324 alist_init(&global_alist); /* Init the argument list to empty. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000325
326 /*
327 * Set the default values for the options.
328 * NOTE: Non-latin1 translated messages are working only after this,
329 * because this is where "has_mbyte" will be set, which is used by
330 * msg_outtrans_len_attr().
331 * First find out the home directory, needed to expand "~" in options.
332 */
333 init_homedir(); /* find real value of $HOME */
334 set_init_1();
335 TIME_MSG("inits 1");
336
337#ifdef FEAT_EVAL
338 set_lang_var(); /* set v:lang and v:ctype */
339#endif
340
341#ifdef FEAT_CLIENTSERVER
342 /*
343 * Do the client-server stuff, unless "--servername ''" was used.
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000344 * This may exit Vim if the command was sent to the server.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000345 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000346 exec_on_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000347#endif
348
349 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000350 * Figure out the way to work from the command name argv[0].
351 * "vimdiff" starts diff mode, "rvim" sets "restricted", etc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000352 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000353 parse_command_name(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000354
355 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000356 * Process the command line arguments. File names are put in the global
357 * argument list "global_alist".
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000358 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000359 command_line_scan(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000360 TIME_MSG("parsing arguments");
361
362 /*
363 * On some systems, when we compile with the GUI, we always use it. On Mac
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000364 * there is no terminal version, and on Windows we can't fork one off with
365 * :gui.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000366 */
367#ifdef ALWAYS_USE_GUI
368 gui.starting = TRUE;
369#else
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000370# if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000371 /*
372 * Check if the GUI can be started. Reset gui.starting if not.
373 * Don't know about other systems, stay on the safe side and don't check.
374 */
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000375 if (gui.starting)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000376 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000377 if (gui_init_check() == FAIL)
378 {
379 gui.starting = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000380
Bram Moolenaar5d985b92009-12-16 17:28:07 +0000381 /* When running "evim" or "gvim -y" we need the menus, exit if we
382 * don't have them. */
383 if (params.evim_mode)
384 mch_exit(1);
385 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000386 }
387# endif
388#endif
389
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000390 if (GARGCOUNT > 0)
391 {
392#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
393 /*
394 * Expand wildcards in file names.
395 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000396 if (!params.literal)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000397 {
398 /* Temporarily add '(' and ')' to 'isfname'. These are valid
399 * filename characters but are excluded from 'isfname' to make
400 * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
401 do_cmdline_cmd((char_u *)":set isf+=(,)");
Bram Moolenaar86b68352004-12-27 21:59:20 +0000402 alist_expand(NULL, 0);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000403 do_cmdline_cmd((char_u *)":set isf&");
404 }
405#endif
406 fname = alist_name(&GARGLIST[0]);
407 }
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000408
409#if defined(WIN32) && defined(FEAT_MBYTE)
410 {
411 extern void set_alist_count(void);
412
413 /* Remember the number of entries in the argument list. If it changes
414 * we don't react on setting 'encoding'. */
415 set_alist_count();
416 }
417#endif
418
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000419#ifdef MSWIN
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000420 if (GARGCOUNT == 1 && params.full_path)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000421 {
422 /*
423 * If there is one filename, fully qualified, we have very probably
424 * been invoked from explorer, so change to the file's directory.
425 * Hint: to avoid this when typing a command use a forward slash.
426 * If the cd fails, it doesn't matter.
427 */
428 (void)vim_chdirfile(fname);
429 }
430#endif
431 TIME_MSG("expanding arguments");
432
433#ifdef FEAT_DIFF
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000434 if (params.diff_mode && params.window_count == -1)
435 params.window_count = 0; /* open up to 3 windows */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000436#endif
437
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000438 /* Don't redraw until much later. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000439 ++RedrawingDisabled;
440
441 /*
442 * When listing swap file names, don't do cursor positioning et. al.
443 */
444 if (recoverymode && fname == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000445 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000446
447 /*
448 * When certain to start the GUI, don't check capabilities of terminal.
449 * For GTK we can't be sure, but when started from the desktop it doesn't
450 * make sense to try using a terminal.
451 */
Bram Moolenaar241a8aa2005-12-06 20:04:44 +0000452#if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000453 if (gui.starting
454# ifdef FEAT_GUI_GTK
455 && !isatty(2)
456# endif
457 )
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000458 params.want_full_screen = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000459#endif
460
461#if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
462 /* When the GUI is started from Finder, need to display messages in a
463 * message box. isatty(2) returns TRUE anyway, thus we need to check the
464 * name to know we're not started from a terminal. */
465 if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000466 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000467 params.want_full_screen = FALSE;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +0000468
469 /* Avoid always using "/" as the current directory. Note that when
470 * started from Finder the arglist will be filled later in
471 * HandleODocAE() and "fname" will be NULL. */
472 if (getcwd((char *)NameBuff, MAXPATHL) != NULL
473 && STRCMP(NameBuff, "/") == 0)
474 {
475 if (fname != NULL)
476 (void)vim_chdirfile(fname);
477 else
478 {
479 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
480 vim_chdir(NameBuff);
481 }
482 }
483 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000484#endif
485
486 /*
487 * mch_init() sets up the terminal (window) for use. This must be
488 * done after resetting full_screen, otherwise it may move the cursor
489 * (MSDOS).
490 * Note that we may use mch_exit() before mch_init()!
491 */
492 mch_init();
493 TIME_MSG("shell init");
494
495#ifdef USE_XSMP
496 /*
497 * For want of anywhere else to do it, try to connect to xsmp here.
498 * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
499 * Hijacking -X 'no X connection' to also disable XSMP connection as that
500 * has a similar delay upon failure.
501 * Only try if SESSION_MANAGER is set to something non-null.
502 */
503 if (!x_no_connect)
504 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000505 char *p = getenv("SESSION_MANAGER");
506
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000507 if (p != NULL && *p != NUL)
508 {
509 xsmp_init();
510 TIME_MSG("xsmp init");
511 }
512 }
513#endif
514
515 /*
516 * Print a warning if stdout is not a terminal.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000517 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000518 check_tty(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000519
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000520 /* This message comes before term inits, but after setting "silent_mode"
521 * when the input is not a tty. */
522 if (GARGCOUNT > 1 && !silent_mode)
523 printf(_("%d files to edit\n"), GARGCOUNT);
524
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000525 if (params.want_full_screen && !silent_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000526 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000527 termcapinit(params.term); /* set terminal name and get terminal
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000528 capabilities (will set full_screen) */
529 screen_start(); /* don't know where cursor is now */
530 TIME_MSG("Termcap init");
531 }
532
533 /*
534 * Set the default values for the options that use Rows and Columns.
535 */
536 ui_get_shellsize(); /* inits Rows and Columns */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000537 win_init_size();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000538#ifdef FEAT_DIFF
539 /* Set the 'diff' option now, so that it can be checked for in a .vimrc
540 * file. There is no buffer yet though. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000541 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000542 diff_win_options(firstwin, FALSE);
543#endif
544
545 cmdline_row = Rows - p_ch;
546 msg_row = cmdline_row;
547 screenalloc(FALSE); /* allocate screen buffers */
548 set_init_2();
549 TIME_MSG("inits 2");
550
551 msg_scroll = TRUE;
552 no_wait_return = TRUE;
553
554 init_mappings(); /* set up initial mappings */
555
556 init_highlight(TRUE, FALSE); /* set the default highlight groups */
557 TIME_MSG("init highlight");
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000558
559#ifdef FEAT_EVAL
560 /* Set the break level after the terminal is initialized. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000561 debug_break_level = params.use_debug_break_level;
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000562#endif
563
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100564#ifdef FEAT_MZSCHEME
565 /*
566 * Newer version of MzScheme (Racket) require earlier (trampolined)
567 * initialisation via scheme_main_setup.
568 * Implement this by initialising it as early as possible
569 * and splitting off remaining Vim main into vim_main2
570 */
571 {
572 /* Pack up preprocessed command line arguments.
573 * It is safe because Scheme does not access argc/argv. */
574 char *args[2];
575 args[0] = (char *)fname;
576 args[1] = (char *)&params;
577 return mzscheme_main(2, args);
578 }
579}
Bram Moolenaar8866d272012-11-28 15:55:42 +0100580#endif
581#endif /* NO_VIM_MAIN */
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100582
Bram Moolenaar8866d272012-11-28 15:55:42 +0100583/* vim_main2() needs to be produced when FEAT_MZSCHEME is defined even when
584 * NO_VIM_MAIN is defined. */
585#ifdef FEAT_MZSCHEME
586 int
587vim_main2(int argc UNUSED, char **argv UNUSED)
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100588{
Bram Moolenaar8866d272012-11-28 15:55:42 +0100589# ifndef NO_VIM_MAIN
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100590 char_u *fname = (char_u *)argv[0];
591 mparm_T params;
592
593 memcpy(&params, argv[1], sizeof(params));
Bram Moolenaar8866d272012-11-28 15:55:42 +0100594# else
595 return 0;
596}
597# endif
Bram Moolenaarbbc98db2012-02-12 01:55:55 +0100598#endif
599
Bram Moolenaar8866d272012-11-28 15:55:42 +0100600#ifndef NO_VIM_MAIN
Bram Moolenaar58d98232005-07-23 22:25:46 +0000601 /* Execute --cmd arguments. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000602 exe_pre_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000603
Bram Moolenaar58d98232005-07-23 22:25:46 +0000604 /* Source startup scripts. */
605 source_startup_scripts(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000606
607#ifdef FEAT_EVAL
608 /*
609 * Read all the plugin files.
610 * Only when compiled with +eval, since most plugins need it.
611 */
612 if (p_lpl)
613 {
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000614# ifdef VMS /* Somehow VMS doesn't handle the "**". */
615 source_runtime((char_u *)"plugin/*.vim", TRUE);
616# else
Bram Moolenaar07d4d732005-10-03 22:04:08 +0000617 source_runtime((char_u *)"plugin/**/*.vim", TRUE);
Bram Moolenaarc1cb78c2006-06-20 16:51:47 +0000618# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000619 TIME_MSG("loading plugins");
620 }
621#endif
622
Bram Moolenaar27dc1952006-03-15 23:06:44 +0000623#ifdef FEAT_DIFF
624 /* Decide about window layout for diff mode after reading vimrc. */
625 if (params.diff_mode && params.window_layout == 0)
626 {
627 if (diffopt_horizontal())
628 params.window_layout = WIN_HOR; /* use horizontal split */
629 else
630 params.window_layout = WIN_VER; /* use vertical split */
631 }
632#endif
633
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000634 /*
635 * Recovery mode without a file name: List swap files.
636 * This uses the 'dir' option, therefore it must be after the
637 * initializations.
638 */
639 if (recoverymode && fname == NULL)
640 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200641 recover_names(NULL, TRUE, 0, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000642 mch_exit(0);
643 }
644
645 /*
646 * Set a few option defaults after reading .vimrc files:
647 * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
648 */
649 set_init_3();
650 TIME_MSG("inits 3");
651
652 /*
653 * "-n" argument: Disable swap file by setting 'updatecount' to 0.
654 * Note that this overrides anything from a vimrc file.
655 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000656 if (params.no_swap_file)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000657 p_uc = 0;
658
659#ifdef FEAT_FKMAP
660 if (curwin->w_p_rl && p_altkeymap)
661 {
662 p_hkmap = FALSE; /* Reset the Hebrew keymap mode */
663# ifdef FEAT_ARABIC
664 curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
665# endif
666 p_fkmap = TRUE; /* Set the Farsi keymap mode */
667 }
668#endif
669
670#ifdef FEAT_GUI
671 if (gui.starting)
672 {
673#if defined(UNIX) || defined(VMS)
674 /* When something caused a message from a vimrc script, need to output
675 * an extra newline before the shell prompt. */
676 if (did_emsg || msg_didout)
677 putchar('\n');
678#endif
679
680 gui_start(); /* will set full_screen to TRUE */
681 TIME_MSG("starting GUI");
682
683 /* When running "evim" or "gvim -y" we need the menus, exit if we
684 * don't have them. */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000685 if (!gui.in_use && params.evim_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000686 mch_exit(1);
687 }
688#endif
689
690#ifdef SPAWNO /* special MSDOS swapping library */
691 init_SPAWNO("", SWAP_ANY);
692#endif
693
694#ifdef FEAT_VIMINFO
695 /*
Bram Moolenaard812df62008-11-09 12:46:09 +0000696 * Read in registers, history etc, but not marks, from the viminfo file.
697 * This is where v:oldfiles gets filled.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000698 */
699 if (*p_viminfo != NUL)
700 {
Bram Moolenaard812df62008-11-09 12:46:09 +0000701 read_viminfo(NULL, VIF_WANT_INFO | VIF_GET_OLDFILES);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000702 TIME_MSG("reading viminfo");
703 }
704#endif
705
706#ifdef FEAT_QUICKFIX
707 /*
708 * "-q errorfile": Load the error file now.
709 * If the error file can't be read, exit before doing anything else.
710 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000711 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000712 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000713 if (params.use_ef != NULL)
714 set_string_option_direct((char_u *)"ef", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000715 params.use_ef, OPT_FREE, SID_CARG);
Bram Moolenaar7fd73202010-07-25 16:58:46 +0200716 vim_snprintf((char *)IObuff, IOSIZE, "cfile %s", p_ef);
717 if (qf_init(NULL, p_ef, p_efm, TRUE, IObuff) < 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000718 {
719 out_char('\n');
720 mch_exit(3);
721 }
722 TIME_MSG("reading errorfile");
723 }
724#endif
725
726 /*
727 * Start putting things on the screen.
728 * Scroll screen down before drawing over it
729 * Clear screen now, so file message will not be cleared.
730 */
731 starting = NO_BUFFERS;
732 no_wait_return = FALSE;
733 if (!exmode_active)
734 msg_scroll = FALSE;
735
736#ifdef FEAT_GUI
737 /*
738 * This seems to be required to make callbacks to be called now, instead
739 * of after things have been put on the screen, which then may be deleted
740 * when getting a resize callback.
741 * For the Mac this handles putting files dropped on the Vim icon to
742 * global_alist.
743 */
744 if (gui.in_use)
745 {
746# ifdef FEAT_SUN_WORKSHOP
747 if (!usingSunWorkShop)
748# endif
749 gui_wait_for_chars(50L);
750 TIME_MSG("GUI delay");
751 }
752#endif
753
754#if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
755 qnx_clip_init();
756#endif
757
Bram Moolenaarc8bbaa32010-07-14 16:54:21 +0200758#if defined(MACOS_X) && defined(FEAT_CLIPBOARD)
759 clip_init(TRUE);
760#endif
761
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000762#ifdef FEAT_XCLIPBOARD
763 /* Start using the X clipboard, unless the GUI was started. */
764# ifdef FEAT_GUI
765 if (!gui.in_use)
766# endif
767 {
768 setup_term_clip();
769 TIME_MSG("setup clipboard");
770 }
771#endif
772
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000773#ifdef FEAT_CLIENTSERVER
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000774 /* Prepare for being a Vim server. */
775 prepare_server(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000776#endif
777
778 /*
779 * If "-" argument given: Read file from stdin.
780 * Do this before starting Raw mode, because it may change things that the
781 * writing end of the pipe doesn't like, e.g., in case stdin and stderr
782 * are the same terminal: "cat | vim -".
783 * Using autocommands here may cause trouble...
784 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000785 if (params.edit_type == EDIT_STDIN && !recoverymode)
786 read_stdin();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000787
788#if defined(UNIX) || defined(VMS)
789 /* When switching screens and something caused a message from a vimrc
790 * script, need to output an extra newline on exit. */
791 if ((did_emsg || msg_didout) && *T_TI != NUL)
792 newline_on_exit = TRUE;
793#endif
794
795 /*
796 * When done something that is not allowed or error message call
797 * wait_return. This must be done before starttermcap(), because it may
798 * switch to another screen. It must be done after settmode(TMODE_RAW),
799 * because we want to react on a single key stroke.
800 * Call settmode and starttermcap here, so the T_KS and T_TI may be
Bram Moolenaar49325942007-05-10 19:19:59 +0000801 * defined by termcapinit and redefined in .exrc.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000802 */
803 settmode(TMODE_RAW);
804 TIME_MSG("setting raw mode");
805
806 if (need_wait_return || msg_didany)
807 {
808 wait_return(TRUE);
809 TIME_MSG("waiting for return");
810 }
811
812 starttermcap(); /* start termcap if not done by wait_return() */
813 TIME_MSG("start termcap");
Bram Moolenaar9584b312013-03-13 19:29:28 +0100814#if defined(FEAT_TERMRESPONSE) && defined(FEAT_MBYTE)
815 may_req_ambiguous_character_width();
816#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000817
818#ifdef FEAT_MOUSE
819 setmouse(); /* may start using the mouse */
820#endif
821 if (scroll_region)
822 scroll_region_reset(); /* In case Rows changed */
Bram Moolenaar58d98232005-07-23 22:25:46 +0000823 scroll_start(); /* may scroll the screen to the right position */
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000824
825 /*
826 * Don't clear the screen when starting in Ex mode, unless using the GUI.
827 */
828 if (exmode_active
829#ifdef FEAT_GUI
830 && !gui.in_use
831#endif
832 )
833 must_redraw = CLEAR;
834 else
835 {
836 screenclear(); /* clear screen */
837 TIME_MSG("clearing screen");
838 }
839
840#ifdef FEAT_CRYPT
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000841 if (params.ask_for_key)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000842 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +0200843 (void)blowfish_self_test();
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000844 (void)get_crypt_key(TRUE, TRUE);
845 TIME_MSG("getting crypt key");
846 }
847#endif
848
849 no_wait_return = TRUE;
850
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000851 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000852 * Create the requested number of windows and edit buffers in them.
853 * Also does recovery if "recoverymode" set.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000854 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000855 create_windows(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000856 TIME_MSG("opening buffers");
857
Bram Moolenaar867a4b72007-03-18 20:51:46 +0000858#ifdef FEAT_EVAL
859 /* clear v:swapcommand */
860 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
861#endif
862
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000863 /* Ex starts at last line of the file */
864 if (exmode_active)
865 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
866
867#ifdef FEAT_AUTOCMD
868 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
869 TIME_MSG("BufEnter autocommands");
870#endif
871 setpcmark();
872
873#ifdef FEAT_QUICKFIX
874 /*
875 * When started with "-q errorfile" jump to first error now.
876 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000877 if (params.edit_type == EDIT_QF)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000878 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000879 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000880 TIME_MSG("jump to first error");
881 }
882#endif
883
884#ifdef FEAT_WINDOWS
885 /*
886 * If opened more than one window, start editing files in the other
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000887 * windows.
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000888 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000889 edit_buffers(&params);
890#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000891
892#ifdef FEAT_DIFF
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000893 if (params.diff_mode)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000894 {
895 win_T *wp;
896
897 /* set options in each window for "vimdiff". */
898 for (wp = firstwin; wp != NULL; wp = wp->w_next)
899 diff_win_options(wp, TRUE);
900 }
901#endif
902
903 /*
904 * Shorten any of the filenames, but only when absolute.
905 */
906 shorten_fnames(FALSE);
907
908 /*
909 * Need to jump to the tag before executing the '-c command'.
910 * Makes "vim -c '/return' -t main" work.
911 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000912 if (params.tagname != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000913 {
Bram Moolenaar146522e2005-12-16 21:55:46 +0000914#if defined(HAS_SWAP_EXISTS_ACTION)
915 swap_exists_did_quit = FALSE;
916#endif
917
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000918 vim_snprintf((char *)IObuff, IOSIZE, "ta %s", params.tagname);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000919 do_cmdline_cmd(IObuff);
920 TIME_MSG("jumping to tag");
Bram Moolenaar146522e2005-12-16 21:55:46 +0000921
922#if defined(HAS_SWAP_EXISTS_ACTION)
923 /* If the user doesn't want to edit the file then we quit here. */
924 if (swap_exists_did_quit)
925 getout(1);
926#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000927 }
928
Bram Moolenaarc013cb62005-07-24 21:18:31 +0000929 /* Execute any "+", "-c" and "-S" arguments. */
930 if (params.n_commands > 0)
931 exe_commands(&params);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000932
933 RedrawingDisabled = 0;
934 redraw_all_later(NOT_VALID);
935 no_wait_return = FALSE;
936 starting = 0;
937
Bram Moolenaara40ceaf2006-01-13 22:35:40 +0000938#ifdef FEAT_TERMRESPONSE
939 /* Requesting the termresponse is postponed until here, so that a "-c q"
940 * argument doesn't make it appear in the shell Vim was started from. */
941 may_req_termresponse();
942#endif
943
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000944 /* start in insert mode */
945 if (p_im)
946 need_start_insertmode = TRUE;
947
948#ifdef FEAT_AUTOCMD
949 apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
950 TIME_MSG("VimEnter autocommands");
951#endif
952
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200953#if defined(FEAT_EVAL) && defined(FEAT_CLIPBOARD)
954 /* Adjust default register name for "unnamed" in 'clipboard'. Can only be
955 * done after the clipboard is available and all initial commands that may
956 * modify the 'clipboard' setting have run; i.e. just before entering the
957 * main loop. */
958 {
959 int default_regname = 0;
960 adjust_clip_reg(&default_regname);
961 set_reg_var(default_regname);
962 }
963#endif
964
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000965#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
966 /* When a startup script or session file setup for diff'ing and
967 * scrollbind, sync the scrollbind now. */
968 if (curwin->w_p_diff && curwin->w_p_scb)
969 {
970 update_topline();
971 check_scrollbind((linenr_T)0, 0L);
972 TIME_MSG("diff scrollbinding");
973 }
974#endif
975
976#if defined(WIN3264) && !defined(FEAT_GUI_W32)
977 mch_set_winsize_now(); /* Allow winsize changes from now on */
978#endif
979
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000980#if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
981 /* When tab pages were created, may need to update the tab pages line and
982 * scrollbars. This is skipped while creating them. */
983 if (first_tabpage->tp_next != NULL)
984 {
985 out_flush();
986 gui_init_which_components(NULL);
987 gui_update_scrollbars(TRUE);
988 }
989 need_mouse_correct = TRUE;
990#endif
991
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000992 /* If ":startinsert" command used, stuff a dummy command to be able to
993 * call normal_cmd(), which will then start Insert mode. */
994 if (restart_edit != 0)
Bram Moolenaarebefac62005-12-28 22:39:57 +0000995 stuffcharReadbuff(K_NOP);
Bram Moolenaarb4210b32004-06-13 14:51:16 +0000996
997#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200998 if (netbeansArg != NULL && strncmp("-nb", netbeansArg, 3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +0200999 {
1000# ifdef FEAT_GUI
Bram Moolenaar173c9852010-09-29 17:27:01 +02001001# if !defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK) \
Bram Moolenaar67c53842010-05-22 18:28:27 +02001002 && !defined(FEAT_GUI_W32)
1003 if (gui.in_use)
1004 {
1005 mch_errmsg(_("netbeans is not supported with this GUI\n"));
1006 mch_exit(2);
1007 }
1008# endif
1009# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001010 /* Tell the client that it can start sending commands. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001011 netbeans_open(netbeansArg + 3, TRUE);
Bram Moolenaar67c53842010-05-22 18:28:27 +02001012 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001013#endif
1014
1015 TIME_MSG("before starting main loop");
1016
1017 /*
1018 * Call the main command loop. This never returns.
Bram Moolenaarbbc98db2012-02-12 01:55:55 +01001019 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001020 main_loop(FALSE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001021
1022 return 0;
1023}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001024#endif /* NO_VIM_MAIN */
Bram Moolenaar8866d272012-11-28 15:55:42 +01001025#endif /* PROTO */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001026
1027/*
1028 * Main loop: Execute Normal mode commands until exiting Vim.
1029 * Also used to handle commands in the command-line window, until the window
1030 * is closed.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001031 * Also used to handle ":visual" command after ":global": execute Normal mode
1032 * commands, return when entering Ex mode. "noexmode" is TRUE then.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001033 */
1034 void
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001035main_loop(cmdwin, noexmode)
1036 int cmdwin; /* TRUE when working in the command-line window */
1037 int noexmode; /* TRUE when return on entering Ex mode */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001038{
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001039 oparg_T oa; /* operator arguments */
1040 int previous_got_int = FALSE; /* "got_int" was TRUE */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001041#ifdef FEAT_CONCEAL
1042 linenr_T conceal_old_cursor_line = 0;
1043 linenr_T conceal_new_cursor_line = 0;
1044 int conceal_update_lines = FALSE;
1045#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001046
1047#if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
1048 /* Setup to catch a terminating error from the X server. Just ignore
1049 * it, restore the state and continue. This might not always work
1050 * properly, but at least we don't exit unexpectedly when the X server
1051 * exists while Vim is running in a console. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 if (!cmdwin && !noexmode && SETJMP(x_jump_env))
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001053 {
1054 State = NORMAL;
1055# ifdef FEAT_VISUAL
1056 VIsual_active = FALSE;
1057# endif
1058 got_int = TRUE;
1059 need_wait_return = FALSE;
1060 global_busy = FALSE;
1061 exmode_active = 0;
1062 skip_redraw = FALSE;
1063 RedrawingDisabled = 0;
1064 no_wait_return = 0;
Bram Moolenaar7701c242011-10-04 16:43:53 +02001065 vgetc_busy = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001066# ifdef FEAT_EVAL
1067 emsg_skip = 0;
1068# endif
1069 emsg_off = 0;
1070# ifdef FEAT_MOUSE
1071 setmouse();
1072# endif
1073 settmode(TMODE_RAW);
1074 starttermcap();
1075 scroll_start();
1076 redraw_later_clear();
1077 }
1078#endif
1079
1080 clear_oparg(&oa);
1081 while (!cmdwin
1082#ifdef FEAT_CMDWIN
1083 || cmdwin_result == 0
1084#endif
1085 )
1086 {
1087 if (stuff_empty())
1088 {
1089 did_check_timestamps = FALSE;
1090 if (need_check_timestamps)
1091 check_timestamps(FALSE);
1092 if (need_wait_return) /* if wait_return still needed ... */
1093 wait_return(FALSE); /* ... call it now */
1094 if (need_start_insertmode && goto_im()
1095#ifdef FEAT_VISUAL
1096 && !VIsual_active
1097#endif
1098 )
1099 {
1100 need_start_insertmode = FALSE;
1101 stuffReadbuff((char_u *)"i"); /* start insert mode next */
1102 /* skip the fileinfo message now, because it would be shown
1103 * after insert mode finishes! */
1104 need_fileinfo = FALSE;
1105 }
1106 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001107
1108 /* Reset "got_int" now that we got back to the main loop. Except when
1109 * inside a ":g/pat/cmd" command, then the "got_int" needs to abort
1110 * the ":g" command.
1111 * For ":g/pat/vi" we reset "got_int" when used once. When used
1112 * a second time we go back to Ex mode and abort the ":g" command. */
1113 if (got_int)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001114 {
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001115 if (noexmode && global_busy && !exmode_active && previous_got_int)
1116 {
1117 /* Typed two CTRL-C in a row: go back to ex mode as if "Q" was
1118 * used and keep "got_int" set, so that it aborts ":g". */
1119 exmode_active = EXMODE_NORMAL;
1120 State = NORMAL;
1121 }
1122 else if (!global_busy || !exmode_active)
1123 {
1124 if (!quit_more)
1125 (void)vgetc(); /* flush all buffers */
1126 got_int = FALSE;
1127 }
1128 previous_got_int = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001129 }
Bram Moolenaar225d32b2007-08-10 19:33:47 +00001130 else
1131 previous_got_int = FALSE;
1132
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001133 if (!exmode_active)
1134 msg_scroll = FALSE;
1135 quit_more = FALSE;
1136
1137 /*
1138 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
1139 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
1140 * update cursor and redraw.
1141 */
1142 if (skip_redraw || exmode_active)
1143 skip_redraw = FALSE;
1144 else if (do_redraw || stuff_empty())
1145 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001146#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001147 /* Trigger CursorMoved if the cursor moved. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001148 if (!finish_op && (
1149# ifdef FEAT_AUTOCMD
1150 has_cursormoved()
1151# endif
1152# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1153 ||
1154# endif
1155# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001156 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001157# endif
1158 )
1159 && !equalpos(last_cursormoved, curwin->w_cursor))
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001160 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001161# ifdef FEAT_AUTOCMD
1162 if (has_cursormoved())
1163 apply_autocmds(EVENT_CURSORMOVED, NULL, NULL,
1164 FALSE, curbuf);
1165# endif
1166# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001167 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001168 {
1169 conceal_old_cursor_line = last_cursormoved.lnum;
1170 conceal_new_cursor_line = curwin->w_cursor.lnum;
1171 conceal_update_lines = TRUE;
1172 }
1173# endif
Bram Moolenaar3d0a6032006-02-09 23:54:54 +00001174 last_cursormoved = curwin->w_cursor;
1175 }
1176#endif
1177
Bram Moolenaar186628f2013-03-19 13:33:23 +01001178#ifdef FEAT_AUTOCMD
1179 /* Trigger TextChanged if b_changedtick differs. */
1180 if (!finish_op && has_textchanged()
1181 && last_changedtick != curbuf->b_changedtick)
1182 {
1183 if (last_changedtick_buf == curbuf)
1184 apply_autocmds(EVENT_TEXTCHANGED, NULL, NULL,
1185 FALSE, curbuf);
1186 last_changedtick_buf = curbuf;
1187 last_changedtick = curbuf->b_changedtick;
1188 }
1189#endif
1190
Bram Moolenaar33aec762006-01-22 23:30:12 +00001191#if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1192 /* Scroll-binding for diff mode may have been postponed until
1193 * here. Avoids doing it for every change. */
1194 if (diff_need_scrollbind)
1195 {
1196 check_scrollbind((linenr_T)0, 0L);
1197 diff_need_scrollbind = FALSE;
1198 }
1199#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001200#if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
1201 /* Include a closed fold completely in the Visual area. */
1202 foldAdjustVisual();
1203#endif
1204#ifdef FEAT_FOLDING
1205 /*
1206 * When 'foldclose' is set, apply 'foldlevel' to folds that don't
1207 * contain the cursor.
1208 * When 'foldopen' is "all", open the fold(s) under the cursor.
1209 * This may mark the window for redrawing.
1210 */
1211 if (hasAnyFolding(curwin) && !char_avail())
1212 {
1213 foldCheckClose();
1214 if (fdo_flags & FDO_ALL)
1215 foldOpenCursor();
1216 }
1217#endif
1218
1219 /*
1220 * Before redrawing, make sure w_topline is correct, and w_leftcol
1221 * if lines don't wrap, and w_skipcol if lines wrap.
1222 */
1223 update_topline();
1224 validate_cursor();
1225
1226#ifdef FEAT_VISUAL
1227 if (VIsual_active)
1228 update_curbuf(INVERTED);/* update inverted part */
1229 else
1230#endif
1231 if (must_redraw)
1232 update_screen(0);
1233 else if (redraw_cmdline || clear_cmdline)
1234 showmode();
1235#ifdef FEAT_WINDOWS
1236 redraw_statuslines();
1237#endif
1238#ifdef FEAT_TITLE
1239 if (need_maketitle)
1240 maketitle();
1241#endif
1242 /* display message after redraw */
1243 if (keep_msg != NULL)
1244 {
1245 char_u *p;
1246
1247 /* msg_attr_keep() will set keep_msg to NULL, must free the
1248 * string here. */
1249 p = keep_msg;
Bram Moolenaar238a5642006-02-21 22:12:05 +00001250 keep_msg = NULL;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001251 msg_attr(p, keep_msg_attr);
1252 vim_free(p);
1253 }
1254 if (need_fileinfo) /* show file info after redraw */
1255 {
1256 fileinfo(FALSE, TRUE, FALSE);
1257 need_fileinfo = FALSE;
1258 }
1259
1260 emsg_on_display = FALSE; /* can delete error message now */
1261 did_emsg = FALSE;
1262 msg_didany = FALSE; /* reset lines_left in msg_start() */
Bram Moolenaar661b1822005-07-28 22:36:45 +00001263 may_clear_sb_text(); /* clear scroll-back text on next msg */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001264 showruler(FALSE);
1265
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001266# if defined(FEAT_CONCEAL)
1267 if (conceal_update_lines
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001268 && (conceal_old_cursor_line != conceal_new_cursor_line
1269 || conceal_cursor_line(curwin)
1270 || need_cursor_line_redraw))
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001271 {
Bram Moolenaarc2b4c622011-02-15 16:29:59 +01001272 if (conceal_old_cursor_line != conceal_new_cursor_line
1273 && conceal_old_cursor_line
1274 <= curbuf->b_ml.ml_line_count)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001275 update_single_line(curwin, conceal_old_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001276 update_single_line(curwin, conceal_new_cursor_line);
1277 curwin->w_valid &= ~VALID_CROW;
1278 }
1279# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001280 setcursor();
1281 cursor_on();
1282
1283 do_redraw = FALSE;
Bram Moolenaar3f269672009-11-03 11:11:11 +00001284
1285#ifdef STARTUPTIME
1286 /* Now that we have drawn the first screen all the startup stuff
1287 * has been done, close any file for startup messages. */
1288 if (time_fd != NULL)
1289 {
1290 TIME_MSG("first screen update");
1291 TIME_MSG("--- VIM STARTED ---");
1292 fclose(time_fd);
1293 time_fd = NULL;
1294 }
1295#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001296 }
1297#ifdef FEAT_GUI
1298 if (need_mouse_correct)
1299 gui_mouse_correct();
1300#endif
1301
1302 /*
1303 * Update w_curswant if w_set_curswant has been set.
1304 * Postponed until here to avoid computing w_virtcol too often.
1305 */
1306 update_curswant();
1307
Bram Moolenaar9fecb462006-09-05 10:59:47 +00001308#ifdef FEAT_EVAL
1309 /*
1310 * May perform garbage collection when waiting for a character, but
1311 * only at the very toplevel. Otherwise we may be using a List or
1312 * Dict internally somewhere.
1313 * "may_garbage_collect" is reset in vgetc() which is invoked through
1314 * do_exmode() and normal_cmd().
1315 */
1316 may_garbage_collect = (!cmdwin && !noexmode);
1317#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001318 /*
1319 * If we're invoked as ex, do a round of ex commands.
1320 * Otherwise, get and execute a normal mode command.
1321 */
1322 if (exmode_active)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001323 {
1324 if (noexmode) /* End of ":global/path/visual" commands */
1325 return;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001326 do_exmode(exmode_active == EXMODE_VIM);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001327 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001328 else
1329 normal_cmd(&oa, TRUE);
1330 }
1331}
1332
1333
1334#if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
1335/*
1336 * Exit, but leave behind swap files for modified buffers.
1337 */
1338 void
1339getout_preserve_modified(exitval)
1340 int exitval;
1341{
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001342# if defined(SIGHUP) && defined(SIG_IGN)
1343 /* Ignore SIGHUP, because a dropped connection causes a read error, which
1344 * makes Vim exit and then handling SIGHUP causes various reentrance
1345 * problems. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001346 signal(SIGHUP, SIG_IGN);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001347# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001348
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001349 ml_close_notmod(); /* close all not-modified buffers */
1350 ml_sync_all(FALSE, FALSE); /* preserve all swap files */
1351 ml_close_all(FALSE); /* close all memfiles, without deleting */
1352 getout(exitval); /* exit Vim properly */
1353}
1354#endif
1355
1356
1357/* Exit properly */
1358 void
1359getout(exitval)
1360 int exitval;
1361{
1362#ifdef FEAT_AUTOCMD
1363 buf_T *buf;
1364 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00001365 tabpage_T *tp, *next_tp;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001366#endif
1367
1368 exiting = TRUE;
1369
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001370 /* When running in Ex mode an error causes us to exit with a non-zero exit
1371 * code. POSIX requires this, although it's not 100% clear from the
1372 * standard. */
1373 if (exmode_active)
1374 exitval += ex_exitval;
1375
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001376 /* Position the cursor on the last screen line, below all the text */
1377#ifdef FEAT_GUI
1378 if (!gui.in_use)
1379#endif
1380 windgoto((int)Rows - 1, 0);
1381
Bram Moolenaar0e21a3f2005-04-17 20:28:32 +00001382#if defined(FEAT_EVAL) || defined(FEAT_SYN_HL)
1383 /* Optionally print hashtable efficiency. */
1384 hash_debug_results();
1385#endif
1386
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001387#ifdef FEAT_GUI
1388 msg_didany = FALSE;
1389#endif
1390
1391#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001392 if (get_vim_var_nr(VV_DYING) <= 1)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001393 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001394 /* Trigger BufWinLeave for all windows, but only once per buffer. */
1395# if defined FEAT_WINDOWS
1396 for (tp = first_tabpage; tp != NULL; tp = next_tp)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001397 {
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001398 next_tp = tp->tp_next;
1399 for (wp = (tp == curtab)
1400 ? firstwin : tp->tp_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001401 {
Bram Moolenaar802418d2013-01-17 14:00:11 +01001402 if (wp->w_buffer == NULL)
1403 /* Autocmd must have close the buffer already, skip. */
1404 continue;
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001405 buf = wp->w_buffer;
1406 if (buf->b_changedtick != -1)
1407 {
1408 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname,
1409 buf->b_fname, FALSE, buf);
Bram Moolenaarb429cde2012-04-25 18:24:29 +02001410 buf->b_changedtick = -1; /* note that we did it already */
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001411 /* start all over, autocommands may mess up the lists */
1412 next_tp = first_tabpage;
1413 break;
1414 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001415 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001416 }
Bram Moolenaarf740b292006-02-16 22:11:02 +00001417# else
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001418 apply_autocmds(EVENT_BUFWINLEAVE, curbuf, curbuf->b_fname,
1419 FALSE, curbuf);
Bram Moolenaarf740b292006-02-16 22:11:02 +00001420# endif
Bram Moolenaar33aec762006-01-22 23:30:12 +00001421
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001422 /* Trigger BufUnload for buffers that are loaded */
1423 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1424 if (buf->b_ml.ml_mfp != NULL)
1425 {
1426 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001427 FALSE, buf);
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001428 if (!buf_valid(buf)) /* autocmd may delete the buffer */
1429 break;
1430 }
1431 apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
1432 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001433#endif
1434
1435#ifdef FEAT_VIMINFO
1436 if (*p_viminfo != NUL)
1437 /* Write out the registers, history, marks etc, to the viminfo file */
1438 write_viminfo(NULL, FALSE);
1439#endif
1440
1441#ifdef FEAT_AUTOCMD
Bram Moolenaar0e1e25f2010-05-28 21:07:08 +02001442 if (get_vim_var_nr(VV_DYING) <= 1)
1443 apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001444#endif
1445
Bram Moolenaar05159a02005-02-26 23:04:13 +00001446#ifdef FEAT_PROFILE
1447 profile_dump();
1448#endif
1449
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001450 if (did_emsg
1451#ifdef FEAT_GUI
1452 || (gui.in_use && msg_didany && p_verbose > 0)
1453#endif
1454 )
1455 {
1456 /* give the user a chance to read the (error) message */
1457 no_wait_return = FALSE;
1458 wait_return(FALSE);
1459 }
1460
1461#ifdef FEAT_AUTOCMD
1462 /* Position the cursor again, the autocommands may have moved it */
1463# ifdef FEAT_GUI
1464 if (!gui.in_use)
1465# endif
1466 windgoto((int)Rows - 1, 0);
1467#endif
1468
Bram Moolenaar0ba04292010-07-14 23:23:17 +02001469#ifdef FEAT_LUA
1470 lua_end();
1471#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001472#ifdef FEAT_MZSCHEME
1473 mzscheme_end();
1474#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001475#ifdef FEAT_TCL
1476 tcl_end();
1477#endif
1478#ifdef FEAT_RUBY
1479 ruby_end();
1480#endif
1481#ifdef FEAT_PYTHON
1482 python_end();
1483#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02001484#ifdef FEAT_PYTHON3
1485 python3_end();
1486#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001487#ifdef FEAT_PERL
1488 perl_end();
1489#endif
1490#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
1491 iconv_end();
1492#endif
1493#ifdef FEAT_NETBEANS_INTG
1494 netbeans_end();
1495#endif
Bram Moolenaar02b06312007-09-06 15:39:22 +00001496#ifdef FEAT_CSCOPE
1497 cs_end();
1498#endif
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00001499#ifdef FEAT_EVAL
1500 if (garbage_collect_at_exit)
1501 garbage_collect();
1502#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001503
1504 mch_exit(exitval);
1505}
1506
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01001507#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001508/*
1509 * Get a (optional) count for a Vim argument.
1510 */
1511 static int
1512get_number_arg(p, idx, def)
1513 char_u *p; /* pointer to argument */
1514 int *idx; /* index in argument, is incremented */
1515 int def; /* default value */
1516{
1517 if (vim_isdigit(p[*idx]))
1518 {
1519 def = atoi((char *)&(p[*idx]));
1520 while (vim_isdigit(p[*idx]))
1521 *idx = *idx + 1;
1522 }
1523 return def;
1524}
1525
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001526#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
1527/*
1528 * Setup to use the current locale (for ctype() and many other things).
1529 */
1530 static void
1531init_locale()
1532{
1533 setlocale(LC_ALL, "");
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001534
Bram Moolenaarc6af8122010-05-21 12:04:55 +02001535# ifdef FEAT_GUI_GTK
1536 /* Tell Gtk not to change our locale settings. */
1537 gtk_disable_setlocale();
1538# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001539# if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
1540 /* Make sure strtod() uses a decimal point, not a comma. */
1541 setlocale(LC_NUMERIC, "C");
1542# endif
1543
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001544# ifdef WIN32
1545 /* Apparently MS-Windows printf() may cause a crash when we give it 8-bit
1546 * text while it's expecting text in the current locale. This call avoids
1547 * that. */
1548 setlocale(LC_CTYPE, "C");
1549# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001550
1551# ifdef FEAT_GETTEXT
1552 {
1553 int mustfree = FALSE;
1554 char_u *p;
1555
1556# ifdef DYNAMIC_GETTEXT
1557 /* Initialize the gettext library */
1558 dyn_libintl_init(NULL);
1559# endif
1560 /* expand_env() doesn't work yet, because chartab[] is not initialized
1561 * yet, call vim_getenv() directly */
1562 p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
1563 if (p != NULL && *p != NUL)
1564 {
Bram Moolenaar1ad2f132007-06-19 18:27:18 +00001565 vim_snprintf((char *)NameBuff, MAXPATHL, "%s/lang", p);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001566 bindtextdomain(VIMPACKAGE, (char *)NameBuff);
1567 }
1568 if (mustfree)
1569 vim_free(p);
1570 textdomain(VIMPACKAGE);
1571 }
1572# endif
1573}
1574#endif
1575
1576/*
1577 * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
1578 * If the executable name starts with "r" we disable shell commands.
1579 * If the next character is "e" we run in Easy mode.
1580 * If the next character is "g" we run the GUI version.
1581 * If the next characters are "view" we start in readonly mode.
1582 * If the next characters are "diff" or "vimdiff" we start in diff mode.
1583 * If the next characters are "ex" we start in Ex mode. If it's followed
1584 * by "im" use improved Ex mode.
1585 */
1586 static void
1587parse_command_name(parmp)
1588 mparm_T *parmp;
1589{
1590 char_u *initstr;
1591
1592 initstr = gettail((char_u *)parmp->argv[0]);
1593
1594#ifdef MACOS_X_UNIX
1595 /* An issue has been seen when launching Vim in such a way that
1596 * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
1597 * executable or a symbolic link of it. Until this issue is resolved
1598 * we prohibit the GUI from being used.
1599 */
1600 if (STRCMP(initstr, parmp->argv[0]) == 0)
1601 disallow_gui = TRUE;
1602
1603 /* TODO: On MacOS X default to gui if argv[0] ends in:
Bram Moolenaar27dc1952006-03-15 23:06:44 +00001604 * /Vim.app/Contents/MacOS/Vim */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001605#endif
1606
1607#ifdef FEAT_EVAL
1608 set_vim_var_string(VV_PROGNAME, initstr, -1);
1609#endif
1610
1611 if (TOLOWER_ASC(initstr[0]) == 'r')
1612 {
1613 restricted = TRUE;
1614 ++initstr;
1615 }
1616
Bram Moolenaarad249fb2010-05-07 16:35:04 +02001617 /* Use evim mode for "evim" and "egvim", not for "editor". */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001618 if (TOLOWER_ASC(initstr[0]) == 'e'
1619 && (TOLOWER_ASC(initstr[1]) == 'v'
1620 || TOLOWER_ASC(initstr[1]) == 'g'))
1621 {
1622#ifdef FEAT_GUI
1623 gui.starting = TRUE;
1624#endif
1625 parmp->evim_mode = TRUE;
1626 ++initstr;
1627 }
1628
Bram Moolenaar806875d2008-09-18 18:57:10 +00001629 /* "gvim" starts the GUI. Also accept "Gvim" for MS-Windows. */
1630 if (TOLOWER_ASC(initstr[0]) == 'g')
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001631 {
1632 main_start_gui();
1633#ifdef FEAT_GUI
1634 ++initstr;
1635#endif
1636 }
1637
1638 if (STRNICMP(initstr, "view", 4) == 0)
1639 {
1640 readonlymode = TRUE;
1641 curbuf->b_p_ro = TRUE;
1642 p_uc = 10000; /* don't update very often */
1643 initstr += 4;
1644 }
1645 else if (STRNICMP(initstr, "vim", 3) == 0)
1646 initstr += 3;
1647
1648 /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
1649 if (STRICMP(initstr, "diff") == 0)
1650 {
1651#ifdef FEAT_DIFF
1652 parmp->diff_mode = TRUE;
1653#else
1654 mch_errmsg(_("This Vim was not compiled with the diff feature."));
1655 mch_errmsg("\n");
1656 mch_exit(2);
1657#endif
1658 }
1659
1660 if (STRNICMP(initstr, "ex", 2) == 0)
1661 {
1662 if (STRNICMP(initstr + 2, "im", 2) == 0)
1663 exmode_active = EXMODE_VIM;
1664 else
1665 exmode_active = EXMODE_NORMAL;
1666 change_compatible(TRUE); /* set 'compatible' */
1667 }
1668}
1669
Bram Moolenaarb4210b32004-06-13 14:51:16 +00001670/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00001671 * Get the name of the display, before gui_prepare() removes it from
1672 * argv[]. Used for the xterm-clipboard display.
1673 *
Bram Moolenaar78e17622007-08-30 10:26:19 +00001674 * Also find the --server... arguments and --socketid and --windowid
Bram Moolenaar58d98232005-07-23 22:25:46 +00001675 */
Bram Moolenaar58d98232005-07-23 22:25:46 +00001676 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001677early_arg_scan(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00001678 mparm_T *parmp UNUSED;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001679{
Bram Moolenaar03005972008-11-20 13:12:36 +00001680#if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER) \
1681 || !defined(FEAT_NETBEANS_INTG)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001682 int argc = parmp->argc;
1683 char **argv = parmp->argv;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001684 int i;
1685
1686 for (i = 1; i < argc; i++)
1687 {
1688 if (STRCMP(argv[i], "--") == 0)
1689 break;
1690# ifdef FEAT_XCLIPBOARD
1691 else if (STRICMP(argv[i], "-display") == 0
Bram Moolenaar241a8aa2005-12-06 20:04:44 +00001692# if defined(FEAT_GUI_GTK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001693 || STRICMP(argv[i], "--display") == 0
1694# endif
1695 )
1696 {
1697 if (i == argc - 1)
1698 mainerr_arg_missing((char_u *)argv[i]);
1699 xterm_display = argv[++i];
1700 }
1701# endif
1702# ifdef FEAT_CLIENTSERVER
1703 else if (STRICMP(argv[i], "--servername") == 0)
1704 {
1705 if (i == argc - 1)
1706 mainerr_arg_missing((char_u *)argv[i]);
1707 parmp->serverName_arg = (char_u *)argv[++i];
1708 }
Bram Moolenaareb94e552006-03-11 21:35:11 +00001709 else if (STRICMP(argv[i], "--serverlist") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001710 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001711 else if (STRNICMP(argv[i], "--remote", 8) == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00001712 {
1713 parmp->serverArg = TRUE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00001714# ifdef FEAT_GUI
1715 if (strstr(argv[i], "-wait") != 0)
1716 /* don't fork() when starting the GUI to edit files ourself */
1717 gui.dofork = FALSE;
1718# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001719 }
1720# endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001721
1722# if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1723# ifdef FEAT_GUI_W32
1724 else if (STRICMP(argv[i], "--windowid") == 0)
1725# else
Bram Moolenaar58d98232005-07-23 22:25:46 +00001726 else if (STRICMP(argv[i], "--socketid") == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001727# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001728 {
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001729 long_u id;
1730 int count;
Bram Moolenaar58d98232005-07-23 22:25:46 +00001731
1732 if (i == argc - 1)
1733 mainerr_arg_missing((char_u *)argv[i]);
1734 if (STRNICMP(argv[i+1], "0x", 2) == 0)
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001735 count = sscanf(&(argv[i + 1][2]), SCANF_HEX_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001736 else
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00001737 count = sscanf(argv[i + 1], SCANF_DECIMAL_LONG_U, &id);
Bram Moolenaar58d98232005-07-23 22:25:46 +00001738 if (count != 1)
1739 mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
1740 else
Bram Moolenaar78e17622007-08-30 10:26:19 +00001741# ifdef FEAT_GUI_W32
1742 win_socket_id = id;
1743# else
1744 gtk_socket_id = id;
1745# endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00001746 i++;
1747 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001748# endif
1749# ifdef FEAT_GUI_GTK
Bram Moolenaar58d98232005-07-23 22:25:46 +00001750 else if (STRICMP(argv[i], "--echo-wid") == 0)
1751 echo_wid_arg = TRUE;
1752# endif
Bram Moolenaar03005972008-11-20 13:12:36 +00001753# ifndef FEAT_NETBEANS_INTG
1754 else if (strncmp(argv[i], "-nb", (size_t)3) == 0)
Bram Moolenaar67c53842010-05-22 18:28:27 +02001755 {
1756 mch_errmsg(_("'-nb' cannot be used: not enabled at compile time\n"));
1757 mch_exit(2);
1758 }
Bram Moolenaar03005972008-11-20 13:12:36 +00001759# endif
1760
Bram Moolenaar58d98232005-07-23 22:25:46 +00001761 }
1762#endif
1763}
1764
1765/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001766 * Scan the command line arguments.
1767 */
1768 static void
1769command_line_scan(parmp)
1770 mparm_T *parmp;
1771{
1772 int argc = parmp->argc;
1773 char **argv = parmp->argv;
1774 int argv_idx; /* index in argv[n][] */
1775 int had_minmin = FALSE; /* found "--" argument */
1776 int want_argument; /* option argument with argument */
1777 int c;
Bram Moolenaar231334e2005-07-25 20:46:57 +00001778 char_u *p = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001779 long n;
1780
1781 --argc;
1782 ++argv;
1783 argv_idx = 1; /* active option letter is argv[0][argv_idx] */
1784 while (argc > 0)
1785 {
1786 /*
1787 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
1788 */
1789 if (argv[0][0] == '+' && !had_minmin)
1790 {
1791 if (parmp->n_commands >= MAX_ARG_CMDS)
1792 mainerr(ME_EXTRA_CMD, NULL);
1793 argv_idx = -1; /* skip to next argument */
1794 if (argv[0][1] == NUL)
1795 parmp->commands[parmp->n_commands++] = (char_u *)"$";
1796 else
1797 parmp->commands[parmp->n_commands++] = (char_u *)&(argv[0][1]);
1798 }
1799
1800 /*
1801 * Optional argument.
1802 */
1803 else if (argv[0][0] == '-' && !had_minmin)
1804 {
1805 want_argument = FALSE;
1806 c = argv[0][argv_idx++];
1807#ifdef VMS
1808 /*
1809 * VMS only uses upper case command lines. Interpret "-X" as "-x"
1810 * and "-/X" as "-X".
1811 */
1812 if (c == '/')
1813 {
1814 c = argv[0][argv_idx++];
1815 c = TOUPPER_ASC(c);
1816 }
1817 else
1818 c = TOLOWER_ASC(c);
1819#endif
1820 switch (c)
1821 {
1822 case NUL: /* "vim -" read from stdin */
1823 /* "ex -" silent mode */
1824 if (exmode_active)
1825 silent_mode = TRUE;
1826 else
1827 {
1828 if (parmp->edit_type != EDIT_NONE)
1829 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1830 parmp->edit_type = EDIT_STDIN;
1831 read_cmd_fd = 2; /* read from stderr instead of stdin */
1832 }
1833 argv_idx = -1; /* skip to next argument */
1834 break;
1835
1836 case '-': /* "--" don't take any more option arguments */
1837 /* "--help" give help message */
1838 /* "--version" give version message */
1839 /* "--literal" take files literally */
1840 /* "--nofork" don't fork */
1841 /* "--noplugin[s]" skip plugins */
1842 /* "--cmd <cmd>" execute cmd before vimrc */
1843 if (STRICMP(argv[0] + argv_idx, "help") == 0)
1844 usage();
1845 else if (STRICMP(argv[0] + argv_idx, "version") == 0)
1846 {
1847 Columns = 80; /* need to init Columns */
1848 info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
1849 list_version();
1850 msg_putchar('\n');
1851 msg_didout = FALSE;
1852 mch_exit(0);
1853 }
1854 else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
1855 {
1856#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1857 parmp->literal = TRUE;
1858#endif
1859 }
1860 else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
1861 {
1862#ifdef FEAT_GUI
1863 gui.dofork = FALSE; /* don't fork() when starting GUI */
1864#endif
1865 }
1866 else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
1867 p_lpl = FALSE;
1868 else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
1869 {
1870 want_argument = TRUE;
1871 argv_idx += 3;
1872 }
Bram Moolenaaref94eec2009-11-11 13:22:11 +00001873 else if (STRNICMP(argv[0] + argv_idx, "startuptime", 11) == 0)
1874 {
1875 want_argument = TRUE;
1876 argv_idx += 11;
1877 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001878#ifdef FEAT_CLIENTSERVER
1879 else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
1880 ; /* already processed -- no arg */
1881 else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
1882 || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
1883 {
1884 /* already processed -- snatch the following arg */
1885 if (argc > 1)
1886 {
1887 --argc;
1888 ++argv;
1889 }
1890 }
1891#endif
Bram Moolenaar78e17622007-08-30 10:26:19 +00001892#if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_W32)
1893# ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001894 else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
Bram Moolenaar78e17622007-08-30 10:26:19 +00001895# else
1896 else if (STRNICMP(argv[0] + argv_idx, "windowid", 8) == 0)
1897# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001898 {
1899 /* already processed -- snatch the following arg */
1900 if (argc > 1)
1901 {
1902 --argc;
1903 ++argv;
1904 }
1905 }
Bram Moolenaar78e17622007-08-30 10:26:19 +00001906#endif
1907#ifdef FEAT_GUI_GTK
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001908 else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
1909 {
1910 /* already processed, skip */
1911 }
1912#endif
1913 else
1914 {
1915 if (argv[0][argv_idx])
1916 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
1917 had_minmin = TRUE;
1918 }
1919 if (!want_argument)
1920 argv_idx = -1; /* skip to next argument */
1921 break;
1922
1923 case 'A': /* "-A" start in Arabic mode */
1924#ifdef FEAT_ARABIC
1925 set_option_value((char_u *)"arabic", 1L, NULL, 0);
1926#else
1927 mch_errmsg(_(e_noarabic));
1928 mch_exit(2);
1929#endif
1930 break;
1931
1932 case 'b': /* "-b" binary mode */
Bram Moolenaar231334e2005-07-25 20:46:57 +00001933 /* Needs to be effective before expanding file names, because
1934 * for Win32 this makes us edit a shortcut file itself,
1935 * instead of the file it links to. */
1936 set_options_bin(curbuf->b_p_bin, 1, 0);
1937 curbuf->b_p_bin = 1; /* binary file I/O */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001938 break;
1939
1940 case 'C': /* "-C" Compatible */
1941 change_compatible(TRUE);
1942 break;
1943
1944 case 'e': /* "-e" Ex mode */
1945 exmode_active = EXMODE_NORMAL;
1946 break;
1947
1948 case 'E': /* "-E" Improved Ex mode */
1949 exmode_active = EXMODE_VIM;
1950 break;
1951
1952 case 'f': /* "-f" GUI: run in foreground. Amiga: open
1953 window directly, not with newcli */
1954#ifdef FEAT_GUI
1955 gui.dofork = FALSE; /* don't fork() when starting GUI */
1956#endif
1957 break;
1958
1959 case 'g': /* "-g" start GUI */
1960 main_start_gui();
1961 break;
1962
1963 case 'F': /* "-F" start in Farsi mode: rl + fkmap set */
1964#ifdef FEAT_FKMAP
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001965 p_fkmap = TRUE;
1966 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001967#else
1968 mch_errmsg(_(e_nofarsi));
1969 mch_exit(2);
1970#endif
1971 break;
1972
1973 case 'h': /* "-h" give help message */
1974#ifdef FEAT_GUI_GNOME
1975 /* Tell usage() to exit for "gvim". */
1976 gui.starting = FALSE;
1977#endif
1978 usage();
1979 break;
1980
1981 case 'H': /* "-H" start in Hebrew mode: rl + hkmap set */
1982#ifdef FEAT_RIGHTLEFT
Bram Moolenaarc4cd38f2008-01-13 15:18:01 +00001983 p_hkmap = TRUE;
1984 set_option_value((char_u *)"rl", 1L, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001985#else
1986 mch_errmsg(_(e_nohebrew));
1987 mch_exit(2);
1988#endif
1989 break;
1990
1991 case 'l': /* "-l" lisp mode, 'lisp' and 'showmatch' on */
1992#ifdef FEAT_LISP
1993 set_option_value((char_u *)"lisp", 1L, NULL, 0);
1994 p_sm = TRUE;
1995#endif
1996 break;
1997
Bram Moolenaarc013cb62005-07-24 21:18:31 +00001998 case 'M': /* "-M" no changes or writing of files */
1999 reset_modifiable();
2000 /* FALLTHROUGH */
2001
2002 case 'm': /* "-m" no writing of files */
2003 p_write = FALSE;
2004 break;
2005
2006 case 'y': /* "-y" easy mode */
2007#ifdef FEAT_GUI
2008 gui.starting = TRUE; /* start GUI a bit later */
2009#endif
2010 parmp->evim_mode = TRUE;
2011 break;
2012
2013 case 'N': /* "-N" Nocompatible */
2014 change_compatible(FALSE);
2015 break;
2016
2017 case 'n': /* "-n" no swap file */
Bram Moolenaar67c53842010-05-22 18:28:27 +02002018#ifdef FEAT_NETBEANS_INTG
2019 /* checking for "-nb", netbeans parameters */
2020 if (argv[0][argv_idx] == 'b')
2021 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02002022 netbeansArg = argv[0];
2023 argv_idx = -1; /* skip to next argument */
2024 }
2025 else
2026#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002027 parmp->no_swap_file = TRUE;
2028 break;
2029
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002030 case 'p': /* "-p[N]" open N tab pages */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00002031#ifdef TARGET_API_MAC_OSX
2032 /* For some reason on MacOS X, an argument like:
2033 -psn_0_10223617 is passed in when invoke from Finder
2034 or with the 'open' command */
2035 if (argv[0][argv_idx] == 's')
2036 {
2037 argv_idx = -1; /* bypass full -psn */
2038 main_start_gui();
2039 break;
2040 }
2041#endif
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002042#ifdef FEAT_WINDOWS
2043 /* default is 0: open window for each file */
2044 parmp->window_count = get_number_arg((char_u *)argv[0],
2045 &argv_idx, 0);
2046 parmp->window_layout = WIN_TABS;
2047#endif
2048 break;
2049
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002050 case 'o': /* "-o[N]" open N horizontal split windows */
2051#ifdef FEAT_WINDOWS
2052 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002053 parmp->window_count = get_number_arg((char_u *)argv[0],
2054 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002055 parmp->window_layout = WIN_HOR;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002056#endif
2057 break;
2058
2059 case 'O': /* "-O[N]" open N vertical split windows */
2060#if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
2061 /* default is 0: open window for each file */
Bram Moolenaar231334e2005-07-25 20:46:57 +00002062 parmp->window_count = get_number_arg((char_u *)argv[0],
2063 &argv_idx, 0);
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002064 parmp->window_layout = WIN_VER;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002065#endif
2066 break;
2067
2068#ifdef FEAT_QUICKFIX
2069 case 'q': /* "-q" QuickFix mode */
2070 if (parmp->edit_type != EDIT_NONE)
2071 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2072 parmp->edit_type = EDIT_QF;
2073 if (argv[0][argv_idx]) /* "-q{errorfile}" */
2074 {
2075 parmp->use_ef = (char_u *)argv[0] + argv_idx;
2076 argv_idx = -1;
2077 }
2078 else if (argc > 1) /* "-q {errorfile}" */
2079 want_argument = TRUE;
2080 break;
2081#endif
2082
2083 case 'R': /* "-R" readonly mode */
2084 readonlymode = TRUE;
2085 curbuf->b_p_ro = TRUE;
2086 p_uc = 10000; /* don't update very often */
2087 break;
2088
2089 case 'r': /* "-r" recovery mode */
2090 case 'L': /* "-L" recovery mode */
2091 recoverymode = 1;
2092 break;
2093
2094 case 's':
2095 if (exmode_active) /* "-s" silent (batch) mode */
2096 silent_mode = TRUE;
2097 else /* "-s {scriptin}" read from script file */
2098 want_argument = TRUE;
2099 break;
2100
2101 case 't': /* "-t {tag}" or "-t{tag}" jump to tag */
2102 if (parmp->edit_type != EDIT_NONE)
2103 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2104 parmp->edit_type = EDIT_TAG;
2105 if (argv[0][argv_idx]) /* "-t{tag}" */
2106 {
2107 parmp->tagname = (char_u *)argv[0] + argv_idx;
2108 argv_idx = -1;
2109 }
2110 else /* "-t {tag}" */
2111 want_argument = TRUE;
2112 break;
2113
2114#ifdef FEAT_EVAL
2115 case 'D': /* "-D" Debugging */
2116 parmp->use_debug_break_level = 9999;
2117 break;
2118#endif
2119#ifdef FEAT_DIFF
2120 case 'd': /* "-d" 'diff' */
2121# ifdef AMIGA
2122 /* check for "-dev {device}" */
2123 if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
2124 want_argument = TRUE;
2125 else
2126# endif
2127 parmp->diff_mode = TRUE;
2128 break;
2129#endif
2130 case 'V': /* "-V{N}" Verbose level */
2131 /* default is 10: a little bit verbose */
2132 p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2133 if (argv[0][argv_idx] != NUL)
2134 {
2135 set_option_value((char_u *)"verbosefile", 0L,
2136 (char_u *)argv[0] + argv_idx, 0);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002137 argv_idx = (int)STRLEN(argv[0]);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002138 }
2139 break;
2140
2141 case 'v': /* "-v" Vi-mode (as if called "vi") */
2142 exmode_active = 0;
2143#ifdef FEAT_GUI
2144 gui.starting = FALSE; /* don't start GUI */
2145#endif
2146 break;
2147
2148 case 'w': /* "-w{number}" set window height */
2149 /* "-w {scriptout}" write to script */
2150 if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
2151 {
2152 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2153 set_option_value((char_u *)"window", n, NULL, 0);
2154 break;
2155 }
2156 want_argument = TRUE;
2157 break;
2158
2159#ifdef FEAT_CRYPT
2160 case 'x': /* "-x" encrypted reading/writing of files */
2161 parmp->ask_for_key = TRUE;
2162 break;
2163#endif
2164
2165 case 'X': /* "-X" don't connect to X server */
2166#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2167 x_no_connect = TRUE;
2168#endif
2169 break;
2170
2171 case 'Z': /* "-Z" restricted mode */
2172 restricted = TRUE;
2173 break;
2174
2175 case 'c': /* "-c{command}" or "-c {command}" execute
2176 command */
2177 if (argv[0][argv_idx] != NUL)
2178 {
2179 if (parmp->n_commands >= MAX_ARG_CMDS)
2180 mainerr(ME_EXTRA_CMD, NULL);
Bram Moolenaar231334e2005-07-25 20:46:57 +00002181 parmp->commands[parmp->n_commands++] = (char_u *)argv[0]
2182 + argv_idx;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002183 argv_idx = -1;
2184 break;
2185 }
2186 /*FALLTHROUGH*/
2187 case 'S': /* "-S {file}" execute Vim script */
2188 case 'i': /* "-i {viminfo}" use for viminfo */
2189#ifndef FEAT_DIFF
2190 case 'd': /* "-d {device}" device (for Amiga) */
2191#endif
2192 case 'T': /* "-T {terminal}" terminal name */
2193 case 'u': /* "-u {vimrc}" vim inits file */
2194 case 'U': /* "-U {gvimrc}" gvim inits file */
2195 case 'W': /* "-W {scriptout}" overwrite */
2196#ifdef FEAT_GUI_W32
2197 case 'P': /* "-P {parent title}" MDI parent */
2198#endif
2199 want_argument = TRUE;
2200 break;
2201
2202 default:
2203 mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
2204 }
2205
2206 /*
2207 * Handle option arguments with argument.
2208 */
2209 if (want_argument)
2210 {
2211 /*
2212 * Check for garbage immediately after the option letter.
2213 */
2214 if (argv[0][argv_idx] != NUL)
2215 mainerr(ME_GARBAGE, (char_u *)argv[0]);
2216
2217 --argc;
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002218 if (argc < 1 && c != 'S') /* -S has an optional argument */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002219 mainerr_arg_missing((char_u *)argv[0]);
2220 ++argv;
2221 argv_idx = -1;
2222
2223 switch (c)
2224 {
2225 case 'c': /* "-c {command}" execute command */
2226 case 'S': /* "-S {file}" execute Vim script */
2227 if (parmp->n_commands >= MAX_ARG_CMDS)
2228 mainerr(ME_EXTRA_CMD, NULL);
2229 if (c == 'S')
2230 {
2231 char *a;
2232
2233 if (argc < 1)
2234 /* "-S" without argument: use default session file
2235 * name. */
2236 a = SESSION_FILE;
2237 else if (argv[0][0] == '-')
2238 {
2239 /* "-S" followed by another option: use default
2240 * session file name. */
2241 a = SESSION_FILE;
2242 ++argc;
2243 --argv;
2244 }
2245 else
2246 a = argv[0];
2247 p = alloc((unsigned)(STRLEN(a) + 4));
2248 if (p == NULL)
2249 mch_exit(2);
2250 sprintf((char *)p, "so %s", a);
2251 parmp->cmds_tofree[parmp->n_commands] = TRUE;
2252 parmp->commands[parmp->n_commands++] = p;
2253 }
2254 else
Bram Moolenaar231334e2005-07-25 20:46:57 +00002255 parmp->commands[parmp->n_commands++] =
2256 (char_u *)argv[0];
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002257 break;
2258
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002259 case '-':
2260 if (argv[-1][2] == 'c')
2261 {
2262 /* "--cmd {command}" execute command */
2263 if (parmp->n_pre_commands >= MAX_ARG_CMDS)
2264 mainerr(ME_EXTRA_CMD, NULL);
2265 parmp->pre_commands[parmp->n_pre_commands++] =
Bram Moolenaar231334e2005-07-25 20:46:57 +00002266 (char_u *)argv[0];
Bram Moolenaaref94eec2009-11-11 13:22:11 +00002267 }
2268 /* "--startuptime <file>" already handled */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002269 break;
2270
2271 /* case 'd': -d {device} is handled in mch_check_win() for the
2272 * Amiga */
2273
2274#ifdef FEAT_QUICKFIX
2275 case 'q': /* "-q {errorfile}" QuickFix mode */
2276 parmp->use_ef = (char_u *)argv[0];
2277 break;
2278#endif
2279
2280 case 'i': /* "-i {viminfo}" use for viminfo */
2281 use_viminfo = (char_u *)argv[0];
2282 break;
2283
2284 case 's': /* "-s {scriptin}" read from script file */
2285 if (scriptin[0] != NULL)
2286 {
2287scripterror:
2288 mch_errmsg(_("Attempt to open script file again: \""));
2289 mch_errmsg(argv[-1]);
2290 mch_errmsg(" ");
2291 mch_errmsg(argv[0]);
2292 mch_errmsg("\"\n");
2293 mch_exit(2);
2294 }
2295 if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
2296 {
2297 mch_errmsg(_("Cannot open for reading: \""));
2298 mch_errmsg(argv[0]);
2299 mch_errmsg("\"\n");
2300 mch_exit(2);
2301 }
2302 if (save_typebuf() == FAIL)
2303 mch_exit(2); /* out of memory */
2304 break;
2305
2306 case 't': /* "-t {tag}" */
2307 parmp->tagname = (char_u *)argv[0];
2308 break;
2309
2310 case 'T': /* "-T {terminal}" terminal name */
2311 /*
2312 * The -T term argument is always available and when
2313 * HAVE_TERMLIB is supported it overrides the environment
2314 * variable TERM.
2315 */
2316#ifdef FEAT_GUI
2317 if (term_is_gui((char_u *)argv[0]))
2318 gui.starting = TRUE; /* start GUI a bit later */
2319 else
2320#endif
2321 parmp->term = (char_u *)argv[0];
2322 break;
2323
2324 case 'u': /* "-u {vimrc}" vim inits file */
2325 parmp->use_vimrc = (char_u *)argv[0];
2326 break;
2327
2328 case 'U': /* "-U {gvimrc}" gvim inits file */
2329#ifdef FEAT_GUI
2330 use_gvimrc = (char_u *)argv[0];
2331#endif
2332 break;
2333
2334 case 'w': /* "-w {nr}" 'window' value */
2335 /* "-w {scriptout}" append to script file */
2336 if (vim_isdigit(*((char_u *)argv[0])))
2337 {
2338 argv_idx = 0;
2339 n = get_number_arg((char_u *)argv[0], &argv_idx, 10);
2340 set_option_value((char_u *)"window", n, NULL, 0);
2341 argv_idx = -1;
2342 break;
2343 }
2344 /*FALLTHROUGH*/
2345 case 'W': /* "-W {scriptout}" overwrite script file */
2346 if (scriptout != NULL)
2347 goto scripterror;
2348 if ((scriptout = mch_fopen(argv[0],
2349 c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
2350 {
2351 mch_errmsg(_("Cannot open for script output: \""));
2352 mch_errmsg(argv[0]);
2353 mch_errmsg("\"\n");
2354 mch_exit(2);
2355 }
2356 break;
2357
2358#ifdef FEAT_GUI_W32
2359 case 'P': /* "-P {parent title}" MDI parent */
2360 gui_mch_set_parent(argv[0]);
2361 break;
2362#endif
2363 }
2364 }
2365 }
2366
2367 /*
2368 * File name argument.
2369 */
2370 else
2371 {
2372 argv_idx = -1; /* skip to next argument */
2373
2374 /* Check for only one type of editing. */
2375 if (parmp->edit_type != EDIT_NONE && parmp->edit_type != EDIT_FILE)
2376 mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
2377 parmp->edit_type = EDIT_FILE;
2378
2379#ifdef MSWIN
2380 /* Remember if the argument was a full path before changing
2381 * slashes to backslashes. */
2382 if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
2383 parmp->full_path = TRUE;
2384#endif
2385
2386 /* Add the file to the global argument list. */
2387 if (ga_grow(&global_alist.al_ga, 1) == FAIL
2388 || (p = vim_strsave((char_u *)argv[0])) == NULL)
2389 mch_exit(2);
2390#ifdef FEAT_DIFF
2391 if (parmp->diff_mode && mch_isdir(p) && GARGCOUNT > 0
2392 && !mch_isdir(alist_name(&GARGLIST[0])))
2393 {
2394 char_u *r;
2395
2396 r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
2397 if (r != NULL)
2398 {
2399 vim_free(p);
2400 p = r;
2401 }
2402 }
2403#endif
2404#if defined(__CYGWIN32__) && !defined(WIN32)
2405 /*
2406 * If vim is invoked by non-Cygwin tools, convert away any
2407 * DOS paths, so things like .swp files are created correctly.
2408 * Look for evidence of non-Cygwin paths before we bother.
2409 * This is only for when using the Unix files.
2410 */
Bram Moolenaarad249fb2010-05-07 16:35:04 +02002411 if (strpbrk(p, "\\:") != NULL && !path_with_url(p))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002412 {
2413 char posix_path[PATH_MAX];
2414
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002415# if CYGWIN_VERSION_DLL_MAJOR >= 1007
2416 cygwin_conv_path(CCP_WIN_A_TO_POSIX, p, posix_path, PATH_MAX);
2417# else
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002418 cygwin_conv_to_posix_path(p, posix_path);
Bram Moolenaar0d1498e2008-06-29 12:00:49 +00002419# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002420 vim_free(p);
2421 p = vim_strsave(posix_path);
2422 if (p == NULL)
2423 mch_exit(2);
2424 }
2425#endif
Bram Moolenaarcc016f52005-12-10 20:23:46 +00002426
2427#ifdef USE_FNAME_CASE
2428 /* Make the case of the file name match the actual file. */
2429 fname_case(p, 0);
2430#endif
2431
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002432 alist_add(&global_alist, p,
2433#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
Bram Moolenaar231334e2005-07-25 20:46:57 +00002434 parmp->literal ? 2 : 0 /* add buffer nr after exp. */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002435#else
2436 2 /* add buffer number now and use curbuf */
2437#endif
2438 );
2439
2440#if defined(FEAT_MBYTE) && defined(WIN32)
2441 {
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002442 /* Remember this argument has been added to the argument list.
2443 * Needed when 'encoding' is changed. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002444 used_file_arg(argv[0], parmp->literal, parmp->full_path,
Bram Moolenaar688e5f72008-07-24 11:51:40 +00002445# ifdef FEAT_DIFF
2446 parmp->diff_mode
2447# else
2448 FALSE
2449# endif
2450 );
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002451 }
2452#endif
2453 }
2454
2455 /*
2456 * If there are no more letters after the current "-", go to next
2457 * argument. argv_idx is set to -1 when the current argument is to be
2458 * skipped.
2459 */
2460 if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
2461 {
2462 --argc;
2463 ++argv;
2464 argv_idx = 1;
2465 }
2466 }
Bram Moolenaar867a4b72007-03-18 20:51:46 +00002467
2468#ifdef FEAT_EVAL
2469 /* If there is a "+123" or "-c" command, set v:swapcommand to the first
2470 * one. */
2471 if (parmp->n_commands > 0)
2472 {
2473 p = alloc((unsigned)STRLEN(parmp->commands[0]) + 3);
2474 if (p != NULL)
2475 {
2476 sprintf((char *)p, ":%s\r", parmp->commands[0]);
2477 set_vim_var_string(VV_SWAPCOMMAND, p, -1);
2478 vim_free(p);
2479 }
2480 }
2481#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002482}
2483
2484/*
2485 * Print a warning if stdout is not a terminal.
2486 * When starting in Ex mode and commands come from a file, set Silent mode.
2487 */
2488 static void
2489check_tty(parmp)
2490 mparm_T *parmp;
2491{
2492 int input_isatty; /* is active input a terminal? */
2493
2494 input_isatty = mch_input_isatty();
2495 if (exmode_active)
2496 {
2497 if (!input_isatty)
2498 silent_mode = TRUE;
2499 }
2500 else if (parmp->want_full_screen && (!parmp->stdout_isatty || !input_isatty)
2501#ifdef FEAT_GUI
2502 /* don't want the delay when started from the desktop */
2503 && !gui.starting
2504#endif
2505 )
2506 {
2507#ifdef NBDEBUG
2508 /*
2509 * This shouldn't be necessary. But if I run netbeans with the log
2510 * output coming to the console and XOpenDisplay fails, I get vim
2511 * trying to start with input/output to my console tty. This fills my
2512 * input buffer so fast I can't even kill the process in under 2
Bram Moolenaar49325942007-05-10 19:19:59 +00002513 * minutes (and it beeps continuously the whole time :-)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002514 */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02002515 if (netbeans_active() && (!parmp->stdout_isatty || !input_isatty))
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002516 {
2517 mch_errmsg(_("Vim: Error: Failure to start gvim from NetBeans\n"));
2518 exit(1);
2519 }
2520#endif
2521 if (!parmp->stdout_isatty)
2522 mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
2523 if (!input_isatty)
2524 mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
2525 out_flush();
2526 if (scriptin[0] == NULL)
2527 ui_delay(2000L, TRUE);
2528 TIME_MSG("Warning delay");
2529 }
2530}
2531
2532/*
2533 * Read text from stdin.
2534 */
2535 static void
2536read_stdin()
2537{
2538 int i;
2539
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002540#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002541 /* When getting the ATTENTION prompt here, use a dialog */
2542 swap_exists_action = SEA_DIALOG;
2543#endif
2544 no_wait_return = TRUE;
2545 i = msg_didany;
2546 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002547 (void)open_buffer(TRUE, NULL, 0); /* create memfile and read file */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002548 no_wait_return = FALSE;
2549 msg_didany = i;
2550 TIME_MSG("reading stdin");
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002551#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002552 check_swap_exists_action();
2553#endif
2554#if !(defined(AMIGA) || defined(MACOS))
2555 /*
2556 * Close stdin and dup it from stderr. Required for GPM to work
2557 * properly, and for running external commands.
2558 * Is there any other system that cannot do this?
2559 */
2560 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002561 ignored = dup(2);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002562#endif
2563}
2564
2565/*
2566 * Create the requested number of windows and edit buffers in them.
2567 * Also does recovery if "recoverymode" set.
2568 */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002569 static void
2570create_windows(parmp)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002571 mparm_T *parmp UNUSED;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002572{
2573#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002574 int dorewind;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002575 int done = 0;
2576
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002577 /*
2578 * Create the number of windows that was requested.
2579 */
2580 if (parmp->window_count == -1) /* was not set */
2581 parmp->window_count = 1;
2582 if (parmp->window_count == 0)
2583 parmp->window_count = GARGCOUNT;
2584 if (parmp->window_count > 1)
2585 {
2586 /* Don't change the windows if there was a command in .vimrc that
2587 * already split some windows */
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002588 if (parmp->window_layout == 0)
2589 parmp->window_layout = WIN_HOR;
2590 if (parmp->window_layout == WIN_TABS)
2591 {
2592 parmp->window_count = make_tabpages(parmp->window_count);
2593 TIME_MSG("making tab pages");
2594 }
2595 else if (firstwin->w_next == NULL)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002596 {
2597 parmp->window_count = make_windows(parmp->window_count,
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002598 parmp->window_layout == WIN_VER);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002599 TIME_MSG("making windows");
2600 }
2601 else
2602 parmp->window_count = win_count();
2603 }
2604 else
2605 parmp->window_count = 1;
2606#endif
2607
2608 if (recoverymode) /* do recover */
2609 {
2610 msg_scroll = TRUE; /* scroll message up */
2611 ml_recover();
2612 if (curbuf->b_ml.ml_mfp == NULL) /* failed */
2613 getout(1);
Bram Moolenaara3227e22006-03-08 21:32:40 +00002614 do_modelines(0); /* do modelines */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002615 }
2616 else
2617 {
2618 /*
2619 * Open a buffer for windows that don't have one yet.
2620 * Commands in the .vimrc might have loaded a file or split the window.
2621 * Watch out for autocommands that delete a window.
2622 */
2623#ifdef FEAT_AUTOCMD
2624 /*
2625 * Don't execute Win/Buf Enter/Leave autocommands here
2626 */
2627 ++autocmd_no_enter;
2628 ++autocmd_no_leave;
2629#endif
2630#ifdef FEAT_WINDOWS
Bram Moolenaar89d40322006-08-29 15:30:07 +00002631 dorewind = TRUE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002632 while (done++ < 1000)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002633 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002634 if (dorewind)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002635 {
2636 if (parmp->window_layout == WIN_TABS)
2637 goto_tabpage(1);
2638 else
2639 curwin = firstwin;
2640 }
2641 else if (parmp->window_layout == WIN_TABS)
2642 {
2643 if (curtab->tp_next == NULL)
2644 break;
2645 goto_tabpage(0);
2646 }
2647 else
2648 {
2649 if (curwin->w_next == NULL)
2650 break;
2651 curwin = curwin->w_next;
2652 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002653 dorewind = FALSE;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002654#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002655 curbuf = curwin->w_buffer;
2656 if (curbuf->b_ml.ml_mfp == NULL)
2657 {
2658#ifdef FEAT_FOLDING
2659 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2660 if (p_fdls >= 0)
2661 curwin->w_p_fdl = p_fdls;
2662#endif
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002663#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002664 /* When getting the ATTENTION prompt here, use a dialog */
2665 swap_exists_action = SEA_DIALOG;
2666#endif
2667 set_buflisted(TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002668
2669 /* create memfile, read file */
2670 (void)open_buffer(FALSE, NULL, 0);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002671
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00002672#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar84212822006-11-07 21:59:47 +00002673 if (swap_exists_action == SEA_QUIT)
2674 {
2675 if (got_int || only_one_window())
2676 {
2677 /* abort selected or quit and only one window */
2678 did_emsg = FALSE; /* avoid hit-enter prompt */
2679 getout(1);
2680 }
2681 /* We can't close the window, it would disturb what
2682 * happens next. Clear the file name and set the arg
2683 * index to -1 to delete it later. */
2684 setfname(curbuf, NULL, NULL, FALSE);
2685 curwin->w_arg_idx = -1;
2686 swap_exists_action = SEA_NONE;
2687 }
2688 else
2689 handle_swap_exists(NULL);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002690#endif
2691#ifdef FEAT_AUTOCMD
Bram Moolenaar89d40322006-08-29 15:30:07 +00002692 dorewind = TRUE; /* start again */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002693#endif
2694 }
2695#ifdef FEAT_WINDOWS
2696 ui_breakcheck();
2697 if (got_int)
2698 {
2699 (void)vgetc(); /* only break the file loading, not the rest */
2700 break;
2701 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002702 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002703#endif
2704#ifdef FEAT_WINDOWS
2705 if (parmp->window_layout == WIN_TABS)
2706 goto_tabpage(1);
2707 else
2708 curwin = firstwin;
2709 curbuf = curwin->w_buffer;
2710#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002711#ifdef FEAT_AUTOCMD
2712 --autocmd_no_enter;
2713 --autocmd_no_leave;
2714#endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002715 }
2716}
2717
2718#ifdef FEAT_WINDOWS
2719 /*
2720 * If opened more than one window, start editing files in the other
2721 * windows. make_windows() has already opened the windows.
2722 */
2723 static void
2724edit_buffers(parmp)
2725 mparm_T *parmp;
2726{
2727 int arg_idx; /* index in argument list */
2728 int i;
Bram Moolenaar84212822006-11-07 21:59:47 +00002729 int advance = TRUE;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002730
2731# ifdef FEAT_AUTOCMD
2732 /*
2733 * Don't execute Win/Buf Enter/Leave autocommands here
2734 */
2735 ++autocmd_no_enter;
2736 ++autocmd_no_leave;
2737# endif
Bram Moolenaar84212822006-11-07 21:59:47 +00002738
2739 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2740 if (curwin->w_arg_idx == -1)
2741 {
2742 win_close(curwin, TRUE);
2743 advance = FALSE;
2744 }
2745
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002746 arg_idx = 1;
2747 for (i = 1; i < parmp->window_count; ++i)
2748 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002749 /* When w_arg_idx is -1 remove the window (see create_windows()). */
2750 if (curwin->w_arg_idx == -1)
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002751 {
Bram Moolenaar84212822006-11-07 21:59:47 +00002752 ++arg_idx;
2753 win_close(curwin, TRUE);
2754 advance = FALSE;
2755 continue;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002756 }
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002757
Bram Moolenaar84212822006-11-07 21:59:47 +00002758 if (advance)
2759 {
2760 if (parmp->window_layout == WIN_TABS)
2761 {
2762 if (curtab->tp_next == NULL) /* just checking */
2763 break;
2764 goto_tabpage(0);
2765 }
2766 else
2767 {
2768 if (curwin->w_next == NULL) /* just checking */
2769 break;
2770 win_enter(curwin->w_next, FALSE);
2771 }
2772 }
2773 advance = TRUE;
2774
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002775 /* Only open the file if there is no file in this window yet (that can
Bram Moolenaar84212822006-11-07 21:59:47 +00002776 * happen when .vimrc contains ":sall"). */
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002777 if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
2778 {
2779 curwin->w_arg_idx = arg_idx;
Bram Moolenaar84212822006-11-07 21:59:47 +00002780 /* Edit file from arg list, if there is one. When "Quit" selected
2781 * at the ATTENTION prompt close the window. */
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002782# ifdef HAS_SWAP_EXISTS_ACTION
2783 swap_exists_did_quit = FALSE;
2784# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002785 (void)do_ecmd(0, arg_idx < GARGCOUNT
2786 ? alist_name(&GARGLIST[arg_idx]) : NULL,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002787 NULL, NULL, ECMD_LASTL, ECMD_HIDE, curwin);
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002788# ifdef HAS_SWAP_EXISTS_ACTION
2789 if (swap_exists_did_quit)
Bram Moolenaar84212822006-11-07 21:59:47 +00002790 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002791 /* abort or quit selected */
Bram Moolenaar84212822006-11-07 21:59:47 +00002792 if (got_int || only_one_window())
2793 {
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002794 /* abort selected and only one window */
Bram Moolenaar84212822006-11-07 21:59:47 +00002795 did_emsg = FALSE; /* avoid hit-enter prompt */
2796 getout(1);
2797 }
2798 win_close(curwin, TRUE);
2799 advance = FALSE;
2800 }
Bram Moolenaar4bfa6082008-07-24 17:34:23 +00002801# endif
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002802 if (arg_idx == GARGCOUNT - 1)
2803 arg_had_last = TRUE;
2804 ++arg_idx;
2805 }
2806 ui_breakcheck();
2807 if (got_int)
2808 {
2809 (void)vgetc(); /* only break the file loading, not the rest */
2810 break;
2811 }
2812 }
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002813
2814 if (parmp->window_layout == WIN_TABS)
2815 goto_tabpage(1);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002816# ifdef FEAT_AUTOCMD
2817 --autocmd_no_enter;
2818# endif
2819 win_enter(firstwin, FALSE); /* back to first window */
2820# ifdef FEAT_AUTOCMD
2821 --autocmd_no_leave;
2822# endif
2823 TIME_MSG("editing files in windows");
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00002824 if (parmp->window_count > 1 && parmp->window_layout != WIN_TABS)
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002825 win_equal(curwin, FALSE, 'b'); /* adjust heights */
2826}
2827#endif /* FEAT_WINDOWS */
2828
2829/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002830 * Execute the commands from --cmd arguments "cmds[cnt]".
2831 */
2832 static void
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002833exe_pre_commands(parmp)
2834 mparm_T *parmp;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002835{
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002836 char_u **cmds = parmp->pre_commands;
2837 int cnt = parmp->n_pre_commands;
Bram Moolenaar58d98232005-07-23 22:25:46 +00002838 int i;
2839
2840 if (cnt > 0)
2841 {
2842 curwin->w_cursor.lnum = 0; /* just in case.. */
2843 sourcing_name = (char_u *)_("pre-vimrc command line");
2844# ifdef FEAT_EVAL
2845 current_SID = SID_CMDARG;
2846# endif
2847 for (i = 0; i < cnt; ++i)
2848 do_cmdline_cmd(cmds[i]);
2849 sourcing_name = NULL;
2850# ifdef FEAT_EVAL
2851 current_SID = 0;
2852# endif
2853 TIME_MSG("--cmd commands");
2854 }
2855}
2856
2857/*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002858 * Execute "+", "-c" and "-S" arguments.
2859 */
2860 static void
2861exe_commands(parmp)
2862 mparm_T *parmp;
2863{
2864 int i;
2865
2866 /*
2867 * We start commands on line 0, make "vim +/pat file" match a
2868 * pattern on line 1. But don't move the cursor when an autocommand
2869 * with g`" was used.
2870 */
2871 msg_scroll = TRUE;
2872 if (parmp->tagname == NULL && curwin->w_cursor.lnum <= 1)
2873 curwin->w_cursor.lnum = 0;
2874 sourcing_name = (char_u *)"command line";
2875#ifdef FEAT_EVAL
2876 current_SID = SID_CARG;
2877#endif
2878 for (i = 0; i < parmp->n_commands; ++i)
2879 {
2880 do_cmdline_cmd(parmp->commands[i]);
2881 if (parmp->cmds_tofree[i])
2882 vim_free(parmp->commands[i]);
2883 }
2884 sourcing_name = NULL;
2885#ifdef FEAT_EVAL
2886 current_SID = 0;
2887#endif
2888 if (curwin->w_cursor.lnum == 0)
2889 curwin->w_cursor.lnum = 1;
2890
2891 if (!exmode_active)
2892 msg_scroll = FALSE;
2893
2894#ifdef FEAT_QUICKFIX
2895 /* When started with "-q errorfile" jump to first error again. */
2896 if (parmp->edit_type == EDIT_QF)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002897 qf_jump(NULL, 0, 0, FALSE);
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002898#endif
2899 TIME_MSG("executing command arguments");
2900}
2901
2902/*
Bram Moolenaar58d98232005-07-23 22:25:46 +00002903 * Source startup scripts.
2904 */
2905 static void
2906source_startup_scripts(parmp)
2907 mparm_T *parmp;
2908{
2909 int i;
2910
2911 /*
2912 * For "evim" source evim.vim first of all, so that the user can overrule
2913 * any things he doesn't like.
2914 */
2915 if (parmp->evim_mode)
2916 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002917 (void)do_source((char_u *)EVIM_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002918 TIME_MSG("source evim file");
2919 }
2920
2921 /*
Bram Moolenaarc013cb62005-07-24 21:18:31 +00002922 * If -u argument given, use only the initializations from that file and
Bram Moolenaar58d98232005-07-23 22:25:46 +00002923 * nothing else.
2924 */
2925 if (parmp->use_vimrc != NULL)
2926 {
Bram Moolenaar231334e2005-07-25 20:46:57 +00002927 if (STRCMP(parmp->use_vimrc, "NONE") == 0
2928 || STRCMP(parmp->use_vimrc, "NORC") == 0)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002929 {
2930#ifdef FEAT_GUI
2931 if (use_gvimrc == NULL) /* don't load gvimrc either */
2932 use_gvimrc = parmp->use_vimrc;
2933#endif
2934 if (parmp->use_vimrc[2] == 'N')
2935 p_lpl = FALSE; /* don't load plugins either */
2936 }
2937 else
2938 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002939 if (do_source(parmp->use_vimrc, FALSE, DOSO_NONE) != OK)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002940 EMSG2(_("E282: Cannot read from \"%s\""), parmp->use_vimrc);
2941 }
2942 }
2943 else if (!silent_mode)
2944 {
2945#ifdef AMIGA
2946 struct Process *proc = (struct Process *)FindTask(0L);
2947 APTR save_winptr = proc->pr_WindowPtr;
2948
2949 /* Avoid a requester here for a volume that doesn't exist. */
2950 proc->pr_WindowPtr = (APTR)-1L;
2951#endif
2952
2953 /*
2954 * Get system wide defaults, if the file name is defined.
2955 */
2956#ifdef SYS_VIMRC_FILE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002957 (void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002958#endif
Bram Moolenaar1056d982006-03-09 22:37:52 +00002959#ifdef MACOS_X
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002960 (void)do_source((char_u *)"$VIMRUNTIME/macmap.vim", FALSE, DOSO_NONE);
Bram Moolenaar1056d982006-03-09 22:37:52 +00002961#endif
Bram Moolenaar58d98232005-07-23 22:25:46 +00002962
2963 /*
2964 * Try to read initialization commands from the following places:
2965 * - environment variable VIMINIT
2966 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
2967 * - second user vimrc file ($VIM/.vimrc for Dos)
2968 * - environment variable EXINIT
2969 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
2970 * - second user exrc file ($VIM/.exrc for Dos)
2971 * The first that exists is used, the rest is ignored.
2972 */
2973 if (process_env((char_u *)"VIMINIT", TRUE) != OK)
2974 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002975 if (do_source((char_u *)USR_VIMRC_FILE, TRUE, DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002976#ifdef USR_VIMRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002977 && do_source((char_u *)USR_VIMRC_FILE2, TRUE,
2978 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002979#endif
2980#ifdef USR_VIMRC_FILE3
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002981 && do_source((char_u *)USR_VIMRC_FILE3, TRUE,
2982 DOSO_VIMRC) == FAIL
Bram Moolenaar58d98232005-07-23 22:25:46 +00002983#endif
2984 && process_env((char_u *)"EXINIT", FALSE) == FAIL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002985 && do_source((char_u *)USR_EXRC_FILE, FALSE, DOSO_NONE) == FAIL)
Bram Moolenaar58d98232005-07-23 22:25:46 +00002986 {
2987#ifdef USR_EXRC_FILE2
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002988 (void)do_source((char_u *)USR_EXRC_FILE2, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00002989#endif
2990 }
2991 }
2992
2993 /*
2994 * Read initialization commands from ".vimrc" or ".exrc" in current
2995 * directory. This is only done if the 'exrc' option is set.
2996 * Because of security reasons we disallow shell and write commands
2997 * now, except for unix if the file is owned by the user or 'secure'
2998 * option has been reset in environment of global ".exrc" or ".vimrc".
2999 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
3000 * SYS_VIMRC_FILE.
3001 */
3002 if (p_exrc)
3003 {
3004#if defined(UNIX) || defined(VMS)
3005 /* If ".vimrc" file is not owned by user, set 'secure' mode. */
3006 if (!file_owned(VIMRC_FILE))
3007#endif
3008 secure = p_secure;
3009
3010 i = FAIL;
3011 if (fullpathcmp((char_u *)USR_VIMRC_FILE,
3012 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3013#ifdef USR_VIMRC_FILE2
3014 && fullpathcmp((char_u *)USR_VIMRC_FILE2,
3015 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3016#endif
3017#ifdef USR_VIMRC_FILE3
3018 && fullpathcmp((char_u *)USR_VIMRC_FILE3,
3019 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3020#endif
3021#ifdef SYS_VIMRC_FILE
3022 && fullpathcmp((char_u *)SYS_VIMRC_FILE,
3023 (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
3024#endif
3025 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003026 i = do_source((char_u *)VIMRC_FILE, TRUE, DOSO_VIMRC);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003027
3028 if (i == FAIL)
3029 {
3030#if defined(UNIX) || defined(VMS)
3031 /* if ".exrc" is not owned by user set 'secure' mode */
3032 if (!file_owned(EXRC_FILE))
3033 secure = p_secure;
3034 else
3035 secure = 0;
3036#endif
3037 if ( fullpathcmp((char_u *)USR_EXRC_FILE,
3038 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3039#ifdef USR_EXRC_FILE2
3040 && fullpathcmp((char_u *)USR_EXRC_FILE2,
3041 (char_u *)EXRC_FILE, FALSE) != FPC_SAME
3042#endif
3043 )
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003044 (void)do_source((char_u *)EXRC_FILE, FALSE, DOSO_NONE);
Bram Moolenaar58d98232005-07-23 22:25:46 +00003045 }
3046 }
3047 if (secure == 2)
3048 need_wait_return = TRUE;
3049 secure = 0;
3050#ifdef AMIGA
3051 proc->pr_WindowPtr = save_winptr;
3052#endif
3053 }
3054 TIME_MSG("sourcing vimrc file(s)");
3055}
3056
3057/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003058 * Setup to start using the GUI. Exit with an error when not available.
3059 */
3060 static void
3061main_start_gui()
3062{
3063#ifdef FEAT_GUI
3064 gui.starting = TRUE; /* start GUI a bit later */
3065#else
3066 mch_errmsg(_(e_nogvim));
3067 mch_errmsg("\n");
3068 mch_exit(2);
3069#endif
3070}
3071
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003072#endif /* NO_VIM_MAIN */
3073
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003074/*
Bram Moolenaar49325942007-05-10 19:19:59 +00003075 * Get an environment variable, and execute it as Ex commands.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003076 * Returns FAIL if the environment variable was not executed, OK otherwise.
3077 */
3078 int
3079process_env(env, is_viminit)
3080 char_u *env;
3081 int is_viminit; /* when TRUE, called for VIMINIT */
3082{
3083 char_u *initstr;
3084 char_u *save_sourcing_name;
3085 linenr_T save_sourcing_lnum;
3086#ifdef FEAT_EVAL
3087 scid_T save_sid;
3088#endif
3089
3090 if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
3091 {
3092 if (is_viminit)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003093 vimrc_found(NULL, NULL);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003094 save_sourcing_name = sourcing_name;
3095 save_sourcing_lnum = sourcing_lnum;
3096 sourcing_name = env;
3097 sourcing_lnum = 0;
3098#ifdef FEAT_EVAL
3099 save_sid = current_SID;
3100 current_SID = SID_ENV;
3101#endif
3102 do_cmdline_cmd(initstr);
3103 sourcing_name = save_sourcing_name;
3104 sourcing_lnum = save_sourcing_lnum;
3105#ifdef FEAT_EVAL
3106 current_SID = save_sid;;
3107#endif
3108 return OK;
3109 }
3110 return FAIL;
3111}
3112
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003113#if (defined(UNIX) || defined(VMS)) && !defined(NO_VIM_MAIN)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003114/*
3115 * Return TRUE if we are certain the user owns the file "fname".
3116 * Used for ".vimrc" and ".exrc".
3117 * Use both stat() and lstat() for extra security.
3118 */
3119 static int
3120file_owned(fname)
3121 char *fname;
3122{
3123 struct stat s;
3124# ifdef UNIX
3125 uid_t uid = getuid();
3126# else /* VMS */
3127 uid_t uid = ((getgid() << 16) | getuid());
3128# endif
3129
3130 return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
3131# ifdef HAVE_LSTAT
3132 || mch_lstat(fname, &s) != 0 || s.st_uid != uid
3133# endif
3134 );
3135}
3136#endif
3137
3138/*
3139 * Give an error message main_errors["n"] and exit.
3140 */
3141 static void
3142mainerr(n, str)
3143 int n; /* one of the ME_ defines */
3144 char_u *str; /* extra argument or NULL */
3145{
3146#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3147 reset_signals(); /* kill us with CTRL-C here, if you like */
3148#endif
3149
3150 mch_errmsg(longVersion);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003151 mch_errmsg("\n");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003152 mch_errmsg(_(main_errors[n]));
3153 if (str != NULL)
3154 {
3155 mch_errmsg(": \"");
3156 mch_errmsg((char *)str);
3157 mch_errmsg("\"");
3158 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003159 mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003160
3161 mch_exit(1);
3162}
3163
3164 void
3165mainerr_arg_missing(str)
3166 char_u *str;
3167{
3168 mainerr(ME_ARG_MISSING, str);
3169}
3170
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003171#ifndef NO_VIM_MAIN
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003172/*
3173 * print a message with three spaces prepended and '\n' appended.
3174 */
3175 static void
3176main_msg(s)
3177 char *s;
3178{
3179 mch_msg(" ");
3180 mch_msg(s);
3181 mch_msg("\n");
3182}
3183
3184/*
3185 * Print messages for "vim -h" or "vim --help" and exit.
3186 */
3187 static void
3188usage()
3189{
3190 int i;
3191 static char *(use[]) =
3192 {
3193 N_("[file ..] edit specified file(s)"),
3194 N_("- read text from stdin"),
3195 N_("-t tag edit file where tag is defined"),
3196#ifdef FEAT_QUICKFIX
3197 N_("-q [errorfile] edit file with first error")
3198#endif
3199 };
3200
3201#if defined(UNIX) || defined(__EMX__) || defined(VMS)
3202 reset_signals(); /* kill us with CTRL-C here, if you like */
3203#endif
3204
3205 mch_msg(longVersion);
3206 mch_msg(_("\n\nusage:"));
3207 for (i = 0; ; ++i)
3208 {
3209 mch_msg(_(" vim [arguments] "));
3210 mch_msg(_(use[i]));
3211 if (i == (sizeof(use) / sizeof(char_u *)) - 1)
3212 break;
3213 mch_msg(_("\n or:"));
3214 }
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003215#ifdef VMS
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003216 mch_msg(_("\nWhere case is ignored prepend / to make flag upper case"));
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003217#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003218
3219 mch_msg(_("\n\nArguments:\n"));
3220 main_msg(_("--\t\t\tOnly file names after this"));
3221#if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
3222 main_msg(_("--literal\t\tDon't expand wildcards"));
3223#endif
3224#ifdef FEAT_OLE
3225 main_msg(_("-register\t\tRegister this gvim for OLE"));
3226 main_msg(_("-unregister\t\tUnregister gvim for OLE"));
3227#endif
3228#ifdef FEAT_GUI
3229 main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
3230 main_msg(_("-f or --nofork\tForeground: Don't fork when starting GUI"));
3231#endif
3232 main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
3233 main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003234 main_msg(_("-E\t\t\tImproved Ex mode"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003235 main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
3236#ifdef FEAT_DIFF
3237 main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
3238#endif
3239 main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
3240 main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
3241 main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
3242 main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
3243 main_msg(_("-M\t\t\tModifications in text not allowed"));
3244 main_msg(_("-b\t\t\tBinary mode"));
3245#ifdef FEAT_LISP
3246 main_msg(_("-l\t\t\tLisp mode"));
3247#endif
3248 main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
3249 main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003250 main_msg(_("-V[N][fname]\t\tBe verbose [level N] [log messages to fname]"));
3251#ifdef FEAT_EVAL
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003252 main_msg(_("-D\t\t\tDebugging mode"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003253#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003254 main_msg(_("-n\t\t\tNo swap file, use memory only"));
3255 main_msg(_("-r\t\t\tList swap files and exit"));
3256 main_msg(_("-r (with file name)\tRecover crashed session"));
3257 main_msg(_("-L\t\t\tSame as -r"));
3258#ifdef AMIGA
3259 main_msg(_("-f\t\t\tDon't use newcli to open window"));
3260 main_msg(_("-dev <device>\t\tUse <device> for I/O"));
3261#endif
3262#ifdef FEAT_ARABIC
3263 main_msg(_("-A\t\t\tstart in Arabic mode"));
3264#endif
3265#ifdef FEAT_RIGHTLEFT
3266 main_msg(_("-H\t\t\tStart in Hebrew mode"));
3267#endif
3268#ifdef FEAT_FKMAP
3269 main_msg(_("-F\t\t\tStart in Farsi mode"));
3270#endif
3271 main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
3272 main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
3273#ifdef FEAT_GUI
3274 main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
3275#endif
3276 main_msg(_("--noplugin\t\tDon't load plugin scripts"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003277#ifdef FEAT_WINDOWS
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00003278 main_msg(_("-p[N]\t\tOpen N tab pages (default: one for each file)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003279 main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
3280 main_msg(_("-O[N]\t\tLike -o but split vertically"));
Bram Moolenaar8cfdc0d2007-05-06 14:12:36 +00003281#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003282 main_msg(_("+\t\t\tStart at end of file"));
3283 main_msg(_("+<lnum>\t\tStart at line <lnum>"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003284 main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003285 main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
3286 main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
3287 main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
3288 main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
3289 main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
3290#ifdef FEAT_CRYPT
3291 main_msg(_("-x\t\t\tEdit encrypted files"));
3292#endif
3293#if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
3294# if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
3295 main_msg(_("-display <display>\tConnect vim to this particular X-server"));
3296# endif
3297 main_msg(_("-X\t\t\tDo not connect to X server"));
3298#endif
3299#ifdef FEAT_CLIENTSERVER
3300 main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
3301 main_msg(_("--remote-silent <files> Same, don't complain if there is no server"));
3302 main_msg(_("--remote-wait <files> As --remote but wait for files to have been edited"));
3303 main_msg(_("--remote-wait-silent <files> Same, don't complain if there is no server"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003304# ifdef FEAT_WINDOWS
Bram Moolenaar82ad3242008-01-11 19:26:36 +00003305 main_msg(_("--remote-tab[-wait][-silent] <files> As --remote but use tab page per file"));
Bram Moolenaar0ce29932006-03-13 22:18:45 +00003306# endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003307 main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
3308 main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
3309 main_msg(_("--serverlist\t\tList available Vim server names and exit"));
3310 main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
3311#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003312#ifdef STARTUPTIME
Bram Moolenaar34ef52d2009-11-17 11:31:25 +00003313 main_msg(_("--startuptime <file>\tWrite startup timing messages to <file>"));
Bram Moolenaaref94eec2009-11-11 13:22:11 +00003314#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003315#ifdef FEAT_VIMINFO
3316 main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
3317#endif
3318 main_msg(_("-h or --help\tPrint Help (this message) and exit"));
3319 main_msg(_("--version\t\tPrint version information and exit"));
3320
3321#ifdef FEAT_GUI_X11
3322# ifdef FEAT_GUI_MOTIF
3323 mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
3324# else
3325# ifdef FEAT_GUI_ATHENA
3326# ifdef FEAT_GUI_NEXTAW
3327 mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
3328# else
3329 mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
3330# endif
3331# endif
3332# endif
3333 main_msg(_("-display <display>\tRun vim on <display>"));
3334 main_msg(_("-iconic\t\tStart vim iconified"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003335 main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
3336 main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
3337 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3338 main_msg(_("-boldfont <font>\tUse <font> for bold text"));
3339 main_msg(_("-italicfont <font>\tUse <font> for italic text"));
3340 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3341 main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
3342 main_msg(_("-scrollbarwidth <width> Use a scrollbar width of <width> (also: -sw)"));
3343# ifdef FEAT_GUI_ATHENA
3344 main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
3345# endif
3346 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3347 main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
3348 main_msg(_("-xrm <resource>\tSet the specified resource"));
3349#endif /* FEAT_GUI_X11 */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003350#ifdef FEAT_GUI_GTK
3351 mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
3352 main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
3353 main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
3354 main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
3355 main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003356 main_msg(_("--role <role>\tSet a unique role to identify the main window"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003357 main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
Bram Moolenaarf99bc6d2012-03-28 17:10:31 +02003358 main_msg(_("--echo-wid\t\tMake gvim echo the Window ID on stdout"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003359#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003360#ifdef FEAT_GUI_W32
3361 main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
Bram Moolenaar78e17622007-08-30 10:26:19 +00003362 main_msg(_("--windowid <HWND>\tOpen Vim inside another win32 widget"));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003363#endif
3364
3365#ifdef FEAT_GUI_GNOME
3366 /* Gnome gives extra messages for --help if we continue, but not for -h. */
3367 if (gui.starting)
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003368 {
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003369 mch_msg("\n");
Bram Moolenaarf4120a82011-12-08 15:57:59 +01003370 gui.dofork = FALSE;
3371 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003372 else
3373#endif
3374 mch_exit(0);
3375}
3376
Bram Moolenaard5bc83f2005-12-07 21:07:59 +00003377#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003378/*
3379 * Check the result of the ATTENTION dialog:
3380 * When "Quit" selected, exit Vim.
3381 * When "Recover" selected, recover the file.
3382 */
3383 static void
3384check_swap_exists_action()
3385{
3386 if (swap_exists_action == SEA_QUIT)
3387 getout(1);
3388 handle_swap_exists(NULL);
3389}
3390#endif
3391
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003392#endif
3393
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003394#if defined(STARTUPTIME) || defined(PROTO)
3395static void time_diff __ARGS((struct timeval *then, struct timeval *now));
3396
3397static struct timeval prev_timeval;
3398
Bram Moolenaar3f269672009-11-03 11:11:11 +00003399# ifdef WIN3264
3400/*
3401 * Windows doesn't have gettimeofday(), although it does have struct timeval.
3402 */
3403 static int
3404gettimeofday(struct timeval *tv, char *dummy)
3405{
3406 long t = clock();
3407 tv->tv_sec = t / CLOCKS_PER_SEC;
3408 tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
3409 return 0;
3410}
3411# endif
3412
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003413/*
3414 * Save the previous time before doing something that could nest.
3415 * set "*tv_rel" to the time elapsed so far.
3416 */
3417 void
3418time_push(tv_rel, tv_start)
3419 void *tv_rel, *tv_start;
3420{
3421 *((struct timeval *)tv_rel) = prev_timeval;
3422 gettimeofday(&prev_timeval, NULL);
3423 ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
3424 - ((struct timeval *)tv_rel)->tv_usec;
3425 ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
3426 - ((struct timeval *)tv_rel)->tv_sec;
3427 if (((struct timeval *)tv_rel)->tv_usec < 0)
3428 {
3429 ((struct timeval *)tv_rel)->tv_usec += 1000000;
3430 --((struct timeval *)tv_rel)->tv_sec;
3431 }
3432 *(struct timeval *)tv_start = prev_timeval;
3433}
3434
3435/*
3436 * Compute the previous time after doing something that could nest.
3437 * Subtract "*tp" from prev_timeval;
3438 * Note: The arguments are (void *) to avoid trouble with systems that don't
3439 * have struct timeval.
3440 */
3441 void
3442time_pop(tp)
3443 void *tp; /* actually (struct timeval *) */
3444{
3445 prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
3446 prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
3447 if (prev_timeval.tv_usec < 0)
3448 {
3449 prev_timeval.tv_usec += 1000000;
3450 --prev_timeval.tv_sec;
3451 }
3452}
3453
3454 static void
3455time_diff(then, now)
3456 struct timeval *then;
3457 struct timeval *now;
3458{
3459 long usec;
3460 long msec;
3461
3462 usec = now->tv_usec - then->tv_usec;
3463 msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
3464 usec = usec % 1000L;
3465 fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
3466}
3467
3468 void
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003469time_msg(mesg, tv_start)
3470 char *mesg;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003471 void *tv_start; /* only for do_source: start time; actually
3472 (struct timeval *) */
3473{
3474 static struct timeval start;
3475 struct timeval now;
3476
3477 if (time_fd != NULL)
3478 {
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003479 if (strstr(mesg, "STARTING") != NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003480 {
3481 gettimeofday(&start, NULL);
3482 prev_timeval = start;
3483 fprintf(time_fd, "\n\ntimes in msec\n");
3484 fprintf(time_fd, " clock self+sourced self: sourced script\n");
3485 fprintf(time_fd, " clock elapsed: other lines\n\n");
3486 }
3487 gettimeofday(&now, NULL);
3488 time_diff(&start, &now);
3489 if (((struct timeval *)tv_start) != NULL)
3490 {
3491 fprintf(time_fd, " ");
3492 time_diff(((struct timeval *)tv_start), &now);
3493 }
3494 fprintf(time_fd, " ");
3495 time_diff(&prev_timeval, &now);
3496 prev_timeval = now;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02003497 fprintf(time_fd, ": %s\n", mesg);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003498 }
3499}
3500
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003501#endif
3502
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003503#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003504
3505/*
3506 * Common code for the X command server and the Win32 command server.
3507 */
3508
Bram Moolenaareb94e552006-03-11 21:35:11 +00003509static char_u *build_drop_cmd __ARGS((int filec, char **filev, int tabs, int sendReply));
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003510
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003511/*
3512 * Do the client-server stuff, unless "--servername ''" was used.
3513 */
3514 static void
3515exec_on_server(parmp)
3516 mparm_T *parmp;
3517{
3518 if (parmp->serverName_arg == NULL || *parmp->serverName_arg != NUL)
3519 {
3520# ifdef WIN32
3521 /* Initialise the client/server messaging infrastructure. */
3522 serverInitMessaging();
3523# endif
3524
3525 /*
3526 * When a command server argument was found, execute it. This may
3527 * exit Vim when it was successful. Otherwise it's executed further
3528 * on. Remember the encoding used here in "serverStrEnc".
3529 */
3530 if (parmp->serverArg)
3531 {
3532 cmdsrv_main(&parmp->argc, parmp->argv,
3533 parmp->serverName_arg, &parmp->serverStr);
3534# ifdef FEAT_MBYTE
3535 parmp->serverStrEnc = vim_strsave(p_enc);
3536# endif
3537 }
3538
3539 /* If we're still running, get the name to register ourselves.
3540 * On Win32 can register right now, for X11 need to setup the
3541 * clipboard first, it's further down. */
3542 parmp->servername = serverMakeName(parmp->serverName_arg,
3543 parmp->argv[0]);
3544# ifdef WIN32
3545 if (parmp->servername != NULL)
3546 {
3547 serverSetName(parmp->servername);
3548 vim_free(parmp->servername);
3549 }
3550# endif
3551 }
3552}
3553
3554/*
3555 * Prepare for running as a Vim server.
3556 */
3557 static void
3558prepare_server(parmp)
3559 mparm_T *parmp;
3560{
3561# if defined(FEAT_X11)
3562 /*
3563 * Register for remote command execution with :serversend and --remote
3564 * unless there was a -X or a --servername '' on the command line.
3565 * Only register nongui-vim's with an explicit --servername argument.
Bram Moolenaar5f402312006-08-15 19:40:35 +00003566 * When running as root --servername is also required.
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003567 */
3568 if (X_DISPLAY != NULL && parmp->servername != NULL && (
3569# ifdef FEAT_GUI
Bram Moolenaar5f402312006-08-15 19:40:35 +00003570 (gui.in_use
3571# ifdef UNIX
Bram Moolenaar311d9822007-02-27 15:48:28 +00003572 && getuid() != ROOT_UID
Bram Moolenaar5f402312006-08-15 19:40:35 +00003573# endif
3574 ) ||
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003575# endif
3576 parmp->serverName_arg != NULL))
3577 {
3578 (void)serverRegisterName(X_DISPLAY, parmp->servername);
3579 vim_free(parmp->servername);
3580 TIME_MSG("register server name");
3581 }
3582 else
3583 serverDelayedStartName = parmp->servername;
3584# endif
3585
3586 /*
3587 * Execute command ourselves if we're here because the send failed (or
3588 * else we would have exited above).
3589 */
3590 if (parmp->serverStr != NULL)
3591 {
3592 char_u *p;
3593
3594 server_to_input_buf(serverConvert(parmp->serverStrEnc,
3595 parmp->serverStr, &p));
3596 vim_free(p);
3597 }
3598}
3599
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003600 static void
3601cmdsrv_main(argc, argv, serverName_arg, serverStr)
3602 int *argc;
3603 char **argv;
3604 char_u *serverName_arg;
3605 char_u **serverStr;
3606{
3607 char_u *res;
3608 int i;
3609 char_u *sname;
3610 int ret;
3611 int didone = FALSE;
3612 int exiterr = 0;
3613 char **newArgV = argv + 1;
3614 int newArgC = 1,
3615 Argc = *argc;
3616 int argtype;
3617#define ARGTYPE_OTHER 0
3618#define ARGTYPE_EDIT 1
3619#define ARGTYPE_EDIT_WAIT 2
3620#define ARGTYPE_SEND 3
3621 int silent = FALSE;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003622 int tabs = FALSE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003623# ifndef FEAT_X11
3624 HWND srv;
3625# else
3626 Window srv;
3627
3628 setup_term_clip();
3629# endif
3630
3631 sname = serverMakeName(serverName_arg, argv[0]);
3632 if (sname == NULL)
3633 return;
3634
3635 /*
3636 * Execute the command server related arguments and remove them
3637 * from the argc/argv array; We may have to return into main()
3638 */
3639 for (i = 1; i < Argc; i++)
3640 {
3641 res = NULL;
Bram Moolenaarc013cb62005-07-24 21:18:31 +00003642 if (STRCMP(argv[i], "--") == 0) /* end of option arguments */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003643 {
3644 for (; i < *argc; i++)
3645 {
3646 *newArgV++ = argv[i];
3647 newArgC++;
3648 }
3649 break;
3650 }
3651
Bram Moolenaareb94e552006-03-11 21:35:11 +00003652 if (STRICMP(argv[i], "--remote-send") == 0)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003653 argtype = ARGTYPE_SEND;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003654 else if (STRNICMP(argv[i], "--remote", 8) == 0)
3655 {
3656 char *p = argv[i] + 8;
3657
3658 argtype = ARGTYPE_EDIT;
3659 while (*p != NUL)
3660 {
3661 if (STRNICMP(p, "-wait", 5) == 0)
3662 {
3663 argtype = ARGTYPE_EDIT_WAIT;
3664 p += 5;
3665 }
3666 else if (STRNICMP(p, "-silent", 7) == 0)
3667 {
3668 silent = TRUE;
3669 p += 7;
3670 }
3671 else if (STRNICMP(p, "-tab", 4) == 0)
3672 {
3673 tabs = TRUE;
3674 p += 4;
3675 }
3676 else
3677 {
3678 argtype = ARGTYPE_OTHER;
3679 break;
3680 }
3681 }
3682 }
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003683 else
3684 argtype = ARGTYPE_OTHER;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003685
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003686 if (argtype != ARGTYPE_OTHER)
3687 {
3688 if (i == *argc - 1)
3689 mainerr_arg_missing((char_u *)argv[i]);
3690 if (argtype == ARGTYPE_SEND)
3691 {
3692 *serverStr = (char_u *)argv[i + 1];
3693 i++;
3694 }
3695 else
3696 {
3697 *serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
Bram Moolenaareb94e552006-03-11 21:35:11 +00003698 tabs, argtype == ARGTYPE_EDIT_WAIT);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003699 if (*serverStr == NULL)
3700 {
3701 /* Probably out of memory, exit. */
3702 didone = TRUE;
3703 exiterr = 1;
3704 break;
3705 }
3706 Argc = i;
3707 }
3708# ifdef FEAT_X11
3709 if (xterm_dpy == NULL)
3710 {
3711 mch_errmsg(_("No display"));
3712 ret = -1;
3713 }
3714 else
3715 ret = serverSendToVim(xterm_dpy, sname, *serverStr,
3716 NULL, &srv, 0, 0, silent);
3717# else
3718 /* Win32 always works? */
3719 ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
3720# endif
3721 if (ret < 0)
3722 {
3723 if (argtype == ARGTYPE_SEND)
3724 {
3725 /* Failed to send, abort. */
3726 mch_errmsg(_(": Send failed.\n"));
3727 didone = TRUE;
3728 exiterr = 1;
3729 }
3730 else if (!silent)
3731 /* Let vim start normally. */
3732 mch_errmsg(_(": Send failed. Trying to execute locally\n"));
3733 break;
3734 }
3735
3736# ifdef FEAT_GUI_W32
3737 /* Guess that when the server name starts with "g" it's a GUI
3738 * server, which we can bring to the foreground here.
3739 * Foreground() in the server doesn't work very well. */
3740 if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
3741 SetForegroundWindow(srv);
3742# endif
3743
3744 /*
3745 * For --remote-wait: Wait until the server did edit each
3746 * file. Also detect that the server no longer runs.
3747 */
3748 if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
3749 {
3750 int numFiles = *argc - i - 1;
3751 int j;
3752 char_u *done = alloc(numFiles);
3753 char_u *p;
3754# ifdef FEAT_GUI_W32
3755 NOTIFYICONDATA ni;
3756 int count = 0;
3757 extern HWND message_window;
3758# endif
3759
3760 if (numFiles > 0 && argv[i + 1][0] == '+')
3761 /* Skip "+cmd" argument, don't wait for it to be edited. */
3762 --numFiles;
3763
3764# ifdef FEAT_GUI_W32
3765 ni.cbSize = sizeof(ni);
3766 ni.hWnd = message_window;
3767 ni.uID = 0;
3768 ni.uFlags = NIF_ICON|NIF_TIP;
3769 ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
3770 sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
3771 Shell_NotifyIcon(NIM_ADD, &ni);
3772# endif
3773
3774 /* Wait for all files to unload in remote */
Bram Moolenaar7db5fc82010-05-24 11:59:29 +02003775 vim_memset(done, 0, numFiles);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003776 while (memchr(done, 0, numFiles) != NULL)
3777 {
3778# ifdef WIN32
3779 p = serverGetReply(srv, NULL, TRUE, TRUE);
3780 if (p == NULL)
3781 break;
3782# else
3783 if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
3784 break;
3785# endif
3786 j = atoi((char *)p);
3787 if (j >= 0 && j < numFiles)
3788 {
3789# ifdef FEAT_GUI_W32
3790 ++count;
3791 sprintf(ni.szTip, _("%d of %d edited"),
3792 count, numFiles);
3793 Shell_NotifyIcon(NIM_MODIFY, &ni);
3794# endif
3795 done[j] = 1;
3796 }
3797 }
3798# ifdef FEAT_GUI_W32
3799 Shell_NotifyIcon(NIM_DELETE, &ni);
3800# endif
3801 }
3802 }
3803 else if (STRICMP(argv[i], "--remote-expr") == 0)
3804 {
3805 if (i == *argc - 1)
3806 mainerr_arg_missing((char_u *)argv[i]);
3807# ifdef WIN32
3808 /* Win32 always works? */
3809 if (serverSendToVim(sname, (char_u *)argv[i + 1],
3810 &res, NULL, 1, FALSE) < 0)
3811# else
3812 if (xterm_dpy == NULL)
3813 mch_errmsg(_("No display: Send expression failed.\n"));
3814 else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
3815 &res, NULL, 1, 1, FALSE) < 0)
3816# endif
3817 {
3818 if (res != NULL && *res != NUL)
3819 {
3820 /* Output error from remote */
3821 mch_errmsg((char *)res);
3822 vim_free(res);
3823 res = NULL;
3824 }
3825 mch_errmsg(_(": Send expression failed.\n"));
3826 }
3827 }
3828 else if (STRICMP(argv[i], "--serverlist") == 0)
3829 {
3830# ifdef WIN32
3831 /* Win32 always works? */
3832 res = serverGetVimNames();
3833# else
3834 if (xterm_dpy != NULL)
3835 res = serverGetVimNames(xterm_dpy);
3836# endif
3837 if (called_emsg)
3838 mch_errmsg("\n");
3839 }
3840 else if (STRICMP(argv[i], "--servername") == 0)
3841 {
Bram Moolenaar5d985b92009-12-16 17:28:07 +00003842 /* Already processed. Take it out of the command line */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003843 i++;
3844 continue;
3845 }
3846 else
3847 {
3848 *newArgV++ = argv[i];
3849 newArgC++;
3850 continue;
3851 }
3852 didone = TRUE;
3853 if (res != NULL && *res != NUL)
3854 {
3855 mch_msg((char *)res);
3856 if (res[STRLEN(res) - 1] != '\n')
3857 mch_msg("\n");
3858 }
3859 vim_free(res);
3860 }
3861
3862 if (didone)
3863 {
3864 display_errors(); /* display any collected messages */
3865 exit(exiterr); /* Mission accomplished - get out */
3866 }
3867
3868 /* Return back into main() */
3869 *argc = newArgC;
3870 vim_free(sname);
3871}
3872
3873/*
3874 * Build a ":drop" command to send to a Vim server.
3875 */
3876 static char_u *
Bram Moolenaareb94e552006-03-11 21:35:11 +00003877build_drop_cmd(filec, filev, tabs, sendReply)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003878 int filec;
3879 char **filev;
Bram Moolenaareb94e552006-03-11 21:35:11 +00003880 int tabs; /* Use ":tab drop" instead of ":drop". */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003881 int sendReply;
3882{
3883 garray_T ga;
3884 int i;
3885 char_u *inicmd = NULL;
3886 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003887 char_u *cwd;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003888
3889 if (filec > 0 && filev[0][0] == '+')
3890 {
3891 inicmd = (char_u *)filev[0] + 1;
3892 filev++;
3893 filec--;
3894 }
3895 /* Check if we have at least one argument. */
3896 if (filec <= 0)
3897 mainerr_arg_missing((char_u *)filev[-1]);
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003898
3899 /* Temporarily cd to the current directory to handle relative file names. */
Bram Moolenaard9462e32011-04-11 21:35:11 +02003900 cwd = alloc(MAXPATHL);
3901 if (cwd == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003902 return NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +02003903 if (mch_dirname(cwd, MAXPATHL) != OK)
3904 {
3905 vim_free(cwd);
3906 return NULL;
3907 }
3908 p = vim_strsave_escaped_ext(cwd,
Bram Moolenaar02b06312007-09-06 15:39:22 +00003909#ifdef BACKSLASH_IN_FILENAME
3910 "", /* rem_backslash() will tell what chars to escape */
3911#else
3912 PATH_ESC_CHARS,
3913#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +02003914 '\\', TRUE);
3915 vim_free(cwd);
3916 if (p == NULL)
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003917 return NULL;
3918 ga_init2(&ga, 1, 100);
3919 ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3920 ga_concat(&ga, p);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003921 vim_free(p);
Bram Moolenaareb94e552006-03-11 21:35:11 +00003922
3923 /* Call inputsave() so that a prompt for an encryption key works. */
3924 ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|");
3925 if (tabs)
3926 ga_concat(&ga, (char_u *)"tab ");
3927 ga_concat(&ga, (char_u *)"drop");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003928 for (i = 0; i < filec; i++)
3929 {
3930 /* On Unix the shell has already expanded the wildcards, don't want to
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003931 * do it again in the Vim server. On MS-Windows only escape
3932 * non-wildcard characters. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003933 p = vim_strsave_escaped((char_u *)filev[i],
3934#ifdef UNIX
3935 PATH_ESC_CHARS
3936#else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003937 (char_u *)" \t%#"
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003938#endif
3939 );
3940 if (p == NULL)
3941 {
3942 vim_free(ga.ga_data);
3943 return NULL;
3944 }
3945 ga_concat(&ga, (char_u *)" ");
3946 ga_concat(&ga, p);
3947 vim_free(p);
3948 }
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003949 ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3950
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003951 /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3952 * CTRL-\ CTRL-N again. */
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003953 ga_concat(&ga, (char_u *)"<C-\\><C-N>");
3954
3955 /* Switch back to the correct current directory (prior to temporary path
3956 * switch) unless 'autochdir' is set, in which case it will already be
3957 * correct after the :drop command. */
3958 ga_concat(&ga, (char_u *)":if !exists('+acd')||!&acd|cd -|endif<CR>");
3959
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003960 if (sendReply)
Bram Moolenaar00b78c12010-11-16 16:25:51 +01003961 ga_concat(&ga, (char_u *)":call SetupRemoteReplies()<CR>");
3962 ga_concat(&ga, (char_u *)":");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003963 if (inicmd != NULL)
3964 {
3965 /* Can't use <CR> after "inicmd", because an "startinsert" would cause
3966 * the following commands to be inserted as text. Use a "|",
3967 * hopefully "inicmd" does allow this... */
3968 ga_concat(&ga, inicmd);
3969 ga_concat(&ga, (char_u *)"|");
3970 }
3971 /* Bring the window to the foreground, goto Insert mode when 'im' set and
3972 * clear command line. */
Bram Moolenaar567e4de2004-12-31 21:01:02 +00003973 ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|redr|f<CR>");
Bram Moolenaarb4210b32004-06-13 14:51:16 +00003974 ga_append(&ga, NUL);
3975 return ga.ga_data;
3976}
3977
3978/*
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01003979 * Make our basic server name: use the specified "arg" if given, otherwise use
3980 * the tail of the command "cmd" we were started with.
3981 * Return the name in allocated memory. This doesn't include a serial number.
3982 */
3983 static char_u *
3984serverMakeName(arg, cmd)
3985 char_u *arg;
3986 char *cmd;
3987{
3988 char_u *p;
3989
3990 if (arg != NULL && *arg != NUL)
3991 p = vim_strsave_up(arg);
3992 else
3993 {
3994 p = vim_strsave_up(gettail((char_u *)cmd));
3995 /* Remove .exe or .bat from the name. */
3996 if (p != NULL && vim_strchr(p, '.') != NULL)
3997 *vim_strchr(p, '.') = NUL;
3998 }
3999 return p;
4000}
4001#endif /* FEAT_CLIENTSERVER */
4002
4003#if defined(FEAT_CLIENTSERVER) || defined(PROTO)
4004/*
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004005 * Replace termcodes such as <CR> and insert as key presses if there is room.
4006 */
4007 void
4008server_to_input_buf(str)
4009 char_u *str;
4010{
4011 char_u *ptr = NULL;
4012 char_u *cpo_save = p_cpo;
4013
4014 /* Set 'cpoptions' the way we want it.
4015 * B set - backslashes are *not* treated specially
4016 * k set - keycodes are *not* reverse-engineered
4017 * < unset - <Key> sequences *are* interpreted
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004018 * The last but one parameter of replace_termcodes() is TRUE so that the
4019 * <lt> sequence is recognised - needed for a real backslash.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004020 */
4021 p_cpo = (char_u *)"Bk";
Bram Moolenaar8b2d9c42006-05-03 21:28:47 +00004022 str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE, FALSE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004023 p_cpo = cpo_save;
4024
4025 if (*ptr != NUL) /* trailing CTRL-V results in nothing */
4026 {
4027 /*
4028 * Add the string to the input stream.
4029 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
4030 *
4031 * First clear typed characters from the typeahead buffer, there could
4032 * be half a mapping there. Then append to the existing string, so
4033 * that multiple commands from a client are concatenated.
4034 */
4035 if (typebuf.tb_maplen < typebuf.tb_len)
4036 del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
4037 (void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
4038
4039 /* Let input_available() know we inserted text in the typeahead
4040 * buffer. */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004041 typebuf_was_filled = TRUE;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004042 }
4043 vim_free((char_u *)ptr);
4044}
4045
4046/*
4047 * Evaluate an expression that the client sent to a string.
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004048 */
4049 char_u *
4050eval_client_expr_to_string(expr)
4051 char_u *expr;
4052{
4053 char_u *res;
4054 int save_dbl = debug_break_level;
4055 int save_ro = redir_off;
4056
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004057 /* Disable debugging, otherwise Vim hangs, waiting for "cont" to be
4058 * typed. */
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004059 debug_break_level = -1;
4060 redir_off = 0;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004061 /* Do not display error message, otherwise Vim hangs, waiting for "cont"
4062 * to be typed. Do generate errors so that try/catch works. */
4063 ++emsg_silent;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004064
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004065 res = eval_to_string(expr, NULL, TRUE);
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004066
4067 debug_break_level = save_dbl;
4068 redir_off = save_ro;
Bram Moolenaar1e284f52013-03-13 20:23:22 +01004069 --emsg_silent;
4070 if (emsg_silent < 0)
4071 emsg_silent = 0;
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004072
Bram Moolenaar63a121b2005-12-11 21:36:39 +00004073 /* A client can tell us to redraw, but not to display the cursor, so do
4074 * that here. */
4075 setcursor();
4076 out_flush();
4077#ifdef FEAT_GUI
4078 if (gui.in_use)
4079 gui_update_cursor(FALSE, FALSE);
4080#endif
4081
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004082 return res;
4083}
4084
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004085/*
4086 * If conversion is needed, convert "data" from "client_enc" to 'encoding' and
4087 * return an allocated string. Otherwise return "data".
4088 * "*tofree" is set to the result when it needs to be freed later.
4089 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004090 char_u *
4091serverConvert(client_enc, data, tofree)
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00004092 char_u *client_enc UNUSED;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004093 char_u *data;
4094 char_u **tofree;
4095{
4096 char_u *res = data;
4097
4098 *tofree = NULL;
4099# ifdef FEAT_MBYTE
4100 if (client_enc != NULL && p_enc != NULL)
4101 {
4102 vimconv_T vimconv;
4103
4104 vimconv.vc_type = CONV_NONE;
4105 if (convert_setup(&vimconv, client_enc, p_enc) != FAIL
4106 && vimconv.vc_type != CONV_NONE)
4107 {
4108 res = string_convert(&vimconv, data, NULL);
4109 if (res == NULL)
4110 res = data;
4111 else
4112 *tofree = res;
4113 }
4114 convert_setup(&vimconv, NULL, NULL);
4115 }
4116# endif
4117 return res;
4118}
Bram Moolenaarb05b10a2011-03-22 18:10:45 +01004119#endif
Bram Moolenaarb4210b32004-06-13 14:51:16 +00004120
4121/*
4122 * When FEAT_FKMAP is defined, also compile the Farsi source code.
4123 */
4124#if defined(FEAT_FKMAP) || defined(PROTO)
4125# include "farsi.c"
4126#endif
4127
4128/*
4129 * When FEAT_ARABIC is defined, also compile the Arabic source code.
4130 */
4131#if defined(FEAT_ARABIC) || defined(PROTO)
4132# include "arabic.c"
4133#endif