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/*
11 * fileio.c: read from and write to a file
12 */
13
14#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
15# include "vimio.h"	/* for lseek(), must be before vim.h */
16#endif
17
18#if defined __EMX__
19# include "vimio.h"	/* for mktemp(), CJW 1997-12-03 */
20#endif
21
22#include "vim.h"
23
24#if defined(__TANDEM) || defined(__MINT__)
25# include <limits.h>		/* for SSIZE_MAX */
26#endif
27
28#if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
29# include <utime.h>		/* for struct utimbuf */
30#endif
31
32#if defined(HAVE_COPYFILE_H)
33#include <copyfile.h>
34#endif
35
36#define BUFSIZE		8192	/* size of normal write buffer */
37#define SMBUFSIZE	256	/* size of emergency write buffer */
38
39#ifdef FEAT_CRYPT
40/* crypt_magic[0] is pkzip crypt, crypt_magic[1] is sha2+blowfish */
41static char	*crypt_magic[] = {"VimCrypt~01!", "VimCrypt~02!"};
42static char	crypt_magic_head[] = "VimCrypt~";
43# define CRYPT_MAGIC_LEN	12		/* must be multiple of 4! */
44
45/* For blowfish, after the magic header, we store 8 bytes of salt and then 8
46 * bytes of seed (initialisation vector). */
47static int	crypt_salt_len[] = {0, 8};
48static int	crypt_seed_len[] = {0, 8};
49#define CRYPT_SALT_LEN_MAX 8
50#define CRYPT_SEED_LEN_MAX 8
51#endif
52
53/* Is there any system that doesn't have access()? */
54#define USE_MCH_ACCESS
55
56#if defined(sun) && defined(S_ISCHR)
57# define OPEN_CHR_FILES
58static int is_dev_fd_file(char_u *fname);
59#endif
60#ifdef FEAT_MBYTE
61static char_u *next_fenc __ARGS((char_u **pp));
62# ifdef FEAT_EVAL
63static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp));
64# endif
65#endif
66#ifdef FEAT_VIMINFO
67static void check_marks_read __ARGS((void));
68#endif
69#ifdef FEAT_CRYPT
70static int crypt_method_from_magic __ARGS((char *ptr, int len));
71static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, char_u *fname, int *did_ask));
72#endif
73#ifdef UNIX
74static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime));
75#endif
76static int set_rw_fname __ARGS((char_u *fname, char_u *sfname));
77static int msg_add_fileformat __ARGS((int eol_type));
78static void msg_add_eol __ARGS((void));
79static int check_mtime __ARGS((buf_T *buf, struct stat *s));
80static int time_differs __ARGS((long t1, long t2));
81#ifdef FEAT_AUTOCMD
82static int apply_autocmds_exarg __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap));
83static int au_find_group __ARGS((char_u *name));
84
85# define AUGROUP_DEFAULT    -1	    /* default autocmd group */
86# define AUGROUP_ERROR	    -2	    /* erroneous autocmd group */
87# define AUGROUP_ALL	    -3	    /* all autocmd groups */
88#endif
89
90#if defined(FEAT_CRYPT) || defined(FEAT_MBYTE)
91# define HAS_BW_FLAGS
92# define FIO_LATIN1	0x01	/* convert Latin1 */
93# define FIO_UTF8	0x02	/* convert UTF-8 */
94# define FIO_UCS2	0x04	/* convert UCS-2 */
95# define FIO_UCS4	0x08	/* convert UCS-4 */
96# define FIO_UTF16	0x10	/* convert UTF-16 */
97# ifdef WIN3264
98#  define FIO_CODEPAGE	0x20	/* convert MS-Windows codepage */
99#  define FIO_PUT_CP(x) (((x) & 0xffff) << 16)	/* put codepage in top word */
100#  define FIO_GET_CP(x)	(((x)>>16) & 0xffff)	/* get codepage from top word */
101# endif
102# ifdef MACOS_X
103#  define FIO_MACROMAN	0x20	/* convert MacRoman */
104# endif
105# define FIO_ENDIAN_L	0x80	/* little endian */
106# define FIO_ENCRYPTED	0x1000	/* encrypt written bytes */
107# define FIO_NOCONVERT	0x2000	/* skip encoding conversion */
108# define FIO_UCSBOM	0x4000	/* check for BOM at start of file */
109# define FIO_ALL	-1	/* allow all formats */
110#endif
111
112/* When converting, a read() or write() may leave some bytes to be converted
113 * for the next call.  The value is guessed... */
114#define CONV_RESTLEN 30
115
116/* We have to guess how much a sequence of bytes may expand when converting
117 * with iconv() to be able to allocate a buffer. */
118#define ICONV_MULT 8
119
120/*
121 * Structure to pass arguments from buf_write() to buf_write_bytes().
122 */
123struct bw_info
124{
125    int		bw_fd;		/* file descriptor */
126    char_u	*bw_buf;	/* buffer with data to be written */
127    int		bw_len;		/* length of data */
128#ifdef HAS_BW_FLAGS
129    int		bw_flags;	/* FIO_ flags */
130#endif
131#ifdef FEAT_MBYTE
132    char_u	bw_rest[CONV_RESTLEN]; /* not converted bytes */
133    int		bw_restlen;	/* nr of bytes in bw_rest[] */
134    int		bw_first;	/* first write call */
135    char_u	*bw_conv_buf;	/* buffer for writing converted chars */
136    int		bw_conv_buflen; /* size of bw_conv_buf */
137    int		bw_conv_error;	/* set for conversion error */
138    linenr_T	bw_conv_error_lnum;  /* first line with error or zero */
139    linenr_T	bw_start_lnum;  /* line number at start of buffer */
140# ifdef USE_ICONV
141    iconv_t	bw_iconv_fd;	/* descriptor for iconv() or -1 */
142# endif
143#endif
144};
145
146static int  buf_write_bytes __ARGS((struct bw_info *ip));
147
148#ifdef FEAT_MBYTE
149static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp));
150static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags));
151static int need_conversion __ARGS((char_u *fenc));
152static int get_fio_flags __ARGS((char_u *ptr));
153static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags));
154static int make_bom __ARGS((char_u *buf, char_u *name));
155# ifdef WIN3264
156static int get_win_fio_flags __ARGS((char_u *ptr));
157# endif
158# ifdef MACOS_X
159static int get_mac_fio_flags __ARGS((char_u *ptr));
160# endif
161#endif
162static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf));
163#ifdef TEMPDIRNAMES
164static void vim_settempdir __ARGS((char_u *tempdir));
165#endif
166#ifdef FEAT_AUTOCMD
167static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
168#endif
169
170    void
171filemess(buf, name, s, attr)
172    buf_T	*buf;
173    char_u	*name;
174    char_u	*s;
175    int		attr;
176{
177    int		msg_scroll_save;
178
179    if (msg_silent != 0)
180	return;
181    msg_add_fname(buf, name);	    /* put file name in IObuff with quotes */
182    /* If it's extremely long, truncate it. */
183    if (STRLEN(IObuff) > IOSIZE - 80)
184	IObuff[IOSIZE - 80] = NUL;
185    STRCAT(IObuff, s);
186    /*
187     * For the first message may have to start a new line.
188     * For further ones overwrite the previous one, reset msg_scroll before
189     * calling filemess().
190     */
191    msg_scroll_save = msg_scroll;
192    if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
193	msg_scroll = FALSE;
194    if (!msg_scroll)	/* wait a bit when overwriting an error msg */
195	check_for_delay(FALSE);
196    msg_start();
197    msg_scroll = msg_scroll_save;
198    msg_scrolled_ign = TRUE;
199    /* may truncate the message to avoid a hit-return prompt */
200    msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
201    msg_clr_eos();
202    out_flush();
203    msg_scrolled_ign = FALSE;
204}
205
206/*
207 * Read lines from file "fname" into the buffer after line "from".
208 *
209 * 1. We allocate blocks with lalloc, as big as possible.
210 * 2. Each block is filled with characters from the file with a single read().
211 * 3. The lines are inserted in the buffer with ml_append().
212 *
213 * (caller must check that fname != NULL, unless READ_STDIN is used)
214 *
215 * "lines_to_skip" is the number of lines that must be skipped
216 * "lines_to_read" is the number of lines that are appended
217 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
218 *
219 * flags:
220 * READ_NEW	starting to edit a new buffer
221 * READ_FILTER	reading filter output
222 * READ_STDIN	read from stdin instead of a file
223 * READ_BUFFER	read from curbuf instead of a file (converting after reading
224 *		stdin)
225 * READ_DUMMY	read into a dummy buffer (to check if file contents changed)
226 * READ_KEEP_UNDO  don't clear undo info or read it from a file
227 *
228 * return FAIL for failure, OK otherwise
229 */
230    int
231readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags)
232    char_u	*fname;
233    char_u	*sfname;
234    linenr_T	from;
235    linenr_T	lines_to_skip;
236    linenr_T	lines_to_read;
237    exarg_T	*eap;			/* can be NULL! */
238    int		flags;
239{
240    int		fd = 0;
241    int		newfile = (flags & READ_NEW);
242    int		check_readonly;
243    int		filtering = (flags & READ_FILTER);
244    int		read_stdin = (flags & READ_STDIN);
245    int		read_buffer = (flags & READ_BUFFER);
246    int		set_options = newfile || read_buffer
247					   || (eap != NULL && eap->read_edit);
248    linenr_T	read_buf_lnum = 1;	/* next line to read from curbuf */
249    colnr_T	read_buf_col = 0;	/* next char to read from this line */
250    char_u	c;
251    linenr_T	lnum = from;
252    char_u	*ptr = NULL;		/* pointer into read buffer */
253    char_u	*buffer = NULL;		/* read buffer */
254    char_u	*new_buffer = NULL;	/* init to shut up gcc */
255    char_u	*line_start = NULL;	/* init to shut up gcc */
256    int		wasempty;		/* buffer was empty before reading */
257    colnr_T	len;
258    long	size = 0;
259    char_u	*p;
260    off_t	filesize = 0;
261    int		skip_read = FALSE;
262#ifdef FEAT_CRYPT
263    char_u	*cryptkey = NULL;
264    int		did_ask_for_key = FALSE;
265#endif
266#ifdef FEAT_PERSISTENT_UNDO
267    context_sha256_T sha_ctx;
268    int		read_undo_file = FALSE;
269#endif
270    int		split = 0;		/* number of split lines */
271#define UNKNOWN	 0x0fffffff		/* file size is unknown */
272    linenr_T	linecnt;
273    int		error = FALSE;		/* errors encountered */
274    int		ff_error = EOL_UNKNOWN; /* file format with errors */
275    long	linerest = 0;		/* remaining chars in line */
276#ifdef UNIX
277    int		perm = 0;
278    int		swap_mode = -1;		/* protection bits for swap file */
279#else
280    int		perm;
281#endif
282    int		fileformat = 0;		/* end-of-line format */
283    int		keep_fileformat = FALSE;
284    struct stat	st;
285    int		file_readonly;
286    linenr_T	skip_count = 0;
287    linenr_T	read_count = 0;
288    int		msg_save = msg_scroll;
289    linenr_T	read_no_eol_lnum = 0;   /* non-zero lnum when last line of
290					 * last read was missing the eol */
291    int		try_mac = (vim_strchr(p_ffs, 'm') != NULL);
292    int		try_dos = (vim_strchr(p_ffs, 'd') != NULL);
293    int		try_unix = (vim_strchr(p_ffs, 'x') != NULL);
294    int		file_rewind = FALSE;
295#ifdef FEAT_MBYTE
296    int		can_retry;
297    linenr_T	conv_error = 0;		/* line nr with conversion error */
298    linenr_T	illegal_byte = 0;	/* line nr with illegal byte */
299    int		keep_dest_enc = FALSE;	/* don't retry when char doesn't fit
300					   in destination encoding */
301    int		bad_char_behavior = BAD_REPLACE;
302					/* BAD_KEEP, BAD_DROP or character to
303					 * replace with */
304    char_u	*tmpname = NULL;	/* name of 'charconvert' output file */
305    int		fio_flags = 0;
306    char_u	*fenc;			/* fileencoding to use */
307    int		fenc_alloced;		/* fenc_next is in allocated memory */
308    char_u	*fenc_next = NULL;	/* next item in 'fencs' or NULL */
309    int		advance_fenc = FALSE;
310    long	real_size = 0;
311# ifdef USE_ICONV
312    iconv_t	iconv_fd = (iconv_t)-1;	/* descriptor for iconv() or -1 */
313#  ifdef FEAT_EVAL
314    int		did_iconv = FALSE;	/* TRUE when iconv() failed and trying
315					   'charconvert' next */
316#  endif
317# endif
318    int		converted = FALSE;	/* TRUE if conversion done */
319    int		notconverted = FALSE;	/* TRUE if conversion wanted but it
320					   wasn't possible */
321    char_u	conv_rest[CONV_RESTLEN];
322    int		conv_restlen = 0;	/* nr of bytes in conv_rest[] */
323#endif
324#ifdef FEAT_AUTOCMD
325    buf_T	*old_curbuf;
326    char_u	*old_b_ffname;
327    char_u	*old_b_fname;
328    int		using_b_ffname;
329    int		using_b_fname;
330#endif
331
332    write_no_eol_lnum = 0;	/* in case it was set by the previous read */
333
334    /*
335     * If there is no file name yet, use the one for the read file.
336     * BF_NOTEDITED is set to reflect this.
337     * Don't do this for a read from a filter.
338     * Only do this when 'cpoptions' contains the 'f' flag.
339     */
340    if (curbuf->b_ffname == NULL
341	    && !filtering
342	    && fname != NULL
343	    && vim_strchr(p_cpo, CPO_FNAMER) != NULL
344	    && !(flags & READ_DUMMY))
345    {
346	if (set_rw_fname(fname, sfname) == FAIL)
347	    return FAIL;
348    }
349
350#ifdef FEAT_AUTOCMD
351    /* Remember the initial values of curbuf, curbuf->b_ffname and
352     * curbuf->b_fname to detect whether they are altered as a result of
353     * executing nasty autocommands.  Also check if "fname" and "sfname"
354     * point to one of these values. */
355    old_curbuf = curbuf;
356    old_b_ffname = curbuf->b_ffname;
357    old_b_fname = curbuf->b_fname;
358    using_b_ffname = (fname == curbuf->b_ffname)
359					      || (sfname == curbuf->b_ffname);
360    using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
361#endif
362
363    /* After reading a file the cursor line changes but we don't want to
364     * display the line. */
365    ex_no_reprint = TRUE;
366
367    /* don't display the file info for another buffer now */
368    need_fileinfo = FALSE;
369
370    /*
371     * For Unix: Use the short file name whenever possible.
372     * Avoids problems with networks and when directory names are changed.
373     * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
374     * another directory, which we don't detect.
375     */
376    if (sfname == NULL)
377	sfname = fname;
378#if defined(UNIX) || defined(__EMX__)
379    fname = sfname;
380#endif
381
382#ifdef FEAT_AUTOCMD
383    /*
384     * The BufReadCmd and FileReadCmd events intercept the reading process by
385     * executing the associated commands instead.
386     */
387    if (!filtering && !read_stdin && !read_buffer)
388    {
389	pos_T	    pos;
390
391	pos = curbuf->b_op_start;
392
393	/* Set '[ mark to the line above where the lines go (line 1 if zero). */
394	curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
395	curbuf->b_op_start.col = 0;
396
397	if (newfile)
398	{
399	    if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
400							  FALSE, curbuf, eap))
401#ifdef FEAT_EVAL
402		return aborting() ? FAIL : OK;
403#else
404		return OK;
405#endif
406	}
407	else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
408							    FALSE, NULL, eap))
409#ifdef FEAT_EVAL
410	    return aborting() ? FAIL : OK;
411#else
412	    return OK;
413#endif
414
415	curbuf->b_op_start = pos;
416    }
417#endif
418
419    if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
420	msg_scroll = FALSE;	/* overwrite previous file message */
421    else
422	msg_scroll = TRUE;	/* don't overwrite previous file message */
423
424    /*
425     * If the name ends in a path separator, we can't open it.  Check here,
426     * because reading the file may actually work, but then creating the swap
427     * file may destroy it!  Reported on MS-DOS and Win 95.
428     * If the name is too long we might crash further on, quit here.
429     */
430    if (fname != NULL && *fname != NUL)
431    {
432	p = fname + STRLEN(fname);
433	if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
434	{
435	    filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
436	    msg_end();
437	    msg_scroll = msg_save;
438	    return FAIL;
439	}
440    }
441
442#ifdef UNIX
443    /*
444     * On Unix it is possible to read a directory, so we have to
445     * check for it before the mch_open().
446     */
447    if (!read_stdin && !read_buffer)
448    {
449	perm = mch_getperm(fname);
450	if (perm >= 0 && !S_ISREG(perm)		    /* not a regular file ... */
451# ifdef S_ISFIFO
452		      && !S_ISFIFO(perm)	    /* ... or fifo */
453# endif
454# ifdef S_ISSOCK
455		      && !S_ISSOCK(perm)	    /* ... or socket */
456# endif
457# ifdef OPEN_CHR_FILES
458		      && !(S_ISCHR(perm) && is_dev_fd_file(fname))
459			/* ... or a character special file named /dev/fd/<n> */
460# endif
461						)
462	{
463	    if (S_ISDIR(perm))
464		filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
465	    else
466		filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
467	    msg_end();
468	    msg_scroll = msg_save;
469	    return FAIL;
470	}
471
472# if defined(MSDOS) || defined(MSWIN) || defined(OS2)
473	/*
474	 * MS-Windows allows opening a device, but we will probably get stuck
475	 * trying to read it.
476	 */
477	if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
478	{
479	    filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
480	    msg_end();
481	    msg_scroll = msg_save;
482	    return FAIL;
483	}
484# endif
485    }
486#endif
487
488    /* set default 'fileformat' */
489    if (set_options)
490    {
491	if (eap != NULL && eap->force_ff != 0)
492	    set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
493	else if (*p_ffs != NUL)
494	    set_fileformat(default_fileformat(), OPT_LOCAL);
495    }
496
497    /* set or reset 'binary' */
498    if (eap != NULL && eap->force_bin != 0)
499    {
500	int	oldval = curbuf->b_p_bin;
501
502	curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
503	set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
504    }
505
506    /*
507     * When opening a new file we take the readonly flag from the file.
508     * Default is r/w, can be set to r/o below.
509     * Don't reset it when in readonly mode
510     * Only set/reset b_p_ro when BF_CHECK_RO is set.
511     */
512    check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
513    if (check_readonly && !readonlymode)
514	curbuf->b_p_ro = FALSE;
515
516    if (newfile && !read_stdin && !read_buffer)
517    {
518	/* Remember time of file.
519	 * For RISCOS, also remember the filetype.
520	 */
521	if (mch_stat((char *)fname, &st) >= 0)
522	{
523	    buf_store_time(curbuf, &st, fname);
524	    curbuf->b_mtime_read = curbuf->b_mtime;
525
526#if defined(RISCOS) && defined(FEAT_OSFILETYPE)
527	    /* Read the filetype into the buffer local filetype option. */
528	    mch_read_filetype(fname);
529#endif
530#ifdef UNIX
531	    /*
532	     * Use the protection bits of the original file for the swap file.
533	     * This makes it possible for others to read the name of the
534	     * edited file from the swapfile, but only if they can read the
535	     * edited file.
536	     * Remove the "write" and "execute" bits for group and others
537	     * (they must not write the swapfile).
538	     * Add the "read" and "write" bits for the user, otherwise we may
539	     * not be able to write to the file ourselves.
540	     * Setting the bits is done below, after creating the swap file.
541	     */
542	    swap_mode = (st.st_mode & 0644) | 0600;
543#endif
544#ifdef FEAT_CW_EDITOR
545	    /* Get the FSSpec on MacOS
546	     * TODO: Update it properly when the buffer name changes
547	     */
548	    (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
549#endif
550#ifdef VMS
551	    curbuf->b_fab_rfm = st.st_fab_rfm;
552	    curbuf->b_fab_rat = st.st_fab_rat;
553	    curbuf->b_fab_mrs = st.st_fab_mrs;
554#endif
555	}
556	else
557	{
558	    curbuf->b_mtime = 0;
559	    curbuf->b_mtime_read = 0;
560	    curbuf->b_orig_size = 0;
561	    curbuf->b_orig_mode = 0;
562	}
563
564	/* Reset the "new file" flag.  It will be set again below when the
565	 * file doesn't exist. */
566	curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
567    }
568
569/*
570 * for UNIX: check readonly with perm and mch_access()
571 * for RISCOS: same as Unix, otherwise file gets re-datestamped!
572 * for MSDOS and Amiga: check readonly by trying to open the file for writing
573 */
574    file_readonly = FALSE;
575    if (read_stdin)
576    {
577#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
578	/* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */
579	setmode(0, O_BINARY);
580#endif
581    }
582    else if (!read_buffer)
583    {
584#ifdef USE_MCH_ACCESS
585	if (
586# ifdef UNIX
587	    !(perm & 0222) ||
588# endif
589				mch_access((char *)fname, W_OK))
590	    file_readonly = TRUE;
591	fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
592#else
593	if (!newfile
594		|| readonlymode
595		|| (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
596	{
597	    file_readonly = TRUE;
598	    /* try to open ro */
599	    fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
600	}
601#endif
602    }
603
604    if (fd < 0)			    /* cannot open at all */
605    {
606#ifndef UNIX
607	int	isdir_f;
608#endif
609	msg_scroll = msg_save;
610#ifndef UNIX
611	/*
612	 * On MSDOS and Amiga we can't open a directory, check here.
613	 */
614	isdir_f = (mch_isdir(fname));
615	perm = mch_getperm(fname);  /* check if the file exists */
616	if (isdir_f)
617	{
618	    filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
619	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
620	}
621	else
622#endif
623	    if (newfile)
624	    {
625		if (perm < 0
626#ifdef ENOENT
627			&& errno == ENOENT
628#endif
629		   )
630		{
631		    /*
632		     * Set the 'new-file' flag, so that when the file has
633		     * been created by someone else, a ":w" will complain.
634		     */
635		    curbuf->b_flags |= BF_NEW;
636
637		    /* Create a swap file now, so that other Vims are warned
638		     * that we are editing this file.  Don't do this for a
639		     * "nofile" or "nowrite" buffer type. */
640#ifdef FEAT_QUICKFIX
641		    if (!bt_dontwrite(curbuf))
642#endif
643		    {
644			check_need_swap(newfile);
645#ifdef FEAT_AUTOCMD
646			/* SwapExists autocommand may mess things up */
647			if (curbuf != old_curbuf
648				|| (using_b_ffname
649					&& (old_b_ffname != curbuf->b_ffname))
650				|| (using_b_fname
651					 && (old_b_fname != curbuf->b_fname)))
652			{
653			    EMSG(_(e_auchangedbuf));
654			    return FAIL;
655			}
656#endif
657		    }
658		    if (dir_of_file_exists(fname))
659			filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
660		    else
661			filemess(curbuf, sfname,
662					   (char_u *)_("[New DIRECTORY]"), 0);
663#ifdef FEAT_VIMINFO
664		    /* Even though this is a new file, it might have been
665		     * edited before and deleted.  Get the old marks. */
666		    check_marks_read();
667#endif
668#ifdef FEAT_MBYTE
669		    if (eap != NULL && eap->force_enc != 0)
670		    {
671			/* set forced 'fileencoding' */
672			fenc = enc_canonize(eap->cmd + eap->force_enc);
673			if (fenc != NULL)
674			    set_string_option_direct((char_u *)"fenc", -1,
675						 fenc, OPT_FREE|OPT_LOCAL, 0);
676			vim_free(fenc);
677		    }
678#endif
679#ifdef FEAT_AUTOCMD
680		    apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
681							  FALSE, curbuf, eap);
682#endif
683		    /* remember the current fileformat */
684		    save_file_ff(curbuf);
685
686#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
687		    if (aborting())   /* autocmds may abort script processing */
688			return FAIL;
689#endif
690		    return OK;	    /* a new file is not an error */
691		}
692		else
693		{
694		    filemess(curbuf, sfname, (char_u *)(
695# ifdef EFBIG
696			    (errno == EFBIG) ? _("[File too big]") :
697# endif
698# ifdef EOVERFLOW
699			    (errno == EOVERFLOW) ? _("[File too big]") :
700# endif
701						_("[Permission Denied]")), 0);
702		    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
703		}
704	    }
705
706	return FAIL;
707    }
708
709    /*
710     * Only set the 'ro' flag for readonly files the first time they are
711     * loaded.	Help files always get readonly mode
712     */
713    if ((check_readonly && file_readonly) || curbuf->b_help)
714	curbuf->b_p_ro = TRUE;
715
716    if (set_options)
717    {
718	/* Don't change 'eol' if reading from buffer as it will already be
719	 * correctly set when reading stdin. */
720	if (!read_buffer)
721	{
722	    curbuf->b_p_eol = TRUE;
723	    curbuf->b_start_eol = TRUE;
724	}
725#ifdef FEAT_MBYTE
726	curbuf->b_p_bomb = FALSE;
727	curbuf->b_start_bomb = FALSE;
728#endif
729    }
730
731    /* Create a swap file now, so that other Vims are warned that we are
732     * editing this file.
733     * Don't do this for a "nofile" or "nowrite" buffer type. */
734#ifdef FEAT_QUICKFIX
735    if (!bt_dontwrite(curbuf))
736#endif
737    {
738	check_need_swap(newfile);
739#ifdef FEAT_AUTOCMD
740	if (!read_stdin && (curbuf != old_curbuf
741		|| (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
742		|| (using_b_fname && (old_b_fname != curbuf->b_fname))))
743	{
744	    EMSG(_(e_auchangedbuf));
745	    if (!read_buffer)
746		close(fd);
747	    return FAIL;
748	}
749#endif
750#ifdef UNIX
751	/* Set swap file protection bits after creating it. */
752	if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
753			  && curbuf->b_ml.ml_mfp->mf_fname != NULL)
754	    (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode);
755#endif
756    }
757
758#if defined(HAS_SWAP_EXISTS_ACTION)
759    /* If "Quit" selected at ATTENTION dialog, don't load the file */
760    if (swap_exists_action == SEA_QUIT)
761    {
762	if (!read_buffer && !read_stdin)
763	    close(fd);
764	return FAIL;
765    }
766#endif
767
768    ++no_wait_return;	    /* don't wait for return yet */
769
770    /*
771     * Set '[ mark to the line above where the lines go (line 1 if zero).
772     */
773    curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
774    curbuf->b_op_start.col = 0;
775
776#ifdef FEAT_AUTOCMD
777    if (!read_buffer)
778    {
779	int	m = msg_scroll;
780	int	n = msg_scrolled;
781
782	/*
783	 * The file must be closed again, the autocommands may want to change
784	 * the file before reading it.
785	 */
786	if (!read_stdin)
787	    close(fd);		/* ignore errors */
788
789	/*
790	 * The output from the autocommands should not overwrite anything and
791	 * should not be overwritten: Set msg_scroll, restore its value if no
792	 * output was done.
793	 */
794	msg_scroll = TRUE;
795	if (filtering)
796	    apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
797							  FALSE, curbuf, eap);
798	else if (read_stdin)
799	    apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
800							  FALSE, curbuf, eap);
801	else if (newfile)
802	    apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
803							  FALSE, curbuf, eap);
804	else
805	    apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
806							    FALSE, NULL, eap);
807	if (msg_scrolled == n)
808	    msg_scroll = m;
809
810#ifdef FEAT_EVAL
811	if (aborting())	    /* autocmds may abort script processing */
812	{
813	    --no_wait_return;
814	    msg_scroll = msg_save;
815	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
816	    return FAIL;
817	}
818#endif
819	/*
820	 * Don't allow the autocommands to change the current buffer.
821	 * Try to re-open the file.
822	 *
823	 * Don't allow the autocommands to change the buffer name either
824	 * (cd for example) if it invalidates fname or sfname.
825	 */
826	if (!read_stdin && (curbuf != old_curbuf
827		|| (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
828		|| (using_b_fname && (old_b_fname != curbuf->b_fname))
829		|| (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
830	{
831	    --no_wait_return;
832	    msg_scroll = msg_save;
833	    if (fd < 0)
834		EMSG(_("E200: *ReadPre autocommands made the file unreadable"));
835	    else
836		EMSG(_("E201: *ReadPre autocommands must not change current buffer"));
837	    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
838	    return FAIL;
839	}
840    }
841#endif /* FEAT_AUTOCMD */
842
843    /* Autocommands may add lines to the file, need to check if it is empty */
844    wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
845
846    if (!recoverymode && !filtering && !(flags & READ_DUMMY))
847    {
848	/*
849	 * Show the user that we are busy reading the input.  Sometimes this
850	 * may take a while.  When reading from stdin another program may
851	 * still be running, don't move the cursor to the last line, unless
852	 * always using the GUI.
853	 */
854	if (read_stdin)
855	{
856#ifndef ALWAYS_USE_GUI
857	    mch_msg(_("Vim: Reading from stdin...\n"));
858#endif
859#ifdef FEAT_GUI
860	    /* Also write a message in the GUI window, if there is one. */
861	    if (gui.in_use && !gui.dying && !gui.starting)
862	    {
863		p = (char_u *)_("Reading from stdin...");
864		gui_write(p, (int)STRLEN(p));
865	    }
866#endif
867	}
868	else if (!read_buffer)
869	    filemess(curbuf, sfname, (char_u *)"", 0);
870    }
871
872    msg_scroll = FALSE;			/* overwrite the file message */
873
874    /*
875     * Set linecnt now, before the "retry" caused by a wrong guess for
876     * fileformat, and after the autocommands, which may change them.
877     */
878    linecnt = curbuf->b_ml.ml_line_count;
879
880#ifdef FEAT_MBYTE
881    /* "++bad=" argument. */
882    if (eap != NULL && eap->bad_char != 0)
883    {
884	bad_char_behavior = eap->bad_char;
885	if (set_options)
886	    curbuf->b_bad_char = eap->bad_char;
887    }
888    else
889	curbuf->b_bad_char = 0;
890
891    /*
892     * Decide which 'encoding' to use or use first.
893     */
894    if (eap != NULL && eap->force_enc != 0)
895    {
896	fenc = enc_canonize(eap->cmd + eap->force_enc);
897	fenc_alloced = TRUE;
898	keep_dest_enc = TRUE;
899    }
900    else if (curbuf->b_p_bin)
901    {
902	fenc = (char_u *)"";		/* binary: don't convert */
903	fenc_alloced = FALSE;
904    }
905    else if (curbuf->b_help)
906    {
907	char_u	    firstline[80];
908	int	    fc;
909
910	/* Help files are either utf-8 or latin1.  Try utf-8 first, if this
911	 * fails it must be latin1.
912	 * Always do this when 'encoding' is "utf-8".  Otherwise only do
913	 * this when needed to avoid [converted] remarks all the time.
914	 * It is needed when the first line contains non-ASCII characters.
915	 * That is only in *.??x files. */
916	fenc = (char_u *)"latin1";
917	c = enc_utf8;
918	if (!c && !read_stdin)
919	{
920	    fc = fname[STRLEN(fname) - 1];
921	    if (TOLOWER_ASC(fc) == 'x')
922	    {
923		/* Read the first line (and a bit more).  Immediately rewind to
924		 * the start of the file.  If the read() fails "len" is -1. */
925		len = vim_read(fd, firstline, 80);
926		lseek(fd, (off_t)0L, SEEK_SET);
927		for (p = firstline; p < firstline + len; ++p)
928		    if (*p >= 0x80)
929		    {
930			c = TRUE;
931			break;
932		    }
933	    }
934	}
935
936	if (c)
937	{
938	    fenc_next = fenc;
939	    fenc = (char_u *)"utf-8";
940
941	    /* When the file is utf-8 but a character doesn't fit in
942	     * 'encoding' don't retry.  In help text editing utf-8 bytes
943	     * doesn't make sense. */
944	    if (!enc_utf8)
945		keep_dest_enc = TRUE;
946	}
947	fenc_alloced = FALSE;
948    }
949    else if (*p_fencs == NUL)
950    {
951	fenc = curbuf->b_p_fenc;	/* use format from buffer */
952	fenc_alloced = FALSE;
953    }
954    else
955    {
956	fenc_next = p_fencs;		/* try items in 'fileencodings' */
957	fenc = next_fenc(&fenc_next);
958	fenc_alloced = TRUE;
959    }
960#endif
961
962    /*
963     * Jump back here to retry reading the file in different ways.
964     * Reasons to retry:
965     * - encoding conversion failed: try another one from "fenc_next"
966     * - BOM detected and fenc was set, need to setup conversion
967     * - "fileformat" check failed: try another
968     *
969     * Variables set for special retry actions:
970     * "file_rewind"	Rewind the file to start reading it again.
971     * "advance_fenc"	Advance "fenc" using "fenc_next".
972     * "skip_read"	Re-use already read bytes (BOM detected).
973     * "did_iconv"	iconv() conversion failed, try 'charconvert'.
974     * "keep_fileformat" Don't reset "fileformat".
975     *
976     * Other status indicators:
977     * "tmpname"	When != NULL did conversion with 'charconvert'.
978     *			Output file has to be deleted afterwards.
979     * "iconv_fd"	When != -1 did conversion with iconv().
980     */
981retry:
982
983    if (file_rewind)
984    {
985	if (read_buffer)
986	{
987	    read_buf_lnum = 1;
988	    read_buf_col = 0;
989	}
990	else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0)
991	{
992	    /* Can't rewind the file, give up. */
993	    error = TRUE;
994	    goto failed;
995	}
996	/* Delete the previously read lines. */
997	while (lnum > from)
998	    ml_delete(lnum--, FALSE);
999	file_rewind = FALSE;
1000#ifdef FEAT_MBYTE
1001	if (set_options)
1002	{
1003	    curbuf->b_p_bomb = FALSE;
1004	    curbuf->b_start_bomb = FALSE;
1005	}
1006	conv_error = 0;
1007#endif
1008    }
1009
1010#ifdef FEAT_CRYPT
1011    if (cryptkey != NULL)
1012	/* Need to reset the state, but keep the key, don't want to ask for it
1013	 * again. */
1014	crypt_pop_state();
1015#endif
1016
1017    /*
1018     * When retrying with another "fenc" and the first time "fileformat"
1019     * will be reset.
1020     */
1021    if (keep_fileformat)
1022	keep_fileformat = FALSE;
1023    else
1024    {
1025	if (eap != NULL && eap->force_ff != 0)
1026	{
1027	    fileformat = get_fileformat_force(curbuf, eap);
1028	    try_unix = try_dos = try_mac = FALSE;
1029	}
1030	else if (curbuf->b_p_bin)
1031	    fileformat = EOL_UNIX;		/* binary: use Unix format */
1032	else if (*p_ffs == NUL)
1033	    fileformat = get_fileformat(curbuf);/* use format from buffer */
1034	else
1035	    fileformat = EOL_UNKNOWN;		/* detect from file */
1036    }
1037
1038#ifdef FEAT_MBYTE
1039# ifdef USE_ICONV
1040    if (iconv_fd != (iconv_t)-1)
1041    {
1042	/* aborted conversion with iconv(), close the descriptor */
1043	iconv_close(iconv_fd);
1044	iconv_fd = (iconv_t)-1;
1045    }
1046# endif
1047
1048    if (advance_fenc)
1049    {
1050	/*
1051	 * Try the next entry in 'fileencodings'.
1052	 */
1053	advance_fenc = FALSE;
1054
1055	if (eap != NULL && eap->force_enc != 0)
1056	{
1057	    /* Conversion given with "++cc=" wasn't possible, read
1058	     * without conversion. */
1059	    notconverted = TRUE;
1060	    conv_error = 0;
1061	    if (fenc_alloced)
1062		vim_free(fenc);
1063	    fenc = (char_u *)"";
1064	    fenc_alloced = FALSE;
1065	}
1066	else
1067	{
1068	    if (fenc_alloced)
1069		vim_free(fenc);
1070	    if (fenc_next != NULL)
1071	    {
1072		fenc = next_fenc(&fenc_next);
1073		fenc_alloced = (fenc_next != NULL);
1074	    }
1075	    else
1076	    {
1077		fenc = (char_u *)"";
1078		fenc_alloced = FALSE;
1079	    }
1080	}
1081	if (tmpname != NULL)
1082	{
1083	    mch_remove(tmpname);		/* delete converted file */
1084	    vim_free(tmpname);
1085	    tmpname = NULL;
1086	}
1087    }
1088
1089    /*
1090     * Conversion may be required when the encoding of the file is different
1091     * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
1092     */
1093    fio_flags = 0;
1094    converted = need_conversion(fenc);
1095    if (converted)
1096    {
1097
1098	/* "ucs-bom" means we need to check the first bytes of the file
1099	 * for a BOM. */
1100	if (STRCMP(fenc, ENC_UCSBOM) == 0)
1101	    fio_flags = FIO_UCSBOM;
1102
1103	/*
1104	 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1105	 * done.  This is handled below after read().  Prepare the
1106	 * fio_flags to avoid having to parse the string each time.
1107	 * Also check for Unicode to Latin1 conversion, because iconv()
1108	 * appears not to handle this correctly.  This works just like
1109	 * conversion to UTF-8 except how the resulting character is put in
1110	 * the buffer.
1111	 */
1112	else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1113	    fio_flags = get_fio_flags(fenc);
1114
1115# ifdef WIN3264
1116	/*
1117	 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1118	 * is handled with MultiByteToWideChar().
1119	 */
1120	if (fio_flags == 0)
1121	    fio_flags = get_win_fio_flags(fenc);
1122# endif
1123
1124# ifdef MACOS_X
1125	/* Conversion from Apple MacRoman to latin1 or UTF-8 */
1126	if (fio_flags == 0)
1127	    fio_flags = get_mac_fio_flags(fenc);
1128# endif
1129
1130# ifdef USE_ICONV
1131	/*
1132	 * Try using iconv() if we can't convert internally.
1133	 */
1134	if (fio_flags == 0
1135#  ifdef FEAT_EVAL
1136		&& !did_iconv
1137#  endif
1138		)
1139	    iconv_fd = (iconv_t)my_iconv_open(
1140				  enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
1141# endif
1142
1143# ifdef FEAT_EVAL
1144	/*
1145	 * Use the 'charconvert' expression when conversion is required
1146	 * and we can't do it internally or with iconv().
1147	 */
1148	if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
1149#  ifdef USE_ICONV
1150						    && iconv_fd == (iconv_t)-1
1151#  endif
1152		)
1153	{
1154#  ifdef USE_ICONV
1155	    did_iconv = FALSE;
1156#  endif
1157	    /* Skip conversion when it's already done (retry for wrong
1158	     * "fileformat"). */
1159	    if (tmpname == NULL)
1160	    {
1161		tmpname = readfile_charconvert(fname, fenc, &fd);
1162		if (tmpname == NULL)
1163		{
1164		    /* Conversion failed.  Try another one. */
1165		    advance_fenc = TRUE;
1166		    if (fd < 0)
1167		    {
1168			/* Re-opening the original file failed! */
1169			EMSG(_("E202: Conversion made file unreadable!"));
1170			error = TRUE;
1171			goto failed;
1172		    }
1173		    goto retry;
1174		}
1175	    }
1176	}
1177	else
1178# endif
1179	{
1180	    if (fio_flags == 0
1181# ifdef USE_ICONV
1182		    && iconv_fd == (iconv_t)-1
1183# endif
1184	       )
1185	    {
1186		/* Conversion wanted but we can't.
1187		 * Try the next conversion in 'fileencodings' */
1188		advance_fenc = TRUE;
1189		goto retry;
1190	    }
1191	}
1192    }
1193
1194    /* Set "can_retry" when it's possible to rewind the file and try with
1195     * another "fenc" value.  It's FALSE when no other "fenc" to try, reading
1196     * stdin or fixed at a specific encoding. */
1197    can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc);
1198#endif
1199
1200    if (!skip_read)
1201    {
1202	linerest = 0;
1203	filesize = 0;
1204	skip_count = lines_to_skip;
1205	read_count = lines_to_read;
1206#ifdef FEAT_MBYTE
1207	conv_restlen = 0;
1208#endif
1209#ifdef FEAT_PERSISTENT_UNDO
1210	read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1211				  && curbuf->b_ffname != NULL
1212				  && curbuf->b_p_udf
1213				  && !filtering
1214				  && !read_stdin
1215				  && !read_buffer);
1216	if (read_undo_file)
1217	    sha256_start(&sha_ctx);
1218#endif
1219    }
1220
1221    while (!error && !got_int)
1222    {
1223	/*
1224	 * We allocate as much space for the file as we can get, plus
1225	 * space for the old line plus room for one terminating NUL.
1226	 * The amount is limited by the fact that read() only can read
1227	 * upto max_unsigned characters (and other things).
1228	 */
1229#if SIZEOF_INT <= 2
1230	if (linerest >= 0x7ff0)
1231	{
1232	    ++split;
1233	    *ptr = NL;		    /* split line by inserting a NL */
1234	    size = 1;
1235	}
1236	else
1237#endif
1238	{
1239	    if (!skip_read)
1240	    {
1241#if SIZEOF_INT > 2
1242# if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
1243		size = SSIZE_MAX;		    /* use max I/O size, 52K */
1244# else
1245		size = 0x10000L;		    /* use buffer >= 64K */
1246# endif
1247#else
1248		size = 0x7ff0L - linerest;	    /* limit buffer to 32K */
1249#endif
1250
1251		for ( ; size >= 10; size = (long)((long_u)size >> 1))
1252		{
1253		    if ((new_buffer = lalloc((long_u)(size + linerest + 1),
1254							      FALSE)) != NULL)
1255			break;
1256		}
1257		if (new_buffer == NULL)
1258		{
1259		    do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1260		    error = TRUE;
1261		    break;
1262		}
1263		if (linerest)	/* copy characters from the previous buffer */
1264		    mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1265		vim_free(buffer);
1266		buffer = new_buffer;
1267		ptr = buffer + linerest;
1268		line_start = buffer;
1269
1270#ifdef FEAT_MBYTE
1271		/* May need room to translate into.
1272		 * For iconv() we don't really know the required space, use a
1273		 * factor ICONV_MULT.
1274		 * latin1 to utf-8: 1 byte becomes up to 2 bytes
1275		 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1276		 * become up to 4 bytes, size must be multiple of 2
1277		 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1278		 * multiple of 2
1279		 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1280		 * multiple of 4 */
1281		real_size = (int)size;
1282# ifdef USE_ICONV
1283		if (iconv_fd != (iconv_t)-1)
1284		    size = size / ICONV_MULT;
1285		else
1286# endif
1287		    if (fio_flags & FIO_LATIN1)
1288		    size = size / 2;
1289		else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1290		    size = (size * 2 / 3) & ~1;
1291		else if (fio_flags & FIO_UCS4)
1292		    size = (size * 2 / 3) & ~3;
1293		else if (fio_flags == FIO_UCSBOM)
1294		    size = size / ICONV_MULT;	/* worst case */
1295# ifdef WIN3264
1296		else if (fio_flags & FIO_CODEPAGE)
1297		    size = size / ICONV_MULT;	/* also worst case */
1298# endif
1299# ifdef MACOS_X
1300		else if (fio_flags & FIO_MACROMAN)
1301		    size = size / ICONV_MULT;	/* also worst case */
1302# endif
1303#endif
1304
1305#ifdef FEAT_MBYTE
1306		if (conv_restlen > 0)
1307		{
1308		    /* Insert unconverted bytes from previous line. */
1309		    mch_memmove(ptr, conv_rest, conv_restlen);
1310		    ptr += conv_restlen;
1311		    size -= conv_restlen;
1312		}
1313#endif
1314
1315		if (read_buffer)
1316		{
1317		    /*
1318		     * Read bytes from curbuf.  Used for converting text read
1319		     * from stdin.
1320		     */
1321		    if (read_buf_lnum > from)
1322			size = 0;
1323		    else
1324		    {
1325			int	n, ni;
1326			long	tlen;
1327
1328			tlen = 0;
1329			for (;;)
1330			{
1331			    p = ml_get(read_buf_lnum) + read_buf_col;
1332			    n = (int)STRLEN(p);
1333			    if ((int)tlen + n + 1 > size)
1334			    {
1335				/* Filled up to "size", append partial line.
1336				 * Change NL to NUL to reverse the effect done
1337				 * below. */
1338				n = (int)(size - tlen);
1339				for (ni = 0; ni < n; ++ni)
1340				{
1341				    if (p[ni] == NL)
1342					ptr[tlen++] = NUL;
1343				    else
1344					ptr[tlen++] = p[ni];
1345				}
1346				read_buf_col += n;
1347				break;
1348			    }
1349			    else
1350			    {
1351				/* Append whole line and new-line.  Change NL
1352				 * to NUL to reverse the effect done below. */
1353				for (ni = 0; ni < n; ++ni)
1354				{
1355				    if (p[ni] == NL)
1356					ptr[tlen++] = NUL;
1357				    else
1358					ptr[tlen++] = p[ni];
1359				}
1360				ptr[tlen++] = NL;
1361				read_buf_col = 0;
1362				if (++read_buf_lnum > from)
1363				{
1364				    /* When the last line didn't have an
1365				     * end-of-line don't add it now either. */
1366				    if (!curbuf->b_p_eol)
1367					--tlen;
1368				    size = tlen;
1369				    break;
1370				}
1371			    }
1372			}
1373		    }
1374		}
1375		else
1376		{
1377		    /*
1378		     * Read bytes from the file.
1379		     */
1380		    size = vim_read(fd, ptr, size);
1381		}
1382
1383		if (size <= 0)
1384		{
1385		    if (size < 0)		    /* read error */
1386			error = TRUE;
1387#ifdef FEAT_MBYTE
1388		    else if (conv_restlen > 0)
1389		    {
1390			/*
1391			 * Reached end-of-file but some trailing bytes could
1392			 * not be converted.  Truncated file?
1393			 */
1394
1395			/* When we did a conversion report an error. */
1396			if (fio_flags != 0
1397# ifdef USE_ICONV
1398				|| iconv_fd != (iconv_t)-1
1399# endif
1400			   )
1401			{
1402			    if (conv_error == 0)
1403				conv_error = curbuf->b_ml.ml_line_count
1404								- linecnt + 1;
1405			}
1406			/* Remember the first linenr with an illegal byte */
1407			else if (illegal_byte == 0)
1408			    illegal_byte = curbuf->b_ml.ml_line_count
1409								- linecnt + 1;
1410			if (bad_char_behavior == BAD_DROP)
1411			{
1412			    *(ptr - conv_restlen) = NUL;
1413			    conv_restlen = 0;
1414			}
1415			else
1416			{
1417			    /* Replace the trailing bytes with the replacement
1418			     * character if we were converting; if we weren't,
1419			     * leave the UTF8 checking code to do it, as it
1420			     * works slightly differently. */
1421			    if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
1422# ifdef USE_ICONV
1423				    || iconv_fd != (iconv_t)-1
1424# endif
1425			       ))
1426			    {
1427				while (conv_restlen > 0)
1428				{
1429				    *(--ptr) = bad_char_behavior;
1430				    --conv_restlen;
1431				}
1432			    }
1433			    fio_flags = 0;	/* don't convert this */
1434# ifdef USE_ICONV
1435			    if (iconv_fd != (iconv_t)-1)
1436			    {
1437				iconv_close(iconv_fd);
1438				iconv_fd = (iconv_t)-1;
1439			    }
1440# endif
1441			}
1442		    }
1443#endif
1444		}
1445
1446#ifdef FEAT_CRYPT
1447		/*
1448		 * At start of file: Check for magic number of encryption.
1449		 */
1450		if (filesize == 0)
1451		    cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
1452					&filesize, newfile, sfname,
1453					&did_ask_for_key);
1454		/*
1455		 * Decrypt the read bytes.
1456		 */
1457		if (cryptkey != NULL && size > 0)
1458		    crypt_decode(ptr, size);
1459#endif
1460	    }
1461	    skip_read = FALSE;
1462
1463#ifdef FEAT_MBYTE
1464	    /*
1465	     * At start of file (or after crypt magic number): Check for BOM.
1466	     * Also check for a BOM for other Unicode encodings, but not after
1467	     * converting with 'charconvert' or when a BOM has already been
1468	     * found.
1469	     */
1470	    if ((filesize == 0
1471# ifdef FEAT_CRYPT
1472		   || (filesize == (CRYPT_MAGIC_LEN
1473					   + crypt_salt_len[use_crypt_method]
1474					   + crypt_seed_len[use_crypt_method])
1475							  && cryptkey != NULL)
1476# endif
1477		       )
1478		    && (fio_flags == FIO_UCSBOM
1479			|| (!curbuf->b_p_bomb
1480			    && tmpname == NULL
1481			    && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1482	    {
1483		char_u	*ccname;
1484		int	blen;
1485
1486		/* no BOM detection in a short file or in binary mode */
1487		if (size < 2 || curbuf->b_p_bin)
1488		    ccname = NULL;
1489		else
1490		    ccname = check_for_bom(ptr, size, &blen,
1491		      fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1492		if (ccname != NULL)
1493		{
1494		    /* Remove BOM from the text */
1495		    filesize += blen;
1496		    size -= blen;
1497		    mch_memmove(ptr, ptr + blen, (size_t)size);
1498		    if (set_options)
1499		    {
1500			curbuf->b_p_bomb = TRUE;
1501			curbuf->b_start_bomb = TRUE;
1502		    }
1503		}
1504
1505		if (fio_flags == FIO_UCSBOM)
1506		{
1507		    if (ccname == NULL)
1508		    {
1509			/* No BOM detected: retry with next encoding. */
1510			advance_fenc = TRUE;
1511		    }
1512		    else
1513		    {
1514			/* BOM detected: set "fenc" and jump back */
1515			if (fenc_alloced)
1516			    vim_free(fenc);
1517			fenc = ccname;
1518			fenc_alloced = FALSE;
1519		    }
1520		    /* retry reading without getting new bytes or rewinding */
1521		    skip_read = TRUE;
1522		    goto retry;
1523		}
1524	    }
1525
1526	    /* Include not converted bytes. */
1527	    ptr -= conv_restlen;
1528	    size += conv_restlen;
1529	    conv_restlen = 0;
1530#endif
1531	    /*
1532	     * Break here for a read error or end-of-file.
1533	     */
1534	    if (size <= 0)
1535		break;
1536
1537#ifdef FEAT_MBYTE
1538
1539# ifdef USE_ICONV
1540	    if (iconv_fd != (iconv_t)-1)
1541	    {
1542		/*
1543		 * Attempt conversion of the read bytes to 'encoding' using
1544		 * iconv().
1545		 */
1546		const char	*fromp;
1547		char		*top;
1548		size_t		from_size;
1549		size_t		to_size;
1550
1551		fromp = (char *)ptr;
1552		from_size = size;
1553		ptr += size;
1554		top = (char *)ptr;
1555		to_size = real_size - size;
1556
1557		/*
1558		 * If there is conversion error or not enough room try using
1559		 * another conversion.  Except for when there is no
1560		 * alternative (help files).
1561		 */
1562		while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1563							       &top, &to_size)
1564			    == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1565						  || from_size > CONV_RESTLEN)
1566		{
1567		    if (can_retry)
1568			goto rewind_retry;
1569		    if (conv_error == 0)
1570			conv_error = readfile_linenr(linecnt,
1571							  ptr, (char_u *)top);
1572
1573		    /* Deal with a bad byte and continue with the next. */
1574		    ++fromp;
1575		    --from_size;
1576		    if (bad_char_behavior == BAD_KEEP)
1577		    {
1578			*top++ = *(fromp - 1);
1579			--to_size;
1580		    }
1581		    else if (bad_char_behavior != BAD_DROP)
1582		    {
1583			*top++ = bad_char_behavior;
1584			--to_size;
1585		    }
1586		}
1587
1588		if (from_size > 0)
1589		{
1590		    /* Some remaining characters, keep them for the next
1591		     * round. */
1592		    mch_memmove(conv_rest, (char_u *)fromp, from_size);
1593		    conv_restlen = (int)from_size;
1594		}
1595
1596		/* move the linerest to before the converted characters */
1597		line_start = ptr - linerest;
1598		mch_memmove(line_start, buffer, (size_t)linerest);
1599		size = (long)((char_u *)top - ptr);
1600	    }
1601# endif
1602
1603# ifdef WIN3264
1604	    if (fio_flags & FIO_CODEPAGE)
1605	    {
1606		char_u	*src, *dst;
1607		WCHAR	ucs2buf[3];
1608		int	ucs2len;
1609		int	codepage = FIO_GET_CP(fio_flags);
1610		int	bytelen;
1611		int	found_bad;
1612		char	replstr[2];
1613
1614		/*
1615		 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
1616		 * a codepage, using standard MS-Windows functions.  This
1617		 * requires two steps:
1618		 * 1. convert from 'fileencoding' to ucs-2
1619		 * 2. convert from ucs-2 to 'encoding'
1620		 *
1621		 * Because there may be illegal bytes AND an incomplete byte
1622		 * sequence at the end, we may have to do the conversion one
1623		 * character at a time to get it right.
1624		 */
1625
1626		/* Replacement string for WideCharToMultiByte(). */
1627		if (bad_char_behavior > 0)
1628		    replstr[0] = bad_char_behavior;
1629		else
1630		    replstr[0] = '?';
1631		replstr[1] = NUL;
1632
1633		/*
1634		 * Move the bytes to the end of the buffer, so that we have
1635		 * room to put the result at the start.
1636		 */
1637		src = ptr + real_size - size;
1638		mch_memmove(src, ptr, size);
1639
1640		/*
1641		 * Do the conversion.
1642		 */
1643		dst = ptr;
1644		size = size;
1645		while (size > 0)
1646		{
1647		    found_bad = FALSE;
1648
1649#  ifdef CP_UTF8	/* VC 4.1 doesn't define CP_UTF8 */
1650		    if (codepage == CP_UTF8)
1651		    {
1652			/* Handle CP_UTF8 input ourselves to be able to handle
1653			 * trailing bytes properly.
1654			 * Get one UTF-8 character from src. */
1655			bytelen = (int)utf_ptr2len_len(src, size);
1656			if (bytelen > size)
1657			{
1658			    /* Only got some bytes of a character.  Normally
1659			     * it's put in "conv_rest", but if it's too long
1660			     * deal with it as if they were illegal bytes. */
1661			    if (bytelen <= CONV_RESTLEN)
1662				break;
1663
1664			    /* weird overlong byte sequence */
1665			    bytelen = size;
1666			    found_bad = TRUE;
1667			}
1668			else
1669			{
1670			    int	    u8c = utf_ptr2char(src);
1671
1672			    if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
1673				found_bad = TRUE;
1674			    ucs2buf[0] = u8c;
1675			    ucs2len = 1;
1676			}
1677		    }
1678		    else
1679#  endif
1680		    {
1681			/* We don't know how long the byte sequence is, try
1682			 * from one to three bytes. */
1683			for (bytelen = 1; bytelen <= size && bytelen <= 3;
1684								    ++bytelen)
1685			{
1686			    ucs2len = MultiByteToWideChar(codepage,
1687							 MB_ERR_INVALID_CHARS,
1688							 (LPCSTR)src, bytelen,
1689								   ucs2buf, 3);
1690			    if (ucs2len > 0)
1691				break;
1692			}
1693			if (ucs2len == 0)
1694			{
1695			    /* If we have only one byte then it's probably an
1696			     * incomplete byte sequence.  Otherwise discard
1697			     * one byte as a bad character. */
1698			    if (size == 1)
1699				break;
1700			    found_bad = TRUE;
1701			    bytelen = 1;
1702			}
1703		    }
1704
1705		    if (!found_bad)
1706		    {
1707			int	i;
1708
1709			/* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */
1710			if (enc_utf8)
1711			{
1712			    /* From UCS-2 to UTF-8.  Cannot fail. */
1713			    for (i = 0; i < ucs2len; ++i)
1714				dst += utf_char2bytes(ucs2buf[i], dst);
1715			}
1716			else
1717			{
1718			    BOOL	bad = FALSE;
1719			    int		dstlen;
1720
1721			    /* From UCS-2 to "enc_codepage".  If the
1722			     * conversion uses the default character "?",
1723			     * the data doesn't fit in this encoding. */
1724			    dstlen = WideCharToMultiByte(enc_codepage, 0,
1725				    (LPCWSTR)ucs2buf, ucs2len,
1726				    (LPSTR)dst, (int)(src - dst),
1727				    replstr, &bad);
1728			    if (bad)
1729				found_bad = TRUE;
1730			    else
1731				dst += dstlen;
1732			}
1733		    }
1734
1735		    if (found_bad)
1736		    {
1737			/* Deal with bytes we can't convert. */
1738			if (can_retry)
1739			    goto rewind_retry;
1740			if (conv_error == 0)
1741			    conv_error = readfile_linenr(linecnt, ptr, dst);
1742			if (bad_char_behavior != BAD_DROP)
1743			{
1744			    if (bad_char_behavior == BAD_KEEP)
1745			    {
1746				mch_memmove(dst, src, bytelen);
1747				dst += bytelen;
1748			    }
1749			    else
1750				*dst++ = bad_char_behavior;
1751			}
1752		    }
1753
1754		    src += bytelen;
1755		    size -= bytelen;
1756		}
1757
1758		if (size > 0)
1759		{
1760		    /* An incomplete byte sequence remaining. */
1761		    mch_memmove(conv_rest, src, size);
1762		    conv_restlen = size;
1763		}
1764
1765		/* The new size is equal to how much "dst" was advanced. */
1766		size = (long)(dst - ptr);
1767	    }
1768	    else
1769# endif
1770# ifdef MACOS_CONVERT
1771	    if (fio_flags & FIO_MACROMAN)
1772	    {
1773		/*
1774		 * Conversion from Apple MacRoman char encoding to UTF-8 or
1775		 * latin1.  This is in os_mac_conv.c.
1776		 */
1777		if (macroman2enc(ptr, &size, real_size) == FAIL)
1778		    goto rewind_retry;
1779	    }
1780	    else
1781# endif
1782	    if (fio_flags != 0)
1783	    {
1784		int	u8c;
1785		char_u	*dest;
1786		char_u	*tail = NULL;
1787
1788		/*
1789		 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1790		 * "enc_utf8" not set: Convert Unicode to Latin1.
1791		 * Go from end to start through the buffer, because the number
1792		 * of bytes may increase.
1793		 * "dest" points to after where the UTF-8 bytes go, "p" points
1794		 * to after the next character to convert.
1795		 */
1796		dest = ptr + real_size;
1797		if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1798		{
1799		    p = ptr + size;
1800		    if (fio_flags == FIO_UTF8)
1801		    {
1802			/* Check for a trailing incomplete UTF-8 sequence */
1803			tail = ptr + size - 1;
1804			while (tail > ptr && (*tail & 0xc0) == 0x80)
1805			    --tail;
1806			if (tail + utf_byte2len(*tail) <= ptr + size)
1807			    tail = NULL;
1808			else
1809			    p = tail;
1810		    }
1811		}
1812		else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1813		{
1814		    /* Check for a trailing byte */
1815		    p = ptr + (size & ~1);
1816		    if (size & 1)
1817			tail = p;
1818		    if ((fio_flags & FIO_UTF16) && p > ptr)
1819		    {
1820			/* Check for a trailing leading word */
1821			if (fio_flags & FIO_ENDIAN_L)
1822			{
1823			    u8c = (*--p << 8);
1824			    u8c += *--p;
1825			}
1826			else
1827			{
1828			    u8c = *--p;
1829			    u8c += (*--p << 8);
1830			}
1831			if (u8c >= 0xd800 && u8c <= 0xdbff)
1832			    tail = p;
1833			else
1834			    p += 2;
1835		    }
1836		}
1837		else /*  FIO_UCS4 */
1838		{
1839		    /* Check for trailing 1, 2 or 3 bytes */
1840		    p = ptr + (size & ~3);
1841		    if (size & 3)
1842			tail = p;
1843		}
1844
1845		/* If there is a trailing incomplete sequence move it to
1846		 * conv_rest[]. */
1847		if (tail != NULL)
1848		{
1849		    conv_restlen = (int)((ptr + size) - tail);
1850		    mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1851		    size -= conv_restlen;
1852		}
1853
1854
1855		while (p > ptr)
1856		{
1857		    if (fio_flags & FIO_LATIN1)
1858			u8c = *--p;
1859		    else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1860		    {
1861			if (fio_flags & FIO_ENDIAN_L)
1862			{
1863			    u8c = (*--p << 8);
1864			    u8c += *--p;
1865			}
1866			else
1867			{
1868			    u8c = *--p;
1869			    u8c += (*--p << 8);
1870			}
1871			if ((fio_flags & FIO_UTF16)
1872					    && u8c >= 0xdc00 && u8c <= 0xdfff)
1873			{
1874			    int u16c;
1875
1876			    if (p == ptr)
1877			    {
1878				/* Missing leading word. */
1879				if (can_retry)
1880				    goto rewind_retry;
1881				if (conv_error == 0)
1882				    conv_error = readfile_linenr(linecnt,
1883								      ptr, p);
1884				if (bad_char_behavior == BAD_DROP)
1885				    continue;
1886				if (bad_char_behavior != BAD_KEEP)
1887				    u8c = bad_char_behavior;
1888			    }
1889
1890			    /* found second word of double-word, get the first
1891			     * word and compute the resulting character */
1892			    if (fio_flags & FIO_ENDIAN_L)
1893			    {
1894				u16c = (*--p << 8);
1895				u16c += *--p;
1896			    }
1897			    else
1898			    {
1899				u16c = *--p;
1900				u16c += (*--p << 8);
1901			    }
1902			    u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1903							      + (u8c & 0x3ff);
1904
1905			    /* Check if the word is indeed a leading word. */
1906			    if (u16c < 0xd800 || u16c > 0xdbff)
1907			    {
1908				if (can_retry)
1909				    goto rewind_retry;
1910				if (conv_error == 0)
1911				    conv_error = readfile_linenr(linecnt,
1912								      ptr, p);
1913				if (bad_char_behavior == BAD_DROP)
1914				    continue;
1915				if (bad_char_behavior != BAD_KEEP)
1916				    u8c = bad_char_behavior;
1917			    }
1918			}
1919		    }
1920		    else if (fio_flags & FIO_UCS4)
1921		    {
1922			if (fio_flags & FIO_ENDIAN_L)
1923			{
1924			    u8c = (*--p << 24);
1925			    u8c += (*--p << 16);
1926			    u8c += (*--p << 8);
1927			    u8c += *--p;
1928			}
1929			else	/* big endian */
1930			{
1931			    u8c = *--p;
1932			    u8c += (*--p << 8);
1933			    u8c += (*--p << 16);
1934			    u8c += (*--p << 24);
1935			}
1936		    }
1937		    else    /* UTF-8 */
1938		    {
1939			if (*--p < 0x80)
1940			    u8c = *p;
1941			else
1942			{
1943			    len = utf_head_off(ptr, p);
1944			    p -= len;
1945			    u8c = utf_ptr2char(p);
1946			    if (len == 0)
1947			    {
1948				/* Not a valid UTF-8 character, retry with
1949				 * another fenc when possible, otherwise just
1950				 * report the error. */
1951				if (can_retry)
1952				    goto rewind_retry;
1953				if (conv_error == 0)
1954				    conv_error = readfile_linenr(linecnt,
1955								      ptr, p);
1956				if (bad_char_behavior == BAD_DROP)
1957				    continue;
1958				if (bad_char_behavior != BAD_KEEP)
1959				    u8c = bad_char_behavior;
1960			    }
1961			}
1962		    }
1963		    if (enc_utf8)	/* produce UTF-8 */
1964		    {
1965			dest -= utf_char2len(u8c);
1966			(void)utf_char2bytes(u8c, dest);
1967		    }
1968		    else		/* produce Latin1 */
1969		    {
1970			--dest;
1971			if (u8c >= 0x100)
1972			{
1973			    /* character doesn't fit in latin1, retry with
1974			     * another fenc when possible, otherwise just
1975			     * report the error. */
1976			    if (can_retry)
1977				goto rewind_retry;
1978			    if (conv_error == 0)
1979				conv_error = readfile_linenr(linecnt, ptr, p);
1980			    if (bad_char_behavior == BAD_DROP)
1981				++dest;
1982			    else if (bad_char_behavior == BAD_KEEP)
1983				*dest = u8c;
1984			    else if (eap != NULL && eap->bad_char != 0)
1985				*dest = bad_char_behavior;
1986			    else
1987				*dest = 0xBF;
1988			}
1989			else
1990			    *dest = u8c;
1991		    }
1992		}
1993
1994		/* move the linerest to before the converted characters */
1995		line_start = dest - linerest;
1996		mch_memmove(line_start, buffer, (size_t)linerest);
1997		size = (long)((ptr + real_size) - dest);
1998		ptr = dest;
1999	    }
2000	    else if (enc_utf8 && !curbuf->b_p_bin)
2001	    {
2002		int  incomplete_tail = FALSE;
2003
2004		/* Reading UTF-8: Check if the bytes are valid UTF-8. */
2005		for (p = ptr; ; ++p)
2006		{
2007		    int	 todo = (int)((ptr + size) - p);
2008		    int	 l;
2009
2010		    if (todo <= 0)
2011			break;
2012		    if (*p >= 0x80)
2013		    {
2014			/* A length of 1 means it's an illegal byte.  Accept
2015			 * an incomplete character at the end though, the next
2016			 * read() will get the next bytes, we'll check it
2017			 * then. */
2018			l = utf_ptr2len_len(p, todo);
2019			if (l > todo && !incomplete_tail)
2020			{
2021			    /* Avoid retrying with a different encoding when
2022			     * a truncated file is more likely, or attempting
2023			     * to read the rest of an incomplete sequence when
2024			     * we have already done so. */
2025			    if (p > ptr || filesize > 0)
2026				incomplete_tail = TRUE;
2027			    /* Incomplete byte sequence, move it to conv_rest[]
2028			     * and try to read the rest of it, unless we've
2029			     * already done so. */
2030			    if (p > ptr)
2031			    {
2032				conv_restlen = todo;
2033				mch_memmove(conv_rest, p, conv_restlen);
2034				size -= conv_restlen;
2035				break;
2036			    }
2037			}
2038			if (l == 1 || l > todo)
2039			{
2040			    /* Illegal byte.  If we can try another encoding
2041			     * do that, unless at EOF where a truncated
2042			     * file is more likely than a conversion error. */
2043			    if (can_retry && !incomplete_tail)
2044				break;
2045# ifdef USE_ICONV
2046			    /* When we did a conversion report an error. */
2047			    if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2048				conv_error = readfile_linenr(linecnt, ptr, p);
2049# endif
2050			    /* Remember the first linenr with an illegal byte */
2051			    if (conv_error == 0 && illegal_byte == 0)
2052				illegal_byte = readfile_linenr(linecnt, ptr, p);
2053
2054			    /* Drop, keep or replace the bad byte. */
2055			    if (bad_char_behavior == BAD_DROP)
2056			    {
2057				mch_memmove(p, p + 1, todo - 1);
2058				--p;
2059				--size;
2060			    }
2061			    else if (bad_char_behavior != BAD_KEEP)
2062				*p = bad_char_behavior;
2063			}
2064			else
2065			    p += l - 1;
2066		    }
2067		}
2068		if (p < ptr + size && !incomplete_tail)
2069		{
2070		    /* Detected a UTF-8 error. */
2071rewind_retry:
2072		    /* Retry reading with another conversion. */
2073# if defined(FEAT_EVAL) && defined(USE_ICONV)
2074		    if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
2075			/* iconv() failed, try 'charconvert' */
2076			did_iconv = TRUE;
2077		    else
2078# endif
2079			/* use next item from 'fileencodings' */
2080			advance_fenc = TRUE;
2081		    file_rewind = TRUE;
2082		    goto retry;
2083		}
2084	    }
2085#endif
2086
2087	    /* count the number of characters (after conversion!) */
2088	    filesize += size;
2089
2090	    /*
2091	     * when reading the first part of a file: guess EOL type
2092	     */
2093	    if (fileformat == EOL_UNKNOWN)
2094	    {
2095		/* First try finding a NL, for Dos and Unix */
2096		if (try_dos || try_unix)
2097		{
2098		    for (p = ptr; p < ptr + size; ++p)
2099		    {
2100			if (*p == NL)
2101			{
2102			    if (!try_unix
2103				    || (try_dos && p > ptr && p[-1] == CAR))
2104				fileformat = EOL_DOS;
2105			    else
2106				fileformat = EOL_UNIX;
2107			    break;
2108			}
2109		    }
2110
2111		    /* Don't give in to EOL_UNIX if EOL_MAC is more likely */
2112		    if (fileformat == EOL_UNIX && try_mac)
2113		    {
2114			/* Need to reset the counters when retrying fenc. */
2115			try_mac = 1;
2116			try_unix = 1;
2117			for (; p >= ptr && *p != CAR; p--)
2118			    ;
2119			if (p >= ptr)
2120			{
2121			    for (p = ptr; p < ptr + size; ++p)
2122			    {
2123				if (*p == NL)
2124				    try_unix++;
2125				else if (*p == CAR)
2126				    try_mac++;
2127			    }
2128			    if (try_mac > try_unix)
2129				fileformat = EOL_MAC;
2130			}
2131		    }
2132		}
2133
2134		/* No NL found: may use Mac format */
2135		if (fileformat == EOL_UNKNOWN && try_mac)
2136		    fileformat = EOL_MAC;
2137
2138		/* Still nothing found?  Use first format in 'ffs' */
2139		if (fileformat == EOL_UNKNOWN)
2140		    fileformat = default_fileformat();
2141
2142		/* if editing a new file: may set p_tx and p_ff */
2143		if (set_options)
2144		    set_fileformat(fileformat, OPT_LOCAL);
2145	    }
2146	}
2147
2148	if (p_bf) {
2149		/* beautify throws away all non-printable characters on input
2150		   except tab, newline, and form-feed	*/
2151		int i=0;
2152		for (p = ptr; p < ptr + size; ++p) {
2153		    if (isprint(*p) || *p == '\t' || *p == '\n' || *p == '\f')
2154			ptr[i++] = *p;
2155		}
2156		size=i;
2157	}
2158
2159	/*
2160	 * This loop is executed once for every character read.
2161	 * Keep it fast!
2162	 */
2163	if (fileformat == EOL_MAC)
2164	{
2165	    --ptr;
2166	    while (++ptr, --size >= 0)
2167	    {
2168		/* catch most common case first */
2169		if ((c = *ptr) != NUL && c != CAR && c != NL)
2170		    continue;
2171		if (c == NUL)
2172		    *ptr = NL;	/* NULs are replaced by newlines! */
2173		else if (c == NL)
2174		    *ptr = CAR;	/* NLs are replaced by CRs! */
2175		else
2176		{
2177		    if (skip_count == 0)
2178		    {
2179			*ptr = NUL;	    /* end of line */
2180			len = (colnr_T) (ptr - line_start + 1);
2181			if (ml_append(lnum, line_start, len, newfile) == FAIL)
2182			{
2183			    error = TRUE;
2184			    break;
2185			}
2186#ifdef FEAT_PERSISTENT_UNDO
2187			if (read_undo_file)
2188			    sha256_update(&sha_ctx, line_start, len);
2189#endif
2190			++lnum;
2191			if (--read_count == 0)
2192			{
2193			    error = TRUE;	/* break loop */
2194			    line_start = ptr;	/* nothing left to write */
2195			    break;
2196			}
2197		    }
2198		    else
2199			--skip_count;
2200		    line_start = ptr + 1;
2201		}
2202	    }
2203	}
2204	else
2205	{
2206	    --ptr;
2207	    while (++ptr, --size >= 0)
2208	    {
2209		if ((c = *ptr) != NUL && c != NL)  /* catch most common case */
2210		    continue;
2211		if (c == NUL)
2212		    *ptr = NL;	/* NULs are replaced by newlines! */
2213		else
2214		{
2215		    if (skip_count == 0)
2216		    {
2217			*ptr = NUL;		/* end of line */
2218			len = (colnr_T)(ptr - line_start + 1);
2219			if (fileformat == EOL_DOS)
2220			{
2221			    if (ptr[-1] == CAR)	/* remove CR */
2222			    {
2223				ptr[-1] = NUL;
2224				--len;
2225			    }
2226			    /*
2227			     * Reading in Dos format, but no CR-LF found!
2228			     * When 'fileformats' includes "unix", delete all
2229			     * the lines read so far and start all over again.
2230			     * Otherwise give an error message later.
2231			     */
2232			    else if (ff_error != EOL_DOS)
2233			    {
2234				if (   try_unix
2235				    && !read_stdin
2236				    && (read_buffer
2237					|| lseek(fd, (off_t)0L, SEEK_SET) == 0))
2238				{
2239				    fileformat = EOL_UNIX;
2240				    if (set_options)
2241					set_fileformat(EOL_UNIX, OPT_LOCAL);
2242				    file_rewind = TRUE;
2243				    keep_fileformat = TRUE;
2244				    goto retry;
2245				}
2246				ff_error = EOL_DOS;
2247			    }
2248			}
2249			if (ml_append(lnum, line_start, len, newfile) == FAIL)
2250			{
2251			    error = TRUE;
2252			    break;
2253			}
2254#ifdef FEAT_PERSISTENT_UNDO
2255			if (read_undo_file)
2256			    sha256_update(&sha_ctx, line_start, len);
2257#endif
2258			++lnum;
2259			if (--read_count == 0)
2260			{
2261			    error = TRUE;	    /* break loop */
2262			    line_start = ptr;	/* nothing left to write */
2263			    break;
2264			}
2265		    }
2266		    else
2267			--skip_count;
2268		    line_start = ptr + 1;
2269		}
2270	    }
2271	}
2272	linerest = (long)(ptr - line_start);
2273	ui_breakcheck();
2274    }
2275
2276failed:
2277    /* not an error, max. number of lines reached */
2278    if (error && read_count == 0)
2279	error = FALSE;
2280
2281    /*
2282     * If we get EOF in the middle of a line, note the fact and
2283     * complete the line ourselves.
2284     * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2285     */
2286    if (!error
2287	    && !got_int
2288	    && linerest != 0
2289	    && !(!curbuf->b_p_bin
2290		&& fileformat == EOL_DOS
2291		&& *line_start == Ctrl_Z
2292		&& ptr == line_start + 1))
2293    {
2294	/* remember for when writing */
2295	if (set_options)
2296	    curbuf->b_p_eol = FALSE;
2297	*ptr = NUL;
2298	len = (colnr_T)(ptr - line_start + 1);
2299	if (ml_append(lnum, line_start, len, newfile) == FAIL)
2300	    error = TRUE;
2301	else
2302	{
2303#ifdef FEAT_PERSISTENT_UNDO
2304	    if (read_undo_file)
2305		sha256_update(&sha_ctx, line_start, len);
2306#endif
2307	    read_no_eol_lnum = ++lnum;
2308	}
2309    }
2310
2311    if (set_options)
2312	save_file_ff(curbuf);		/* remember the current file format */
2313
2314#ifdef FEAT_CRYPT
2315    if (cryptkey != NULL)
2316    {
2317	crypt_pop_state();
2318	if (cryptkey != curbuf->b_p_key)
2319	    free_crypt_key(cryptkey);
2320	/* don't set cryptkey to NULL, it's used below as a flag that
2321	 * encryption was used */
2322    }
2323#endif
2324
2325#ifdef FEAT_MBYTE
2326    /* If editing a new file: set 'fenc' for the current buffer.
2327     * Also for ":read ++edit file". */
2328    if (set_options)
2329	set_string_option_direct((char_u *)"fenc", -1, fenc,
2330						       OPT_FREE|OPT_LOCAL, 0);
2331    if (fenc_alloced)
2332	vim_free(fenc);
2333# ifdef USE_ICONV
2334    if (iconv_fd != (iconv_t)-1)
2335    {
2336	iconv_close(iconv_fd);
2337	iconv_fd = (iconv_t)-1;
2338    }
2339# endif
2340#endif
2341
2342    if (!read_buffer && !read_stdin)
2343	close(fd);				/* errors are ignored */
2344#ifdef HAVE_FD_CLOEXEC
2345    else
2346    {
2347	int fdflags = fcntl(fd, F_GETFD);
2348	if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
2349	    fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
2350    }
2351#endif
2352    vim_free(buffer);
2353
2354#ifdef HAVE_DUP
2355    if (read_stdin)
2356    {
2357	/* Use stderr for stdin, makes shell commands work. */
2358	close(0);
2359	ignored = dup(2);
2360    }
2361#endif
2362
2363#ifdef FEAT_MBYTE
2364    if (tmpname != NULL)
2365    {
2366	mch_remove(tmpname);		/* delete converted file */
2367	vim_free(tmpname);
2368    }
2369#endif
2370    --no_wait_return;			/* may wait for return now */
2371
2372    /*
2373     * In recovery mode everything but autocommands is skipped.
2374     */
2375    if (!recoverymode)
2376    {
2377	/* need to delete the last line, which comes from the empty buffer */
2378	if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2379	{
2380#ifdef FEAT_NETBEANS_INTG
2381	    netbeansFireChanges = 0;
2382#endif
2383	    ml_delete(curbuf->b_ml.ml_line_count, FALSE);
2384#ifdef FEAT_NETBEANS_INTG
2385	    netbeansFireChanges = 1;
2386#endif
2387	    --linecnt;
2388	}
2389	linecnt = curbuf->b_ml.ml_line_count - linecnt;
2390	if (filesize == 0)
2391	    linecnt = 0;
2392	if (newfile || read_buffer)
2393	{
2394	    redraw_curbuf_later(NOT_VALID);
2395#ifdef FEAT_DIFF
2396	    /* After reading the text into the buffer the diff info needs to
2397	     * be updated. */
2398	    diff_invalidate(curbuf);
2399#endif
2400#ifdef FEAT_FOLDING
2401	    /* All folds in the window are invalid now.  Mark them for update
2402	     * before triggering autocommands. */
2403	    foldUpdateAll(curwin);
2404#endif
2405	}
2406	else if (linecnt)		/* appended at least one line */
2407	    appended_lines_mark(from, linecnt);
2408
2409#ifndef ALWAYS_USE_GUI
2410	/*
2411	 * If we were reading from the same terminal as where messages go,
2412	 * the screen will have been messed up.
2413	 * Switch on raw mode now and clear the screen.
2414	 */
2415	if (read_stdin)
2416	{
2417	    settmode(TMODE_RAW);	/* set to raw mode */
2418	    starttermcap();
2419	    screenclear();
2420	}
2421#endif
2422
2423	if (got_int)
2424	{
2425	    if (!(flags & READ_DUMMY))
2426	    {
2427		filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2428		if (newfile)
2429		    curbuf->b_p_ro = TRUE;	/* must use "w!" now */
2430	    }
2431	    msg_scroll = msg_save;
2432#ifdef FEAT_VIMINFO
2433	    check_marks_read();
2434#endif
2435	    return OK;		/* an interrupt isn't really an error */
2436	}
2437
2438	if (!filtering && !(flags & READ_DUMMY))
2439	{
2440	    msg_add_fname(curbuf, sfname);   /* fname in IObuff with quotes */
2441	    c = FALSE;
2442
2443#ifdef UNIX
2444# ifdef S_ISFIFO
2445	    if (S_ISFIFO(perm))			    /* fifo or socket */
2446	    {
2447		STRCAT(IObuff, _("[fifo/socket]"));
2448		c = TRUE;
2449	    }
2450# else
2451#  ifdef S_IFIFO
2452	    if ((perm & S_IFMT) == S_IFIFO)	    /* fifo */
2453	    {
2454		STRCAT(IObuff, _("[fifo]"));
2455		c = TRUE;
2456	    }
2457#  endif
2458#  ifdef S_IFSOCK
2459	    if ((perm & S_IFMT) == S_IFSOCK)	    /* or socket */
2460	    {
2461		STRCAT(IObuff, _("[socket]"));
2462		c = TRUE;
2463	    }
2464#  endif
2465# endif
2466# ifdef OPEN_CHR_FILES
2467	    if (S_ISCHR(perm))			    /* or character special */
2468	    {
2469		STRCAT(IObuff, _("[character special]"));
2470		c = TRUE;
2471	    }
2472# endif
2473#endif
2474	    if (curbuf->b_p_ro)
2475	    {
2476		STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2477		c = TRUE;
2478	    }
2479	    if (read_no_eol_lnum)
2480	    {
2481		msg_add_eol();
2482		c = TRUE;
2483	    }
2484	    if (ff_error == EOL_DOS)
2485	    {
2486		STRCAT(IObuff, _("[CR missing]"));
2487		c = TRUE;
2488	    }
2489	    if (split)
2490	    {
2491		STRCAT(IObuff, _("[long lines split]"));
2492		c = TRUE;
2493	    }
2494#ifdef FEAT_MBYTE
2495	    if (notconverted)
2496	    {
2497		STRCAT(IObuff, _("[NOT converted]"));
2498		c = TRUE;
2499	    }
2500	    else if (converted)
2501	    {
2502		STRCAT(IObuff, _("[converted]"));
2503		c = TRUE;
2504	    }
2505#endif
2506#ifdef FEAT_CRYPT
2507	    if (cryptkey != NULL)
2508	    {
2509		STRCAT(IObuff, _("[crypted]"));
2510		c = TRUE;
2511	    }
2512#endif
2513#ifdef FEAT_MBYTE
2514	    if (conv_error != 0)
2515	    {
2516		sprintf((char *)IObuff + STRLEN(IObuff),
2517		       _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
2518		c = TRUE;
2519	    }
2520	    else if (illegal_byte > 0)
2521	    {
2522		sprintf((char *)IObuff + STRLEN(IObuff),
2523			 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2524		c = TRUE;
2525	    }
2526	    else
2527#endif
2528		if (error)
2529	    {
2530		STRCAT(IObuff, _("[READ ERRORS]"));
2531		c = TRUE;
2532	    }
2533	    if (msg_add_fileformat(fileformat))
2534		c = TRUE;
2535#ifdef FEAT_CRYPT
2536	    if (cryptkey != NULL)
2537		msg_add_lines(c, (long)linecnt, filesize
2538			- CRYPT_MAGIC_LEN
2539			- crypt_salt_len[use_crypt_method]
2540			- crypt_seed_len[use_crypt_method]);
2541	    else
2542#endif
2543		msg_add_lines(c, (long)linecnt, filesize);
2544
2545	    vim_free(keep_msg);
2546	    keep_msg = NULL;
2547	    msg_scrolled_ign = TRUE;
2548#ifdef ALWAYS_USE_GUI
2549	    /* Don't show the message when reading stdin, it would end up in a
2550	     * message box (which might be shown when exiting!) */
2551	    if (read_stdin || read_buffer)
2552		p = msg_may_trunc(FALSE, IObuff);
2553	    else
2554#endif
2555		p = msg_trunc_attr(IObuff, FALSE, 0);
2556	    if (read_stdin || read_buffer || restart_edit != 0
2557		    || (msg_scrolled != 0 && !need_wait_return))
2558		/* Need to repeat the message after redrawing when:
2559		 * - When reading from stdin (the screen will be cleared next).
2560		 * - When restart_edit is set (otherwise there will be a delay
2561		 *   before redrawing).
2562		 * - When the screen was scrolled but there is no wait-return
2563		 *   prompt. */
2564		set_keep_msg(p, 0);
2565	    msg_scrolled_ign = FALSE;
2566	}
2567
2568	/* with errors writing the file requires ":w!" */
2569	if (newfile && (error
2570#ifdef FEAT_MBYTE
2571		    || conv_error != 0
2572		    || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)
2573#endif
2574		    ))
2575	    curbuf->b_p_ro = TRUE;
2576
2577	u_clearline();	    /* cannot use "U" command after adding lines */
2578
2579	/*
2580	 * In Ex mode: cursor at last new line.
2581	 * Otherwise: cursor at first new line.
2582	 */
2583	if (exmode_active)
2584	    curwin->w_cursor.lnum = from + linecnt;
2585	else
2586	    curwin->w_cursor.lnum = from + 1;
2587	check_cursor_lnum();
2588	beginline(BL_WHITE | BL_FIX);	    /* on first non-blank */
2589
2590	/*
2591	 * Set '[ and '] marks to the newly read lines.
2592	 */
2593	curbuf->b_op_start.lnum = from + 1;
2594	curbuf->b_op_start.col = 0;
2595	curbuf->b_op_end.lnum = from + linecnt;
2596	curbuf->b_op_end.col = 0;
2597
2598#ifdef WIN32
2599	/*
2600	 * Work around a weird problem: When a file has two links (only
2601	 * possible on NTFS) and we write through one link, then stat() it
2602	 * through the other link, the timestamp information may be wrong.
2603	 * It's correct again after reading the file, thus reset the timestamp
2604	 * here.
2605	 */
2606	if (newfile && !read_stdin && !read_buffer
2607					 && mch_stat((char *)fname, &st) >= 0)
2608	{
2609	    buf_store_time(curbuf, &st, fname);
2610	    curbuf->b_mtime_read = curbuf->b_mtime;
2611	}
2612#endif
2613    }
2614    msg_scroll = msg_save;
2615
2616#ifdef FEAT_VIMINFO
2617    /*
2618     * Get the marks before executing autocommands, so they can be used there.
2619     */
2620    check_marks_read();
2621#endif
2622
2623    /*
2624     * Trick: We remember if the last line of the read didn't have
2625     * an eol for when writing it again.  This is required for
2626     * ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
2627     */
2628    write_no_eol_lnum = read_no_eol_lnum;
2629
2630    /* When reloading a buffer put the cursor at the first line that is
2631     * different. */
2632    if (flags & READ_KEEP_UNDO)
2633	u_find_first_changed();
2634
2635#ifdef FEAT_PERSISTENT_UNDO
2636    /*
2637     * When opening a new file locate undo info and read it.
2638     */
2639    if (read_undo_file)
2640    {
2641	char_u	hash[UNDO_HASH_SIZE];
2642
2643	sha256_finish(&sha_ctx, hash);
2644	u_read_undo(NULL, hash, fname);
2645    }
2646#endif
2647
2648#ifdef FEAT_AUTOCMD
2649    if (!read_stdin && !read_buffer)
2650    {
2651	int m = msg_scroll;
2652	int n = msg_scrolled;
2653
2654	/* Save the fileformat now, otherwise the buffer will be considered
2655	 * modified if the format/encoding was automatically detected. */
2656	if (set_options)
2657	    save_file_ff(curbuf);
2658
2659	/*
2660	 * The output from the autocommands should not overwrite anything and
2661	 * should not be overwritten: Set msg_scroll, restore its value if no
2662	 * output was done.
2663	 */
2664	msg_scroll = TRUE;
2665	if (filtering)
2666	    apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2667							  FALSE, curbuf, eap);
2668	else if (newfile)
2669	    apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2670							  FALSE, curbuf, eap);
2671	else
2672	    apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2673							    FALSE, NULL, eap);
2674	if (msg_scrolled == n)
2675	    msg_scroll = m;
2676#ifdef FEAT_EVAL
2677	if (aborting())	    /* autocmds may abort script processing */
2678	    return FAIL;
2679#endif
2680    }
2681#endif
2682
2683    if (recoverymode && error)
2684	return FAIL;
2685    return OK;
2686}
2687
2688#ifdef OPEN_CHR_FILES
2689/*
2690 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2691 * which is the name of files used for process substitution output by
2692 * some shells on some operating systems, e.g., bash on SunOS.
2693 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2694 */
2695    static int
2696is_dev_fd_file(fname)
2697    char_u	*fname;
2698{
2699    return (STRNCMP(fname, "/dev/fd/", 8) == 0
2700	    && VIM_ISDIGIT(fname[8])
2701	    && *skipdigits(fname + 9) == NUL
2702	    && (fname[9] != NUL
2703		|| (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2704}
2705#endif
2706
2707#ifdef FEAT_MBYTE
2708
2709/*
2710 * From the current line count and characters read after that, estimate the
2711 * line number where we are now.
2712 * Used for error messages that include a line number.
2713 */
2714    static linenr_T
2715readfile_linenr(linecnt, p, endp)
2716    linenr_T	linecnt;	/* line count before reading more bytes */
2717    char_u	*p;		/* start of more bytes read */
2718    char_u	*endp;		/* end of more bytes read */
2719{
2720    char_u	*s;
2721    linenr_T	lnum;
2722
2723    lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2724    for (s = p; s < endp; ++s)
2725	if (*s == '\n')
2726	    ++lnum;
2727    return lnum;
2728}
2729#endif
2730
2731/*
2732 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2733 * equal to the buffer "buf".  Used for calling readfile().
2734 * Returns OK or FAIL.
2735 */
2736    int
2737prep_exarg(eap, buf)
2738    exarg_T	*eap;
2739    buf_T	*buf;
2740{
2741    eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff)
2742#ifdef FEAT_MBYTE
2743		+ STRLEN(buf->b_p_fenc)
2744#endif
2745						 + 15));
2746    if (eap->cmd == NULL)
2747	return FAIL;
2748
2749#ifdef FEAT_MBYTE
2750    sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc);
2751    eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff);
2752    eap->bad_char = buf->b_bad_char;
2753#else
2754    sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff);
2755#endif
2756    eap->force_ff = 7;
2757
2758    eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
2759    eap->read_edit = FALSE;
2760    eap->forceit = FALSE;
2761    return OK;
2762}
2763
2764#ifdef FEAT_MBYTE
2765/*
2766 * Find next fileencoding to use from 'fileencodings'.
2767 * "pp" points to fenc_next.  It's advanced to the next item.
2768 * When there are no more items, an empty string is returned and *pp is set to
2769 * NULL.
2770 * When *pp is not set to NULL, the result is in allocated memory.
2771 */
2772    static char_u *
2773next_fenc(pp)
2774    char_u	**pp;
2775{
2776    char_u	*p;
2777    char_u	*r;
2778
2779    if (**pp == NUL)
2780    {
2781	*pp = NULL;
2782	return (char_u *)"";
2783    }
2784    p = vim_strchr(*pp, ',');
2785    if (p == NULL)
2786    {
2787	r = enc_canonize(*pp);
2788	*pp += STRLEN(*pp);
2789    }
2790    else
2791    {
2792	r = vim_strnsave(*pp, (int)(p - *pp));
2793	*pp = p + 1;
2794	if (r != NULL)
2795	{
2796	    p = enc_canonize(r);
2797	    vim_free(r);
2798	    r = p;
2799	}
2800    }
2801    if (r == NULL)	/* out of memory */
2802    {
2803	r = (char_u *)"";
2804	*pp = NULL;
2805    }
2806    return r;
2807}
2808
2809# ifdef FEAT_EVAL
2810/*
2811 * Convert a file with the 'charconvert' expression.
2812 * This closes the file which is to be read, converts it and opens the
2813 * resulting file for reading.
2814 * Returns name of the resulting converted file (the caller should delete it
2815 * after reading it).
2816 * Returns NULL if the conversion failed ("*fdp" is not set) .
2817 */
2818    static char_u *
2819readfile_charconvert(fname, fenc, fdp)
2820    char_u	*fname;		/* name of input file */
2821    char_u	*fenc;		/* converted from */
2822    int		*fdp;		/* in/out: file descriptor of file */
2823{
2824    char_u	*tmpname;
2825    char_u	*errmsg = NULL;
2826
2827    tmpname = vim_tempname('r');
2828    if (tmpname == NULL)
2829	errmsg = (char_u *)_("Can't find temp file for conversion");
2830    else
2831    {
2832	close(*fdp);		/* close the input file, ignore errors */
2833	*fdp = -1;
2834	if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2835						      fname, tmpname) == FAIL)
2836	    errmsg = (char_u *)_("Conversion with 'charconvert' failed");
2837	if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2838						  O_RDONLY | O_EXTRA, 0)) < 0)
2839	    errmsg = (char_u *)_("can't read output of 'charconvert'");
2840    }
2841
2842    if (errmsg != NULL)
2843    {
2844	/* Don't use emsg(), it breaks mappings, the retry with
2845	 * another type of conversion might still work. */
2846	MSG(errmsg);
2847	if (tmpname != NULL)
2848	{
2849	    mch_remove(tmpname);	/* delete converted file */
2850	    vim_free(tmpname);
2851	    tmpname = NULL;
2852	}
2853    }
2854
2855    /* If the input file is closed, open it (caller should check for error). */
2856    if (*fdp < 0)
2857	*fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2858
2859    return tmpname;
2860}
2861# endif
2862
2863#endif
2864
2865#ifdef FEAT_VIMINFO
2866/*
2867 * Read marks for the current buffer from the viminfo file, when we support
2868 * buffer marks and the buffer has a name.
2869 */
2870    static void
2871check_marks_read()
2872{
2873    if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0
2874						  && curbuf->b_ffname != NULL)
2875	read_viminfo(NULL, VIF_WANT_MARKS);
2876
2877    /* Always set b_marks_read; needed when 'viminfo' is changed to include
2878     * the ' parameter after opening a buffer. */
2879    curbuf->b_marks_read = TRUE;
2880}
2881#endif
2882
2883#if defined(FEAT_CRYPT) || defined(PROTO)
2884/*
2885 * Get the crypt method used for a file from "ptr[len]", the magic text at the
2886 * start of the file.
2887 * Returns -1 when no encryption used.
2888 */
2889    static int
2890crypt_method_from_magic(ptr, len)
2891    char  *ptr;
2892    int   len;
2893{
2894    int i;
2895
2896    for (i = 0; i < (int)(sizeof(crypt_magic) / sizeof(crypt_magic[0])); i++)
2897    {
2898	if (len < (CRYPT_MAGIC_LEN + crypt_salt_len[i] + crypt_seed_len[i]))
2899	    continue;
2900	if (memcmp(ptr, crypt_magic[i], CRYPT_MAGIC_LEN) == 0)
2901	    return i;
2902    }
2903
2904    i = (int)STRLEN(crypt_magic_head);
2905    if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0)
2906	EMSG(_("E821: File is encrypted with unknown method"));
2907
2908    return -1;
2909}
2910
2911/*
2912 * Check for magic number used for encryption.  Applies to the current buffer.
2913 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2914 * *filesizep are updated.
2915 * Return the (new) encryption key, NULL for no encryption.
2916 */
2917    static char_u *
2918check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask)
2919    char_u	*cryptkey;	/* previous encryption key or NULL */
2920    char_u	*ptr;		/* pointer to read bytes */
2921    long	*sizep;		/* length of read bytes */
2922    off_t	*filesizep;	/* nr of bytes used from file */
2923    int		newfile;	/* editing a new buffer */
2924    char_u	*fname;		/* file name to display */
2925    int		*did_ask;	/* flag: whether already asked for key */
2926{
2927    int method = crypt_method_from_magic((char *)ptr, *sizep);
2928
2929    if (method >= 0)
2930    {
2931	set_crypt_method(curbuf, method);
2932	if (method > 0)
2933	    (void)blowfish_self_test();
2934	if (cryptkey == NULL && !*did_ask)
2935	{
2936	    if (*curbuf->b_p_key)
2937		cryptkey = curbuf->b_p_key;
2938	    else
2939	    {
2940		/* When newfile is TRUE, store the typed key in the 'key'
2941		 * option and don't free it.  bf needs hash of the key saved.
2942		 * Don't ask for the key again when first time Enter was hit.
2943		 * Happens when retrying to detect encoding. */
2944		smsg((char_u *)_(need_key_msg), fname);
2945		msg_scroll = TRUE;
2946		cryptkey = get_crypt_key(newfile, FALSE);
2947		*did_ask = TRUE;
2948
2949		/* check if empty key entered */
2950		if (cryptkey != NULL && *cryptkey == NUL)
2951		{
2952		    if (cryptkey != curbuf->b_p_key)
2953			vim_free(cryptkey);
2954		    cryptkey = NULL;
2955		}
2956	    }
2957	}
2958
2959	if (cryptkey != NULL)
2960	{
2961	    int seed_len = crypt_seed_len[method];
2962	    int salt_len = crypt_salt_len[method];
2963
2964	    crypt_push_state();
2965	    use_crypt_method = method;
2966	    if (method == 0)
2967		crypt_init_keys(cryptkey);
2968	    else
2969	    {
2970		bf_key_init(cryptkey, ptr + CRYPT_MAGIC_LEN, salt_len);
2971		bf_ofb_init(ptr + CRYPT_MAGIC_LEN + salt_len, seed_len);
2972	    }
2973
2974	    /* Remove magic number from the text */
2975	    *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len;
2976	    *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len;
2977	    mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len,
2978							      (size_t)*sizep);
2979	}
2980    }
2981    /* When starting to edit a new file which does not have encryption, clear
2982     * the 'key' option, except when starting up (called with -x argument) */
2983    else if (newfile && *curbuf->b_p_key != NUL && !starting)
2984	set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2985
2986    return cryptkey;
2987}
2988
2989/*
2990 * Check for magic number used for encryption.  Applies to the current buffer.
2991 * If found and decryption is possible returns OK;
2992 */
2993    int
2994prepare_crypt_read(fp)
2995    FILE	*fp;
2996{
2997    int		method;
2998    char_u	buffer[CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX
2999						    + CRYPT_SEED_LEN_MAX + 2];
3000
3001    if (fread(buffer, CRYPT_MAGIC_LEN, 1, fp) != 1)
3002	return FAIL;
3003    method = crypt_method_from_magic((char *)buffer,
3004					CRYPT_MAGIC_LEN +
3005					CRYPT_SEED_LEN_MAX +
3006					CRYPT_SALT_LEN_MAX);
3007    if (method < 0 || method != get_crypt_method(curbuf))
3008	return FAIL;
3009
3010    crypt_push_state();
3011    if (method == 0)
3012	crypt_init_keys(curbuf->b_p_key);
3013    else
3014    {
3015	int salt_len = crypt_salt_len[method];
3016	int seed_len = crypt_seed_len[method];
3017
3018	if (fread(buffer, salt_len + seed_len, 1, fp) != 1)
3019	    return FAIL;
3020	bf_key_init(curbuf->b_p_key, buffer, salt_len);
3021	bf_ofb_init(buffer + salt_len, seed_len);
3022    }
3023    return OK;
3024}
3025
3026/*
3027 * Prepare for writing encrypted bytes for buffer "buf".
3028 * Returns a pointer to an allocated header of length "*lenp".
3029 * When out of memory returns NULL.
3030 * Otherwise calls crypt_push_state(), call crypt_pop_state() later.
3031 */
3032    char_u *
3033prepare_crypt_write(buf, lenp)
3034    buf_T *buf;
3035    int   *lenp;
3036{
3037    char_u  *header;
3038    int	    seed_len = crypt_seed_len[get_crypt_method(buf)];
3039    int     salt_len = crypt_salt_len[get_crypt_method(buf)];
3040    char_u  *salt;
3041    char_u  *seed;
3042
3043    header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX
3044						    + CRYPT_SEED_LEN_MAX + 2);
3045    if (header != NULL)
3046    {
3047	crypt_push_state();
3048	use_crypt_method = get_crypt_method(buf);  /* select zip or blowfish */
3049	vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method],
3050							     CRYPT_MAGIC_LEN);
3051	if (use_crypt_method == 0)
3052	    crypt_init_keys(buf->b_p_key);
3053	else
3054	{
3055	    /* Using blowfish, add salt and seed. */
3056	    salt = header + CRYPT_MAGIC_LEN;
3057	    seed = salt + salt_len;
3058	    sha2_seed(salt, salt_len, seed, seed_len);
3059	    bf_key_init(buf->b_p_key, salt, salt_len);
3060	    bf_ofb_init(seed, seed_len);
3061	}
3062    }
3063    *lenp = CRYPT_MAGIC_LEN + salt_len + seed_len;
3064    return header;
3065}
3066
3067#endif  /* FEAT_CRYPT */
3068
3069#ifdef UNIX
3070    static void
3071set_file_time(fname, atime, mtime)
3072    char_u  *fname;
3073    time_t  atime;	    /* access time */
3074    time_t  mtime;	    /* modification time */
3075{
3076# if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
3077    struct utimbuf  buf;
3078
3079    buf.actime	= atime;
3080    buf.modtime	= mtime;
3081    (void)utime((char *)fname, &buf);
3082# else
3083#  if defined(HAVE_UTIMES)
3084    struct timeval  tvp[2];
3085
3086    tvp[0].tv_sec   = atime;
3087    tvp[0].tv_usec  = 0;
3088    tvp[1].tv_sec   = mtime;
3089    tvp[1].tv_usec  = 0;
3090#   ifdef NeXT
3091    (void)utimes((char *)fname, tvp);
3092#   else
3093    (void)utimes((char *)fname, (const struct timeval *)&tvp);
3094#   endif
3095#  endif
3096# endif
3097}
3098#endif /* UNIX */
3099
3100#if defined(VMS) && !defined(MIN)
3101/* Older DECC compiler for VAX doesn't define MIN() */
3102# define MIN(a, b) ((a) < (b) ? (a) : (b))
3103#endif
3104
3105/*
3106 * Return TRUE if a file appears to be read-only from the file permissions.
3107 */
3108    int
3109check_file_readonly(fname, perm)
3110    char_u	*fname;		/* full path to file */
3111    int		perm;		/* known permissions on file */
3112{
3113#ifndef USE_MCH_ACCESS
3114    int	    fd = 0;
3115#endif
3116
3117    return (
3118#ifdef USE_MCH_ACCESS
3119# ifdef UNIX
3120	(perm & 0222) == 0 ||
3121# endif
3122	mch_access((char *)fname, W_OK)
3123#else
3124	(fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3125					? TRUE : (close(fd), FALSE)
3126#endif
3127	);
3128}
3129
3130
3131/*
3132 * buf_write() - write to file "fname" lines "start" through "end"
3133 *
3134 * We do our own buffering here because fwrite() is so slow.
3135 *
3136 * If "forceit" is true, we don't care for errors when attempting backups.
3137 * In case of an error everything possible is done to restore the original
3138 * file.  But when "forceit" is TRUE, we risk losing it.
3139 *
3140 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and
3141 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed.
3142 *
3143 * This function must NOT use NameBuff (because it's called by autowrite()).
3144 *
3145 * return FAIL for failure, OK otherwise
3146 */
3147    int
3148buf_write(buf, fname, sfname, start, end, eap, append, forceit,
3149						      reset_changed, filtering)
3150    buf_T	    *buf;
3151    char_u	    *fname;
3152    char_u	    *sfname;
3153    linenr_T	    start, end;
3154    exarg_T	    *eap;		/* for forced 'ff' and 'fenc', can be
3155					   NULL! */
3156    int		    append;		/* append to the file */
3157    int		    forceit;
3158    int		    reset_changed;
3159    int		    filtering;
3160{
3161    int		    fd;
3162    char_u	    *backup = NULL;
3163    int		    backup_copy = FALSE; /* copy the original file? */
3164    int		    dobackup;
3165    char_u	    *ffname;
3166    char_u	    *wfname = NULL;	/* name of file to write to */
3167    char_u	    *s;
3168    char_u	    *ptr;
3169    char_u	    c;
3170    int		    len;
3171    linenr_T	    lnum;
3172    long	    nchars;
3173    char_u	    *errmsg = NULL;
3174    int		    errmsg_allocated = FALSE;
3175    char_u	    *errnum = NULL;
3176    char_u	    *buffer;
3177    char_u	    smallbuf[SMBUFSIZE];
3178    char_u	    *backup_ext;
3179    int		    bufsize;
3180    long	    perm;		    /* file permissions */
3181    int		    retval = OK;
3182    int		    newfile = FALSE;	    /* TRUE if file doesn't exist yet */
3183    int		    msg_save = msg_scroll;
3184    int		    overwriting;	    /* TRUE if writing over original */
3185    int		    no_eol = FALSE;	    /* no end-of-line written */
3186    int		    device = FALSE;	    /* writing to a device */
3187    struct stat	    st_old;
3188    int		    prev_got_int = got_int;
3189    int		    file_readonly = FALSE;  /* overwritten file is read-only */
3190    static char	    *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')";
3191#if defined(UNIX) || defined(__EMX__XX)	    /*XXX fix me sometime? */
3192    int		    made_writable = FALSE;  /* 'w' bit has been set */
3193#endif
3194					/* writing everything */
3195    int		    whole = (start == 1 && end == buf->b_ml.ml_line_count);
3196#ifdef FEAT_AUTOCMD
3197    linenr_T	    old_line_count = buf->b_ml.ml_line_count;
3198#endif
3199    int		    attr;
3200    int		    fileformat;
3201    int		    write_bin;
3202    struct bw_info  write_info;		/* info for buf_write_bytes() */
3203#ifdef FEAT_MBYTE
3204    int		    converted = FALSE;
3205    int		    notconverted = FALSE;
3206    char_u	    *fenc;		/* effective 'fileencoding' */
3207    char_u	    *fenc_tofree = NULL; /* allocated "fenc" */
3208#endif
3209#ifdef HAS_BW_FLAGS
3210    int		    wb_flags = 0;
3211#endif
3212#ifdef HAVE_ACL
3213    vim_acl_T	    acl = NULL;		/* ACL copied from original file to
3214					   backup or new file */
3215#endif
3216#ifdef FEAT_PERSISTENT_UNDO
3217    int		    write_undo_file = FALSE;
3218    context_sha256_T sha_ctx;
3219#endif
3220#ifdef HAVE_COPYFILE
3221    copyfile_state_t	copyfile_state = NULL;
3222#endif
3223
3224    if (fname == NULL || *fname == NUL)	/* safety check */
3225	return FAIL;
3226    if (buf->b_ml.ml_mfp == NULL && !Unix2003_compat)
3227    {
3228	/* This can happen during startup when there is a stray "w" in the
3229	 * vimrc file. */
3230	EMSG(_(e_emptybuf));
3231	return FAIL;
3232    }
3233
3234    /*
3235     * Disallow writing from .exrc and .vimrc in current directory for
3236     * security reasons.
3237     */
3238    if (check_secure())
3239	return FAIL;
3240
3241    /* Avoid a crash for a long name. */
3242    if (STRLEN(fname) >= MAXPATHL)
3243    {
3244	EMSG(_(e_longname));
3245	return FAIL;
3246    }
3247
3248#ifdef FEAT_MBYTE
3249    /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */
3250    write_info.bw_conv_buf = NULL;
3251    write_info.bw_conv_error = FALSE;
3252    write_info.bw_conv_error_lnum = 0;
3253    write_info.bw_restlen = 0;
3254# ifdef USE_ICONV
3255    write_info.bw_iconv_fd = (iconv_t)-1;
3256# endif
3257#endif
3258
3259    /* After writing a file changedtick changes but we don't want to display
3260     * the line. */
3261    ex_no_reprint = TRUE;
3262
3263    /*
3264     * If there is no file name yet, use the one for the written file.
3265     * BF_NOTEDITED is set to reflect this (in case the write fails).
3266     * Don't do this when the write is for a filter command.
3267     * Don't do this when appending.
3268     * Only do this when 'cpoptions' contains the 'F' flag.
3269     */
3270    if (buf->b_ffname == NULL
3271	    && reset_changed
3272	    && whole
3273	    && buf == curbuf
3274#ifdef FEAT_QUICKFIX
3275	    && !bt_nofile(buf)
3276#endif
3277	    && !filtering
3278	    && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL)
3279	    && vim_strchr(p_cpo, CPO_FNAMEW) != NULL)
3280    {
3281	if (set_rw_fname(fname, sfname) == FAIL)
3282	    return FAIL;
3283	buf = curbuf;	    /* just in case autocmds made "buf" invalid */
3284    }
3285
3286    if (sfname == NULL)
3287	sfname = fname;
3288    /*
3289     * For Unix: Use the short file name whenever possible.
3290     * Avoids problems with networks and when directory names are changed.
3291     * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
3292     * another directory, which we don't detect
3293     */
3294    ffname = fname;			    /* remember full fname */
3295#ifdef UNIX
3296    fname = sfname;
3297#endif
3298
3299    if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0)
3300	overwriting = TRUE;
3301    else
3302	overwriting = FALSE;
3303
3304    if (exiting)
3305	settmode(TMODE_COOK);	    /* when exiting allow typahead now */
3306
3307    ++no_wait_return;		    /* don't wait for return yet */
3308
3309    /*
3310     * Set '[ and '] marks to the lines to be written.
3311     */
3312    buf->b_op_start.lnum = start;
3313    buf->b_op_start.col = 0;
3314    buf->b_op_end.lnum = end;
3315    buf->b_op_end.col = 0;
3316
3317#ifdef FEAT_AUTOCMD
3318    {
3319	aco_save_T	aco;
3320	int		buf_ffname = FALSE;
3321	int		buf_sfname = FALSE;
3322	int		buf_fname_f = FALSE;
3323	int		buf_fname_s = FALSE;
3324	int		did_cmd = FALSE;
3325	int		nofile_err = FALSE;
3326	int		empty_memline = (buf->b_ml.ml_mfp == NULL);
3327
3328	/*
3329	 * Apply PRE aucocommands.
3330	 * Set curbuf to the buffer to be written.
3331	 * Careful: The autocommands may call buf_write() recursively!
3332	 */
3333	if (ffname == buf->b_ffname)
3334	    buf_ffname = TRUE;
3335	if (sfname == buf->b_sfname)
3336	    buf_sfname = TRUE;
3337	if (fname == buf->b_ffname)
3338	    buf_fname_f = TRUE;
3339	if (fname == buf->b_sfname)
3340	    buf_fname_s = TRUE;
3341
3342	/* set curwin/curbuf to buf and save a few things */
3343	aucmd_prepbuf(&aco, buf);
3344
3345	if (append)
3346	{
3347	    if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD,
3348					 sfname, sfname, FALSE, curbuf, eap)))
3349	    {
3350#ifdef FEAT_QUICKFIX
3351		if (overwriting && bt_nofile(curbuf))
3352		    nofile_err = TRUE;
3353		else
3354#endif
3355		    apply_autocmds_exarg(EVENT_FILEAPPENDPRE,
3356					  sfname, sfname, FALSE, curbuf, eap);
3357	    }
3358	}
3359	else if (filtering)
3360	{
3361	    apply_autocmds_exarg(EVENT_FILTERWRITEPRE,
3362					    NULL, sfname, FALSE, curbuf, eap);
3363	}
3364	else if (reset_changed && whole)
3365	{
3366	    if (!(did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD,
3367					 sfname, sfname, FALSE, curbuf, eap)))
3368	    {
3369#ifdef FEAT_QUICKFIX
3370		if (overwriting && bt_nofile(curbuf))
3371		    nofile_err = TRUE;
3372		else
3373#endif
3374		    apply_autocmds_exarg(EVENT_BUFWRITEPRE,
3375					  sfname, sfname, FALSE, curbuf, eap);
3376	    }
3377	}
3378	else
3379	{
3380	    if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD,
3381					 sfname, sfname, FALSE, curbuf, eap)))
3382	    {
3383#ifdef FEAT_QUICKFIX
3384		if (overwriting && bt_nofile(curbuf))
3385		    nofile_err = TRUE;
3386		else
3387#endif
3388		    apply_autocmds_exarg(EVENT_FILEWRITEPRE,
3389					  sfname, sfname, FALSE, curbuf, eap);
3390	    }
3391	}
3392
3393	/* restore curwin/curbuf and a few other things */
3394	aucmd_restbuf(&aco);
3395
3396	/*
3397	 * In three situations we return here and don't write the file:
3398	 * 1. the autocommands deleted or unloaded the buffer.
3399	 * 2. The autocommands abort script processing.
3400	 * 3. If one of the "Cmd" autocommands was executed.
3401	 */
3402	if (!buf_valid(buf))
3403	    buf = NULL;
3404	if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline)
3405				       || did_cmd || nofile_err
3406#ifdef FEAT_EVAL
3407				       || aborting()
3408#endif
3409				       )
3410	{
3411	    --no_wait_return;
3412	    msg_scroll = msg_save;
3413	    if (nofile_err)
3414		EMSG(_("E676: No matching autocommands for acwrite buffer"));
3415
3416	    if (nofile_err
3417#ifdef FEAT_EVAL
3418		    || aborting()
3419#endif
3420		    )
3421		/* An aborting error, interrupt or exception in the
3422		 * autocommands. */
3423		return FAIL;
3424	    if (did_cmd)
3425	    {
3426		if (buf == NULL)
3427		    /* The buffer was deleted.  We assume it was written
3428		     * (can't retry anyway). */
3429		    return OK;
3430		if (overwriting)
3431		{
3432		    /* Assume the buffer was written, update the timestamp. */
3433		    ml_timestamp(buf);
3434		    if (append)
3435			buf->b_flags &= ~BF_NEW;
3436		    else
3437			buf->b_flags &= ~BF_WRITE_MASK;
3438		}
3439		if (reset_changed && buf->b_changed && !append
3440			&& (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
3441		    /* Buffer still changed, the autocommands didn't work
3442		     * properly. */
3443		    return FAIL;
3444		return OK;
3445	    }
3446#ifdef FEAT_EVAL
3447	    if (!aborting())
3448#endif
3449		EMSG(_("E203: Autocommands deleted or unloaded buffer to be written"));
3450	    return FAIL;
3451	}
3452
3453	/*
3454	 * The autocommands may have changed the number of lines in the file.
3455	 * When writing the whole file, adjust the end.
3456	 * When writing part of the file, assume that the autocommands only
3457	 * changed the number of lines that are to be written (tricky!).
3458	 */
3459	if (buf->b_ml.ml_line_count != old_line_count)
3460	{
3461	    if (whole)						/* write all */
3462		end = buf->b_ml.ml_line_count;
3463	    else if (buf->b_ml.ml_line_count > old_line_count)	/* more lines */
3464		end += buf->b_ml.ml_line_count - old_line_count;
3465	    else						/* less lines */
3466	    {
3467		end -= old_line_count - buf->b_ml.ml_line_count;
3468		if (end < start)
3469		{
3470		    --no_wait_return;
3471		    msg_scroll = msg_save;
3472		    EMSG(_("E204: Autocommand changed number of lines in unexpected way"));
3473		    return FAIL;
3474		}
3475	    }
3476	}
3477
3478	/*
3479	 * The autocommands may have changed the name of the buffer, which may
3480	 * be kept in fname, ffname and sfname.
3481	 */
3482	if (buf_ffname)
3483	    ffname = buf->b_ffname;
3484	if (buf_sfname)
3485	    sfname = buf->b_sfname;
3486	if (buf_fname_f)
3487	    fname = buf->b_ffname;
3488	if (buf_fname_s)
3489	    fname = buf->b_sfname;
3490    }
3491#endif
3492
3493#ifdef FEAT_NETBEANS_INTG
3494    if (netbeans_active() && isNetbeansBuffer(buf))
3495    {
3496	if (whole)
3497	{
3498	    /*
3499	     * b_changed can be 0 after an undo, but we still need to write
3500	     * the buffer to NetBeans.
3501	     */
3502	    if (buf->b_changed || isNetbeansModified(buf))
3503	    {
3504		--no_wait_return;		/* may wait for return now */
3505		msg_scroll = msg_save;
3506		netbeans_save_buffer(buf);	/* no error checking... */
3507		return retval;
3508	    }
3509	    else
3510	    {
3511		errnum = (char_u *)"E656: ";
3512		errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers");
3513		buffer = NULL;
3514		goto fail;
3515	    }
3516	}
3517	else
3518	{
3519	    errnum = (char_u *)"E657: ";
3520	    errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers");
3521	    buffer = NULL;
3522	    goto fail;
3523	}
3524    }
3525#endif
3526
3527    if (shortmess(SHM_OVER) && !exiting)
3528	msg_scroll = FALSE;	    /* overwrite previous file message */
3529    else
3530	msg_scroll = TRUE;	    /* don't overwrite previous file message */
3531    if (!filtering)
3532	filemess(buf,
3533#ifndef UNIX
3534		sfname,
3535#else
3536		fname,
3537#endif
3538		    (char_u *)"", 0);	/* show that we are busy */
3539    msg_scroll = FALSE;		    /* always overwrite the file message now */
3540
3541    buffer = alloc(BUFSIZE);
3542    if (buffer == NULL)		    /* can't allocate big buffer, use small
3543				     * one (to be able to write when out of
3544				     * memory) */
3545    {
3546	buffer = smallbuf;
3547	bufsize = SMBUFSIZE;
3548    }
3549    else
3550	bufsize = BUFSIZE;
3551
3552    /*
3553     * Get information about original file (if there is one).
3554     */
3555#if defined(UNIX) && !defined(ARCHIE)
3556    st_old.st_dev = 0;
3557    st_old.st_ino = 0;
3558    perm = -1;
3559    if (mch_stat((char *)fname, &st_old) < 0)
3560	newfile = TRUE;
3561    else
3562    {
3563	perm = st_old.st_mode;
3564	if (!S_ISREG(st_old.st_mode))		/* not a file */
3565	{
3566	    if (S_ISDIR(st_old.st_mode))
3567	    {
3568		errnum = (char_u *)"E502: ";
3569		errmsg = (char_u *)_("is a directory");
3570		goto fail;
3571	    }
3572	    if (mch_nodetype(fname) != NODE_WRITABLE)
3573	    {
3574		errnum = (char_u *)"E503: ";
3575		errmsg = (char_u *)_("is not a file or writable device");
3576		goto fail;
3577	    }
3578	    /* It's a device of some kind (or a fifo) which we can write to
3579	     * but for which we can't make a backup. */
3580	    device = TRUE;
3581	    newfile = TRUE;
3582	    perm = -1;
3583	}
3584    }
3585#else /* !UNIX */
3586    /*
3587     * Check for a writable device name.
3588     */
3589    c = mch_nodetype(fname);
3590    if (c == NODE_OTHER)
3591    {
3592	errnum = (char_u *)"E503: ";
3593	errmsg = (char_u *)_("is not a file or writable device");
3594	goto fail;
3595    }
3596    if (c == NODE_WRITABLE)
3597    {
3598# if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3599	/* MS-Windows allows opening a device, but we will probably get stuck
3600	 * trying to write to it.  */
3601	if (!p_odev)
3602	{
3603	    errnum = (char_u *)"E796: ";
3604	    errmsg = (char_u *)_("writing to device disabled with 'opendevice' option");
3605	    goto fail;
3606	}
3607# endif
3608	device = TRUE;
3609	newfile = TRUE;
3610	perm = -1;
3611    }
3612    else
3613    {
3614	perm = mch_getperm(fname);
3615	if (perm < 0)
3616	    newfile = TRUE;
3617	else if (mch_isdir(fname))
3618	{
3619	    errnum = (char_u *)"E502: ";
3620	    errmsg = (char_u *)_("is a directory");
3621	    goto fail;
3622	}
3623	if (overwriting)
3624	    (void)mch_stat((char *)fname, &st_old);
3625    }
3626#endif /* !UNIX */
3627
3628    if (!device && !newfile)
3629    {
3630	/*
3631	 * Check if the file is really writable (when renaming the file to
3632	 * make a backup we won't discover it later).
3633	 */
3634	file_readonly = check_file_readonly(fname, (int)perm);
3635
3636	if (!forceit && file_readonly)
3637	{
3638	    if (vim_strchr(p_cpo, CPO_FWRITE) != NULL)
3639	    {
3640		errnum = (char_u *)"E504: ";
3641		errmsg = (char_u *)_(err_readonly);
3642	    }
3643	    else
3644	    {
3645		errnum = (char_u *)"E505: ";
3646		errmsg = (char_u *)_("is read-only (add ! to override)");
3647	    }
3648	    goto fail;
3649	}
3650
3651	/*
3652	 * Check if the timestamp hasn't changed since reading the file.
3653	 */
3654	if (overwriting)
3655	{
3656	    retval = check_mtime(buf, &st_old);
3657	    if (retval == FAIL)
3658		goto fail;
3659	}
3660    }
3661
3662#ifdef HAVE_ACL
3663    /*
3664     * For systems that support ACL: get the ACL from the original file.
3665     */
3666    if (!newfile)
3667	acl = mch_get_acl(fname);
3668#endif
3669#ifdef HAVE_COPYFILE
3670    if (!newfile && copyfile((char*)fname, NULL, 0, COPYFILE_XATTR | COPYFILE_CHECK))
3671    {
3672	copyfile_state = copyfile_state_alloc();
3673	copyfile((char*)fname, NULL, copyfile_state, 0);
3674    }
3675#endif
3676
3677    /*
3678     * If 'backupskip' is not empty, don't make a backup for some files.
3679     */
3680    dobackup = (p_wb || p_bk || *p_pm != NUL);
3681#ifdef FEAT_WILDIGN
3682    if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname))
3683	dobackup = FALSE;
3684#endif
3685
3686    /*
3687     * Save the value of got_int and reset it.  We don't want a previous
3688     * interruption cancel writing, only hitting CTRL-C while writing should
3689     * abort it.
3690     */
3691    prev_got_int = got_int;
3692    got_int = FALSE;
3693
3694    /* Mark the buffer as 'being saved' to prevent changed buffer warnings */
3695    buf->b_saving = TRUE;
3696
3697    /*
3698     * If we are not appending or filtering, the file exists, and the
3699     * 'writebackup', 'backup' or 'patchmode' option is set, need a backup.
3700     * When 'patchmode' is set also make a backup when appending.
3701     *
3702     * Do not make any backup, if 'writebackup' and 'backup' are both switched
3703     * off.  This helps when editing large files on almost-full disks.
3704     */
3705    if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup)
3706    {
3707#if defined(UNIX) || defined(WIN32)
3708	struct stat st;
3709#endif
3710
3711	if ((bkc_flags & BKC_YES) || append)	/* "yes" */
3712	    backup_copy = TRUE;
3713#if defined(UNIX) || defined(WIN32)
3714	else if ((bkc_flags & BKC_AUTO))	/* "auto" */
3715	{
3716	    int		i;
3717
3718# ifdef UNIX
3719	    /*
3720	     * Don't rename the file when:
3721	     * - it's a hard link
3722	     * - it's a symbolic link
3723	     * - we don't have write permission in the directory
3724	     * - we can't set the owner/group of the new file
3725	     */
3726	    if (st_old.st_nlink > 1
3727		    || mch_lstat((char *)fname, &st) < 0
3728		    || st.st_dev != st_old.st_dev
3729		    || st.st_ino != st_old.st_ino
3730#  ifndef HAVE_FCHOWN
3731		    || st.st_uid != st_old.st_uid
3732		    || st.st_gid != st_old.st_gid
3733#  endif
3734		    )
3735		backup_copy = TRUE;
3736	    else
3737# else
3738#  ifdef WIN32
3739	    /* On NTFS file systems hard links are possible. */
3740	    if (mch_is_linked(fname))
3741		backup_copy = TRUE;
3742	    else
3743#  endif
3744# endif
3745	    {
3746		/*
3747		 * Check if we can create a file and set the owner/group to
3748		 * the ones from the original file.
3749		 * First find a file name that doesn't exist yet (use some
3750		 * arbitrary numbers).
3751		 */
3752		STRCPY(IObuff, fname);
3753		for (i = 4913; ; i += 123)
3754		{
3755		    sprintf((char *)gettail(IObuff), "%d", i);
3756		    if (mch_lstat((char *)IObuff, &st) < 0)
3757			break;
3758		}
3759		fd = mch_open((char *)IObuff,
3760				    O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
3761		if (fd < 0)	/* can't write in directory */
3762		    backup_copy = TRUE;
3763		else
3764		{
3765# ifdef UNIX
3766#  ifdef HAVE_FCHOWN
3767		    ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
3768#  endif
3769		    if (mch_stat((char *)IObuff, &st) < 0
3770			    || st.st_uid != st_old.st_uid
3771			    || st.st_gid != st_old.st_gid
3772			    || (long)st.st_mode != perm)
3773			backup_copy = TRUE;
3774# endif
3775		    /* Close the file before removing it, on MS-Windows we
3776		     * can't delete an open file. */
3777		    close(fd);
3778		    mch_remove(IObuff);
3779# ifdef MSWIN
3780		    /* MS-Windows may trigger a virus scanner to open the
3781		     * file, we can't delete it then.  Keep trying for half a
3782		     * second. */
3783		    {
3784			int try;
3785
3786			for (try = 0; try < 10; ++try)
3787			{
3788			    if (mch_lstat((char *)IObuff, &st) < 0)
3789				break;
3790			    ui_delay(50L, TRUE);  /* wait 50 msec */
3791			    mch_remove(IObuff);
3792			}
3793		    }
3794# endif
3795		}
3796	    }
3797	}
3798
3799# ifdef UNIX
3800	/*
3801	 * Break symlinks and/or hardlinks if we've been asked to.
3802	 */
3803	if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK))
3804	{
3805	    int	lstat_res;
3806
3807	    lstat_res = mch_lstat((char *)fname, &st);
3808
3809	    /* Symlinks. */
3810	    if ((bkc_flags & BKC_BREAKSYMLINK)
3811		    && lstat_res == 0
3812		    && st.st_ino != st_old.st_ino)
3813		backup_copy = FALSE;
3814
3815	    /* Hardlinks. */
3816	    if ((bkc_flags & BKC_BREAKHARDLINK)
3817		    && st_old.st_nlink > 1
3818		    && (lstat_res != 0 || st.st_ino == st_old.st_ino))
3819		backup_copy = FALSE;
3820	}
3821#endif
3822
3823#endif
3824
3825	/* make sure we have a valid backup extension to use */
3826	if (*p_bex == NUL)
3827	{
3828#ifdef RISCOS
3829	    backup_ext = (char_u *)"/bak";
3830#else
3831	    backup_ext = (char_u *)".bak";
3832#endif
3833	}
3834	else
3835	    backup_ext = p_bex;
3836
3837	if (backup_copy
3838		&& (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0)
3839	{
3840	    int		bfd;
3841	    char_u	*copybuf, *wp;
3842	    int		some_error = FALSE;
3843	    struct stat	st_new;
3844	    char_u	*dirp;
3845	    char_u	*rootname;
3846#if defined(UNIX) && !defined(SHORT_FNAME)
3847	    int		did_set_shortname;
3848#endif
3849
3850	    copybuf = alloc(BUFSIZE + 1);
3851	    if (copybuf == NULL)
3852	    {
3853		some_error = TRUE;	    /* out of memory */
3854		goto nobackup;
3855	    }
3856
3857	    /*
3858	     * Try to make the backup in each directory in the 'bdir' option.
3859	     *
3860	     * Unix semantics has it, that we may have a writable file,
3861	     * that cannot be recreated with a simple open(..., O_CREAT, ) e.g:
3862	     *  - the directory is not writable,
3863	     *  - the file may be a symbolic link,
3864	     *  - the file may belong to another user/group, etc.
3865	     *
3866	     * For these reasons, the existing writable file must be truncated
3867	     * and reused. Creation of a backup COPY will be attempted.
3868	     */
3869	    dirp = p_bdir;
3870	    while (*dirp)
3871	    {
3872#ifdef UNIX
3873		st_new.st_ino = 0;
3874		st_new.st_dev = 0;
3875		st_new.st_gid = 0;
3876#endif
3877
3878		/*
3879		 * Isolate one directory name, using an entry in 'bdir'.
3880		 */
3881		(void)copy_option_part(&dirp, copybuf, BUFSIZE, ",");
3882		rootname = get_file_in_dir(fname, copybuf);
3883		if (rootname == NULL)
3884		{
3885		    some_error = TRUE;	    /* out of memory */
3886		    goto nobackup;
3887		}
3888
3889#if defined(UNIX) && !defined(SHORT_FNAME)
3890		did_set_shortname = FALSE;
3891#endif
3892
3893		/*
3894		 * May try twice if 'shortname' not set.
3895		 */
3896		for (;;)
3897		{
3898		    /*
3899		     * Make backup file name.
3900		     */
3901		    backup = buf_modname(
3902#ifdef SHORT_FNAME
3903			    TRUE,
3904#else
3905			    (buf->b_p_sn || buf->b_shortname),
3906#endif
3907						 rootname, backup_ext, FALSE);
3908		    if (backup == NULL)
3909		    {
3910			vim_free(rootname);
3911			some_error = TRUE;		/* out of memory */
3912			goto nobackup;
3913		    }
3914
3915		    /*
3916		     * Check if backup file already exists.
3917		     */
3918		    if (mch_stat((char *)backup, &st_new) >= 0)
3919		    {
3920#ifdef UNIX
3921			/*
3922			 * Check if backup file is same as original file.
3923			 * May happen when modname() gave the same file back.
3924			 * E.g. silly link, or file name-length reached.
3925			 * If we don't check here, we either ruin the file
3926			 * when copying or erase it after writing. jw.
3927			 */
3928			if (st_new.st_dev == st_old.st_dev
3929					    && st_new.st_ino == st_old.st_ino)
3930			{
3931			    vim_free(backup);
3932			    backup = NULL;	/* no backup file to delete */
3933# ifndef SHORT_FNAME
3934			    /*
3935			     * may try again with 'shortname' set
3936			     */
3937			    if (!(buf->b_shortname || buf->b_p_sn))
3938			    {
3939				buf->b_shortname = TRUE;
3940				did_set_shortname = TRUE;
3941				continue;
3942			    }
3943				/* setting shortname didn't help */
3944			    if (did_set_shortname)
3945				buf->b_shortname = FALSE;
3946# endif
3947			    break;
3948			}
3949#endif
3950
3951			/*
3952			 * If we are not going to keep the backup file, don't
3953			 * delete an existing one, try to use another name.
3954			 * Change one character, just before the extension.
3955			 */
3956			if (!p_bk)
3957			{
3958			    wp = backup + STRLEN(backup) - 1
3959							 - STRLEN(backup_ext);
3960			    if (wp < backup)	/* empty file name ??? */
3961				wp = backup;
3962			    *wp = 'z';
3963			    while (*wp > 'a'
3964				    && mch_stat((char *)backup, &st_new) >= 0)
3965				--*wp;
3966			    /* They all exist??? Must be something wrong. */
3967			    if (*wp == 'a')
3968			    {
3969				vim_free(backup);
3970				backup = NULL;
3971			    }
3972			}
3973		    }
3974		    break;
3975		}
3976		vim_free(rootname);
3977
3978		/*
3979		 * Try to create the backup file
3980		 */
3981		if (backup != NULL)
3982		{
3983		    /* remove old backup, if present */
3984		    mch_remove(backup);
3985		    /* Open with O_EXCL to avoid the file being created while
3986		     * we were sleeping (symlink hacker attack?) */
3987		    bfd = mch_open((char *)backup,
3988				O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW,
3989								 perm & 0777);
3990		    if (bfd < 0)
3991		    {
3992			vim_free(backup);
3993			backup = NULL;
3994		    }
3995		    else
3996		    {
3997			/* set file protection same as original file, but
3998			 * strip s-bit */
3999			(void)mch_setperm(backup, perm & 0777);
4000
4001#ifdef UNIX
4002			/*
4003			 * Try to set the group of the backup same as the
4004			 * original file. If this fails, set the protection
4005			 * bits for the group same as the protection bits for
4006			 * others.
4007			 */
4008			if (st_new.st_gid != st_old.st_gid
4009# ifdef HAVE_FCHOWN  /* sequent-ptx lacks fchown() */
4010				&& fchown(bfd, (uid_t)-1, st_old.st_gid) != 0
4011# endif
4012						)
4013			    mch_setperm(backup,
4014					  (perm & 0707) | ((perm & 07) << 3));
4015# ifdef HAVE_SELINUX
4016			mch_copy_sec(fname, backup);
4017# endif
4018#endif
4019
4020			/*
4021			 * copy the file.
4022			 */
4023			write_info.bw_fd = bfd;
4024			write_info.bw_buf = copybuf;
4025#ifdef HAS_BW_FLAGS
4026			write_info.bw_flags = FIO_NOCONVERT;
4027#endif
4028			while ((write_info.bw_len = vim_read(fd, copybuf,
4029								BUFSIZE)) > 0)
4030			{
4031			    if (buf_write_bytes(&write_info) == FAIL)
4032			    {
4033				errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)");
4034				break;
4035			    }
4036			    ui_breakcheck();
4037			    if (got_int)
4038			    {
4039				errmsg = (char_u *)_(e_interr);
4040				break;
4041			    }
4042			}
4043
4044			if (close(bfd) < 0 && errmsg == NULL)
4045			    errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)");
4046			if (write_info.bw_len < 0)
4047			    errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)");
4048#ifdef UNIX
4049			set_file_time(backup, st_old.st_atime, st_old.st_mtime);
4050#endif
4051#ifdef HAVE_ACL
4052			mch_set_acl(backup, acl);
4053#endif
4054#ifdef HAVE_COPYFILE
4055			if (copyfile_state)
4056			copyfile(NULL, (char*)backup, copyfile_state, COPYFILE_XATTR);
4057#endif
4058#ifdef HAVE_SELINUX
4059			mch_copy_sec(fname, backup);
4060#endif
4061			break;
4062		    }
4063		}
4064	    }
4065    nobackup:
4066	    close(fd);		/* ignore errors for closing read file */
4067	    vim_free(copybuf);
4068
4069	    if (backup == NULL && errmsg == NULL)
4070		errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)");
4071	    /* ignore errors when forceit is TRUE */
4072	    if ((some_error || errmsg != NULL) && !forceit)
4073	    {
4074		retval = FAIL;
4075		goto fail;
4076	    }
4077	    errmsg = NULL;
4078	}
4079	else
4080	{
4081	    char_u	*dirp;
4082	    char_u	*p;
4083	    char_u	*rootname;
4084
4085	    /*
4086	     * Make a backup by renaming the original file.
4087	     */
4088	    /*
4089	     * If 'cpoptions' includes the "W" flag, we don't want to
4090	     * overwrite a read-only file.  But rename may be possible
4091	     * anyway, thus we need an extra check here.
4092	     */
4093	    if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL)
4094	    {
4095		errnum = (char_u *)"E504: ";
4096		errmsg = (char_u *)_(err_readonly);
4097		goto fail;
4098	    }
4099
4100	    /*
4101	     *
4102	     * Form the backup file name - change path/fo.o.h to
4103	     * path/fo.o.h.bak Try all directories in 'backupdir', first one
4104	     * that works is used.
4105	     */
4106	    dirp = p_bdir;
4107	    while (*dirp)
4108	    {
4109		/*
4110		 * Isolate one directory name and make the backup file name.
4111		 */
4112		(void)copy_option_part(&dirp, IObuff, IOSIZE, ",");
4113		rootname = get_file_in_dir(fname, IObuff);
4114		if (rootname == NULL)
4115		    backup = NULL;
4116		else
4117		{
4118		    backup = buf_modname(
4119#ifdef SHORT_FNAME
4120			    TRUE,
4121#else
4122			    (buf->b_p_sn || buf->b_shortname),
4123#endif
4124						 rootname, backup_ext, FALSE);
4125		    vim_free(rootname);
4126		}
4127
4128		if (backup != NULL)
4129		{
4130		    /*
4131		     * If we are not going to keep the backup file, don't
4132		     * delete an existing one, try to use another name.
4133		     * Change one character, just before the extension.
4134		     */
4135		    if (!p_bk && mch_getperm(backup) >= 0)
4136		    {
4137			p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext);
4138			if (p < backup)	/* empty file name ??? */
4139			    p = backup;
4140			*p = 'z';
4141			while (*p > 'a' && mch_getperm(backup) >= 0)
4142			    --*p;
4143			/* They all exist??? Must be something wrong! */
4144			if (*p == 'a')
4145			{
4146			    vim_free(backup);
4147			    backup = NULL;
4148			}
4149		    }
4150		}
4151		if (backup != NULL)
4152		{
4153		    /*
4154		     * Delete any existing backup and move the current version
4155		     * to the backup.	For safety, we don't remove the backup
4156		     * until the write has finished successfully. And if the
4157		     * 'backup' option is set, leave it around.
4158		     */
4159		    /*
4160		     * If the renaming of the original file to the backup file
4161		     * works, quit here.
4162		     */
4163		    if (vim_rename(fname, backup) == 0)
4164			break;
4165
4166		    vim_free(backup);   /* don't do the rename below */
4167		    backup = NULL;
4168		}
4169	    }
4170	    if (backup == NULL && !forceit)
4171	    {
4172		errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)");
4173		goto fail;
4174	    }
4175	}
4176    }
4177
4178#if defined(UNIX) && !defined(ARCHIE)
4179    /* When using ":w!" and the file was read-only: make it writable */
4180    if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid()
4181				     && vim_strchr(p_cpo, CPO_FWRITE) == NULL)
4182    {
4183	perm |= 0200;
4184	(void)mch_setperm(fname, perm);
4185	made_writable = TRUE;
4186    }
4187#endif
4188
4189    /* When using ":w!" and writing to the current file, 'readonly' makes no
4190     * sense, reset it, unless 'Z' appears in 'cpoptions'.  */
4191    if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL)
4192    {
4193	buf->b_p_ro = FALSE;
4194#ifdef FEAT_TITLE
4195	need_maketitle = TRUE;	    /* set window title later */
4196#endif
4197#ifdef FEAT_WINDOWS
4198	status_redraw_all();	    /* redraw status lines later */
4199#endif
4200    }
4201
4202    if (end > buf->b_ml.ml_line_count)
4203	end = buf->b_ml.ml_line_count;
4204    if (buf->b_ml.ml_flags & ML_EMPTY)
4205	start = end + 1;
4206
4207    /*
4208     * If the original file is being overwritten, there is a small chance that
4209     * we crash in the middle of writing. Therefore the file is preserved now.
4210     * This makes all block numbers positive so that recovery does not need
4211     * the original file.
4212     * Don't do this if there is a backup file and we are exiting.
4213     */
4214    if (reset_changed && !newfile && overwriting
4215					      && !(exiting && backup != NULL))
4216    {
4217	ml_preserve(buf, FALSE);
4218	if (got_int)
4219	{
4220	    errmsg = (char_u *)_(e_interr);
4221	    goto restore_backup;
4222	}
4223    }
4224
4225#ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */
4226    /*
4227     * Before risking to lose the original file verify if there's
4228     * a resource fork to preserve, and if cannot be done warn
4229     * the users. This happens when overwriting without backups.
4230     */
4231    if (backup == NULL && overwriting && !append)
4232	if (mch_has_resource_fork(fname))
4233	{
4234	    errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)");
4235	    goto restore_backup;
4236	}
4237#endif
4238
4239#ifdef VMS
4240    vms_remove_version(fname); /* remove version */
4241#endif
4242    /* Default: write the file directly.  May write to a temp file for
4243     * multi-byte conversion. */
4244    wfname = fname;
4245
4246#ifdef FEAT_MBYTE
4247    /* Check for forced 'fileencoding' from "++opt=val" argument. */
4248    if (eap != NULL && eap->force_enc != 0)
4249    {
4250	fenc = eap->cmd + eap->force_enc;
4251	fenc = enc_canonize(fenc);
4252	fenc_tofree = fenc;
4253    }
4254    else
4255	fenc = buf->b_p_fenc;
4256
4257    /*
4258     * Check if the file needs to be converted.
4259     */
4260    converted = need_conversion(fenc);
4261
4262    /*
4263     * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done.  Or
4264     * Latin1 to Unicode conversion.  This is handled in buf_write_bytes().
4265     * Prepare the flags for it and allocate bw_conv_buf when needed.
4266     */
4267    if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0))
4268    {
4269	wb_flags = get_fio_flags(fenc);
4270	if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8))
4271	{
4272	    /* Need to allocate a buffer to translate into. */
4273	    if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8))
4274		write_info.bw_conv_buflen = bufsize * 2;
4275	    else /* FIO_UCS4 */
4276		write_info.bw_conv_buflen = bufsize * 4;
4277	    write_info.bw_conv_buf
4278			   = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4279	    if (write_info.bw_conv_buf == NULL)
4280		end = 0;
4281	}
4282    }
4283
4284# ifdef WIN3264
4285    if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0)
4286    {
4287	/* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS.  Worst-case * 4: */
4288	write_info.bw_conv_buflen = bufsize * 4;
4289	write_info.bw_conv_buf
4290			    = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4291	if (write_info.bw_conv_buf == NULL)
4292	    end = 0;
4293    }
4294# endif
4295
4296# ifdef MACOS_X
4297    if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0)
4298    {
4299	write_info.bw_conv_buflen = bufsize * 3;
4300	write_info.bw_conv_buf
4301			    = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4302	if (write_info.bw_conv_buf == NULL)
4303	    end = 0;
4304    }
4305# endif
4306
4307# if defined(FEAT_EVAL) || defined(USE_ICONV)
4308    if (converted && wb_flags == 0)
4309    {
4310#  ifdef USE_ICONV
4311	/*
4312	 * Use iconv() conversion when conversion is needed and it's not done
4313	 * internally.
4314	 */
4315	write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc,
4316					enc_utf8 ? (char_u *)"utf-8" : p_enc);
4317	if (write_info.bw_iconv_fd != (iconv_t)-1)
4318	{
4319	    /* We're going to use iconv(), allocate a buffer to convert in. */
4320	    write_info.bw_conv_buflen = bufsize * ICONV_MULT;
4321	    write_info.bw_conv_buf
4322			   = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4323	    if (write_info.bw_conv_buf == NULL)
4324		end = 0;
4325	    write_info.bw_first = TRUE;
4326	}
4327#   ifdef FEAT_EVAL
4328	else
4329#   endif
4330#  endif
4331
4332#  ifdef FEAT_EVAL
4333	    /*
4334	     * When the file needs to be converted with 'charconvert' after
4335	     * writing, write to a temp file instead and let the conversion
4336	     * overwrite the original file.
4337	     */
4338	    if (*p_ccv != NUL)
4339	    {
4340		wfname = vim_tempname('w');
4341		if (wfname == NULL)	/* Can't write without a tempfile! */
4342		{
4343		    errmsg = (char_u *)_("E214: Can't find temp file for writing");
4344		    goto restore_backup;
4345		}
4346	    }
4347#  endif
4348    }
4349# endif
4350    if (converted && wb_flags == 0
4351#  ifdef USE_ICONV
4352	    && write_info.bw_iconv_fd == (iconv_t)-1
4353#  endif
4354#  ifdef FEAT_EVAL
4355	    && wfname == fname
4356#  endif
4357	    )
4358    {
4359	if (!forceit)
4360	{
4361	    errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)");
4362	    goto restore_backup;
4363	}
4364	notconverted = TRUE;
4365    }
4366#endif
4367
4368    /*
4369     * Open the file "wfname" for writing.
4370     * We may try to open the file twice: If we can't write to the
4371     * file and forceit is TRUE we delete the existing file and try to create
4372     * a new one. If this still fails we may have lost the original file!
4373     * (this may happen when the user reached his quotum for number of files).
4374     * Appending will fail if the file does not exist and forceit is FALSE.
4375     */
4376    while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append
4377			? (forceit ? (O_APPEND | O_CREAT) : O_APPEND)
4378			: (O_CREAT | O_TRUNC))
4379			, perm < 0 ? 0666 : (perm & 0777))) < 0)
4380    {
4381	/*
4382	 * A forced write will try to create a new file if the old one is
4383	 * still readonly. This may also happen when the directory is
4384	 * read-only. In that case the mch_remove() will fail.
4385	 */
4386	if (errmsg == NULL)
4387	{
4388#ifdef UNIX
4389	    struct stat	st;
4390
4391	    /* Don't delete the file when it's a hard or symbolic link. */
4392	    if ((!newfile && st_old.st_nlink > 1)
4393		    || (mch_lstat((char *)fname, &st) == 0
4394			&& (st.st_dev != st_old.st_dev
4395			    || st.st_ino != st_old.st_ino)))
4396		errmsg = (char_u *)_("E166: Can't open linked file for writing");
4397	    else
4398#endif
4399	    {
4400		errmsg = (char_u *)_("E212: Can't open file for writing");
4401		if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL
4402								 && perm >= 0)
4403		{
4404#ifdef UNIX
4405		    /* we write to the file, thus it should be marked
4406		       writable after all */
4407		    if (!(perm & 0200))
4408			made_writable = TRUE;
4409		    perm |= 0200;
4410		    if (st_old.st_uid != getuid() || st_old.st_gid != getgid())
4411			perm &= 0777;
4412#endif
4413		    if (!append)	    /* don't remove when appending */
4414			mch_remove(wfname);
4415		    continue;
4416		}
4417	    }
4418	}
4419
4420restore_backup:
4421	{
4422	    struct stat st;
4423
4424	    /*
4425	     * If we failed to open the file, we don't need a backup. Throw it
4426	     * away.  If we moved or removed the original file try to put the
4427	     * backup in its place.
4428	     */
4429	    if (backup != NULL && wfname == fname)
4430	    {
4431		if (backup_copy)
4432		{
4433		    /*
4434		     * There is a small chance that we removed the original,
4435		     * try to move the copy in its place.
4436		     * This may not work if the vim_rename() fails.
4437		     * In that case we leave the copy around.
4438		     */
4439		    /* If file does not exist, put the copy in its place */
4440		    if (mch_stat((char *)fname, &st) < 0)
4441			vim_rename(backup, fname);
4442		    /* if original file does exist throw away the copy */
4443		    if (mch_stat((char *)fname, &st) >= 0)
4444			mch_remove(backup);
4445		}
4446		else
4447		{
4448		    /* try to put the original file back */
4449		    vim_rename(backup, fname);
4450		}
4451	    }
4452
4453	    /* if original file no longer exists give an extra warning */
4454	    if (!newfile && mch_stat((char *)fname, &st) < 0)
4455		end = 0;
4456	}
4457
4458#ifdef FEAT_MBYTE
4459	if (wfname != fname)
4460	    vim_free(wfname);
4461#endif
4462	goto fail;
4463    }
4464    errmsg = NULL;
4465
4466#if defined(MACOS_CLASSIC) || defined(WIN3264)
4467    /* TODO: Is it need for MACOS_X? (Dany) */
4468    /*
4469     * On macintosh copy the original files attributes (i.e. the backup)
4470     * This is done in order to preserve the resource fork and the
4471     * Finder attribute (label, comments, custom icons, file creator)
4472     */
4473    if (backup != NULL && overwriting && !append)
4474    {
4475	if (backup_copy)
4476	    (void)mch_copy_file_attribute(wfname, backup);
4477	else
4478	    (void)mch_copy_file_attribute(backup, wfname);
4479    }
4480
4481    if (!overwriting && !append)
4482    {
4483	if (buf->b_ffname != NULL)
4484	    (void)mch_copy_file_attribute(buf->b_ffname, wfname);
4485	/* Should copy resource fork */
4486    }
4487#endif
4488
4489    write_info.bw_fd = fd;
4490
4491#ifdef FEAT_CRYPT
4492    if (*buf->b_p_key != NUL && !filtering)
4493    {
4494	char_u *header;
4495	int    header_len;
4496
4497	header = prepare_crypt_write(buf, &header_len);
4498	if (header == NULL)
4499	    end = 0;
4500	else
4501	{
4502	    /* Write magic number, so that Vim knows that this file is
4503	     * encrypted when reading it again.  This also undergoes utf-8 to
4504	     * ucs-2/4 conversion when needed. */
4505	    write_info.bw_buf = header;
4506	    write_info.bw_len = header_len;
4507	    write_info.bw_flags = FIO_NOCONVERT;
4508	    if (buf_write_bytes(&write_info) == FAIL)
4509		end = 0;
4510	    wb_flags |= FIO_ENCRYPTED;
4511	    vim_free(header);
4512	}
4513    }
4514#endif
4515
4516    write_info.bw_buf = buffer;
4517    nchars = 0;
4518
4519    /* use "++bin", "++nobin" or 'binary' */
4520    if (eap != NULL && eap->force_bin != 0)
4521	write_bin = (eap->force_bin == FORCE_BIN);
4522    else
4523	write_bin = buf->b_p_bin;
4524
4525#ifdef FEAT_MBYTE
4526    /*
4527     * The BOM is written just after the encryption magic number.
4528     * Skip it when appending and the file already existed, the BOM only makes
4529     * sense at the start of the file.
4530     */
4531    if (buf->b_p_bomb && !write_bin && (!append || perm < 0))
4532    {
4533	write_info.bw_len = make_bom(buffer, fenc);
4534	if (write_info.bw_len > 0)
4535	{
4536	    /* don't convert, do encryption */
4537	    write_info.bw_flags = FIO_NOCONVERT | wb_flags;
4538	    if (buf_write_bytes(&write_info) == FAIL)
4539		end = 0;
4540	    else
4541		nchars += write_info.bw_len;
4542	}
4543    }
4544    write_info.bw_start_lnum = start;
4545#endif
4546
4547#ifdef FEAT_PERSISTENT_UNDO
4548    write_undo_file = (buf->b_p_udf && overwriting && !append
4549					      && !filtering && reset_changed);
4550    if (write_undo_file)
4551	/* Prepare for computing the hash value of the text. */
4552	sha256_start(&sha_ctx);
4553#endif
4554
4555    write_info.bw_len = bufsize;
4556#ifdef HAS_BW_FLAGS
4557    write_info.bw_flags = wb_flags;
4558#endif
4559    fileformat = get_fileformat_force(buf, eap);
4560    s = buffer;
4561    len = 0;
4562    for (lnum = start; lnum <= end; ++lnum)
4563    {
4564	/*
4565	 * The next while loop is done once for each character written.
4566	 * Keep it fast!
4567	 */
4568	ptr = ml_get_buf(buf, lnum, FALSE) - 1;
4569#ifdef FEAT_PERSISTENT_UNDO
4570	if (write_undo_file)
4571	    sha256_update(&sha_ctx, ptr + 1, (UINT32_T)(STRLEN(ptr + 1) + 1));
4572#endif
4573	while ((c = *++ptr) != NUL)
4574	{
4575	    if (c == NL)
4576		*s = NUL;		/* replace newlines with NULs */
4577	    else if (c == CAR && fileformat == EOL_MAC)
4578		*s = NL;		/* Mac: replace CRs with NLs */
4579	    else
4580		*s = c;
4581	    ++s;
4582	    if (++len != bufsize)
4583		continue;
4584	    if (buf_write_bytes(&write_info) == FAIL)
4585	    {
4586		end = 0;		/* write error: break loop */
4587		break;
4588	    }
4589	    nchars += bufsize;
4590	    s = buffer;
4591	    len = 0;
4592#ifdef FEAT_MBYTE
4593	    write_info.bw_start_lnum = lnum;
4594#endif
4595	}
4596	/* write failed or last line has no EOL: stop here */
4597	if (end == 0
4598		|| (lnum == end
4599		    && write_bin
4600		    && (lnum == write_no_eol_lnum
4601			|| (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol))))
4602	{
4603	    ++lnum;			/* written the line, count it */
4604	    no_eol = TRUE;
4605	    break;
4606	}
4607	if (fileformat == EOL_UNIX)
4608	    *s++ = NL;
4609	else
4610	{
4611	    *s++ = CAR;			/* EOL_MAC or EOL_DOS: write CR */
4612	    if (fileformat == EOL_DOS)	/* write CR-NL */
4613	    {
4614		if (++len == bufsize)
4615		{
4616		    if (buf_write_bytes(&write_info) == FAIL)
4617		    {
4618			end = 0;	/* write error: break loop */
4619			break;
4620		    }
4621		    nchars += bufsize;
4622		    s = buffer;
4623		    len = 0;
4624		}
4625		*s++ = NL;
4626	    }
4627	}
4628	if (++len == bufsize && end)
4629	{
4630	    if (buf_write_bytes(&write_info) == FAIL)
4631	    {
4632		end = 0;		/* write error: break loop */
4633		break;
4634	    }
4635	    nchars += bufsize;
4636	    s = buffer;
4637	    len = 0;
4638
4639	    ui_breakcheck();
4640	    if (got_int)
4641	    {
4642		end = 0;		/* Interrupted, break loop */
4643		break;
4644	    }
4645	}
4646#ifdef VMS
4647	/*
4648	 * On VMS there is a problem: newlines get added when writing blocks
4649	 * at a time. Fix it by writing a line at a time.
4650	 * This is much slower!
4651	 * Explanation: VAX/DECC RTL insists that records in some RMS
4652	 * structures end with a newline (carriage return) character, and if
4653	 * they don't it adds one.
4654	 * With other RMS structures it works perfect without this fix.
4655	 */
4656	if (buf->b_fab_rfm == FAB$C_VFC
4657		|| ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
4658	{
4659	    int b2write;
4660
4661	    buf->b_fab_mrs = (buf->b_fab_mrs == 0
4662		    ? MIN(4096, bufsize)
4663		    : MIN(buf->b_fab_mrs, bufsize));
4664
4665	    b2write = len;
4666	    while (b2write > 0)
4667	    {
4668		write_info.bw_len = MIN(b2write, buf->b_fab_mrs);
4669		if (buf_write_bytes(&write_info) == FAIL)
4670		{
4671		    end = 0;
4672		    break;
4673		}
4674		b2write -= MIN(b2write, buf->b_fab_mrs);
4675	    }
4676	    write_info.bw_len = bufsize;
4677	    nchars += len;
4678	    s = buffer;
4679	    len = 0;
4680	}
4681#endif
4682    }
4683    if (len > 0 && end > 0)
4684    {
4685	write_info.bw_len = len;
4686	if (buf_write_bytes(&write_info) == FAIL)
4687	    end = 0;		    /* write error */
4688	nchars += len;
4689    }
4690
4691#if defined(UNIX) && defined(HAVE_FSYNC)
4692    /* On many journalling file systems there is a bug that causes both the
4693     * original and the backup file to be lost when halting the system right
4694     * after writing the file.  That's because only the meta-data is
4695     * journalled.  Syncing the file slows down the system, but assures it has
4696     * been written to disk and we don't lose it.
4697     * For a device do try the fsync() but don't complain if it does not work
4698     * (could be a pipe).
4699     * If the 'fsync' option is FALSE, don't fsync().  Useful for laptops. */
4700    if (p_fs && fsync(fd) != 0 && !device)
4701    {
4702	errmsg = (char_u *)_("E667: Fsync failed");
4703	end = 0;
4704    }
4705#endif
4706
4707#ifdef HAVE_SELINUX
4708    /* Probably need to set the security context. */
4709    if (!backup_copy)
4710	mch_copy_sec(backup, wfname);
4711#endif
4712
4713#ifdef UNIX
4714    /* When creating a new file, set its owner/group to that of the original
4715     * file.  Get the new device and inode number. */
4716    if (backup != NULL && !backup_copy)
4717    {
4718# ifdef HAVE_FCHOWN
4719	struct stat	st;
4720
4721	/* don't change the owner when it's already OK, some systems remove
4722	 * permission or ACL stuff */
4723	if (mch_stat((char *)wfname, &st) < 0
4724		|| st.st_uid != st_old.st_uid
4725		|| st.st_gid != st_old.st_gid)
4726	{
4727	    ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
4728	    if (perm >= 0)	/* set permission again, may have changed */
4729		(void)mch_setperm(wfname, perm);
4730	}
4731# endif
4732	buf_setino(buf);
4733    }
4734    else if (!buf->b_dev_valid)
4735	/* Set the inode when creating a new file. */
4736	buf_setino(buf);
4737#endif
4738#ifdef HAVE_COPYFILE
4739    if (!backup_copy && copyfile_state)
4740	copyfile(NULL, (char*)wfname, copyfile_state, COPYFILE_XATTR);
4741#endif
4742
4743    if (close(fd) != 0)
4744    {
4745	errmsg = (char_u *)_("E512: Close failed");
4746	end = 0;
4747    }
4748
4749#ifdef UNIX
4750    if (made_writable)
4751	perm &= ~0200;		/* reset 'w' bit for security reasons */
4752#endif
4753    if (perm >= 0)		/* set perm. of new file same as old file */
4754	(void)mch_setperm(wfname, perm);
4755#ifdef RISCOS
4756    if (!append && !filtering)
4757	/* Set the filetype after writing the file. */
4758	mch_set_filetype(wfname, buf->b_p_oft);
4759#endif
4760#ifdef HAVE_ACL
4761    /* Probably need to set the ACL before changing the user (can't set the
4762     * ACL on a file the user doesn't own). */
4763    if (!backup_copy)
4764	mch_set_acl(wfname, acl);
4765#endif
4766#ifdef FEAT_CRYPT
4767    if (wb_flags & FIO_ENCRYPTED)
4768	crypt_pop_state();
4769#endif
4770
4771
4772#if defined(FEAT_MBYTE) && defined(FEAT_EVAL)
4773    if (wfname != fname)
4774    {
4775	/*
4776	 * The file was written to a temp file, now it needs to be converted
4777	 * with 'charconvert' to (overwrite) the output file.
4778	 */
4779	if (end != 0)
4780	{
4781	    if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc,
4782						       wfname, fname) == FAIL)
4783	    {
4784		write_info.bw_conv_error = TRUE;
4785		end = 0;
4786	    }
4787	}
4788	mch_remove(wfname);
4789	vim_free(wfname);
4790    }
4791#endif
4792
4793    if (end == 0)
4794    {
4795	if (errmsg == NULL)
4796	{
4797#ifdef FEAT_MBYTE
4798	    if (write_info.bw_conv_error)
4799	    {
4800		if (write_info.bw_conv_error_lnum == 0)
4801		    errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)");
4802		else
4803		{
4804		    errmsg_allocated = TRUE;
4805		    errmsg = alloc(300);
4806		    vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"),
4807					 (long)write_info.bw_conv_error_lnum);
4808		}
4809	    }
4810	    else
4811#endif
4812		if (got_int)
4813		    errmsg = (char_u *)_(e_interr);
4814		else
4815		    errmsg = (char_u *)_("E514: write error (file system full?)");
4816	}
4817
4818	/*
4819	 * If we have a backup file, try to put it in place of the new file,
4820	 * because the new file is probably corrupt.  This avoids losing the
4821	 * original file when trying to make a backup when writing the file a
4822	 * second time.
4823	 * When "backup_copy" is set we need to copy the backup over the new
4824	 * file.  Otherwise rename the backup file.
4825	 * If this is OK, don't give the extra warning message.
4826	 */
4827	if (backup != NULL)
4828	{
4829	    if (backup_copy)
4830	    {
4831		/* This may take a while, if we were interrupted let the user
4832		 * know we got the message. */
4833		if (got_int)
4834		{
4835		    MSG(_(e_interr));
4836		    out_flush();
4837		}
4838		if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0)
4839		{
4840		    if ((write_info.bw_fd = mch_open((char *)fname,
4841				    O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA,
4842							   perm & 0777)) >= 0)
4843		    {
4844			/* copy the file. */
4845			write_info.bw_buf = smallbuf;
4846#ifdef HAS_BW_FLAGS
4847			write_info.bw_flags = FIO_NOCONVERT;
4848#endif
4849			while ((write_info.bw_len = vim_read(fd, smallbuf,
4850						      SMBUFSIZE)) > 0)
4851			    if (buf_write_bytes(&write_info) == FAIL)
4852				break;
4853
4854			if (close(write_info.bw_fd) >= 0
4855						   && write_info.bw_len == 0)
4856			    end = 1;		/* success */
4857		    }
4858		    close(fd);	/* ignore errors for closing read file */
4859		}
4860	    }
4861	    else
4862	    {
4863		if (vim_rename(backup, fname) == 0)
4864		    end = 1;
4865	    }
4866	}
4867	goto fail;
4868    }
4869
4870    lnum -= start;	    /* compute number of written lines */
4871    --no_wait_return;	    /* may wait for return now */
4872
4873#if !(defined(UNIX) || defined(VMS))
4874    fname = sfname;	    /* use shortname now, for the messages */
4875#endif
4876    if (!filtering)
4877    {
4878	msg_add_fname(buf, fname);	/* put fname in IObuff with quotes */
4879	c = FALSE;
4880#ifdef FEAT_MBYTE
4881	if (write_info.bw_conv_error)
4882	{
4883	    STRCAT(IObuff, _(" CONVERSION ERROR"));
4884	    c = TRUE;
4885	    if (write_info.bw_conv_error_lnum != 0)
4886		vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"),
4887			(long)write_info.bw_conv_error_lnum);
4888	}
4889	else if (notconverted)
4890	{
4891	    STRCAT(IObuff, _("[NOT converted]"));
4892	    c = TRUE;
4893	}
4894	else if (converted)
4895	{
4896	    STRCAT(IObuff, _("[converted]"));
4897	    c = TRUE;
4898	}
4899#endif
4900	if (device)
4901	{
4902	    STRCAT(IObuff, _("[Device]"));
4903	    c = TRUE;
4904	}
4905	else if (newfile)
4906	{
4907	    STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]"));
4908	    c = TRUE;
4909	}
4910	if (no_eol)
4911	{
4912	    msg_add_eol();
4913	    c = TRUE;
4914	}
4915	/* may add [unix/dos/mac] */
4916	if (msg_add_fileformat(fileformat))
4917	    c = TRUE;
4918#ifdef FEAT_CRYPT
4919	if (wb_flags & FIO_ENCRYPTED)
4920	{
4921	    STRCAT(IObuff, _("[crypted]"));
4922	    c = TRUE;
4923	}
4924#endif
4925	msg_add_lines(c, (long)lnum, nchars);	/* add line/char count */
4926	if (!shortmess(SHM_WRITE))
4927	{
4928	    if (append)
4929		STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended"));
4930	    else
4931		STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written"));
4932	}
4933
4934	set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0);
4935    }
4936
4937    /* When written everything correctly: reset 'modified'.  Unless not
4938     * writing to the original file and '+' is not in 'cpoptions'. */
4939    if (reset_changed && whole && !append
4940#ifdef FEAT_MBYTE
4941	    && !write_info.bw_conv_error
4942#endif
4943	    && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)
4944	    )
4945    {
4946	unchanged(buf, TRUE);
4947	u_unchanged(buf);
4948	u_update_save_nr(buf);
4949    }
4950
4951    /*
4952     * If written to the current file, update the timestamp of the swap file
4953     * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime.
4954     */
4955    if (overwriting)
4956    {
4957	ml_timestamp(buf);
4958	if (append)
4959	    buf->b_flags &= ~BF_NEW;
4960	else
4961	    buf->b_flags &= ~BF_WRITE_MASK;
4962    }
4963
4964    /*
4965     * If we kept a backup until now, and we are in patch mode, then we make
4966     * the backup file our 'original' file.
4967     */
4968    if (*p_pm && dobackup)
4969    {
4970	char *org = (char *)buf_modname(
4971#ifdef SHORT_FNAME
4972					TRUE,
4973#else
4974					(buf->b_p_sn || buf->b_shortname),
4975#endif
4976							  fname, p_pm, FALSE);
4977
4978	if (backup != NULL)
4979	{
4980	    struct stat st;
4981
4982	    /*
4983	     * If the original file does not exist yet
4984	     * the current backup file becomes the original file
4985	     */
4986	    if (org == NULL)
4987		EMSG(_("E205: Patchmode: can't save original file"));
4988	    else if (mch_stat(org, &st) < 0)
4989	    {
4990		vim_rename(backup, (char_u *)org);
4991		vim_free(backup);	    /* don't delete the file */
4992		backup = NULL;
4993#ifdef UNIX
4994		set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime);
4995#endif
4996	    }
4997	}
4998	/*
4999	 * If there is no backup file, remember that a (new) file was
5000	 * created.
5001	 */
5002	else
5003	{
5004	    int empty_fd;
5005
5006	    if (org == NULL
5007		    || (empty_fd = mch_open(org,
5008				      O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW,
5009					perm < 0 ? 0666 : (perm & 0777))) < 0)
5010	      EMSG(_("E206: patchmode: can't touch empty original file"));
5011	    else
5012	      close(empty_fd);
5013	}
5014	if (org != NULL)
5015	{
5016	    mch_setperm((char_u *)org, mch_getperm(fname) & 0777);
5017	    vim_free(org);
5018	}
5019    }
5020
5021    /*
5022     * Remove the backup unless 'backup' option is set
5023     */
5024    if (!p_bk && backup != NULL && mch_remove(backup) != 0)
5025	EMSG(_("E207: Can't delete backup file"));
5026
5027#ifdef FEAT_SUN_WORKSHOP
5028    if (usingSunWorkShop)
5029	workshop_file_saved((char *) ffname);
5030#endif
5031
5032    goto nofail;
5033
5034    /*
5035     * Finish up.  We get here either after failure or success.
5036     */
5037fail:
5038    --no_wait_return;		/* may wait for return now */
5039nofail:
5040
5041    /* Done saving, we accept changed buffer warnings again */
5042    buf->b_saving = FALSE;
5043
5044    vim_free(backup);
5045    if (buffer != smallbuf)
5046	vim_free(buffer);
5047#ifdef FEAT_MBYTE
5048    vim_free(fenc_tofree);
5049    vim_free(write_info.bw_conv_buf);
5050# ifdef USE_ICONV
5051    if (write_info.bw_iconv_fd != (iconv_t)-1)
5052    {
5053	iconv_close(write_info.bw_iconv_fd);
5054	write_info.bw_iconv_fd = (iconv_t)-1;
5055    }
5056# endif
5057#endif
5058#ifdef HAVE_ACL
5059    mch_free_acl(acl);
5060#endif
5061#ifdef HAVE_COPYFILE
5062    if (copyfile_state)
5063	copyfile_state_free(copyfile_state);
5064#endif
5065
5066    if (errmsg != NULL)
5067    {
5068	int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0;
5069
5070	attr = hl_attr(HLF_E);	/* set highlight for error messages */
5071	msg_add_fname(buf,
5072#ifndef UNIX
5073		sfname
5074#else
5075		fname
5076#endif
5077		     );		/* put file name in IObuff with quotes */
5078	if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE)
5079	    IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL;
5080	/* If the error message has the form "is ...", put the error number in
5081	 * front of the file name. */
5082	if (errnum != NULL)
5083	{
5084	    STRMOVE(IObuff + numlen, IObuff);
5085	    mch_memmove(IObuff, errnum, (size_t)numlen);
5086	}
5087	STRCAT(IObuff, errmsg);
5088	emsg(IObuff);
5089	if (errmsg_allocated)
5090	    vim_free(errmsg);
5091
5092	retval = FAIL;
5093	if (end == 0)
5094	{
5095	    MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"),
5096		    attr | MSG_HIST);
5097	    MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"),
5098		    attr | MSG_HIST);
5099
5100	    /* Update the timestamp to avoid an "overwrite changed file"
5101	     * prompt when writing again. */
5102	    if (mch_stat((char *)fname, &st_old) >= 0)
5103	    {
5104		buf_store_time(buf, &st_old, fname);
5105		buf->b_mtime_read = buf->b_mtime;
5106	    }
5107	}
5108    }
5109    msg_scroll = msg_save;
5110
5111#ifdef FEAT_PERSISTENT_UNDO
5112    /*
5113     * When writing the whole file and 'undofile' is set, also write the undo
5114     * file.
5115     */
5116    if (retval == OK && write_undo_file)
5117    {
5118	char_u	    hash[UNDO_HASH_SIZE];
5119
5120	sha256_finish(&sha_ctx, hash);
5121	u_write_undo(NULL, FALSE, buf, hash);
5122    }
5123#endif
5124
5125#ifdef FEAT_AUTOCMD
5126#ifdef FEAT_EVAL
5127    if (!should_abort(retval))
5128#else
5129    if (!got_int)
5130#endif
5131    {
5132	aco_save_T	aco;
5133
5134	write_no_eol_lnum = 0;	/* in case it was set by the previous read */
5135
5136	/*
5137	 * Apply POST autocommands.
5138	 * Careful: The autocommands may call buf_write() recursively!
5139	 */
5140	aucmd_prepbuf(&aco, buf);
5141
5142	if (append)
5143	    apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname,
5144							  FALSE, curbuf, eap);
5145	else if (filtering)
5146	    apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname,
5147							  FALSE, curbuf, eap);
5148	else if (reset_changed && whole)
5149	    apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname,
5150							  FALSE, curbuf, eap);
5151	else
5152	    apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname,
5153							  FALSE, curbuf, eap);
5154
5155	/* restore curwin/curbuf and a few other things */
5156	aucmd_restbuf(&aco);
5157
5158#ifdef FEAT_EVAL
5159	if (aborting())	    /* autocmds may abort script processing */
5160	    retval = FALSE;
5161#endif
5162    }
5163#endif
5164
5165    got_int |= prev_got_int;
5166
5167#ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */
5168    /* Update machine specific information. */
5169    mch_post_buffer_write(buf);
5170#endif
5171    return retval;
5172}
5173
5174/*
5175 * Set the name of the current buffer.  Use when the buffer doesn't have a
5176 * name and a ":r" or ":w" command with a file name is used.
5177 */
5178    static int
5179set_rw_fname(fname, sfname)
5180    char_u	*fname;
5181    char_u	*sfname;
5182{
5183#ifdef FEAT_AUTOCMD
5184    buf_T	*buf = curbuf;
5185
5186    /* It's like the unnamed buffer is deleted.... */
5187    if (curbuf->b_p_bl)
5188	apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5189    apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
5190# ifdef FEAT_EVAL
5191    if (aborting())	    /* autocmds may abort script processing */
5192	return FAIL;
5193# endif
5194    if (curbuf != buf)
5195    {
5196	/* We are in another buffer now, don't do the renaming. */
5197	EMSG(_(e_auchangedbuf));
5198	return FAIL;
5199    }
5200#endif
5201
5202    if (setfname(curbuf, fname, sfname, FALSE) == OK)
5203	curbuf->b_flags |= BF_NOTEDITED;
5204
5205#ifdef FEAT_AUTOCMD
5206    /* ....and a new named one is created */
5207    apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
5208    if (curbuf->b_p_bl)
5209	apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5210# ifdef FEAT_EVAL
5211    if (aborting())	    /* autocmds may abort script processing */
5212	return FAIL;
5213# endif
5214
5215    /* Do filetype detection now if 'filetype' is empty. */
5216    if (*curbuf->b_p_ft == NUL)
5217    {
5218	if (au_has_group((char_u *)"filetypedetect"))
5219	    (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE);
5220	do_modelines(0);
5221    }
5222#endif
5223
5224    return OK;
5225}
5226
5227/*
5228 * Put file name into IObuff with quotes.
5229 */
5230    void
5231msg_add_fname(buf, fname)
5232    buf_T	*buf;
5233    char_u	*fname;
5234{
5235    if (fname == NULL)
5236	fname = (char_u *)"-stdin-";
5237    home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
5238    IObuff[0] = '"';
5239    STRCAT(IObuff, "\" ");
5240}
5241
5242/*
5243 * Append message for text mode to IObuff.
5244 * Return TRUE if something appended.
5245 */
5246    static int
5247msg_add_fileformat(eol_type)
5248    int	    eol_type;
5249{
5250#ifndef USE_CRNL
5251    if (eol_type == EOL_DOS)
5252    {
5253	STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
5254	return TRUE;
5255    }
5256#endif
5257#ifndef USE_CR
5258    if (eol_type == EOL_MAC)
5259    {
5260	STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
5261	return TRUE;
5262    }
5263#endif
5264#if defined(USE_CRNL) || defined(USE_CR)
5265    if (eol_type == EOL_UNIX)
5266    {
5267	STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
5268	return TRUE;
5269    }
5270#endif
5271    return FALSE;
5272}
5273
5274/*
5275 * Append line and character count to IObuff.
5276 */
5277    void
5278msg_add_lines(insert_space, lnum, nchars)
5279    int	    insert_space;
5280    long    lnum;
5281    off_t   nchars;
5282{
5283    char_u  *p;
5284
5285    p = IObuff + STRLEN(IObuff);
5286
5287    if (insert_space)
5288	*p++ = ' ';
5289    if (shortmess(SHM_LINES))
5290	sprintf((char *)p,
5291#ifdef LONG_LONG_OFF_T
5292		"%ldL, %lldC", lnum, nchars
5293#else
5294		/* Explicit typecast avoids warning on Mac OS X 10.6 */
5295		"%ldL, %ldC", lnum, (long)nchars
5296#endif
5297		);
5298    else
5299    {
5300	if (lnum == 1)
5301	    STRCPY(p, _("1 line, "));
5302	else
5303	    sprintf((char *)p, _("%ld lines, "), lnum);
5304	p += STRLEN(p);
5305	if (nchars == 1)
5306	    STRCPY(p, _("1 character"));
5307	else
5308	    sprintf((char *)p,
5309#ifdef LONG_LONG_OFF_T
5310		    _("%lld characters"), nchars
5311#else
5312		    /* Explicit typecast avoids warning on Mac OS X 10.6 */
5313		    _("%ld characters"), (long)nchars
5314#endif
5315		    );
5316    }
5317}
5318
5319/*
5320 * Append message for missing line separator to IObuff.
5321 */
5322    static void
5323msg_add_eol()
5324{
5325    STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
5326}
5327
5328/*
5329 * Check modification time of file, before writing to it.
5330 * The size isn't checked, because using a tool like "gzip" takes care of
5331 * using the same timestamp but can't set the size.
5332 */
5333    static int
5334check_mtime(buf, st)
5335    buf_T		*buf;
5336    struct stat		*st;
5337{
5338    if (buf->b_mtime_read != 0
5339	    && time_differs((long)st->st_mtime, buf->b_mtime_read))
5340    {
5341	msg_scroll = TRUE;	    /* don't overwrite messages here */
5342	msg_silent = 0;		    /* must give this prompt */
5343	/* don't use emsg() here, don't want to flush the buffers */
5344	MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"),
5345						       hl_attr(HLF_E));
5346	if (ask_yesno((char_u *)_("Do you really want to write to it"),
5347								 TRUE) == 'n')
5348	    return FAIL;
5349	msg_scroll = FALSE;	    /* always overwrite the file message now */
5350    }
5351    return OK;
5352}
5353
5354    static int
5355time_differs(t1, t2)
5356    long	t1, t2;
5357{
5358#if defined(__linux__) || defined(MSDOS) || defined(MSWIN)
5359    /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store
5360     * the seconds.  Since the roundoff is done when flushing the inode, the
5361     * time may change unexpectedly by one second!!! */
5362    return (t1 - t2 > 1 || t2 - t1 > 1);
5363#else
5364    return (t1 != t2);
5365#endif
5366}
5367
5368/*
5369 * Call write() to write a number of bytes to the file.
5370 * Also handles encryption and 'encoding' conversion.
5371 *
5372 * Return FAIL for failure, OK otherwise.
5373 */
5374    static int
5375buf_write_bytes(ip)
5376    struct bw_info *ip;
5377{
5378    int		wlen;
5379    char_u	*buf = ip->bw_buf;	/* data to write */
5380    int		len = ip->bw_len;	/* length of data */
5381#ifdef HAS_BW_FLAGS
5382    int		flags = ip->bw_flags;	/* extra flags */
5383#endif
5384
5385#ifdef FEAT_MBYTE
5386    /*
5387     * Skip conversion when writing the crypt magic number or the BOM.
5388     */
5389    if (!(flags & FIO_NOCONVERT))
5390    {
5391	char_u		*p;
5392	unsigned	c;
5393	int		n;
5394
5395	if (flags & FIO_UTF8)
5396	{
5397	    /*
5398	     * Convert latin1 in the buffer to UTF-8 in the file.
5399	     */
5400	    p = ip->bw_conv_buf;	/* translate to buffer */
5401	    for (wlen = 0; wlen < len; ++wlen)
5402		p += utf_char2bytes(buf[wlen], p);
5403	    buf = ip->bw_conv_buf;
5404	    len = (int)(p - ip->bw_conv_buf);
5405	}
5406	else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1))
5407	{
5408	    /*
5409	     * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or
5410	     * Latin1 chars in the file.
5411	     */
5412	    if (flags & FIO_LATIN1)
5413		p = buf;	/* translate in-place (can only get shorter) */
5414	    else
5415		p = ip->bw_conv_buf;	/* translate to buffer */
5416	    for (wlen = 0; wlen < len; wlen += n)
5417	    {
5418		if (wlen == 0 && ip->bw_restlen != 0)
5419		{
5420		    int		l;
5421
5422		    /* Use remainder of previous call.  Append the start of
5423		     * buf[] to get a full sequence.  Might still be too
5424		     * short! */
5425		    l = CONV_RESTLEN - ip->bw_restlen;
5426		    if (l > len)
5427			l = len;
5428		    mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l);
5429		    n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l);
5430		    if (n > ip->bw_restlen + len)
5431		    {
5432			/* We have an incomplete byte sequence at the end to
5433			 * be written.  We can't convert it without the
5434			 * remaining bytes.  Keep them for the next call. */
5435			if (ip->bw_restlen + len > CONV_RESTLEN)
5436			    return FAIL;
5437			ip->bw_restlen += len;
5438			break;
5439		    }
5440		    if (n > 1)
5441			c = utf_ptr2char(ip->bw_rest);
5442		    else
5443			c = ip->bw_rest[0];
5444		    if (n >= ip->bw_restlen)
5445		    {
5446			n -= ip->bw_restlen;
5447			ip->bw_restlen = 0;
5448		    }
5449		    else
5450		    {
5451			ip->bw_restlen -= n;
5452			mch_memmove(ip->bw_rest, ip->bw_rest + n,
5453						      (size_t)ip->bw_restlen);
5454			n = 0;
5455		    }
5456		}
5457		else
5458		{
5459		    n = utf_ptr2len_len(buf + wlen, len - wlen);
5460		    if (n > len - wlen)
5461		    {
5462			/* We have an incomplete byte sequence at the end to
5463			 * be written.  We can't convert it without the
5464			 * remaining bytes.  Keep them for the next call. */
5465			if (len - wlen > CONV_RESTLEN)
5466			    return FAIL;
5467			ip->bw_restlen = len - wlen;
5468			mch_memmove(ip->bw_rest, buf + wlen,
5469						      (size_t)ip->bw_restlen);
5470			break;
5471		    }
5472		    if (n > 1)
5473			c = utf_ptr2char(buf + wlen);
5474		    else
5475			c = buf[wlen];
5476		}
5477
5478		if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error)
5479		{
5480		    ip->bw_conv_error = TRUE;
5481		    ip->bw_conv_error_lnum = ip->bw_start_lnum;
5482		}
5483		if (c == NL)
5484		    ++ip->bw_start_lnum;
5485	    }
5486	    if (flags & FIO_LATIN1)
5487		len = (int)(p - buf);
5488	    else
5489	    {
5490		buf = ip->bw_conv_buf;
5491		len = (int)(p - ip->bw_conv_buf);
5492	    }
5493	}
5494
5495# ifdef WIN3264
5496	else if (flags & FIO_CODEPAGE)
5497	{
5498	    /*
5499	     * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows
5500	     * codepage.
5501	     */
5502	    char_u	*from;
5503	    size_t	fromlen;
5504	    char_u	*to;
5505	    int		u8c;
5506	    BOOL	bad = FALSE;
5507	    int		needed;
5508
5509	    if (ip->bw_restlen > 0)
5510	    {
5511		/* Need to concatenate the remainder of the previous call and
5512		 * the bytes of the current call.  Use the end of the
5513		 * conversion buffer for this. */
5514		fromlen = len + ip->bw_restlen;
5515		from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5516		mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
5517		mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
5518	    }
5519	    else
5520	    {
5521		from = buf;
5522		fromlen = len;
5523	    }
5524
5525	    to = ip->bw_conv_buf;
5526	    if (enc_utf8)
5527	    {
5528		/* Convert from UTF-8 to UCS-2, to the start of the buffer.
5529		 * The buffer has been allocated to be big enough. */
5530		while (fromlen > 0)
5531		{
5532		    n = (int)utf_ptr2len_len(from, (int)fromlen);
5533		    if (n > (int)fromlen)	/* incomplete byte sequence */
5534			break;
5535		    u8c = utf_ptr2char(from);
5536		    *to++ = (u8c & 0xff);
5537		    *to++ = (u8c >> 8);
5538		    fromlen -= n;
5539		    from += n;
5540		}
5541
5542		/* Copy remainder to ip->bw_rest[] to be used for the next
5543		 * call. */
5544		if (fromlen > CONV_RESTLEN)
5545		{
5546		    /* weird overlong sequence */
5547		    ip->bw_conv_error = TRUE;
5548		    return FAIL;
5549		}
5550		mch_memmove(ip->bw_rest, from, fromlen);
5551		ip->bw_restlen = (int)fromlen;
5552	    }
5553	    else
5554	    {
5555		/* Convert from enc_codepage to UCS-2, to the start of the
5556		 * buffer.  The buffer has been allocated to be big enough. */
5557		ip->bw_restlen = 0;
5558		needed = MultiByteToWideChar(enc_codepage,
5559			     MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen,
5560								     NULL, 0);
5561		if (needed == 0)
5562		{
5563		    /* When conversion fails there may be a trailing byte. */
5564		    needed = MultiByteToWideChar(enc_codepage,
5565			 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1,
5566								     NULL, 0);
5567		    if (needed == 0)
5568		    {
5569			/* Conversion doesn't work. */
5570			ip->bw_conv_error = TRUE;
5571			return FAIL;
5572		    }
5573		    /* Save the trailing byte for the next call. */
5574		    ip->bw_rest[0] = from[fromlen - 1];
5575		    ip->bw_restlen = 1;
5576		}
5577		needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS,
5578				(LPCSTR)from, (int)(fromlen - ip->bw_restlen),
5579							  (LPWSTR)to, needed);
5580		if (needed == 0)
5581		{
5582		    /* Safety check: Conversion doesn't work. */
5583		    ip->bw_conv_error = TRUE;
5584		    return FAIL;
5585		}
5586		to += needed * 2;
5587	    }
5588
5589	    fromlen = to - ip->bw_conv_buf;
5590	    buf = to;
5591#  ifdef CP_UTF8	/* VC 4.1 doesn't define CP_UTF8 */
5592	    if (FIO_GET_CP(flags) == CP_UTF8)
5593	    {
5594		/* Convert from UCS-2 to UTF-8, using the remainder of the
5595		 * conversion buffer.  Fails when out of space. */
5596		for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2)
5597		{
5598		    u8c = *from++;
5599		    u8c += (*from++ << 8);
5600		    to += utf_char2bytes(u8c, to);
5601		    if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen)
5602		    {
5603			ip->bw_conv_error = TRUE;
5604			return FAIL;
5605		    }
5606		}
5607		len = (int)(to - buf);
5608	    }
5609	    else
5610#endif
5611	    {
5612		/* Convert from UCS-2 to the codepage, using the remainder of
5613		 * the conversion buffer.  If the conversion uses the default
5614		 * character "0", the data doesn't fit in this encoding, so
5615		 * fail. */
5616		len = WideCharToMultiByte(FIO_GET_CP(flags), 0,
5617			(LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR),
5618			(LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0,
5619									&bad);
5620		if (bad)
5621		{
5622		    ip->bw_conv_error = TRUE;
5623		    return FAIL;
5624		}
5625	    }
5626	}
5627# endif
5628
5629# ifdef MACOS_CONVERT
5630	else if (flags & FIO_MACROMAN)
5631	{
5632	    /*
5633	     * Convert UTF-8 or latin1 to Apple MacRoman.
5634	     */
5635	    char_u	*from;
5636	    size_t	fromlen;
5637
5638	    if (ip->bw_restlen > 0)
5639	    {
5640		/* Need to concatenate the remainder of the previous call and
5641		 * the bytes of the current call.  Use the end of the
5642		 * conversion buffer for this. */
5643		fromlen = len + ip->bw_restlen;
5644		from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5645		mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
5646		mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
5647	    }
5648	    else
5649	    {
5650		from = buf;
5651		fromlen = len;
5652	    }
5653
5654	    if (enc2macroman(from, fromlen,
5655			ip->bw_conv_buf, &len, ip->bw_conv_buflen,
5656			ip->bw_rest, &ip->bw_restlen) == FAIL)
5657	    {
5658		ip->bw_conv_error = TRUE;
5659		return FAIL;
5660	    }
5661	    buf = ip->bw_conv_buf;
5662	}
5663# endif
5664
5665# ifdef USE_ICONV
5666	if (ip->bw_iconv_fd != (iconv_t)-1)
5667	{
5668	    const char	*from;
5669	    size_t	fromlen;
5670	    char	*to;
5671	    size_t	tolen;
5672
5673	    /* Convert with iconv(). */
5674	    if (ip->bw_restlen > 0)
5675	    {
5676		char *fp;
5677
5678		/* Need to concatenate the remainder of the previous call and
5679		 * the bytes of the current call.  Use the end of the
5680		 * conversion buffer for this. */
5681		fromlen = len + ip->bw_restlen;
5682		fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5683		mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen);
5684		mch_memmove(fp + ip->bw_restlen, buf, (size_t)len);
5685		from = fp;
5686		tolen = ip->bw_conv_buflen - fromlen;
5687	    }
5688	    else
5689	    {
5690		from = (const char *)buf;
5691		fromlen = len;
5692		tolen = ip->bw_conv_buflen;
5693	    }
5694	    to = (char *)ip->bw_conv_buf;
5695
5696	    if (ip->bw_first)
5697	    {
5698		size_t	save_len = tolen;
5699
5700		/* output the initial shift state sequence */
5701		(void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen);
5702
5703		/* There is a bug in iconv() on Linux (which appears to be
5704		 * wide-spread) which sets "to" to NULL and messes up "tolen".
5705		 */
5706		if (to == NULL)
5707		{
5708		    to = (char *)ip->bw_conv_buf;
5709		    tolen = save_len;
5710		}
5711		ip->bw_first = FALSE;
5712	    }
5713
5714	    /*
5715	     * If iconv() has an error or there is not enough room, fail.
5716	     */
5717	    if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen)
5718			== (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
5719						    || fromlen > CONV_RESTLEN)
5720	    {
5721		ip->bw_conv_error = TRUE;
5722		return FAIL;
5723	    }
5724
5725	    /* copy remainder to ip->bw_rest[] to be used for the next call. */
5726	    if (fromlen > 0)
5727		mch_memmove(ip->bw_rest, (void *)from, fromlen);
5728	    ip->bw_restlen = (int)fromlen;
5729
5730	    buf = ip->bw_conv_buf;
5731	    len = (int)((char_u *)to - ip->bw_conv_buf);
5732	}
5733# endif
5734    }
5735#endif /* FEAT_MBYTE */
5736
5737#ifdef FEAT_CRYPT
5738    if (flags & FIO_ENCRYPTED)		/* encrypt the data */
5739	crypt_encode(buf, len, buf);
5740#endif
5741
5742    /* Repeat the write(), it may be interrupted by a signal. */
5743    while (len > 0)
5744    {
5745	wlen = vim_write(ip->bw_fd, buf, len);
5746	if (wlen <= 0)		    /* error! */
5747	    return FAIL;
5748	len -= wlen;
5749	buf += wlen;
5750    }
5751    return OK;
5752}
5753
5754#ifdef FEAT_MBYTE
5755/*
5756 * Convert a Unicode character to bytes.
5757 * Return TRUE for an error, FALSE when it's OK.
5758 */
5759    static int
5760ucs2bytes(c, pp, flags)
5761    unsigned	c;		/* in: character */
5762    char_u	**pp;		/* in/out: pointer to result */
5763    int		flags;		/* FIO_ flags */
5764{
5765    char_u	*p = *pp;
5766    int		error = FALSE;
5767    int		cc;
5768
5769
5770    if (flags & FIO_UCS4)
5771    {
5772	if (flags & FIO_ENDIAN_L)
5773	{
5774	    *p++ = c;
5775	    *p++ = (c >> 8);
5776	    *p++ = (c >> 16);
5777	    *p++ = (c >> 24);
5778	}
5779	else
5780	{
5781	    *p++ = (c >> 24);
5782	    *p++ = (c >> 16);
5783	    *p++ = (c >> 8);
5784	    *p++ = c;
5785	}
5786    }
5787    else if (flags & (FIO_UCS2 | FIO_UTF16))
5788    {
5789	if (c >= 0x10000)
5790	{
5791	    if (flags & FIO_UTF16)
5792	    {
5793		/* Make two words, ten bits of the character in each.  First
5794		 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */
5795		c -= 0x10000;
5796		if (c >= 0x100000)
5797		    error = TRUE;
5798		cc = ((c >> 10) & 0x3ff) + 0xd800;
5799		if (flags & FIO_ENDIAN_L)
5800		{
5801		    *p++ = cc;
5802		    *p++ = ((unsigned)cc >> 8);
5803		}
5804		else
5805		{
5806		    *p++ = ((unsigned)cc >> 8);
5807		    *p++ = cc;
5808		}
5809		c = (c & 0x3ff) + 0xdc00;
5810	    }
5811	    else
5812		error = TRUE;
5813	}
5814	if (flags & FIO_ENDIAN_L)
5815	{
5816	    *p++ = c;
5817	    *p++ = (c >> 8);
5818	}
5819	else
5820	{
5821	    *p++ = (c >> 8);
5822	    *p++ = c;
5823	}
5824    }
5825    else    /* Latin1 */
5826    {
5827	if (c >= 0x100)
5828	{
5829	    error = TRUE;
5830	    *p++ = 0xBF;
5831	}
5832	else
5833	    *p++ = c;
5834    }
5835
5836    *pp = p;
5837    return error;
5838}
5839
5840/*
5841 * Return TRUE if file encoding "fenc" requires conversion from or to
5842 * 'encoding'.
5843 */
5844    static int
5845need_conversion(fenc)
5846    char_u	*fenc;
5847{
5848    int		same_encoding;
5849    int		enc_flags;
5850    int		fenc_flags;
5851
5852    if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
5853    {
5854	same_encoding = TRUE;
5855	fenc_flags = 0;
5856    }
5857    else
5858    {
5859	/* Ignore difference between "ansi" and "latin1", "ucs-4" and
5860	 * "ucs-4be", etc. */
5861	enc_flags = get_fio_flags(p_enc);
5862	fenc_flags = get_fio_flags(fenc);
5863	same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
5864    }
5865    if (same_encoding)
5866    {
5867	/* Specified encoding matches with 'encoding'.  This requires
5868	 * conversion when 'encoding' is Unicode but not UTF-8. */
5869	return enc_unicode != 0;
5870    }
5871
5872    /* Encodings differ.  However, conversion is not needed when 'enc' is any
5873     * Unicode encoding and the file is UTF-8. */
5874    return !(enc_utf8 && fenc_flags == FIO_UTF8);
5875}
5876
5877/*
5878 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
5879 * internal conversion.
5880 * if "ptr" is an empty string, use 'encoding'.
5881 */
5882    static int
5883get_fio_flags(ptr)
5884    char_u	*ptr;
5885{
5886    int		prop;
5887
5888    if (*ptr == NUL)
5889	ptr = p_enc;
5890
5891    prop = enc_canon_props(ptr);
5892    if (prop & ENC_UNICODE)
5893    {
5894	if (prop & ENC_2BYTE)
5895	{
5896	    if (prop & ENC_ENDIAN_L)
5897		return FIO_UCS2 | FIO_ENDIAN_L;
5898	    return FIO_UCS2;
5899	}
5900	if (prop & ENC_4BYTE)
5901	{
5902	    if (prop & ENC_ENDIAN_L)
5903		return FIO_UCS4 | FIO_ENDIAN_L;
5904	    return FIO_UCS4;
5905	}
5906	if (prop & ENC_2WORD)
5907	{
5908	    if (prop & ENC_ENDIAN_L)
5909		return FIO_UTF16 | FIO_ENDIAN_L;
5910	    return FIO_UTF16;
5911	}
5912	return FIO_UTF8;
5913    }
5914    if (prop & ENC_LATIN1)
5915	return FIO_LATIN1;
5916    /* must be ENC_DBCS, requires iconv() */
5917    return 0;
5918}
5919
5920#ifdef WIN3264
5921/*
5922 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
5923 * for the conversion MS-Windows can do for us.  Also accept "utf-8".
5924 * Used for conversion between 'encoding' and 'fileencoding'.
5925 */
5926    static int
5927get_win_fio_flags(ptr)
5928    char_u	*ptr;
5929{
5930    int		cp;
5931
5932    /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */
5933    if (!enc_utf8 && enc_codepage <= 0)
5934	return 0;
5935
5936    cp = encname2codepage(ptr);
5937    if (cp == 0)
5938    {
5939#  ifdef CP_UTF8	/* VC 4.1 doesn't define CP_UTF8 */
5940	if (STRCMP(ptr, "utf-8") == 0)
5941	    cp = CP_UTF8;
5942	else
5943#  endif
5944	    return 0;
5945    }
5946    return FIO_PUT_CP(cp) | FIO_CODEPAGE;
5947}
5948#endif
5949
5950#ifdef MACOS_X
5951/*
5952 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
5953 * needed for the internal conversion to/from utf-8 or latin1.
5954 */
5955    static int
5956get_mac_fio_flags(ptr)
5957    char_u	*ptr;
5958{
5959    if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
5960				     && (enc_canon_props(ptr) & ENC_MACROMAN))
5961	return FIO_MACROMAN;
5962    return 0;
5963}
5964#endif
5965
5966/*
5967 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
5968 * "size" must be at least 2.
5969 * Return the name of the encoding and set "*lenp" to the length.
5970 * Returns NULL when no BOM found.
5971 */
5972    static char_u *
5973check_for_bom(p, size, lenp, flags)
5974    char_u	*p;
5975    long	size;
5976    int		*lenp;
5977    int		flags;
5978{
5979    char	*name = NULL;
5980    int		len = 2;
5981
5982    if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
5983	    && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
5984    {
5985	name = "utf-8";		/* EF BB BF */
5986	len = 3;
5987    }
5988    else if (p[0] == 0xff && p[1] == 0xfe)
5989    {
5990	if (size >= 4 && p[2] == 0 && p[3] == 0
5991	    && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
5992	{
5993	    name = "ucs-4le";	/* FF FE 00 00 */
5994	    len = 4;
5995	}
5996	else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
5997	    name = "ucs-2le";	/* FF FE */
5998	else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
5999	    /* utf-16le is preferred, it also works for ucs-2le text */
6000	    name = "utf-16le";	/* FF FE */
6001    }
6002    else if (p[0] == 0xfe && p[1] == 0xff
6003	    && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
6004    {
6005	/* Default to utf-16, it works also for ucs-2 text. */
6006	if (flags == FIO_UCS2)
6007	    name = "ucs-2";	/* FE FF */
6008	else
6009	    name = "utf-16";	/* FE FF */
6010    }
6011    else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
6012	    && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
6013    {
6014	name = "ucs-4";		/* 00 00 FE FF */
6015	len = 4;
6016    }
6017
6018    *lenp = len;
6019    return (char_u *)name;
6020}
6021
6022/*
6023 * Generate a BOM in "buf[4]" for encoding "name".
6024 * Return the length of the BOM (zero when no BOM).
6025 */
6026    static int
6027make_bom(buf, name)
6028    char_u	*buf;
6029    char_u	*name;
6030{
6031    int		flags;
6032    char_u	*p;
6033
6034    flags = get_fio_flags(name);
6035
6036    /* Can't put a BOM in a non-Unicode file. */
6037    if (flags == FIO_LATIN1 || flags == 0)
6038	return 0;
6039
6040    if (flags == FIO_UTF8)	/* UTF-8 */
6041    {
6042	buf[0] = 0xef;
6043	buf[1] = 0xbb;
6044	buf[2] = 0xbf;
6045	return 3;
6046    }
6047    p = buf;
6048    (void)ucs2bytes(0xfeff, &p, flags);
6049    return (int)(p - buf);
6050}
6051#endif
6052
6053#if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \
6054    defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO)
6055/*
6056 * Try to find a shortname by comparing the fullname with the current
6057 * directory.
6058 * Returns "full_path" or pointer into "full_path" if shortened.
6059 */
6060    char_u *
6061shorten_fname1(full_path)
6062    char_u	*full_path;
6063{
6064    char_u	dirname[MAXPATHL];
6065    char_u	*p = full_path;
6066
6067    if (mch_dirname(dirname, MAXPATHL) == OK)
6068    {
6069	p = shorten_fname(full_path, dirname);
6070	if (p == NULL || *p == NUL)
6071	    p = full_path;
6072    }
6073    return p;
6074}
6075#endif
6076
6077/*
6078 * Try to find a shortname by comparing the fullname with the current
6079 * directory.
6080 * Returns NULL if not shorter name possible, pointer into "full_path"
6081 * otherwise.
6082 */
6083    char_u *
6084shorten_fname(full_path, dir_name)
6085    char_u	*full_path;
6086    char_u	*dir_name;
6087{
6088    int		len;
6089    char_u	*p;
6090
6091    if (full_path == NULL)
6092	return NULL;
6093    len = (int)STRLEN(dir_name);
6094    if (fnamencmp(dir_name, full_path, len) == 0)
6095    {
6096	p = full_path + len;
6097#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
6098	/*
6099	 * MSDOS: when a file is in the root directory, dir_name will end in a
6100	 * slash, since C: by itself does not define a specific dir. In this
6101	 * case p may already be correct. <negri>
6102	 */
6103	if (!((len > 2) && (*(p - 2) == ':')))
6104#endif
6105	{
6106	    if (vim_ispathsep(*p))
6107		++p;
6108#ifndef VMS   /* the path separator is always part of the path */
6109	    else
6110		p = NULL;
6111#endif
6112	}
6113    }
6114#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
6115    /*
6116     * When using a file in the current drive, remove the drive name:
6117     * "A:\dir\file" -> "\dir\file".  This helps when moving a session file on
6118     * a floppy from "A:\dir" to "B:\dir".
6119     */
6120    else if (len > 3
6121	    && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
6122	    && full_path[1] == ':'
6123	    && vim_ispathsep(full_path[2]))
6124	p = full_path + 2;
6125#endif
6126    else
6127	p = NULL;
6128    return p;
6129}
6130
6131/*
6132 * Shorten filenames for all buffers.
6133 * When "force" is TRUE: Use full path from now on for files currently being
6134 * edited, both for file name and swap file name.  Try to shorten the file
6135 * names a bit, if safe to do so.
6136 * When "force" is FALSE: Only try to shorten absolute file names.
6137 * For buffers that have buftype "nofile" or "scratch": never change the file
6138 * name.
6139 */
6140    void
6141shorten_fnames(force)
6142    int		force;
6143{
6144    char_u	dirname[MAXPATHL];
6145    buf_T	*buf;
6146    char_u	*p;
6147
6148    mch_dirname(dirname, MAXPATHL);
6149    for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6150    {
6151	if (buf->b_fname != NULL
6152#ifdef FEAT_QUICKFIX
6153		&& !bt_nofile(buf)
6154#endif
6155		&& !path_with_url(buf->b_fname)
6156		&& (force
6157		    || buf->b_sfname == NULL
6158		    || mch_isFullName(buf->b_sfname)))
6159	{
6160	    vim_free(buf->b_sfname);
6161	    buf->b_sfname = NULL;
6162	    p = shorten_fname(buf->b_ffname, dirname);
6163	    if (p != NULL)
6164	    {
6165		buf->b_sfname = vim_strsave(p);
6166		buf->b_fname = buf->b_sfname;
6167	    }
6168	    if (p == NULL || buf->b_fname == NULL)
6169		buf->b_fname = buf->b_ffname;
6170	}
6171
6172	/* Always make the swap file name a full path, a "nofile" buffer may
6173	 * also have a swap file. */
6174	mf_fullname(buf->b_ml.ml_mfp);
6175    }
6176#ifdef FEAT_WINDOWS
6177    status_redraw_all();
6178    redraw_tabline = TRUE;
6179#endif
6180}
6181
6182#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
6183	|| defined(FEAT_GUI_MSWIN) \
6184	|| defined(FEAT_GUI_MAC) \
6185	|| defined(PROTO)
6186/*
6187 * Shorten all filenames in "fnames[count]" by current directory.
6188 */
6189    void
6190shorten_filenames(fnames, count)
6191    char_u	**fnames;
6192    int		count;
6193{
6194    int		i;
6195    char_u	dirname[MAXPATHL];
6196    char_u	*p;
6197
6198    if (fnames == NULL || count < 1)
6199	return;
6200    mch_dirname(dirname, sizeof(dirname));
6201    for (i = 0; i < count; ++i)
6202    {
6203	if ((p = shorten_fname(fnames[i], dirname)) != NULL)
6204	{
6205	    /* shorten_fname() returns pointer in given "fnames[i]".  If free
6206	     * "fnames[i]" first, "p" becomes invalid.  So we need to copy
6207	     * "p" first then free fnames[i]. */
6208	    p = vim_strsave(p);
6209	    vim_free(fnames[i]);
6210	    fnames[i] = p;
6211	}
6212    }
6213}
6214#endif
6215
6216/*
6217 * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
6218 * fo_o_h.ext for MSDOS or when shortname option set.
6219 *
6220 * Assumed that fname is a valid name found in the filesystem we assure that
6221 * the return value is a different name and ends in 'ext'.
6222 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
6223 * characters otherwise.
6224 * Space for the returned name is allocated, must be freed later.
6225 * Returns NULL when out of memory.
6226 */
6227    char_u *
6228modname(fname, ext, prepend_dot)
6229    char_u *fname, *ext;
6230    int	    prepend_dot;	/* may prepend a '.' to file name */
6231{
6232    return buf_modname(
6233#ifdef SHORT_FNAME
6234			TRUE,
6235#else
6236			(curbuf->b_p_sn || curbuf->b_shortname),
6237#endif
6238						     fname, ext, prepend_dot);
6239}
6240
6241    char_u *
6242buf_modname(shortname, fname, ext, prepend_dot)
6243    int	    shortname;		/* use 8.3 file name */
6244    char_u  *fname, *ext;
6245    int	    prepend_dot;	/* may prepend a '.' to file name */
6246{
6247    char_u	*retval;
6248    char_u	*s;
6249    char_u	*e;
6250    char_u	*ptr;
6251    int		fnamelen, extlen;
6252
6253    extlen = (int)STRLEN(ext);
6254
6255    /*
6256     * If there is no file name we must get the name of the current directory
6257     * (we need the full path in case :cd is used).
6258     */
6259    if (fname == NULL || *fname == NUL)
6260    {
6261	retval = alloc((unsigned)(MAXPATHL + extlen + 3));
6262	if (retval == NULL)
6263	    return NULL;
6264	if (mch_dirname(retval, MAXPATHL) == FAIL ||
6265				     (fnamelen = (int)STRLEN(retval)) == 0)
6266	{
6267	    vim_free(retval);
6268	    return NULL;
6269	}
6270	if (!after_pathsep(retval, retval + fnamelen))
6271	{
6272	    retval[fnamelen++] = PATHSEP;
6273	    retval[fnamelen] = NUL;
6274	}
6275#ifndef SHORT_FNAME
6276	prepend_dot = FALSE;	    /* nothing to prepend a dot to */
6277#endif
6278    }
6279    else
6280    {
6281	fnamelen = (int)STRLEN(fname);
6282	retval = alloc((unsigned)(fnamelen + extlen + 3));
6283	if (retval == NULL)
6284	    return NULL;
6285	STRCPY(retval, fname);
6286#ifdef VMS
6287	vms_remove_version(retval); /* we do not need versions here */
6288#endif
6289    }
6290
6291    /*
6292     * search backwards until we hit a '/', '\' or ':' replacing all '.'
6293     * by '_' for MSDOS or when shortname option set and ext starts with a dot.
6294     * Then truncate what is after the '/', '\' or ':' to 8 characters for
6295     * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
6296     */
6297    for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr))
6298    {
6299#ifndef RISCOS
6300	if (*ext == '.'
6301# ifdef USE_LONG_FNAME
6302		    && (!USE_LONG_FNAME || shortname)
6303# else
6304#  ifndef SHORT_FNAME
6305		    && shortname
6306#  endif
6307# endif
6308								)
6309	    if (*ptr == '.')	/* replace '.' by '_' */
6310		*ptr = '_';
6311#endif
6312	if (vim_ispathsep(*ptr))
6313	{
6314	    ++ptr;
6315	    break;
6316	}
6317    }
6318
6319    /* the file name has at most BASENAMELEN characters. */
6320#ifndef SHORT_FNAME
6321    if (STRLEN(ptr) > (unsigned)BASENAMELEN)
6322	ptr[BASENAMELEN] = '\0';
6323#endif
6324
6325    s = ptr + STRLEN(ptr);
6326
6327    /*
6328     * For 8.3 file names we may have to reduce the length.
6329     */
6330#ifdef USE_LONG_FNAME
6331    if (!USE_LONG_FNAME || shortname)
6332#else
6333# ifndef SHORT_FNAME
6334    if (shortname)
6335# endif
6336#endif
6337    {
6338	/*
6339	 * If there is no file name, or the file name ends in '/', and the
6340	 * extension starts with '.', put a '_' before the dot, because just
6341	 * ".ext" is invalid.
6342	 */
6343	if (fname == NULL || *fname == NUL
6344				   || vim_ispathsep(fname[STRLEN(fname) - 1]))
6345	{
6346#ifdef RISCOS
6347	    if (*ext == '/')
6348#else
6349	    if (*ext == '.')
6350#endif
6351		*s++ = '_';
6352	}
6353	/*
6354	 * If the extension starts with '.', truncate the base name at 8
6355	 * characters
6356	 */
6357#ifdef RISCOS
6358	/* We normally use '/', but swap files are '_' */
6359	else if (*ext == '/' || *ext == '_')
6360#else
6361	else if (*ext == '.')
6362#endif
6363	{
6364	    if ((size_t)(s - ptr) > (size_t)8)
6365	    {
6366		s = ptr + 8;
6367		*s = '\0';
6368	    }
6369	}
6370	/*
6371	 * If the extension doesn't start with '.', and the file name
6372	 * doesn't have an extension yet, append a '.'
6373	 */
6374#ifdef RISCOS
6375	else if ((e = vim_strchr(ptr, '/')) == NULL)
6376	    *s++ = '/';
6377#else
6378	else if ((e = vim_strchr(ptr, '.')) == NULL)
6379	    *s++ = '.';
6380#endif
6381	/*
6382	 * If the extension doesn't start with '.', and there already is an
6383	 * extension, it may need to be truncated
6384	 */
6385	else if ((int)STRLEN(e) + extlen > 4)
6386	    s = e + 4 - extlen;
6387    }
6388#if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264)
6389    /*
6390     * If there is no file name, and the extension starts with '.', put a
6391     * '_' before the dot, because just ".ext" may be invalid if it's on a
6392     * FAT partition, and on HPFS it doesn't matter.
6393     */
6394    else if ((fname == NULL || *fname == NUL) && *ext == '.')
6395	*s++ = '_';
6396#endif
6397
6398    /*
6399     * Append the extension.
6400     * ext can start with '.' and cannot exceed 3 more characters.
6401     */
6402    STRCPY(s, ext);
6403
6404#ifndef SHORT_FNAME
6405    /*
6406     * Prepend the dot.
6407     */
6408    if (prepend_dot && !shortname && *(e = gettail(retval)) !=
6409#ifdef RISCOS
6410	    '/'
6411#else
6412	    '.'
6413#endif
6414#ifdef USE_LONG_FNAME
6415	    && USE_LONG_FNAME
6416#endif
6417				)
6418    {
6419	STRMOVE(e + 1, e);
6420#ifdef RISCOS
6421	*e = '/';
6422#else
6423	*e = '.';
6424#endif
6425    }
6426#endif
6427
6428    /*
6429     * Check that, after appending the extension, the file name is really
6430     * different.
6431     */
6432    if (fname != NULL && STRCMP(fname, retval) == 0)
6433    {
6434	/* we search for a character that can be replaced by '_' */
6435	while (--s >= ptr)
6436	{
6437	    if (*s != '_')
6438	    {
6439		*s = '_';
6440		break;
6441	    }
6442	}
6443	if (s < ptr)	/* fname was "________.<ext>", how tricky! */
6444	    *ptr = 'v';
6445    }
6446    return retval;
6447}
6448
6449/*
6450 * Like fgets(), but if the file line is too long, it is truncated and the
6451 * rest of the line is thrown away.  Returns TRUE for end-of-file.
6452 */
6453    int
6454vim_fgets(buf, size, fp)
6455    char_u	*buf;
6456    int		size;
6457    FILE	*fp;
6458{
6459    char	*eof;
6460#define FGETS_SIZE 200
6461    char	tbuf[FGETS_SIZE];
6462
6463    buf[size - 2] = NUL;
6464#ifdef USE_CR
6465    eof = fgets_cr((char *)buf, size, fp);
6466#else
6467    eof = fgets((char *)buf, size, fp);
6468#endif
6469    if (buf[size - 2] != NUL && buf[size - 2] != '\n')
6470    {
6471	buf[size - 1] = NUL;	    /* Truncate the line */
6472
6473	/* Now throw away the rest of the line: */
6474	do
6475	{
6476	    tbuf[FGETS_SIZE - 2] = NUL;
6477#ifdef USE_CR
6478	    ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp);
6479#else
6480	    ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
6481#endif
6482	} while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
6483    }
6484    return (eof == NULL);
6485}
6486
6487#if defined(USE_CR) || defined(PROTO)
6488/*
6489 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF.
6490 * Returns TRUE for end-of-file.
6491 * Only used for the Mac, because it's much slower than vim_fgets().
6492 */
6493    int
6494tag_fgets(buf, size, fp)
6495    char_u	*buf;
6496    int		size;
6497    FILE	*fp;
6498{
6499    int		i = 0;
6500    int		c;
6501    int		eof = FALSE;
6502
6503    for (;;)
6504    {
6505	c = fgetc(fp);
6506	if (c == EOF)
6507	{
6508	    eof = TRUE;
6509	    break;
6510	}
6511	if (c == '\r')
6512	{
6513	    /* Always store a NL for end-of-line. */
6514	    if (i < size - 1)
6515		buf[i++] = '\n';
6516	    c = fgetc(fp);
6517	    if (c != '\n')	/* Macintosh format: single CR. */
6518		ungetc(c, fp);
6519	    break;
6520	}
6521	if (i < size - 1)
6522	    buf[i++] = c;
6523	if (c == '\n')
6524	    break;
6525    }
6526    buf[i] = NUL;
6527    return eof;
6528}
6529#endif
6530
6531/*
6532 * rename() only works if both files are on the same file system, this
6533 * function will (attempts to?) copy the file across if rename fails -- webb
6534 * Return -1 for failure, 0 for success.
6535 */
6536    int
6537vim_rename(from, to)
6538    char_u	*from;
6539    char_u	*to;
6540{
6541    int		fd_in;
6542    int		fd_out;
6543    int		n;
6544    char	*errmsg = NULL;
6545    char	*buffer;
6546#ifdef AMIGA
6547    BPTR	flock;
6548#endif
6549    struct stat	st;
6550    long	perm;
6551#ifdef HAVE_ACL
6552    vim_acl_T	acl;		/* ACL from original file */
6553#endif
6554#if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME)
6555    int		use_tmp_file = FALSE;
6556#endif
6557
6558    /*
6559     * When the names are identical, there is nothing to do.  When they refer
6560     * to the same file (ignoring case and slash/backslash differences) but
6561     * the file name differs we need to go through a temp file.
6562     */
6563    if (fnamecmp(from, to) == 0)
6564    {
6565#ifdef CASE_INSENSITIVE_FILENAME
6566	if (STRCMP(gettail(from), gettail(to)) != 0)
6567	    use_tmp_file = TRUE;
6568	else
6569#endif
6570	    return 0;
6571    }
6572
6573    /*
6574     * Fail if the "from" file doesn't exist.  Avoids that "to" is deleted.
6575     */
6576    if (mch_stat((char *)from, &st) < 0)
6577	return -1;
6578
6579#ifdef UNIX
6580    {
6581	struct stat	st_to;
6582
6583	/* It's possible for the source and destination to be the same file.
6584	 * This happens when "from" and "to" differ in case and are on a FAT32
6585	 * filesystem.  In that case go through a temp file name. */
6586	if (mch_stat((char *)to, &st_to) >= 0
6587		&& st.st_dev == st_to.st_dev
6588		&& st.st_ino == st_to.st_ino)
6589	    use_tmp_file = TRUE;
6590    }
6591#endif
6592
6593#if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME)
6594    if (use_tmp_file)
6595    {
6596	char	tempname[MAXPATHL + 1];
6597
6598	/*
6599	 * Find a name that doesn't exist and is in the same directory.
6600	 * Rename "from" to "tempname" and then rename "tempname" to "to".
6601	 */
6602	if (STRLEN(from) >= MAXPATHL - 5)
6603	    return -1;
6604	STRCPY(tempname, from);
6605	for (n = 123; n < 99999; ++n)
6606	{
6607	    sprintf((char *)gettail((char_u *)tempname), "%d", n);
6608	    if (mch_stat(tempname, &st) < 0)
6609	    {
6610		if (mch_rename((char *)from, tempname) == 0)
6611		{
6612		    if (mch_rename(tempname, (char *)to) == 0)
6613			return 0;
6614		    /* Strange, the second step failed.  Try moving the
6615		     * file back and return failure. */
6616		    mch_rename(tempname, (char *)from);
6617		    return -1;
6618		}
6619		/* If it fails for one temp name it will most likely fail
6620		 * for any temp name, give up. */
6621		return -1;
6622	    }
6623	}
6624	return -1;
6625    }
6626#endif
6627
6628    /*
6629     * Delete the "to" file, this is required on some systems to make the
6630     * mch_rename() work, on other systems it makes sure that we don't have
6631     * two files when the mch_rename() fails.
6632     */
6633
6634#ifdef AMIGA
6635    /*
6636     * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
6637     * that the name of the "to" file is the same as the "from" file, even
6638     * though the names are different. To avoid the chance of accidentally
6639     * deleting the "from" file (horror!) we lock it during the remove.
6640     *
6641     * When used for making a backup before writing the file: This should not
6642     * happen with ":w", because startscript() should detect this problem and
6643     * set buf->b_shortname, causing modname() to return a correct ".bak" file
6644     * name.  This problem does exist with ":w filename", but then the
6645     * original file will be somewhere else so the backup isn't really
6646     * important. If autoscripting is off the rename may fail.
6647     */
6648    flock = Lock((UBYTE *)from, (long)ACCESS_READ);
6649#endif
6650    mch_remove(to);
6651#ifdef AMIGA
6652    if (flock)
6653	UnLock(flock);
6654#endif
6655
6656    /*
6657     * First try a normal rename, return if it works.
6658     */
6659    if (mch_rename((char *)from, (char *)to) == 0)
6660	return 0;
6661
6662    /*
6663     * Rename() failed, try copying the file.
6664     */
6665    perm = mch_getperm(from);
6666#ifdef HAVE_ACL
6667    /* For systems that support ACL: get the ACL from the original file. */
6668    acl = mch_get_acl(from);
6669#endif
6670    fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
6671    if (fd_in == -1)
6672    {
6673#ifdef HAVE_ACL
6674	mch_free_acl(acl);
6675#endif
6676	return -1;
6677    }
6678
6679    /* Create the new file with same permissions as the original. */
6680    fd_out = mch_open((char *)to,
6681		       O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
6682    if (fd_out == -1)
6683    {
6684	close(fd_in);
6685#ifdef HAVE_ACL
6686	mch_free_acl(acl);
6687#endif
6688	return -1;
6689    }
6690
6691    buffer = (char *)alloc(BUFSIZE);
6692    if (buffer == NULL)
6693    {
6694	close(fd_out);
6695	close(fd_in);
6696#ifdef HAVE_ACL
6697	mch_free_acl(acl);
6698#endif
6699	return -1;
6700    }
6701
6702    while ((n = vim_read(fd_in, buffer, BUFSIZE)) > 0)
6703	if (vim_write(fd_out, buffer, n) != n)
6704	{
6705	    errmsg = _("E208: Error writing to \"%s\"");
6706	    break;
6707	}
6708
6709    vim_free(buffer);
6710    close(fd_in);
6711    if (close(fd_out) < 0)
6712	errmsg = _("E209: Error closing \"%s\"");
6713    if (n < 0)
6714    {
6715	errmsg = _("E210: Error reading \"%s\"");
6716	to = from;
6717    }
6718#ifndef UNIX	    /* for Unix mch_open() already set the permission */
6719    mch_setperm(to, perm);
6720#endif
6721#ifdef HAVE_ACL
6722    mch_set_acl(to, acl);
6723    mch_free_acl(acl);
6724#endif
6725    if (errmsg != NULL)
6726    {
6727	EMSG2(errmsg, to);
6728	return -1;
6729    }
6730    mch_remove(from);
6731    return 0;
6732}
6733
6734static int already_warned = FALSE;
6735
6736/*
6737 * Check if any not hidden buffer has been changed.
6738 * Postpone the check if there are characters in the stuff buffer, a global
6739 * command is being executed, a mapping is being executed or an autocommand is
6740 * busy.
6741 * Returns TRUE if some message was written (screen should be redrawn and
6742 * cursor positioned).
6743 */
6744    int
6745check_timestamps(focus)
6746    int		focus;		/* called for GUI focus event */
6747{
6748    buf_T	*buf;
6749    int		didit = 0;
6750    int		n;
6751
6752    /* Don't check timestamps while system() or another low-level function may
6753     * cause us to lose and gain focus. */
6754    if (no_check_timestamps > 0)
6755	return FALSE;
6756
6757    /* Avoid doing a check twice.  The OK/Reload dialog can cause a focus
6758     * event and we would keep on checking if the file is steadily growing.
6759     * Do check again after typing something. */
6760    if (focus && did_check_timestamps)
6761    {
6762	need_check_timestamps = TRUE;
6763	return FALSE;
6764    }
6765
6766    if (!stuff_empty() || global_busy || !typebuf_typed()
6767#ifdef FEAT_AUTOCMD
6768			|| autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0
6769#endif
6770					)
6771	need_check_timestamps = TRUE;		/* check later */
6772    else
6773    {
6774	++no_wait_return;
6775	did_check_timestamps = TRUE;
6776	already_warned = FALSE;
6777	for (buf = firstbuf; buf != NULL; )
6778	{
6779	    /* Only check buffers in a window. */
6780	    if (buf->b_nwindows > 0)
6781	    {
6782		n = buf_check_timestamp(buf, focus);
6783		if (didit < n)
6784		    didit = n;
6785		if (n > 0 && !buf_valid(buf))
6786		{
6787		    /* Autocommands have removed the buffer, start at the
6788		     * first one again. */
6789		    buf = firstbuf;
6790		    continue;
6791		}
6792	    }
6793	    buf = buf->b_next;
6794	}
6795	--no_wait_return;
6796	need_check_timestamps = FALSE;
6797	if (need_wait_return && didit == 2)
6798	{
6799	    /* make sure msg isn't overwritten */
6800	    msg_puts((char_u *)"\n");
6801	    out_flush();
6802	}
6803    }
6804    return didit;
6805}
6806
6807/*
6808 * Move all the lines from buffer "frombuf" to buffer "tobuf".
6809 * Return OK or FAIL.  When FAIL "tobuf" is incomplete and/or "frombuf" is not
6810 * empty.
6811 */
6812    static int
6813move_lines(frombuf, tobuf)
6814    buf_T	*frombuf;
6815    buf_T	*tobuf;
6816{
6817    buf_T	*tbuf = curbuf;
6818    int		retval = OK;
6819    linenr_T	lnum;
6820    char_u	*p;
6821
6822    /* Copy the lines in "frombuf" to "tobuf". */
6823    curbuf = tobuf;
6824    for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
6825    {
6826	p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
6827	if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
6828	{
6829	    vim_free(p);
6830	    retval = FAIL;
6831	    break;
6832	}
6833	vim_free(p);
6834    }
6835
6836    /* Delete all the lines in "frombuf". */
6837    if (retval != FAIL)
6838    {
6839	curbuf = frombuf;
6840	for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
6841	    if (ml_delete(lnum, FALSE) == FAIL)
6842	    {
6843		/* Oops!  We could try putting back the saved lines, but that
6844		 * might fail again... */
6845		retval = FAIL;
6846		break;
6847	    }
6848    }
6849
6850    curbuf = tbuf;
6851    return retval;
6852}
6853
6854/*
6855 * Check if buffer "buf" has been changed.
6856 * Also check if the file for a new buffer unexpectedly appeared.
6857 * return 1 if a changed buffer was found.
6858 * return 2 if a message has been displayed.
6859 * return 0 otherwise.
6860 */
6861    int
6862buf_check_timestamp(buf, focus)
6863    buf_T	*buf;
6864    int		focus UNUSED;	/* called for GUI focus event */
6865{
6866    struct stat	st;
6867    int		stat_res;
6868    int		retval = 0;
6869    char_u	*path;
6870    char_u	*tbuf;
6871    char	*mesg = NULL;
6872    char	*mesg2 = "";
6873    int		helpmesg = FALSE;
6874    int		reload = FALSE;
6875#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6876    int		can_reload = FALSE;
6877#endif
6878    off_t	orig_size = buf->b_orig_size;
6879    int		orig_mode = buf->b_orig_mode;
6880#ifdef FEAT_GUI
6881    int		save_mouse_correct = need_mouse_correct;
6882#endif
6883#ifdef FEAT_AUTOCMD
6884    static int	busy = FALSE;
6885    int		n;
6886    char_u	*s;
6887#endif
6888    char	*reason;
6889
6890    /* If there is no file name, the buffer is not loaded, 'buftype' is
6891     * set, we are in the middle of a save or being called recursively: ignore
6892     * this buffer. */
6893    if (buf->b_ffname == NULL
6894	    || buf->b_ml.ml_mfp == NULL
6895#if defined(FEAT_QUICKFIX)
6896	    || *buf->b_p_bt != NUL
6897#endif
6898	    || buf->b_saving
6899#ifdef FEAT_AUTOCMD
6900	    || busy
6901#endif
6902#ifdef FEAT_NETBEANS_INTG
6903	    || isNetbeansBuffer(buf)
6904#endif
6905	    )
6906	return 0;
6907
6908    if (       !(buf->b_flags & BF_NOTEDITED)
6909	    && buf->b_mtime != 0
6910	    && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
6911		|| time_differs((long)st.st_mtime, buf->b_mtime)
6912#ifdef HAVE_ST_MODE
6913		|| (int)st.st_mode != buf->b_orig_mode
6914#else
6915		|| mch_getperm(buf->b_ffname) != buf->b_orig_mode
6916#endif
6917		))
6918    {
6919	retval = 1;
6920
6921	/* set b_mtime to stop further warnings (e.g., when executing
6922	 * FileChangedShell autocmd) */
6923	if (stat_res < 0)
6924	{
6925	    buf->b_mtime = 0;
6926	    buf->b_orig_size = 0;
6927	    buf->b_orig_mode = 0;
6928	}
6929	else
6930	    buf_store_time(buf, &st, buf->b_ffname);
6931
6932	/* Don't do anything for a directory.  Might contain the file
6933	 * explorer. */
6934	if (mch_isdir(buf->b_fname))
6935	    ;
6936
6937	/*
6938	 * If 'autoread' is set, the buffer has no changes and the file still
6939	 * exists, reload the buffer.  Use the buffer-local option value if it
6940	 * was set, the global option value otherwise.
6941	 */
6942	else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
6943				       && !bufIsChanged(buf) && stat_res >= 0)
6944	    reload = TRUE;
6945	else
6946	{
6947	    if (stat_res < 0)
6948		reason = "deleted";
6949	    else if (bufIsChanged(buf))
6950		reason = "conflict";
6951	    else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
6952		reason = "changed";
6953	    else if (orig_mode != buf->b_orig_mode)
6954		reason = "mode";
6955	    else
6956		reason = "time";
6957
6958#ifdef FEAT_AUTOCMD
6959	    /*
6960	     * Only give the warning if there are no FileChangedShell
6961	     * autocommands.
6962	     * Avoid being called recursively by setting "busy".
6963	     */
6964	    busy = TRUE;
6965# ifdef FEAT_EVAL
6966	    set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
6967	    set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
6968# endif
6969	    ++allbuf_lock;
6970	    n = apply_autocmds(EVENT_FILECHANGEDSHELL,
6971				      buf->b_fname, buf->b_fname, FALSE, buf);
6972	    --allbuf_lock;
6973	    busy = FALSE;
6974	    if (n)
6975	    {
6976		if (!buf_valid(buf))
6977		    EMSG(_("E246: FileChangedShell autocommand deleted buffer"));
6978# ifdef FEAT_EVAL
6979		s = get_vim_var_str(VV_FCS_CHOICE);
6980		if (STRCMP(s, "reload") == 0 && *reason != 'd')
6981		    reload = TRUE;
6982		else if (STRCMP(s, "ask") == 0)
6983		    n = FALSE;
6984		else
6985# endif
6986		    return 2;
6987	    }
6988	    if (!n)
6989#endif
6990	    {
6991		if (*reason == 'd')
6992		    mesg = _("E211: File \"%s\" no longer available");
6993		else
6994		{
6995		    helpmesg = TRUE;
6996#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6997		    can_reload = TRUE;
6998#endif
6999		    /*
7000		     * Check if the file contents really changed to avoid
7001		     * giving a warning when only the timestamp was set (e.g.,
7002		     * checked out of CVS).  Always warn when the buffer was
7003		     * changed.
7004		     */
7005		    if (reason[2] == 'n')
7006		    {
7007			mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
7008			mesg2 = _("See \":help W12\" for more info.");
7009		    }
7010		    else if (reason[1] == 'h')
7011		    {
7012			mesg = _("W11: Warning: File \"%s\" has changed since editing started");
7013			mesg2 = _("See \":help W11\" for more info.");
7014		    }
7015		    else if (*reason == 'm')
7016		    {
7017			mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
7018			mesg2 = _("See \":help W16\" for more info.");
7019		    }
7020		    else
7021			/* Only timestamp changed, store it to avoid a warning
7022			 * in check_mtime() later. */
7023			buf->b_mtime_read = buf->b_mtime;
7024		}
7025	    }
7026	}
7027
7028    }
7029    else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
7030						&& vim_fexists(buf->b_ffname))
7031    {
7032	retval = 1;
7033	mesg = _("W13: Warning: File \"%s\" has been created after editing started");
7034	buf->b_flags |= BF_NEW_W;
7035#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
7036	can_reload = TRUE;
7037#endif
7038    }
7039
7040    if (mesg != NULL)
7041    {
7042	path = home_replace_save(buf, buf->b_fname);
7043	if (path != NULL)
7044	{
7045	    if (!helpmesg)
7046		mesg2 = "";
7047	    tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg)
7048							+ STRLEN(mesg2) + 2));
7049	    sprintf((char *)tbuf, mesg, path);
7050#ifdef FEAT_EVAL
7051	    /* Set warningmsg here, before the unimportant and output-specific
7052	     * mesg2 has been appended. */
7053	    set_vim_var_string(VV_WARNINGMSG, tbuf, -1);
7054#endif
7055#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
7056	    if (can_reload)
7057	    {
7058		if (*mesg2 != NUL)
7059		{
7060		    STRCAT(tbuf, "\n");
7061		    STRCAT(tbuf, mesg2);
7062		}
7063		if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf,
7064				(char_u *)_("&OK\n&Load File"), 1, NULL) == 2)
7065		    reload = TRUE;
7066	    }
7067	    else
7068#endif
7069	    if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
7070	    {
7071		if (*mesg2 != NUL)
7072		{
7073		    STRCAT(tbuf, "; ");
7074		    STRCAT(tbuf, mesg2);
7075		}
7076		EMSG(tbuf);
7077		retval = 2;
7078	    }
7079	    else
7080	    {
7081# ifdef FEAT_AUTOCMD
7082		if (!autocmd_busy)
7083# endif
7084		{
7085		    msg_start();
7086		    msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST);
7087		    if (*mesg2 != NUL)
7088			msg_puts_attr((char_u *)mesg2,
7089						   hl_attr(HLF_W) + MSG_HIST);
7090		    msg_clr_eos();
7091		    (void)msg_end();
7092		    if (emsg_silent == 0)
7093		    {
7094			out_flush();
7095# ifdef FEAT_GUI
7096			if (!focus)
7097# endif
7098			    /* give the user some time to think about it */
7099			    ui_delay(1000L, TRUE);
7100
7101			/* don't redraw and erase the message */
7102			redraw_cmdline = FALSE;
7103		    }
7104		}
7105		already_warned = TRUE;
7106	    }
7107
7108	    vim_free(path);
7109	    vim_free(tbuf);
7110	}
7111    }
7112
7113    if (reload)
7114	/* Reload the buffer. */
7115	buf_reload(buf, orig_mode);
7116
7117#ifdef FEAT_AUTOCMD
7118    /* Trigger FileChangedShell when the file was changed in any way. */
7119    if (buf_valid(buf) && retval != 0)
7120	(void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
7121				      buf->b_fname, buf->b_fname, FALSE, buf);
7122#endif
7123#ifdef FEAT_GUI
7124    /* restore this in case an autocommand has set it; it would break
7125     * 'mousefocus' */
7126    need_mouse_correct = save_mouse_correct;
7127#endif
7128
7129    return retval;
7130}
7131
7132/*
7133 * Reload a buffer that is already loaded.
7134 * Used when the file was changed outside of Vim.
7135 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
7136 * buf->b_orig_mode may have been reset already.
7137 */
7138    void
7139buf_reload(buf, orig_mode)
7140    buf_T	*buf;
7141    int		orig_mode;
7142{
7143    exarg_T	ea;
7144    pos_T	old_cursor;
7145    linenr_T	old_topline;
7146    int		old_ro = buf->b_p_ro;
7147    buf_T	*savebuf;
7148    int		saved = OK;
7149    aco_save_T	aco;
7150    int		flags = READ_NEW;
7151
7152    /* set curwin/curbuf for "buf" and save some things */
7153    aucmd_prepbuf(&aco, buf);
7154
7155    /* We only want to read the text from the file, not reset the syntax
7156     * highlighting, clear marks, diff status, etc.  Force the fileformat
7157     * and encoding to be the same. */
7158    if (prep_exarg(&ea, buf) == OK)
7159    {
7160	old_cursor = curwin->w_cursor;
7161	old_topline = curwin->w_topline;
7162
7163	if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
7164	{
7165	    /* Save all the text, so that the reload can be undone.
7166	     * Sync first so that this is a separate undo-able action. */
7167	    u_sync(FALSE);
7168	    saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
7169	    flags |= READ_KEEP_UNDO;
7170	}
7171
7172	/*
7173	 * To behave like when a new file is edited (matters for
7174	 * BufReadPost autocommands) we first need to delete the current
7175	 * buffer contents.  But if reading the file fails we should keep
7176	 * the old contents.  Can't use memory only, the file might be
7177	 * too big.  Use a hidden buffer to move the buffer contents to.
7178	 */
7179	if (bufempty() || saved == FAIL)
7180	    savebuf = NULL;
7181	else
7182	{
7183	    /* Allocate a buffer without putting it in the buffer list. */
7184	    savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
7185	    if (savebuf != NULL && buf == curbuf)
7186	    {
7187		/* Open the memline. */
7188		curbuf = savebuf;
7189		curwin->w_buffer = savebuf;
7190		saved = ml_open(curbuf);
7191		curbuf = buf;
7192		curwin->w_buffer = buf;
7193	    }
7194	    if (savebuf == NULL || saved == FAIL || buf != curbuf
7195				      || move_lines(buf, savebuf) == FAIL)
7196	    {
7197		EMSG2(_("E462: Could not prepare for reloading \"%s\""),
7198							    buf->b_fname);
7199		saved = FAIL;
7200	    }
7201	}
7202
7203	if (saved == OK)
7204	{
7205	    curbuf->b_flags |= BF_CHECK_RO;	/* check for RO again */
7206#ifdef FEAT_AUTOCMD
7207	    keep_filetype = TRUE;		/* don't detect 'filetype' */
7208#endif
7209	    if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
7210			(linenr_T)0,
7211			(linenr_T)MAXLNUM, &ea, flags) == FAIL)
7212	    {
7213#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7214		if (!aborting())
7215#endif
7216		    EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname);
7217		if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf)
7218		{
7219		    /* Put the text back from the save buffer.  First
7220		     * delete any lines that readfile() added. */
7221		    while (!bufempty())
7222			if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL)
7223			    break;
7224		    (void)move_lines(savebuf, buf);
7225		}
7226	    }
7227	    else if (buf == curbuf)  /* "buf" still valid */
7228	    {
7229		/* Mark the buffer as unmodified and free undo info. */
7230		unchanged(buf, TRUE);
7231		if ((flags & READ_KEEP_UNDO) == 0)
7232		{
7233		    u_blockfree(buf);
7234		    u_clearall(buf);
7235		}
7236		else
7237		{
7238		    /* Mark all undo states as changed. */
7239		    u_unchanged(curbuf);
7240		}
7241	    }
7242	}
7243	vim_free(ea.cmd);
7244
7245	if (savebuf != NULL && buf_valid(savebuf))
7246	    wipe_buffer(savebuf, FALSE);
7247
7248#ifdef FEAT_DIFF
7249	/* Invalidate diff info if necessary. */
7250	diff_invalidate(curbuf);
7251#endif
7252
7253	/* Restore the topline and cursor position and check it (lines may
7254	 * have been removed). */
7255	if (old_topline > curbuf->b_ml.ml_line_count)
7256	    curwin->w_topline = curbuf->b_ml.ml_line_count;
7257	else
7258	    curwin->w_topline = old_topline;
7259	curwin->w_cursor = old_cursor;
7260	check_cursor();
7261	update_topline();
7262#ifdef FEAT_AUTOCMD
7263	keep_filetype = FALSE;
7264#endif
7265#ifdef FEAT_FOLDING
7266	{
7267	    win_T	*wp;
7268	    tabpage_T	*tp;
7269
7270	    /* Update folds unless they are defined manually. */
7271	    FOR_ALL_TAB_WINDOWS(tp, wp)
7272		if (wp->w_buffer == curwin->w_buffer
7273			&& !foldmethodIsManual(wp))
7274		    foldUpdateAll(wp);
7275	}
7276#endif
7277	/* If the mode didn't change and 'readonly' was set, keep the old
7278	 * value; the user probably used the ":view" command.  But don't
7279	 * reset it, might have had a read error. */
7280	if (orig_mode == curbuf->b_orig_mode)
7281	    curbuf->b_p_ro |= old_ro;
7282    }
7283
7284    /* restore curwin/curbuf and a few other things */
7285    aucmd_restbuf(&aco);
7286    /* Careful: autocommands may have made "buf" invalid! */
7287}
7288
7289    void
7290buf_store_time(buf, st, fname)
7291    buf_T	*buf;
7292    struct stat	*st;
7293    char_u	*fname UNUSED;
7294{
7295    buf->b_mtime = (long)st->st_mtime;
7296    buf->b_orig_size = st->st_size;
7297#ifdef HAVE_ST_MODE
7298    buf->b_orig_mode = (int)st->st_mode;
7299#else
7300    buf->b_orig_mode = mch_getperm(fname);
7301#endif
7302}
7303
7304/*
7305 * Adjust the line with missing eol, used for the next write.
7306 * Used for do_filter(), when the input lines for the filter are deleted.
7307 */
7308    void
7309write_lnum_adjust(offset)
7310    linenr_T	offset;
7311{
7312    if (write_no_eol_lnum != 0)		/* only if there is a missing eol */
7313	write_no_eol_lnum += offset;
7314}
7315
7316#if defined(TEMPDIRNAMES) || defined(PROTO)
7317static long	temp_count = 0;		/* Temp filename counter. */
7318
7319/*
7320 * Delete the temp directory and all files it contains.
7321 */
7322    void
7323vim_deltempdir()
7324{
7325    char_u	**files;
7326    int		file_count;
7327    int		i;
7328
7329    if (vim_tempdir != NULL)
7330    {
7331	sprintf((char *)NameBuff, "%s*", vim_tempdir);
7332	if (gen_expand_wildcards(1, &NameBuff, &file_count, &files,
7333					      EW_DIR|EW_FILE|EW_SILENT) == OK)
7334	{
7335	    for (i = 0; i < file_count; ++i)
7336		mch_remove(files[i]);
7337	    FreeWild(file_count, files);
7338	}
7339	gettail(NameBuff)[-1] = NUL;
7340	(void)mch_rmdir(NameBuff);
7341
7342	vim_free(vim_tempdir);
7343	vim_tempdir = NULL;
7344    }
7345}
7346#endif
7347
7348#ifdef TEMPDIRNAMES
7349/*
7350 * Directory "tempdir" was created.  Expand this name to a full path and put
7351 * it in "vim_tempdir".  This avoids that using ":cd" would confuse us.
7352 * "tempdir" must be no longer than MAXPATHL.
7353 */
7354    static void
7355vim_settempdir(tempdir)
7356    char_u	*tempdir;
7357{
7358    char_u	*buf;
7359
7360    buf = alloc((unsigned)MAXPATHL + 2);
7361    if (buf != NULL)
7362    {
7363	if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
7364	    STRCPY(buf, tempdir);
7365# ifdef __EMX__
7366	if (vim_strchr(buf, '/') != NULL)
7367	    STRCAT(buf, "/");
7368	else
7369# endif
7370	    add_pathsep(buf);
7371	vim_tempdir = vim_strsave(buf);
7372	vim_free(buf);
7373    }
7374}
7375#endif
7376
7377/*
7378 * vim_tempname(): Return a unique name that can be used for a temp file.
7379 *
7380 * The temp file is NOT created.
7381 *
7382 * The returned pointer is to allocated memory.
7383 * The returned pointer is NULL if no valid name was found.
7384 */
7385    char_u  *
7386vim_tempname(extra_char)
7387    int	    extra_char UNUSED;  /* char to use in the name instead of '?' */
7388{
7389#ifdef USE_TMPNAM
7390    char_u	itmp[L_tmpnam];	/* use tmpnam() */
7391#else
7392    char_u	itmp[TEMPNAMELEN];
7393#endif
7394
7395#ifdef TEMPDIRNAMES
7396    static char	*(tempdirs[]) = {TEMPDIRNAMES};
7397    int		i;
7398# ifndef EEXIST
7399    struct stat	st;
7400# endif
7401
7402    /*
7403     * This will create a directory for private use by this instance of Vim.
7404     * This is done once, and the same directory is used for all temp files.
7405     * This method avoids security problems because of symlink attacks et al.
7406     * It's also a bit faster, because we only need to check for an existing
7407     * file when creating the directory and not for each temp file.
7408     */
7409    if (vim_tempdir == NULL)
7410    {
7411	/*
7412	 * Try the entries in TEMPDIRNAMES to create the temp directory.
7413	 */
7414	for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
7415	{
7416# ifndef HAVE_MKDTEMP
7417	    size_t	itmplen;
7418	    long	nr;
7419	    long	off;
7420# endif
7421
7422	    /* expand $TMP, leave room for "/v1100000/999999999" */
7423	    expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
7424	    if (mch_isdir(itmp))		/* directory exists */
7425	    {
7426# ifdef __EMX__
7427		/* If $TMP contains a forward slash (perhaps using bash or
7428		 * tcsh), don't add a backslash, use a forward slash!
7429		 * Adding 2 backslashes didn't work. */
7430		if (vim_strchr(itmp, '/') != NULL)
7431		    STRCAT(itmp, "/");
7432		else
7433# endif
7434		    add_pathsep(itmp);
7435
7436# ifdef HAVE_MKDTEMP
7437		/* Leave room for filename */
7438		STRCAT(itmp, "vXXXXXX");
7439		if (mkdtemp((char *)itmp) != NULL)
7440		    vim_settempdir(itmp);
7441# else
7442		/* Get an arbitrary number of up to 6 digits.  When it's
7443		 * unlikely that it already exists it will be faster,
7444		 * otherwise it doesn't matter.  The use of mkdir() avoids any
7445		 * security problems because of the predictable number. */
7446		nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
7447		itmplen = STRLEN(itmp);
7448
7449		/* Try up to 10000 different values until we find a name that
7450		 * doesn't exist. */
7451		for (off = 0; off < 10000L; ++off)
7452		{
7453		    int		r;
7454#  if defined(UNIX) || defined(VMS)
7455		    mode_t	umask_save;
7456#  endif
7457
7458		    sprintf((char *)itmp + itmplen, "v%ld", nr + off);
7459#  ifndef EEXIST
7460		    /* If mkdir() does not set errno to EEXIST, check for
7461		     * existing file here.  There is a race condition then,
7462		     * although it's fail-safe. */
7463		    if (mch_stat((char *)itmp, &st) >= 0)
7464			continue;
7465#  endif
7466#  if defined(UNIX) || defined(VMS)
7467		    /* Make sure the umask doesn't remove the executable bit.
7468		     * "repl" has been reported to use "177". */
7469		    umask_save = umask(077);
7470#  endif
7471		    r = vim_mkdir(itmp, 0700);
7472#  if defined(UNIX) || defined(VMS)
7473		    (void)umask(umask_save);
7474#  endif
7475		    if (r == 0)
7476		    {
7477			vim_settempdir(itmp);
7478			break;
7479		    }
7480#  ifdef EEXIST
7481		    /* If the mkdir() didn't fail because the file/dir exists,
7482		     * we probably can't create any dir here, try another
7483		     * place. */
7484		    if (errno != EEXIST)
7485#  endif
7486			break;
7487		}
7488# endif /* HAVE_MKDTEMP */
7489		if (vim_tempdir != NULL)
7490		    break;
7491	    }
7492	}
7493    }
7494
7495    if (vim_tempdir != NULL)
7496    {
7497	/* There is no need to check if the file exists, because we own the
7498	 * directory and nobody else creates a file in it. */
7499	sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
7500	return vim_strsave(itmp);
7501    }
7502
7503    return NULL;
7504
7505#else /* TEMPDIRNAMES */
7506
7507# ifdef WIN3264
7508    char	szTempFile[_MAX_PATH + 1];
7509    char	buf4[4];
7510    char_u	*retval;
7511    char_u	*p;
7512
7513    STRCPY(itmp, "");
7514    if (GetTempPath(_MAX_PATH, szTempFile) == 0)
7515	szTempFile[0] = NUL;	/* GetTempPath() failed, use current dir */
7516    strcpy(buf4, "VIM");
7517    buf4[2] = extra_char;   /* make it "VIa", "VIb", etc. */
7518    if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0)
7519	return NULL;
7520    /* GetTempFileName() will create the file, we don't want that */
7521    (void)DeleteFile(itmp);
7522
7523    /* Backslashes in a temp file name cause problems when filtering with
7524     * "sh".  NOTE: This also checks 'shellcmdflag' to help those people who
7525     * didn't set 'shellslash'. */
7526    retval = vim_strsave(itmp);
7527    if (*p_shcf == '-' || p_ssl)
7528	for (p = retval; *p; ++p)
7529	    if (*p == '\\')
7530		*p = '/';
7531    return retval;
7532
7533# else /* WIN3264 */
7534
7535#  ifdef USE_TMPNAM
7536    /* tmpnam() will make its own name */
7537    if (*tmpnam((char *)itmp) == NUL)
7538	return NULL;
7539#  else
7540    char_u	*p;
7541
7542#   ifdef VMS_TEMPNAM
7543    /* mktemp() is not working on VMS.  It seems to be
7544     * a do-nothing function. Therefore we use tempnam().
7545     */
7546    sprintf((char *)itmp, "VIM%c", extra_char);
7547    p = (char_u *)tempnam("tmp:", (char *)itmp);
7548    if (p != NULL)
7549    {
7550	/* VMS will use '.LOG' if we don't explicitly specify an extension,
7551	 * and VIM will then be unable to find the file later */
7552	STRCPY(itmp, p);
7553	STRCAT(itmp, ".txt");
7554	free(p);
7555    }
7556    else
7557	return NULL;
7558#   else
7559    STRCPY(itmp, TEMPNAME);
7560    if ((p = vim_strchr(itmp, '?')) != NULL)
7561	*p = extra_char;
7562    if (mktemp((char *)itmp) == NULL)
7563	return NULL;
7564#   endif
7565#  endif
7566
7567    return vim_strsave(itmp);
7568# endif /* WIN3264 */
7569#endif /* TEMPDIRNAMES */
7570}
7571
7572#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7573/*
7574 * Convert all backslashes in fname to forward slashes in-place.
7575 */
7576    void
7577forward_slash(fname)
7578    char_u	*fname;
7579{
7580    char_u	*p;
7581
7582    for (p = fname; *p != NUL; ++p)
7583# ifdef  FEAT_MBYTE
7584	/* The Big5 encoding can have '\' in the trail byte. */
7585	if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
7586	    ++p;
7587	else
7588# endif
7589	if (*p == '\\')
7590	    *p = '/';
7591}
7592#endif
7593
7594
7595/*
7596 * Code for automatic commands.
7597 *
7598 * Only included when "FEAT_AUTOCMD" has been defined.
7599 */
7600
7601#if defined(FEAT_AUTOCMD) || defined(PROTO)
7602
7603/*
7604 * The autocommands are stored in a list for each event.
7605 * Autocommands for the same pattern, that are consecutive, are joined
7606 * together, to avoid having to match the pattern too often.
7607 * The result is an array of Autopat lists, which point to AutoCmd lists:
7608 *
7609 * first_autopat[0] --> Autopat.next  -->  Autopat.next -->  NULL
7610 *			Autopat.cmds	   Autopat.cmds
7611 *			    |			 |
7612 *			    V			 V
7613 *			AutoCmd.next	   AutoCmd.next
7614 *			    |			 |
7615 *			    V			 V
7616 *			AutoCmd.next		NULL
7617 *			    |
7618 *			    V
7619 *			   NULL
7620 *
7621 * first_autopat[1] --> Autopat.next  -->  NULL
7622 *			Autopat.cmds
7623 *			    |
7624 *			    V
7625 *			AutoCmd.next
7626 *			    |
7627 *			    V
7628 *			   NULL
7629 *   etc.
7630 *
7631 *   The order of AutoCmds is important, this is the order in which they were
7632 *   defined and will have to be executed.
7633 */
7634typedef struct AutoCmd
7635{
7636    char_u	    *cmd;		/* The command to be executed (NULL
7637					   when command has been removed) */
7638    char	    nested;		/* If autocommands nest here */
7639    char	    last;		/* last command in list */
7640#ifdef FEAT_EVAL
7641    scid_T	    scriptID;		/* script ID where defined */
7642#endif
7643    struct AutoCmd  *next;		/* Next AutoCmd in list */
7644} AutoCmd;
7645
7646typedef struct AutoPat
7647{
7648    int		    group;		/* group ID */
7649    char_u	    *pat;		/* pattern as typed (NULL when pattern
7650					   has been removed) */
7651    int		    patlen;		/* strlen() of pat */
7652    regprog_T	    *reg_prog;		/* compiled regprog for pattern */
7653    char	    allow_dirs;		/* Pattern may match whole path */
7654    char	    last;		/* last pattern for apply_autocmds() */
7655    AutoCmd	    *cmds;		/* list of commands to do */
7656    struct AutoPat  *next;		/* next AutoPat in AutoPat list */
7657    int		    buflocal_nr;	/* !=0 for buffer-local AutoPat */
7658} AutoPat;
7659
7660static struct event_name
7661{
7662    char	*name;	/* event name */
7663    event_T	event;	/* event number */
7664} event_names[] =
7665{
7666    {"BufAdd",		EVENT_BUFADD},
7667    {"BufCreate",	EVENT_BUFADD},
7668    {"BufDelete",	EVENT_BUFDELETE},
7669    {"BufEnter",	EVENT_BUFENTER},
7670    {"BufFilePost",	EVENT_BUFFILEPOST},
7671    {"BufFilePre",	EVENT_BUFFILEPRE},
7672    {"BufHidden",	EVENT_BUFHIDDEN},
7673    {"BufLeave",	EVENT_BUFLEAVE},
7674    {"BufNew",		EVENT_BUFNEW},
7675    {"BufNewFile",	EVENT_BUFNEWFILE},
7676    {"BufRead",		EVENT_BUFREADPOST},
7677    {"BufReadCmd",	EVENT_BUFREADCMD},
7678    {"BufReadPost",	EVENT_BUFREADPOST},
7679    {"BufReadPre",	EVENT_BUFREADPRE},
7680    {"BufUnload",	EVENT_BUFUNLOAD},
7681    {"BufWinEnter",	EVENT_BUFWINENTER},
7682    {"BufWinLeave",	EVENT_BUFWINLEAVE},
7683    {"BufWipeout",	EVENT_BUFWIPEOUT},
7684    {"BufWrite",	EVENT_BUFWRITEPRE},
7685    {"BufWritePost",	EVENT_BUFWRITEPOST},
7686    {"BufWritePre",	EVENT_BUFWRITEPRE},
7687    {"BufWriteCmd",	EVENT_BUFWRITECMD},
7688    {"CmdwinEnter",	EVENT_CMDWINENTER},
7689    {"CmdwinLeave",	EVENT_CMDWINLEAVE},
7690    {"ColorScheme",	EVENT_COLORSCHEME},
7691    {"CursorHold",	EVENT_CURSORHOLD},
7692    {"CursorHoldI",	EVENT_CURSORHOLDI},
7693    {"CursorMoved",	EVENT_CURSORMOVED},
7694    {"CursorMovedI",	EVENT_CURSORMOVEDI},
7695    {"EncodingChanged",	EVENT_ENCODINGCHANGED},
7696    {"FileEncoding",	EVENT_ENCODINGCHANGED},
7697    {"FileAppendPost",	EVENT_FILEAPPENDPOST},
7698    {"FileAppendPre",	EVENT_FILEAPPENDPRE},
7699    {"FileAppendCmd",	EVENT_FILEAPPENDCMD},
7700    {"FileChangedShell",EVENT_FILECHANGEDSHELL},
7701    {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST},
7702    {"FileChangedRO",	EVENT_FILECHANGEDRO},
7703    {"FileReadPost",	EVENT_FILEREADPOST},
7704    {"FileReadPre",	EVENT_FILEREADPRE},
7705    {"FileReadCmd",	EVENT_FILEREADCMD},
7706    {"FileType",	EVENT_FILETYPE},
7707    {"FileWritePost",	EVENT_FILEWRITEPOST},
7708    {"FileWritePre",	EVENT_FILEWRITEPRE},
7709    {"FileWriteCmd",	EVENT_FILEWRITECMD},
7710    {"FilterReadPost",	EVENT_FILTERREADPOST},
7711    {"FilterReadPre",	EVENT_FILTERREADPRE},
7712    {"FilterWritePost",	EVENT_FILTERWRITEPOST},
7713    {"FilterWritePre",	EVENT_FILTERWRITEPRE},
7714    {"FocusGained",	EVENT_FOCUSGAINED},
7715    {"FocusLost",	EVENT_FOCUSLOST},
7716    {"FuncUndefined",	EVENT_FUNCUNDEFINED},
7717    {"GUIEnter",	EVENT_GUIENTER},
7718    {"GUIFailed",	EVENT_GUIFAILED},
7719    {"InsertChange",	EVENT_INSERTCHANGE},
7720    {"InsertEnter",	EVENT_INSERTENTER},
7721    {"InsertLeave",	EVENT_INSERTLEAVE},
7722    {"MenuPopup",	EVENT_MENUPOPUP},
7723    {"QuickFixCmdPost",	EVENT_QUICKFIXCMDPOST},
7724    {"QuickFixCmdPre",	EVENT_QUICKFIXCMDPRE},
7725    {"RemoteReply",	EVENT_REMOTEREPLY},
7726    {"SessionLoadPost",	EVENT_SESSIONLOADPOST},
7727    {"ShellCmdPost",	EVENT_SHELLCMDPOST},
7728    {"ShellFilterPost",	EVENT_SHELLFILTERPOST},
7729    {"SourcePre",	EVENT_SOURCEPRE},
7730    {"SourceCmd",	EVENT_SOURCECMD},
7731    {"SpellFileMissing",EVENT_SPELLFILEMISSING},
7732    {"StdinReadPost",	EVENT_STDINREADPOST},
7733    {"StdinReadPre",	EVENT_STDINREADPRE},
7734    {"SwapExists",	EVENT_SWAPEXISTS},
7735    {"Syntax",		EVENT_SYNTAX},
7736    {"TabEnter",	EVENT_TABENTER},
7737    {"TabLeave",	EVENT_TABLEAVE},
7738    {"TermChanged",	EVENT_TERMCHANGED},
7739    {"TermResponse",	EVENT_TERMRESPONSE},
7740    {"User",		EVENT_USER},
7741    {"VimEnter",	EVENT_VIMENTER},
7742    {"VimLeave",	EVENT_VIMLEAVE},
7743    {"VimLeavePre",	EVENT_VIMLEAVEPRE},
7744    {"WinEnter",	EVENT_WINENTER},
7745    {"WinLeave",	EVENT_WINLEAVE},
7746    {"VimResized",	EVENT_VIMRESIZED},
7747    {NULL,		(event_T)0}
7748};
7749
7750static AutoPat *first_autopat[NUM_EVENTS] =
7751{
7752    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7753    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7754    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7755    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7756    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7757    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
7758};
7759
7760/*
7761 * struct used to keep status while executing autocommands for an event.
7762 */
7763typedef struct AutoPatCmd
7764{
7765    AutoPat	*curpat;	/* next AutoPat to examine */
7766    AutoCmd	*nextcmd;	/* next AutoCmd to execute */
7767    int		group;		/* group being used */
7768    char_u	*fname;		/* fname to match with */
7769    char_u	*sfname;	/* sfname to match with */
7770    char_u	*tail;		/* tail of fname */
7771    event_T	event;		/* current event */
7772    int		arg_bufnr;	/* initially equal to <abuf>, set to zero when
7773				   buf is deleted */
7774    struct AutoPatCmd   *next;	/* chain of active apc-s for auto-invalidation*/
7775} AutoPatCmd;
7776
7777static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */
7778
7779/*
7780 * augroups stores a list of autocmd group names.
7781 */
7782static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL};
7783#define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i])
7784
7785/*
7786 * The ID of the current group.  Group 0 is the default one.
7787 */
7788static int current_augroup = AUGROUP_DEFAULT;
7789
7790static int au_need_clean = FALSE;   /* need to delete marked patterns */
7791
7792static void show_autocmd __ARGS((AutoPat *ap, event_T event));
7793static void au_remove_pat __ARGS((AutoPat *ap));
7794static void au_remove_cmds __ARGS((AutoPat *ap));
7795static void au_cleanup __ARGS((void));
7796static int au_new_group __ARGS((char_u *name));
7797static void au_del_group __ARGS((char_u *name));
7798static event_T event_name2nr __ARGS((char_u *start, char_u **end));
7799static char_u *event_nr2name __ARGS((event_T event));
7800static char_u *find_end_event __ARGS((char_u *arg, int have_group));
7801static int event_ignored __ARGS((event_T event));
7802static int au_get_grouparg __ARGS((char_u **argp));
7803static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group));
7804static char_u *getnextac __ARGS((int c, void *cookie, int indent));
7805static int apply_autocmds_group __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap));
7806static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last));
7807
7808
7809static event_T	last_event;
7810static int	last_group;
7811static int	autocmd_blocked = 0;	/* block all autocmds */
7812
7813/*
7814 * Show the autocommands for one AutoPat.
7815 */
7816    static void
7817show_autocmd(ap, event)
7818    AutoPat	*ap;
7819    event_T	event;
7820{
7821    AutoCmd *ac;
7822
7823    /* Check for "got_int" (here and at various places below), which is set
7824     * when "q" has been hit for the "--more--" prompt */
7825    if (got_int)
7826	return;
7827    if (ap->pat == NULL)		/* pattern has been removed */
7828	return;
7829
7830    msg_putchar('\n');
7831    if (got_int)
7832	return;
7833    if (event != last_event || ap->group != last_group)
7834    {
7835	if (ap->group != AUGROUP_DEFAULT)
7836	{
7837	    if (AUGROUP_NAME(ap->group) == NULL)
7838		msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E));
7839	    else
7840		msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T));
7841	    msg_puts((char_u *)"  ");
7842	}
7843	msg_puts_attr(event_nr2name(event), hl_attr(HLF_T));
7844	last_event = event;
7845	last_group = ap->group;
7846	msg_putchar('\n');
7847	if (got_int)
7848	    return;
7849    }
7850    msg_col = 4;
7851    msg_outtrans(ap->pat);
7852
7853    for (ac = ap->cmds; ac != NULL; ac = ac->next)
7854    {
7855	if (ac->cmd != NULL)		/* skip removed commands */
7856	{
7857	    if (msg_col >= 14)
7858		msg_putchar('\n');
7859	    msg_col = 14;
7860	    if (got_int)
7861		return;
7862	    msg_outtrans(ac->cmd);
7863#ifdef FEAT_EVAL
7864	    if (p_verbose > 0)
7865		last_set_msg(ac->scriptID);
7866#endif
7867	    if (got_int)
7868		return;
7869	    if (ac->next != NULL)
7870	    {
7871		msg_putchar('\n');
7872		if (got_int)
7873		    return;
7874	    }
7875	}
7876    }
7877}
7878
7879/*
7880 * Mark an autocommand pattern for deletion.
7881 */
7882    static void
7883au_remove_pat(ap)
7884    AutoPat *ap;
7885{
7886    vim_free(ap->pat);
7887    ap->pat = NULL;
7888    ap->buflocal_nr = -1;
7889    au_need_clean = TRUE;
7890}
7891
7892/*
7893 * Mark all commands for a pattern for deletion.
7894 */
7895    static void
7896au_remove_cmds(ap)
7897    AutoPat *ap;
7898{
7899    AutoCmd *ac;
7900
7901    for (ac = ap->cmds; ac != NULL; ac = ac->next)
7902    {
7903	vim_free(ac->cmd);
7904	ac->cmd = NULL;
7905    }
7906    au_need_clean = TRUE;
7907}
7908
7909/*
7910 * Cleanup autocommands and patterns that have been deleted.
7911 * This is only done when not executing autocommands.
7912 */
7913    static void
7914au_cleanup()
7915{
7916    AutoPat	*ap, **prev_ap;
7917    AutoCmd	*ac, **prev_ac;
7918    event_T	event;
7919
7920    if (autocmd_busy || !au_need_clean)
7921	return;
7922
7923    /* loop over all events */
7924    for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
7925					    event = (event_T)((int)event + 1))
7926    {
7927	/* loop over all autocommand patterns */
7928	prev_ap = &(first_autopat[(int)event]);
7929	for (ap = *prev_ap; ap != NULL; ap = *prev_ap)
7930	{
7931	    /* loop over all commands for this pattern */
7932	    prev_ac = &(ap->cmds);
7933	    for (ac = *prev_ac; ac != NULL; ac = *prev_ac)
7934	    {
7935		/* remove the command if the pattern is to be deleted or when
7936		 * the command has been marked for deletion */
7937		if (ap->pat == NULL || ac->cmd == NULL)
7938		{
7939		    *prev_ac = ac->next;
7940		    vim_free(ac->cmd);
7941		    vim_free(ac);
7942		}
7943		else
7944		    prev_ac = &(ac->next);
7945	    }
7946
7947	    /* remove the pattern if it has been marked for deletion */
7948	    if (ap->pat == NULL)
7949	    {
7950		*prev_ap = ap->next;
7951		vim_free(ap->reg_prog);
7952		vim_free(ap);
7953	    }
7954	    else
7955		prev_ap = &(ap->next);
7956	}
7957    }
7958
7959    au_need_clean = FALSE;
7960}
7961
7962/*
7963 * Called when buffer is freed, to remove/invalidate related buffer-local
7964 * autocmds.
7965 */
7966    void
7967aubuflocal_remove(buf)
7968    buf_T	*buf;
7969{
7970    AutoPat	*ap;
7971    event_T	event;
7972    AutoPatCmd	*apc;
7973
7974    /* invalidate currently executing autocommands */
7975    for (apc = active_apc_list; apc; apc = apc->next)
7976	if (buf->b_fnum == apc->arg_bufnr)
7977	    apc->arg_bufnr = 0;
7978
7979    /* invalidate buflocals looping through events */
7980    for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
7981					    event = (event_T)((int)event + 1))
7982	/* loop over all autocommand patterns */
7983	for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
7984	    if (ap->buflocal_nr == buf->b_fnum)
7985	    {
7986		au_remove_pat(ap);
7987		if (p_verbose >= 6)
7988		{
7989		    verbose_enter();
7990		    smsg((char_u *)
7991			    _("auto-removing autocommand: %s <buffer=%d>"),
7992					   event_nr2name(event), buf->b_fnum);
7993		    verbose_leave();
7994		}
7995	    }
7996    au_cleanup();
7997}
7998
7999/*
8000 * Add an autocmd group name.
8001 * Return it's ID.  Returns AUGROUP_ERROR (< 0) for error.
8002 */
8003    static int
8004au_new_group(name)
8005    char_u	*name;
8006{
8007    int		i;
8008
8009    i = au_find_group(name);
8010    if (i == AUGROUP_ERROR)	/* the group doesn't exist yet, add it */
8011    {
8012	/* First try using a free entry. */
8013	for (i = 0; i < augroups.ga_len; ++i)
8014	    if (AUGROUP_NAME(i) == NULL)
8015		break;
8016	if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL)
8017	    return AUGROUP_ERROR;
8018
8019	AUGROUP_NAME(i) = vim_strsave(name);
8020	if (AUGROUP_NAME(i) == NULL)
8021	    return AUGROUP_ERROR;
8022	if (i == augroups.ga_len)
8023	    ++augroups.ga_len;
8024    }
8025
8026    return i;
8027}
8028
8029    static void
8030au_del_group(name)
8031    char_u	*name;
8032{
8033    int	    i;
8034
8035    i = au_find_group(name);
8036    if (i == AUGROUP_ERROR)	/* the group doesn't exist */
8037	EMSG2(_("E367: No such group: \"%s\""), name);
8038    else
8039    {
8040	vim_free(AUGROUP_NAME(i));
8041	AUGROUP_NAME(i) = NULL;
8042    }
8043}
8044
8045/*
8046 * Find the ID of an autocmd group name.
8047 * Return it's ID.  Returns AUGROUP_ERROR (< 0) for error.
8048 */
8049    static int
8050au_find_group(name)
8051    char_u	*name;
8052{
8053    int	    i;
8054
8055    for (i = 0; i < augroups.ga_len; ++i)
8056	if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0)
8057	    return i;
8058    return AUGROUP_ERROR;
8059}
8060
8061/*
8062 * Return TRUE if augroup "name" exists.
8063 */
8064    int
8065au_has_group(name)
8066    char_u	*name;
8067{
8068    return au_find_group(name) != AUGROUP_ERROR;
8069}
8070
8071/*
8072 * ":augroup {name}".
8073 */
8074    void
8075do_augroup(arg, del_group)
8076    char_u	*arg;
8077    int		del_group;
8078{
8079    int	    i;
8080
8081    if (del_group)
8082    {
8083	if (*arg == NUL)
8084	    EMSG(_(e_argreq));
8085	else
8086	    au_del_group(arg);
8087    }
8088    else if (STRICMP(arg, "end") == 0)   /* ":aug end": back to group 0 */
8089	current_augroup = AUGROUP_DEFAULT;
8090    else if (*arg)		    /* ":aug xxx": switch to group xxx */
8091    {
8092	i = au_new_group(arg);
8093	if (i != AUGROUP_ERROR)
8094	    current_augroup = i;
8095    }
8096    else			    /* ":aug": list the group names */
8097    {
8098	msg_start();
8099	for (i = 0; i < augroups.ga_len; ++i)
8100	{
8101	    if (AUGROUP_NAME(i) != NULL)
8102	    {
8103		msg_puts(AUGROUP_NAME(i));
8104		msg_puts((char_u *)"  ");
8105	    }
8106	}
8107	msg_clr_eos();
8108	msg_end();
8109    }
8110}
8111
8112#if defined(EXITFREE) || defined(PROTO)
8113    void
8114free_all_autocmds()
8115{
8116    for (current_augroup = -1; current_augroup < augroups.ga_len;
8117							    ++current_augroup)
8118	do_autocmd((char_u *)"", TRUE);
8119    ga_clear_strings(&augroups);
8120}
8121#endif
8122
8123/*
8124 * Return the event number for event name "start".
8125 * Return NUM_EVENTS if the event name was not found.
8126 * Return a pointer to the next event name in "end".
8127 */
8128    static event_T
8129event_name2nr(start, end)
8130    char_u  *start;
8131    char_u  **end;
8132{
8133    char_u	*p;
8134    int		i;
8135    int		len;
8136
8137    /* the event name ends with end of line, a blank or a comma */
8138    for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p)
8139	;
8140    for (i = 0; event_names[i].name != NULL; ++i)
8141    {
8142	len = (int)STRLEN(event_names[i].name);
8143	if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0)
8144	    break;
8145    }
8146    if (*p == ',')
8147	++p;
8148    *end = p;
8149    if (event_names[i].name == NULL)
8150	return NUM_EVENTS;
8151    return event_names[i].event;
8152}
8153
8154/*
8155 * Return the name for event "event".
8156 */
8157    static char_u *
8158event_nr2name(event)
8159    event_T	event;
8160{
8161    int	    i;
8162
8163    for (i = 0; event_names[i].name != NULL; ++i)
8164	if (event_names[i].event == event)
8165	    return (char_u *)event_names[i].name;
8166    return (char_u *)"Unknown";
8167}
8168
8169/*
8170 * Scan over the events.  "*" stands for all events.
8171 */
8172    static char_u *
8173find_end_event(arg, have_group)
8174    char_u  *arg;
8175    int	    have_group;	    /* TRUE when group name was found */
8176{
8177    char_u  *pat;
8178    char_u  *p;
8179
8180    if (*arg == '*')
8181    {
8182	if (arg[1] && !vim_iswhite(arg[1]))
8183	{
8184	    EMSG2(_("E215: Illegal character after *: %s"), arg);
8185	    return NULL;
8186	}
8187	pat = arg + 1;
8188    }
8189    else
8190    {
8191	for (pat = arg; *pat && !vim_iswhite(*pat); pat = p)
8192	{
8193	    if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS)
8194	    {
8195		if (have_group)
8196		    EMSG2(_("E216: No such event: %s"), pat);
8197		else
8198		    EMSG2(_("E216: No such group or event: %s"), pat);
8199		return NULL;
8200	    }
8201	}
8202    }
8203    return pat;
8204}
8205
8206/*
8207 * Return TRUE if "event" is included in 'eventignore'.
8208 */
8209    static int
8210event_ignored(event)
8211    event_T	event;
8212{
8213    char_u	*p = p_ei;
8214
8215    while (*p != NUL)
8216    {
8217	if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ','))
8218	    return TRUE;
8219	if (event_name2nr(p, &p) == event)
8220	    return TRUE;
8221    }
8222
8223    return FALSE;
8224}
8225
8226/*
8227 * Return OK when the contents of p_ei is valid, FAIL otherwise.
8228 */
8229    int
8230check_ei()
8231{
8232    char_u	*p = p_ei;
8233
8234    while (*p)
8235    {
8236	if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ','))
8237	{
8238	    p += 3;
8239	    if (*p == ',')
8240		++p;
8241	}
8242	else if (event_name2nr(p, &p) == NUM_EVENTS)
8243	    return FAIL;
8244    }
8245
8246    return OK;
8247}
8248
8249# if defined(FEAT_SYN_HL) || defined(PROTO)
8250
8251/*
8252 * Add "what" to 'eventignore' to skip loading syntax highlighting for every
8253 * buffer loaded into the window.  "what" must start with a comma.
8254 * Returns the old value of 'eventignore' in allocated memory.
8255 */
8256    char_u *
8257au_event_disable(what)
8258    char	*what;
8259{
8260    char_u	*new_ei;
8261    char_u	*save_ei;
8262
8263    save_ei = vim_strsave(p_ei);
8264    if (save_ei != NULL)
8265    {
8266	new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what)));
8267	if (new_ei != NULL)
8268	{
8269	    if (*what == ',' && *p_ei == NUL)
8270		STRCPY(new_ei, what + 1);
8271	    else
8272		STRCAT(new_ei, what);
8273	    set_string_option_direct((char_u *)"ei", -1, new_ei,
8274							  OPT_FREE, SID_NONE);
8275	    vim_free(new_ei);
8276	}
8277    }
8278    return save_ei;
8279}
8280
8281    void
8282au_event_restore(old_ei)
8283    char_u	*old_ei;
8284{
8285    if (old_ei != NULL)
8286    {
8287	set_string_option_direct((char_u *)"ei", -1, old_ei,
8288							  OPT_FREE, SID_NONE);
8289	vim_free(old_ei);
8290    }
8291}
8292# endif  /* FEAT_SYN_HL */
8293
8294/*
8295 * do_autocmd() -- implements the :autocmd command.  Can be used in the
8296 *  following ways:
8297 *
8298 * :autocmd <event> <pat> <cmd>	    Add <cmd> to the list of commands that
8299 *				    will be automatically executed for <event>
8300 *				    when editing a file matching <pat>, in
8301 *				    the current group.
8302 * :autocmd <event> <pat>	    Show the auto-commands associated with
8303 *				    <event> and <pat>.
8304 * :autocmd <event>		    Show the auto-commands associated with
8305 *				    <event>.
8306 * :autocmd			    Show all auto-commands.
8307 * :autocmd! <event> <pat> <cmd>    Remove all auto-commands associated with
8308 *				    <event> and <pat>, and add the command
8309 *				    <cmd>, for the current group.
8310 * :autocmd! <event> <pat>	    Remove all auto-commands associated with
8311 *				    <event> and <pat> for the current group.
8312 * :autocmd! <event>		    Remove all auto-commands associated with
8313 *				    <event> for the current group.
8314 * :autocmd!			    Remove ALL auto-commands for the current
8315 *				    group.
8316 *
8317 *  Multiple events and patterns may be given separated by commas.  Here are
8318 *  some examples:
8319 * :autocmd bufread,bufenter *.c,*.h	set tw=0 smartindent noic
8320 * :autocmd bufleave	     *		set tw=79 nosmartindent ic infercase
8321 *
8322 * :autocmd * *.c		show all autocommands for *.c files.
8323 *
8324 * Mostly a {group} argument can optionally appear before <event>.
8325 */
8326    void
8327do_autocmd(arg, forceit)
8328    char_u  *arg;
8329    int	    forceit;
8330{
8331    char_u	*pat;
8332    char_u	*envpat = NULL;
8333    char_u	*cmd;
8334    event_T	event;
8335    int		need_free = FALSE;
8336    int		nested = FALSE;
8337    int		group;
8338
8339    /*
8340     * Check for a legal group name.  If not, use AUGROUP_ALL.
8341     */
8342    group = au_get_grouparg(&arg);
8343    if (arg == NULL)	    /* out of memory */
8344	return;
8345
8346    /*
8347     * Scan over the events.
8348     * If we find an illegal name, return here, don't do anything.
8349     */
8350    pat = find_end_event(arg, group != AUGROUP_ALL);
8351    if (pat == NULL)
8352	return;
8353
8354    /*
8355     * Scan over the pattern.  Put a NUL at the end.
8356     */
8357    pat = skipwhite(pat);
8358    cmd = pat;
8359    while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\'))
8360	cmd++;
8361    if (*cmd)
8362	*cmd++ = NUL;
8363
8364    /* Expand environment variables in the pattern.  Set 'shellslash', we want
8365     * forward slashes here. */
8366    if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL)
8367    {
8368#ifdef BACKSLASH_IN_FILENAME
8369	int	p_ssl_save = p_ssl;
8370
8371	p_ssl = TRUE;
8372#endif
8373	envpat = expand_env_save(pat);
8374#ifdef BACKSLASH_IN_FILENAME
8375	p_ssl = p_ssl_save;
8376#endif
8377	if (envpat != NULL)
8378	    pat = envpat;
8379    }
8380
8381    /*
8382     * Check for "nested" flag.
8383     */
8384    cmd = skipwhite(cmd);
8385    if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6]))
8386    {
8387	nested = TRUE;
8388	cmd = skipwhite(cmd + 6);
8389    }
8390
8391    /*
8392     * Find the start of the commands.
8393     * Expand <sfile> in it.
8394     */
8395    if (*cmd != NUL)
8396    {
8397	cmd = expand_sfile(cmd);
8398	if (cmd == NULL)	    /* some error */
8399	    return;
8400	need_free = TRUE;
8401    }
8402
8403    /*
8404     * Print header when showing autocommands.
8405     */
8406    if (!forceit && *cmd == NUL)
8407    {
8408	/* Highlight title */
8409	MSG_PUTS_TITLE(_("\n--- Auto-Commands ---"));
8410    }
8411
8412    /*
8413     * Loop over the events.
8414     */
8415    last_event = (event_T)-1;		/* for listing the event name */
8416    last_group = AUGROUP_ERROR;		/* for listing the group name */
8417    if (*arg == '*' || *arg == NUL)
8418    {
8419	for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
8420					    event = (event_T)((int)event + 1))
8421	    if (do_autocmd_event(event, pat,
8422					 nested, cmd, forceit, group) == FAIL)
8423		break;
8424    }
8425    else
8426    {
8427	while (*arg && !vim_iswhite(*arg))
8428	    if (do_autocmd_event(event_name2nr(arg, &arg), pat,
8429					nested,	cmd, forceit, group) == FAIL)
8430		break;
8431    }
8432
8433    if (need_free)
8434	vim_free(cmd);
8435    vim_free(envpat);
8436}
8437
8438/*
8439 * Find the group ID in a ":autocmd" or ":doautocmd" argument.
8440 * The "argp" argument is advanced to the following argument.
8441 *
8442 * Returns the group ID, AUGROUP_ERROR for error (out of memory).
8443 */
8444    static int
8445au_get_grouparg(argp)
8446    char_u	**argp;
8447{
8448    char_u	*group_name;
8449    char_u	*p;
8450    char_u	*arg = *argp;
8451    int		group = AUGROUP_ALL;
8452
8453    p = skiptowhite(arg);
8454    if (p > arg)
8455    {
8456	group_name = vim_strnsave(arg, (int)(p - arg));
8457	if (group_name == NULL)		/* out of memory */
8458	    return AUGROUP_ERROR;
8459	group = au_find_group(group_name);
8460	if (group == AUGROUP_ERROR)
8461	    group = AUGROUP_ALL;	/* no match, use all groups */
8462	else
8463	    *argp = skipwhite(p);	/* match, skip over group name */
8464	vim_free(group_name);
8465    }
8466    return group;
8467}
8468
8469/*
8470 * do_autocmd() for one event.
8471 * If *pat == NUL do for all patterns.
8472 * If *cmd == NUL show entries.
8473 * If forceit == TRUE delete entries.
8474 * If group is not AUGROUP_ALL, only use this group.
8475 */
8476    static int
8477do_autocmd_event(event, pat, nested, cmd, forceit, group)
8478    event_T	event;
8479    char_u	*pat;
8480    int		nested;
8481    char_u	*cmd;
8482    int		forceit;
8483    int		group;
8484{
8485    AutoPat	*ap;
8486    AutoPat	**prev_ap;
8487    AutoCmd	*ac;
8488    AutoCmd	**prev_ac;
8489    int		brace_level;
8490    char_u	*endpat;
8491    int		findgroup;
8492    int		allgroups;
8493    int		patlen;
8494    int		is_buflocal;
8495    int		buflocal_nr;
8496    char_u	buflocal_pat[25];	/* for "<buffer=X>" */
8497
8498    if (group == AUGROUP_ALL)
8499	findgroup = current_augroup;
8500    else
8501	findgroup = group;
8502    allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL);
8503
8504    /*
8505     * Show or delete all patterns for an event.
8506     */
8507    if (*pat == NUL)
8508    {
8509	for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
8510	{
8511	    if (forceit)  /* delete the AutoPat, if it's in the current group */
8512	    {
8513		if (ap->group == findgroup)
8514		    au_remove_pat(ap);
8515	    }
8516	    else if (group == AUGROUP_ALL || ap->group == group)
8517		show_autocmd(ap, event);
8518	}
8519    }
8520
8521    /*
8522     * Loop through all the specified patterns.
8523     */
8524    for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat))
8525    {
8526	/*
8527	 * Find end of the pattern.
8528	 * Watch out for a comma in braces, like "*.\{obj,o\}".
8529	 */
8530	brace_level = 0;
8531	for (endpat = pat; *endpat && (*endpat != ',' || brace_level
8532					     || endpat[-1] == '\\'); ++endpat)
8533	{
8534	    if (*endpat == '{')
8535		brace_level++;
8536	    else if (*endpat == '}')
8537		brace_level--;
8538	}
8539	if (pat == endpat)		/* ignore single comma */
8540	    continue;
8541	patlen = (int)(endpat - pat);
8542
8543	/*
8544	 * detect special <buflocal[=X]> buffer-local patterns
8545	 */
8546	is_buflocal = FALSE;
8547	buflocal_nr = 0;
8548
8549	if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0
8550						    && pat[patlen - 1] == '>')
8551	{
8552	    /* Error will be printed only for addition. printing and removing
8553	     * will proceed silently. */
8554	    is_buflocal = TRUE;
8555	    if (patlen == 8)
8556		buflocal_nr = curbuf->b_fnum;
8557	    else if (patlen > 9 && pat[7] == '=')
8558	    {
8559		/* <buffer=abuf> */
8560		if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13))
8561		    buflocal_nr = autocmd_bufnr;
8562		/* <buffer=123> */
8563		else if (skipdigits(pat + 8) == pat + patlen - 1)
8564		    buflocal_nr = atoi((char *)pat + 8);
8565	    }
8566	}
8567
8568	if (is_buflocal)
8569	{
8570	    /* normalize pat into standard "<buffer>#N" form */
8571	    sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr);
8572	    pat = buflocal_pat;			/* can modify pat and patlen */
8573	    patlen = (int)STRLEN(buflocal_pat);	/*   but not endpat */
8574	}
8575
8576	/*
8577	 * Find AutoPat entries with this pattern.
8578	 */
8579	prev_ap = &first_autopat[(int)event];
8580	while ((ap = *prev_ap) != NULL)
8581	{
8582	    if (ap->pat != NULL)
8583	    {
8584		/* Accept a pattern when:
8585		 * - a group was specified and it's that group, or a group was
8586		 *   not specified and it's the current group, or a group was
8587		 *   not specified and we are listing
8588		 * - the length of the pattern matches
8589		 * - the pattern matches.
8590		 * For <buffer[=X]>, this condition works because we normalize
8591		 * all buffer-local patterns.
8592		 */
8593		if ((allgroups || ap->group == findgroup)
8594			&& ap->patlen == patlen
8595			&& STRNCMP(pat, ap->pat, patlen) == 0)
8596		{
8597		    /*
8598		     * Remove existing autocommands.
8599		     * If adding any new autocmd's for this AutoPat, don't
8600		     * delete the pattern from the autopat list, append to
8601		     * this list.
8602		     */
8603		    if (forceit)
8604		    {
8605			if (*cmd != NUL && ap->next == NULL)
8606			{
8607			    au_remove_cmds(ap);
8608			    break;
8609			}
8610			au_remove_pat(ap);
8611		    }
8612
8613		    /*
8614		     * Show autocmd's for this autopat, or buflocals <buffer=X>
8615		     */
8616		    else if (*cmd == NUL)
8617			show_autocmd(ap, event);
8618
8619		    /*
8620		     * Add autocmd to this autopat, if it's the last one.
8621		     */
8622		    else if (ap->next == NULL)
8623			break;
8624		}
8625	    }
8626	    prev_ap = &ap->next;
8627	}
8628
8629	/*
8630	 * Add a new command.
8631	 */
8632	if (*cmd != NUL)
8633	{
8634	    /*
8635	     * If the pattern we want to add a command to does appear at the
8636	     * end of the list (or not is not in the list at all), add the
8637	     * pattern at the end of the list.
8638	     */
8639	    if (ap == NULL)
8640	    {
8641		/* refuse to add buffer-local ap if buffer number is invalid */
8642		if (is_buflocal && (buflocal_nr == 0
8643				      || buflist_findnr(buflocal_nr) == NULL))
8644		{
8645		    EMSGN(_("E680: <buffer=%d>: invalid buffer number "),
8646								 buflocal_nr);
8647		    return FAIL;
8648		}
8649
8650		ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat));
8651		if (ap == NULL)
8652		    return FAIL;
8653		ap->pat = vim_strnsave(pat, patlen);
8654		ap->patlen = patlen;
8655		if (ap->pat == NULL)
8656		{
8657		    vim_free(ap);
8658		    return FAIL;
8659		}
8660
8661		if (is_buflocal)
8662		{
8663		    ap->buflocal_nr = buflocal_nr;
8664		    ap->reg_prog = NULL;
8665		}
8666		else
8667		{
8668		    char_u	*reg_pat;
8669
8670		    ap->buflocal_nr = 0;
8671		    reg_pat = file_pat_to_reg_pat(pat, endpat,
8672							 &ap->allow_dirs, TRUE);
8673		    if (reg_pat != NULL)
8674			ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC);
8675		    vim_free(reg_pat);
8676		    if (reg_pat == NULL || ap->reg_prog == NULL)
8677		    {
8678			vim_free(ap->pat);
8679			vim_free(ap);
8680			return FAIL;
8681		    }
8682		}
8683		ap->cmds = NULL;
8684		*prev_ap = ap;
8685		ap->next = NULL;
8686		if (group == AUGROUP_ALL)
8687		    ap->group = current_augroup;
8688		else
8689		    ap->group = group;
8690	    }
8691
8692	    /*
8693	     * Add the autocmd at the end of the AutoCmd list.
8694	     */
8695	    prev_ac = &(ap->cmds);
8696	    while ((ac = *prev_ac) != NULL)
8697		prev_ac = &ac->next;
8698	    ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd));
8699	    if (ac == NULL)
8700		return FAIL;
8701	    ac->cmd = vim_strsave(cmd);
8702#ifdef FEAT_EVAL
8703	    ac->scriptID = current_SID;
8704#endif
8705	    if (ac->cmd == NULL)
8706	    {
8707		vim_free(ac);
8708		return FAIL;
8709	    }
8710	    ac->next = NULL;
8711	    *prev_ac = ac;
8712	    ac->nested = nested;
8713	}
8714    }
8715
8716    au_cleanup();	/* may really delete removed patterns/commands now */
8717    return OK;
8718}
8719
8720/*
8721 * Implementation of ":doautocmd [group] event [fname]".
8722 * Return OK for success, FAIL for failure;
8723 */
8724    int
8725do_doautocmd(arg, do_msg)
8726    char_u	*arg;
8727    int		do_msg;	    /* give message for no matching autocmds? */
8728{
8729    char_u	*fname;
8730    int		nothing_done = TRUE;
8731    int		group;
8732
8733    /*
8734     * Check for a legal group name.  If not, use AUGROUP_ALL.
8735     */
8736    group = au_get_grouparg(&arg);
8737    if (arg == NULL)	    /* out of memory */
8738	return FAIL;
8739
8740    if (*arg == '*')
8741    {
8742	EMSG(_("E217: Can't execute autocommands for ALL events"));
8743	return FAIL;
8744    }
8745
8746    /*
8747     * Scan over the events.
8748     * If we find an illegal name, return here, don't do anything.
8749     */
8750    fname = find_end_event(arg, group != AUGROUP_ALL);
8751    if (fname == NULL)
8752	return FAIL;
8753
8754    fname = skipwhite(fname);
8755
8756    /*
8757     * Loop over the events.
8758     */
8759    while (*arg && !vim_iswhite(*arg))
8760	if (apply_autocmds_group(event_name2nr(arg, &arg),
8761				      fname, NULL, TRUE, group, curbuf, NULL))
8762	    nothing_done = FALSE;
8763
8764    if (nothing_done && do_msg)
8765	MSG(_("No matching autocommands"));
8766
8767#ifdef FEAT_EVAL
8768    return aborting() ? FAIL : OK;
8769#else
8770    return OK;
8771#endif
8772}
8773
8774/*
8775 * ":doautoall": execute autocommands for each loaded buffer.
8776 */
8777    void
8778ex_doautoall(eap)
8779    exarg_T	*eap;
8780{
8781    int		retval;
8782    aco_save_T	aco;
8783    buf_T	*buf;
8784
8785    /*
8786     * This is a bit tricky: For some commands curwin->w_buffer needs to be
8787     * equal to curbuf, but for some buffers there may not be a window.
8788     * So we change the buffer for the current window for a moment.  This
8789     * gives problems when the autocommands make changes to the list of
8790     * buffers or windows...
8791     */
8792    for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8793    {
8794	if (buf->b_ml.ml_mfp != NULL)
8795	{
8796	    /* find a window for this buffer and save some values */
8797	    aucmd_prepbuf(&aco, buf);
8798
8799	    /* execute the autocommands for this buffer */
8800	    retval = do_doautocmd(eap->arg, FALSE);
8801
8802	    /* Execute the modeline settings, but don't set window-local
8803	     * options if we are using the current window for another buffer. */
8804	    do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0);
8805
8806	    /* restore the current window */
8807	    aucmd_restbuf(&aco);
8808
8809	    /* stop if there is some error or buffer was deleted */
8810	    if (retval == FAIL || !buf_valid(buf))
8811		break;
8812	}
8813    }
8814
8815    check_cursor();	    /* just in case lines got deleted */
8816}
8817
8818/*
8819 * Prepare for executing autocommands for (hidden) buffer "buf".
8820 * Search for a visible window containing the current buffer.  If there isn't
8821 * one then use "aucmd_win".
8822 * Set "curbuf" and "curwin" to match "buf".
8823 * When FEAT_AUTOCMD is not defined another version is used, see below.
8824 */
8825    void
8826aucmd_prepbuf(aco, buf)
8827    aco_save_T	*aco;		/* structure to save values in */
8828    buf_T	*buf;		/* new curbuf */
8829{
8830    win_T	*win;
8831#ifdef FEAT_WINDOWS
8832    int		save_ea;
8833#endif
8834
8835    /* Find a window that is for the new buffer */
8836    if (buf == curbuf)		/* be quick when buf is curbuf */
8837	win = curwin;
8838    else
8839#ifdef FEAT_WINDOWS
8840	for (win = firstwin; win != NULL; win = win->w_next)
8841	    if (win->w_buffer == buf)
8842		break;
8843#else
8844	win = NULL;
8845#endif
8846
8847    /* Allocate "aucmd_win" when needed.  If this fails (out of memory) fall
8848     * back to using the current window. */
8849    if (win == NULL && aucmd_win == NULL)
8850    {
8851	win_alloc_aucmd_win();
8852	if (aucmd_win == NULL)
8853	    win = curwin;
8854    }
8855    if (win == NULL && aucmd_win_used)
8856	/* Strange recursive autocommand, fall back to using the current
8857	 * window.  Expect a few side effects... */
8858	win = curwin;
8859
8860    aco->save_curwin = curwin;
8861    aco->save_curbuf = curbuf;
8862    if (win != NULL)
8863    {
8864	/* There is a window for "buf" in the current tab page, make it the
8865	 * curwin.  This is preferred, it has the least side effects (esp. if
8866	 * "buf" is curbuf). */
8867	aco->use_aucmd_win = FALSE;
8868	curwin = win;
8869    }
8870    else
8871    {
8872	/* There is no window for "buf", use "aucmd_win".  To minimize the side
8873	 * effects, insert it in a the current tab page.
8874	 * Anything related to a window (e.g., setting folds) may have
8875	 * unexpected results. */
8876	aco->use_aucmd_win = TRUE;
8877	aucmd_win_used = TRUE;
8878	aucmd_win->w_buffer = buf;
8879	aucmd_win->w_s = &buf->b_s;
8880	++buf->b_nwindows;
8881	win_init_empty(aucmd_win); /* set cursor and topline to safe values */
8882	vim_free(aucmd_win->w_localdir);
8883	aucmd_win->w_localdir = NULL;
8884
8885	/* Make sure w_localdir and globaldir are NULL to avoid a chdir() in
8886	 * win_enter_ext(). */
8887	aucmd_win->w_localdir = NULL;
8888	aco->globaldir = globaldir;
8889	globaldir = NULL;
8890
8891
8892#ifdef FEAT_WINDOWS
8893	/* Split the current window, put the aucmd_win in the upper half.
8894	 * We don't want the BufEnter or WinEnter autocommands. */
8895	block_autocmds();
8896	make_snapshot(SNAP_AUCMD_IDX);
8897	save_ea = p_ea;
8898	p_ea = FALSE;
8899	(void)win_split_ins(0, WSP_TOP, aucmd_win, 0);
8900	(void)win_comp_pos();   /* recompute window positions */
8901	p_ea = save_ea;
8902	unblock_autocmds();
8903#endif
8904	curwin = aucmd_win;
8905    }
8906    curbuf = buf;
8907    aco->new_curwin = curwin;
8908    aco->new_curbuf = curbuf;
8909}
8910
8911/*
8912 * Cleanup after executing autocommands for a (hidden) buffer.
8913 * Restore the window as it was (if possible).
8914 * When FEAT_AUTOCMD is not defined another version is used, see below.
8915 */
8916    void
8917aucmd_restbuf(aco)
8918    aco_save_T	*aco;		/* structure holding saved values */
8919{
8920#ifdef FEAT_WINDOWS
8921    int dummy;
8922#endif
8923
8924    if (aco->use_aucmd_win)
8925    {
8926	--curbuf->b_nwindows;
8927#ifdef FEAT_WINDOWS
8928	/* Find "aucmd_win", it can't be closed, but it may be in another tab
8929	 * page. Do not trigger autocommands here. */
8930	block_autocmds();
8931	if (curwin != aucmd_win)
8932	{
8933	    tabpage_T	*tp;
8934	    win_T	*wp;
8935
8936	    FOR_ALL_TAB_WINDOWS(tp, wp)
8937	    {
8938		if (wp == aucmd_win)
8939		{
8940		    if (tp != curtab)
8941			goto_tabpage_tp(tp);
8942		    win_goto(aucmd_win);
8943		    break;
8944		}
8945	    }
8946	}
8947
8948	/* Remove the window and frame from the tree of frames. */
8949	(void)winframe_remove(curwin, &dummy, NULL);
8950	win_remove(curwin, NULL);
8951	aucmd_win_used = FALSE;
8952	last_status(FALSE);	    /* may need to remove last status line */
8953	restore_snapshot(SNAP_AUCMD_IDX, FALSE);
8954	(void)win_comp_pos();   /* recompute window positions */
8955	unblock_autocmds();
8956
8957	if (win_valid(aco->save_curwin))
8958	    curwin = aco->save_curwin;
8959	else
8960	    /* Hmm, original window disappeared.  Just use the first one. */
8961	    curwin = firstwin;
8962# ifdef FEAT_EVAL
8963	vars_clear(&aucmd_win->w_vars.dv_hashtab);  /* free all w: variables */
8964	hash_init(&aucmd_win->w_vars.dv_hashtab);   /* re-use the hashtab */
8965# endif
8966#else
8967	curwin = aco->save_curwin;
8968#endif
8969	curbuf = curwin->w_buffer;
8970
8971	vim_free(globaldir);
8972	globaldir = aco->globaldir;
8973
8974	/* the buffer contents may have changed */
8975	check_cursor();
8976	if (curwin->w_topline > curbuf->b_ml.ml_line_count)
8977	{
8978	    curwin->w_topline = curbuf->b_ml.ml_line_count;
8979#ifdef FEAT_DIFF
8980	    curwin->w_topfill = 0;
8981#endif
8982	}
8983#if defined(FEAT_GUI)
8984	/* Hide the scrollbars from the aucmd_win and update. */
8985	gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE);
8986	gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE);
8987	gui_may_update_scrollbars();
8988#endif
8989    }
8990    else
8991    {
8992	/* restore curwin */
8993#ifdef FEAT_WINDOWS
8994	if (win_valid(aco->save_curwin))
8995#endif
8996	{
8997	    /* Restore the buffer which was previously edited by curwin, if
8998	     * it was changed, we are still the same window and the buffer is
8999	     * valid. */
9000	    if (curwin == aco->new_curwin
9001		    && curbuf != aco->new_curbuf
9002		    && buf_valid(aco->new_curbuf)
9003		    && aco->new_curbuf->b_ml.ml_mfp != NULL)
9004	    {
9005		--curbuf->b_nwindows;
9006		curbuf = aco->new_curbuf;
9007		curwin->w_buffer = curbuf;
9008		++curbuf->b_nwindows;
9009	    }
9010
9011	    curwin = aco->save_curwin;
9012	    curbuf = curwin->w_buffer;
9013	}
9014    }
9015}
9016
9017static int	autocmd_nested = FALSE;
9018
9019/*
9020 * Execute autocommands for "event" and file name "fname".
9021 * Return TRUE if some commands were executed.
9022 */
9023    int
9024apply_autocmds(event, fname, fname_io, force, buf)
9025    event_T	event;
9026    char_u	*fname;	    /* NULL or empty means use actual file name */
9027    char_u	*fname_io;  /* fname to use for <afile> on cmdline */
9028    int		force;	    /* when TRUE, ignore autocmd_busy */
9029    buf_T	*buf;	    /* buffer for <abuf> */
9030{
9031    return apply_autocmds_group(event, fname, fname_io, force,
9032						      AUGROUP_ALL, buf, NULL);
9033}
9034
9035/*
9036 * Like apply_autocmds(), but with extra "eap" argument.  This takes care of
9037 * setting v:filearg.
9038 */
9039    static int
9040apply_autocmds_exarg(event, fname, fname_io, force, buf, eap)
9041    event_T	event;
9042    char_u	*fname;
9043    char_u	*fname_io;
9044    int		force;
9045    buf_T	*buf;
9046    exarg_T	*eap;
9047{
9048    return apply_autocmds_group(event, fname, fname_io, force,
9049						       AUGROUP_ALL, buf, eap);
9050}
9051
9052/*
9053 * Like apply_autocmds(), but handles the caller's retval.  If the script
9054 * processing is being aborted or if retval is FAIL when inside a try
9055 * conditional, no autocommands are executed.  If otherwise the autocommands
9056 * cause the script to be aborted, retval is set to FAIL.
9057 */
9058    int
9059apply_autocmds_retval(event, fname, fname_io, force, buf, retval)
9060    event_T	event;
9061    char_u	*fname;	    /* NULL or empty means use actual file name */
9062    char_u	*fname_io;  /* fname to use for <afile> on cmdline */
9063    int		force;	    /* when TRUE, ignore autocmd_busy */
9064    buf_T	*buf;	    /* buffer for <abuf> */
9065    int		*retval;    /* pointer to caller's retval */
9066{
9067    int		did_cmd;
9068
9069#ifdef FEAT_EVAL
9070    if (should_abort(*retval))
9071	return FALSE;
9072#endif
9073
9074    did_cmd = apply_autocmds_group(event, fname, fname_io, force,
9075						      AUGROUP_ALL, buf, NULL);
9076    if (did_cmd
9077#ifdef FEAT_EVAL
9078	    && aborting()
9079#endif
9080	    )
9081	*retval = FAIL;
9082    return did_cmd;
9083}
9084
9085/*
9086 * Return TRUE when there is a CursorHold autocommand defined.
9087 */
9088    int
9089has_cursorhold()
9090{
9091    return (first_autopat[(int)(get_real_state() == NORMAL_BUSY
9092			    ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL);
9093}
9094
9095/*
9096 * Return TRUE if the CursorHold event can be triggered.
9097 */
9098    int
9099trigger_cursorhold()
9100{
9101    int		state;
9102
9103    if (!did_cursorhold && has_cursorhold() && !Recording
9104#ifdef FEAT_INS_EXPAND
9105	    && !ins_compl_active()
9106#endif
9107	    )
9108    {
9109	state = get_real_state();
9110	if (state == NORMAL_BUSY || (state & INSERT) != 0)
9111	    return TRUE;
9112    }
9113    return FALSE;
9114}
9115
9116/*
9117 * Return TRUE when there is a CursorMoved autocommand defined.
9118 */
9119    int
9120has_cursormoved()
9121{
9122    return (first_autopat[(int)EVENT_CURSORMOVED] != NULL);
9123}
9124
9125/*
9126 * Return TRUE when there is a CursorMovedI autocommand defined.
9127 */
9128    int
9129has_cursormovedI()
9130{
9131    return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
9132}
9133
9134    static int
9135apply_autocmds_group(event, fname, fname_io, force, group, buf, eap)
9136    event_T	event;
9137    char_u	*fname;	    /* NULL or empty means use actual file name */
9138    char_u	*fname_io;  /* fname to use for <afile> on cmdline, NULL means
9139			       use fname */
9140    int		force;	    /* when TRUE, ignore autocmd_busy */
9141    int		group;	    /* group ID, or AUGROUP_ALL */
9142    buf_T	*buf;	    /* buffer for <abuf> */
9143    exarg_T	*eap;	    /* command arguments */
9144{
9145    char_u	*sfname = NULL;	/* short file name */
9146    char_u	*tail;
9147    int		save_changed;
9148    buf_T	*old_curbuf;
9149    int		retval = FALSE;
9150    char_u	*save_sourcing_name;
9151    linenr_T	save_sourcing_lnum;
9152    char_u	*save_autocmd_fname;
9153    int		save_autocmd_fname_full;
9154    int		save_autocmd_bufnr;
9155    char_u	*save_autocmd_match;
9156    int		save_autocmd_busy;
9157    int		save_autocmd_nested;
9158    static int	nesting = 0;
9159    AutoPatCmd	patcmd;
9160    AutoPat	*ap;
9161#ifdef FEAT_EVAL
9162    scid_T	save_current_SID;
9163    void	*save_funccalp;
9164    char_u	*save_cmdarg;
9165    long	save_cmdbang;
9166#endif
9167    static int	filechangeshell_busy = FALSE;
9168#ifdef FEAT_PROFILE
9169    proftime_T	wait_time;
9170#endif
9171
9172    /*
9173     * Quickly return if there are no autocommands for this event or
9174     * autocommands are blocked.
9175     */
9176    if (first_autopat[(int)event] == NULL || autocmd_blocked > 0)
9177	goto BYPASS_AU;
9178
9179    /*
9180     * When autocommands are busy, new autocommands are only executed when
9181     * explicitly enabled with the "nested" flag.
9182     */
9183    if (autocmd_busy && !(force || autocmd_nested))
9184	goto BYPASS_AU;
9185
9186#ifdef FEAT_EVAL
9187    /*
9188     * Quickly return when immediately aborting on error, or when an interrupt
9189     * occurred or an exception was thrown but not caught.
9190     */
9191    if (aborting())
9192	goto BYPASS_AU;
9193#endif
9194
9195    /*
9196     * FileChangedShell never nests, because it can create an endless loop.
9197     */
9198    if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL
9199				      || event == EVENT_FILECHANGEDSHELLPOST))
9200	goto BYPASS_AU;
9201
9202    /*
9203     * Ignore events in 'eventignore'.
9204     */
9205    if (event_ignored(event))
9206	goto BYPASS_AU;
9207
9208    /*
9209     * Allow nesting of autocommands, but restrict the depth, because it's
9210     * possible to create an endless loop.
9211     */
9212    if (nesting == 10)
9213    {
9214	EMSG(_("E218: autocommand nesting too deep"));
9215	goto BYPASS_AU;
9216    }
9217
9218    /*
9219     * Check if these autocommands are disabled.  Used when doing ":all" or
9220     * ":ball".
9221     */
9222    if (       (autocmd_no_enter
9223		&& (event == EVENT_WINENTER || event == EVENT_BUFENTER))
9224	    || (autocmd_no_leave
9225		&& (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE)))
9226	goto BYPASS_AU;
9227
9228    /*
9229     * Save the autocmd_* variables and info about the current buffer.
9230     */
9231    save_autocmd_fname = autocmd_fname;
9232    save_autocmd_fname_full = autocmd_fname_full;
9233    save_autocmd_bufnr = autocmd_bufnr;
9234    save_autocmd_match = autocmd_match;
9235    save_autocmd_busy = autocmd_busy;
9236    save_autocmd_nested = autocmd_nested;
9237    save_changed = curbuf->b_changed;
9238    old_curbuf = curbuf;
9239
9240    /*
9241     * Set the file name to be used for <afile>.
9242     * Make a copy to avoid that changing a buffer name or directory makes it
9243     * invalid.
9244     */
9245    if (fname_io == NULL)
9246    {
9247	if (fname != NULL && *fname != NUL)
9248	    autocmd_fname = fname;
9249	else if (buf != NULL)
9250	    autocmd_fname = buf->b_ffname;
9251	else
9252	    autocmd_fname = NULL;
9253    }
9254    else
9255	autocmd_fname = fname_io;
9256    if (autocmd_fname != NULL)
9257	autocmd_fname = vim_strsave(autocmd_fname);
9258    autocmd_fname_full = FALSE; /* call FullName_save() later */
9259
9260    /*
9261     * Set the buffer number to be used for <abuf>.
9262     */
9263    if (buf == NULL)
9264	autocmd_bufnr = 0;
9265    else
9266	autocmd_bufnr = buf->b_fnum;
9267
9268    /*
9269     * When the file name is NULL or empty, use the file name of buffer "buf".
9270     * Always use the full path of the file name to match with, in case
9271     * "allow_dirs" is set.
9272     */
9273    if (fname == NULL || *fname == NUL)
9274    {
9275	if (buf == NULL)
9276	    fname = NULL;
9277	else
9278	{
9279#ifdef FEAT_SYN_HL
9280	    if (event == EVENT_SYNTAX)
9281		fname = buf->b_p_syn;
9282	    else
9283#endif
9284		if (event == EVENT_FILETYPE)
9285		    fname = buf->b_p_ft;
9286		else
9287		{
9288		    if (buf->b_sfname != NULL)
9289			sfname = vim_strsave(buf->b_sfname);
9290		    fname = buf->b_ffname;
9291		}
9292	}
9293	if (fname == NULL)
9294	    fname = (char_u *)"";
9295	fname = vim_strsave(fname);	/* make a copy, so we can change it */
9296    }
9297    else
9298    {
9299	sfname = vim_strsave(fname);
9300	/* Don't try expanding FileType, Syntax, FuncUndefined, WindowID or
9301	 * QuickFixCmd* */
9302	if (event == EVENT_FILETYPE
9303		|| event == EVENT_SYNTAX
9304		|| event == EVENT_FUNCUNDEFINED
9305		|| event == EVENT_REMOTEREPLY
9306		|| event == EVENT_SPELLFILEMISSING
9307		|| event == EVENT_QUICKFIXCMDPRE
9308		|| event == EVENT_QUICKFIXCMDPOST)
9309	    fname = vim_strsave(fname);
9310	else
9311	    fname = FullName_save(fname, FALSE);
9312    }
9313    if (fname == NULL)	    /* out of memory */
9314    {
9315	vim_free(sfname);
9316	retval = FALSE;
9317	goto BYPASS_AU;
9318    }
9319
9320#ifdef BACKSLASH_IN_FILENAME
9321    /*
9322     * Replace all backslashes with forward slashes.  This makes the
9323     * autocommand patterns portable between Unix and MS-DOS.
9324     */
9325    if (sfname != NULL)
9326	forward_slash(sfname);
9327    forward_slash(fname);
9328#endif
9329
9330#ifdef VMS
9331    /* remove version for correct match */
9332    if (sfname != NULL)
9333	vms_remove_version(sfname);
9334    vms_remove_version(fname);
9335#endif
9336
9337    /*
9338     * Set the name to be used for <amatch>.
9339     */
9340    autocmd_match = fname;
9341
9342
9343    /* Don't redraw while doing auto commands. */
9344    ++RedrawingDisabled;
9345    save_sourcing_name = sourcing_name;
9346    sourcing_name = NULL;	/* don't free this one */
9347    save_sourcing_lnum = sourcing_lnum;
9348    sourcing_lnum = 0;		/* no line number here */
9349
9350#ifdef FEAT_EVAL
9351    save_current_SID = current_SID;
9352
9353# ifdef FEAT_PROFILE
9354    if (do_profiling == PROF_YES)
9355	prof_child_enter(&wait_time); /* doesn't count for the caller itself */
9356# endif
9357
9358    /* Don't use local function variables, if called from a function */
9359    save_funccalp = save_funccal();
9360#endif
9361
9362    /*
9363     * When starting to execute autocommands, save the search patterns.
9364     */
9365    if (!autocmd_busy)
9366    {
9367	save_search_patterns();
9368	saveRedobuff();
9369	did_filetype = keep_filetype;
9370    }
9371
9372    /*
9373     * Note that we are applying autocmds.  Some commands need to know.
9374     */
9375    autocmd_busy = TRUE;
9376    filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL);
9377    ++nesting;		/* see matching decrement below */
9378
9379    /* Remember that FileType was triggered.  Used for did_filetype(). */
9380    if (event == EVENT_FILETYPE)
9381	did_filetype = TRUE;
9382
9383    tail = gettail(fname);
9384
9385    /* Find first autocommand that matches */
9386    patcmd.curpat = first_autopat[(int)event];
9387    patcmd.nextcmd = NULL;
9388    patcmd.group = group;
9389    patcmd.fname = fname;
9390    patcmd.sfname = sfname;
9391    patcmd.tail = tail;
9392    patcmd.event = event;
9393    patcmd.arg_bufnr = autocmd_bufnr;
9394    patcmd.next = NULL;
9395    auto_next_pat(&patcmd, FALSE);
9396
9397    /* found one, start executing the autocommands */
9398    if (patcmd.curpat != NULL)
9399    {
9400	/* add to active_apc_list */
9401	patcmd.next = active_apc_list;
9402	active_apc_list = &patcmd;
9403
9404#ifdef FEAT_EVAL
9405	/* set v:cmdarg (only when there is a matching pattern) */
9406	save_cmdbang = get_vim_var_nr(VV_CMDBANG);
9407	if (eap != NULL)
9408	{
9409	    save_cmdarg = set_cmdarg(eap, NULL);
9410	    set_vim_var_nr(VV_CMDBANG, (long)eap->forceit);
9411	}
9412	else
9413	    save_cmdarg = NULL;	/* avoid gcc warning */
9414#endif
9415	retval = TRUE;
9416	/* mark the last pattern, to avoid an endless loop when more patterns
9417	 * are added when executing autocommands */
9418	for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next)
9419	    ap->last = FALSE;
9420	ap->last = TRUE;
9421	check_lnums(TRUE);	/* make sure cursor and topline are valid */
9422	do_cmdline(NULL, getnextac, (void *)&patcmd,
9423				     DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
9424#ifdef FEAT_EVAL
9425	if (eap != NULL)
9426	{
9427	    (void)set_cmdarg(NULL, save_cmdarg);
9428	    set_vim_var_nr(VV_CMDBANG, save_cmdbang);
9429	}
9430#endif
9431	/* delete from active_apc_list */
9432	if (active_apc_list == &patcmd)	    /* just in case */
9433	    active_apc_list = patcmd.next;
9434    }
9435
9436    --RedrawingDisabled;
9437    autocmd_busy = save_autocmd_busy;
9438    filechangeshell_busy = FALSE;
9439    autocmd_nested = save_autocmd_nested;
9440    vim_free(sourcing_name);
9441    sourcing_name = save_sourcing_name;
9442    sourcing_lnum = save_sourcing_lnum;
9443    vim_free(autocmd_fname);
9444    autocmd_fname = save_autocmd_fname;
9445    autocmd_fname_full = save_autocmd_fname_full;
9446    autocmd_bufnr = save_autocmd_bufnr;
9447    autocmd_match = save_autocmd_match;
9448#ifdef FEAT_EVAL
9449    current_SID = save_current_SID;
9450    restore_funccal(save_funccalp);
9451# ifdef FEAT_PROFILE
9452    if (do_profiling == PROF_YES)
9453	prof_child_exit(&wait_time);
9454# endif
9455#endif
9456    vim_free(fname);
9457    vim_free(sfname);
9458    --nesting;		/* see matching increment above */
9459
9460    /*
9461     * When stopping to execute autocommands, restore the search patterns and
9462     * the redo buffer.
9463     */
9464    if (!autocmd_busy)
9465    {
9466	restore_search_patterns();
9467	restoreRedobuff();
9468	did_filetype = FALSE;
9469    }
9470
9471    /*
9472     * Some events don't set or reset the Changed flag.
9473     * Check if still in the same buffer!
9474     */
9475    if (curbuf == old_curbuf
9476	    && (event == EVENT_BUFREADPOST
9477		|| event == EVENT_BUFWRITEPOST
9478		|| event == EVENT_FILEAPPENDPOST
9479		|| event == EVENT_VIMLEAVE
9480		|| event == EVENT_VIMLEAVEPRE))
9481    {
9482#ifdef FEAT_TITLE
9483	if (curbuf->b_changed != save_changed)
9484	    need_maketitle = TRUE;
9485#endif
9486	curbuf->b_changed = save_changed;
9487    }
9488
9489    au_cleanup();	/* may really delete removed patterns/commands now */
9490
9491BYPASS_AU:
9492    /* When wiping out a buffer make sure all its buffer-local autocommands
9493     * are deleted. */
9494    if (event == EVENT_BUFWIPEOUT && buf != NULL)
9495	aubuflocal_remove(buf);
9496
9497    return retval;
9498}
9499
9500# ifdef FEAT_EVAL
9501static char_u	*old_termresponse = NULL;
9502# endif
9503
9504/*
9505 * Block triggering autocommands until unblock_autocmd() is called.
9506 * Can be used recursively, so long as it's symmetric.
9507 */
9508    void
9509block_autocmds()
9510{
9511# ifdef FEAT_EVAL
9512    /* Remember the value of v:termresponse. */
9513    if (autocmd_blocked == 0)
9514	old_termresponse = get_vim_var_str(VV_TERMRESPONSE);
9515# endif
9516    ++autocmd_blocked;
9517}
9518
9519    void
9520unblock_autocmds()
9521{
9522    --autocmd_blocked;
9523
9524# ifdef FEAT_EVAL
9525    /* When v:termresponse was set while autocommands were blocked, trigger
9526     * the autocommands now.  Esp. useful when executing a shell command
9527     * during startup (vimdiff). */
9528    if (autocmd_blocked == 0
9529		      && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse)
9530	apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf);
9531# endif
9532}
9533
9534/*
9535 * Find next autocommand pattern that matches.
9536 */
9537    static void
9538auto_next_pat(apc, stop_at_last)
9539    AutoPatCmd	*apc;
9540    int		stop_at_last;	    /* stop when 'last' flag is set */
9541{
9542    AutoPat	*ap;
9543    AutoCmd	*cp;
9544    char_u	*name;
9545    char	*s;
9546
9547    vim_free(sourcing_name);
9548    sourcing_name = NULL;
9549
9550    for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next)
9551    {
9552	apc->curpat = NULL;
9553
9554	/* Only use a pattern when it has not been removed, has commands and
9555	 * the group matches. For buffer-local autocommands only check the
9556	 * buffer number. */
9557	if (ap->pat != NULL && ap->cmds != NULL
9558		&& (apc->group == AUGROUP_ALL || apc->group == ap->group))
9559	{
9560	    /* execution-condition */
9561	    if (ap->buflocal_nr == 0
9562		    ? (match_file_pat(NULL, ap->reg_prog, apc->fname,
9563				      apc->sfname, apc->tail, ap->allow_dirs))
9564		    : ap->buflocal_nr == apc->arg_bufnr)
9565	    {
9566		name = event_nr2name(apc->event);
9567		s = _("%s Auto commands for \"%s\"");
9568		sourcing_name = alloc((unsigned)(STRLEN(s)
9569					    + STRLEN(name) + ap->patlen + 1));
9570		if (sourcing_name != NULL)
9571		{
9572		    sprintf((char *)sourcing_name, s,
9573					       (char *)name, (char *)ap->pat);
9574		    if (p_verbose >= 8)
9575		    {
9576			verbose_enter();
9577			smsg((char_u *)_("Executing %s"), sourcing_name);
9578			verbose_leave();
9579		    }
9580		}
9581
9582		apc->curpat = ap;
9583		apc->nextcmd = ap->cmds;
9584		/* mark last command */
9585		for (cp = ap->cmds; cp->next != NULL; cp = cp->next)
9586		    cp->last = FALSE;
9587		cp->last = TRUE;
9588	    }
9589	    line_breakcheck();
9590	    if (apc->curpat != NULL)	    /* found a match */
9591		break;
9592	}
9593	if (stop_at_last && ap->last)
9594	    break;
9595    }
9596}
9597
9598/*
9599 * Get next autocommand command.
9600 * Called by do_cmdline() to get the next line for ":if".
9601 * Returns allocated string, or NULL for end of autocommands.
9602 */
9603    static char_u *
9604getnextac(c, cookie, indent)
9605    int	    c UNUSED;
9606    void    *cookie;
9607    int	    indent UNUSED;
9608{
9609    AutoPatCmd	    *acp = (AutoPatCmd *)cookie;
9610    char_u	    *retval;
9611    AutoCmd	    *ac;
9612
9613    /* Can be called again after returning the last line. */
9614    if (acp->curpat == NULL)
9615	return NULL;
9616
9617    /* repeat until we find an autocommand to execute */
9618    for (;;)
9619    {
9620	/* skip removed commands */
9621	while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL)
9622	    if (acp->nextcmd->last)
9623		acp->nextcmd = NULL;
9624	    else
9625		acp->nextcmd = acp->nextcmd->next;
9626
9627	if (acp->nextcmd != NULL)
9628	    break;
9629
9630	/* at end of commands, find next pattern that matches */
9631	if (acp->curpat->last)
9632	    acp->curpat = NULL;
9633	else
9634	    acp->curpat = acp->curpat->next;
9635	if (acp->curpat != NULL)
9636	    auto_next_pat(acp, TRUE);
9637	if (acp->curpat == NULL)
9638	    return NULL;
9639    }
9640
9641    ac = acp->nextcmd;
9642
9643    if (p_verbose >= 9)
9644    {
9645	verbose_enter_scroll();
9646	smsg((char_u *)_("autocommand %s"), ac->cmd);
9647	msg_puts((char_u *)"\n");   /* don't overwrite this either */
9648	verbose_leave_scroll();
9649    }
9650    retval = vim_strsave(ac->cmd);
9651    autocmd_nested = ac->nested;
9652#ifdef FEAT_EVAL
9653    current_SID = ac->scriptID;
9654#endif
9655    if (ac->last)
9656	acp->nextcmd = NULL;
9657    else
9658	acp->nextcmd = ac->next;
9659    return retval;
9660}
9661
9662/*
9663 * Return TRUE if there is a matching autocommand for "fname".
9664 * To account for buffer-local autocommands, function needs to know
9665 * in which buffer the file will be opened.
9666 */
9667    int
9668has_autocmd(event, sfname, buf)
9669    event_T	event;
9670    char_u	*sfname;
9671    buf_T       *buf;
9672{
9673    AutoPat	*ap;
9674    char_u	*fname;
9675    char_u	*tail = gettail(sfname);
9676    int		retval = FALSE;
9677
9678    fname = FullName_save(sfname, FALSE);
9679    if (fname == NULL)
9680	return FALSE;
9681
9682#ifdef BACKSLASH_IN_FILENAME
9683    /*
9684     * Replace all backslashes with forward slashes.  This makes the
9685     * autocommand patterns portable between Unix and MS-DOS.
9686     */
9687    sfname = vim_strsave(sfname);
9688    if (sfname != NULL)
9689	forward_slash(sfname);
9690    forward_slash(fname);
9691#endif
9692
9693    for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
9694	if (ap->pat != NULL && ap->cmds != NULL
9695	      && (ap->buflocal_nr == 0
9696		? match_file_pat(NULL, ap->reg_prog,
9697					  fname, sfname, tail, ap->allow_dirs)
9698		: buf != NULL && ap->buflocal_nr == buf->b_fnum
9699	   ))
9700	{
9701	    retval = TRUE;
9702	    break;
9703	}
9704
9705    vim_free(fname);
9706#ifdef BACKSLASH_IN_FILENAME
9707    vim_free(sfname);
9708#endif
9709
9710    return retval;
9711}
9712
9713#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
9714/*
9715 * Function given to ExpandGeneric() to obtain the list of autocommand group
9716 * names.
9717 */
9718    char_u *
9719get_augroup_name(xp, idx)
9720    expand_T	*xp UNUSED;
9721    int		idx;
9722{
9723    if (idx == augroups.ga_len)		/* add "END" add the end */
9724	return (char_u *)"END";
9725    if (idx >= augroups.ga_len)		/* end of list */
9726	return NULL;
9727    if (AUGROUP_NAME(idx) == NULL)	/* skip deleted entries */
9728	return (char_u *)"";
9729    return AUGROUP_NAME(idx);		/* return a name */
9730}
9731
9732static int include_groups = FALSE;
9733
9734    char_u  *
9735set_context_in_autocmd(xp, arg, doautocmd)
9736    expand_T	*xp;
9737    char_u	*arg;
9738    int		doautocmd;	/* TRUE for :doauto*, FALSE for :autocmd */
9739{
9740    char_u	*p;
9741    int		group;
9742
9743    /* check for a group name, skip it if present */
9744    include_groups = FALSE;
9745    p = arg;
9746    group = au_get_grouparg(&arg);
9747    if (group == AUGROUP_ERROR)
9748	return NULL;
9749    /* If there only is a group name that's what we expand. */
9750    if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1]))
9751    {
9752	arg = p;
9753	group = AUGROUP_ALL;
9754    }
9755
9756    /* skip over event name */
9757    for (p = arg; *p != NUL && !vim_iswhite(*p); ++p)
9758	if (*p == ',')
9759	    arg = p + 1;
9760    if (*p == NUL)
9761    {
9762	if (group == AUGROUP_ALL)
9763	    include_groups = TRUE;
9764	xp->xp_context = EXPAND_EVENTS;	    /* expand event name */
9765	xp->xp_pattern = arg;
9766	return NULL;
9767    }
9768
9769    /* skip over pattern */
9770    arg = skipwhite(p);
9771    while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\'))
9772	arg++;
9773    if (*arg)
9774	return arg;			    /* expand (next) command */
9775
9776    if (doautocmd)
9777	xp->xp_context = EXPAND_FILES;	    /* expand file names */
9778    else
9779	xp->xp_context = EXPAND_NOTHING;    /* pattern is not expanded */
9780    return NULL;
9781}
9782
9783/*
9784 * Function given to ExpandGeneric() to obtain the list of event names.
9785 */
9786    char_u *
9787get_event_name(xp, idx)
9788    expand_T	*xp UNUSED;
9789    int		idx;
9790{
9791    if (idx < augroups.ga_len)		/* First list group names, if wanted */
9792    {
9793	if (!include_groups || AUGROUP_NAME(idx) == NULL)
9794	    return (char_u *)"";	/* skip deleted entries */
9795	return AUGROUP_NAME(idx);	/* return a name */
9796    }
9797    return (char_u *)event_names[idx - augroups.ga_len].name;
9798}
9799
9800#endif	/* FEAT_CMDL_COMPL */
9801
9802/*
9803 * Return TRUE if autocmd is supported.
9804 */
9805    int
9806autocmd_supported(name)
9807    char_u	*name;
9808{
9809    char_u *p;
9810
9811    return (event_name2nr(name, &p) != NUM_EVENTS);
9812}
9813
9814/*
9815 * Return TRUE if an autocommand is defined for a group, event and
9816 * pattern:  The group can be omitted to accept any group. "event" and "pattern"
9817 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept
9818 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted.
9819 * Used for:
9820 *	exists("#Group") or
9821 *	exists("#Group#Event") or
9822 *	exists("#Group#Event#pat") or
9823 *	exists("#Event") or
9824 *	exists("#Event#pat")
9825 */
9826    int
9827au_exists(arg)
9828    char_u	*arg;
9829{
9830    char_u	*arg_save;
9831    char_u	*pattern = NULL;
9832    char_u	*event_name;
9833    char_u	*p;
9834    event_T	event;
9835    AutoPat	*ap;
9836    buf_T	*buflocal_buf = NULL;
9837    int		group;
9838    int		retval = FALSE;
9839
9840    /* Make a copy so that we can change the '#' chars to a NUL. */
9841    arg_save = vim_strsave(arg);
9842    if (arg_save == NULL)
9843	return FALSE;
9844    p = vim_strchr(arg_save, '#');
9845    if (p != NULL)
9846	*p++ = NUL;
9847
9848    /* First, look for an autocmd group name */
9849    group = au_find_group(arg_save);
9850    if (group == AUGROUP_ERROR)
9851    {
9852	/* Didn't match a group name, assume the first argument is an event. */
9853	group = AUGROUP_ALL;
9854	event_name = arg_save;
9855    }
9856    else
9857    {
9858	if (p == NULL)
9859	{
9860	    /* "Group": group name is present and it's recognized */
9861	    retval = TRUE;
9862	    goto theend;
9863	}
9864
9865	/* Must be "Group#Event" or "Group#Event#pat". */
9866	event_name = p;
9867	p = vim_strchr(event_name, '#');
9868	if (p != NULL)
9869	    *p++ = NUL;	    /* "Group#Event#pat" */
9870    }
9871
9872    pattern = p;	    /* "pattern" is NULL when there is no pattern */
9873
9874    /* find the index (enum) for the event name */
9875    event = event_name2nr(event_name, &p);
9876
9877    /* return FALSE if the event name is not recognized */
9878    if (event == NUM_EVENTS)
9879	goto theend;
9880
9881    /* Find the first autocommand for this event.
9882     * If there isn't any, return FALSE;
9883     * If there is one and no pattern given, return TRUE; */
9884    ap = first_autopat[(int)event];
9885    if (ap == NULL)
9886	goto theend;
9887
9888    /* if pattern is "<buffer>", special handling is needed which uses curbuf */
9889    /* for pattern "<buffer=N>, fnamecmp() will work fine */
9890    if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0)
9891	buflocal_buf = curbuf;
9892
9893    /* Check if there is an autocommand with the given pattern. */
9894    for ( ; ap != NULL; ap = ap->next)
9895	/* only use a pattern when it has not been removed and has commands. */
9896	/* For buffer-local autocommands, fnamecmp() works fine. */
9897	if (ap->pat != NULL && ap->cmds != NULL
9898	    && (group == AUGROUP_ALL || ap->group == group)
9899	    && (pattern == NULL
9900		|| (buflocal_buf == NULL
9901		    ? fnamecmp(ap->pat, pattern) == 0
9902		    : ap->buflocal_nr == buflocal_buf->b_fnum)))
9903	{
9904	    retval = TRUE;
9905	    break;
9906	}
9907
9908theend:
9909    vim_free(arg_save);
9910    return retval;
9911}
9912
9913#else	/* FEAT_AUTOCMD */
9914
9915/*
9916 * Prepare for executing commands for (hidden) buffer "buf".
9917 * This is the non-autocommand version, it simply saves "curbuf" and sets
9918 * "curbuf" and "curwin" to match "buf".
9919 */
9920    void
9921aucmd_prepbuf(aco, buf)
9922    aco_save_T	*aco;		/* structure to save values in */
9923    buf_T	*buf;		/* new curbuf */
9924{
9925    aco->save_curbuf = curbuf;
9926    --curbuf->b_nwindows;
9927    curbuf = buf;
9928    curwin->w_buffer = buf;
9929    ++curbuf->b_nwindows;
9930}
9931
9932/*
9933 * Restore after executing commands for a (hidden) buffer.
9934 * This is the non-autocommand version.
9935 */
9936    void
9937aucmd_restbuf(aco)
9938    aco_save_T	*aco;		/* structure holding saved values */
9939{
9940    --curbuf->b_nwindows;
9941    curbuf = aco->save_curbuf;
9942    curwin->w_buffer = curbuf;
9943    ++curbuf->b_nwindows;
9944}
9945
9946#endif	/* FEAT_AUTOCMD */
9947
9948
9949#if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO)
9950/*
9951 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
9952 * precompiled regprog "prog" ("pattern" is NULL).  That avoids calling
9953 * vim_regcomp() often.
9954 * Used for autocommands and 'wildignore'.
9955 * Returns TRUE if there is a match, FALSE otherwise.
9956 */
9957    int
9958match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs)
9959    char_u	*pattern;		/* pattern to match with */
9960    regprog_T	*prog;			/* pre-compiled regprog or NULL */
9961    char_u	*fname;			/* full path of file name */
9962    char_u	*sfname;		/* short file name or NULL */
9963    char_u	*tail;			/* tail of path */
9964    int		allow_dirs;		/* allow matching with dir */
9965{
9966    regmatch_T	regmatch;
9967    int		result = FALSE;
9968#ifdef FEAT_OSFILETYPE
9969    int		no_pattern = FALSE; /* TRUE if check is filetype only */
9970    char_u	*type_start;
9971    char_u	c;
9972    int		match = FALSE;
9973#endif
9974
9975#ifdef CASE_INSENSITIVE_FILENAME
9976    regmatch.rm_ic = TRUE;		/* Always ignore case */
9977#else
9978    regmatch.rm_ic = FALSE;		/* Don't ever ignore case */
9979#endif
9980#ifdef FEAT_OSFILETYPE
9981    if (*pattern == '<')
9982    {
9983	/* There is a filetype condition specified with this pattern.
9984	 * Check the filetype matches first. If not, don't bother with the
9985	 * pattern (set regprog to NULL).
9986	 * Always use magic for the regexp.
9987	 */
9988
9989	for (type_start = pattern + 1; (c = *pattern); pattern++)
9990	{
9991	    if ((c == ';' || c == '>') && match == FALSE)
9992	    {
9993		*pattern = NUL;	    /* Terminate the string */
9994		match = mch_check_filetype(fname, type_start);
9995		*pattern = c;	    /* Restore the terminator */
9996		type_start = pattern + 1;
9997	    }
9998	    if (c == '>')
9999		break;
10000	}
10001
10002	/* (c should never be NUL, but check anyway) */
10003	if (match == FALSE || c == NUL)
10004	    regmatch.regprog = NULL;	/* Doesn't match - don't check pat. */
10005	else if (*pattern == NUL)
10006	{
10007	    regmatch.regprog = NULL;	/* Vim will try to free regprog later */
10008	    no_pattern = TRUE;	/* Always matches - don't check pat. */
10009	}
10010	else
10011	    regmatch.regprog = vim_regcomp(pattern + 1, RE_MAGIC);
10012    }
10013    else
10014#endif
10015    {
10016	if (prog != NULL)
10017	    regmatch.regprog = prog;
10018	else
10019	    regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
10020    }
10021
10022    /*
10023     * Try for a match with the pattern with:
10024     * 1. the full file name, when the pattern has a '/'.
10025     * 2. the short file name, when the pattern has a '/'.
10026     * 3. the tail of the file name, when the pattern has no '/'.
10027     */
10028    if (
10029#ifdef FEAT_OSFILETYPE
10030	    /* If the check is for a filetype only and we don't care
10031	     * about the path then skip all the regexp stuff.
10032	     */
10033	    no_pattern ||
10034#endif
10035	    (regmatch.regprog != NULL
10036	     && ((allow_dirs
10037		     && (vim_regexec(&regmatch, fname, (colnr_T)0)
10038			 || (sfname != NULL
10039			     && vim_regexec(&regmatch, sfname, (colnr_T)0))))
10040		 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0)))))
10041	result = TRUE;
10042
10043    if (prog == NULL)
10044	vim_free(regmatch.regprog);
10045    return result;
10046}
10047#endif
10048
10049#if defined(FEAT_WILDIGN) || defined(PROTO)
10050/*
10051 * Return TRUE if a file matches with a pattern in "list".
10052 * "list" is a comma-separated list of patterns, like 'wildignore'.
10053 * "sfname" is the short file name or NULL, "ffname" the long file name.
10054 */
10055    int
10056match_file_list(list, sfname, ffname)
10057    char_u	*list;
10058    char_u	*sfname;
10059    char_u	*ffname;
10060{
10061    char_u	buf[100];
10062    char_u	*tail;
10063    char_u	*regpat;
10064    char	allow_dirs;
10065    int		match;
10066    char_u	*p;
10067
10068    tail = gettail(sfname);
10069
10070    /* try all patterns in 'wildignore' */
10071    p = list;
10072    while (*p)
10073    {
10074	copy_option_part(&p, buf, 100, ",");
10075	regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
10076	if (regpat == NULL)
10077	    break;
10078	match = match_file_pat(regpat, NULL, ffname, sfname,
10079						       tail, (int)allow_dirs);
10080	vim_free(regpat);
10081	if (match)
10082	    return TRUE;
10083    }
10084    return FALSE;
10085}
10086#endif
10087
10088/*
10089 * Convert the given pattern "pat" which has shell style wildcards in it, into
10090 * a regular expression, and return the result in allocated memory.  If there
10091 * is a directory path separator to be matched, then TRUE is put in
10092 * allow_dirs, otherwise FALSE is put there -- webb.
10093 * Handle backslashes before special characters, like "\*" and "\ ".
10094 *
10095 * If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg:
10096 * '<html>myfile' becomes '<html>^myfile$' -- leonard.
10097 *
10098 * Returns NULL when out of memory.
10099 */
10100    char_u *
10101file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash)
10102    char_u	*pat;
10103    char_u	*pat_end;	/* first char after pattern or NULL */
10104    char	*allow_dirs;	/* Result passed back out in here */
10105    int		no_bslash UNUSED; /* Don't use a backward slash as pathsep */
10106{
10107    int		size;
10108    char_u	*endp;
10109    char_u	*reg_pat;
10110    char_u	*p;
10111    int		i;
10112    int		nested = 0;
10113    int		add_dollar = TRUE;
10114#ifdef FEAT_OSFILETYPE
10115    int		check_length = 0;
10116#endif
10117
10118    if (allow_dirs != NULL)
10119	*allow_dirs = FALSE;
10120    if (pat_end == NULL)
10121	pat_end = pat + STRLEN(pat);
10122
10123#ifdef FEAT_OSFILETYPE
10124    /* Find out how much of the string is the filetype check */
10125    if (*pat == '<')
10126    {
10127	/* Count chars until the next '>' */
10128	for (p = pat + 1; p < pat_end && *p != '>'; p++)
10129	    ;
10130	if (p < pat_end)
10131	{
10132	    /* Pattern is of the form <.*>.*  */
10133	    check_length = p - pat + 1;
10134	    if (p + 1 >= pat_end)
10135	    {
10136		/* The 'pattern' is a filetype check ONLY */
10137		reg_pat = (char_u *)alloc(check_length + 1);
10138		if (reg_pat != NULL)
10139		{
10140		    mch_memmove(reg_pat, pat, (size_t)check_length);
10141		    reg_pat[check_length] = NUL;
10142		}
10143		return reg_pat;
10144	    }
10145	}
10146	/* else: there was no closing '>' - assume it was a normal pattern */
10147
10148    }
10149    pat += check_length;
10150    size = 2 + check_length;
10151#else
10152    size = 2;		/* '^' at start, '$' at end */
10153#endif
10154
10155    for (p = pat; p < pat_end; p++)
10156    {
10157	switch (*p)
10158	{
10159	    case '*':
10160	    case '.':
10161	    case ',':
10162	    case '{':
10163	    case '}':
10164	    case '~':
10165		size += 2;	/* extra backslash */
10166		break;
10167#ifdef BACKSLASH_IN_FILENAME
10168	    case '\\':
10169	    case '/':
10170		size += 4;	/* could become "[\/]" */
10171		break;
10172#endif
10173	    default:
10174		size++;
10175# ifdef  FEAT_MBYTE
10176		if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
10177		{
10178		    ++p;
10179		    ++size;
10180		}
10181# endif
10182		break;
10183	}
10184    }
10185    reg_pat = alloc(size + 1);
10186    if (reg_pat == NULL)
10187	return NULL;
10188
10189#ifdef FEAT_OSFILETYPE
10190    /* Copy the type check in to the start. */
10191    if (check_length)
10192	mch_memmove(reg_pat, pat - check_length, (size_t)check_length);
10193    i = check_length;
10194#else
10195    i = 0;
10196#endif
10197
10198    if (pat[0] == '*')
10199	while (pat[0] == '*' && pat < pat_end - 1)
10200	    pat++;
10201    else
10202	reg_pat[i++] = '^';
10203    endp = pat_end - 1;
10204    if (*endp == '*')
10205    {
10206	while (endp - pat > 0 && *endp == '*')
10207	    endp--;
10208	add_dollar = FALSE;
10209    }
10210    for (p = pat; *p && nested >= 0 && p <= endp; p++)
10211    {
10212	switch (*p)
10213	{
10214	    case '*':
10215		reg_pat[i++] = '.';
10216		reg_pat[i++] = '*';
10217		while (p[1] == '*')	/* "**" matches like "*" */
10218		    ++p;
10219		break;
10220	    case '.':
10221#ifdef RISCOS
10222		if (allow_dirs != NULL)
10223		     *allow_dirs = TRUE;
10224		/* FALLTHROUGH */
10225#endif
10226	    case '~':
10227		reg_pat[i++] = '\\';
10228		reg_pat[i++] = *p;
10229		break;
10230	    case '?':
10231#ifdef RISCOS
10232	    case '#':
10233#endif
10234		reg_pat[i++] = '.';
10235		break;
10236	    case '\\':
10237		if (p[1] == NUL)
10238		    break;
10239#ifdef BACKSLASH_IN_FILENAME
10240		if (!no_bslash)
10241		{
10242		    /* translate:
10243		     * "\x" to "\\x"  e.g., "dir\file"
10244		     * "\*" to "\\.*" e.g., "dir\*.c"
10245		     * "\?" to "\\."  e.g., "dir\??.c"
10246		     * "\+" to "\+"   e.g., "fileX\+.c"
10247		     */
10248		    if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
10249			    && p[1] != '+')
10250		    {
10251			reg_pat[i++] = '[';
10252			reg_pat[i++] = '\\';
10253			reg_pat[i++] = '/';
10254			reg_pat[i++] = ']';
10255			if (allow_dirs != NULL)
10256			    *allow_dirs = TRUE;
10257			break;
10258		    }
10259		}
10260#endif
10261		/* Undo escaping from ExpandEscape():
10262		 * foo\?bar -> foo?bar
10263		 * foo\%bar -> foo%bar
10264		 * foo\,bar -> foo,bar
10265		 * foo\ bar -> foo bar
10266		 * Don't unescape \, * and others that are also special in a
10267		 * regexp. */
10268		if (*++p == '?'
10269#ifdef BACKSLASH_IN_FILENAME
10270			&& no_bslash
10271#endif
10272			)
10273		    reg_pat[i++] = '?';
10274		else
10275		    if (*p == ',' || *p == '%' || *p == '#' || *p == ' ')
10276			reg_pat[i++] = *p;
10277		    else
10278		    {
10279			if (allow_dirs != NULL && vim_ispathsep(*p)
10280#ifdef BACKSLASH_IN_FILENAME
10281				&& (!no_bslash || *p != '\\')
10282#endif
10283				)
10284			    *allow_dirs = TRUE;
10285			reg_pat[i++] = '\\';
10286			reg_pat[i++] = *p;
10287		    }
10288		break;
10289#ifdef BACKSLASH_IN_FILENAME
10290	    case '/':
10291		reg_pat[i++] = '[';
10292		reg_pat[i++] = '\\';
10293		reg_pat[i++] = '/';
10294		reg_pat[i++] = ']';
10295		if (allow_dirs != NULL)
10296		    *allow_dirs = TRUE;
10297		break;
10298#endif
10299	    case '{':
10300		reg_pat[i++] = '\\';
10301		reg_pat[i++] = '(';
10302		nested++;
10303		break;
10304	    case '}':
10305		reg_pat[i++] = '\\';
10306		reg_pat[i++] = ')';
10307		--nested;
10308		break;
10309	    case ',':
10310		if (nested)
10311		{
10312		    reg_pat[i++] = '\\';
10313		    reg_pat[i++] = '|';
10314		}
10315		else
10316		    reg_pat[i++] = ',';
10317		break;
10318	    default:
10319# ifdef  FEAT_MBYTE
10320		if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
10321		    reg_pat[i++] = *p++;
10322		else
10323# endif
10324		if (allow_dirs != NULL && vim_ispathsep(*p))
10325		    *allow_dirs = TRUE;
10326		reg_pat[i++] = *p;
10327		break;
10328	}
10329    }
10330    if (add_dollar)
10331	reg_pat[i++] = '$';
10332    reg_pat[i] = NUL;
10333    if (nested != 0)
10334    {
10335	if (nested < 0)
10336	    EMSG(_("E219: Missing {."));
10337	else
10338	    EMSG(_("E220: Missing }."));
10339	vim_free(reg_pat);
10340	reg_pat = NULL;
10341    }
10342    return reg_pat;
10343}
10344