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