xref: /vim-8.2.3635/src/main.c (revision 2df6dcc5)
1 /* 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 
10 #if defined(MSDOS) || defined(WIN32) || defined(_WIN64)
11 # include <io.h>		/* for close() and dup() */
12 #endif
13 
14 #define EXTERN
15 #include "vim.h"
16 
17 #ifdef SPAWNO
18 # include <spawno.h>		/* special MSDOS swapping library */
19 #endif
20 
21 #ifdef HAVE_FCNTL_H
22 # include <fcntl.h>
23 #endif
24 
25 #ifdef __CYGWIN__
26 # ifndef WIN32
27 #  include <sys/cygwin.h>	/* for cygwin_conv_to_posix_path() */
28 # endif
29 # include <limits.h>
30 #endif
31 
32 #if defined(UNIX) || defined(VMS)
33 static int file_owned __ARGS((char *fname));
34 #endif
35 static void mainerr __ARGS((int, char_u *));
36 static void main_msg __ARGS((char *s));
37 static void usage __ARGS((void));
38 static int get_number_arg __ARGS((char_u *p, int *idx, int def));
39 static void main_start_gui __ARGS((void));
40 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
41 static void check_swap_exists_action __ARGS((void));
42 #endif
43 #ifdef FEAT_CLIENTSERVER
44 static void cmdsrv_main __ARGS((int *argc, char **argv, char_u *serverName_arg, char_u **serverStr));
45 static char_u *serverMakeName __ARGS((char_u *arg, char *cmd));
46 #endif
47 
48 
49 #ifdef STARTUPTIME
50 static FILE *time_fd = NULL;
51 #endif
52 
53 #define FEAT_PRECOMMANDS
54 
55 /*
56  * Different types of error messages.
57  */
58 static char *(main_errors[]) =
59 {
60     N_("Unknown option"),
61 #define ME_UNKNOWN_OPTION	0
62     N_("Too many edit arguments"),
63 #define ME_TOO_MANY_ARGS	1
64     N_("Argument missing after"),
65 #define ME_ARG_MISSING		2
66     N_("Garbage after option"),
67 #define ME_GARBAGE		3
68     N_("Too many \"+command\", \"-c command\" or \"--cmd command\" arguments"),
69 #define ME_EXTRA_CMD		4
70     N_("Invalid argument for"),
71 #define ME_INVALID_ARG		5
72 };
73 
74 /* Maximum number of commands from + or -c options */
75 #define MAX_ARG_CMDS 10
76 
77 #ifndef PROTO	    /* don't want a prototype for main() */
78     int
79 # ifdef VIMDLL
80 _export
81 # endif
82 # ifdef FEAT_GUI_MSWIN
83 #  ifdef __BORLANDC__
84 _cdecl
85 #  endif
86 VimMain
87 # else
88 main
89 # endif
90 (argc, argv)
91     int		argc;
92     char	**argv;
93 {
94     char_u	*initstr;		/* init string from environment */
95     char_u	*term = NULL;		/* specified terminal name */
96     char_u	*fname = NULL;		/* file name from command line */
97     char_u	*tagname = NULL;	/* tag from -t option */
98     char_u	*use_vimrc = NULL;	/* vimrc from -u option */
99 #ifdef FEAT_QUICKFIX
100     char_u	*use_ef = NULL;		/* 'errorfile' from -q option */
101 #endif
102 #ifdef FEAT_CRYPT
103     int		ask_for_key = FALSE;    /* -x argument */
104 #endif
105     int		n_commands = 0;		/* no. of commands from + or -c */
106     char_u	*commands[MAX_ARG_CMDS]; /* commands from + or -c option */
107 #ifdef FEAT_PRECOMMANDS
108     int		p_commands = 0;		/* no. of commands from --cmd */
109     char_u	*pre_commands[MAX_ARG_CMDS]; /* commands from --cmd option */
110 #endif
111     int		no_swap_file = FALSE;   /* "-n" option used */
112     int		c;
113     int		i;
114     char_u	*p = NULL;
115     int		bin_mode = FALSE;	/* -b option used */
116 #ifdef FEAT_EVAL
117     int		use_debug_break_level = -1;
118 #endif
119 #ifdef FEAT_WINDOWS
120     int		window_count = -1;	/* number of windows to use */
121     int		arg_idx;		/* index in argument list */
122     int		vert_windows = MAYBE;   /* "-O" used instead of "-o" */
123 #endif
124     int		had_minmin = FALSE;	/* found "--" option */
125     int		argv_idx;		/* index in argv[n][] */
126     int		want_full_screen = TRUE;
127     int		want_argument;		/* option with argument */
128 #define EDIT_NONE   0	    /* no edit type yet */
129 #define EDIT_FILE   1	    /* file name argument[s] given, use argument list */
130 #define EDIT_STDIN  2	    /* read file from stdin */
131 #define EDIT_TAG    3	    /* tag name argument given, use tagname */
132 #define EDIT_QF	    4	    /* start in quickfix mode */
133     int		edit_type = EDIT_NONE;  /* type of editing to do */
134 #ifdef FEAT_DIFF
135     int		diff_mode = FALSE;	/* start with 'diff' set */
136 #endif
137     int		evim_mode = FALSE;	/* started as "evim" */
138     int		stdout_isatty;		/* is stdout a terminal? */
139     int		input_isatty;		/* is active input a terminal? */
140 #ifdef MSWIN
141     int		full_path = FALSE;
142 #endif
143 #ifdef FEAT_CLIENTSERVER
144     char_u	*serverStr = NULL;
145     char_u	*serverName_arg = NULL;	/* cmdline arg for server name */
146     int		serverArg = FALSE;	/* TRUE when argument for a server */
147     char_u	*servername = NULL;	/* allocated name for our server */
148 #endif
149 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
150     int		literal = FALSE;	/* don't expand file names */
151 #endif
152 
153 # ifdef NBDEBUG
154     nbdebug_log_init("SPRO_GVIM_DEBUG", "SPRO_GVIM_DLEVEL");
155     nbdebug_wait(WT_ENV | WT_WAIT | WT_STOP, "SPRO_GVIM_WAIT", 20);
156 # endif
157 
158     /*
159      * Do any system-specific initialisations.  These can NOT use IObuff or
160      * NameBuff.  Thus emsg2() cannot be called!
161      */
162     mch_early_init();
163 
164 #ifdef FEAT_TCL
165     vim_tcl_init(argv[0]);
166 #endif
167 
168 #ifdef MEM_PROFILE
169     atexit(vim_mem_profile_dump);
170 #endif
171 
172 #ifdef STARTUPTIME
173     time_fd = fopen(STARTUPTIME, "a");
174     TIME_MSG("--- VIM STARTING ---");
175 #endif
176 
177 #ifdef __EMX__
178     _wildcard(&argc, &argv);
179 #endif
180 
181 #ifdef FEAT_MBYTE
182     (void)mb_init();	/* init mb_bytelen_tab[] to ones */
183 #endif
184 
185 #ifdef __QNXNTO__
186     qnx_init();		/* PhAttach() for clipboard, (and gui) */
187 #endif
188 
189 #ifdef MAC_OS_CLASSIC
190     /* Macintosh needs this before any memory is allocated. */
191     gui_prepare(&argc, argv);	/* Prepare for possibly starting GUI sometime */
192     TIME_MSG("GUI prepared");
193 #endif
194 
195     /* Init the table of Normal mode commands. */
196     init_normal_cmds();
197 
198 #if defined(HAVE_DATE_TIME) && defined(VMS) && defined(VAXC)
199     make_version();
200 #endif
201 
202     /*
203      * Allocate space for the generic buffers (needed for set_init_1() and
204      * EMSG2()).
205      */
206     if ((IObuff = alloc(IOSIZE)) == NULL
207 	    || (NameBuff = alloc(MAXPATHL)) == NULL)
208 	mch_exit(0);
209 
210     TIME_MSG("Allocated generic buffers");
211 
212 #if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
213     /*
214      * Setup to use the current locale (for ctype() and many other things).
215      * NOTE: Translated messages with encodings other than latin1 will not
216      * work until set_init_1() has been called!
217      */
218     setlocale(LC_ALL, "");
219 
220 # ifdef FEAT_GETTEXT
221     {
222 	int	mustfree = FALSE;
223 
224 #  ifdef DYNAMIC_GETTEXT
225 	/* Initialize the gettext library */
226 	dyn_libintl_init(NULL);
227 #  endif
228 	/* expand_env() doesn't work yet, because chartab[] is not initialized
229 	 * yet, call vim_getenv() directly */
230 	p = vim_getenv((char_u *)"VIMRUNTIME", &mustfree);
231 	if (p != NULL && *p != NUL)
232 	{
233 	    STRCPY(NameBuff, p);
234 	    STRCAT(NameBuff, "/lang");
235 	    bindtextdomain(VIMPACKAGE, (char *)NameBuff);
236 	}
237 	if (mustfree)
238 	    vim_free(p);
239 	textdomain(VIMPACKAGE);
240     }
241 # endif
242     TIME_MSG("locale set");
243 #endif
244 
245 #ifdef FEAT_GUI
246     gui.dofork = TRUE;		    /* default is to use fork() */
247 #endif
248 
249 #if defined(FEAT_XCLIPBOARD) || defined(FEAT_CLIENTSERVER)
250     /*
251      * Get the name of the display, before gui_prepare() removes it from
252      * argv[].  Used for the xterm-clipboard display.
253      *
254      * Also find the --server... arguments
255      */
256     for (i = 1; i < argc; i++)
257     {
258 	if (STRCMP(argv[i], "--") == 0)
259 	    break;
260 # ifdef FEAT_XCLIPBOARD
261 	else if (STRICMP(argv[i], "-display") == 0
262 #  if defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE)
263 		|| STRICMP(argv[i], "--display") == 0
264 #  endif
265 		)
266 	{
267 	    if (i == argc - 1)
268 		mainerr_arg_missing((char_u *)argv[i]);
269 	    xterm_display = argv[++i];
270 	}
271 # endif
272 # ifdef FEAT_CLIENTSERVER
273 	else if (STRICMP(argv[i], "--servername") == 0)
274 	{
275 	    if (i == argc - 1)
276 		mainerr_arg_missing((char_u *)argv[i]);
277 	    serverName_arg = (char_u *)argv[++i];
278 	}
279 	else if (STRICMP(argv[i], "--serverlist") == 0
280 		 || STRICMP(argv[i], "--remote-send") == 0
281 		 || STRICMP(argv[i], "--remote-expr") == 0
282 		 || STRICMP(argv[i], "--remote") == 0
283 		 || STRICMP(argv[i], "--remote-silent") == 0)
284 	    serverArg = TRUE;
285 	else if (STRICMP(argv[i], "--remote-wait") == 0
286 		|| STRICMP(argv[i], "--remote-wait-silent") == 0)
287 	{
288 	    serverArg = TRUE;
289 #ifdef FEAT_GUI
290 	    /* don't fork() when starting the GUI to edit the files ourself */
291 	    gui.dofork = FALSE;
292 #endif
293 	}
294 # endif
295 # ifdef FEAT_GUI_GTK
296 	else if (STRICMP(argv[i], "--socketid") == 0)
297 	{
298 	    unsigned int    socket_id;
299 	    int		    count;
300 
301 	    if (i == argc - 1)
302 		mainerr_arg_missing((char_u *)argv[i]);
303 	    if (STRNICMP(argv[i+1], "0x", 2) == 0)
304 		count = sscanf(&(argv[i + 1][2]), "%x", &socket_id);
305 	    else
306 		count = sscanf(argv[i+1], "%u", &socket_id);
307 	    if (count != 1)
308 		mainerr(ME_INVALID_ARG, (char_u *)argv[i]);
309 	    else
310 		gtk_socket_id = socket_id;
311 	    i++;
312 	}
313 	else if (STRICMP(argv[i], "--echo-wid") == 0)
314 	    echo_wid_arg = TRUE;
315 # endif
316     }
317 #endif
318 
319 #ifdef FEAT_SUN_WORKSHOP
320     findYourself(argv[0]);
321 #endif
322 #if defined(FEAT_GUI) && !defined(MAC_OS_CLASSIC)
323     gui_prepare(&argc, argv);	/* Prepare for possibly starting GUI sometime */
324     TIME_MSG("GUI prepared");
325 #endif
326 
327 #ifdef FEAT_CLIPBOARD
328     clip_init(FALSE);		/* Initialise clipboard stuff */
329     TIME_MSG("clipboard setup");
330 #endif
331 
332     /*
333      * Check if we have an interactive window.
334      * On the Amiga: If there is no window, we open one with a newcli command
335      * (needed for :! to * work). mch_check_win() will also handle the -d or
336      * -dev argument.
337      */
338     stdout_isatty = (mch_check_win(argc, argv) != FAIL);
339     TIME_MSG("window checked");
340 
341     /*
342      * Allocate the first window and buffer. Can't do much without it.
343      */
344     win_alloc_first();
345 
346     init_yank();		/* init yank buffers */
347 
348     /* Init the argument list to empty. */
349     alist_init(&global_alist);
350 
351     /*
352      * Set the default values for the options.
353      * NOTE: Non-latin1 translated messages are working only after this,
354      * because this is where "has_mbyte" will be set, which is used by
355      * msg_outtrans_len_attr().
356      * First find out the home directory, needed to expand "~" in options.
357      */
358     init_homedir();		/* find real value of $HOME */
359     set_init_1();
360     TIME_MSG("inits 1");
361 
362 #ifdef FEAT_EVAL
363     set_lang_var();		/* set v:lang and v:ctype */
364 #endif
365 
366 #ifdef FEAT_CLIENTSERVER
367     /*
368      * Do the client-server stuff, unless "--servername ''" was used.
369      */
370     if (serverName_arg == NULL || *serverName_arg != NUL)
371     {
372 # ifdef WIN32
373 	/* Initialise the client/server messaging infrastructure. */
374 	serverInitMessaging();
375 # endif
376 
377 	/*
378 	 * When a command server argument was found, execute it.  This may
379 	 * exit Vim when it was successful.
380 	 */
381 	if (serverArg)
382 	    cmdsrv_main(&argc, argv, serverName_arg, &serverStr);
383 
384 	/* If we're still running, get the name to register ourselves.
385 	 * On Win32 can register right now, for X11 need to setup the
386 	 * clipboard first, it's further down. */
387 	servername = serverMakeName(serverName_arg, argv[0]);
388 # ifdef WIN32
389 	if (servername != NULL)
390 	{
391 	    serverSetName(servername);
392 	    vim_free(servername);
393 	}
394 # endif
395     }
396 #endif
397 
398     /*
399      * Check for: [r][e][g][vi|vim|view][diff][ex[im]]
400      * If the executable name starts with "r" we disable shell commands.
401      * If the next character is "e" we run in Easy mode.
402      * If the next character is "g" we run the GUI version.
403      * If the next characters are "view" we start in readonly mode.
404      * If the next characters are "diff" or "vimdiff" we start in diff mode.
405      * If the next characters are "ex" we start in Ex mode.  If it's followed
406      * by "im" use improved Ex mode.
407      */
408     initstr = gettail((char_u *)argv[0]);
409 
410 #ifdef MACOS_X_UNIX
411     /* An issue has been seen when launching Vim in such a way that
412      * $PWD/$ARGV[0] or $ARGV[0] is not the absolute path to the
413      * executable or a symbolic link of it. Until this issue is resolved
414      * we prohibit the GUI from being used.
415      */
416     if (STRCMP(initstr, argv[0]) == 0)
417 	disallow_gui = TRUE;
418 #endif
419 
420 #ifdef FEAT_EVAL
421     set_vim_var_string(VV_PROGNAME, initstr, -1);
422 #endif
423 
424     /* TODO: On MacOS X default to gui if argv[0] ends in:
425      *       /vim.app/Contents/MacOS/Vim */
426 
427     if (TOLOWER_ASC(initstr[0]) == 'r')
428     {
429 	restricted = TRUE;
430 	++initstr;
431     }
432 
433     /* Avoid using evim mode for "editor". */
434     if (TOLOWER_ASC(initstr[0]) == 'e'
435 	    && (TOLOWER_ASC(initstr[1]) == 'v'
436 					   || TOLOWER_ASC(initstr[1]) == 'g'))
437     {
438 #ifdef FEAT_GUI
439 	gui.starting = TRUE;
440 #endif
441 	evim_mode = TRUE;
442 	++initstr;
443     }
444 
445     if (TOLOWER_ASC(initstr[0]) == 'g' || initstr[0] == 'k')
446     {
447 	main_start_gui();
448 #ifdef FEAT_GUI
449 	++initstr;
450 #endif
451     }
452 
453     if (STRNICMP(initstr, "view", 4) == 0)
454     {
455 	readonlymode = TRUE;
456 	curbuf->b_p_ro = TRUE;
457 	p_uc = 10000;			/* don't update very often */
458 	initstr += 4;
459     }
460     else if (STRNICMP(initstr, "vim", 3) == 0)
461 	initstr += 3;
462 
463     /* Catch "[r][g]vimdiff" and "[r][g]viewdiff". */
464     if (STRICMP(initstr, "diff") == 0)
465     {
466 #ifdef FEAT_DIFF
467 	diff_mode = TRUE;
468 #else
469 	mch_errmsg(_("This Vim was not compiled with the diff feature."));
470 	mch_errmsg("\n");
471 	mch_exit(2);
472 #endif
473     }
474 
475     if (STRNICMP(initstr, "ex", 2) == 0)
476     {
477 	if (STRNICMP(initstr + 2, "im", 2) == 0)
478 	    exmode_active = EXMODE_VIM;
479 	else
480 	    exmode_active = EXMODE_NORMAL;
481 	change_compatible(TRUE);	/* set 'compatible' */
482     }
483 
484     initstr = gettail((char_u *)argv[0]);
485     ++argv;
486     --argc;
487 
488     /*
489      * Process the command line arguments.
490      */
491     argv_idx = 1;	    /* active option letter is argv[0][argv_idx] */
492     while (argc > 0)
493     {
494 	/*
495 	 * "+" or "+{number}" or "+/{pat}" or "+{command}" argument.
496 	 */
497 	if (argv[0][0] == '+' && !had_minmin)
498 	{
499 	    if (n_commands >= MAX_ARG_CMDS)
500 		mainerr(ME_EXTRA_CMD, NULL);
501 	    argv_idx = -1;	    /* skip to next argument */
502 	    if (argv[0][1] == NUL)
503 		commands[n_commands++] = (char_u *)"$";
504 	    else
505 		commands[n_commands++] = (char_u *)&(argv[0][1]);
506 	}
507 
508 	/*
509 	 * Optional argument.
510 	 */
511 	else if (argv[0][0] == '-' && !had_minmin)
512 	{
513 	    want_argument = FALSE;
514 	    c = argv[0][argv_idx++];
515 #ifdef VMS
516 	    /*
517 	     * VMS only uses upper case command lines.  Interpret "-X" as "-x"
518 	     * and "-/X" as "-X".
519 	     */
520 	    if (c == '/')
521 	    {
522 		c = argv[0][argv_idx++];
523 		c = TOUPPER_ASC(c);
524 	    }
525 	    else
526 		c = TOLOWER_ASC(c);
527 #endif
528 	    switch (c)
529 	    {
530 	    case NUL:		/* "vim -"  read from stdin */
531 				/* "ex -" silent mode */
532 		if (exmode_active)
533 		    silent_mode = TRUE;
534 		else
535 		{
536 		    if (edit_type != EDIT_NONE)
537 			mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
538 		    edit_type = EDIT_STDIN;
539 		    read_cmd_fd = 2;	/* read from stderr instead of stdin */
540 		}
541 		argv_idx = -1;		/* skip to next argument */
542 		break;
543 
544 	    case '-':		/* "--" don't take any more options */
545 				/* "--help" give help message */
546 				/* "--version" give version message */
547 				/* "--literal" take files literally */
548 				/* "--nofork" don't fork */
549 				/* "--noplugin[s]" skip plugins */
550 				/* "--cmd <cmd>" execute cmd before vimrc */
551 		if (STRICMP(argv[0] + argv_idx, "help") == 0)
552 		    usage();
553 		else if (STRICMP(argv[0] + argv_idx, "version") == 0)
554 		{
555 		    Columns = 80;	/* need to init Columns */
556 		    info_message = TRUE; /* use mch_msg(), not mch_errmsg() */
557 		    list_version();
558 		    msg_putchar('\n');
559 		    msg_didout = FALSE;
560 		    mch_exit(0);
561 		}
562 		else if (STRNICMP(argv[0] + argv_idx, "literal", 7) == 0)
563 		{
564 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
565 		    literal = TRUE;
566 #endif
567 		}
568 		else if (STRNICMP(argv[0] + argv_idx, "nofork", 6) == 0)
569 		{
570 #ifdef FEAT_GUI
571 		    gui.dofork = FALSE;	/* don't fork() when starting GUI */
572 #endif
573 		}
574 		else if (STRNICMP(argv[0] + argv_idx, "noplugin", 8) == 0)
575 		    p_lpl = FALSE;
576 #ifdef FEAT_PRECOMMANDS
577 		else if (STRNICMP(argv[0] + argv_idx, "cmd", 3) == 0)
578 		{
579 		    want_argument = TRUE;
580 		    argv_idx += 3;
581 		}
582 #endif
583 #ifdef FEAT_CLIENTSERVER
584 		else if (STRNICMP(argv[0] + argv_idx, "serverlist", 10) == 0)
585 		    ; /* already processed -- no arg */
586 		else if (STRNICMP(argv[0] + argv_idx, "servername", 10) == 0
587 		       || STRNICMP(argv[0] + argv_idx, "serversend", 10) == 0)
588 		{
589 		    /* already processed -- snatch the following arg */
590 		    if (argc > 1)
591 		    {
592 			--argc;
593 			++argv;
594 		    }
595 		}
596 #endif
597 #ifdef FEAT_GUI_GTK
598 		else if (STRNICMP(argv[0] + argv_idx, "socketid", 8) == 0)
599 		{
600 		    /* already processed -- snatch the following arg */
601 		    if (argc > 1)
602 		    {
603 			--argc;
604 			++argv;
605 		    }
606 		}
607 		else if (STRNICMP(argv[0] + argv_idx, "echo-wid", 8) == 0)
608 		{
609 		    /* already processed, skip */
610 		}
611 #endif
612 		else
613 		{
614 		    if (argv[0][argv_idx])
615 			mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
616 		    had_minmin = TRUE;
617 		}
618 		if (!want_argument)
619 		    argv_idx = -1;	/* skip to next argument */
620 		break;
621 
622 	    case 'A':		/* "-A" start in Arabic mode */
623 #ifdef FEAT_ARABIC
624 		set_option_value((char_u *)"arabic", 1L, NULL, 0);
625 #else
626 		mch_errmsg(_(e_noarabic));
627 		mch_exit(2);
628 #endif
629 		break;
630 
631 	    case 'b':		/* "-b" binary mode */
632 		bin_mode = TRUE;    /* postpone to after reading .exrc files */
633 		break;
634 
635 	    case 'C':		/* "-C"  Compatible */
636 		change_compatible(TRUE);
637 		break;
638 
639 	    case 'e':		/* "-e" Ex mode */
640 		exmode_active = EXMODE_NORMAL;
641 		break;
642 
643 	    case 'E':		/* "-E" Improved Ex mode */
644 		exmode_active = EXMODE_VIM;
645 		break;
646 
647 	    case 'f':		/* "-f"  GUI: run in foreground.  Amiga: open
648 				window directly, not with newcli */
649 #ifdef FEAT_GUI
650 		gui.dofork = FALSE;	/* don't fork() when starting GUI */
651 #endif
652 		break;
653 
654 	    case 'g':		/* "-g" start GUI */
655 		main_start_gui();
656 		break;
657 
658 	    case 'F':		/* "-F" start in Farsi mode: rl + fkmap set */
659 #ifdef FEAT_FKMAP
660 		curwin->w_p_rl = p_fkmap = TRUE;
661 #else
662 		mch_errmsg(_(e_nofarsi));
663 		mch_exit(2);
664 #endif
665 		break;
666 
667 	    case 'h':		/* "-h" give help message */
668 #ifdef FEAT_GUI_GNOME
669 		/* Tell usage() to exit for "gvim". */
670 		gui.starting = FALSE;
671 #endif
672 		usage();
673 		break;
674 
675 	    case 'H':		/* "-H" start in Hebrew mode: rl + hkmap set */
676 #ifdef FEAT_RIGHTLEFT
677 		curwin->w_p_rl = p_hkmap = TRUE;
678 #else
679 		mch_errmsg(_(e_nohebrew));
680 		mch_exit(2);
681 #endif
682 		break;
683 
684 	    case 'l':		/* "-l" lisp mode, 'lisp' and 'showmatch' on */
685 #ifdef FEAT_LISP
686 		set_option_value((char_u *)"lisp", 1L, NULL, 0);
687 		p_sm = TRUE;
688 #endif
689 		break;
690 
691 #ifdef TARGET_API_MAC_OSX
692 		/* For some reason on MacOS X, an argument like:
693 		   -psn_0_10223617 is passed in when invoke from Finder
694 		   or with the 'open' command */
695 	    case 'p':
696 		argv_idx = -1; /* bypass full -psn */
697 		main_start_gui();
698 		break;
699 #endif
700 	    case 'M':		/* "-M"  no changes or writing of files */
701 		reset_modifiable();
702 		/* FALLTRHOUGH */
703 
704 	    case 'm':		/* "-m"  no writing of files */
705 		p_write = FALSE;
706 		break;
707 
708 	    case 'y':		/* "-y"  easy mode */
709 #ifdef FEAT_GUI
710 		gui.starting = TRUE;	/* start GUI a bit later */
711 #endif
712 		evim_mode = TRUE;
713 		break;
714 
715 	    case 'N':		/* "-N"  Nocompatible */
716 		change_compatible(FALSE);
717 		break;
718 
719 	    case 'n':		/* "-n" no swap file */
720 		no_swap_file = TRUE;
721 		break;
722 
723 	    case 'o':		/* "-o[N]" open N horizontal split windows */
724 #ifdef FEAT_WINDOWS
725 		/* default is 0: open window for each file */
726 		window_count = get_number_arg((char_u *)argv[0], &argv_idx, 0);
727 		vert_windows = FALSE;
728 #endif
729 		break;
730 
731 		case 'O':	/* "-O[N]" open N vertical split windows */
732 #if defined(FEAT_VERTSPLIT) && defined(FEAT_WINDOWS)
733 		/* default is 0: open window for each file */
734 		window_count = get_number_arg((char_u *)argv[0], &argv_idx, 0);
735 		vert_windows = TRUE;
736 #endif
737 		break;
738 
739 #ifdef FEAT_QUICKFIX
740 	    case 'q':		/* "-q" QuickFix mode */
741 		if (edit_type != EDIT_NONE)
742 		    mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
743 		edit_type = EDIT_QF;
744 		if (argv[0][argv_idx])		/* "-q{errorfile}" */
745 		{
746 		    use_ef = (char_u *)argv[0] + argv_idx;
747 		    argv_idx = -1;
748 		}
749 		else if (argc > 1)		/* "-q {errorfile}" */
750 		    want_argument = TRUE;
751 		break;
752 #endif
753 
754 	    case 'R':		/* "-R" readonly mode */
755 		readonlymode = TRUE;
756 		curbuf->b_p_ro = TRUE;
757 		p_uc = 10000;			/* don't update very often */
758 		break;
759 
760 	    case 'r':		/* "-r" recovery mode */
761 	    case 'L':		/* "-L" recovery mode */
762 		recoverymode = 1;
763 		break;
764 
765 	    case 's':
766 		if (exmode_active)	/* "-s" silent (batch) mode */
767 		    silent_mode = TRUE;
768 		else		/* "-s {scriptin}" read from script file */
769 		    want_argument = TRUE;
770 		break;
771 
772 	    case 't':		/* "-t {tag}" or "-t{tag}" jump to tag */
773 		if (edit_type != EDIT_NONE)
774 		    mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
775 		edit_type = EDIT_TAG;
776 		if (argv[0][argv_idx])		/* "-t{tag}" */
777 		{
778 		    tagname = (char_u *)argv[0] + argv_idx;
779 		    argv_idx = -1;
780 		}
781 		else				/* "-t {tag}" */
782 		    want_argument = TRUE;
783 		break;
784 
785 #ifdef FEAT_EVAL
786 	    case 'D':		/* "-D"		Debugging */
787 		use_debug_break_level = 9999;
788 		break;
789 #endif
790 #ifdef FEAT_DIFF
791 	    case 'd':		/* "-d"		'diff' */
792 # ifdef AMIGA
793 		/* check for "-dev {device}" */
794 		if (argv[0][argv_idx] == 'e' && argv[0][argv_idx + 1] == 'v')
795 		    want_argument = TRUE;
796 		else
797 # endif
798 		    diff_mode = TRUE;
799 		break;
800 #endif
801 	    case 'V':		/* "-V{N}"	Verbose level */
802 		/* default is 10: a little bit verbose */
803 		p_verbose = get_number_arg((char_u *)argv[0], &argv_idx, 10);
804 		break;
805 
806 	    case 'v':		/* "-v"  Vi-mode (as if called "vi") */
807 		exmode_active = 0;
808 #ifdef FEAT_GUI
809 		gui.starting = FALSE;	/* don't start GUI */
810 #endif
811 		break;
812 
813 	    case 'w':		/* "-w{number}"	set window height */
814 				/* "-w {scriptout}"	write to script */
815 		if (vim_isdigit(((char_u *)argv[0])[argv_idx]))
816 		{
817 		    argv_idx = -1;
818 		    break;			/* not implemented, ignored */
819 		}
820 		want_argument = TRUE;
821 		break;
822 
823 #ifdef FEAT_CRYPT
824 	    case 'x':		/* "-x"  encrypted reading/writing of files */
825 		ask_for_key = TRUE;
826 		break;
827 #endif
828 
829 	    case 'X':		/* "-X"  don't connect to X server */
830 #if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
831 		x_no_connect = TRUE;
832 #endif
833 		break;
834 
835 	    case 'Z':		/* "-Z"  restricted mode */
836 		restricted = TRUE;
837 		break;
838 
839 	    case 'c':		/* "-c {command}" execute command */
840 	    case 'S':		/* "-S {file}" execute Vim script */
841 	    case 'i':		/* "-i {viminfo}" use for viminfo */
842 #ifndef FEAT_DIFF
843 	    case 'd':		/* "-d {device}" device (for Amiga) */
844 #endif
845 	    case 'T':		/* "-T {terminal}" terminal name */
846 	    case 'u':		/* "-u {vimrc}" vim inits file */
847 	    case 'U':		/* "-U {gvimrc}" gvim inits file */
848 	    case 'W':		/* "-W {scriptout}" overwrite */
849 #ifdef FEAT_GUI_W32
850 	    case 'P':		/* "-P {parent title}" MDI parent */
851 #endif
852 		want_argument = TRUE;
853 		break;
854 
855 	    default:
856 		mainerr(ME_UNKNOWN_OPTION, (char_u *)argv[0]);
857 	    }
858 
859 	    /*
860 	     * Handle options with argument.
861 	     */
862 	    if (want_argument)
863 	    {
864 		/*
865 		 * Check for garbage immediately after the option letter.
866 		 */
867 		if (argv[0][argv_idx] != NUL)
868 		    mainerr(ME_GARBAGE, (char_u *)argv[0]);
869 
870 		--argc;
871 		if (argc < 1 && c != 'S')
872 		    mainerr_arg_missing((char_u *)argv[0]);
873 		++argv;
874 		argv_idx = -1;
875 
876 		switch (c)
877 		{
878 		case 'c':	/* "-c {command}" execute command */
879 		case 'S':	/* "-S {file}" execute Vim script */
880 		    if (n_commands >= MAX_ARG_CMDS)
881 			mainerr(ME_EXTRA_CMD, NULL);
882 		    if (c == 'S')
883 		    {
884 			char	*a;
885 
886 			if (argc < 1)
887 			    /* "-S" without argument: use default session file
888 			     * name. */
889 			    a = SESSION_FILE;
890 			else if (argv[0][0] == '-')
891 			{
892 			    /* "-S" followed by another option: use default
893 			     * session file name. */
894 			    a = SESSION_FILE;
895 			    ++argc;
896 			    --argv;
897 			}
898 			else
899 			    a = argv[0];
900 			p = alloc((unsigned)(STRLEN(a) + 4));
901 			if (p == NULL)
902 			    mch_exit(2);
903 			sprintf((char *)p, "so %s", a);
904 			commands[n_commands++] = p;
905 		    }
906 		    else
907 			commands[n_commands++] = (char_u *)argv[0];
908 		    break;
909 
910 #ifdef FEAT_PRECOMMANDS
911 		case '-':	/* "--cmd {command}" execute command */
912 		    if (p_commands >= MAX_ARG_CMDS)
913 			mainerr(ME_EXTRA_CMD, NULL);
914 		    pre_commands[p_commands++] = (char_u *)argv[0];
915 		    break;
916 #endif
917 
918 	    /*	case 'd':   -d {device} is handled in mch_check_win() for the
919 	     *		    Amiga */
920 
921 #ifdef FEAT_QUICKFIX
922 		case 'q':	/* "-q {errorfile}" QuickFix mode */
923 		    use_ef = (char_u *)argv[0];
924 		    break;
925 #endif
926 
927 		case 'i':	/* "-i {viminfo}" use for viminfo */
928 		    use_viminfo = (char_u *)argv[0];
929 		    break;
930 
931 		case 's':	/* "-s {scriptin}" read from script file */
932 		    if (scriptin[0] != NULL)
933 		    {
934 scripterror:
935 			mch_errmsg(_("Attempt to open script file again: \""));
936 			mch_errmsg(argv[-1]);
937 			mch_errmsg(" ");
938 			mch_errmsg(argv[0]);
939 			mch_errmsg("\"\n");
940 			mch_exit(2);
941 		    }
942 		    if ((scriptin[0] = mch_fopen(argv[0], READBIN)) == NULL)
943 		    {
944 			mch_errmsg(_("Cannot open for reading: \""));
945 			mch_errmsg(argv[0]);
946 			mch_errmsg("\"\n");
947 			mch_exit(2);
948 		    }
949 		    if (save_typebuf() == FAIL)
950 			mch_exit(2);	/* out of memory */
951 		    break;
952 
953 		case 't':	/* "-t {tag}" */
954 		    tagname = (char_u *)argv[0];
955 		    break;
956 
957 		case 'T':	/* "-T {terminal}" terminal name */
958 		    /*
959 		     * The -T term option is always available and when
960 		     * HAVE_TERMLIB is supported it overrides the environment
961 		     * variable TERM.
962 		     */
963 #ifdef FEAT_GUI
964 		    if (term_is_gui((char_u *)argv[0]))
965 			gui.starting = TRUE;	/* start GUI a bit later */
966 		    else
967 #endif
968 			term = (char_u *)argv[0];
969 		    break;
970 
971 		case 'u':	/* "-u {vimrc}" vim inits file */
972 		    use_vimrc = (char_u *)argv[0];
973 		    break;
974 
975 		case 'U':	/* "-U {gvimrc}" gvim inits file */
976 #ifdef FEAT_GUI
977 		    use_gvimrc = (char_u *)argv[0];
978 #endif
979 		    break;
980 
981 		case 'w':	/* "-w {scriptout}" append to script file */
982 		case 'W':	/* "-W {scriptout}" overwrite script file */
983 		    if (scriptout != NULL)
984 			goto scripterror;
985 		    if ((scriptout = mch_fopen(argv[0],
986 				    c == 'w' ? APPENDBIN : WRITEBIN)) == NULL)
987 		    {
988 			mch_errmsg(_("Cannot open for script output: \""));
989 			mch_errmsg(argv[0]);
990 			mch_errmsg("\"\n");
991 			mch_exit(2);
992 		    }
993 		    break;
994 
995 #ifdef FEAT_GUI_W32
996 		case 'P':		/* "-P {parent title}" MDI parent */
997 		    gui_mch_set_parent(argv[0]);
998 		    break;
999 #endif
1000 		}
1001 	    }
1002 	}
1003 
1004 	/*
1005 	 * File name argument.
1006 	 */
1007 	else
1008 	{
1009 	    argv_idx = -1;	    /* skip to next argument */
1010 
1011 	    /* Check for only one type of editing. */
1012 	    if (edit_type != EDIT_NONE && edit_type != EDIT_FILE)
1013 		mainerr(ME_TOO_MANY_ARGS, (char_u *)argv[0]);
1014 	    edit_type = EDIT_FILE;
1015 
1016 #ifdef MSWIN
1017 	    /* Remember if the argument was a full path before changing
1018 	     * slashes to backslashes. */
1019 	    if (argv[0][0] != NUL && argv[0][1] == ':' && argv[0][2] == '\\')
1020 		full_path = TRUE;
1021 #endif
1022 
1023 	    /* Add the file to the global argument list. */
1024 	    if (ga_grow(&global_alist.al_ga, 1) == FAIL
1025 		    || (p = vim_strsave((char_u *)argv[0])) == NULL)
1026 		mch_exit(2);
1027 #ifdef FEAT_DIFF
1028 	    if (diff_mode && mch_isdir(p) && GARGCOUNT > 0
1029 				      && !mch_isdir(alist_name(&GARGLIST[0])))
1030 	    {
1031 		char_u	    *r;
1032 
1033 		r = concat_fnames(p, gettail(alist_name(&GARGLIST[0])), TRUE);
1034 		if (r != NULL)
1035 		{
1036 		    vim_free(p);
1037 		    p = r;
1038 		}
1039 	    }
1040 #endif
1041 #if defined(__CYGWIN32__) && !defined(WIN32)
1042 	    /*
1043 	     * If vim is invoked by non-Cygwin tools, convert away any
1044 	     * DOS paths, so things like .swp files are created correctly.
1045 	     * Look for evidence of non-Cygwin paths before we bother.
1046 	     * This is only for when using the Unix files.
1047 	     */
1048 	    if (strpbrk(p, "\\:") != NULL)
1049 	    {
1050 		char posix_path[PATH_MAX];
1051 
1052 		cygwin_conv_to_posix_path(p, posix_path);
1053 		vim_free(p);
1054 		p = vim_strsave(posix_path);
1055 		if (p == NULL)
1056 		    mch_exit(2);
1057 	    }
1058 #endif
1059 	    alist_add(&global_alist, p,
1060 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1061 		    literal ? 2 : 0	/* add buffer number after expanding */
1062 #else
1063 		    2		/* add buffer number now and use curbuf */
1064 #endif
1065 		    );
1066 	}
1067 
1068 	/*
1069 	 * If there are no more letters after the current "-", go to next
1070 	 * argument.  argv_idx is set to -1 when the current argument is to be
1071 	 * skipped.
1072 	 */
1073 	if (argv_idx <= 0 || argv[0][argv_idx] == NUL)
1074 	{
1075 	    --argc;
1076 	    ++argv;
1077 	    argv_idx = 1;
1078 	}
1079     }
1080     TIME_MSG("parsing arguments");
1081 
1082     /*
1083      * On some systems, when we compile with the GUI, we always use it.  On Mac
1084      * there is no terminal version, and on Windows we can't figure out how to
1085      * fork one off with :gui.
1086      */
1087 #ifdef ALWAYS_USE_GUI
1088     gui.starting = TRUE;
1089 #else
1090 # if defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE)
1091     /*
1092      * Check if the GUI can be started.  Reset gui.starting if not.
1093      * Don't know about other systems, stay on the safe side and don't check.
1094      */
1095     if (gui.starting && gui_init_check() == FAIL)
1096     {
1097 	gui.starting = FALSE;
1098 
1099 	/* When running "evim" or "gvim -y" we need the menus, exit if we
1100 	 * don't have them. */
1101 	if (evim_mode)
1102 	    mch_exit(1);
1103     }
1104 # endif
1105 #endif
1106 
1107     /* "-b" argument used.  Check before expanding file names, because for
1108      * Win32 this makes us edit a shortcut file itself, instead of the file it
1109      * links to. */
1110     if (bin_mode)
1111     {
1112 	set_options_bin(curbuf->b_p_bin, 1, 0);
1113 	curbuf->b_p_bin = 1;	    /* binary file I/O */
1114     }
1115 
1116     if (GARGCOUNT > 0)
1117     {
1118 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
1119 	/*
1120 	 * Expand wildcards in file names.
1121 	 */
1122 	if (!literal)
1123 	{
1124 	    /* Temporarily add '(' and ')' to 'isfname'.  These are valid
1125 	     * filename characters but are excluded from 'isfname' to make
1126 	     * "gf" work on a file name in parenthesis (e.g.: see vim.h). */
1127 	    do_cmdline_cmd((char_u *)":set isf+=(,)");
1128 	    alist_expand();
1129 	    do_cmdline_cmd((char_u *)":set isf&");
1130 	}
1131 #endif
1132 	fname = alist_name(&GARGLIST[0]);
1133     }
1134     if (GARGCOUNT > 1)
1135 	printf(_("%d files to edit\n"), GARGCOUNT);
1136 #ifdef MSWIN
1137     else if (GARGCOUNT == 1 && full_path)
1138     {
1139 	/*
1140 	 * If there is one filename, fully qualified, we have very probably
1141 	 * been invoked from explorer, so change to the file's directory.
1142 	 * Hint: to avoid this when typing a command use a forward slash.
1143 	 * If the cd fails, it doesn't matter.
1144 	 */
1145 	(void)vim_chdirfile(fname);
1146     }
1147 #endif
1148     TIME_MSG("expanding arguments");
1149 
1150 #ifdef FEAT_DIFF
1151     if (diff_mode)
1152     {
1153 	if (window_count == -1)
1154 	    window_count = 0;		/* open up to 3 files in a window */
1155 	if (vert_windows == MAYBE)
1156 	    vert_windows = TRUE;	/* use vertical split */
1157     }
1158 #endif
1159 
1160     ++RedrawingDisabled;
1161 
1162     /*
1163      * When listing swap file names, don't do cursor positioning et. al.
1164      */
1165     if (recoverymode && fname == NULL)
1166 	want_full_screen = FALSE;
1167 
1168     /*
1169      * When certain to start the GUI, don't check capabilities of terminal.
1170      * For GTK we can't be sure, but when started from the desktop it doesn't
1171      * make sense to try using a terminal.
1172      */
1173 #if defined(ALWAYS_USE_GUI) || defined(FEAT_GUI_X11) || defined(FEAT_GUI_GTK) || defined(FEAT_GUI_KDE)
1174     if (gui.starting
1175 # ifdef FEAT_GUI_GTK
1176 	    && !isatty(2)
1177 # endif
1178 	    )
1179 	want_full_screen = FALSE;
1180 #endif
1181 
1182 #if defined(FEAT_GUI_MAC) && defined(MACOS_X_UNIX)
1183     /* When the GUI is started from Finder, need to display messages in a
1184      * message box.  isatty(2) returns TRUE anyway, thus we need to check the
1185      * name to know we're not started from a terminal. */
1186     if (gui.starting && (!isatty(2) || strcmp("/dev/console", ttyname(2)) == 0))
1187 	want_full_screen = FALSE;
1188 #endif
1189 
1190     /*
1191      * mch_init() sets up the terminal (window) for use.  This must be
1192      * done after resetting full_screen, otherwise it may move the cursor
1193      * (MSDOS).
1194      * Note that we may use mch_exit() before mch_init()!
1195      */
1196     mch_init();
1197     TIME_MSG("shell init");
1198 
1199 #ifdef USE_XSMP
1200     /*
1201      * For want of anywhere else to do it, try to connect to xsmp here.
1202      * Fitting it in after gui_mch_init, but before gui_init (via termcapinit).
1203      * Hijacking -X 'no X connection' to also disable XSMP connection as that
1204      * has a similar delay upon failure.
1205      * Only try if SESSION_MANAGER is set to something non-null.
1206      */
1207     if (!x_no_connect)
1208     {
1209 	p = (char_u *)getenv("SESSION_MANAGER");
1210 	if (p != NULL && *p != NUL)
1211 	{
1212 	    xsmp_init();
1213 	    TIME_MSG("xsmp init");
1214 	}
1215     }
1216 #endif
1217 
1218     /*
1219      * Print a warning if stdout is not a terminal.
1220      * When starting in Ex mode and commands come from a file, set Silent mode.
1221      */
1222     input_isatty = mch_input_isatty();
1223     if (exmode_active)
1224     {
1225 	if (!input_isatty)
1226 	    silent_mode = TRUE;
1227     }
1228     else if (want_full_screen && (!stdout_isatty || !input_isatty)
1229 #ifdef FEAT_GUI
1230 	    /* don't want the delay when started from the desktop */
1231 	    && !gui.starting
1232 #endif
1233 	    )
1234     {
1235 	if (!stdout_isatty)
1236 	    mch_errmsg(_("Vim: Warning: Output is not to a terminal\n"));
1237 	if (!input_isatty)
1238 	    mch_errmsg(_("Vim: Warning: Input is not from a terminal\n"));
1239 	out_flush();
1240 	if (scriptin[0] == NULL)
1241 	    ui_delay(2000L, TRUE);
1242 	TIME_MSG("Warning delay");
1243     }
1244 
1245     if (want_full_screen)
1246     {
1247 	termcapinit(term);	/* set terminal name and get terminal
1248 				   capabilities (will set full_screen) */
1249 	screen_start();		/* don't know where cursor is now */
1250 	TIME_MSG("Termcap init");
1251     }
1252 
1253     /*
1254      * Set the default values for the options that use Rows and Columns.
1255      */
1256     ui_get_shellsize();		/* inits Rows and Columns */
1257 #ifdef FEAT_NETBEANS_INTG
1258     if (usingNetbeans)
1259 	Columns += 2;		/* leave room for glyph gutter */
1260 #endif
1261     firstwin->w_height = Rows - p_ch;
1262     topframe->fr_height = Rows - p_ch;
1263 #ifdef FEAT_VERTSPLIT
1264     firstwin->w_width = Columns;
1265     topframe->fr_width = Columns;
1266 #endif
1267 #ifdef FEAT_DIFF
1268     /* Set the 'diff' option now, so that it can be checked for in a .vimrc
1269      * file.  There is no buffer yet though. */
1270     if (diff_mode)
1271 	diff_win_options(firstwin, FALSE);
1272 #endif
1273 
1274     cmdline_row = Rows - p_ch;
1275     msg_row = cmdline_row;
1276     screenalloc(FALSE);		/* allocate screen buffers */
1277     set_init_2();
1278     TIME_MSG("inits 2");
1279 
1280     msg_scroll = TRUE;
1281     no_wait_return = TRUE;
1282 
1283     init_mappings();		/* set up initial mappings */
1284 
1285     init_highlight(TRUE, FALSE); /* set the default highlight groups */
1286     TIME_MSG("init highlight");
1287 #ifdef CURSOR_SHAPE
1288     parse_shape_opt(SHAPE_CURSOR); /* set cursor shapes from 'guicursor' */
1289 #endif
1290 #ifdef FEAT_MOUSESHAPE
1291     parse_shape_opt(SHAPE_MOUSE);  /* set mouse shapes from 'mouseshape' */
1292 #endif
1293 #ifdef FEAT_PRINTER
1294     parse_list_options(p_popt, printer_opts, OPT_PRINT_NUM_OPTIONS);
1295 #endif
1296 
1297 #ifdef FEAT_EVAL
1298     /* Set the break level after the terminal is initialized. */
1299     debug_break_level = use_debug_break_level;
1300 #endif
1301 
1302 #ifdef FEAT_PRECOMMANDS
1303     if (p_commands > 0)
1304     {
1305 	curwin->w_cursor.lnum = 0; /* just in case.. */
1306 	sourcing_name = (char_u *)_("pre-vimrc command line");
1307 # ifdef FEAT_EVAL
1308 	current_SID = SID_CMDARG;
1309 # endif
1310 	for (i = 0; i < p_commands; ++i)
1311 	    do_cmdline_cmd(pre_commands[i]);
1312 	sourcing_name = NULL;
1313 # ifdef FEAT_EVAL
1314 	current_SID = 0;
1315 # endif
1316     }
1317 #endif
1318 
1319     /*
1320      * For "evim" source evim.vim first of all, so that the user can overrule
1321      * any things he doesn't like.
1322      */
1323     if (evim_mode)
1324     {
1325 	(void)do_source((char_u *)EVIM_FILE, FALSE, FALSE);
1326 	TIME_MSG("source evim file");
1327     }
1328 
1329     /*
1330      * If -u option given, use only the initializations from that file and
1331      * nothing else.
1332      */
1333     if (use_vimrc != NULL)
1334     {
1335 	if (STRCMP(use_vimrc, "NONE") == 0 || STRCMP(use_vimrc, "NORC") == 0)
1336 	{
1337 #ifdef FEAT_GUI
1338 	    if (use_gvimrc == NULL)	    /* don't load gvimrc either */
1339 		use_gvimrc = use_vimrc;
1340 #endif
1341 	    if (use_vimrc[2] == 'N')
1342 		p_lpl = FALSE;		    /* don't load plugins either */
1343 	}
1344 	else
1345 	{
1346 	    if (do_source(use_vimrc, FALSE, FALSE) != OK)
1347 		EMSG2(_("E282: Cannot read from \"%s\""), use_vimrc);
1348 	}
1349     }
1350     else if (!silent_mode)
1351     {
1352 #ifdef AMIGA
1353 	struct Process	*proc = (struct Process *)FindTask(0L);
1354 	APTR		save_winptr = proc->pr_WindowPtr;
1355 
1356 	/* Avoid a requester here for a volume that doesn't exist. */
1357 	proc->pr_WindowPtr = (APTR)-1L;
1358 #endif
1359 
1360 	/*
1361 	 * Get system wide defaults, if the file name is defined.
1362 	 */
1363 #ifdef SYS_VIMRC_FILE
1364 	(void)do_source((char_u *)SYS_VIMRC_FILE, FALSE, FALSE);
1365 #endif
1366 
1367 	/*
1368 	 * Try to read initialization commands from the following places:
1369 	 * - environment variable VIMINIT
1370 	 * - user vimrc file (s:.vimrc for Amiga, ~/.vimrc otherwise)
1371 	 * - second user vimrc file ($VIM/.vimrc for Dos)
1372 	 * - environment variable EXINIT
1373 	 * - user exrc file (s:.exrc for Amiga, ~/.exrc otherwise)
1374 	 * - second user exrc file ($VIM/.exrc for Dos)
1375 	 * The first that exists is used, the rest is ignored.
1376 	 */
1377 	if (process_env((char_u *)"VIMINIT", TRUE) != OK)
1378 	{
1379 	    if (do_source((char_u *)USR_VIMRC_FILE, TRUE, TRUE) == FAIL
1380 #ifdef USR_VIMRC_FILE2
1381 		&& do_source((char_u *)USR_VIMRC_FILE2, TRUE, TRUE) == FAIL
1382 #endif
1383 #ifdef USR_VIMRC_FILE3
1384 		&& do_source((char_u *)USR_VIMRC_FILE3, TRUE, TRUE) == FAIL
1385 #endif
1386 		&& process_env((char_u *)"EXINIT", FALSE) == FAIL
1387 		&& do_source((char_u *)USR_EXRC_FILE, FALSE, FALSE) == FAIL)
1388 	    {
1389 #ifdef USR_EXRC_FILE2
1390 		(void)do_source((char_u *)USR_EXRC_FILE2, FALSE, FALSE);
1391 #endif
1392 	    }
1393 	}
1394 
1395 	/*
1396 	 * Read initialization commands from ".vimrc" or ".exrc" in current
1397 	 * directory.  This is only done if the 'exrc' option is set.
1398 	 * Because of security reasons we disallow shell and write commands
1399 	 * now, except for unix if the file is owned by the user or 'secure'
1400 	 * option has been reset in environmet of global ".exrc" or ".vimrc".
1401 	 * Only do this if VIMRC_FILE is not the same as USR_VIMRC_FILE or
1402 	 * SYS_VIMRC_FILE.
1403 	 */
1404 	if (p_exrc)
1405 	{
1406 #if defined(UNIX) || defined(VMS)
1407 	    /* If ".vimrc" file is not owned by user, set 'secure' mode. */
1408 	    if (!file_owned(VIMRC_FILE))
1409 #endif
1410 		secure = p_secure;
1411 
1412 	    i = FAIL;
1413 	    if (fullpathcmp((char_u *)USR_VIMRC_FILE,
1414 				      (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
1415 #ifdef USR_VIMRC_FILE2
1416 		    && fullpathcmp((char_u *)USR_VIMRC_FILE2,
1417 				      (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
1418 #endif
1419 #ifdef USR_VIMRC_FILE3
1420 		    && fullpathcmp((char_u *)USR_VIMRC_FILE3,
1421 				      (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
1422 #endif
1423 #ifdef SYS_VIMRC_FILE
1424 		    && fullpathcmp((char_u *)SYS_VIMRC_FILE,
1425 				      (char_u *)VIMRC_FILE, FALSE) != FPC_SAME
1426 #endif
1427 				)
1428 		i = do_source((char_u *)VIMRC_FILE, TRUE, TRUE);
1429 
1430 	    if (i == FAIL)
1431 	    {
1432 #if defined(UNIX) || defined(VMS)
1433 		/* if ".exrc" is not owned by user set 'secure' mode */
1434 		if (!file_owned(EXRC_FILE))
1435 		    secure = p_secure;
1436 		else
1437 		    secure = 0;
1438 #endif
1439 		if (	   fullpathcmp((char_u *)USR_EXRC_FILE,
1440 				      (char_u *)EXRC_FILE, FALSE) != FPC_SAME
1441 #ifdef USR_EXRC_FILE2
1442 			&& fullpathcmp((char_u *)USR_EXRC_FILE2,
1443 				      (char_u *)EXRC_FILE, FALSE) != FPC_SAME
1444 #endif
1445 				)
1446 		    (void)do_source((char_u *)EXRC_FILE, FALSE, FALSE);
1447 	    }
1448 	}
1449 	if (secure == 2)
1450 	    need_wait_return = TRUE;
1451 	secure = 0;
1452 #ifdef AMIGA
1453 	proc->pr_WindowPtr = save_winptr;
1454 #endif
1455     }
1456     TIME_MSG("sourcing vimrc file(s)");
1457 
1458 #ifdef FEAT_EVAL
1459     /*
1460      * Read all the plugin files.
1461      * Only when compiled with +eval, since most plugins need it.
1462      */
1463     if (p_lpl)
1464     {
1465 	cmd_runtime((char_u *)"plugin/*.vim", TRUE);
1466 	TIME_MSG("loading plugins");
1467     }
1468 #endif
1469 
1470     /*
1471      * Recovery mode without a file name: List swap files.
1472      * This uses the 'dir' option, therefore it must be after the
1473      * initializations.
1474      */
1475     if (recoverymode && fname == NULL)
1476     {
1477 	recover_names(NULL, TRUE, 0);
1478 	mch_exit(0);
1479     }
1480 
1481     /*
1482      * Set a few option defaults after reading .vimrc files:
1483      * 'title' and 'icon', Unix: 'shellpipe' and 'shellredir'.
1484      */
1485     set_init_3();
1486     TIME_MSG("inits 3");
1487 
1488     /*
1489      * "-n" argument: Disable swap file by setting 'updatecount' to 0.
1490      * Note that this overrides anything from a vimrc file.
1491      */
1492     if (no_swap_file)
1493 	p_uc = 0;
1494 
1495 #ifdef FEAT_FKMAP
1496     if (curwin->w_p_rl && p_altkeymap)
1497     {
1498 	p_hkmap = FALSE;	/* Reset the Hebrew keymap mode */
1499 # ifdef FEAT_ARABIC
1500 	curwin->w_p_arab = FALSE; /* Reset the Arabic keymap mode */
1501 # endif
1502 	p_fkmap = TRUE;		/* Set the Farsi keymap mode */
1503     }
1504 #endif
1505 
1506 #ifdef FEAT_GUI
1507     if (gui.starting)
1508     {
1509 #if defined(UNIX) || defined(VMS)
1510 	/* When something caused a message from a vimrc script, need to output
1511 	 * an extra newline before the shell prompt. */
1512 	if (did_emsg || msg_didout)
1513 	    putchar('\n');
1514 #endif
1515 
1516 	gui_start();		/* will set full_screen to TRUE */
1517 	TIME_MSG("starting GUI");
1518 
1519 	/* When running "evim" or "gvim -y" we need the menus, exit if we
1520 	 * don't have them. */
1521 	if (!gui.in_use && evim_mode)
1522 	    mch_exit(1);
1523     }
1524 #endif
1525 
1526 #ifdef SPAWNO		/* special MSDOS swapping library */
1527     init_SPAWNO("", SWAP_ANY);
1528 #endif
1529 
1530 #ifdef FEAT_VIMINFO
1531     /*
1532      * Read in registers, history etc, but not marks, from the viminfo file
1533      */
1534     if (*p_viminfo != NUL)
1535     {
1536 	read_viminfo(NULL, TRUE, FALSE, FALSE);
1537 	TIME_MSG("reading viminfo");
1538     }
1539 #endif
1540 
1541 #ifdef FEAT_QUICKFIX
1542     /*
1543      * "-q errorfile": Load the error file now.
1544      * If the error file can't be read, exit before doing anything else.
1545      */
1546     if (edit_type == EDIT_QF)
1547     {
1548 	if (use_ef != NULL)
1549 	    set_string_option_direct((char_u *)"ef", -1, use_ef, OPT_FREE);
1550 	if (qf_init(p_ef, p_efm, TRUE) < 0)
1551 	{
1552 	    out_char('\n');
1553 	    mch_exit(3);
1554 	}
1555 	TIME_MSG("reading errorfile");
1556     }
1557 #endif
1558 
1559     /*
1560      * Start putting things on the screen.
1561      * Scroll screen down before drawing over it
1562      * Clear screen now, so file message will not be cleared.
1563      */
1564     starting = NO_BUFFERS;
1565     no_wait_return = FALSE;
1566     if (!exmode_active)
1567 	msg_scroll = FALSE;
1568 
1569 #ifdef FEAT_GUI
1570     /*
1571      * This seems to be required to make callbacks to be called now, instead
1572      * of after things have been put on the screen, which then may be deleted
1573      * when getting a resize callback.
1574      * For the Mac this handles putting files dropped on the Vim icon to
1575      * global_alist.
1576      */
1577     if (gui.in_use)
1578     {
1579 # ifdef FEAT_SUN_WORKSHOP
1580 	if (!usingSunWorkShop)
1581 # endif
1582 	    gui_wait_for_chars(50L);
1583 	TIME_MSG("GUI delay");
1584     }
1585 #endif
1586 
1587 #if defined(FEAT_GUI_PHOTON) && defined(FEAT_CLIPBOARD)
1588     qnx_clip_init();
1589 #endif
1590 
1591 #ifdef FEAT_XCLIPBOARD
1592     /* Start using the X clipboard, unless the GUI was started. */
1593 # ifdef FEAT_GUI
1594     if (!gui.in_use)
1595 # endif
1596     {
1597 	setup_term_clip();
1598 	TIME_MSG("setup clipboard");
1599     }
1600 #endif
1601 
1602 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
1603     /*
1604      * Register for remote command execution with :serversend and --remote
1605      * unless there was a -X or a --servername '' on the command line.
1606      * Only register nongui-vim's with an explicit --servername argument.
1607      */
1608     if (X_DISPLAY != NULL && servername != NULL && (
1609 # ifdef FEAT_GUI
1610 		gui.in_use ||
1611 # endif
1612 		serverName_arg != NULL))
1613     {
1614 	(void)serverRegisterName(X_DISPLAY, servername);
1615 	vim_free(servername);
1616 	TIME_MSG("register server name");
1617     }
1618     else
1619 	serverDelayedStartName = servername;
1620 #endif
1621 
1622 #ifdef FEAT_CLIENTSERVER
1623     /*
1624      * Execute command ourselves if we're here because the send failed (or
1625      * else we would have exited above).
1626      */
1627     if (serverStr != NULL)
1628 	server_to_input_buf(serverStr);
1629 #endif
1630 
1631     /*
1632      * If "-" argument given: Read file from stdin.
1633      * Do this before starting Raw mode, because it may change things that the
1634      * writing end of the pipe doesn't like, e.g., in case stdin and stderr
1635      * are the same terminal: "cat | vim -".
1636      * Using autocommands here may cause trouble...
1637      */
1638     if (edit_type == EDIT_STDIN && !recoverymode)
1639     {
1640 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1641 	/* When getting the ATTENTION prompt here, use a dialog */
1642 	swap_exists_action = SEA_DIALOG;
1643 #endif
1644 	no_wait_return = TRUE;
1645 	i = msg_didany;
1646 	set_buflisted(TRUE);
1647 	(void)open_buffer(TRUE, NULL);	/* create memfile and read file */
1648 	no_wait_return = FALSE;
1649 	msg_didany = i;
1650 	TIME_MSG("reading stdin");
1651 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1652 	check_swap_exists_action();
1653 #endif
1654 #if !(defined(AMIGA) || defined(MACOS))
1655 	/*
1656 	 * Close stdin and dup it from stderr.  Required for GPM to work
1657 	 * properly, and for running external commands.
1658 	 * Is there any other system that cannot do this?
1659 	 */
1660 	close(0);
1661 	dup(2);
1662 #endif
1663     }
1664 
1665 #if defined(UNIX) || defined(VMS)
1666     /* When switching screens and something caused a message from a vimrc
1667      * script, need to output an extra newline on exit. */
1668     if ((did_emsg || msg_didout) && *T_TI != NUL)
1669 	newline_on_exit = TRUE;
1670 #endif
1671 
1672     /*
1673      * When done something that is not allowed or error message call
1674      * wait_return.  This must be done before starttermcap(), because it may
1675      * switch to another screen. It must be done after settmode(TMODE_RAW),
1676      * because we want to react on a single key stroke.
1677      * Call settmode and starttermcap here, so the T_KS and T_TI may be
1678      * defined by termcapinit and redifined in .exrc.
1679      */
1680     settmode(TMODE_RAW);
1681     TIME_MSG("setting raw mode");
1682 
1683     if (need_wait_return || msg_didany)
1684     {
1685 	wait_return(TRUE);
1686 	TIME_MSG("waiting for return");
1687     }
1688 
1689     starttermcap();	    /* start termcap if not done by wait_return() */
1690     TIME_MSG("start termcap");
1691 
1692 #ifdef FEAT_MOUSE
1693     setmouse();				/* may start using the mouse */
1694 #endif
1695     if (scroll_region)
1696 	scroll_region_reset();		/* In case Rows changed */
1697 
1698     scroll_start();
1699 
1700     /*
1701      * Don't clear the screen when starting in Ex mode, unless using the GUI.
1702      */
1703     if (exmode_active
1704 #ifdef FEAT_GUI
1705 			&& !gui.in_use
1706 #endif
1707 					)
1708 	must_redraw = CLEAR;
1709     else
1710     {
1711 	screenclear();			/* clear screen */
1712 	TIME_MSG("clearing screen");
1713     }
1714 
1715 #ifdef FEAT_CRYPT
1716     if (ask_for_key)
1717     {
1718 	(void)get_crypt_key(TRUE, TRUE);
1719 	TIME_MSG("getting crypt key");
1720     }
1721 #endif
1722 
1723     no_wait_return = TRUE;
1724 
1725 #ifdef FEAT_WINDOWS
1726     /*
1727      * Create the number of windows that was requested.
1728      */
1729     if (window_count == -1)	/* was not set */
1730 	window_count = 1;
1731     if (window_count == 0)
1732 	window_count = GARGCOUNT;
1733     if (window_count > 1)
1734     {
1735 	/* Don't change the windows if there was a command in .vimrc that
1736 	 * already split some windows */
1737 	if (vert_windows == MAYBE)
1738 	    vert_windows = FALSE;
1739 	if (firstwin->w_next == NULL)
1740 	{
1741 	    window_count = make_windows(window_count, vert_windows);
1742 	    TIME_MSG("making windows");
1743 	}
1744 	else
1745 	    window_count = win_count();
1746     }
1747     else
1748 	window_count = 1;
1749 #endif
1750 
1751     if (recoverymode)			/* do recover */
1752     {
1753 	msg_scroll = TRUE;		/* scroll message up */
1754 	ml_recover();
1755 	if (curbuf->b_ml.ml_mfp == NULL) /* failed */
1756 	    getout(1);
1757 	do_modelines();			/* do modelines */
1758     }
1759     else
1760     {
1761 	/*
1762 	 * Open a buffer for windows that don't have one yet.
1763 	 * Commands in the .vimrc might have loaded a file or split the window.
1764 	 * Watch out for autocommands that delete a window.
1765 	 */
1766 #ifdef FEAT_AUTOCMD
1767 	/*
1768 	 * Don't execute Win/Buf Enter/Leave autocommands here
1769 	 */
1770 	++autocmd_no_enter;
1771 	++autocmd_no_leave;
1772 #endif
1773 #ifdef FEAT_WINDOWS
1774 	for (curwin = firstwin; curwin != NULL; curwin = W_NEXT(curwin))
1775 #endif
1776 	{
1777 	    curbuf = curwin->w_buffer;
1778 	    if (curbuf->b_ml.ml_mfp == NULL)
1779 	    {
1780 #ifdef FEAT_FOLDING
1781 		/* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
1782 		if (p_fdls >= 0)
1783 		    curwin->w_p_fdl = p_fdls;
1784 #endif
1785 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1786 		/* When getting the ATTENTION prompt here, use a dialog */
1787 		swap_exists_action = SEA_DIALOG;
1788 #endif
1789 		set_buflisted(TRUE);
1790 		(void)open_buffer(FALSE, NULL); /* create memfile, read file */
1791 
1792 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1793 		check_swap_exists_action();
1794 #endif
1795 #ifdef FEAT_AUTOCMD
1796 		curwin = firstwin;	    /* start again */
1797 #endif
1798 	    }
1799 #ifdef FEAT_WINDOWS
1800 	    ui_breakcheck();
1801 	    if (got_int)
1802 	    {
1803 		(void)vgetc();	/* only break the file loading, not the rest */
1804 		break;
1805 	    }
1806 #endif
1807 	}
1808 #ifdef FEAT_AUTOCMD
1809 	--autocmd_no_enter;
1810 	--autocmd_no_leave;
1811 #endif
1812 #ifdef FEAT_WINDOWS
1813 	curwin = firstwin;
1814 	curbuf = curwin->w_buffer;
1815 #endif
1816     }
1817     TIME_MSG("opening buffers");
1818 
1819     /* Ex starts at last line of the file */
1820     if (exmode_active)
1821 	curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1822 
1823 #ifdef FEAT_AUTOCMD
1824     apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1825     TIME_MSG("BufEnter autocommands");
1826 #endif
1827     setpcmark();
1828 
1829 #ifdef FEAT_QUICKFIX
1830     /*
1831      * When started with "-q errorfile" jump to first error now.
1832      */
1833     if (edit_type == EDIT_QF)
1834     {
1835 	qf_jump(0, 0, FALSE);
1836 	TIME_MSG("jump to first error");
1837     }
1838 #endif
1839 
1840 #ifdef FEAT_WINDOWS
1841     /*
1842      * If opened more than one window, start editing files in the other
1843      * windows.  Make_windows() has already opened the windows.
1844      */
1845 # ifdef FEAT_AUTOCMD
1846     /*
1847      * Don't execute Win/Buf Enter/Leave autocommands here
1848      */
1849     ++autocmd_no_enter;
1850     ++autocmd_no_leave;
1851 # endif
1852     arg_idx = 1;
1853     for (i = 1; i < window_count; ++i)
1854     {
1855 	if (curwin->w_next == NULL)	    /* just checking */
1856 	    break;
1857 	win_enter(curwin->w_next, FALSE);
1858 
1859 	/* Only open the file if there is no file in this window yet (that can
1860 	 * happen when .vimrc contains ":sall") */
1861 	if (curbuf == firstwin->w_buffer || curbuf->b_ffname == NULL)
1862 	{
1863 	    curwin->w_arg_idx = arg_idx;
1864 	    /* edit file from arg list, if there is one */
1865 	    (void)do_ecmd(0, arg_idx < GARGCOUNT
1866 			  ? alist_name(&GARGLIST[arg_idx]) : NULL,
1867 			  NULL, NULL, ECMD_LASTL, ECMD_HIDE);
1868 	    if (arg_idx == GARGCOUNT - 1)
1869 		arg_had_last = TRUE;
1870 	    ++arg_idx;
1871 	}
1872 	ui_breakcheck();
1873 	if (got_int)
1874 	{
1875 	    (void)vgetc();	/* only break the file loading, not the rest */
1876 	    break;
1877 	}
1878     }
1879 # ifdef FEAT_AUTOCMD
1880     --autocmd_no_enter;
1881 # endif
1882     win_enter(firstwin, FALSE);		/* back to first window */
1883 # ifdef FEAT_AUTOCMD
1884     --autocmd_no_leave;
1885 # endif
1886     TIME_MSG("editing files in windows");
1887     if (window_count > 1)
1888 	win_equal(curwin, FALSE, 'b');	/* adjust heights */
1889 #endif /* FEAT_WINDOWS */
1890 
1891 #ifdef FEAT_DIFF
1892     if (diff_mode)
1893     {
1894 	win_T	*wp;
1895 
1896 	/* set options in each window for "vimdiff". */
1897 	for (wp = firstwin; wp != NULL; wp = wp->w_next)
1898 	    diff_win_options(wp, TRUE);
1899     }
1900 #endif
1901 
1902     /*
1903      * Shorten any of the filenames, but only when absolute.
1904      */
1905     shorten_fnames(FALSE);
1906 
1907     /*
1908      * Need to jump to the tag before executing the '-c command'.
1909      * Makes "vim -c '/return' -t main" work.
1910      */
1911     if (tagname != NULL)
1912     {
1913 	STRCPY(IObuff, "ta ");
1914 
1915 	STRNCAT(IObuff, tagname, IOSIZE - 4);
1916 	IObuff[IOSIZE - 1] = NUL;
1917 	do_cmdline_cmd(IObuff);
1918 	TIME_MSG("jumping to tag");
1919     }
1920 
1921     if (n_commands > 0)
1922     {
1923 	/*
1924 	 * We start commands on line 0, make "vim +/pat file" match a
1925 	 * pattern on line 1.
1926 	 */
1927 	msg_scroll = TRUE;
1928 	if (tagname == NULL)
1929 	    curwin->w_cursor.lnum = 0;
1930 	sourcing_name = (char_u *)"command line";
1931 #ifdef FEAT_EVAL
1932 	current_SID = SID_CARG;
1933 #endif
1934 	for (i = 0; i < n_commands; ++i)
1935 	    do_cmdline_cmd(commands[i]);
1936 	sourcing_name = NULL;
1937 #ifdef FEAT_EVAL
1938 	current_SID = 0;
1939 #endif
1940 	if (curwin->w_cursor.lnum == 0)
1941 	    curwin->w_cursor.lnum = 1;
1942 
1943 	if (!exmode_active)
1944 	    msg_scroll = FALSE;
1945 
1946 #ifdef FEAT_QUICKFIX
1947 	/* When started with "-q errorfile" jump to first error again. */
1948 	if (edit_type == EDIT_QF)
1949 	    qf_jump(0, 0, FALSE);
1950 #endif
1951 	TIME_MSG("executing command arguments");
1952     }
1953 
1954     RedrawingDisabled = 0;
1955     redraw_all_later(NOT_VALID);
1956     no_wait_return = FALSE;
1957     starting = 0;
1958 
1959     /* start in insert mode */
1960     if (p_im)
1961 	need_start_insertmode = TRUE;
1962 
1963 #ifdef FEAT_AUTOCMD
1964     apply_autocmds(EVENT_VIMENTER, NULL, NULL, FALSE, curbuf);
1965     TIME_MSG("VimEnter autocommands");
1966 #endif
1967 
1968 #if defined(FEAT_DIFF) && defined(FEAT_SCROLLBIND)
1969     /* When a startup script or session file setup for diff'ing and
1970      * scrollbind, sync the scrollbind now. */
1971     if (curwin->w_p_diff && curwin->w_p_scb)
1972     {
1973 	update_topline();
1974 	check_scrollbind((linenr_T)0, 0L);
1975 	TIME_MSG("diff scrollbinding");
1976     }
1977 #endif
1978 
1979 #if defined(WIN3264) && !defined(FEAT_GUI_W32)
1980     mch_set_winsize_now();	    /* Allow winsize changes from now on */
1981 #endif
1982 
1983     /* If ":startinsert" command used, stuff a dummy command to be able to
1984      * call normal_cmd(), which will then start Insert mode. */
1985     if (restart_edit != 0)
1986 	stuffcharReadbuff(K_IGNORE);
1987 
1988 #ifdef FEAT_NETBEANS_INTG
1989     if (usingNetbeans)
1990 	/* Tell the client that it can start sending commands. */
1991 	netbeans_startup_done();
1992 #endif
1993 
1994     TIME_MSG("before starting main loop");
1995 
1996     /*
1997      * Call the main command loop.  This never returns.
1998      */
1999     main_loop(FALSE);
2000 
2001     return 0;
2002 }
2003 #endif /* PROTO */
2004 
2005 /*
2006  * Main loop: Execute Normal mode commands until exiting Vim.
2007  * Also used to handle commands in the command-line window, until the window
2008  * is closed.
2009  */
2010     void
2011 main_loop(cmdwin)
2012     int		cmdwin;	/* TRUE when working in the command-line window */
2013 {
2014     oparg_T	oa;	/* operator arguments */
2015 
2016 #if defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)
2017     /* Setup to catch a terminating error from the X server.  Just ignore
2018      * it, restore the state and continue.  This might not always work
2019      * properly, but at least we don't exit unexpectedly when the X server
2020      * exists while Vim is running in a console. */
2021     if (!cmdwin && SETJMP(x_jump_env))
2022     {
2023 	State = NORMAL;
2024 # ifdef FEAT_VISUAL
2025 	VIsual_active = FALSE;
2026 # endif
2027 	got_int = TRUE;
2028 	need_wait_return = FALSE;
2029 	global_busy = FALSE;
2030 	exmode_active = 0;
2031 	skip_redraw = FALSE;
2032 	RedrawingDisabled = 0;
2033 	no_wait_return = 0;
2034 # ifdef FEAT_EVAL
2035 	emsg_skip = 0;
2036 # endif
2037 	emsg_off = 0;
2038 # ifdef FEAT_MOUSE
2039 	setmouse();
2040 # endif
2041 	settmode(TMODE_RAW);
2042 	starttermcap();
2043 	scroll_start();
2044 	redraw_later_clear();
2045     }
2046 #endif
2047 
2048     clear_oparg(&oa);
2049     while (!cmdwin
2050 #ifdef FEAT_CMDWIN
2051 	    || cmdwin_result == 0
2052 #endif
2053 	    )
2054     {
2055 	if (stuff_empty())
2056 	{
2057 	    did_check_timestamps = FALSE;
2058 	    if (need_check_timestamps)
2059 		check_timestamps(FALSE);
2060 	    if (need_wait_return)	/* if wait_return still needed ... */
2061 		wait_return(FALSE);	/* ... call it now */
2062 	    if (need_start_insertmode && goto_im()
2063 #ifdef FEAT_VISUAL
2064 		    && !VIsual_active
2065 #endif
2066 		    )
2067 	    {
2068 		need_start_insertmode = FALSE;
2069 		stuffReadbuff((char_u *)"i");	/* start insert mode next */
2070 		/* skip the fileinfo message now, because it would be shown
2071 		 * after insert mode finishes! */
2072 		need_fileinfo = FALSE;
2073 	    }
2074 	}
2075 	if (got_int && !global_busy)
2076 	{
2077 	    if (!quit_more)
2078 		(void)vgetc();		/* flush all buffers */
2079 	    got_int = FALSE;
2080 	}
2081 	if (!exmode_active)
2082 	    msg_scroll = FALSE;
2083 	quit_more = FALSE;
2084 
2085 	/*
2086 	 * If skip redraw is set (for ":" in wait_return()), don't redraw now.
2087 	 * If there is nothing in the stuff_buffer or do_redraw is TRUE,
2088 	 * update cursor and redraw.
2089 	 */
2090 	if (skip_redraw || exmode_active)
2091 	    skip_redraw = FALSE;
2092 	else if (do_redraw || stuff_empty())
2093 	{
2094 #if defined(FEAT_FOLDING) && defined(FEAT_VISUAL)
2095 	    /* Include a closed fold completely in the Visual area. */
2096 	    foldAdjustVisual();
2097 #endif
2098 #ifdef FEAT_FOLDING
2099 	    /*
2100 	     * When 'foldclose' is set, apply 'foldlevel' to folds that don't
2101 	     * contain the cursor.
2102 	     * When 'foldopen' is "all", open the fold(s) under the cursor.
2103 	     * This may mark the window for redrawing.
2104 	     */
2105 	    if (hasAnyFolding(curwin) && !char_avail())
2106 	    {
2107 		foldCheckClose();
2108 		if (fdo_flags & FDO_ALL)
2109 		    foldOpenCursor();
2110 	    }
2111 #endif
2112 
2113 	    /*
2114 	     * Before redrawing, make sure w_topline is correct, and w_leftcol
2115 	     * if lines don't wrap, and w_skipcol if lines wrap.
2116 	     */
2117 	    update_topline();
2118 	    validate_cursor();
2119 
2120 #ifdef FEAT_VISUAL
2121 	    if (VIsual_active)
2122 		update_curbuf(INVERTED);/* update inverted part */
2123 	    else
2124 #endif
2125 		if (must_redraw)
2126 		update_screen(0);
2127 	    else if (redraw_cmdline || clear_cmdline)
2128 		showmode();
2129 #ifdef FEAT_WINDOWS
2130 	    redraw_statuslines();
2131 #endif
2132 #ifdef FEAT_TITLE
2133 	    if (need_maketitle)
2134 		maketitle();
2135 #endif
2136 	    /* display message after redraw */
2137 	    if (keep_msg != NULL)
2138 	    {
2139 		char_u *p;
2140 
2141 		/* msg_attr_keep() will set keep_msg to NULL, must free the
2142 		 * string here. */
2143 		p = keep_msg;
2144 		msg_attr(p, keep_msg_attr);
2145 		vim_free(p);
2146 	    }
2147 	    if (need_fileinfo)		/* show file info after redraw */
2148 	    {
2149 		fileinfo(FALSE, TRUE, FALSE);
2150 		need_fileinfo = FALSE;
2151 	    }
2152 
2153 	    emsg_on_display = FALSE;	/* can delete error message now */
2154 	    did_emsg = FALSE;
2155 	    msg_didany = FALSE;		/* reset lines_left in msg_start() */
2156 	    showruler(FALSE);
2157 
2158 	    setcursor();
2159 	    cursor_on();
2160 
2161 	    do_redraw = FALSE;
2162 	}
2163 #ifdef FEAT_GUI
2164 	if (need_mouse_correct)
2165 	    gui_mouse_correct();
2166 #endif
2167 
2168 	/*
2169 	 * Update w_curswant if w_set_curswant has been set.
2170 	 * Postponed until here to avoid computing w_virtcol too often.
2171 	 */
2172 	update_curswant();
2173 
2174 	/*
2175 	 * If we're invoked as ex, do a round of ex commands.
2176 	 * Otherwise, get and execute a normal mode command.
2177 	 */
2178 	if (exmode_active)
2179 	    do_exmode(exmode_active == EXMODE_VIM);
2180 	else
2181 	    normal_cmd(&oa, TRUE);
2182     }
2183 }
2184 
2185 
2186 #if defined(USE_XSMP) || defined(FEAT_GUI_MSWIN) || defined(PROTO)
2187 /*
2188  * Exit, but leave behind swap files for modified buffers.
2189  */
2190     void
2191 getout_preserve_modified(exitval)
2192     int		exitval;
2193 {
2194     ml_close_notmod();		    /* close all not-modified buffers */
2195     ml_sync_all(FALSE, FALSE);	    /* preserve all swap files */
2196     ml_close_all(FALSE);	    /* close all memfiles, without deleting */
2197     getout(exitval);		    /* exit Vim properly */
2198 }
2199 #endif
2200 
2201 
2202 /* Exit properly */
2203     void
2204 getout(exitval)
2205     int		exitval;
2206 {
2207 #ifdef FEAT_AUTOCMD
2208     buf_T	*buf;
2209     win_T	*wp;
2210 #endif
2211 
2212     exiting = TRUE;
2213 
2214     /* Position the cursor on the last screen line, below all the text */
2215 #ifdef FEAT_GUI
2216     if (!gui.in_use)
2217 #endif
2218 	windgoto((int)Rows - 1, 0);
2219 
2220 #ifdef FEAT_GUI
2221     msg_didany = FALSE;
2222 #endif
2223 
2224 #ifdef FEAT_AUTOCMD
2225     /* Trigger BufWinLeave for all windows, but only once per buffer. */
2226     for (wp = firstwin; wp != NULL; )
2227     {
2228 	buf = wp->w_buffer;
2229 	if (buf->b_changedtick != -1)
2230 	{
2231 	    apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
2232 								  FALSE, buf);
2233 	    buf->b_changedtick = -1;	/* note that we did it already */
2234 	    wp = firstwin;		/* restart, window may be closed */
2235 	}
2236 	else
2237 	    wp = wp->w_next;
2238     }
2239     /* Trigger BufUnload for buffers that are loaded */
2240     for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2241 	if (buf->b_ml.ml_mfp != NULL)
2242 	{
2243 	    apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
2244 								  FALSE, buf);
2245 	    if (!buf_valid(buf))	/* autocmd may delete the buffer */
2246 		break;
2247 	}
2248     apply_autocmds(EVENT_VIMLEAVEPRE, NULL, NULL, FALSE, curbuf);
2249 #endif
2250 
2251 #ifdef FEAT_VIMINFO
2252     if (*p_viminfo != NUL)
2253 	/* Write out the registers, history, marks etc, to the viminfo file */
2254 	write_viminfo(NULL, FALSE);
2255 #endif
2256 
2257 #ifdef FEAT_AUTOCMD
2258     apply_autocmds(EVENT_VIMLEAVE, NULL, NULL, FALSE, curbuf);
2259 #endif
2260 
2261     if (did_emsg
2262 #ifdef FEAT_GUI
2263 	    || (gui.in_use && msg_didany && p_verbose > 0)
2264 #endif
2265 	    )
2266     {
2267 	/* give the user a chance to read the (error) message */
2268 	no_wait_return = FALSE;
2269 	wait_return(FALSE);
2270     }
2271 
2272 #ifdef FEAT_AUTOCMD
2273     /* Position the cursor again, the autocommands may have moved it */
2274 # ifdef FEAT_GUI
2275     if (!gui.in_use)
2276 # endif
2277 	windgoto((int)Rows - 1, 0);
2278 #endif
2279 
2280 #ifdef FEAT_MZSCHEME
2281     mzscheme_end();
2282 #endif
2283 #ifdef FEAT_TCL
2284     tcl_end();
2285 #endif
2286 #ifdef FEAT_RUBY
2287     ruby_end();
2288 #endif
2289 #ifdef FEAT_PYTHON
2290     python_end();
2291 #endif
2292 #ifdef FEAT_PERL
2293     perl_end();
2294 #endif
2295 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
2296     iconv_end();
2297 #endif
2298 #ifdef FEAT_NETBEANS_INTG
2299     netbeans_end();
2300 #endif
2301 
2302     mch_exit(exitval);
2303 }
2304 
2305 /*
2306  * Get a (optional) count for a Vim argument.
2307  */
2308     static int
2309 get_number_arg(p, idx, def)
2310     char_u	*p;	    /* pointer to argument */
2311     int		*idx;	    /* index in argument, is incremented */
2312     int		def;	    /* default value */
2313 {
2314     if (vim_isdigit(p[*idx]))
2315     {
2316 	def = atoi((char *)&(p[*idx]));
2317 	while (vim_isdigit(p[*idx]))
2318 	    *idx = *idx + 1;
2319     }
2320     return def;
2321 }
2322 
2323 /*
2324  * Setup to start using the GUI.  Exit with an error when not available.
2325  */
2326     static void
2327 main_start_gui()
2328 {
2329 #ifdef FEAT_GUI
2330     gui.starting = TRUE;	/* start GUI a bit later */
2331 #else
2332     mch_errmsg(_(e_nogvim));
2333     mch_errmsg("\n");
2334     mch_exit(2);
2335 #endif
2336 }
2337 
2338 /*
2339  * Get an evironment variable, and execute it as Ex commands.
2340  * Returns FAIL if the environment variable was not executed, OK otherwise.
2341  */
2342     int
2343 process_env(env, is_viminit)
2344     char_u	*env;
2345     int		is_viminit; /* when TRUE, called for VIMINIT */
2346 {
2347     char_u	*initstr;
2348     char_u	*save_sourcing_name;
2349     linenr_T	save_sourcing_lnum;
2350 #ifdef FEAT_EVAL
2351     scid_T	save_sid;
2352 #endif
2353 
2354     if ((initstr = mch_getenv(env)) != NULL && *initstr != NUL)
2355     {
2356 	if (is_viminit)
2357 	    vimrc_found();
2358 	save_sourcing_name = sourcing_name;
2359 	save_sourcing_lnum = sourcing_lnum;
2360 	sourcing_name = env;
2361 	sourcing_lnum = 0;
2362 #ifdef FEAT_EVAL
2363 	save_sid = current_SID;
2364 	current_SID = SID_ENV;
2365 #endif
2366 	do_cmdline_cmd(initstr);
2367 	sourcing_name = save_sourcing_name;
2368 	sourcing_lnum = save_sourcing_lnum;
2369 #ifdef FEAT_EVAL
2370 	current_SID = save_sid;;
2371 #endif
2372 	return OK;
2373     }
2374     return FAIL;
2375 }
2376 
2377 #if defined(UNIX) || defined(VMS)
2378 /*
2379  * Return TRUE if we are certain the user owns the file "fname".
2380  * Used for ".vimrc" and ".exrc".
2381  * Use both stat() and lstat() for extra security.
2382  */
2383     static int
2384 file_owned(fname)
2385     char	*fname;
2386 {
2387     struct stat s;
2388 # ifdef UNIX
2389     uid_t	uid = getuid();
2390 # else	 /* VMS */
2391     uid_t	uid = ((getgid() << 16) | getuid());
2392 # endif
2393 
2394     return !(mch_stat(fname, &s) != 0 || s.st_uid != uid
2395 # ifdef HAVE_LSTAT
2396 	    || mch_lstat(fname, &s) != 0 || s.st_uid != uid
2397 # endif
2398 	    );
2399 }
2400 #endif
2401 
2402 /*
2403  * Give an error message main_errors["n"] and exit.
2404  */
2405     static void
2406 mainerr(n, str)
2407     int		n;	/* one of the ME_ defines */
2408     char_u	*str;	/* extra argument or NULL */
2409 {
2410 #if defined(UNIX) || defined(__EMX__) || defined(VMS)
2411     reset_signals();		/* kill us with CTRL-C here, if you like */
2412 #endif
2413 
2414     mch_errmsg(longVersion);
2415     mch_errmsg("\n");
2416     mch_errmsg(_(main_errors[n]));
2417     if (str != NULL)
2418     {
2419 	mch_errmsg(": \"");
2420 	mch_errmsg((char *)str);
2421 	mch_errmsg("\"");
2422     }
2423     mch_errmsg(_("\nMore info with: \"vim -h\"\n"));
2424 
2425     mch_exit(1);
2426 }
2427 
2428     void
2429 mainerr_arg_missing(str)
2430     char_u	*str;
2431 {
2432     mainerr(ME_ARG_MISSING, str);
2433 }
2434 
2435 /*
2436  * print a message with three spaces prepended and '\n' appended.
2437  */
2438     static void
2439 main_msg(s)
2440     char *s;
2441 {
2442     mch_msg("   ");
2443     mch_msg(s);
2444     mch_msg("\n");
2445 }
2446 
2447 /*
2448  * Print messages for "vim -h" or "vim --help" and exit.
2449  */
2450     static void
2451 usage()
2452 {
2453     int		i;
2454     static char	*(use[]) =
2455     {
2456 	N_("[file ..]       edit specified file(s)"),
2457 	N_("-               read text from stdin"),
2458 	N_("-t tag          edit file where tag is defined"),
2459 #ifdef FEAT_QUICKFIX
2460 	N_("-q [errorfile]  edit file with first error")
2461 #endif
2462     };
2463 
2464 #if defined(UNIX) || defined(__EMX__) || defined(VMS)
2465     reset_signals();		/* kill us with CTRL-C here, if you like */
2466 #endif
2467 
2468     mch_msg(longVersion);
2469     mch_msg(_("\n\nusage:"));
2470     for (i = 0; ; ++i)
2471     {
2472 	mch_msg(_(" vim [arguments] "));
2473 	mch_msg(_(use[i]));
2474 	if (i == (sizeof(use) / sizeof(char_u *)) - 1)
2475 	    break;
2476 	mch_msg(_("\n   or:"));
2477     }
2478 
2479     mch_msg(_("\n\nArguments:\n"));
2480     main_msg(_("--\t\t\tOnly file names after this"));
2481 #if (!defined(UNIX) && !defined(__EMX__)) || defined(ARCHIE)
2482     main_msg(_("--literal\t\tDon't expand wildcards"));
2483 #endif
2484 #ifdef FEAT_OLE
2485     main_msg(_("-register\t\tRegister this gvim for OLE"));
2486     main_msg(_("-unregister\t\tUnregister gvim for OLE"));
2487 #endif
2488 #ifdef FEAT_GUI
2489     main_msg(_("-g\t\t\tRun using GUI (like \"gvim\")"));
2490     main_msg(_("-f  or  --nofork\tForeground: Don't fork when starting GUI"));
2491 #endif
2492     main_msg(_("-v\t\t\tVi mode (like \"vi\")"));
2493     main_msg(_("-e\t\t\tEx mode (like \"ex\")"));
2494     main_msg(_("-s\t\t\tSilent (batch) mode (only for \"ex\")"));
2495 #ifdef FEAT_DIFF
2496     main_msg(_("-d\t\t\tDiff mode (like \"vimdiff\")"));
2497 #endif
2498     main_msg(_("-y\t\t\tEasy mode (like \"evim\", modeless)"));
2499     main_msg(_("-R\t\t\tReadonly mode (like \"view\")"));
2500     main_msg(_("-Z\t\t\tRestricted mode (like \"rvim\")"));
2501     main_msg(_("-m\t\t\tModifications (writing files) not allowed"));
2502     main_msg(_("-M\t\t\tModifications in text not allowed"));
2503     main_msg(_("-b\t\t\tBinary mode"));
2504 #ifdef FEAT_LISP
2505     main_msg(_("-l\t\t\tLisp mode"));
2506 #endif
2507     main_msg(_("-C\t\t\tCompatible with Vi: 'compatible'"));
2508     main_msg(_("-N\t\t\tNot fully Vi compatible: 'nocompatible'"));
2509     main_msg(_("-V[N]\t\tVerbose level"));
2510     main_msg(_("-D\t\t\tDebugging mode"));
2511     main_msg(_("-n\t\t\tNo swap file, use memory only"));
2512     main_msg(_("-r\t\t\tList swap files and exit"));
2513     main_msg(_("-r (with file name)\tRecover crashed session"));
2514     main_msg(_("-L\t\t\tSame as -r"));
2515 #ifdef AMIGA
2516     main_msg(_("-f\t\t\tDon't use newcli to open window"));
2517     main_msg(_("-dev <device>\t\tUse <device> for I/O"));
2518 #endif
2519 #ifdef FEAT_ARABIC
2520     main_msg(_("-A\t\t\tstart in Arabic mode"));
2521 #endif
2522 #ifdef FEAT_RIGHTLEFT
2523     main_msg(_("-H\t\t\tStart in Hebrew mode"));
2524 #endif
2525 #ifdef FEAT_FKMAP
2526     main_msg(_("-F\t\t\tStart in Farsi mode"));
2527 #endif
2528     main_msg(_("-T <terminal>\tSet terminal type to <terminal>"));
2529     main_msg(_("-u <vimrc>\t\tUse <vimrc> instead of any .vimrc"));
2530 #ifdef FEAT_GUI
2531     main_msg(_("-U <gvimrc>\t\tUse <gvimrc> instead of any .gvimrc"));
2532 #endif
2533     main_msg(_("--noplugin\t\tDon't load plugin scripts"));
2534     main_msg(_("-o[N]\t\tOpen N windows (default: one for each file)"));
2535     main_msg(_("-O[N]\t\tLike -o but split vertically"));
2536     main_msg(_("+\t\t\tStart at end of file"));
2537     main_msg(_("+<lnum>\t\tStart at line <lnum>"));
2538 #ifdef FEAT_PRECOMMANDS
2539     main_msg(_("--cmd <command>\tExecute <command> before loading any vimrc file"));
2540 #endif
2541     main_msg(_("-c <command>\t\tExecute <command> after loading the first file"));
2542     main_msg(_("-S <session>\t\tSource file <session> after loading the first file"));
2543     main_msg(_("-s <scriptin>\tRead Normal mode commands from file <scriptin>"));
2544     main_msg(_("-w <scriptout>\tAppend all typed commands to file <scriptout>"));
2545     main_msg(_("-W <scriptout>\tWrite all typed commands to file <scriptout>"));
2546 #ifdef FEAT_CRYPT
2547     main_msg(_("-x\t\t\tEdit encrypted files"));
2548 #endif
2549 #if (defined(UNIX) || defined(VMS)) && defined(FEAT_X11)
2550 # if defined(FEAT_GUI_X11) && !defined(FEAT_GUI_GTK)
2551     main_msg(_("-display <display>\tConnect vim to this particular X-server"));
2552 # endif
2553     main_msg(_("-X\t\t\tDo not connect to X server"));
2554 #endif
2555 #ifdef FEAT_CLIENTSERVER
2556     main_msg(_("--remote <files>\tEdit <files> in a Vim server if possible"));
2557     main_msg(_("--remote-silent <files>  Same, don't complain if there is no server"));
2558     main_msg(_("--remote-wait <files>  As --remote but wait for files to have been edited"));
2559     main_msg(_("--remote-wait-silent <files>  Same, don't complain if there is no server"));
2560     main_msg(_("--remote-send <keys>\tSend <keys> to a Vim server and exit"));
2561     main_msg(_("--remote-expr <expr>\tEvaluate <expr> in a Vim server and print result"));
2562     main_msg(_("--serverlist\t\tList available Vim server names and exit"));
2563     main_msg(_("--servername <name>\tSend to/become the Vim server <name>"));
2564 #endif
2565 #ifdef FEAT_VIMINFO
2566     main_msg(_("-i <viminfo>\t\tUse <viminfo> instead of .viminfo"));
2567 #endif
2568     main_msg(_("-h  or  --help\tPrint Help (this message) and exit"));
2569     main_msg(_("--version\t\tPrint version information and exit"));
2570 
2571 #ifdef FEAT_GUI_X11
2572 # ifdef FEAT_GUI_MOTIF
2573     mch_msg(_("\nArguments recognised by gvim (Motif version):\n"));
2574 # else
2575 #  ifdef FEAT_GUI_ATHENA
2576 #   ifdef FEAT_GUI_NEXTAW
2577     mch_msg(_("\nArguments recognised by gvim (neXtaw version):\n"));
2578 #   else
2579     mch_msg(_("\nArguments recognised by gvim (Athena version):\n"));
2580 #   endif
2581 #  endif
2582 # endif
2583     main_msg(_("-display <display>\tRun vim on <display>"));
2584     main_msg(_("-iconic\t\tStart vim iconified"));
2585 # if 0
2586     main_msg(_("-name <name>\t\tUse resource as if vim was <name>"));
2587     mch_msg(_("\t\t\t  (Unimplemented)\n"));
2588 # endif
2589     main_msg(_("-background <color>\tUse <color> for the background (also: -bg)"));
2590     main_msg(_("-foreground <color>\tUse <color> for normal text (also: -fg)"));
2591     main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2592     main_msg(_("-boldfont <font>\tUse <font> for bold text"));
2593     main_msg(_("-italicfont <font>\tUse <font> for italic text"));
2594     main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2595     main_msg(_("-borderwidth <width>\tUse a border width of <width> (also: -bw)"));
2596     main_msg(_("-scrollbarwidth <width>  Use a scrollbar width of <width> (also: -sw)"));
2597 # ifdef FEAT_GUI_ATHENA
2598     main_msg(_("-menuheight <height>\tUse a menu bar height of <height> (also: -mh)"));
2599 # endif
2600     main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2601     main_msg(_("+reverse\t\tDon't use reverse video (also: +rv)"));
2602     main_msg(_("-xrm <resource>\tSet the specified resource"));
2603 #endif /* FEAT_GUI_X11 */
2604 #if defined(FEAT_GUI) && defined(RISCOS)
2605     mch_msg(_("\nArguments recognised by gvim (RISC OS version):\n"));
2606     main_msg(_("--columns <number>\tInitial width of window in columns"));
2607     main_msg(_("--rows <number>\tInitial height of window in rows"));
2608 #endif
2609 #ifdef FEAT_GUI_GTK
2610     mch_msg(_("\nArguments recognised by gvim (GTK+ version):\n"));
2611     main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2612     main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2613     main_msg(_("-reverse\t\tUse reverse video (also: -rv)"));
2614     main_msg(_("-display <display>\tRun vim on <display> (also: --display)"));
2615 # ifdef HAVE_GTK2
2616     main_msg(_("--role <role>\tSet a unique role to identify the main window"));
2617 # endif
2618     main_msg(_("--socketid <xid>\tOpen Vim inside another GTK widget"));
2619 #endif
2620 #ifdef FEAT_GUI_KDE
2621     mch_msg(_("\nArguments recognised by kvim (KDE version):\n"));
2622     main_msg(_("-black\t\tUse reverse video"));
2623 #if QT_VERSION>=300
2624     main_msg(_("-tip\t\t\tDisplay the tip dialog on startup"));
2625     main_msg(_("-notip\t\tDisable the tip dialog"));
2626 #endif
2627     main_msg(_("-font <font>\t\tUse <font> for normal text (also: -fn)"));
2628     main_msg(_("-geometry <geom>\tUse <geom> for initial geometry (also: -geom)"));
2629     main_msg(_("--display <display>\tRun vim on <display>"));
2630 #endif
2631 #ifdef FEAT_GUI_W32
2632     main_msg(_("-P <parent title>\tOpen Vim inside parent application"));
2633 #endif
2634 
2635 #ifdef FEAT_GUI_GNOME
2636     /* Gnome gives extra messages for --help if we continue, but not for -h. */
2637     if (gui.starting)
2638 	mch_msg("\n");
2639     else
2640 #endif
2641 	mch_exit(0);
2642 }
2643 
2644 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2645 /*
2646  * Check the result of the ATTENTION dialog:
2647  * When "Quit" selected, exit Vim.
2648  * When "Recover" selected, recover the file.
2649  */
2650     static void
2651 check_swap_exists_action()
2652 {
2653     if (swap_exists_action == SEA_QUIT)
2654 	getout(1);
2655     handle_swap_exists(NULL);
2656 }
2657 #endif
2658 
2659 #if defined(STARTUPTIME) || defined(PROTO)
2660 static void time_diff __ARGS((struct timeval *then, struct timeval *now));
2661 
2662 static struct timeval	prev_timeval;
2663 
2664 /*
2665  * Save the previous time before doing something that could nest.
2666  * set "*tv_rel" to the time elapsed so far.
2667  */
2668     void
2669 time_push(tv_rel, tv_start)
2670     void	*tv_rel, *tv_start;
2671 {
2672     *((struct timeval *)tv_rel) = prev_timeval;
2673     gettimeofday(&prev_timeval, NULL);
2674     ((struct timeval *)tv_rel)->tv_usec = prev_timeval.tv_usec
2675 					- ((struct timeval *)tv_rel)->tv_usec;
2676     ((struct timeval *)tv_rel)->tv_sec = prev_timeval.tv_sec
2677 					 - ((struct timeval *)tv_rel)->tv_sec;
2678     if (((struct timeval *)tv_rel)->tv_usec < 0)
2679     {
2680 	((struct timeval *)tv_rel)->tv_usec += 1000000;
2681 	--((struct timeval *)tv_rel)->tv_sec;
2682     }
2683     *(struct timeval *)tv_start = prev_timeval;
2684 }
2685 
2686 /*
2687  * Compute the previous time after doing something that could nest.
2688  * Subtract "*tp" from prev_timeval;
2689  * Note: The arguments are (void *) to avoid trouble with systems that don't
2690  * have struct timeval.
2691  */
2692     void
2693 time_pop(tp)
2694     void	*tp;	/* actually (struct timeval *) */
2695 {
2696     prev_timeval.tv_usec -= ((struct timeval *)tp)->tv_usec;
2697     prev_timeval.tv_sec -= ((struct timeval *)tp)->tv_sec;
2698     if (prev_timeval.tv_usec < 0)
2699     {
2700 	prev_timeval.tv_usec += 1000000;
2701 	--prev_timeval.tv_sec;
2702     }
2703 }
2704 
2705     static void
2706 time_diff(then, now)
2707     struct timeval	*then;
2708     struct timeval	*now;
2709 {
2710     long	usec;
2711     long	msec;
2712 
2713     usec = now->tv_usec - then->tv_usec;
2714     msec = (now->tv_sec - then->tv_sec) * 1000L + usec / 1000L,
2715     usec = usec % 1000L;
2716     fprintf(time_fd, "%03ld.%03ld", msec, usec >= 0 ? usec : usec + 1000L);
2717 }
2718 
2719     void
2720 time_msg(msg, tv_start)
2721     char	*msg;
2722     void	*tv_start;  /* only for do_source: start time; actually
2723 			       (struct timeval *) */
2724 {
2725     static struct timeval	start;
2726     struct timeval		now;
2727 
2728     if (time_fd != NULL)
2729     {
2730 	if (strstr(msg, "STARTING") != NULL)
2731 	{
2732 	    gettimeofday(&start, NULL);
2733 	    prev_timeval = start;
2734 	    fprintf(time_fd, "\n\ntimes in msec\n");
2735 	    fprintf(time_fd, " clock   self+sourced   self:  sourced script\n");
2736 	    fprintf(time_fd, " clock   elapsed:              other lines\n\n");
2737 	}
2738 	gettimeofday(&now, NULL);
2739 	time_diff(&start, &now);
2740 	if (((struct timeval *)tv_start) != NULL)
2741 	{
2742 	    fprintf(time_fd, "  ");
2743 	    time_diff(((struct timeval *)tv_start), &now);
2744 	}
2745 	fprintf(time_fd, "  ");
2746 	time_diff(&prev_timeval, &now);
2747 	prev_timeval = now;
2748 	fprintf(time_fd, ": %s\n", msg);
2749     }
2750 }
2751 
2752 # ifdef WIN3264
2753 /*
2754  * Windows doesn't have gettimeofday(), although it does have struct timeval.
2755  */
2756     int
2757 gettimeofday(struct timeval *tv, char *dummy)
2758 {
2759     long t = clock();
2760     tv->tv_sec = t / CLOCKS_PER_SEC;
2761     tv->tv_usec = (t - tv->tv_sec * CLOCKS_PER_SEC) * 1000000 / CLOCKS_PER_SEC;
2762     return 0;
2763 }
2764 # endif
2765 
2766 #endif
2767 
2768 #if defined(FEAT_CLIENTSERVER) || defined(PROTO)
2769 
2770 /*
2771  * Common code for the X command server and the Win32 command server.
2772  */
2773 
2774 static char_u *build_drop_cmd __ARGS((int filec, char **filev, int sendReply));
2775 
2776     static void
2777 cmdsrv_main(argc, argv, serverName_arg, serverStr)
2778     int		*argc;
2779     char	**argv;
2780     char_u	*serverName_arg;
2781     char_u	**serverStr;
2782 {
2783     char_u	*res;
2784     int		i;
2785     char_u	*sname;
2786     int		ret;
2787     int		didone = FALSE;
2788     int		exiterr = 0;
2789     char	**newArgV = argv + 1;
2790     int		newArgC = 1,
2791 		Argc = *argc;
2792     int		argtype;
2793 #define ARGTYPE_OTHER		0
2794 #define ARGTYPE_EDIT		1
2795 #define ARGTYPE_EDIT_WAIT	2
2796 #define ARGTYPE_SEND		3
2797     int		silent = FALSE;
2798 # ifndef FEAT_X11
2799     HWND	srv;
2800 # else
2801     Window	srv;
2802 
2803     setup_term_clip();
2804 # endif
2805 
2806     sname = serverMakeName(serverName_arg, argv[0]);
2807     if (sname == NULL)
2808 	return;
2809 
2810     /*
2811      * Execute the command server related arguments and remove them
2812      * from the argc/argv array; We may have to return into main()
2813      */
2814     for (i = 1; i < Argc; i++)
2815     {
2816 	res = NULL;
2817 	if (STRCMP(argv[i], "--") == 0)	/* end of options */
2818 	{
2819 	    for (; i < *argc; i++)
2820 	    {
2821 		*newArgV++ = argv[i];
2822 		newArgC++;
2823 	    }
2824 	    break;
2825 	}
2826 
2827 	if (STRICMP(argv[i], "--remote") == 0)
2828 	    argtype = ARGTYPE_EDIT;
2829 	else if (STRICMP(argv[i], "--remote-silent") == 0)
2830 	{
2831 	    argtype = ARGTYPE_EDIT;
2832 	    silent = TRUE;
2833 	}
2834 	else if (STRICMP(argv[i], "--remote-wait") == 0)
2835 	    argtype = ARGTYPE_EDIT_WAIT;
2836 	else if (STRICMP(argv[i], "--remote-wait-silent") == 0)
2837 	{
2838 	    argtype = ARGTYPE_EDIT_WAIT;
2839 	    silent = TRUE;
2840 	}
2841 	else if (STRICMP(argv[i], "--remote-send") == 0)
2842 	    argtype = ARGTYPE_SEND;
2843 	else
2844 	    argtype = ARGTYPE_OTHER;
2845 	if (argtype != ARGTYPE_OTHER)
2846 	{
2847 	    if (i == *argc - 1)
2848 		mainerr_arg_missing((char_u *)argv[i]);
2849 	    if (argtype == ARGTYPE_SEND)
2850 	    {
2851 		*serverStr = (char_u *)argv[i + 1];
2852 		i++;
2853 	    }
2854 	    else
2855 	    {
2856 		*serverStr = build_drop_cmd(*argc - i - 1, argv + i + 1,
2857 						argtype == ARGTYPE_EDIT_WAIT);
2858 		if (*serverStr == NULL)
2859 		{
2860 		    /* Probably out of memory, exit. */
2861 		    didone = TRUE;
2862 		    exiterr = 1;
2863 		    break;
2864 		}
2865 		Argc = i;
2866 	    }
2867 # ifdef FEAT_X11
2868 	    if (xterm_dpy == NULL)
2869 	    {
2870 		mch_errmsg(_("No display"));
2871 		ret = -1;
2872 	    }
2873 	    else
2874 		ret = serverSendToVim(xterm_dpy, sname, *serverStr,
2875 						    NULL, &srv, 0, 0, silent);
2876 # else
2877 	    /* Win32 always works? */
2878 	    ret = serverSendToVim(sname, *serverStr, NULL, &srv, 0, silent);
2879 # endif
2880 	    if (ret < 0)
2881 	    {
2882 		if (argtype == ARGTYPE_SEND)
2883 		{
2884 		    /* Failed to send, abort. */
2885 		    mch_errmsg(_(": Send failed.\n"));
2886 		    didone = TRUE;
2887 		    exiterr = 1;
2888 		}
2889 		else if (!silent)
2890 		    /* Let vim start normally.  */
2891 		    mch_errmsg(_(": Send failed. Trying to execute locally\n"));
2892 		break;
2893 	    }
2894 
2895 # ifdef FEAT_GUI_W32
2896 	    /* Guess that when the server name starts with "g" it's a GUI
2897 	     * server, which we can bring to the foreground here.
2898 	     * Foreground() in the server doesn't work very well. */
2899 	    if (argtype != ARGTYPE_SEND && TOUPPER_ASC(*sname) == 'G')
2900 		SetForegroundWindow(srv);
2901 # endif
2902 
2903 	    /*
2904 	     * For --remote-wait: Wait until the server did edit each
2905 	     * file.  Also detect that the server no longer runs.
2906 	     */
2907 	    if (ret >= 0 && argtype == ARGTYPE_EDIT_WAIT)
2908 	    {
2909 		int	numFiles = *argc - i - 1;
2910 		int	j;
2911 		char_u  *done = alloc(numFiles);
2912 		char_u  *p;
2913 # ifdef FEAT_GUI_W32
2914 		NOTIFYICONDATA ni;
2915 		int	count = 0;
2916 		extern HWND message_window;
2917 # endif
2918 
2919 		if (numFiles > 0 && argv[i + 1][0] == '+')
2920 		    /* Skip "+cmd" argument, don't wait for it to be edited. */
2921 		    --numFiles;
2922 
2923 # ifdef FEAT_GUI_W32
2924 		ni.cbSize = sizeof(ni);
2925 		ni.hWnd = message_window;
2926 		ni.uID = 0;
2927 		ni.uFlags = NIF_ICON|NIF_TIP;
2928 		ni.hIcon = LoadIcon((HINSTANCE)GetModuleHandle(0), "IDR_VIM");
2929 		sprintf(ni.szTip, _("%d of %d edited"), count, numFiles);
2930 		Shell_NotifyIcon(NIM_ADD, &ni);
2931 # endif
2932 
2933 		/* Wait for all files to unload in remote */
2934 		memset(done, 0, numFiles);
2935 		while (memchr(done, 0, numFiles) != NULL)
2936 		{
2937 # ifdef WIN32
2938 		    p = serverGetReply(srv, NULL, TRUE, TRUE);
2939 		    if (p == NULL)
2940 			break;
2941 # else
2942 		    if (serverReadReply(xterm_dpy, srv, &p, TRUE) < 0)
2943 			break;
2944 # endif
2945 		    j = atoi((char *)p);
2946 		    if (j >= 0 && j < numFiles)
2947 		    {
2948 # ifdef FEAT_GUI_W32
2949 			++count;
2950 			sprintf(ni.szTip, _("%d of %d edited"),
2951 							     count, numFiles);
2952 			Shell_NotifyIcon(NIM_MODIFY, &ni);
2953 # endif
2954 			done[j] = 1;
2955 		    }
2956 		}
2957 # ifdef FEAT_GUI_W32
2958 		Shell_NotifyIcon(NIM_DELETE, &ni);
2959 # endif
2960 	    }
2961 	}
2962 	else if (STRICMP(argv[i], "--remote-expr") == 0)
2963 	{
2964 	    if (i == *argc - 1)
2965 		mainerr_arg_missing((char_u *)argv[i]);
2966 # ifdef WIN32
2967 	    /* Win32 always works? */
2968 	    if (serverSendToVim(sname, (char_u *)argv[i + 1],
2969 						    &res, NULL, 1, FALSE) < 0)
2970 # else
2971 	    if (xterm_dpy == NULL)
2972 		mch_errmsg(_("No display: Send expression failed.\n"));
2973 	    else if (serverSendToVim(xterm_dpy, sname, (char_u *)argv[i + 1],
2974 						 &res, NULL, 1, 1, FALSE) < 0)
2975 # endif
2976 	    {
2977 		if (res != NULL && *res != NUL)
2978 		{
2979 		    /* Output error from remote */
2980 		    mch_errmsg((char *)res);
2981 		    vim_free(res);
2982 		    res = NULL;
2983 		}
2984 		mch_errmsg(_(": Send expression failed.\n"));
2985 	    }
2986 	}
2987 	else if (STRICMP(argv[i], "--serverlist") == 0)
2988 	{
2989 # ifdef WIN32
2990 	    /* Win32 always works? */
2991 	    res = serverGetVimNames();
2992 # else
2993 	    if (xterm_dpy != NULL)
2994 		res = serverGetVimNames(xterm_dpy);
2995 # endif
2996 	    if (called_emsg)
2997 		mch_errmsg("\n");
2998 	}
2999 	else if (STRICMP(argv[i], "--servername") == 0)
3000 	{
3001 	    /* Alredy processed. Take it out of the command line */
3002 	    i++;
3003 	    continue;
3004 	}
3005 	else
3006 	{
3007 	    *newArgV++ = argv[i];
3008 	    newArgC++;
3009 	    continue;
3010 	}
3011 	didone = TRUE;
3012 	if (res != NULL && *res != NUL)
3013 	{
3014 	    mch_msg((char *)res);
3015 	    if (res[STRLEN(res) - 1] != '\n')
3016 		mch_msg("\n");
3017 	}
3018 	vim_free(res);
3019     }
3020 
3021     if (didone)
3022     {
3023 	display_errors();	/* display any collected messages */
3024 	exit(exiterr);	/* Mission accomplished - get out */
3025     }
3026 
3027     /* Return back into main() */
3028     *argc = newArgC;
3029     vim_free(sname);
3030 }
3031 
3032 /*
3033  * Build a ":drop" command to send to a Vim server.
3034  */
3035     static char_u *
3036 build_drop_cmd(filec, filev, sendReply)
3037     int		filec;
3038     char	**filev;
3039     int		sendReply;
3040 {
3041     garray_T	ga;
3042     int		i;
3043     char_u	*inicmd = NULL;
3044     char_u	*p;
3045     char_u	cwd[MAXPATHL];
3046 
3047     if (filec > 0 && filev[0][0] == '+')
3048     {
3049 	inicmd = (char_u *)filev[0] + 1;
3050 	filev++;
3051 	filec--;
3052     }
3053     /* Check if we have at least one argument. */
3054     if (filec <= 0)
3055 	mainerr_arg_missing((char_u *)filev[-1]);
3056     if (mch_dirname(cwd, MAXPATHL) != OK)
3057 	return NULL;
3058     if ((p = vim_strsave_escaped_ext(cwd, PATH_ESC_CHARS, '\\', TRUE)) == NULL)
3059 	return NULL;
3060     ga_init2(&ga, 1, 100);
3061     ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd ");
3062     ga_concat(&ga, p);
3063     /* Call inputsave() so that a prompt for an encryption key works. */
3064     ga_concat(&ga, (char_u *)"<CR>:if exists('*inputsave')|call inputsave()|endif|drop");
3065     vim_free(p);
3066     for (i = 0; i < filec; i++)
3067     {
3068 	/* On Unix the shell has already expanded the wildcards, don't want to
3069 	 * do it again in the Vim server.  On MS-Windows only need to escape a
3070 	 * space. */
3071 	p = vim_strsave_escaped((char_u *)filev[i],
3072 #ifdef UNIX
3073 		PATH_ESC_CHARS
3074 #else
3075 		(char_u *)" "
3076 #endif
3077 		);
3078 	if (p == NULL)
3079 	{
3080 	    vim_free(ga.ga_data);
3081 	    return NULL;
3082 	}
3083 	ga_concat(&ga, (char_u *)" ");
3084 	ga_concat(&ga, p);
3085 	vim_free(p);
3086     }
3087     /* The :drop commands goes to Insert mode when 'insertmode' is set, use
3088      * CTRL-\ CTRL-N again. */
3089     ga_concat(&ga, (char_u *)"|if exists('*inputrestore')|call inputrestore()|endif<CR>");
3090     ga_concat(&ga, (char_u *)"<C-\\><C-N>:cd -");
3091     if (sendReply)
3092 	ga_concat(&ga, (char_u *)"<CR>:call SetupRemoteReplies()");
3093     ga_concat(&ga, (char_u *)"<CR>:");
3094     if (inicmd != NULL)
3095     {
3096 	/* Can't use <CR> after "inicmd", because an "startinsert" would cause
3097 	 * the following commands to be inserted as text.  Use a "|",
3098 	 * hopefully "inicmd" does allow this... */
3099 	ga_concat(&ga, inicmd);
3100 	ga_concat(&ga, (char_u *)"|");
3101     }
3102     /* Bring the window to the foreground, goto Insert mode when 'im' set and
3103      * clear command line. */
3104     ga_concat(&ga, (char_u *)"cal foreground()|if &im|star|en|ec<CR>");
3105     ga_append(&ga, NUL);
3106     return ga.ga_data;
3107 }
3108 
3109 /*
3110  * Replace termcodes such as <CR> and insert as key presses if there is room.
3111  */
3112     void
3113 server_to_input_buf(str)
3114     char_u	*str;
3115 {
3116     char_u      *ptr = NULL;
3117     char_u      *cpo_save = p_cpo;
3118 
3119     /* Set 'cpoptions' the way we want it.
3120      *    B set - backslashes are *not* treated specially
3121      *    k set - keycodes are *not* reverse-engineered
3122      *    < unset - <Key> sequences *are* interpreted
3123      *  The last parameter of replace_termcodes() is TRUE so that the <lt>
3124      *  sequence is recognised - needed for a real backslash.
3125      */
3126     p_cpo = (char_u *)"Bk";
3127     str = replace_termcodes((char_u *)str, &ptr, FALSE, TRUE);
3128     p_cpo = cpo_save;
3129 
3130     if (*ptr != NUL)	/* trailing CTRL-V results in nothing */
3131     {
3132 	/*
3133 	 * Add the string to the input stream.
3134 	 * Can't use add_to_input_buf() here, we now have K_SPECIAL bytes.
3135 	 *
3136 	 * First clear typed characters from the typeahead buffer, there could
3137 	 * be half a mapping there.  Then append to the existing string, so
3138 	 * that multiple commands from a client are concatenated.
3139 	 */
3140 	if (typebuf.tb_maplen < typebuf.tb_len)
3141 	    del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen);
3142 	(void)ins_typebuf(str, REMAP_NONE, typebuf.tb_len, TRUE, FALSE);
3143 
3144 	/* Let input_available() know we inserted text in the typeahead
3145 	 * buffer. */
3146 	received_from_client = TRUE;
3147     }
3148     vim_free((char_u *)ptr);
3149 }
3150 
3151 /*
3152  * Evaluate an expression that the client sent to a string.
3153  * Handles disabling error messages and disables debugging, otherwise Vim
3154  * hangs, waiting for "cont" to be typed.
3155  */
3156     char_u *
3157 eval_client_expr_to_string(expr)
3158     char_u *expr;
3159 {
3160     char_u	*res;
3161     int		save_dbl = debug_break_level;
3162     int		save_ro = redir_off;
3163 
3164     debug_break_level = -1;
3165     redir_off = 0;
3166     ++emsg_skip;
3167 
3168     res = eval_to_string(expr, NULL);
3169 
3170     debug_break_level = save_dbl;
3171     redir_off = save_ro;
3172     --emsg_skip;
3173 
3174     return res;
3175 }
3176 
3177 
3178 /*
3179  * Make our basic server name: use the specified "arg" if given, otherwise use
3180  * the tail of the command "cmd" we were started with.
3181  * Return the name in allocated memory.  This doesn't include a serial number.
3182  */
3183     static char_u *
3184 serverMakeName(arg, cmd)
3185     char_u	*arg;
3186     char	*cmd;
3187 {
3188     char_u *p;
3189 
3190     if (arg != NULL && *arg != NUL)
3191 	p = vim_strsave_up(arg);
3192     else
3193     {
3194 	p = vim_strsave_up(gettail((char_u *)cmd));
3195 	/* Remove .exe or .bat from the name. */
3196 	if (p != NULL && vim_strchr(p, '.') != NULL)
3197 	    *vim_strchr(p, '.') = NUL;
3198     }
3199     return p;
3200 }
3201 #endif /* FEAT_CLIENTSERVER */
3202 
3203 /*
3204  * When FEAT_FKMAP is defined, also compile the Farsi source code.
3205  */
3206 #if defined(FEAT_FKMAP) || defined(PROTO)
3207 # include "farsi.c"
3208 #endif
3209 
3210 /*
3211  * When FEAT_ARABIC is defined, also compile the Arabic source code.
3212  */
3213 #if defined(FEAT_ARABIC) || defined(PROTO)
3214 # include "arabic.c"
3215 #endif
3216