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