blob: 52eb34b3bfd12fc63dc839fd4ad0d0f5dd26a5f2 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * fileio.c: read from and write to a file
12 */
13
Bram Moolenaar071d4272004-06-13 20:20:40 +000014#include "vim.h"
15
Bram Moolenaarf4888d02009-12-02 12:31:27 +000016#if defined(__TANDEM) || defined(__MINT__)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017# 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#ifdef FEAT_CRYPT
Bram Moolenaar40e6a712010-05-16 22:32:54 +020028/* crypt_magic[0] is pkzip crypt, crypt_magic[1] is sha2+blowfish */
Bram Moolenaarf50a2532010-05-21 15:36:08 +020029static char *crypt_magic[] = {"VimCrypt~01!", "VimCrypt~02!"};
30static char crypt_magic_head[] = "VimCrypt~";
31# define CRYPT_MAGIC_LEN 12 /* must be multiple of 4! */
Bram Moolenaar80794b12010-06-13 05:20:42 +020032
33/* For blowfish, after the magic header, we store 8 bytes of salt and then 8
34 * bytes of seed (initialisation vector). */
35static int crypt_salt_len[] = {0, 8};
Bram Moolenaarf50a2532010-05-21 15:36:08 +020036static int crypt_seed_len[] = {0, 8};
Bram Moolenaar80794b12010-06-13 05:20:42 +020037#define CRYPT_SALT_LEN_MAX 8
Bram Moolenaar40e6a712010-05-16 22:32:54 +020038#define CRYPT_SEED_LEN_MAX 8
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#endif
40
41/* Is there any system that doesn't have access()? */
Bram Moolenaar9372a112005-12-06 19:59:18 +000042#define USE_MCH_ACCESS
Bram Moolenaar071d4272004-06-13 20:20:40 +000043
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +000044#if defined(sun) && defined(S_ISCHR)
45# define OPEN_CHR_FILES
46static int is_dev_fd_file(char_u *fname);
47#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#ifdef FEAT_MBYTE
49static char_u *next_fenc __ARGS((char_u **pp));
50# ifdef FEAT_EVAL
51static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp));
52# endif
53#endif
54#ifdef FEAT_VIMINFO
55static void check_marks_read __ARGS((void));
56#endif
57#ifdef FEAT_CRYPT
Bram Moolenaar49771f42010-07-20 17:32:38 +020058static int crypt_method_from_magic __ARGS((char *ptr, int len));
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +020059static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, char_u *fname, int *did_ask));
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61#ifdef UNIX
62static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime));
63#endif
Bram Moolenaar2d3f4892006-01-20 23:02:51 +000064static int set_rw_fname __ARGS((char_u *fname, char_u *sfname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000065static int msg_add_fileformat __ARGS((int eol_type));
Bram Moolenaar071d4272004-06-13 20:20:40 +000066static void msg_add_eol __ARGS((void));
67static int check_mtime __ARGS((buf_T *buf, struct stat *s));
68static int time_differs __ARGS((long t1, long t2));
69#ifdef FEAT_AUTOCMD
Bram Moolenaar754b5602006-02-09 23:53:20 +000070static int apply_autocmds_exarg __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap));
Bram Moolenaar70836c82006-02-20 21:28:49 +000071static int au_find_group __ARGS((char_u *name));
72
73# define AUGROUP_DEFAULT -1 /* default autocmd group */
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +000074# define AUGROUP_ERROR -2 /* erroneous autocmd group */
Bram Moolenaar70836c82006-02-20 21:28:49 +000075# define AUGROUP_ALL -3 /* all autocmd groups */
Bram Moolenaar071d4272004-06-13 20:20:40 +000076#endif
77
78#if defined(FEAT_CRYPT) || defined(FEAT_MBYTE)
79# define HAS_BW_FLAGS
80# define FIO_LATIN1 0x01 /* convert Latin1 */
81# define FIO_UTF8 0x02 /* convert UTF-8 */
82# define FIO_UCS2 0x04 /* convert UCS-2 */
83# define FIO_UCS4 0x08 /* convert UCS-4 */
84# define FIO_UTF16 0x10 /* convert UTF-16 */
85# ifdef WIN3264
86# define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */
87# define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */
88# define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */
89# endif
90# ifdef MACOS_X
91# define FIO_MACROMAN 0x20 /* convert MacRoman */
92# endif
93# define FIO_ENDIAN_L 0x80 /* little endian */
94# define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */
95# define FIO_NOCONVERT 0x2000 /* skip encoding conversion */
96# define FIO_UCSBOM 0x4000 /* check for BOM at start of file */
97# define FIO_ALL -1 /* allow all formats */
98#endif
99
100/* When converting, a read() or write() may leave some bytes to be converted
101 * for the next call. The value is guessed... */
102#define CONV_RESTLEN 30
103
104/* We have to guess how much a sequence of bytes may expand when converting
105 * with iconv() to be able to allocate a buffer. */
106#define ICONV_MULT 8
107
108/*
109 * Structure to pass arguments from buf_write() to buf_write_bytes().
110 */
111struct bw_info
112{
113 int bw_fd; /* file descriptor */
114 char_u *bw_buf; /* buffer with data to be written */
Bram Moolenaard089d9b2007-09-30 12:02:55 +0000115 int bw_len; /* length of data */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#ifdef HAS_BW_FLAGS
117 int bw_flags; /* FIO_ flags */
118#endif
119#ifdef FEAT_MBYTE
120 char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */
121 int bw_restlen; /* nr of bytes in bw_rest[] */
122 int bw_first; /* first write call */
123 char_u *bw_conv_buf; /* buffer for writing converted chars */
124 int bw_conv_buflen; /* size of bw_conv_buf */
125 int bw_conv_error; /* set for conversion error */
Bram Moolenaar32b485f2009-07-29 16:06:27 +0000126 linenr_T bw_conv_error_lnum; /* first line with error or zero */
127 linenr_T bw_start_lnum; /* line number at start of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# ifdef USE_ICONV
129 iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */
130# endif
131#endif
132};
133
134static int buf_write_bytes __ARGS((struct bw_info *ip));
135
136#ifdef FEAT_MBYTE
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000137static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags));
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +0000139static int need_conversion __ARGS((char_u *fenc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140static int get_fio_flags __ARGS((char_u *ptr));
141static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags));
142static int make_bom __ARGS((char_u *buf, char_u *name));
143# ifdef WIN3264
144static int get_win_fio_flags __ARGS((char_u *ptr));
145# endif
146# ifdef MACOS_X
147static int get_mac_fio_flags __ARGS((char_u *ptr));
148# endif
149#endif
150static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf));
Bram Moolenaar4592dee2009-11-18 19:11:58 +0000151#ifdef TEMPDIRNAMES
Bram Moolenaareaf03392009-11-17 11:08:52 +0000152static void vim_settempdir __ARGS((char_u *tempdir));
Bram Moolenaar4592dee2009-11-18 19:11:58 +0000153#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000154#ifdef FEAT_AUTOCMD
155static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name");
156#endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 void
159filemess(buf, name, s, attr)
160 buf_T *buf;
161 char_u *name;
162 char_u *s;
163 int attr;
164{
165 int msg_scroll_save;
166
167 if (msg_silent != 0)
168 return;
169 msg_add_fname(buf, name); /* put file name in IObuff with quotes */
170 /* If it's extremely long, truncate it. */
171 if (STRLEN(IObuff) > IOSIZE - 80)
172 IObuff[IOSIZE - 80] = NUL;
173 STRCAT(IObuff, s);
174 /*
175 * For the first message may have to start a new line.
176 * For further ones overwrite the previous one, reset msg_scroll before
177 * calling filemess().
178 */
179 msg_scroll_save = msg_scroll;
180 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
181 msg_scroll = FALSE;
182 if (!msg_scroll) /* wait a bit when overwriting an error msg */
183 check_for_delay(FALSE);
184 msg_start();
185 msg_scroll = msg_scroll_save;
186 msg_scrolled_ign = TRUE;
187 /* may truncate the message to avoid a hit-return prompt */
188 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
189 msg_clr_eos();
190 out_flush();
191 msg_scrolled_ign = FALSE;
192}
193
194/*
195 * Read lines from file "fname" into the buffer after line "from".
196 *
197 * 1. We allocate blocks with lalloc, as big as possible.
198 * 2. Each block is filled with characters from the file with a single read().
199 * 3. The lines are inserted in the buffer with ml_append().
200 *
201 * (caller must check that fname != NULL, unless READ_STDIN is used)
202 *
203 * "lines_to_skip" is the number of lines that must be skipped
204 * "lines_to_read" is the number of lines that are appended
205 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM.
206 *
207 * flags:
208 * READ_NEW starting to edit a new buffer
209 * READ_FILTER reading filter output
210 * READ_STDIN read from stdin instead of a file
211 * READ_BUFFER read from curbuf instead of a file (converting after reading
212 * stdin)
213 * READ_DUMMY read into a dummy buffer (to check if file contents changed)
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200214 * READ_KEEP_UNDO don't clear undo info or read it from a file
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 *
216 * return FAIL for failure, OK otherwise
217 */
218 int
219readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags)
220 char_u *fname;
221 char_u *sfname;
222 linenr_T from;
223 linenr_T lines_to_skip;
224 linenr_T lines_to_read;
225 exarg_T *eap; /* can be NULL! */
226 int flags;
227{
228 int fd = 0;
229 int newfile = (flags & READ_NEW);
230 int check_readonly;
231 int filtering = (flags & READ_FILTER);
232 int read_stdin = (flags & READ_STDIN);
233 int read_buffer = (flags & READ_BUFFER);
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000234 int set_options = newfile || read_buffer
235 || (eap != NULL && eap->read_edit);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */
237 colnr_T read_buf_col = 0; /* next char to read from this line */
238 char_u c;
239 linenr_T lnum = from;
240 char_u *ptr = NULL; /* pointer into read buffer */
241 char_u *buffer = NULL; /* read buffer */
242 char_u *new_buffer = NULL; /* init to shut up gcc */
243 char_u *line_start = NULL; /* init to shut up gcc */
244 int wasempty; /* buffer was empty before reading */
245 colnr_T len;
246 long size = 0;
247 char_u *p;
Bram Moolenaar914703b2010-05-31 21:59:46 +0200248 off_t filesize = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 int skip_read = FALSE;
250#ifdef FEAT_CRYPT
251 char_u *cryptkey = NULL;
Bram Moolenaarf50a2532010-05-21 15:36:08 +0200252 int did_ask_for_key = FALSE;
Bram Moolenaar4cf35c22011-02-25 16:52:17 +0100253 int crypt_method_used;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200255#ifdef FEAT_PERSISTENT_UNDO
256 context_sha256_T sha_ctx;
257 int read_undo_file = FALSE;
258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 int split = 0; /* number of split lines */
260#define UNKNOWN 0x0fffffff /* file size is unknown */
261 linenr_T linecnt;
262 int error = FALSE; /* errors encountered */
263 int ff_error = EOL_UNKNOWN; /* file format with errors */
264 long linerest = 0; /* remaining chars in line */
265#ifdef UNIX
266 int perm = 0;
267 int swap_mode = -1; /* protection bits for swap file */
268#else
269 int perm;
270#endif
271 int fileformat = 0; /* end-of-line format */
272 int keep_fileformat = FALSE;
273 struct stat st;
274 int file_readonly;
275 linenr_T skip_count = 0;
276 linenr_T read_count = 0;
277 int msg_save = msg_scroll;
278 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of
279 * last read was missing the eol */
280 int try_mac = (vim_strchr(p_ffs, 'm') != NULL);
281 int try_dos = (vim_strchr(p_ffs, 'd') != NULL);
282 int try_unix = (vim_strchr(p_ffs, 'x') != NULL);
283 int file_rewind = FALSE;
284#ifdef FEAT_MBYTE
285 int can_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000286 linenr_T conv_error = 0; /* line nr with conversion error */
287 linenr_T illegal_byte = 0; /* line nr with illegal byte */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit
289 in destination encoding */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000290 int bad_char_behavior = BAD_REPLACE;
291 /* BAD_KEEP, BAD_DROP or character to
292 * replace with */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293 char_u *tmpname = NULL; /* name of 'charconvert' output file */
294 int fio_flags = 0;
295 char_u *fenc; /* fileencoding to use */
296 int fenc_alloced; /* fenc_next is in allocated memory */
297 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */
298 int advance_fenc = FALSE;
299 long real_size = 0;
300# ifdef USE_ICONV
301 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */
302# ifdef FEAT_EVAL
303 int did_iconv = FALSE; /* TRUE when iconv() failed and trying
304 'charconvert' next */
305# endif
306# endif
307 int converted = FALSE; /* TRUE if conversion done */
308 int notconverted = FALSE; /* TRUE if conversion wanted but it
309 wasn't possible */
310 char_u conv_rest[CONV_RESTLEN];
311 int conv_restlen = 0; /* nr of bytes in conv_rest[] */
312#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000313#ifdef FEAT_AUTOCMD
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200314 buf_T *old_curbuf;
315 char_u *old_b_ffname;
316 char_u *old_b_fname;
317 int using_b_ffname;
318 int using_b_fname;
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000319#endif
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200320
Bram Moolenaarcab35ad2011-02-15 17:39:22 +0100321 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
323 /*
324 * If there is no file name yet, use the one for the read file.
325 * BF_NOTEDITED is set to reflect this.
326 * Don't do this for a read from a filter.
327 * Only do this when 'cpoptions' contains the 'f' flag.
328 */
329 if (curbuf->b_ffname == NULL
330 && !filtering
331 && fname != NULL
332 && vim_strchr(p_cpo, CPO_FNAMER) != NULL
333 && !(flags & READ_DUMMY))
334 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +0000335 if (set_rw_fname(fname, sfname) == FAIL)
336 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 }
338
Bram Moolenaarbb3d5dc2010-08-14 14:32:54 +0200339#ifdef FEAT_AUTOCMD
340 /* Remember the initial values of curbuf, curbuf->b_ffname and
341 * curbuf->b_fname to detect whether they are altered as a result of
342 * executing nasty autocommands. Also check if "fname" and "sfname"
343 * point to one of these values. */
344 old_curbuf = curbuf;
345 old_b_ffname = curbuf->b_ffname;
346 old_b_fname = curbuf->b_fname;
347 using_b_ffname = (fname == curbuf->b_ffname)
348 || (sfname == curbuf->b_ffname);
349 using_b_fname = (fname == curbuf->b_fname) || (sfname == curbuf->b_fname);
350#endif
351
Bram Moolenaardf177f62005-02-22 08:39:57 +0000352 /* After reading a file the cursor line changes but we don't want to
353 * display the line. */
354 ex_no_reprint = TRUE;
355
Bram Moolenaar55b7cf82006-09-09 12:52:42 +0000356 /* don't display the file info for another buffer now */
357 need_fileinfo = FALSE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 /*
360 * For Unix: Use the short file name whenever possible.
361 * Avoids problems with networks and when directory names are changed.
362 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
363 * another directory, which we don't detect.
364 */
365 if (sfname == NULL)
366 sfname = fname;
367#if defined(UNIX) || defined(__EMX__)
368 fname = sfname;
369#endif
370
371#ifdef FEAT_AUTOCMD
372 /*
373 * The BufReadCmd and FileReadCmd events intercept the reading process by
374 * executing the associated commands instead.
375 */
376 if (!filtering && !read_stdin && !read_buffer)
377 {
378 pos_T pos;
379
380 pos = curbuf->b_op_start;
381
382 /* Set '[ mark to the line above where the lines go (line 1 if zero). */
383 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
384 curbuf->b_op_start.col = 0;
385
386 if (newfile)
387 {
388 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
389 FALSE, curbuf, eap))
390#ifdef FEAT_EVAL
391 return aborting() ? FAIL : OK;
392#else
393 return OK;
394#endif
395 }
396 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
397 FALSE, NULL, eap))
398#ifdef FEAT_EVAL
399 return aborting() ? FAIL : OK;
400#else
401 return OK;
402#endif
403
404 curbuf->b_op_start = pos;
405 }
406#endif
407
408 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
409 msg_scroll = FALSE; /* overwrite previous file message */
410 else
411 msg_scroll = TRUE; /* don't overwrite previous file message */
412
413 /*
414 * If the name ends in a path separator, we can't open it. Check here,
415 * because reading the file may actually work, but then creating the swap
416 * file may destroy it! Reported on MS-DOS and Win 95.
417 * If the name is too long we might crash further on, quit here.
418 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000419 if (fname != NULL && *fname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000421 p = fname + STRLEN(fname);
422 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +0000423 {
424 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
425 msg_end();
426 msg_scroll = msg_save;
427 return FAIL;
428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 }
430
431#ifdef UNIX
432 /*
433 * On Unix it is possible to read a directory, so we have to
434 * check for it before the mch_open().
435 */
436 if (!read_stdin && !read_buffer)
437 {
438 perm = mch_getperm(fname);
439 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */
440# ifdef S_ISFIFO
441 && !S_ISFIFO(perm) /* ... or fifo */
442# endif
443# ifdef S_ISSOCK
444 && !S_ISSOCK(perm) /* ... or socket */
445# endif
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +0000446# ifdef OPEN_CHR_FILES
447 && !(S_ISCHR(perm) && is_dev_fd_file(fname))
448 /* ... or a character special file named /dev/fd/<n> */
449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 )
451 {
452 if (S_ISDIR(perm))
453 filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
454 else
455 filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
456 msg_end();
457 msg_scroll = msg_save;
458 return FAIL;
459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000461# if defined(MSDOS) || defined(MSWIN) || defined(OS2)
462 /*
463 * MS-Windows allows opening a device, but we will probably get stuck
464 * trying to read it.
465 */
466 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
467 {
Bram Moolenaar5386a122007-06-28 20:02:32 +0000468 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
Bram Moolenaarc67764a2006-10-12 19:14:26 +0000469 msg_end();
470 msg_scroll = msg_save;
471 return FAIL;
472 }
473# endif
Bram Moolenaar043545e2006-10-10 16:44:07 +0000474 }
475#endif
476
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477 /* set default 'fileformat' */
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000478 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479 {
480 if (eap != NULL && eap->force_ff != 0)
481 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
482 else if (*p_ffs != NUL)
483 set_fileformat(default_fileformat(), OPT_LOCAL);
484 }
485
486 /* set or reset 'binary' */
487 if (eap != NULL && eap->force_bin != 0)
488 {
489 int oldval = curbuf->b_p_bin;
490
491 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
492 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
493 }
494
495 /*
496 * When opening a new file we take the readonly flag from the file.
497 * Default is r/w, can be set to r/o below.
498 * Don't reset it when in readonly mode
499 * Only set/reset b_p_ro when BF_CHECK_RO is set.
500 */
501 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000502 if (check_readonly && !readonlymode)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 curbuf->b_p_ro = FALSE;
504
505 if (newfile && !read_stdin && !read_buffer)
506 {
Bram Moolenaare60acc12011-05-10 16:41:25 +0200507 /* Remember time of file. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 if (mch_stat((char *)fname, &st) >= 0)
509 {
510 buf_store_time(curbuf, &st, fname);
511 curbuf->b_mtime_read = curbuf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512#ifdef UNIX
513 /*
514 * Use the protection bits of the original file for the swap file.
515 * This makes it possible for others to read the name of the
516 * edited file from the swapfile, but only if they can read the
517 * edited file.
518 * Remove the "write" and "execute" bits for group and others
519 * (they must not write the swapfile).
520 * Add the "read" and "write" bits for the user, otherwise we may
521 * not be able to write to the file ourselves.
522 * Setting the bits is done below, after creating the swap file.
523 */
524 swap_mode = (st.st_mode & 0644) | 0600;
525#endif
526#ifdef FEAT_CW_EDITOR
527 /* Get the FSSpec on MacOS
528 * TODO: Update it properly when the buffer name changes
529 */
530 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
531#endif
532#ifdef VMS
533 curbuf->b_fab_rfm = st.st_fab_rfm;
Bram Moolenaard4755bb2004-09-02 19:12:26 +0000534 curbuf->b_fab_rat = st.st_fab_rat;
535 curbuf->b_fab_mrs = st.st_fab_mrs;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536#endif
537 }
538 else
539 {
540 curbuf->b_mtime = 0;
541 curbuf->b_mtime_read = 0;
542 curbuf->b_orig_size = 0;
543 curbuf->b_orig_mode = 0;
544 }
545
546 /* Reset the "new file" flag. It will be set again below when the
547 * file doesn't exist. */
548 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
549 }
550
551/*
552 * for UNIX: check readonly with perm and mch_access()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 * for MSDOS and Amiga: check readonly by trying to open the file for writing
554 */
555 file_readonly = FALSE;
556 if (read_stdin)
557 {
558#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
559 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */
560 setmode(0, O_BINARY);
561#endif
562 }
563 else if (!read_buffer)
564 {
565#ifdef USE_MCH_ACCESS
566 if (
567# ifdef UNIX
568 !(perm & 0222) ||
569# endif
570 mch_access((char *)fname, W_OK))
571 file_readonly = TRUE;
572 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
573#else
574 if (!newfile
575 || readonlymode
576 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
577 {
578 file_readonly = TRUE;
579 /* try to open ro */
580 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
581 }
582#endif
583 }
584
585 if (fd < 0) /* cannot open at all */
586 {
587#ifndef UNIX
588 int isdir_f;
589#endif
590 msg_scroll = msg_save;
591#ifndef UNIX
592 /*
593 * On MSDOS and Amiga we can't open a directory, check here.
594 */
595 isdir_f = (mch_isdir(fname));
596 perm = mch_getperm(fname); /* check if the file exists */
597 if (isdir_f)
598 {
599 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
600 curbuf->b_p_ro = TRUE; /* must use "w!" now */
601 }
602 else
603#endif
604 if (newfile)
605 {
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200606 if (perm < 0
607#ifdef ENOENT
608 && errno == ENOENT
609#endif
610 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 {
612 /*
613 * Set the 'new-file' flag, so that when the file has
614 * been created by someone else, a ":w" will complain.
615 */
616 curbuf->b_flags |= BF_NEW;
617
618 /* Create a swap file now, so that other Vims are warned
619 * that we are editing this file. Don't do this for a
620 * "nofile" or "nowrite" buffer type. */
621#ifdef FEAT_QUICKFIX
622 if (!bt_dontwrite(curbuf))
623#endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000626#ifdef FEAT_AUTOCMD
627 /* SwapExists autocommand may mess things up */
628 if (curbuf != old_curbuf
629 || (using_b_ffname
630 && (old_b_ffname != curbuf->b_ffname))
631 || (using_b_fname
632 && (old_b_fname != curbuf->b_fname)))
633 {
634 EMSG(_(e_auchangedbuf));
635 return FAIL;
636 }
637#endif
638 }
Bram Moolenaar5b962cf2005-12-12 21:58:40 +0000639 if (dir_of_file_exists(fname))
640 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
641 else
642 filemess(curbuf, sfname,
643 (char_u *)_("[New DIRECTORY]"), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#ifdef FEAT_VIMINFO
645 /* Even though this is a new file, it might have been
646 * edited before and deleted. Get the old marks. */
647 check_marks_read();
648#endif
649#ifdef FEAT_MBYTE
650 if (eap != NULL && eap->force_enc != 0)
651 {
652 /* set forced 'fileencoding' */
653 fenc = enc_canonize(eap->cmd + eap->force_enc);
654 if (fenc != NULL)
655 set_string_option_direct((char_u *)"fenc", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000656 fenc, OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 vim_free(fenc);
658 }
659#endif
660#ifdef FEAT_AUTOCMD
661 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
662 FALSE, curbuf, eap);
663#endif
664 /* remember the current fileformat */
665 save_file_ff(curbuf);
666
667#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
668 if (aborting()) /* autocmds may abort script processing */
669 return FAIL;
670#endif
671 return OK; /* a new file is not an error */
672 }
673 else
674 {
Bram Moolenaar202795b2005-10-11 20:29:39 +0000675 filemess(curbuf, sfname, (char_u *)(
676# ifdef EFBIG
677 (errno == EFBIG) ? _("[File too big]") :
678# endif
Bram Moolenaar2efbc662010-05-14 18:56:38 +0200679# ifdef EOVERFLOW
680 (errno == EOVERFLOW) ? _("[File too big]") :
681# endif
Bram Moolenaar202795b2005-10-11 20:29:39 +0000682 _("[Permission Denied]")), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 curbuf->b_p_ro = TRUE; /* must use "w!" now */
684 }
685 }
686
687 return FAIL;
688 }
689
690 /*
691 * Only set the 'ro' flag for readonly files the first time they are
692 * loaded. Help files always get readonly mode
693 */
694 if ((check_readonly && file_readonly) || curbuf->b_help)
695 curbuf->b_p_ro = TRUE;
696
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000697 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
Bram Moolenaar690ffc02008-01-04 15:31:21 +0000699 /* Don't change 'eol' if reading from buffer as it will already be
700 * correctly set when reading stdin. */
701 if (!read_buffer)
702 {
703 curbuf->b_p_eol = TRUE;
704 curbuf->b_start_eol = TRUE;
705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706#ifdef FEAT_MBYTE
707 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000708 curbuf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#endif
710 }
711
712 /* Create a swap file now, so that other Vims are warned that we are
713 * editing this file.
714 * Don't do this for a "nofile" or "nowrite" buffer type. */
715#ifdef FEAT_QUICKFIX
716 if (!bt_dontwrite(curbuf))
717#endif
718 {
719 check_need_swap(newfile);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000720#ifdef FEAT_AUTOCMD
721 if (!read_stdin && (curbuf != old_curbuf
722 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
723 || (using_b_fname && (old_b_fname != curbuf->b_fname))))
724 {
725 EMSG(_(e_auchangedbuf));
726 if (!read_buffer)
727 close(fd);
728 return FAIL;
729 }
730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731#ifdef UNIX
732 /* Set swap file protection bits after creating it. */
Bram Moolenaarf061e0b2009-06-24 15:32:01 +0000733 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL
734 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode);
736#endif
737 }
738
Bram Moolenaarb815dac2005-12-07 20:59:24 +0000739#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 /* If "Quit" selected at ATTENTION dialog, don't load the file */
741 if (swap_exists_action == SEA_QUIT)
742 {
743 if (!read_buffer && !read_stdin)
744 close(fd);
745 return FAIL;
746 }
747#endif
748
749 ++no_wait_return; /* don't wait for return yet */
750
751 /*
752 * Set '[ mark to the line above where the lines go (line 1 if zero).
753 */
754 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
755 curbuf->b_op_start.col = 0;
756
757#ifdef FEAT_AUTOCMD
758 if (!read_buffer)
759 {
760 int m = msg_scroll;
761 int n = msg_scrolled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763 /*
764 * The file must be closed again, the autocommands may want to change
765 * the file before reading it.
766 */
767 if (!read_stdin)
768 close(fd); /* ignore errors */
769
770 /*
771 * The output from the autocommands should not overwrite anything and
772 * should not be overwritten: Set msg_scroll, restore its value if no
773 * output was done.
774 */
775 msg_scroll = TRUE;
776 if (filtering)
777 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
778 FALSE, curbuf, eap);
779 else if (read_stdin)
780 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
781 FALSE, curbuf, eap);
782 else if (newfile)
783 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
784 FALSE, curbuf, eap);
785 else
786 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
787 FALSE, NULL, eap);
788 if (msg_scrolled == n)
789 msg_scroll = m;
790
791#ifdef FEAT_EVAL
792 if (aborting()) /* autocmds may abort script processing */
793 {
794 --no_wait_return;
795 msg_scroll = msg_save;
796 curbuf->b_p_ro = TRUE; /* must use "w!" now */
797 return FAIL;
798 }
799#endif
800 /*
801 * Don't allow the autocommands to change the current buffer.
802 * Try to re-open the file.
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000803 *
804 * Don't allow the autocommands to change the buffer name either
805 * (cd for example) if it invalidates fname or sfname.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 */
807 if (!read_stdin && (curbuf != old_curbuf
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +0000808 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname))
809 || (using_b_fname && (old_b_fname != curbuf->b_fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
811 {
812 --no_wait_return;
813 msg_scroll = msg_save;
814 if (fd < 0)
815 EMSG(_("E200: *ReadPre autocommands made the file unreadable"));
816 else
817 EMSG(_("E201: *ReadPre autocommands must not change current buffer"));
818 curbuf->b_p_ro = TRUE; /* must use "w!" now */
819 return FAIL;
820 }
821 }
822#endif /* FEAT_AUTOCMD */
823
824 /* Autocommands may add lines to the file, need to check if it is empty */
825 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
826
827 if (!recoverymode && !filtering && !(flags & READ_DUMMY))
828 {
829 /*
830 * Show the user that we are busy reading the input. Sometimes this
831 * may take a while. When reading from stdin another program may
832 * still be running, don't move the cursor to the last line, unless
833 * always using the GUI.
834 */
835 if (read_stdin)
836 {
837#ifndef ALWAYS_USE_GUI
838 mch_msg(_("Vim: Reading from stdin...\n"));
839#endif
840#ifdef FEAT_GUI
841 /* Also write a message in the GUI window, if there is one. */
842 if (gui.in_use && !gui.dying && !gui.starting)
843 {
844 p = (char_u *)_("Reading from stdin...");
845 gui_write(p, (int)STRLEN(p));
846 }
847#endif
848 }
849 else if (!read_buffer)
850 filemess(curbuf, sfname, (char_u *)"", 0);
851 }
852
853 msg_scroll = FALSE; /* overwrite the file message */
854
855 /*
856 * Set linecnt now, before the "retry" caused by a wrong guess for
857 * fileformat, and after the autocommands, which may change them.
858 */
859 linecnt = curbuf->b_ml.ml_line_count;
860
861#ifdef FEAT_MBYTE
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000862 /* "++bad=" argument. */
863 if (eap != NULL && eap->bad_char != 0)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000864 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000865 bad_char_behavior = eap->bad_char;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000866 if (set_options)
Bram Moolenaar195d6352005-12-19 22:08:24 +0000867 curbuf->b_bad_char = eap->bad_char;
868 }
869 else
870 curbuf->b_bad_char = 0;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000871
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000873 * Decide which 'encoding' to use or use first.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 */
875 if (eap != NULL && eap->force_enc != 0)
876 {
877 fenc = enc_canonize(eap->cmd + eap->force_enc);
878 fenc_alloced = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000879 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 else if (curbuf->b_p_bin)
882 {
883 fenc = (char_u *)""; /* binary: don't convert */
884 fenc_alloced = FALSE;
885 }
886 else if (curbuf->b_help)
887 {
888 char_u firstline[80];
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000889 int fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890
891 /* Help files are either utf-8 or latin1. Try utf-8 first, if this
892 * fails it must be latin1.
893 * Always do this when 'encoding' is "utf-8". Otherwise only do
894 * this when needed to avoid [converted] remarks all the time.
895 * It is needed when the first line contains non-ASCII characters.
896 * That is only in *.??x files. */
897 fenc = (char_u *)"latin1";
898 c = enc_utf8;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000899 if (!c && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000901 fc = fname[STRLEN(fname) - 1];
902 if (TOLOWER_ASC(fc) == 'x')
903 {
904 /* Read the first line (and a bit more). Immediately rewind to
905 * the start of the file. If the read() fails "len" is -1. */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +0100906 len = read_eintr(fd, firstline, 80);
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000907 lseek(fd, (off_t)0L, SEEK_SET);
908 for (p = firstline; p < firstline + len; ++p)
909 if (*p >= 0x80)
910 {
911 c = TRUE;
912 break;
913 }
914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916
917 if (c)
918 {
919 fenc_next = fenc;
920 fenc = (char_u *)"utf-8";
921
922 /* When the file is utf-8 but a character doesn't fit in
923 * 'encoding' don't retry. In help text editing utf-8 bytes
924 * doesn't make sense. */
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000925 if (!enc_utf8)
926 keep_dest_enc = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 }
928 fenc_alloced = FALSE;
929 }
930 else if (*p_fencs == NUL)
931 {
932 fenc = curbuf->b_p_fenc; /* use format from buffer */
933 fenc_alloced = FALSE;
934 }
935 else
936 {
937 fenc_next = p_fencs; /* try items in 'fileencodings' */
938 fenc = next_fenc(&fenc_next);
939 fenc_alloced = TRUE;
940 }
941#endif
942
943 /*
944 * Jump back here to retry reading the file in different ways.
945 * Reasons to retry:
946 * - encoding conversion failed: try another one from "fenc_next"
947 * - BOM detected and fenc was set, need to setup conversion
948 * - "fileformat" check failed: try another
949 *
950 * Variables set for special retry actions:
951 * "file_rewind" Rewind the file to start reading it again.
952 * "advance_fenc" Advance "fenc" using "fenc_next".
953 * "skip_read" Re-use already read bytes (BOM detected).
954 * "did_iconv" iconv() conversion failed, try 'charconvert'.
955 * "keep_fileformat" Don't reset "fileformat".
956 *
957 * Other status indicators:
958 * "tmpname" When != NULL did conversion with 'charconvert'.
959 * Output file has to be deleted afterwards.
960 * "iconv_fd" When != -1 did conversion with iconv().
961 */
962retry:
963
964 if (file_rewind)
965 {
966 if (read_buffer)
967 {
968 read_buf_lnum = 1;
969 read_buf_col = 0;
970 }
971 else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0)
972 {
973 /* Can't rewind the file, give up. */
974 error = TRUE;
975 goto failed;
976 }
977 /* Delete the previously read lines. */
978 while (lnum > from)
979 ml_delete(lnum--, FALSE);
980 file_rewind = FALSE;
981#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000982 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000983 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 curbuf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000985 curbuf->b_start_bomb = FALSE;
986 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +0000987 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#endif
989 }
990
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200991#ifdef FEAT_CRYPT
992 if (cryptkey != NULL)
993 /* Need to reset the state, but keep the key, don't want to ask for it
994 * again. */
995 crypt_pop_state();
996#endif
997
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 /*
999 * When retrying with another "fenc" and the first time "fileformat"
1000 * will be reset.
1001 */
1002 if (keep_fileformat)
1003 keep_fileformat = FALSE;
1004 else
1005 {
1006 if (eap != NULL && eap->force_ff != 0)
Bram Moolenaar1c860362008-11-12 15:05:21 +00001007 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 fileformat = get_fileformat_force(curbuf, eap);
Bram Moolenaar1c860362008-11-12 15:05:21 +00001009 try_unix = try_dos = try_mac = FALSE;
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 else if (curbuf->b_p_bin)
1012 fileformat = EOL_UNIX; /* binary: use Unix format */
1013 else if (*p_ffs == NUL)
1014 fileformat = get_fileformat(curbuf);/* use format from buffer */
1015 else
1016 fileformat = EOL_UNKNOWN; /* detect from file */
1017 }
1018
1019#ifdef FEAT_MBYTE
1020# ifdef USE_ICONV
1021 if (iconv_fd != (iconv_t)-1)
1022 {
1023 /* aborted conversion with iconv(), close the descriptor */
1024 iconv_close(iconv_fd);
1025 iconv_fd = (iconv_t)-1;
1026 }
1027# endif
1028
1029 if (advance_fenc)
1030 {
1031 /*
1032 * Try the next entry in 'fileencodings'.
1033 */
1034 advance_fenc = FALSE;
1035
1036 if (eap != NULL && eap->force_enc != 0)
1037 {
1038 /* Conversion given with "++cc=" wasn't possible, read
1039 * without conversion. */
1040 notconverted = TRUE;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001041 conv_error = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (fenc_alloced)
1043 vim_free(fenc);
1044 fenc = (char_u *)"";
1045 fenc_alloced = FALSE;
1046 }
1047 else
1048 {
1049 if (fenc_alloced)
1050 vim_free(fenc);
1051 if (fenc_next != NULL)
1052 {
1053 fenc = next_fenc(&fenc_next);
1054 fenc_alloced = (fenc_next != NULL);
1055 }
1056 else
1057 {
1058 fenc = (char_u *)"";
1059 fenc_alloced = FALSE;
1060 }
1061 }
1062 if (tmpname != NULL)
1063 {
1064 mch_remove(tmpname); /* delete converted file */
1065 vim_free(tmpname);
1066 tmpname = NULL;
1067 }
1068 }
1069
1070 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00001071 * Conversion may be required when the encoding of the file is different
1072 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 */
1074 fio_flags = 0;
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00001075 converted = need_conversion(fenc);
1076 if (converted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 {
1078
1079 /* "ucs-bom" means we need to check the first bytes of the file
1080 * for a BOM. */
1081 if (STRCMP(fenc, ENC_UCSBOM) == 0)
1082 fio_flags = FIO_UCSBOM;
1083
1084 /*
1085 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be
1086 * done. This is handled below after read(). Prepare the
1087 * fio_flags to avoid having to parse the string each time.
1088 * Also check for Unicode to Latin1 conversion, because iconv()
1089 * appears not to handle this correctly. This works just like
1090 * conversion to UTF-8 except how the resulting character is put in
1091 * the buffer.
1092 */
1093 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
1094 fio_flags = get_fio_flags(fenc);
1095
1096# ifdef WIN3264
1097 /*
1098 * Conversion from an MS-Windows codepage to UTF-8 or another codepage
1099 * is handled with MultiByteToWideChar().
1100 */
1101 if (fio_flags == 0)
1102 fio_flags = get_win_fio_flags(fenc);
1103# endif
1104
1105# ifdef MACOS_X
1106 /* Conversion from Apple MacRoman to latin1 or UTF-8 */
1107 if (fio_flags == 0)
1108 fio_flags = get_mac_fio_flags(fenc);
1109# endif
1110
1111# ifdef USE_ICONV
1112 /*
1113 * Try using iconv() if we can't convert internally.
1114 */
1115 if (fio_flags == 0
1116# ifdef FEAT_EVAL
1117 && !did_iconv
1118# endif
1119 )
1120 iconv_fd = (iconv_t)my_iconv_open(
1121 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
1122# endif
1123
1124# ifdef FEAT_EVAL
1125 /*
1126 * Use the 'charconvert' expression when conversion is required
1127 * and we can't do it internally or with iconv().
1128 */
1129 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
1130# ifdef USE_ICONV
1131 && iconv_fd == (iconv_t)-1
1132# endif
1133 )
1134 {
1135# ifdef USE_ICONV
1136 did_iconv = FALSE;
1137# endif
1138 /* Skip conversion when it's already done (retry for wrong
1139 * "fileformat"). */
1140 if (tmpname == NULL)
1141 {
1142 tmpname = readfile_charconvert(fname, fenc, &fd);
1143 if (tmpname == NULL)
1144 {
1145 /* Conversion failed. Try another one. */
1146 advance_fenc = TRUE;
1147 if (fd < 0)
1148 {
1149 /* Re-opening the original file failed! */
1150 EMSG(_("E202: Conversion made file unreadable!"));
1151 error = TRUE;
1152 goto failed;
1153 }
1154 goto retry;
1155 }
1156 }
1157 }
1158 else
1159# endif
1160 {
1161 if (fio_flags == 0
1162# ifdef USE_ICONV
1163 && iconv_fd == (iconv_t)-1
1164# endif
1165 )
1166 {
1167 /* Conversion wanted but we can't.
1168 * Try the next conversion in 'fileencodings' */
1169 advance_fenc = TRUE;
1170 goto retry;
1171 }
1172 }
1173 }
1174
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001175 /* Set "can_retry" when it's possible to rewind the file and try with
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 * another "fenc" value. It's FALSE when no other "fenc" to try, reading
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001177 * stdin or fixed at a specific encoding. */
1178 can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#endif
1180
1181 if (!skip_read)
1182 {
1183 linerest = 0;
1184 filesize = 0;
1185 skip_count = lines_to_skip;
1186 read_count = lines_to_read;
1187#ifdef FEAT_MBYTE
1188 conv_restlen = 0;
1189#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001190#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001191 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0
1192 && curbuf->b_ffname != NULL
1193 && curbuf->b_p_udf
1194 && !filtering
1195 && !read_stdin
1196 && !read_buffer);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02001197 if (read_undo_file)
1198 sha256_start(&sha_ctx);
1199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 }
1201
1202 while (!error && !got_int)
1203 {
1204 /*
1205 * We allocate as much space for the file as we can get, plus
1206 * space for the old line plus room for one terminating NUL.
1207 * The amount is limited by the fact that read() only can read
1208 * upto max_unsigned characters (and other things).
1209 */
1210#if SIZEOF_INT <= 2
1211 if (linerest >= 0x7ff0)
1212 {
1213 ++split;
1214 *ptr = NL; /* split line by inserting a NL */
1215 size = 1;
1216 }
1217 else
1218#endif
1219 {
1220 if (!skip_read)
1221 {
1222#if SIZEOF_INT > 2
Bram Moolenaar311d9822007-02-27 15:48:28 +00001223# if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 size = SSIZE_MAX; /* use max I/O size, 52K */
1225# else
1226 size = 0x10000L; /* use buffer >= 64K */
1227# endif
1228#else
1229 size = 0x7ff0L - linerest; /* limit buffer to 32K */
1230#endif
1231
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001232 for ( ; size >= 10; size = (long)((long_u)size >> 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 {
1234 if ((new_buffer = lalloc((long_u)(size + linerest + 1),
1235 FALSE)) != NULL)
1236 break;
1237 }
1238 if (new_buffer == NULL)
1239 {
1240 do_outofmem_msg((long_u)(size * 2 + linerest + 1));
1241 error = TRUE;
1242 break;
1243 }
1244 if (linerest) /* copy characters from the previous buffer */
1245 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
1246 vim_free(buffer);
1247 buffer = new_buffer;
1248 ptr = buffer + linerest;
1249 line_start = buffer;
1250
1251#ifdef FEAT_MBYTE
1252 /* May need room to translate into.
1253 * For iconv() we don't really know the required space, use a
1254 * factor ICONV_MULT.
1255 * latin1 to utf-8: 1 byte becomes up to 2 bytes
1256 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes
1257 * become up to 4 bytes, size must be multiple of 2
1258 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be
1259 * multiple of 2
1260 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be
1261 * multiple of 4 */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001262 real_size = (int)size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263# ifdef USE_ICONV
1264 if (iconv_fd != (iconv_t)-1)
1265 size = size / ICONV_MULT;
1266 else
1267# endif
1268 if (fio_flags & FIO_LATIN1)
1269 size = size / 2;
1270 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1271 size = (size * 2 / 3) & ~1;
1272 else if (fio_flags & FIO_UCS4)
1273 size = (size * 2 / 3) & ~3;
1274 else if (fio_flags == FIO_UCSBOM)
1275 size = size / ICONV_MULT; /* worst case */
1276# ifdef WIN3264
1277 else if (fio_flags & FIO_CODEPAGE)
1278 size = size / ICONV_MULT; /* also worst case */
1279# endif
1280# ifdef MACOS_X
1281 else if (fio_flags & FIO_MACROMAN)
1282 size = size / ICONV_MULT; /* also worst case */
1283# endif
1284#endif
1285
1286#ifdef FEAT_MBYTE
1287 if (conv_restlen > 0)
1288 {
1289 /* Insert unconverted bytes from previous line. */
1290 mch_memmove(ptr, conv_rest, conv_restlen);
1291 ptr += conv_restlen;
1292 size -= conv_restlen;
1293 }
1294#endif
1295
1296 if (read_buffer)
1297 {
1298 /*
1299 * Read bytes from curbuf. Used for converting text read
1300 * from stdin.
1301 */
1302 if (read_buf_lnum > from)
1303 size = 0;
1304 else
1305 {
1306 int n, ni;
1307 long tlen;
1308
1309 tlen = 0;
1310 for (;;)
1311 {
1312 p = ml_get(read_buf_lnum) + read_buf_col;
1313 n = (int)STRLEN(p);
1314 if ((int)tlen + n + 1 > size)
1315 {
1316 /* Filled up to "size", append partial line.
1317 * Change NL to NUL to reverse the effect done
1318 * below. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001319 n = (int)(size - tlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 for (ni = 0; ni < n; ++ni)
1321 {
1322 if (p[ni] == NL)
1323 ptr[tlen++] = NUL;
1324 else
1325 ptr[tlen++] = p[ni];
1326 }
1327 read_buf_col += n;
1328 break;
1329 }
1330 else
1331 {
1332 /* Append whole line and new-line. Change NL
1333 * to NUL to reverse the effect done below. */
1334 for (ni = 0; ni < n; ++ni)
1335 {
1336 if (p[ni] == NL)
1337 ptr[tlen++] = NUL;
1338 else
1339 ptr[tlen++] = p[ni];
1340 }
1341 ptr[tlen++] = NL;
1342 read_buf_col = 0;
1343 if (++read_buf_lnum > from)
1344 {
1345 /* When the last line didn't have an
1346 * end-of-line don't add it now either. */
1347 if (!curbuf->b_p_eol)
1348 --tlen;
1349 size = tlen;
1350 break;
1351 }
1352 }
1353 }
1354 }
1355 }
1356 else
1357 {
1358 /*
1359 * Read bytes from the file.
1360 */
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01001361 size = read_eintr(fd, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363
1364 if (size <= 0)
1365 {
1366 if (size < 0) /* read error */
1367 error = TRUE;
1368#ifdef FEAT_MBYTE
1369 else if (conv_restlen > 0)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001370 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001371 /*
1372 * Reached end-of-file but some trailing bytes could
1373 * not be converted. Truncated file?
1374 */
1375
1376 /* When we did a conversion report an error. */
1377 if (fio_flags != 0
1378# ifdef USE_ICONV
1379 || iconv_fd != (iconv_t)-1
1380# endif
1381 )
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001382 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001383 if (conv_error == 0)
1384 conv_error = curbuf->b_ml.ml_line_count
1385 - linecnt + 1;
1386 }
1387 /* Remember the first linenr with an illegal byte */
1388 else if (illegal_byte == 0)
1389 illegal_byte = curbuf->b_ml.ml_line_count
1390 - linecnt + 1;
1391 if (bad_char_behavior == BAD_DROP)
1392 {
1393 *(ptr - conv_restlen) = NUL;
1394 conv_restlen = 0;
1395 }
1396 else
1397 {
1398 /* Replace the trailing bytes with the replacement
1399 * character if we were converting; if we weren't,
1400 * leave the UTF8 checking code to do it, as it
1401 * works slightly differently. */
1402 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
1403# ifdef USE_ICONV
1404 || iconv_fd != (iconv_t)-1
1405# endif
1406 ))
1407 {
1408 while (conv_restlen > 0)
1409 {
1410 *(--ptr) = bad_char_behavior;
1411 --conv_restlen;
1412 }
1413 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001414 fio_flags = 0; /* don't convert this */
Bram Moolenaarb21e5842006-04-16 18:30:08 +00001415# ifdef USE_ICONV
1416 if (iconv_fd != (iconv_t)-1)
1417 {
1418 iconv_close(iconv_fd);
1419 iconv_fd = (iconv_t)-1;
1420 }
1421# endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001422 }
1423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424#endif
1425 }
1426
1427#ifdef FEAT_CRYPT
1428 /*
1429 * At start of file: Check for magic number of encryption.
1430 */
1431 if (filesize == 0)
1432 cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02001433 &filesize, newfile, sfname,
1434 &did_ask_for_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 /*
1436 * Decrypt the read bytes.
1437 */
1438 if (cryptkey != NULL && size > 0)
Bram Moolenaar04c9baf2010-06-01 23:37:39 +02001439 crypt_decode(ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440#endif
1441 }
1442 skip_read = FALSE;
1443
1444#ifdef FEAT_MBYTE
1445 /*
1446 * At start of file (or after crypt magic number): Check for BOM.
1447 * Also check for a BOM for other Unicode encodings, but not after
1448 * converting with 'charconvert' or when a BOM has already been
1449 * found.
1450 */
1451 if ((filesize == 0
1452# ifdef FEAT_CRYPT
Bram Moolenaar40e6a712010-05-16 22:32:54 +02001453 || (filesize == (CRYPT_MAGIC_LEN
Bram Moolenaar80794b12010-06-13 05:20:42 +02001454 + crypt_salt_len[use_crypt_method]
Bram Moolenaar40e6a712010-05-16 22:32:54 +02001455 + crypt_seed_len[use_crypt_method])
1456 && cryptkey != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457# endif
1458 )
1459 && (fio_flags == FIO_UCSBOM
1460 || (!curbuf->b_p_bomb
1461 && tmpname == NULL
1462 && (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
1463 {
1464 char_u *ccname;
1465 int blen;
1466
1467 /* no BOM detection in a short file or in binary mode */
1468 if (size < 2 || curbuf->b_p_bin)
1469 ccname = NULL;
1470 else
1471 ccname = check_for_bom(ptr, size, &blen,
1472 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
1473 if (ccname != NULL)
1474 {
1475 /* Remove BOM from the text */
1476 filesize += blen;
1477 size -= blen;
1478 mch_memmove(ptr, ptr + blen, (size_t)size);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001479 if (set_options)
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 curbuf->b_p_bomb = TRUE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +00001482 curbuf->b_start_bomb = TRUE;
1483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
1485
1486 if (fio_flags == FIO_UCSBOM)
1487 {
1488 if (ccname == NULL)
1489 {
1490 /* No BOM detected: retry with next encoding. */
1491 advance_fenc = TRUE;
1492 }
1493 else
1494 {
1495 /* BOM detected: set "fenc" and jump back */
1496 if (fenc_alloced)
1497 vim_free(fenc);
1498 fenc = ccname;
1499 fenc_alloced = FALSE;
1500 }
1501 /* retry reading without getting new bytes or rewinding */
1502 skip_read = TRUE;
1503 goto retry;
1504 }
1505 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001506
1507 /* Include not converted bytes. */
1508 ptr -= conv_restlen;
1509 size += conv_restlen;
1510 conv_restlen = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
1512 /*
1513 * Break here for a read error or end-of-file.
1514 */
1515 if (size <= 0)
1516 break;
1517
1518#ifdef FEAT_MBYTE
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520# ifdef USE_ICONV
1521 if (iconv_fd != (iconv_t)-1)
1522 {
1523 /*
1524 * Attempt conversion of the read bytes to 'encoding' using
1525 * iconv().
1526 */
1527 const char *fromp;
1528 char *top;
1529 size_t from_size;
1530 size_t to_size;
1531
1532 fromp = (char *)ptr;
1533 from_size = size;
1534 ptr += size;
1535 top = (char *)ptr;
1536 to_size = real_size - size;
1537
1538 /*
1539 * If there is conversion error or not enough room try using
Bram Moolenaar19a09a12005-03-04 23:39:37 +00001540 * another conversion. Except for when there is no
1541 * alternative (help files).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001543 while ((iconv(iconv_fd, (void *)&fromp, &from_size,
1544 &top, &to_size)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
1546 || from_size > CONV_RESTLEN)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001547 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001548 if (can_retry)
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001549 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001550 if (conv_error == 0)
1551 conv_error = readfile_linenr(linecnt,
1552 ptr, (char_u *)top);
Bram Moolenaar42eeac32005-06-29 22:40:58 +00001553
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001554 /* Deal with a bad byte and continue with the next. */
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001555 ++fromp;
1556 --from_size;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001557 if (bad_char_behavior == BAD_KEEP)
1558 {
1559 *top++ = *(fromp - 1);
1560 --to_size;
1561 }
1562 else if (bad_char_behavior != BAD_DROP)
1563 {
1564 *top++ = bad_char_behavior;
1565 --to_size;
1566 }
Bram Moolenaar8cd06ca2005-02-28 22:44:58 +00001567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568
1569 if (from_size > 0)
1570 {
1571 /* Some remaining characters, keep them for the next
1572 * round. */
1573 mch_memmove(conv_rest, (char_u *)fromp, from_size);
1574 conv_restlen = (int)from_size;
1575 }
1576
1577 /* move the linerest to before the converted characters */
1578 line_start = ptr - linerest;
1579 mch_memmove(line_start, buffer, (size_t)linerest);
1580 size = (long)((char_u *)top - ptr);
1581 }
1582# endif
1583
1584# ifdef WIN3264
1585 if (fio_flags & FIO_CODEPAGE)
1586 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001587 char_u *src, *dst;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001588 WCHAR ucs2buf[3];
1589 int ucs2len;
1590 int codepage = FIO_GET_CP(fio_flags);
1591 int bytelen;
1592 int found_bad;
1593 char replstr[2];
1594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 /*
1596 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001597 * a codepage, using standard MS-Windows functions. This
1598 * requires two steps:
1599 * 1. convert from 'fileencoding' to ucs-2
1600 * 2. convert from ucs-2 to 'encoding'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 *
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001602 * Because there may be illegal bytes AND an incomplete byte
1603 * sequence at the end, we may have to do the conversion one
1604 * character at a time to get it right.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001607 /* Replacement string for WideCharToMultiByte(). */
1608 if (bad_char_behavior > 0)
1609 replstr[0] = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 else
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001611 replstr[0] = '?';
1612 replstr[1] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614 /*
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001615 * Move the bytes to the end of the buffer, so that we have
1616 * room to put the result at the start.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001618 src = ptr + real_size - size;
1619 mch_memmove(src, ptr, size);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001621 /*
1622 * Do the conversion.
1623 */
1624 dst = ptr;
1625 size = size;
1626 while (size > 0)
1627 {
1628 found_bad = FALSE;
1629
1630# ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */
1631 if (codepage == CP_UTF8)
1632 {
1633 /* Handle CP_UTF8 input ourselves to be able to handle
1634 * trailing bytes properly.
1635 * Get one UTF-8 character from src. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001636 bytelen = (int)utf_ptr2len_len(src, size);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001637 if (bytelen > size)
1638 {
1639 /* Only got some bytes of a character. Normally
1640 * it's put in "conv_rest", but if it's too long
1641 * deal with it as if they were illegal bytes. */
1642 if (bytelen <= CONV_RESTLEN)
1643 break;
1644
1645 /* weird overlong byte sequence */
1646 bytelen = size;
1647 found_bad = TRUE;
1648 }
1649 else
1650 {
Bram Moolenaarc01140a2006-03-24 22:21:52 +00001651 int u8c = utf_ptr2char(src);
1652
Bram Moolenaar86e01082005-12-29 22:45:34 +00001653 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001654 found_bad = TRUE;
1655 ucs2buf[0] = u8c;
1656 ucs2len = 1;
1657 }
1658 }
1659 else
1660# endif
1661 {
1662 /* We don't know how long the byte sequence is, try
1663 * from one to three bytes. */
1664 for (bytelen = 1; bytelen <= size && bytelen <= 3;
1665 ++bytelen)
1666 {
1667 ucs2len = MultiByteToWideChar(codepage,
1668 MB_ERR_INVALID_CHARS,
1669 (LPCSTR)src, bytelen,
1670 ucs2buf, 3);
1671 if (ucs2len > 0)
1672 break;
1673 }
1674 if (ucs2len == 0)
1675 {
1676 /* If we have only one byte then it's probably an
1677 * incomplete byte sequence. Otherwise discard
1678 * one byte as a bad character. */
1679 if (size == 1)
1680 break;
1681 found_bad = TRUE;
1682 bytelen = 1;
1683 }
1684 }
1685
1686 if (!found_bad)
1687 {
1688 int i;
1689
1690 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */
1691 if (enc_utf8)
1692 {
1693 /* From UCS-2 to UTF-8. Cannot fail. */
1694 for (i = 0; i < ucs2len; ++i)
1695 dst += utf_char2bytes(ucs2buf[i], dst);
1696 }
1697 else
1698 {
1699 BOOL bad = FALSE;
1700 int dstlen;
1701
1702 /* From UCS-2 to "enc_codepage". If the
1703 * conversion uses the default character "?",
1704 * the data doesn't fit in this encoding. */
1705 dstlen = WideCharToMultiByte(enc_codepage, 0,
1706 (LPCWSTR)ucs2buf, ucs2len,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001707 (LPSTR)dst, (int)(src - dst),
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001708 replstr, &bad);
1709 if (bad)
1710 found_bad = TRUE;
1711 else
1712 dst += dstlen;
1713 }
1714 }
1715
1716 if (found_bad)
1717 {
1718 /* Deal with bytes we can't convert. */
1719 if (can_retry)
1720 goto rewind_retry;
1721 if (conv_error == 0)
1722 conv_error = readfile_linenr(linecnt, ptr, dst);
1723 if (bad_char_behavior != BAD_DROP)
1724 {
1725 if (bad_char_behavior == BAD_KEEP)
1726 {
1727 mch_memmove(dst, src, bytelen);
1728 dst += bytelen;
1729 }
1730 else
1731 *dst++ = bad_char_behavior;
1732 }
1733 }
1734
1735 src += bytelen;
1736 size -= bytelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001738
1739 if (size > 0)
1740 {
1741 /* An incomplete byte sequence remaining. */
1742 mch_memmove(conv_rest, src, size);
1743 conv_restlen = size;
1744 }
1745
1746 /* The new size is equal to how much "dst" was advanced. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001747 size = (long)(dst - ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749 else
1750# endif
Bram Moolenaar56718732006-03-15 22:53:57 +00001751# ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 if (fio_flags & FIO_MACROMAN)
1753 {
1754 /*
1755 * Conversion from Apple MacRoman char encoding to UTF-8 or
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001756 * latin1. This is in os_mac_conv.c.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00001758 if (macroman2enc(ptr, &size, real_size) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 goto rewind_retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761 else
1762# endif
1763 if (fio_flags != 0)
1764 {
1765 int u8c;
1766 char_u *dest;
1767 char_u *tail = NULL;
1768
1769 /*
1770 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8.
1771 * "enc_utf8" not set: Convert Unicode to Latin1.
1772 * Go from end to start through the buffer, because the number
1773 * of bytes may increase.
1774 * "dest" points to after where the UTF-8 bytes go, "p" points
1775 * to after the next character to convert.
1776 */
1777 dest = ptr + real_size;
1778 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
1779 {
1780 p = ptr + size;
1781 if (fio_flags == FIO_UTF8)
1782 {
1783 /* Check for a trailing incomplete UTF-8 sequence */
1784 tail = ptr + size - 1;
1785 while (tail > ptr && (*tail & 0xc0) == 0x80)
1786 --tail;
1787 if (tail + utf_byte2len(*tail) <= ptr + size)
1788 tail = NULL;
1789 else
1790 p = tail;
1791 }
1792 }
1793 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1794 {
1795 /* Check for a trailing byte */
1796 p = ptr + (size & ~1);
1797 if (size & 1)
1798 tail = p;
1799 if ((fio_flags & FIO_UTF16) && p > ptr)
1800 {
1801 /* Check for a trailing leading word */
1802 if (fio_flags & FIO_ENDIAN_L)
1803 {
1804 u8c = (*--p << 8);
1805 u8c += *--p;
1806 }
1807 else
1808 {
1809 u8c = *--p;
1810 u8c += (*--p << 8);
1811 }
1812 if (u8c >= 0xd800 && u8c <= 0xdbff)
1813 tail = p;
1814 else
1815 p += 2;
1816 }
1817 }
1818 else /* FIO_UCS4 */
1819 {
1820 /* Check for trailing 1, 2 or 3 bytes */
1821 p = ptr + (size & ~3);
1822 if (size & 3)
1823 tail = p;
1824 }
1825
1826 /* If there is a trailing incomplete sequence move it to
1827 * conv_rest[]. */
1828 if (tail != NULL)
1829 {
1830 conv_restlen = (int)((ptr + size) - tail);
1831 mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
1832 size -= conv_restlen;
1833 }
1834
1835
1836 while (p > ptr)
1837 {
1838 if (fio_flags & FIO_LATIN1)
1839 u8c = *--p;
1840 else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
1841 {
1842 if (fio_flags & FIO_ENDIAN_L)
1843 {
1844 u8c = (*--p << 8);
1845 u8c += *--p;
1846 }
1847 else
1848 {
1849 u8c = *--p;
1850 u8c += (*--p << 8);
1851 }
1852 if ((fio_flags & FIO_UTF16)
1853 && u8c >= 0xdc00 && u8c <= 0xdfff)
1854 {
1855 int u16c;
1856
1857 if (p == ptr)
1858 {
1859 /* Missing leading word. */
1860 if (can_retry)
1861 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001862 if (conv_error == 0)
1863 conv_error = readfile_linenr(linecnt,
1864 ptr, p);
1865 if (bad_char_behavior == BAD_DROP)
1866 continue;
1867 if (bad_char_behavior != BAD_KEEP)
1868 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 }
1870
1871 /* found second word of double-word, get the first
1872 * word and compute the resulting character */
1873 if (fio_flags & FIO_ENDIAN_L)
1874 {
1875 u16c = (*--p << 8);
1876 u16c += *--p;
1877 }
1878 else
1879 {
1880 u16c = *--p;
1881 u16c += (*--p << 8);
1882 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001883 u8c = 0x10000 + ((u16c & 0x3ff) << 10)
1884 + (u8c & 0x3ff);
1885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 /* Check if the word is indeed a leading word. */
1887 if (u16c < 0xd800 || u16c > 0xdbff)
1888 {
1889 if (can_retry)
1890 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001891 if (conv_error == 0)
1892 conv_error = readfile_linenr(linecnt,
1893 ptr, p);
1894 if (bad_char_behavior == BAD_DROP)
1895 continue;
1896 if (bad_char_behavior != BAD_KEEP)
1897 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 }
1900 }
1901 else if (fio_flags & FIO_UCS4)
1902 {
1903 if (fio_flags & FIO_ENDIAN_L)
1904 {
1905 u8c = (*--p << 24);
1906 u8c += (*--p << 16);
1907 u8c += (*--p << 8);
1908 u8c += *--p;
1909 }
1910 else /* big endian */
1911 {
1912 u8c = *--p;
1913 u8c += (*--p << 8);
1914 u8c += (*--p << 16);
1915 u8c += (*--p << 24);
1916 }
1917 }
1918 else /* UTF-8 */
1919 {
1920 if (*--p < 0x80)
1921 u8c = *p;
1922 else
1923 {
1924 len = utf_head_off(ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001925 p -= len;
1926 u8c = utf_ptr2char(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (len == 0)
1928 {
1929 /* Not a valid UTF-8 character, retry with
1930 * another fenc when possible, otherwise just
1931 * report the error. */
1932 if (can_retry)
1933 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001934 if (conv_error == 0)
1935 conv_error = readfile_linenr(linecnt,
1936 ptr, p);
1937 if (bad_char_behavior == BAD_DROP)
1938 continue;
1939 if (bad_char_behavior != BAD_KEEP)
1940 u8c = bad_char_behavior;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 }
1943 }
1944 if (enc_utf8) /* produce UTF-8 */
1945 {
1946 dest -= utf_char2len(u8c);
1947 (void)utf_char2bytes(u8c, dest);
1948 }
1949 else /* produce Latin1 */
1950 {
1951 --dest;
1952 if (u8c >= 0x100)
1953 {
1954 /* character doesn't fit in latin1, retry with
1955 * another fenc when possible, otherwise just
1956 * report the error. */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001957 if (can_retry)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 goto rewind_retry;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001959 if (conv_error == 0)
1960 conv_error = readfile_linenr(linecnt, ptr, p);
1961 if (bad_char_behavior == BAD_DROP)
1962 ++dest;
1963 else if (bad_char_behavior == BAD_KEEP)
1964 *dest = u8c;
1965 else if (eap != NULL && eap->bad_char != 0)
1966 *dest = bad_char_behavior;
1967 else
1968 *dest = 0xBF;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970 else
1971 *dest = u8c;
1972 }
1973 }
1974
1975 /* move the linerest to before the converted characters */
1976 line_start = dest - linerest;
1977 mch_memmove(line_start, buffer, (size_t)linerest);
1978 size = (long)((ptr + real_size) - dest);
1979 ptr = dest;
1980 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00001981 else if (enc_utf8 && !curbuf->b_p_bin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00001983 int incomplete_tail = FALSE;
1984
1985 /* Reading UTF-8: Check if the bytes are valid UTF-8. */
1986 for (p = ptr; ; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001988 int todo = (int)((ptr + size) - p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001989 int l;
1990
1991 if (todo <= 0)
1992 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (*p >= 0x80)
1994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 /* A length of 1 means it's an illegal byte. Accept
1996 * an incomplete character at the end though, the next
1997 * read() will get the next bytes, we'll check it
1998 * then. */
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00001999 l = utf_ptr2len_len(p, todo);
Bram Moolenaarf453d352008-06-04 17:37:34 +00002000 if (l > todo && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002002 /* Avoid retrying with a different encoding when
2003 * a truncated file is more likely, or attempting
2004 * to read the rest of an incomplete sequence when
2005 * we have already done so. */
2006 if (p > ptr || filesize > 0)
2007 incomplete_tail = TRUE;
2008 /* Incomplete byte sequence, move it to conv_rest[]
2009 * and try to read the rest of it, unless we've
2010 * already done so. */
2011 if (p > ptr)
2012 {
2013 conv_restlen = todo;
2014 mch_memmove(conv_rest, p, conv_restlen);
2015 size -= conv_restlen;
2016 break;
2017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002019 if (l == 1 || l > todo)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002020 {
2021 /* Illegal byte. If we can try another encoding
Bram Moolenaarf453d352008-06-04 17:37:34 +00002022 * do that, unless at EOF where a truncated
2023 * file is more likely than a conversion error. */
2024 if (can_retry && !incomplete_tail)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002025 break;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002026# ifdef USE_ICONV
2027 /* When we did a conversion report an error. */
2028 if (iconv_fd != (iconv_t)-1 && conv_error == 0)
2029 conv_error = readfile_linenr(linecnt, ptr, p);
2030# endif
Bram Moolenaarf453d352008-06-04 17:37:34 +00002031 /* Remember the first linenr with an illegal byte */
2032 if (conv_error == 0 && illegal_byte == 0)
2033 illegal_byte = readfile_linenr(linecnt, ptr, p);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002034
2035 /* Drop, keep or replace the bad byte. */
2036 if (bad_char_behavior == BAD_DROP)
2037 {
Bram Moolenaarf453d352008-06-04 17:37:34 +00002038 mch_memmove(p, p + 1, todo - 1);
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002039 --p;
2040 --size;
2041 }
2042 else if (bad_char_behavior != BAD_KEEP)
2043 *p = bad_char_behavior;
2044 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002045 else
2046 p += l - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 }
2048 }
Bram Moolenaarf453d352008-06-04 17:37:34 +00002049 if (p < ptr + size && !incomplete_tail)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
2051 /* Detected a UTF-8 error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052rewind_retry:
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002053 /* Retry reading with another conversion. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054# if defined(FEAT_EVAL) && defined(USE_ICONV)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002055 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
2056 /* iconv() failed, try 'charconvert' */
2057 did_iconv = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 else
2059# endif
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002060 /* use next item from 'fileencodings' */
2061 advance_fenc = TRUE;
2062 file_rewind = TRUE;
2063 goto retry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065 }
2066#endif
2067
2068 /* count the number of characters (after conversion!) */
2069 filesize += size;
2070
2071 /*
2072 * when reading the first part of a file: guess EOL type
2073 */
2074 if (fileformat == EOL_UNKNOWN)
2075 {
2076 /* First try finding a NL, for Dos and Unix */
2077 if (try_dos || try_unix)
2078 {
2079 for (p = ptr; p < ptr + size; ++p)
2080 {
2081 if (*p == NL)
2082 {
2083 if (!try_unix
2084 || (try_dos && p > ptr && p[-1] == CAR))
2085 fileformat = EOL_DOS;
2086 else
2087 fileformat = EOL_UNIX;
2088 break;
2089 }
2090 }
2091
2092 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */
2093 if (fileformat == EOL_UNIX && try_mac)
2094 {
2095 /* Need to reset the counters when retrying fenc. */
2096 try_mac = 1;
2097 try_unix = 1;
2098 for (; p >= ptr && *p != CAR; p--)
2099 ;
2100 if (p >= ptr)
2101 {
2102 for (p = ptr; p < ptr + size; ++p)
2103 {
2104 if (*p == NL)
2105 try_unix++;
2106 else if (*p == CAR)
2107 try_mac++;
2108 }
2109 if (try_mac > try_unix)
2110 fileformat = EOL_MAC;
2111 }
2112 }
2113 }
2114
2115 /* No NL found: may use Mac format */
2116 if (fileformat == EOL_UNKNOWN && try_mac)
2117 fileformat = EOL_MAC;
2118
2119 /* Still nothing found? Use first format in 'ffs' */
2120 if (fileformat == EOL_UNKNOWN)
2121 fileformat = default_fileformat();
2122
2123 /* if editing a new file: may set p_tx and p_ff */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002124 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 set_fileformat(fileformat, OPT_LOCAL);
2126 }
2127 }
2128
2129 /*
2130 * This loop is executed once for every character read.
2131 * Keep it fast!
2132 */
2133 if (fileformat == EOL_MAC)
2134 {
2135 --ptr;
2136 while (++ptr, --size >= 0)
2137 {
2138 /* catch most common case first */
2139 if ((c = *ptr) != NUL && c != CAR && c != NL)
2140 continue;
2141 if (c == NUL)
2142 *ptr = NL; /* NULs are replaced by newlines! */
2143 else if (c == NL)
2144 *ptr = CAR; /* NLs are replaced by CRs! */
2145 else
2146 {
2147 if (skip_count == 0)
2148 {
2149 *ptr = NUL; /* end of line */
2150 len = (colnr_T) (ptr - line_start + 1);
2151 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2152 {
2153 error = TRUE;
2154 break;
2155 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002156#ifdef FEAT_PERSISTENT_UNDO
2157 if (read_undo_file)
2158 sha256_update(&sha_ctx, line_start, len);
2159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 ++lnum;
2161 if (--read_count == 0)
2162 {
2163 error = TRUE; /* break loop */
2164 line_start = ptr; /* nothing left to write */
2165 break;
2166 }
2167 }
2168 else
2169 --skip_count;
2170 line_start = ptr + 1;
2171 }
2172 }
2173 }
2174 else
2175 {
2176 --ptr;
2177 while (++ptr, --size >= 0)
2178 {
2179 if ((c = *ptr) != NUL && c != NL) /* catch most common case */
2180 continue;
2181 if (c == NUL)
2182 *ptr = NL; /* NULs are replaced by newlines! */
2183 else
2184 {
2185 if (skip_count == 0)
2186 {
2187 *ptr = NUL; /* end of line */
2188 len = (colnr_T)(ptr - line_start + 1);
2189 if (fileformat == EOL_DOS)
2190 {
2191 if (ptr[-1] == CAR) /* remove CR */
2192 {
2193 ptr[-1] = NUL;
2194 --len;
2195 }
2196 /*
2197 * Reading in Dos format, but no CR-LF found!
2198 * When 'fileformats' includes "unix", delete all
2199 * the lines read so far and start all over again.
2200 * Otherwise give an error message later.
2201 */
2202 else if (ff_error != EOL_DOS)
2203 {
2204 if ( try_unix
2205 && !read_stdin
2206 && (read_buffer
2207 || lseek(fd, (off_t)0L, SEEK_SET) == 0))
2208 {
2209 fileformat = EOL_UNIX;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002210 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 set_fileformat(EOL_UNIX, OPT_LOCAL);
2212 file_rewind = TRUE;
2213 keep_fileformat = TRUE;
2214 goto retry;
2215 }
2216 ff_error = EOL_DOS;
2217 }
2218 }
2219 if (ml_append(lnum, line_start, len, newfile) == FAIL)
2220 {
2221 error = TRUE;
2222 break;
2223 }
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002224#ifdef FEAT_PERSISTENT_UNDO
2225 if (read_undo_file)
2226 sha256_update(&sha_ctx, line_start, len);
2227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 ++lnum;
2229 if (--read_count == 0)
2230 {
2231 error = TRUE; /* break loop */
2232 line_start = ptr; /* nothing left to write */
2233 break;
2234 }
2235 }
2236 else
2237 --skip_count;
2238 line_start = ptr + 1;
2239 }
2240 }
2241 }
2242 linerest = (long)(ptr - line_start);
2243 ui_breakcheck();
2244 }
2245
2246failed:
2247 /* not an error, max. number of lines reached */
2248 if (error && read_count == 0)
2249 error = FALSE;
2250
2251 /*
2252 * If we get EOF in the middle of a line, note the fact and
2253 * complete the line ourselves.
2254 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set.
2255 */
2256 if (!error
2257 && !got_int
2258 && linerest != 0
2259 && !(!curbuf->b_p_bin
2260 && fileformat == EOL_DOS
2261 && *line_start == Ctrl_Z
2262 && ptr == line_start + 1))
2263 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002264 /* remember for when writing */
2265 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 curbuf->b_p_eol = FALSE;
2267 *ptr = NUL;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002268 len = (colnr_T)(ptr - line_start + 1);
2269 if (ml_append(lnum, line_start, len, newfile) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 error = TRUE;
2271 else
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002272 {
2273#ifdef FEAT_PERSISTENT_UNDO
2274 if (read_undo_file)
2275 sha256_update(&sha_ctx, line_start, len);
2276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 read_no_eol_lnum = ++lnum;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002281 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 save_file_ff(curbuf); /* remember the current file format */
2283
2284#ifdef FEAT_CRYPT
Bram Moolenaar4cf35c22011-02-25 16:52:17 +01002285 crypt_method_used = use_crypt_method;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002286 if (cryptkey != NULL)
2287 {
2288 crypt_pop_state();
2289 if (cryptkey != curbuf->b_p_key)
2290 free_crypt_key(cryptkey);
2291 /* don't set cryptkey to NULL, it's used below as a flag that
2292 * encryption was used */
2293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294#endif
2295
2296#ifdef FEAT_MBYTE
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002297 /* If editing a new file: set 'fenc' for the current buffer.
2298 * Also for ":read ++edit file". */
2299 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 set_string_option_direct((char_u *)"fenc", -1, fenc,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002301 OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 if (fenc_alloced)
2303 vim_free(fenc);
2304# ifdef USE_ICONV
2305 if (iconv_fd != (iconv_t)-1)
2306 {
2307 iconv_close(iconv_fd);
2308 iconv_fd = (iconv_t)-1;
2309 }
2310# endif
2311#endif
2312
2313 if (!read_buffer && !read_stdin)
2314 close(fd); /* errors are ignored */
Bram Moolenaarf05da212009-11-17 16:13:15 +00002315#ifdef HAVE_FD_CLOEXEC
2316 else
2317 {
2318 int fdflags = fcntl(fd, F_GETFD);
2319 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0)
2320 fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC);
2321 }
2322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 vim_free(buffer);
2324
2325#ifdef HAVE_DUP
2326 if (read_stdin)
2327 {
2328 /* Use stderr for stdin, makes shell commands work. */
2329 close(0);
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00002330 ignored = dup(2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 }
2332#endif
2333
2334#ifdef FEAT_MBYTE
2335 if (tmpname != NULL)
2336 {
2337 mch_remove(tmpname); /* delete converted file */
2338 vim_free(tmpname);
2339 }
2340#endif
2341 --no_wait_return; /* may wait for return now */
2342
2343 /*
2344 * In recovery mode everything but autocommands is skipped.
2345 */
2346 if (!recoverymode)
2347 {
2348 /* need to delete the last line, which comes from the empty buffer */
2349 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
2350 {
2351#ifdef FEAT_NETBEANS_INTG
2352 netbeansFireChanges = 0;
2353#endif
2354 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
2355#ifdef FEAT_NETBEANS_INTG
2356 netbeansFireChanges = 1;
2357#endif
2358 --linecnt;
2359 }
2360 linecnt = curbuf->b_ml.ml_line_count - linecnt;
2361 if (filesize == 0)
2362 linecnt = 0;
2363 if (newfile || read_buffer)
Bram Moolenaar7263a772007-05-10 17:35:54 +00002364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 redraw_curbuf_later(NOT_VALID);
Bram Moolenaar7263a772007-05-10 17:35:54 +00002366#ifdef FEAT_DIFF
2367 /* After reading the text into the buffer the diff info needs to
2368 * be updated. */
2369 diff_invalidate(curbuf);
2370#endif
2371#ifdef FEAT_FOLDING
2372 /* All folds in the window are invalid now. Mark them for update
2373 * before triggering autocommands. */
2374 foldUpdateAll(curwin);
2375#endif
2376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 else if (linecnt) /* appended at least one line */
2378 appended_lines_mark(from, linecnt);
2379
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380#ifndef ALWAYS_USE_GUI
2381 /*
2382 * If we were reading from the same terminal as where messages go,
2383 * the screen will have been messed up.
2384 * Switch on raw mode now and clear the screen.
2385 */
2386 if (read_stdin)
2387 {
2388 settmode(TMODE_RAW); /* set to raw mode */
2389 starttermcap();
2390 screenclear();
2391 }
2392#endif
2393
2394 if (got_int)
2395 {
2396 if (!(flags & READ_DUMMY))
2397 {
2398 filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
2399 if (newfile)
2400 curbuf->b_p_ro = TRUE; /* must use "w!" now */
2401 }
2402 msg_scroll = msg_save;
2403#ifdef FEAT_VIMINFO
2404 check_marks_read();
2405#endif
2406 return OK; /* an interrupt isn't really an error */
2407 }
2408
2409 if (!filtering && !(flags & READ_DUMMY))
2410 {
2411 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */
2412 c = FALSE;
2413
2414#ifdef UNIX
2415# ifdef S_ISFIFO
2416 if (S_ISFIFO(perm)) /* fifo or socket */
2417 {
2418 STRCAT(IObuff, _("[fifo/socket]"));
2419 c = TRUE;
2420 }
2421# else
2422# ifdef S_IFIFO
2423 if ((perm & S_IFMT) == S_IFIFO) /* fifo */
2424 {
2425 STRCAT(IObuff, _("[fifo]"));
2426 c = TRUE;
2427 }
2428# endif
2429# ifdef S_IFSOCK
2430 if ((perm & S_IFMT) == S_IFSOCK) /* or socket */
2431 {
2432 STRCAT(IObuff, _("[socket]"));
2433 c = TRUE;
2434 }
2435# endif
2436# endif
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002437# ifdef OPEN_CHR_FILES
2438 if (S_ISCHR(perm)) /* or character special */
2439 {
2440 STRCAT(IObuff, _("[character special]"));
2441 c = TRUE;
2442 }
2443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444#endif
2445 if (curbuf->b_p_ro)
2446 {
2447 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
2448 c = TRUE;
2449 }
2450 if (read_no_eol_lnum)
2451 {
2452 msg_add_eol();
2453 c = TRUE;
2454 }
2455 if (ff_error == EOL_DOS)
2456 {
2457 STRCAT(IObuff, _("[CR missing]"));
2458 c = TRUE;
2459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 if (split)
2461 {
2462 STRCAT(IObuff, _("[long lines split]"));
2463 c = TRUE;
2464 }
2465#ifdef FEAT_MBYTE
2466 if (notconverted)
2467 {
2468 STRCAT(IObuff, _("[NOT converted]"));
2469 c = TRUE;
2470 }
2471 else if (converted)
2472 {
2473 STRCAT(IObuff, _("[converted]"));
2474 c = TRUE;
2475 }
2476#endif
2477#ifdef FEAT_CRYPT
2478 if (cryptkey != NULL)
2479 {
Bram Moolenaar4cf35c22011-02-25 16:52:17 +01002480 if (crypt_method_used == 1)
2481 STRCAT(IObuff, _("[blowfish]"));
2482 else
2483 STRCAT(IObuff, _("[crypted]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 c = TRUE;
2485 }
2486#endif
2487#ifdef FEAT_MBYTE
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002488 if (conv_error != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002490 sprintf((char *)IObuff + STRLEN(IObuff),
2491 _("[CONVERSION ERROR in line %ld]"), (long)conv_error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 c = TRUE;
2493 }
2494 else if (illegal_byte > 0)
2495 {
2496 sprintf((char *)IObuff + STRLEN(IObuff),
2497 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
2498 c = TRUE;
2499 }
2500 else
2501#endif
2502 if (error)
2503 {
2504 STRCAT(IObuff, _("[READ ERRORS]"));
2505 c = TRUE;
2506 }
2507 if (msg_add_fileformat(fileformat))
2508 c = TRUE;
2509#ifdef FEAT_CRYPT
2510 if (cryptkey != NULL)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002511 msg_add_lines(c, (long)linecnt, filesize
Bram Moolenaar80794b12010-06-13 05:20:42 +02002512 - CRYPT_MAGIC_LEN
2513 - crypt_salt_len[use_crypt_method]
2514 - crypt_seed_len[use_crypt_method]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 else
2516#endif
2517 msg_add_lines(c, (long)linecnt, filesize);
2518
2519 vim_free(keep_msg);
2520 keep_msg = NULL;
2521 msg_scrolled_ign = TRUE;
2522#ifdef ALWAYS_USE_GUI
2523 /* Don't show the message when reading stdin, it would end up in a
2524 * message box (which might be shown when exiting!) */
2525 if (read_stdin || read_buffer)
2526 p = msg_may_trunc(FALSE, IObuff);
2527 else
2528#endif
2529 p = msg_trunc_attr(IObuff, FALSE, 0);
2530 if (read_stdin || read_buffer || restart_edit != 0
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002531 || (msg_scrolled != 0 && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 /* Need to repeat the message after redrawing when:
2533 * - When reading from stdin (the screen will be cleared next).
2534 * - When restart_edit is set (otherwise there will be a delay
2535 * before redrawing).
2536 * - When the screen was scrolled but there is no wait-return
2537 * prompt. */
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002538 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 msg_scrolled_ign = FALSE;
2540 }
2541
2542 /* with errors writing the file requires ":w!" */
2543 if (newfile && (error
2544#ifdef FEAT_MBYTE
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002545 || conv_error != 0
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00002546 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547#endif
2548 ))
2549 curbuf->b_p_ro = TRUE;
2550
2551 u_clearline(); /* cannot use "U" command after adding lines */
2552
2553 /*
2554 * In Ex mode: cursor at last new line.
2555 * Otherwise: cursor at first new line.
2556 */
2557 if (exmode_active)
2558 curwin->w_cursor.lnum = from + linecnt;
2559 else
2560 curwin->w_cursor.lnum = from + 1;
2561 check_cursor_lnum();
2562 beginline(BL_WHITE | BL_FIX); /* on first non-blank */
2563
2564 /*
2565 * Set '[ and '] marks to the newly read lines.
2566 */
2567 curbuf->b_op_start.lnum = from + 1;
2568 curbuf->b_op_start.col = 0;
2569 curbuf->b_op_end.lnum = from + linecnt;
2570 curbuf->b_op_end.col = 0;
Bram Moolenaar03f48552006-02-28 23:52:23 +00002571
2572#ifdef WIN32
2573 /*
2574 * Work around a weird problem: When a file has two links (only
2575 * possible on NTFS) and we write through one link, then stat() it
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00002576 * through the other link, the timestamp information may be wrong.
Bram Moolenaar03f48552006-02-28 23:52:23 +00002577 * It's correct again after reading the file, thus reset the timestamp
2578 * here.
2579 */
2580 if (newfile && !read_stdin && !read_buffer
2581 && mch_stat((char *)fname, &st) >= 0)
2582 {
2583 buf_store_time(curbuf, &st, fname);
2584 curbuf->b_mtime_read = curbuf->b_mtime;
2585 }
2586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 msg_scroll = msg_save;
2589
2590#ifdef FEAT_VIMINFO
2591 /*
2592 * Get the marks before executing autocommands, so they can be used there.
2593 */
2594 check_marks_read();
2595#endif
2596
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 /*
2598 * Trick: We remember if the last line of the read didn't have
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002599 * an eol even when 'binary' is off, for when writing it again with
2600 * 'binary' on. This is required for
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 * ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work.
2602 */
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002603 curbuf->b_no_eol_lnum = read_no_eol_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02002605 /* When reloading a buffer put the cursor at the first line that is
2606 * different. */
2607 if (flags & READ_KEEP_UNDO)
2608 u_find_first_changed();
2609
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002610#ifdef FEAT_PERSISTENT_UNDO
2611 /*
2612 * When opening a new file locate undo info and read it.
2613 */
2614 if (read_undo_file)
2615 {
2616 char_u hash[UNDO_HASH_SIZE];
2617
2618 sha256_finish(&sha_ctx, hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02002619 u_read_undo(NULL, hash, fname);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02002620 }
2621#endif
2622
Bram Moolenaardf177f62005-02-22 08:39:57 +00002623#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (!read_stdin && !read_buffer)
2625 {
2626 int m = msg_scroll;
2627 int n = msg_scrolled;
2628
2629 /* Save the fileformat now, otherwise the buffer will be considered
2630 * modified if the format/encoding was automatically detected. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002631 if (set_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 save_file_ff(curbuf);
2633
2634 /*
2635 * The output from the autocommands should not overwrite anything and
2636 * should not be overwritten: Set msg_scroll, restore its value if no
2637 * output was done.
2638 */
2639 msg_scroll = TRUE;
2640 if (filtering)
2641 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
2642 FALSE, curbuf, eap);
2643 else if (newfile)
2644 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
2645 FALSE, curbuf, eap);
2646 else
2647 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
2648 FALSE, NULL, eap);
2649 if (msg_scrolled == n)
2650 msg_scroll = m;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002651# ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (aborting()) /* autocmds may abort script processing */
2653 return FAIL;
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01002654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656#endif
2657
2658 if (recoverymode && error)
2659 return FAIL;
2660 return OK;
2661}
2662
Bram Moolenaarfe1c56d2007-07-10 15:10:54 +00002663#ifdef OPEN_CHR_FILES
2664/*
2665 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+",
2666 * which is the name of files used for process substitution output by
2667 * some shells on some operating systems, e.g., bash on SunOS.
2668 * Do not accept "/dev/fd/[012]", opening these may hang Vim.
2669 */
2670 static int
2671is_dev_fd_file(fname)
2672 char_u *fname;
2673{
2674 return (STRNCMP(fname, "/dev/fd/", 8) == 0
2675 && VIM_ISDIGIT(fname[8])
2676 && *skipdigits(fname + 9) == NUL
2677 && (fname[9] != NUL
2678 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
2679}
2680#endif
2681
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00002682#ifdef FEAT_MBYTE
2683
2684/*
2685 * From the current line count and characters read after that, estimate the
2686 * line number where we are now.
2687 * Used for error messages that include a line number.
2688 */
2689 static linenr_T
2690readfile_linenr(linecnt, p, endp)
2691 linenr_T linecnt; /* line count before reading more bytes */
2692 char_u *p; /* start of more bytes read */
2693 char_u *endp; /* end of more bytes read */
2694{
2695 char_u *s;
2696 linenr_T lnum;
2697
2698 lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
2699 for (s = p; s < endp; ++s)
2700 if (*s == '\n')
2701 ++lnum;
2702 return lnum;
2703}
2704#endif
2705
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00002707 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be
2708 * equal to the buffer "buf". Used for calling readfile().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 * Returns OK or FAIL.
2710 */
2711 int
2712prep_exarg(eap, buf)
2713 exarg_T *eap;
2714 buf_T *buf;
2715{
2716 eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff)
2717#ifdef FEAT_MBYTE
2718 + STRLEN(buf->b_p_fenc)
2719#endif
2720 + 15));
2721 if (eap->cmd == NULL)
2722 return FAIL;
2723
2724#ifdef FEAT_MBYTE
2725 sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc);
2726 eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff);
Bram Moolenaar195d6352005-12-19 22:08:24 +00002727 eap->bad_char = buf->b_bad_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#else
2729 sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff);
2730#endif
2731 eap->force_ff = 7;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002732
2733 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002734 eap->read_edit = FALSE;
Bram Moolenaar195d6352005-12-19 22:08:24 +00002735 eap->forceit = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 return OK;
2737}
2738
2739#ifdef FEAT_MBYTE
2740/*
2741 * Find next fileencoding to use from 'fileencodings'.
2742 * "pp" points to fenc_next. It's advanced to the next item.
2743 * When there are no more items, an empty string is returned and *pp is set to
2744 * NULL.
2745 * When *pp is not set to NULL, the result is in allocated memory.
2746 */
2747 static char_u *
2748next_fenc(pp)
2749 char_u **pp;
2750{
2751 char_u *p;
2752 char_u *r;
2753
2754 if (**pp == NUL)
2755 {
2756 *pp = NULL;
2757 return (char_u *)"";
2758 }
2759 p = vim_strchr(*pp, ',');
2760 if (p == NULL)
2761 {
2762 r = enc_canonize(*pp);
2763 *pp += STRLEN(*pp);
2764 }
2765 else
2766 {
2767 r = vim_strnsave(*pp, (int)(p - *pp));
2768 *pp = p + 1;
2769 if (r != NULL)
2770 {
2771 p = enc_canonize(r);
2772 vim_free(r);
2773 r = p;
2774 }
2775 }
2776 if (r == NULL) /* out of memory */
2777 {
2778 r = (char_u *)"";
2779 *pp = NULL;
2780 }
2781 return r;
2782}
2783
2784# ifdef FEAT_EVAL
2785/*
2786 * Convert a file with the 'charconvert' expression.
2787 * This closes the file which is to be read, converts it and opens the
2788 * resulting file for reading.
2789 * Returns name of the resulting converted file (the caller should delete it
2790 * after reading it).
2791 * Returns NULL if the conversion failed ("*fdp" is not set) .
2792 */
2793 static char_u *
2794readfile_charconvert(fname, fenc, fdp)
2795 char_u *fname; /* name of input file */
2796 char_u *fenc; /* converted from */
2797 int *fdp; /* in/out: file descriptor of file */
2798{
2799 char_u *tmpname;
2800 char_u *errmsg = NULL;
2801
2802 tmpname = vim_tempname('r');
2803 if (tmpname == NULL)
2804 errmsg = (char_u *)_("Can't find temp file for conversion");
2805 else
2806 {
2807 close(*fdp); /* close the input file, ignore errors */
2808 *fdp = -1;
2809 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
2810 fname, tmpname) == FAIL)
2811 errmsg = (char_u *)_("Conversion with 'charconvert' failed");
2812 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
2813 O_RDONLY | O_EXTRA, 0)) < 0)
2814 errmsg = (char_u *)_("can't read output of 'charconvert'");
2815 }
2816
2817 if (errmsg != NULL)
2818 {
2819 /* Don't use emsg(), it breaks mappings, the retry with
2820 * another type of conversion might still work. */
2821 MSG(errmsg);
2822 if (tmpname != NULL)
2823 {
2824 mch_remove(tmpname); /* delete converted file */
2825 vim_free(tmpname);
2826 tmpname = NULL;
2827 }
2828 }
2829
2830 /* If the input file is closed, open it (caller should check for error). */
2831 if (*fdp < 0)
2832 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
2833
2834 return tmpname;
2835}
2836# endif
2837
2838#endif
2839
2840#ifdef FEAT_VIMINFO
2841/*
2842 * Read marks for the current buffer from the viminfo file, when we support
2843 * buffer marks and the buffer has a name.
2844 */
2845 static void
2846check_marks_read()
2847{
2848 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0
2849 && curbuf->b_ffname != NULL)
Bram Moolenaard812df62008-11-09 12:46:09 +00002850 read_viminfo(NULL, VIF_WANT_MARKS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
2852 /* Always set b_marks_read; needed when 'viminfo' is changed to include
2853 * the ' parameter after opening a buffer. */
2854 curbuf->b_marks_read = TRUE;
2855}
2856#endif
2857
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002858#if defined(FEAT_CRYPT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859/*
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002860 * Get the crypt method used for a file from "ptr[len]", the magic text at the
2861 * start of the file.
2862 * Returns -1 when no encryption used.
2863 */
2864 static int
Bram Moolenaar49771f42010-07-20 17:32:38 +02002865crypt_method_from_magic(ptr, len)
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002866 char *ptr;
2867 int len;
2868{
2869 int i;
2870
2871 for (i = 0; i < (int)(sizeof(crypt_magic) / sizeof(crypt_magic[0])); i++)
2872 {
Bram Moolenaar80794b12010-06-13 05:20:42 +02002873 if (len < (CRYPT_MAGIC_LEN + crypt_salt_len[i] + crypt_seed_len[i]))
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002874 continue;
2875 if (memcmp(ptr, crypt_magic[i], CRYPT_MAGIC_LEN) == 0)
2876 return i;
2877 }
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002878
Bram Moolenaar442b4222010-05-24 21:34:22 +02002879 i = (int)STRLEN(crypt_magic_head);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002880 if (len >= i && memcmp(ptr, crypt_magic_head, i) == 0)
2881 EMSG(_("E821: File is encrypted with unknown method"));
2882
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002883 return -1;
2884}
2885
2886/*
2887 * Check for magic number used for encryption. Applies to the current buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 * If found, the magic number is removed from ptr[*sizep] and *sizep and
2889 * *filesizep are updated.
2890 * Return the (new) encryption key, NULL for no encryption.
2891 */
2892 static char_u *
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002893check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 char_u *cryptkey; /* previous encryption key or NULL */
2895 char_u *ptr; /* pointer to read bytes */
2896 long *sizep; /* length of read bytes */
Bram Moolenaar914703b2010-05-31 21:59:46 +02002897 off_t *filesizep; /* nr of bytes used from file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 int newfile; /* editing a new buffer */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002899 char_u *fname; /* file name to display */
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002900 int *did_ask; /* flag: whether already asked for key */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
Bram Moolenaar49771f42010-07-20 17:32:38 +02002902 int method = crypt_method_from_magic((char *)ptr, *sizep);
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002903
2904 if (method >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
Bram Moolenaar49771f42010-07-20 17:32:38 +02002906 set_crypt_method(curbuf, method);
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002907 if (method > 0)
2908 (void)blowfish_self_test();
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002909 if (cryptkey == NULL && !*did_ask)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
2911 if (*curbuf->b_p_key)
2912 cryptkey = curbuf->b_p_key;
2913 else
2914 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002915 /* When newfile is TRUE, store the typed key in the 'key'
2916 * option and don't free it. bf needs hash of the key saved.
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002917 * Don't ask for the key again when first time Enter was hit.
2918 * Happens when retrying to detect encoding. */
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002919 smsg((char_u *)_(need_key_msg), fname);
2920 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 cryptkey = get_crypt_key(newfile, FALSE);
Bram Moolenaarf50a2532010-05-21 15:36:08 +02002922 *did_ask = TRUE;
2923
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 /* check if empty key entered */
2925 if (cryptkey != NULL && *cryptkey == NUL)
2926 {
2927 if (cryptkey != curbuf->b_p_key)
2928 vim_free(cryptkey);
2929 cryptkey = NULL;
2930 }
2931 }
2932 }
2933
2934 if (cryptkey != NULL)
2935 {
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002936 int seed_len = crypt_seed_len[method];
Bram Moolenaar80794b12010-06-13 05:20:42 +02002937 int salt_len = crypt_salt_len[method];
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002938
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002939 crypt_push_state();
2940 use_crypt_method = method;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002941 if (method == 0)
2942 crypt_init_keys(cryptkey);
2943 else
2944 {
Bram Moolenaar80794b12010-06-13 05:20:42 +02002945 bf_key_init(cryptkey, ptr + CRYPT_MAGIC_LEN, salt_len);
2946 bf_ofb_init(ptr + CRYPT_MAGIC_LEN + salt_len, seed_len);
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002947 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948
2949 /* Remove magic number from the text */
Bram Moolenaar80794b12010-06-13 05:20:42 +02002950 *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len;
2951 *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002952 mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len,
2953 (size_t)*sizep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955 }
Bram Moolenaar40e6a712010-05-16 22:32:54 +02002956 /* When starting to edit a new file which does not have encryption, clear
2957 * the 'key' option, except when starting up (called with -x argument) */
Bram Moolenaarfa0ff9a2010-07-25 16:05:19 +02002958 else if (newfile && *curbuf->b_p_key != NUL && !starting)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
2960
2961 return cryptkey;
2962}
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002963
2964/*
2965 * Check for magic number used for encryption. Applies to the current buffer.
2966 * If found and decryption is possible returns OK;
2967 */
2968 int
2969prepare_crypt_read(fp)
2970 FILE *fp;
2971{
2972 int method;
Bram Moolenaar80794b12010-06-13 05:20:42 +02002973 char_u buffer[CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX
2974 + CRYPT_SEED_LEN_MAX + 2];
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002975
2976 if (fread(buffer, CRYPT_MAGIC_LEN, 1, fp) != 1)
2977 return FAIL;
Bram Moolenaar49771f42010-07-20 17:32:38 +02002978 method = crypt_method_from_magic((char *)buffer,
Bram Moolenaar80794b12010-06-13 05:20:42 +02002979 CRYPT_MAGIC_LEN +
Bram Moolenaarcc448b32010-07-14 16:52:17 +02002980 CRYPT_SEED_LEN_MAX +
2981 CRYPT_SALT_LEN_MAX);
Bram Moolenaar49771f42010-07-20 17:32:38 +02002982 if (method < 0 || method != get_crypt_method(curbuf))
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002983 return FAIL;
2984
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02002985 crypt_push_state();
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002986 if (method == 0)
2987 crypt_init_keys(curbuf->b_p_key);
2988 else
2989 {
Bram Moolenaar80794b12010-06-13 05:20:42 +02002990 int salt_len = crypt_salt_len[method];
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002991 int seed_len = crypt_seed_len[method];
2992
Bram Moolenaar80794b12010-06-13 05:20:42 +02002993 if (fread(buffer, salt_len + seed_len, 1, fp) != 1)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002994 return FAIL;
Bram Moolenaar80794b12010-06-13 05:20:42 +02002995 bf_key_init(curbuf->b_p_key, buffer, salt_len);
2996 bf_ofb_init(buffer + salt_len, seed_len);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02002997 }
2998 return OK;
2999}
3000
3001/*
3002 * Prepare for writing encrypted bytes for buffer "buf".
3003 * Returns a pointer to an allocated header of length "*lenp".
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02003004 * When out of memory returns NULL.
3005 * Otherwise calls crypt_push_state(), call crypt_pop_state() later.
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02003006 */
3007 char_u *
3008prepare_crypt_write(buf, lenp)
3009 buf_T *buf;
3010 int *lenp;
3011{
3012 char_u *header;
Bram Moolenaar49771f42010-07-20 17:32:38 +02003013 int seed_len = crypt_seed_len[get_crypt_method(buf)];
3014 int salt_len = crypt_salt_len[get_crypt_method(buf)];
Bram Moolenaar80794b12010-06-13 05:20:42 +02003015 char_u *salt;
3016 char_u *seed;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02003017
Bram Moolenaar80794b12010-06-13 05:20:42 +02003018 header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX
3019 + CRYPT_SEED_LEN_MAX + 2);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02003020 if (header != NULL)
3021 {
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02003022 crypt_push_state();
Bram Moolenaar49771f42010-07-20 17:32:38 +02003023 use_crypt_method = get_crypt_method(buf); /* select zip or blowfish */
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02003024 vim_strncpy(header, (char_u *)crypt_magic[use_crypt_method],
3025 CRYPT_MAGIC_LEN);
Bram Moolenaar49771f42010-07-20 17:32:38 +02003026 if (use_crypt_method == 0)
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02003027 crypt_init_keys(buf->b_p_key);
3028 else
3029 {
Bram Moolenaar80794b12010-06-13 05:20:42 +02003030 /* Using blowfish, add salt and seed. */
3031 salt = header + CRYPT_MAGIC_LEN;
3032 seed = salt + salt_len;
3033 sha2_seed(salt, salt_len, seed, seed_len);
3034 bf_key_init(buf->b_p_key, salt, salt_len);
3035 bf_ofb_init(seed, seed_len);
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02003036 }
3037 }
Bram Moolenaar80794b12010-06-13 05:20:42 +02003038 *lenp = CRYPT_MAGIC_LEN + salt_len + seed_len;
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02003039 return header;
3040}
3041
Bram Moolenaar80794b12010-06-13 05:20:42 +02003042#endif /* FEAT_CRYPT */
3043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#ifdef UNIX
3045 static void
3046set_file_time(fname, atime, mtime)
3047 char_u *fname;
3048 time_t atime; /* access time */
3049 time_t mtime; /* modification time */
3050{
3051# if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
3052 struct utimbuf buf;
3053
3054 buf.actime = atime;
3055 buf.modtime = mtime;
3056 (void)utime((char *)fname, &buf);
3057# else
3058# if defined(HAVE_UTIMES)
3059 struct timeval tvp[2];
3060
3061 tvp[0].tv_sec = atime;
3062 tvp[0].tv_usec = 0;
3063 tvp[1].tv_sec = mtime;
3064 tvp[1].tv_usec = 0;
3065# ifdef NeXT
3066 (void)utimes((char *)fname, tvp);
3067# else
3068 (void)utimes((char *)fname, (const struct timeval *)&tvp);
3069# endif
3070# endif
3071# endif
3072}
3073#endif /* UNIX */
3074
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003075#if defined(VMS) && !defined(MIN)
3076/* Older DECC compiler for VAX doesn't define MIN() */
3077# define MIN(a, b) ((a) < (b) ? (a) : (b))
3078#endif
3079
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080/*
Bram Moolenaar5386a122007-06-28 20:02:32 +00003081 * Return TRUE if a file appears to be read-only from the file permissions.
3082 */
3083 int
3084check_file_readonly(fname, perm)
3085 char_u *fname; /* full path to file */
3086 int perm; /* known permissions on file */
3087{
3088#ifndef USE_MCH_ACCESS
3089 int fd = 0;
3090#endif
3091
3092 return (
3093#ifdef USE_MCH_ACCESS
3094# ifdef UNIX
3095 (perm & 0222) == 0 ||
3096# endif
3097 mch_access((char *)fname, W_OK)
3098#else
3099 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
3100 ? TRUE : (close(fd), FALSE)
3101#endif
3102 );
3103}
3104
3105
3106/*
Bram Moolenaar292ad192005-12-11 21:29:51 +00003107 * buf_write() - write to file "fname" lines "start" through "end"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 *
3109 * We do our own buffering here because fwrite() is so slow.
3110 *
Bram Moolenaar292ad192005-12-11 21:29:51 +00003111 * If "forceit" is true, we don't care for errors when attempting backups.
3112 * In case of an error everything possible is done to restore the original
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003113 * file. But when "forceit" is TRUE, we risk losing it.
Bram Moolenaar292ad192005-12-11 21:29:51 +00003114 *
3115 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and
3116 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 *
3118 * This function must NOT use NameBuff (because it's called by autowrite()).
3119 *
3120 * return FAIL for failure, OK otherwise
3121 */
3122 int
3123buf_write(buf, fname, sfname, start, end, eap, append, forceit,
3124 reset_changed, filtering)
3125 buf_T *buf;
3126 char_u *fname;
3127 char_u *sfname;
3128 linenr_T start, end;
3129 exarg_T *eap; /* for forced 'ff' and 'fenc', can be
3130 NULL! */
Bram Moolenaar292ad192005-12-11 21:29:51 +00003131 int append; /* append to the file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 int forceit;
3133 int reset_changed;
3134 int filtering;
3135{
3136 int fd;
3137 char_u *backup = NULL;
3138 int backup_copy = FALSE; /* copy the original file? */
3139 int dobackup;
3140 char_u *ffname;
3141 char_u *wfname = NULL; /* name of file to write to */
3142 char_u *s;
3143 char_u *ptr;
3144 char_u c;
3145 int len;
3146 linenr_T lnum;
3147 long nchars;
3148 char_u *errmsg = NULL;
Bram Moolenaar32b485f2009-07-29 16:06:27 +00003149 int errmsg_allocated = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 char_u *errnum = NULL;
3151 char_u *buffer;
3152 char_u smallbuf[SMBUFSIZE];
3153 char_u *backup_ext;
3154 int bufsize;
3155 long perm; /* file permissions */
3156 int retval = OK;
3157 int newfile = FALSE; /* TRUE if file doesn't exist yet */
3158 int msg_save = msg_scroll;
3159 int overwriting; /* TRUE if writing over original */
3160 int no_eol = FALSE; /* no end-of-line written */
3161 int device = FALSE; /* writing to a device */
3162 struct stat st_old;
3163 int prev_got_int = got_int;
3164 int file_readonly = FALSE; /* overwritten file is read-only */
3165 static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')";
3166#if defined(UNIX) || defined(__EMX__XX) /*XXX fix me sometime? */
3167 int made_writable = FALSE; /* 'w' bit has been set */
3168#endif
3169 /* writing everything */
3170 int whole = (start == 1 && end == buf->b_ml.ml_line_count);
3171#ifdef FEAT_AUTOCMD
3172 linenr_T old_line_count = buf->b_ml.ml_line_count;
3173#endif
3174 int attr;
3175 int fileformat;
3176 int write_bin;
3177 struct bw_info write_info; /* info for buf_write_bytes() */
3178#ifdef FEAT_MBYTE
3179 int converted = FALSE;
3180 int notconverted = FALSE;
3181 char_u *fenc; /* effective 'fileencoding' */
3182 char_u *fenc_tofree = NULL; /* allocated "fenc" */
3183#endif
3184#ifdef HAS_BW_FLAGS
3185 int wb_flags = 0;
3186#endif
3187#ifdef HAVE_ACL
3188 vim_acl_T acl = NULL; /* ACL copied from original file to
3189 backup or new file */
3190#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +02003191#ifdef FEAT_PERSISTENT_UNDO
3192 int write_undo_file = FALSE;
3193 context_sha256_T sha_ctx;
3194#endif
Bram Moolenaar4cf35c22011-02-25 16:52:17 +01003195#ifdef FEAT_CRYPT
3196 int crypt_method_used;
3197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198
3199 if (fname == NULL || *fname == NUL) /* safety check */
3200 return FAIL;
Bram Moolenaar70d60e92009-12-31 13:53:33 +00003201 if (buf->b_ml.ml_mfp == NULL)
3202 {
3203 /* This can happen during startup when there is a stray "w" in the
3204 * vimrc file. */
3205 EMSG(_(e_emptybuf));
3206 return FAIL;
3207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
3209 /*
3210 * Disallow writing from .exrc and .vimrc in current directory for
3211 * security reasons.
3212 */
3213 if (check_secure())
3214 return FAIL;
3215
3216 /* Avoid a crash for a long name. */
3217 if (STRLEN(fname) >= MAXPATHL)
3218 {
3219 EMSG(_(e_longname));
3220 return FAIL;
3221 }
3222
3223#ifdef FEAT_MBYTE
3224 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */
3225 write_info.bw_conv_buf = NULL;
3226 write_info.bw_conv_error = FALSE;
Bram Moolenaar32b485f2009-07-29 16:06:27 +00003227 write_info.bw_conv_error_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 write_info.bw_restlen = 0;
3229# ifdef USE_ICONV
3230 write_info.bw_iconv_fd = (iconv_t)-1;
3231# endif
3232#endif
3233
Bram Moolenaardf177f62005-02-22 08:39:57 +00003234 /* After writing a file changedtick changes but we don't want to display
3235 * the line. */
3236 ex_no_reprint = TRUE;
3237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 /*
3239 * If there is no file name yet, use the one for the written file.
3240 * BF_NOTEDITED is set to reflect this (in case the write fails).
3241 * Don't do this when the write is for a filter command.
Bram Moolenaar292ad192005-12-11 21:29:51 +00003242 * Don't do this when appending.
3243 * Only do this when 'cpoptions' contains the 'F' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003245 if (buf->b_ffname == NULL
3246 && reset_changed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 && whole
3248 && buf == curbuf
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00003249#ifdef FEAT_QUICKFIX
3250 && !bt_nofile(buf)
3251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 && !filtering
Bram Moolenaar292ad192005-12-11 21:29:51 +00003253 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL)
3255 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003256 if (set_rw_fname(fname, sfname) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 return FAIL;
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00003258 buf = curbuf; /* just in case autocmds made "buf" invalid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 }
3260
3261 if (sfname == NULL)
3262 sfname = fname;
3263 /*
3264 * For Unix: Use the short file name whenever possible.
3265 * Avoids problems with networks and when directory names are changed.
3266 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to
3267 * another directory, which we don't detect
3268 */
3269 ffname = fname; /* remember full fname */
3270#ifdef UNIX
3271 fname = sfname;
3272#endif
3273
3274 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0)
3275 overwriting = TRUE;
3276 else
3277 overwriting = FALSE;
3278
3279 if (exiting)
3280 settmode(TMODE_COOK); /* when exiting allow typahead now */
3281
3282 ++no_wait_return; /* don't wait for return yet */
3283
3284 /*
3285 * Set '[ and '] marks to the lines to be written.
3286 */
3287 buf->b_op_start.lnum = start;
3288 buf->b_op_start.col = 0;
3289 buf->b_op_end.lnum = end;
3290 buf->b_op_end.col = 0;
3291
3292#ifdef FEAT_AUTOCMD
3293 {
3294 aco_save_T aco;
3295 int buf_ffname = FALSE;
3296 int buf_sfname = FALSE;
3297 int buf_fname_f = FALSE;
3298 int buf_fname_s = FALSE;
3299 int did_cmd = FALSE;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003300 int nofile_err = FALSE;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003301 int empty_memline = (buf->b_ml.ml_mfp == NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302
3303 /*
3304 * Apply PRE aucocommands.
3305 * Set curbuf to the buffer to be written.
3306 * Careful: The autocommands may call buf_write() recursively!
3307 */
3308 if (ffname == buf->b_ffname)
3309 buf_ffname = TRUE;
3310 if (sfname == buf->b_sfname)
3311 buf_sfname = TRUE;
3312 if (fname == buf->b_ffname)
3313 buf_fname_f = TRUE;
3314 if (fname == buf->b_sfname)
3315 buf_fname_s = TRUE;
3316
3317 /* set curwin/curbuf to buf and save a few things */
3318 aucmd_prepbuf(&aco, buf);
3319
3320 if (append)
3321 {
3322 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD,
3323 sfname, sfname, FALSE, curbuf, eap)))
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003324 {
Bram Moolenaarb1b715d2006-01-21 22:09:43 +00003325#ifdef FEAT_QUICKFIX
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00003326 if (overwriting && bt_nofile(curbuf))
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003327 nofile_err = TRUE;
3328 else
Bram Moolenaarb1b715d2006-01-21 22:09:43 +00003329#endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003330 apply_autocmds_exarg(EVENT_FILEAPPENDPRE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 sfname, sfname, FALSE, curbuf, eap);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 else if (filtering)
3335 {
3336 apply_autocmds_exarg(EVENT_FILTERWRITEPRE,
3337 NULL, sfname, FALSE, curbuf, eap);
3338 }
3339 else if (reset_changed && whole)
3340 {
Bram Moolenaar39fc42e2011-09-02 11:56:20 +02003341 int was_changed = curbufIsChanged();
3342
3343 did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD,
3344 sfname, sfname, FALSE, curbuf, eap);
3345 if (did_cmd)
3346 {
3347 if (was_changed && !curbufIsChanged())
3348 {
3349 /* Written everything correctly and BufWriteCmd has reset
3350 * 'modified': Correct the undo information so that an
3351 * undo now sets 'modified'. */
3352 u_unchanged(curbuf);
3353 u_update_save_nr(curbuf);
3354 }
3355 }
3356 else
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003357 {
Bram Moolenaarb1b715d2006-01-21 22:09:43 +00003358#ifdef FEAT_QUICKFIX
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003359 if (overwriting && bt_nofile(curbuf))
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003360 nofile_err = TRUE;
3361 else
Bram Moolenaarb1b715d2006-01-21 22:09:43 +00003362#endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003363 apply_autocmds_exarg(EVENT_BUFWRITEPRE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 sfname, sfname, FALSE, curbuf, eap);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
3367 else
3368 {
3369 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD,
3370 sfname, sfname, FALSE, curbuf, eap)))
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003371 {
Bram Moolenaarb1b715d2006-01-21 22:09:43 +00003372#ifdef FEAT_QUICKFIX
Bram Moolenaar19a09a12005-03-04 23:39:37 +00003373 if (overwriting && bt_nofile(curbuf))
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003374 nofile_err = TRUE;
3375 else
Bram Moolenaarb1b715d2006-01-21 22:09:43 +00003376#endif
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003377 apply_autocmds_exarg(EVENT_FILEWRITEPRE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 sfname, sfname, FALSE, curbuf, eap);
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 }
3381
3382 /* restore curwin/curbuf and a few other things */
3383 aucmd_restbuf(&aco);
3384
3385 /*
3386 * In three situations we return here and don't write the file:
3387 * 1. the autocommands deleted or unloaded the buffer.
3388 * 2. The autocommands abort script processing.
3389 * 3. If one of the "Cmd" autocommands was executed.
3390 */
3391 if (!buf_valid(buf))
3392 buf = NULL;
Bram Moolenaar7c626922005-02-07 22:01:03 +00003393 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline)
Bram Moolenaar1e015462005-09-25 22:16:38 +00003394 || did_cmd || nofile_err
3395#ifdef FEAT_EVAL
3396 || aborting()
3397#endif
3398 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 {
3400 --no_wait_return;
3401 msg_scroll = msg_save;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003402 if (nofile_err)
3403 EMSG(_("E676: No matching autocommands for acwrite buffer"));
3404
Bram Moolenaar1e015462005-09-25 22:16:38 +00003405 if (nofile_err
3406#ifdef FEAT_EVAL
3407 || aborting()
3408#endif
3409 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 /* An aborting error, interrupt or exception in the
3411 * autocommands. */
3412 return FAIL;
3413 if (did_cmd)
3414 {
3415 if (buf == NULL)
3416 /* The buffer was deleted. We assume it was written
3417 * (can't retry anyway). */
3418 return OK;
3419 if (overwriting)
3420 {
3421 /* Assume the buffer was written, update the timestamp. */
3422 ml_timestamp(buf);
Bram Moolenaar292ad192005-12-11 21:29:51 +00003423 if (append)
3424 buf->b_flags &= ~BF_NEW;
3425 else
3426 buf->b_flags &= ~BF_WRITE_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 }
Bram Moolenaar292ad192005-12-11 21:29:51 +00003428 if (reset_changed && buf->b_changed && !append
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003429 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 /* Buffer still changed, the autocommands didn't work
3431 * properly. */
3432 return FAIL;
3433 return OK;
3434 }
3435#ifdef FEAT_EVAL
3436 if (!aborting())
3437#endif
3438 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written"));
3439 return FAIL;
3440 }
3441
3442 /*
3443 * The autocommands may have changed the number of lines in the file.
3444 * When writing the whole file, adjust the end.
3445 * When writing part of the file, assume that the autocommands only
3446 * changed the number of lines that are to be written (tricky!).
3447 */
3448 if (buf->b_ml.ml_line_count != old_line_count)
3449 {
3450 if (whole) /* write all */
3451 end = buf->b_ml.ml_line_count;
3452 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */
3453 end += buf->b_ml.ml_line_count - old_line_count;
3454 else /* less lines */
3455 {
3456 end -= old_line_count - buf->b_ml.ml_line_count;
3457 if (end < start)
3458 {
3459 --no_wait_return;
3460 msg_scroll = msg_save;
3461 EMSG(_("E204: Autocommand changed number of lines in unexpected way"));
3462 return FAIL;
3463 }
3464 }
3465 }
3466
3467 /*
3468 * The autocommands may have changed the name of the buffer, which may
3469 * be kept in fname, ffname and sfname.
3470 */
3471 if (buf_ffname)
3472 ffname = buf->b_ffname;
3473 if (buf_sfname)
3474 sfname = buf->b_sfname;
3475 if (buf_fname_f)
3476 fname = buf->b_ffname;
3477 if (buf_fname_s)
3478 fname = buf->b_sfname;
3479 }
3480#endif
3481
3482#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02003483 if (netbeans_active() && isNetbeansBuffer(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 if (whole)
3486 {
3487 /*
3488 * b_changed can be 0 after an undo, but we still need to write
3489 * the buffer to NetBeans.
3490 */
3491 if (buf->b_changed || isNetbeansModified(buf))
3492 {
Bram Moolenaar009b2592004-10-24 19:18:58 +00003493 --no_wait_return; /* may wait for return now */
3494 msg_scroll = msg_save;
3495 netbeans_save_buffer(buf); /* no error checking... */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 return retval;
3497 }
3498 else
3499 {
3500 errnum = (char_u *)"E656: ";
Bram Moolenaared0e7452008-06-27 19:17:34 +00003501 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 buffer = NULL;
3503 goto fail;
3504 }
3505 }
3506 else
3507 {
3508 errnum = (char_u *)"E657: ";
3509 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers");
3510 buffer = NULL;
3511 goto fail;
3512 }
3513 }
3514#endif
3515
3516 if (shortmess(SHM_OVER) && !exiting)
3517 msg_scroll = FALSE; /* overwrite previous file message */
3518 else
3519 msg_scroll = TRUE; /* don't overwrite previous file message */
3520 if (!filtering)
3521 filemess(buf,
3522#ifndef UNIX
3523 sfname,
3524#else
3525 fname,
3526#endif
3527 (char_u *)"", 0); /* show that we are busy */
3528 msg_scroll = FALSE; /* always overwrite the file message now */
3529
3530 buffer = alloc(BUFSIZE);
3531 if (buffer == NULL) /* can't allocate big buffer, use small
3532 * one (to be able to write when out of
3533 * memory) */
3534 {
3535 buffer = smallbuf;
3536 bufsize = SMBUFSIZE;
3537 }
3538 else
3539 bufsize = BUFSIZE;
3540
3541 /*
3542 * Get information about original file (if there is one).
3543 */
3544#if defined(UNIX) && !defined(ARCHIE)
Bram Moolenaar6f192452007-11-08 19:49:02 +00003545 st_old.st_dev = 0;
3546 st_old.st_ino = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 perm = -1;
3548 if (mch_stat((char *)fname, &st_old) < 0)
3549 newfile = TRUE;
3550 else
3551 {
3552 perm = st_old.st_mode;
3553 if (!S_ISREG(st_old.st_mode)) /* not a file */
3554 {
3555 if (S_ISDIR(st_old.st_mode))
3556 {
3557 errnum = (char_u *)"E502: ";
3558 errmsg = (char_u *)_("is a directory");
3559 goto fail;
3560 }
3561 if (mch_nodetype(fname) != NODE_WRITABLE)
3562 {
3563 errnum = (char_u *)"E503: ";
3564 errmsg = (char_u *)_("is not a file or writable device");
3565 goto fail;
3566 }
3567 /* It's a device of some kind (or a fifo) which we can write to
3568 * but for which we can't make a backup. */
3569 device = TRUE;
3570 newfile = TRUE;
3571 perm = -1;
3572 }
3573 }
3574#else /* !UNIX */
3575 /*
3576 * Check for a writable device name.
3577 */
3578 c = mch_nodetype(fname);
3579 if (c == NODE_OTHER)
3580 {
3581 errnum = (char_u *)"E503: ";
3582 errmsg = (char_u *)_("is not a file or writable device");
3583 goto fail;
3584 }
3585 if (c == NODE_WRITABLE)
3586 {
Bram Moolenaar043545e2006-10-10 16:44:07 +00003587# if defined(MSDOS) || defined(MSWIN) || defined(OS2)
3588 /* MS-Windows allows opening a device, but we will probably get stuck
3589 * trying to write to it. */
3590 if (!p_odev)
3591 {
3592 errnum = (char_u *)"E796: ";
3593 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option");
3594 goto fail;
3595 }
3596# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 device = TRUE;
3598 newfile = TRUE;
3599 perm = -1;
3600 }
3601 else
3602 {
3603 perm = mch_getperm(fname);
3604 if (perm < 0)
3605 newfile = TRUE;
3606 else if (mch_isdir(fname))
3607 {
3608 errnum = (char_u *)"E502: ";
3609 errmsg = (char_u *)_("is a directory");
3610 goto fail;
3611 }
3612 if (overwriting)
3613 (void)mch_stat((char *)fname, &st_old);
3614 }
3615#endif /* !UNIX */
3616
3617 if (!device && !newfile)
3618 {
3619 /*
3620 * Check if the file is really writable (when renaming the file to
3621 * make a backup we won't discover it later).
3622 */
Bram Moolenaar5386a122007-06-28 20:02:32 +00003623 file_readonly = check_file_readonly(fname, (int)perm);
3624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (!forceit && file_readonly)
3626 {
3627 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL)
3628 {
3629 errnum = (char_u *)"E504: ";
3630 errmsg = (char_u *)_(err_readonly);
3631 }
3632 else
3633 {
3634 errnum = (char_u *)"E505: ";
3635 errmsg = (char_u *)_("is read-only (add ! to override)");
3636 }
3637 goto fail;
3638 }
3639
3640 /*
3641 * Check if the timestamp hasn't changed since reading the file.
3642 */
3643 if (overwriting)
3644 {
3645 retval = check_mtime(buf, &st_old);
3646 if (retval == FAIL)
3647 goto fail;
3648 }
3649 }
3650
3651#ifdef HAVE_ACL
3652 /*
3653 * For systems that support ACL: get the ACL from the original file.
3654 */
3655 if (!newfile)
3656 acl = mch_get_acl(fname);
3657#endif
3658
3659 /*
3660 * If 'backupskip' is not empty, don't make a backup for some files.
3661 */
3662 dobackup = (p_wb || p_bk || *p_pm != NUL);
3663#ifdef FEAT_WILDIGN
3664 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname))
3665 dobackup = FALSE;
3666#endif
3667
3668 /*
3669 * Save the value of got_int and reset it. We don't want a previous
3670 * interruption cancel writing, only hitting CTRL-C while writing should
3671 * abort it.
3672 */
3673 prev_got_int = got_int;
3674 got_int = FALSE;
3675
3676 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */
3677 buf->b_saving = TRUE;
3678
3679 /*
3680 * If we are not appending or filtering, the file exists, and the
3681 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup.
3682 * When 'patchmode' is set also make a backup when appending.
3683 *
3684 * Do not make any backup, if 'writebackup' and 'backup' are both switched
3685 * off. This helps when editing large files on almost-full disks.
3686 */
3687 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup)
3688 {
3689#if defined(UNIX) || defined(WIN32)
3690 struct stat st;
3691#endif
3692
3693 if ((bkc_flags & BKC_YES) || append) /* "yes" */
3694 backup_copy = TRUE;
3695#if defined(UNIX) || defined(WIN32)
3696 else if ((bkc_flags & BKC_AUTO)) /* "auto" */
3697 {
3698 int i;
3699
3700# ifdef UNIX
3701 /*
3702 * Don't rename the file when:
3703 * - it's a hard link
3704 * - it's a symbolic link
3705 * - we don't have write permission in the directory
3706 * - we can't set the owner/group of the new file
3707 */
3708 if (st_old.st_nlink > 1
3709 || mch_lstat((char *)fname, &st) < 0
3710 || st.st_dev != st_old.st_dev
Bram Moolenaara5792f52005-11-23 21:25:05 +00003711 || st.st_ino != st_old.st_ino
3712# ifndef HAVE_FCHOWN
3713 || st.st_uid != st_old.st_uid
3714 || st.st_gid != st_old.st_gid
3715# endif
3716 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 backup_copy = TRUE;
3718 else
Bram Moolenaar03f48552006-02-28 23:52:23 +00003719# else
3720# ifdef WIN32
3721 /* On NTFS file systems hard links are possible. */
3722 if (mch_is_linked(fname))
3723 backup_copy = TRUE;
3724 else
3725# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726# endif
3727 {
3728 /*
3729 * Check if we can create a file and set the owner/group to
3730 * the ones from the original file.
3731 * First find a file name that doesn't exist yet (use some
3732 * arbitrary numbers).
3733 */
3734 STRCPY(IObuff, fname);
3735 for (i = 4913; ; i += 123)
3736 {
3737 sprintf((char *)gettail(IObuff), "%d", i);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003738 if (mch_lstat((char *)IObuff, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 break;
3740 }
Bram Moolenaara5792f52005-11-23 21:25:05 +00003741 fd = mch_open((char *)IObuff,
3742 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (fd < 0) /* can't write in directory */
3744 backup_copy = TRUE;
3745 else
3746 {
3747# ifdef UNIX
Bram Moolenaara5792f52005-11-23 21:25:05 +00003748# ifdef HAVE_FCHOWN
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00003749 ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
Bram Moolenaara5792f52005-11-23 21:25:05 +00003750# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (mch_stat((char *)IObuff, &st) < 0
3752 || st.st_uid != st_old.st_uid
3753 || st.st_gid != st_old.st_gid
Bram Moolenaar78a15312009-05-15 19:33:18 +00003754 || (long)st.st_mode != perm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 backup_copy = TRUE;
3756# endif
Bram Moolenaar98358622005-11-28 22:58:23 +00003757 /* Close the file before removing it, on MS-Windows we
3758 * can't delete an open file. */
Bram Moolenaara5792f52005-11-23 21:25:05 +00003759 close(fd);
Bram Moolenaar98358622005-11-28 22:58:23 +00003760 mch_remove(IObuff);
Bram Moolenaar3479c5d2010-08-08 18:46:06 +02003761# ifdef MSWIN
3762 /* MS-Windows may trigger a virus scanner to open the
3763 * file, we can't delete it then. Keep trying for half a
3764 * second. */
3765 {
3766 int try;
3767
3768 for (try = 0; try < 10; ++try)
3769 {
3770 if (mch_lstat((char *)IObuff, &st) < 0)
3771 break;
3772 ui_delay(50L, TRUE); /* wait 50 msec */
3773 mch_remove(IObuff);
3774 }
3775 }
3776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
3779 }
3780
3781# ifdef UNIX
3782 /*
3783 * Break symlinks and/or hardlinks if we've been asked to.
3784 */
3785 if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK))
3786 {
3787 int lstat_res;
3788
3789 lstat_res = mch_lstat((char *)fname, &st);
3790
3791 /* Symlinks. */
3792 if ((bkc_flags & BKC_BREAKSYMLINK)
3793 && lstat_res == 0
3794 && st.st_ino != st_old.st_ino)
3795 backup_copy = FALSE;
3796
3797 /* Hardlinks. */
3798 if ((bkc_flags & BKC_BREAKHARDLINK)
3799 && st_old.st_nlink > 1
3800 && (lstat_res != 0 || st.st_ino == st_old.st_ino))
3801 backup_copy = FALSE;
3802 }
3803#endif
3804
3805#endif
3806
3807 /* make sure we have a valid backup extension to use */
3808 if (*p_bex == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 backup_ext = (char_u *)".bak";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 else
3811 backup_ext = p_bex;
3812
3813 if (backup_copy
3814 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0)
3815 {
3816 int bfd;
3817 char_u *copybuf, *wp;
3818 int some_error = FALSE;
3819 struct stat st_new;
3820 char_u *dirp;
3821 char_u *rootname;
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003822#if defined(UNIX) && !defined(SHORT_FNAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 int did_set_shortname;
3824#endif
3825
3826 copybuf = alloc(BUFSIZE + 1);
3827 if (copybuf == NULL)
3828 {
3829 some_error = TRUE; /* out of memory */
3830 goto nobackup;
3831 }
3832
3833 /*
3834 * Try to make the backup in each directory in the 'bdir' option.
3835 *
3836 * Unix semantics has it, that we may have a writable file,
3837 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g:
3838 * - the directory is not writable,
3839 * - the file may be a symbolic link,
3840 * - the file may belong to another user/group, etc.
3841 *
3842 * For these reasons, the existing writable file must be truncated
3843 * and reused. Creation of a backup COPY will be attempted.
3844 */
3845 dirp = p_bdir;
3846 while (*dirp)
3847 {
3848#ifdef UNIX
3849 st_new.st_ino = 0;
3850 st_new.st_dev = 0;
3851 st_new.st_gid = 0;
3852#endif
3853
3854 /*
3855 * Isolate one directory name, using an entry in 'bdir'.
3856 */
3857 (void)copy_option_part(&dirp, copybuf, BUFSIZE, ",");
3858 rootname = get_file_in_dir(fname, copybuf);
3859 if (rootname == NULL)
3860 {
3861 some_error = TRUE; /* out of memory */
3862 goto nobackup;
3863 }
3864
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003865#if defined(UNIX) && !defined(SHORT_FNAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 did_set_shortname = FALSE;
3867#endif
3868
3869 /*
3870 * May try twice if 'shortname' not set.
3871 */
3872 for (;;)
3873 {
3874 /*
3875 * Make backup file name.
3876 */
3877 backup = buf_modname(
3878#ifdef SHORT_FNAME
3879 TRUE,
3880#else
3881 (buf->b_p_sn || buf->b_shortname),
3882#endif
3883 rootname, backup_ext, FALSE);
3884 if (backup == NULL)
3885 {
3886 vim_free(rootname);
3887 some_error = TRUE; /* out of memory */
3888 goto nobackup;
3889 }
3890
3891 /*
3892 * Check if backup file already exists.
3893 */
3894 if (mch_stat((char *)backup, &st_new) >= 0)
3895 {
3896#ifdef UNIX
3897 /*
3898 * Check if backup file is same as original file.
3899 * May happen when modname() gave the same file back.
3900 * E.g. silly link, or file name-length reached.
3901 * If we don't check here, we either ruin the file
3902 * when copying or erase it after writing. jw.
3903 */
3904 if (st_new.st_dev == st_old.st_dev
3905 && st_new.st_ino == st_old.st_ino)
3906 {
3907 vim_free(backup);
3908 backup = NULL; /* no backup file to delete */
3909# ifndef SHORT_FNAME
3910 /*
3911 * may try again with 'shortname' set
3912 */
3913 if (!(buf->b_shortname || buf->b_p_sn))
3914 {
3915 buf->b_shortname = TRUE;
3916 did_set_shortname = TRUE;
3917 continue;
3918 }
3919 /* setting shortname didn't help */
3920 if (did_set_shortname)
3921 buf->b_shortname = FALSE;
3922# endif
3923 break;
3924 }
3925#endif
3926
3927 /*
3928 * If we are not going to keep the backup file, don't
3929 * delete an existing one, try to use another name.
3930 * Change one character, just before the extension.
3931 */
3932 if (!p_bk)
3933 {
3934 wp = backup + STRLEN(backup) - 1
3935 - STRLEN(backup_ext);
3936 if (wp < backup) /* empty file name ??? */
3937 wp = backup;
3938 *wp = 'z';
3939 while (*wp > 'a'
3940 && mch_stat((char *)backup, &st_new) >= 0)
3941 --*wp;
3942 /* They all exist??? Must be something wrong. */
3943 if (*wp == 'a')
3944 {
3945 vim_free(backup);
3946 backup = NULL;
3947 }
3948 }
3949 }
3950 break;
3951 }
3952 vim_free(rootname);
3953
3954 /*
3955 * Try to create the backup file
3956 */
3957 if (backup != NULL)
3958 {
3959 /* remove old backup, if present */
3960 mch_remove(backup);
3961 /* Open with O_EXCL to avoid the file being created while
3962 * we were sleeping (symlink hacker attack?) */
3963 bfd = mch_open((char *)backup,
Bram Moolenaara5792f52005-11-23 21:25:05 +00003964 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW,
3965 perm & 0777);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 if (bfd < 0)
3967 {
3968 vim_free(backup);
3969 backup = NULL;
3970 }
3971 else
3972 {
3973 /* set file protection same as original file, but
3974 * strip s-bit */
3975 (void)mch_setperm(backup, perm & 0777);
3976
3977#ifdef UNIX
3978 /*
3979 * Try to set the group of the backup same as the
3980 * original file. If this fails, set the protection
3981 * bits for the group same as the protection bits for
3982 * others.
3983 */
Bram Moolenaara5792f52005-11-23 21:25:05 +00003984 if (st_new.st_gid != st_old.st_gid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985# ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */
Bram Moolenaara5792f52005-11-23 21:25:05 +00003986 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987# endif
3988 )
3989 mch_setperm(backup,
3990 (perm & 0707) | ((perm & 07) << 3));
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00003991# ifdef HAVE_SELINUX
3992 mch_copy_sec(fname, backup);
3993# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994#endif
3995
3996 /*
3997 * copy the file.
3998 */
3999 write_info.bw_fd = bfd;
4000 write_info.bw_buf = copybuf;
4001#ifdef HAS_BW_FLAGS
4002 write_info.bw_flags = FIO_NOCONVERT;
4003#endif
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004004 while ((write_info.bw_len = read_eintr(fd, copybuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 BUFSIZE)) > 0)
4006 {
4007 if (buf_write_bytes(&write_info) == FAIL)
4008 {
4009 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)");
4010 break;
4011 }
4012 ui_breakcheck();
4013 if (got_int)
4014 {
4015 errmsg = (char_u *)_(e_interr);
4016 break;
4017 }
4018 }
4019
4020 if (close(bfd) < 0 && errmsg == NULL)
4021 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)");
4022 if (write_info.bw_len < 0)
4023 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)");
4024#ifdef UNIX
4025 set_file_time(backup, st_old.st_atime, st_old.st_mtime);
4026#endif
4027#ifdef HAVE_ACL
4028 mch_set_acl(backup, acl);
4029#endif
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00004030#ifdef HAVE_SELINUX
4031 mch_copy_sec(fname, backup);
4032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 break;
4034 }
4035 }
4036 }
4037 nobackup:
4038 close(fd); /* ignore errors for closing read file */
4039 vim_free(copybuf);
4040
4041 if (backup == NULL && errmsg == NULL)
4042 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)");
4043 /* ignore errors when forceit is TRUE */
4044 if ((some_error || errmsg != NULL) && !forceit)
4045 {
4046 retval = FAIL;
4047 goto fail;
4048 }
4049 errmsg = NULL;
4050 }
4051 else
4052 {
4053 char_u *dirp;
4054 char_u *p;
4055 char_u *rootname;
4056
4057 /*
4058 * Make a backup by renaming the original file.
4059 */
4060 /*
4061 * If 'cpoptions' includes the "W" flag, we don't want to
4062 * overwrite a read-only file. But rename may be possible
4063 * anyway, thus we need an extra check here.
4064 */
4065 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL)
4066 {
4067 errnum = (char_u *)"E504: ";
4068 errmsg = (char_u *)_(err_readonly);
4069 goto fail;
4070 }
4071
4072 /*
4073 *
4074 * Form the backup file name - change path/fo.o.h to
4075 * path/fo.o.h.bak Try all directories in 'backupdir', first one
4076 * that works is used.
4077 */
4078 dirp = p_bdir;
4079 while (*dirp)
4080 {
4081 /*
4082 * Isolate one directory name and make the backup file name.
4083 */
4084 (void)copy_option_part(&dirp, IObuff, IOSIZE, ",");
4085 rootname = get_file_in_dir(fname, IObuff);
4086 if (rootname == NULL)
4087 backup = NULL;
4088 else
4089 {
4090 backup = buf_modname(
4091#ifdef SHORT_FNAME
4092 TRUE,
4093#else
4094 (buf->b_p_sn || buf->b_shortname),
4095#endif
4096 rootname, backup_ext, FALSE);
4097 vim_free(rootname);
4098 }
4099
4100 if (backup != NULL)
4101 {
4102 /*
4103 * If we are not going to keep the backup file, don't
4104 * delete an existing one, try to use another name.
4105 * Change one character, just before the extension.
4106 */
4107 if (!p_bk && mch_getperm(backup) >= 0)
4108 {
4109 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext);
4110 if (p < backup) /* empty file name ??? */
4111 p = backup;
4112 *p = 'z';
4113 while (*p > 'a' && mch_getperm(backup) >= 0)
4114 --*p;
4115 /* They all exist??? Must be something wrong! */
4116 if (*p == 'a')
4117 {
4118 vim_free(backup);
4119 backup = NULL;
4120 }
4121 }
4122 }
4123 if (backup != NULL)
4124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 /*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004126 * Delete any existing backup and move the current version
4127 * to the backup. For safety, we don't remove the backup
4128 * until the write has finished successfully. And if the
4129 * 'backup' option is set, leave it around.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 */
4131 /*
4132 * If the renaming of the original file to the backup file
4133 * works, quit here.
4134 */
4135 if (vim_rename(fname, backup) == 0)
4136 break;
4137
4138 vim_free(backup); /* don't do the rename below */
4139 backup = NULL;
4140 }
4141 }
4142 if (backup == NULL && !forceit)
4143 {
4144 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)");
4145 goto fail;
4146 }
4147 }
4148 }
4149
4150#if defined(UNIX) && !defined(ARCHIE)
4151 /* When using ":w!" and the file was read-only: make it writable */
4152 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid()
4153 && vim_strchr(p_cpo, CPO_FWRITE) == NULL)
4154 {
4155 perm |= 0200;
4156 (void)mch_setperm(fname, perm);
4157 made_writable = TRUE;
4158 }
4159#endif
4160
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004161 /* When using ":w!" and writing to the current file, 'readonly' makes no
Bram Moolenaar4399ef42005-02-12 14:29:27 +00004162 * sense, reset it, unless 'Z' appears in 'cpoptions'. */
4163 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
4165 buf->b_p_ro = FALSE;
4166#ifdef FEAT_TITLE
4167 need_maketitle = TRUE; /* set window title later */
4168#endif
4169#ifdef FEAT_WINDOWS
4170 status_redraw_all(); /* redraw status lines later */
4171#endif
4172 }
4173
4174 if (end > buf->b_ml.ml_line_count)
4175 end = buf->b_ml.ml_line_count;
4176 if (buf->b_ml.ml_flags & ML_EMPTY)
4177 start = end + 1;
4178
4179 /*
4180 * If the original file is being overwritten, there is a small chance that
4181 * we crash in the middle of writing. Therefore the file is preserved now.
4182 * This makes all block numbers positive so that recovery does not need
4183 * the original file.
4184 * Don't do this if there is a backup file and we are exiting.
4185 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004186 if (reset_changed && !newfile && overwriting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 && !(exiting && backup != NULL))
4188 {
4189 ml_preserve(buf, FALSE);
4190 if (got_int)
4191 {
4192 errmsg = (char_u *)_(e_interr);
4193 goto restore_backup;
4194 }
4195 }
4196
4197#ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */
4198 /*
4199 * Before risking to lose the original file verify if there's
4200 * a resource fork to preserve, and if cannot be done warn
4201 * the users. This happens when overwriting without backups.
4202 */
4203 if (backup == NULL && overwriting && !append)
4204 if (mch_has_resource_fork(fname))
4205 {
4206 errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)");
4207 goto restore_backup;
4208 }
4209#endif
4210
4211#ifdef VMS
4212 vms_remove_version(fname); /* remove version */
4213#endif
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00004214 /* Default: write the file directly. May write to a temp file for
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 * multi-byte conversion. */
4216 wfname = fname;
4217
4218#ifdef FEAT_MBYTE
4219 /* Check for forced 'fileencoding' from "++opt=val" argument. */
4220 if (eap != NULL && eap->force_enc != 0)
4221 {
4222 fenc = eap->cmd + eap->force_enc;
4223 fenc = enc_canonize(fenc);
4224 fenc_tofree = fenc;
4225 }
4226 else
4227 fenc = buf->b_p_fenc;
4228
4229 /*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00004230 * Check if the file needs to be converted.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 */
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00004232 converted = need_conversion(fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or
4236 * Latin1 to Unicode conversion. This is handled in buf_write_bytes().
4237 * Prepare the flags for it and allocate bw_conv_buf when needed.
4238 */
4239 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0))
4240 {
4241 wb_flags = get_fio_flags(fenc);
4242 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8))
4243 {
4244 /* Need to allocate a buffer to translate into. */
4245 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8))
4246 write_info.bw_conv_buflen = bufsize * 2;
4247 else /* FIO_UCS4 */
4248 write_info.bw_conv_buflen = bufsize * 4;
4249 write_info.bw_conv_buf
4250 = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4251 if (write_info.bw_conv_buf == NULL)
4252 end = 0;
4253 }
4254 }
4255
4256# ifdef WIN3264
4257 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0)
4258 {
4259 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */
4260 write_info.bw_conv_buflen = bufsize * 4;
4261 write_info.bw_conv_buf
4262 = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4263 if (write_info.bw_conv_buf == NULL)
4264 end = 0;
4265 }
4266# endif
4267
4268# ifdef MACOS_X
4269 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0)
4270 {
4271 write_info.bw_conv_buflen = bufsize * 3;
4272 write_info.bw_conv_buf
4273 = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4274 if (write_info.bw_conv_buf == NULL)
4275 end = 0;
4276 }
4277# endif
4278
4279# if defined(FEAT_EVAL) || defined(USE_ICONV)
4280 if (converted && wb_flags == 0)
4281 {
4282# ifdef USE_ICONV
4283 /*
4284 * Use iconv() conversion when conversion is needed and it's not done
4285 * internally.
4286 */
4287 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc,
4288 enc_utf8 ? (char_u *)"utf-8" : p_enc);
4289 if (write_info.bw_iconv_fd != (iconv_t)-1)
4290 {
4291 /* We're going to use iconv(), allocate a buffer to convert in. */
4292 write_info.bw_conv_buflen = bufsize * ICONV_MULT;
4293 write_info.bw_conv_buf
4294 = lalloc((long_u)write_info.bw_conv_buflen, TRUE);
4295 if (write_info.bw_conv_buf == NULL)
4296 end = 0;
4297 write_info.bw_first = TRUE;
4298 }
4299# ifdef FEAT_EVAL
4300 else
4301# endif
4302# endif
4303
4304# ifdef FEAT_EVAL
4305 /*
4306 * When the file needs to be converted with 'charconvert' after
4307 * writing, write to a temp file instead and let the conversion
4308 * overwrite the original file.
4309 */
4310 if (*p_ccv != NUL)
4311 {
4312 wfname = vim_tempname('w');
4313 if (wfname == NULL) /* Can't write without a tempfile! */
4314 {
4315 errmsg = (char_u *)_("E214: Can't find temp file for writing");
4316 goto restore_backup;
4317 }
4318 }
4319# endif
4320 }
4321# endif
4322 if (converted && wb_flags == 0
4323# ifdef USE_ICONV
4324 && write_info.bw_iconv_fd == (iconv_t)-1
4325# endif
4326# ifdef FEAT_EVAL
4327 && wfname == fname
4328# endif
4329 )
4330 {
4331 if (!forceit)
4332 {
4333 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)");
4334 goto restore_backup;
4335 }
4336 notconverted = TRUE;
4337 }
4338#endif
4339
4340 /*
4341 * Open the file "wfname" for writing.
4342 * We may try to open the file twice: If we can't write to the
4343 * file and forceit is TRUE we delete the existing file and try to create
4344 * a new one. If this still fails we may have lost the original file!
4345 * (this may happen when the user reached his quotum for number of files).
4346 * Appending will fail if the file does not exist and forceit is FALSE.
4347 */
4348 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append
4349 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND)
4350 : (O_CREAT | O_TRUNC))
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00004351 , perm < 0 ? 0666 : (perm & 0777))) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
4353 /*
4354 * A forced write will try to create a new file if the old one is
4355 * still readonly. This may also happen when the directory is
4356 * read-only. In that case the mch_remove() will fail.
4357 */
4358 if (errmsg == NULL)
4359 {
4360#ifdef UNIX
4361 struct stat st;
4362
4363 /* Don't delete the file when it's a hard or symbolic link. */
4364 if ((!newfile && st_old.st_nlink > 1)
4365 || (mch_lstat((char *)fname, &st) == 0
4366 && (st.st_dev != st_old.st_dev
4367 || st.st_ino != st_old.st_ino)))
4368 errmsg = (char_u *)_("E166: Can't open linked file for writing");
4369 else
4370#endif
4371 {
4372 errmsg = (char_u *)_("E212: Can't open file for writing");
4373 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL
4374 && perm >= 0)
4375 {
4376#ifdef UNIX
4377 /* we write to the file, thus it should be marked
4378 writable after all */
4379 if (!(perm & 0200))
4380 made_writable = TRUE;
4381 perm |= 0200;
4382 if (st_old.st_uid != getuid() || st_old.st_gid != getgid())
4383 perm &= 0777;
4384#endif
4385 if (!append) /* don't remove when appending */
4386 mch_remove(wfname);
4387 continue;
4388 }
4389 }
4390 }
4391
4392restore_backup:
4393 {
4394 struct stat st;
4395
4396 /*
4397 * If we failed to open the file, we don't need a backup. Throw it
4398 * away. If we moved or removed the original file try to put the
4399 * backup in its place.
4400 */
4401 if (backup != NULL && wfname == fname)
4402 {
4403 if (backup_copy)
4404 {
4405 /*
4406 * There is a small chance that we removed the original,
4407 * try to move the copy in its place.
4408 * This may not work if the vim_rename() fails.
4409 * In that case we leave the copy around.
4410 */
4411 /* If file does not exist, put the copy in its place */
4412 if (mch_stat((char *)fname, &st) < 0)
4413 vim_rename(backup, fname);
4414 /* if original file does exist throw away the copy */
4415 if (mch_stat((char *)fname, &st) >= 0)
4416 mch_remove(backup);
4417 }
4418 else
4419 {
4420 /* try to put the original file back */
4421 vim_rename(backup, fname);
4422 }
4423 }
4424
4425 /* if original file no longer exists give an extra warning */
4426 if (!newfile && mch_stat((char *)fname, &st) < 0)
4427 end = 0;
4428 }
4429
4430#ifdef FEAT_MBYTE
4431 if (wfname != fname)
4432 vim_free(wfname);
4433#endif
4434 goto fail;
4435 }
4436 errmsg = NULL;
4437
4438#if defined(MACOS_CLASSIC) || defined(WIN3264)
4439 /* TODO: Is it need for MACOS_X? (Dany) */
4440 /*
4441 * On macintosh copy the original files attributes (i.e. the backup)
Bram Moolenaar7263a772007-05-10 17:35:54 +00004442 * This is done in order to preserve the resource fork and the
4443 * Finder attribute (label, comments, custom icons, file creator)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 */
4445 if (backup != NULL && overwriting && !append)
4446 {
4447 if (backup_copy)
4448 (void)mch_copy_file_attribute(wfname, backup);
4449 else
4450 (void)mch_copy_file_attribute(backup, wfname);
4451 }
4452
4453 if (!overwriting && !append)
4454 {
4455 if (buf->b_ffname != NULL)
4456 (void)mch_copy_file_attribute(buf->b_ffname, wfname);
Bram Moolenaar7263a772007-05-10 17:35:54 +00004457 /* Should copy resource fork */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459#endif
4460
4461 write_info.bw_fd = fd;
4462
4463#ifdef FEAT_CRYPT
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02004464 if (*buf->b_p_key != NUL && !filtering)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02004466 char_u *header;
4467 int header_len;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02004468
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02004469 header = prepare_crypt_write(buf, &header_len);
4470 if (header == NULL)
4471 end = 0;
Bram Moolenaar40e6a712010-05-16 22:32:54 +02004472 else
4473 {
Bram Moolenaara3ff49f2010-05-30 22:48:02 +02004474 /* Write magic number, so that Vim knows that this file is
4475 * encrypted when reading it again. This also undergoes utf-8 to
4476 * ucs-2/4 conversion when needed. */
4477 write_info.bw_buf = header;
4478 write_info.bw_len = header_len;
4479 write_info.bw_flags = FIO_NOCONVERT;
4480 if (buf_write_bytes(&write_info) == FAIL)
4481 end = 0;
4482 wb_flags |= FIO_ENCRYPTED;
4483 vim_free(header);
Bram Moolenaar40e6a712010-05-16 22:32:54 +02004484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486#endif
4487
4488 write_info.bw_buf = buffer;
4489 nchars = 0;
4490
4491 /* use "++bin", "++nobin" or 'binary' */
4492 if (eap != NULL && eap->force_bin != 0)
4493 write_bin = (eap->force_bin == FORCE_BIN);
4494 else
4495 write_bin = buf->b_p_bin;
4496
4497#ifdef FEAT_MBYTE
4498 /*
4499 * The BOM is written just after the encryption magic number.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004500 * Skip it when appending and the file already existed, the BOM only makes
4501 * sense at the start of the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004503 if (buf->b_p_bomb && !write_bin && (!append || perm < 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
4505 write_info.bw_len = make_bom(buffer, fenc);
4506 if (write_info.bw_len > 0)
4507 {
4508 /* don't convert, do encryption */
4509 write_info.bw_flags = FIO_NOCONVERT | wb_flags;
4510 if (buf_write_bytes(&write_info) == FAIL)
4511 end = 0;
4512 else
4513 nchars += write_info.bw_len;
4514 }
4515 }
Bram Moolenaar32b485f2009-07-29 16:06:27 +00004516 write_info.bw_start_lnum = start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517#endif
4518
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004519#ifdef FEAT_PERSISTENT_UNDO
4520 write_undo_file = (buf->b_p_udf && overwriting && !append
4521 && !filtering && reset_changed);
4522 if (write_undo_file)
4523 /* Prepare for computing the hash value of the text. */
4524 sha256_start(&sha_ctx);
4525#endif
4526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 write_info.bw_len = bufsize;
4528#ifdef HAS_BW_FLAGS
4529 write_info.bw_flags = wb_flags;
4530#endif
4531 fileformat = get_fileformat_force(buf, eap);
4532 s = buffer;
4533 len = 0;
4534 for (lnum = start; lnum <= end; ++lnum)
4535 {
4536 /*
4537 * The next while loop is done once for each character written.
4538 * Keep it fast!
4539 */
4540 ptr = ml_get_buf(buf, lnum, FALSE) - 1;
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004541#ifdef FEAT_PERSISTENT_UNDO
4542 if (write_undo_file)
Bram Moolenaar442b4222010-05-24 21:34:22 +02004543 sha256_update(&sha_ctx, ptr + 1, (UINT32_T)(STRLEN(ptr + 1) + 1));
Bram Moolenaar55debbe2010-05-23 23:34:36 +02004544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 while ((c = *++ptr) != NUL)
4546 {
4547 if (c == NL)
4548 *s = NUL; /* replace newlines with NULs */
4549 else if (c == CAR && fileformat == EOL_MAC)
4550 *s = NL; /* Mac: replace CRs with NLs */
4551 else
4552 *s = c;
4553 ++s;
4554 if (++len != bufsize)
4555 continue;
4556 if (buf_write_bytes(&write_info) == FAIL)
4557 {
4558 end = 0; /* write error: break loop */
4559 break;
4560 }
4561 nchars += bufsize;
4562 s = buffer;
4563 len = 0;
Bram Moolenaar32b485f2009-07-29 16:06:27 +00004564#ifdef FEAT_MBYTE
4565 write_info.bw_start_lnum = lnum;
4566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 }
4568 /* write failed or last line has no EOL: stop here */
4569 if (end == 0
4570 || (lnum == end
4571 && write_bin
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01004572 && (lnum == buf->b_no_eol_lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol))))
4574 {
4575 ++lnum; /* written the line, count it */
4576 no_eol = TRUE;
4577 break;
4578 }
4579 if (fileformat == EOL_UNIX)
4580 *s++ = NL;
4581 else
4582 {
4583 *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */
4584 if (fileformat == EOL_DOS) /* write CR-NL */
4585 {
4586 if (++len == bufsize)
4587 {
4588 if (buf_write_bytes(&write_info) == FAIL)
4589 {
4590 end = 0; /* write error: break loop */
4591 break;
4592 }
4593 nchars += bufsize;
4594 s = buffer;
4595 len = 0;
4596 }
4597 *s++ = NL;
4598 }
4599 }
4600 if (++len == bufsize && end)
4601 {
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
4611 ui_breakcheck();
4612 if (got_int)
4613 {
4614 end = 0; /* Interrupted, break loop */
4615 break;
4616 }
4617 }
4618#ifdef VMS
4619 /*
4620 * On VMS there is a problem: newlines get added when writing blocks
4621 * at a time. Fix it by writing a line at a time.
4622 * This is much slower!
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004623 * Explanation: VAX/DECC RTL insists that records in some RMS
4624 * structures end with a newline (carriage return) character, and if
4625 * they don't it adds one.
4626 * With other RMS structures it works perfect without this fix.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 */
Bram Moolenaarb52e2602007-10-29 21:38:54 +00004628 if (buf->b_fab_rfm == FAB$C_VFC
4629 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004631 int b2write;
4632
4633 buf->b_fab_mrs = (buf->b_fab_mrs == 0
4634 ? MIN(4096, bufsize)
4635 : MIN(buf->b_fab_mrs, bufsize));
4636
4637 b2write = len;
4638 while (b2write > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {
Bram Moolenaard4755bb2004-09-02 19:12:26 +00004640 write_info.bw_len = MIN(b2write, buf->b_fab_mrs);
4641 if (buf_write_bytes(&write_info) == FAIL)
4642 {
4643 end = 0;
4644 break;
4645 }
4646 b2write -= MIN(b2write, buf->b_fab_mrs);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 }
4648 write_info.bw_len = bufsize;
4649 nchars += len;
4650 s = buffer;
4651 len = 0;
4652 }
4653#endif
4654 }
4655 if (len > 0 && end > 0)
4656 {
4657 write_info.bw_len = len;
4658 if (buf_write_bytes(&write_info) == FAIL)
4659 end = 0; /* write error */
4660 nchars += len;
4661 }
4662
4663#if defined(UNIX) && defined(HAVE_FSYNC)
4664 /* On many journalling file systems there is a bug that causes both the
4665 * original and the backup file to be lost when halting the system right
4666 * after writing the file. That's because only the meta-data is
4667 * journalled. Syncing the file slows down the system, but assures it has
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00004668 * been written to disk and we don't lose it.
4669 * For a device do try the fsync() but don't complain if it does not work
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004670 * (could be a pipe).
4671 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */
4672 if (p_fs && fsync(fd) != 0 && !device)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 {
4674 errmsg = (char_u *)_("E667: Fsync failed");
4675 end = 0;
4676 }
4677#endif
4678
Bram Moolenaar588ebeb2008-05-07 17:09:24 +00004679#ifdef HAVE_SELINUX
4680 /* Probably need to set the security context. */
4681 if (!backup_copy)
4682 mch_copy_sec(backup, wfname);
4683#endif
4684
Bram Moolenaara5792f52005-11-23 21:25:05 +00004685#ifdef UNIX
4686 /* When creating a new file, set its owner/group to that of the original
4687 * file. Get the new device and inode number. */
4688 if (backup != NULL && !backup_copy)
4689 {
4690# ifdef HAVE_FCHOWN
4691 struct stat st;
4692
4693 /* don't change the owner when it's already OK, some systems remove
4694 * permission or ACL stuff */
4695 if (mch_stat((char *)wfname, &st) < 0
4696 || st.st_uid != st_old.st_uid
4697 || st.st_gid != st_old.st_gid)
4698 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00004699 ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004700 if (perm >= 0) /* set permission again, may have changed */
4701 (void)mch_setperm(wfname, perm);
4702 }
4703# endif
4704 buf_setino(buf);
4705 }
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00004706 else if (!buf->b_dev_valid)
Bram Moolenaar8fa04452005-12-23 22:13:51 +00004707 /* Set the inode when creating a new file. */
4708 buf_setino(buf);
Bram Moolenaara5792f52005-11-23 21:25:05 +00004709#endif
4710
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 if (close(fd) != 0)
4712 {
4713 errmsg = (char_u *)_("E512: Close failed");
4714 end = 0;
4715 }
4716
4717#ifdef UNIX
4718 if (made_writable)
4719 perm &= ~0200; /* reset 'w' bit for security reasons */
4720#endif
4721 if (perm >= 0) /* set perm. of new file same as old file */
4722 (void)mch_setperm(wfname, perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723#ifdef HAVE_ACL
4724 /* Probably need to set the ACL before changing the user (can't set the
4725 * ACL on a file the user doesn't own). */
4726 if (!backup_copy)
4727 mch_set_acl(wfname, acl);
4728#endif
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02004729#ifdef FEAT_CRYPT
Bram Moolenaar4cf35c22011-02-25 16:52:17 +01004730 crypt_method_used = use_crypt_method;
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +02004731 if (wb_flags & FIO_ENCRYPTED)
4732 crypt_pop_state();
4733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
4736#if defined(FEAT_MBYTE) && defined(FEAT_EVAL)
4737 if (wfname != fname)
4738 {
4739 /*
4740 * The file was written to a temp file, now it needs to be converted
4741 * with 'charconvert' to (overwrite) the output file.
4742 */
4743 if (end != 0)
4744 {
4745 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc,
4746 wfname, fname) == FAIL)
4747 {
4748 write_info.bw_conv_error = TRUE;
4749 end = 0;
4750 }
4751 }
4752 mch_remove(wfname);
4753 vim_free(wfname);
4754 }
4755#endif
4756
4757 if (end == 0)
4758 {
4759 if (errmsg == NULL)
4760 {
4761#ifdef FEAT_MBYTE
4762 if (write_info.bw_conv_error)
Bram Moolenaar32b485f2009-07-29 16:06:27 +00004763 {
4764 if (write_info.bw_conv_error_lnum == 0)
4765 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)");
4766 else
4767 {
4768 errmsg_allocated = TRUE;
4769 errmsg = alloc(300);
4770 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"),
4771 (long)write_info.bw_conv_error_lnum);
4772 }
4773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 else
4775#endif
4776 if (got_int)
4777 errmsg = (char_u *)_(e_interr);
4778 else
4779 errmsg = (char_u *)_("E514: write error (file system full?)");
4780 }
4781
4782 /*
4783 * If we have a backup file, try to put it in place of the new file,
Bram Moolenaare37d50a2008-08-06 17:06:04 +00004784 * because the new file is probably corrupt. This avoids losing the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 * original file when trying to make a backup when writing the file a
4786 * second time.
4787 * When "backup_copy" is set we need to copy the backup over the new
4788 * file. Otherwise rename the backup file.
4789 * If this is OK, don't give the extra warning message.
4790 */
4791 if (backup != NULL)
4792 {
4793 if (backup_copy)
4794 {
4795 /* This may take a while, if we were interrupted let the user
4796 * know we got the message. */
4797 if (got_int)
4798 {
4799 MSG(_(e_interr));
4800 out_flush();
4801 }
4802 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0)
4803 {
4804 if ((write_info.bw_fd = mch_open((char *)fname,
Bram Moolenaar9be038d2005-03-08 22:34:32 +00004805 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA,
4806 perm & 0777)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 {
4808 /* copy the file. */
4809 write_info.bw_buf = smallbuf;
4810#ifdef HAS_BW_FLAGS
4811 write_info.bw_flags = FIO_NOCONVERT;
4812#endif
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01004813 while ((write_info.bw_len = read_eintr(fd, smallbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 SMBUFSIZE)) > 0)
4815 if (buf_write_bytes(&write_info) == FAIL)
4816 break;
4817
4818 if (close(write_info.bw_fd) >= 0
4819 && write_info.bw_len == 0)
4820 end = 1; /* success */
4821 }
4822 close(fd); /* ignore errors for closing read file */
4823 }
4824 }
4825 else
4826 {
4827 if (vim_rename(backup, fname) == 0)
4828 end = 1;
4829 }
4830 }
4831 goto fail;
4832 }
4833
4834 lnum -= start; /* compute number of written lines */
4835 --no_wait_return; /* may wait for return now */
4836
4837#if !(defined(UNIX) || defined(VMS))
4838 fname = sfname; /* use shortname now, for the messages */
4839#endif
4840 if (!filtering)
4841 {
4842 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */
4843 c = FALSE;
4844#ifdef FEAT_MBYTE
4845 if (write_info.bw_conv_error)
4846 {
4847 STRCAT(IObuff, _(" CONVERSION ERROR"));
4848 c = TRUE;
Bram Moolenaar32b485f2009-07-29 16:06:27 +00004849 if (write_info.bw_conv_error_lnum != 0)
Bram Moolenaara800b422010-06-27 01:15:55 +02004850 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"),
Bram Moolenaar32b485f2009-07-29 16:06:27 +00004851 (long)write_info.bw_conv_error_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
4853 else if (notconverted)
4854 {
4855 STRCAT(IObuff, _("[NOT converted]"));
4856 c = TRUE;
4857 }
4858 else if (converted)
4859 {
4860 STRCAT(IObuff, _("[converted]"));
4861 c = TRUE;
4862 }
4863#endif
4864 if (device)
4865 {
4866 STRCAT(IObuff, _("[Device]"));
4867 c = TRUE;
4868 }
4869 else if (newfile)
4870 {
4871 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]"));
4872 c = TRUE;
4873 }
4874 if (no_eol)
4875 {
4876 msg_add_eol();
4877 c = TRUE;
4878 }
4879 /* may add [unix/dos/mac] */
4880 if (msg_add_fileformat(fileformat))
4881 c = TRUE;
4882#ifdef FEAT_CRYPT
4883 if (wb_flags & FIO_ENCRYPTED)
4884 {
Bram Moolenaar4cf35c22011-02-25 16:52:17 +01004885 if (crypt_method_used == 1)
4886 STRCAT(IObuff, _("[blowfish]"));
4887 else
4888 STRCAT(IObuff, _("[crypted]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 c = TRUE;
4890 }
4891#endif
4892 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */
4893 if (!shortmess(SHM_WRITE))
4894 {
4895 if (append)
4896 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended"));
4897 else
4898 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written"));
4899 }
4900
Bram Moolenaar8f7fd652006-02-21 22:04:51 +00004901 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004904 /* When written everything correctly: reset 'modified'. Unless not
4905 * writing to the original file and '+' is not in 'cpoptions'. */
Bram Moolenaar292ad192005-12-11 21:29:51 +00004906 if (reset_changed && whole && !append
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907#ifdef FEAT_MBYTE
4908 && !write_info.bw_conv_error
4909#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004910 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)
4911 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
4913 unchanged(buf, TRUE);
4914 u_unchanged(buf);
Bram Moolenaar730cde92010-06-27 05:18:54 +02004915 u_update_save_nr(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917
4918 /*
4919 * If written to the current file, update the timestamp of the swap file
4920 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime.
4921 */
4922 if (overwriting)
4923 {
4924 ml_timestamp(buf);
Bram Moolenaar292ad192005-12-11 21:29:51 +00004925 if (append)
4926 buf->b_flags &= ~BF_NEW;
4927 else
4928 buf->b_flags &= ~BF_WRITE_MASK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930
4931 /*
4932 * If we kept a backup until now, and we are in patch mode, then we make
4933 * the backup file our 'original' file.
4934 */
4935 if (*p_pm && dobackup)
4936 {
4937 char *org = (char *)buf_modname(
4938#ifdef SHORT_FNAME
4939 TRUE,
4940#else
4941 (buf->b_p_sn || buf->b_shortname),
4942#endif
4943 fname, p_pm, FALSE);
4944
4945 if (backup != NULL)
4946 {
4947 struct stat st;
4948
4949 /*
4950 * If the original file does not exist yet
4951 * the current backup file becomes the original file
4952 */
4953 if (org == NULL)
4954 EMSG(_("E205: Patchmode: can't save original file"));
4955 else if (mch_stat(org, &st) < 0)
4956 {
4957 vim_rename(backup, (char_u *)org);
4958 vim_free(backup); /* don't delete the file */
4959 backup = NULL;
4960#ifdef UNIX
4961 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime);
4962#endif
4963 }
4964 }
4965 /*
4966 * If there is no backup file, remember that a (new) file was
4967 * created.
4968 */
4969 else
4970 {
4971 int empty_fd;
4972
4973 if (org == NULL
Bram Moolenaara5792f52005-11-23 21:25:05 +00004974 || (empty_fd = mch_open(org,
4975 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW,
Bram Moolenaar4317d9b2005-03-18 20:25:31 +00004976 perm < 0 ? 0666 : (perm & 0777))) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 EMSG(_("E206: patchmode: can't touch empty original file"));
4978 else
4979 close(empty_fd);
4980 }
4981 if (org != NULL)
4982 {
4983 mch_setperm((char_u *)org, mch_getperm(fname) & 0777);
4984 vim_free(org);
4985 }
4986 }
4987
4988 /*
4989 * Remove the backup unless 'backup' option is set
4990 */
4991 if (!p_bk && backup != NULL && mch_remove(backup) != 0)
4992 EMSG(_("E207: Can't delete backup file"));
4993
4994#ifdef FEAT_SUN_WORKSHOP
4995 if (usingSunWorkShop)
4996 workshop_file_saved((char *) ffname);
4997#endif
4998
4999 goto nofail;
5000
5001 /*
5002 * Finish up. We get here either after failure or success.
5003 */
5004fail:
5005 --no_wait_return; /* may wait for return now */
5006nofail:
5007
5008 /* Done saving, we accept changed buffer warnings again */
5009 buf->b_saving = FALSE;
5010
5011 vim_free(backup);
5012 if (buffer != smallbuf)
5013 vim_free(buffer);
5014#ifdef FEAT_MBYTE
5015 vim_free(fenc_tofree);
5016 vim_free(write_info.bw_conv_buf);
5017# ifdef USE_ICONV
5018 if (write_info.bw_iconv_fd != (iconv_t)-1)
5019 {
5020 iconv_close(write_info.bw_iconv_fd);
5021 write_info.bw_iconv_fd = (iconv_t)-1;
5022 }
5023# endif
5024#endif
5025#ifdef HAVE_ACL
5026 mch_free_acl(acl);
5027#endif
5028
5029 if (errmsg != NULL)
5030 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005031 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033 attr = hl_attr(HLF_E); /* set highlight for error messages */
5034 msg_add_fname(buf,
5035#ifndef UNIX
5036 sfname
5037#else
5038 fname
5039#endif
5040 ); /* put file name in IObuff with quotes */
5041 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE)
5042 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL;
5043 /* If the error message has the form "is ...", put the error number in
5044 * front of the file name. */
5045 if (errnum != NULL)
5046 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00005047 STRMOVE(IObuff + numlen, IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 mch_memmove(IObuff, errnum, (size_t)numlen);
5049 }
5050 STRCAT(IObuff, errmsg);
5051 emsg(IObuff);
Bram Moolenaar32b485f2009-07-29 16:06:27 +00005052 if (errmsg_allocated)
5053 vim_free(errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 retval = FAIL;
5056 if (end == 0)
5057 {
5058 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"),
5059 attr | MSG_HIST);
5060 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"),
5061 attr | MSG_HIST);
5062
5063 /* Update the timestamp to avoid an "overwrite changed file"
5064 * prompt when writing again. */
5065 if (mch_stat((char *)fname, &st_old) >= 0)
5066 {
5067 buf_store_time(buf, &st_old, fname);
5068 buf->b_mtime_read = buf->b_mtime;
5069 }
5070 }
5071 }
5072 msg_scroll = msg_save;
5073
Bram Moolenaar55debbe2010-05-23 23:34:36 +02005074#ifdef FEAT_PERSISTENT_UNDO
5075 /*
5076 * When writing the whole file and 'undofile' is set, also write the undo
5077 * file.
5078 */
5079 if (retval == OK && write_undo_file)
5080 {
5081 char_u hash[UNDO_HASH_SIZE];
5082
5083 sha256_finish(&sha_ctx, hash);
5084 u_write_undo(NULL, FALSE, buf, hash);
5085 }
5086#endif
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088#ifdef FEAT_AUTOCMD
5089#ifdef FEAT_EVAL
5090 if (!should_abort(retval))
5091#else
5092 if (!got_int)
5093#endif
5094 {
5095 aco_save_T aco;
5096
Bram Moolenaar68a33fc2012-04-25 16:50:48 +02005097 curbuf->b_no_eol_lnum = 0; /* in case it was set by the previous read */
5098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 /*
5100 * Apply POST autocommands.
5101 * Careful: The autocommands may call buf_write() recursively!
5102 */
5103 aucmd_prepbuf(&aco, buf);
5104
5105 if (append)
5106 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname,
5107 FALSE, curbuf, eap);
5108 else if (filtering)
5109 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname,
5110 FALSE, curbuf, eap);
5111 else if (reset_changed && whole)
5112 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname,
5113 FALSE, curbuf, eap);
5114 else
5115 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname,
5116 FALSE, curbuf, eap);
5117
5118 /* restore curwin/curbuf and a few other things */
5119 aucmd_restbuf(&aco);
5120
5121#ifdef FEAT_EVAL
5122 if (aborting()) /* autocmds may abort script processing */
5123 retval = FALSE;
5124#endif
5125 }
5126#endif
5127
5128 got_int |= prev_got_int;
5129
5130#ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */
5131 /* Update machine specific information. */
5132 mch_post_buffer_write(buf);
5133#endif
5134 return retval;
5135}
5136
5137/*
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005138 * Set the name of the current buffer. Use when the buffer doesn't have a
5139 * name and a ":r" or ":w" command with a file name is used.
5140 */
5141 static int
5142set_rw_fname(fname, sfname)
5143 char_u *fname;
5144 char_u *sfname;
5145{
5146#ifdef FEAT_AUTOCMD
Bram Moolenaar8b38e242009-06-16 13:35:20 +00005147 buf_T *buf = curbuf;
5148
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005149 /* It's like the unnamed buffer is deleted.... */
5150 if (curbuf->b_p_bl)
5151 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5152 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
5153# ifdef FEAT_EVAL
5154 if (aborting()) /* autocmds may abort script processing */
5155 return FAIL;
5156# endif
Bram Moolenaar8b38e242009-06-16 13:35:20 +00005157 if (curbuf != buf)
5158 {
5159 /* We are in another buffer now, don't do the renaming. */
5160 EMSG(_(e_auchangedbuf));
5161 return FAIL;
5162 }
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005163#endif
5164
5165 if (setfname(curbuf, fname, sfname, FALSE) == OK)
5166 curbuf->b_flags |= BF_NOTEDITED;
5167
5168#ifdef FEAT_AUTOCMD
5169 /* ....and a new named one is created */
5170 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
5171 if (curbuf->b_p_bl)
5172 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5173# ifdef FEAT_EVAL
5174 if (aborting()) /* autocmds may abort script processing */
5175 return FAIL;
5176# endif
5177
5178 /* Do filetype detection now if 'filetype' is empty. */
5179 if (*curbuf->b_p_ft == NUL)
5180 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005181 if (au_has_group((char_u *)"filetypedetect"))
Bram Moolenaar70836c82006-02-20 21:28:49 +00005182 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005183 do_modelines(0);
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005184 }
5185#endif
5186
5187 return OK;
5188}
5189
5190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 * Put file name into IObuff with quotes.
5192 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00005193 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194msg_add_fname(buf, fname)
5195 buf_T *buf;
5196 char_u *fname;
5197{
5198 if (fname == NULL)
5199 fname = (char_u *)"-stdin-";
5200 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
5201 IObuff[0] = '"';
5202 STRCAT(IObuff, "\" ");
5203}
5204
5205/*
5206 * Append message for text mode to IObuff.
5207 * Return TRUE if something appended.
5208 */
5209 static int
5210msg_add_fileformat(eol_type)
5211 int eol_type;
5212{
5213#ifndef USE_CRNL
5214 if (eol_type == EOL_DOS)
5215 {
5216 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
5217 return TRUE;
5218 }
5219#endif
5220#ifndef USE_CR
5221 if (eol_type == EOL_MAC)
5222 {
5223 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
5224 return TRUE;
5225 }
5226#endif
5227#if defined(USE_CRNL) || defined(USE_CR)
5228 if (eol_type == EOL_UNIX)
5229 {
5230 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
5231 return TRUE;
5232 }
5233#endif
5234 return FALSE;
5235}
5236
5237/*
5238 * Append line and character count to IObuff.
5239 */
Bram Moolenaar009b2592004-10-24 19:18:58 +00005240 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241msg_add_lines(insert_space, lnum, nchars)
5242 int insert_space;
5243 long lnum;
Bram Moolenaar914703b2010-05-31 21:59:46 +02005244 off_t nchars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245{
5246 char_u *p;
5247
5248 p = IObuff + STRLEN(IObuff);
5249
5250 if (insert_space)
5251 *p++ = ' ';
5252 if (shortmess(SHM_LINES))
Bram Moolenaar914703b2010-05-31 21:59:46 +02005253 sprintf((char *)p,
5254#ifdef LONG_LONG_OFF_T
Bram Moolenaarf9b01292010-06-12 06:45:20 +02005255 "%ldL, %lldC", lnum, nchars
Bram Moolenaar914703b2010-05-31 21:59:46 +02005256#else
Bram Moolenaarf9b01292010-06-12 06:45:20 +02005257 /* Explicit typecast avoids warning on Mac OS X 10.6 */
5258 "%ldL, %ldC", lnum, (long)nchars
Bram Moolenaar914703b2010-05-31 21:59:46 +02005259#endif
Bram Moolenaarf9b01292010-06-12 06:45:20 +02005260 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 else
5262 {
5263 if (lnum == 1)
5264 STRCPY(p, _("1 line, "));
5265 else
5266 sprintf((char *)p, _("%ld lines, "), lnum);
5267 p += STRLEN(p);
5268 if (nchars == 1)
5269 STRCPY(p, _("1 character"));
5270 else
Bram Moolenaar914703b2010-05-31 21:59:46 +02005271 sprintf((char *)p,
5272#ifdef LONG_LONG_OFF_T
Bram Moolenaarf9b01292010-06-12 06:45:20 +02005273 _("%lld characters"), nchars
Bram Moolenaar914703b2010-05-31 21:59:46 +02005274#else
Bram Moolenaarf9b01292010-06-12 06:45:20 +02005275 /* Explicit typecast avoids warning on Mac OS X 10.6 */
5276 _("%ld characters"), (long)nchars
Bram Moolenaar914703b2010-05-31 21:59:46 +02005277#endif
Bram Moolenaarf9b01292010-06-12 06:45:20 +02005278 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 }
5280}
5281
5282/*
5283 * Append message for missing line separator to IObuff.
5284 */
5285 static void
5286msg_add_eol()
5287{
5288 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
5289}
5290
5291/*
5292 * Check modification time of file, before writing to it.
5293 * The size isn't checked, because using a tool like "gzip" takes care of
5294 * using the same timestamp but can't set the size.
5295 */
5296 static int
5297check_mtime(buf, st)
5298 buf_T *buf;
5299 struct stat *st;
5300{
5301 if (buf->b_mtime_read != 0
5302 && time_differs((long)st->st_mtime, buf->b_mtime_read))
5303 {
5304 msg_scroll = TRUE; /* don't overwrite messages here */
5305 msg_silent = 0; /* must give this prompt */
5306 /* don't use emsg() here, don't want to flush the buffers */
5307 MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"),
5308 hl_attr(HLF_E));
5309 if (ask_yesno((char_u *)_("Do you really want to write to it"),
5310 TRUE) == 'n')
5311 return FAIL;
5312 msg_scroll = FALSE; /* always overwrite the file message now */
5313 }
5314 return OK;
5315}
5316
5317 static int
5318time_differs(t1, t2)
5319 long t1, t2;
5320{
5321#if defined(__linux__) || defined(MSDOS) || defined(MSWIN)
5322 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store
5323 * the seconds. Since the roundoff is done when flushing the inode, the
5324 * time may change unexpectedly by one second!!! */
5325 return (t1 - t2 > 1 || t2 - t1 > 1);
5326#else
5327 return (t1 != t2);
5328#endif
5329}
5330
5331/*
5332 * Call write() to write a number of bytes to the file.
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005333 * Handles encryption and 'encoding' conversion.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 *
5335 * Return FAIL for failure, OK otherwise.
5336 */
5337 static int
5338buf_write_bytes(ip)
5339 struct bw_info *ip;
5340{
5341 int wlen;
5342 char_u *buf = ip->bw_buf; /* data to write */
5343 int len = ip->bw_len; /* length of data */
5344#ifdef HAS_BW_FLAGS
5345 int flags = ip->bw_flags; /* extra flags */
5346#endif
5347
5348#ifdef FEAT_MBYTE
5349 /*
5350 * Skip conversion when writing the crypt magic number or the BOM.
5351 */
5352 if (!(flags & FIO_NOCONVERT))
5353 {
5354 char_u *p;
5355 unsigned c;
5356 int n;
5357
5358 if (flags & FIO_UTF8)
5359 {
5360 /*
5361 * Convert latin1 in the buffer to UTF-8 in the file.
5362 */
5363 p = ip->bw_conv_buf; /* translate to buffer */
5364 for (wlen = 0; wlen < len; ++wlen)
5365 p += utf_char2bytes(buf[wlen], p);
5366 buf = ip->bw_conv_buf;
5367 len = (int)(p - ip->bw_conv_buf);
5368 }
5369 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1))
5370 {
5371 /*
5372 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or
5373 * Latin1 chars in the file.
5374 */
5375 if (flags & FIO_LATIN1)
5376 p = buf; /* translate in-place (can only get shorter) */
5377 else
5378 p = ip->bw_conv_buf; /* translate to buffer */
5379 for (wlen = 0; wlen < len; wlen += n)
5380 {
5381 if (wlen == 0 && ip->bw_restlen != 0)
5382 {
5383 int l;
5384
5385 /* Use remainder of previous call. Append the start of
5386 * buf[] to get a full sequence. Might still be too
5387 * short! */
5388 l = CONV_RESTLEN - ip->bw_restlen;
5389 if (l > len)
5390 l = len;
5391 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005392 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 if (n > ip->bw_restlen + len)
5394 {
5395 /* We have an incomplete byte sequence at the end to
5396 * be written. We can't convert it without the
5397 * remaining bytes. Keep them for the next call. */
5398 if (ip->bw_restlen + len > CONV_RESTLEN)
5399 return FAIL;
5400 ip->bw_restlen += len;
5401 break;
5402 }
5403 if (n > 1)
5404 c = utf_ptr2char(ip->bw_rest);
5405 else
5406 c = ip->bw_rest[0];
5407 if (n >= ip->bw_restlen)
5408 {
5409 n -= ip->bw_restlen;
5410 ip->bw_restlen = 0;
5411 }
5412 else
5413 {
5414 ip->bw_restlen -= n;
5415 mch_memmove(ip->bw_rest, ip->bw_rest + n,
5416 (size_t)ip->bw_restlen);
5417 n = 0;
5418 }
5419 }
5420 else
5421 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005422 n = utf_ptr2len_len(buf + wlen, len - wlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 if (n > len - wlen)
5424 {
5425 /* We have an incomplete byte sequence at the end to
5426 * be written. We can't convert it without the
5427 * remaining bytes. Keep them for the next call. */
5428 if (len - wlen > CONV_RESTLEN)
5429 return FAIL;
5430 ip->bw_restlen = len - wlen;
5431 mch_memmove(ip->bw_rest, buf + wlen,
5432 (size_t)ip->bw_restlen);
5433 break;
5434 }
5435 if (n > 1)
5436 c = utf_ptr2char(buf + wlen);
5437 else
5438 c = buf[wlen];
5439 }
5440
Bram Moolenaar32b485f2009-07-29 16:06:27 +00005441 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error)
5442 {
5443 ip->bw_conv_error = TRUE;
5444 ip->bw_conv_error_lnum = ip->bw_start_lnum;
5445 }
5446 if (c == NL)
5447 ++ip->bw_start_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 }
5449 if (flags & FIO_LATIN1)
5450 len = (int)(p - buf);
5451 else
5452 {
5453 buf = ip->bw_conv_buf;
5454 len = (int)(p - ip->bw_conv_buf);
5455 }
5456 }
5457
5458# ifdef WIN3264
5459 else if (flags & FIO_CODEPAGE)
5460 {
5461 /*
5462 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows
5463 * codepage.
5464 */
5465 char_u *from;
5466 size_t fromlen;
5467 char_u *to;
5468 int u8c;
5469 BOOL bad = FALSE;
5470 int needed;
5471
5472 if (ip->bw_restlen > 0)
5473 {
5474 /* Need to concatenate the remainder of the previous call and
5475 * the bytes of the current call. Use the end of the
5476 * conversion buffer for this. */
5477 fromlen = len + ip->bw_restlen;
5478 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5479 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
5480 mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
5481 }
5482 else
5483 {
5484 from = buf;
5485 fromlen = len;
5486 }
5487
5488 to = ip->bw_conv_buf;
5489 if (enc_utf8)
5490 {
5491 /* Convert from UTF-8 to UCS-2, to the start of the buffer.
5492 * The buffer has been allocated to be big enough. */
5493 while (fromlen > 0)
5494 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005495 n = (int)utf_ptr2len_len(from, (int)fromlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 if (n > (int)fromlen) /* incomplete byte sequence */
5497 break;
5498 u8c = utf_ptr2char(from);
5499 *to++ = (u8c & 0xff);
5500 *to++ = (u8c >> 8);
5501 fromlen -= n;
5502 from += n;
5503 }
5504
5505 /* Copy remainder to ip->bw_rest[] to be used for the next
5506 * call. */
5507 if (fromlen > CONV_RESTLEN)
5508 {
5509 /* weird overlong sequence */
5510 ip->bw_conv_error = TRUE;
5511 return FAIL;
5512 }
5513 mch_memmove(ip->bw_rest, from, fromlen);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005514 ip->bw_restlen = (int)fromlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
5516 else
5517 {
5518 /* Convert from enc_codepage to UCS-2, to the start of the
5519 * buffer. The buffer has been allocated to be big enough. */
5520 ip->bw_restlen = 0;
5521 needed = MultiByteToWideChar(enc_codepage,
Bram Moolenaar36f5ac02007-05-06 12:40:34 +00005522 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 NULL, 0);
5524 if (needed == 0)
5525 {
5526 /* When conversion fails there may be a trailing byte. */
5527 needed = MultiByteToWideChar(enc_codepage,
Bram Moolenaar36f5ac02007-05-06 12:40:34 +00005528 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 NULL, 0);
5530 if (needed == 0)
5531 {
5532 /* Conversion doesn't work. */
5533 ip->bw_conv_error = TRUE;
5534 return FAIL;
5535 }
5536 /* Save the trailing byte for the next call. */
5537 ip->bw_rest[0] = from[fromlen - 1];
5538 ip->bw_restlen = 1;
5539 }
5540 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS,
Bram Moolenaar36f5ac02007-05-06 12:40:34 +00005541 (LPCSTR)from, (int)(fromlen - ip->bw_restlen),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 (LPWSTR)to, needed);
5543 if (needed == 0)
5544 {
5545 /* Safety check: Conversion doesn't work. */
5546 ip->bw_conv_error = TRUE;
5547 return FAIL;
5548 }
5549 to += needed * 2;
5550 }
5551
5552 fromlen = to - ip->bw_conv_buf;
5553 buf = to;
5554# ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */
5555 if (FIO_GET_CP(flags) == CP_UTF8)
5556 {
5557 /* Convert from UCS-2 to UTF-8, using the remainder of the
5558 * conversion buffer. Fails when out of space. */
5559 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2)
5560 {
5561 u8c = *from++;
5562 u8c += (*from++ << 8);
5563 to += utf_char2bytes(u8c, to);
5564 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen)
5565 {
5566 ip->bw_conv_error = TRUE;
5567 return FAIL;
5568 }
5569 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005570 len = (int)(to - buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
5572 else
5573#endif
5574 {
5575 /* Convert from UCS-2 to the codepage, using the remainder of
5576 * the conversion buffer. If the conversion uses the default
5577 * character "0", the data doesn't fit in this encoding, so
5578 * fail. */
5579 len = WideCharToMultiByte(FIO_GET_CP(flags), 0,
5580 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR),
Bram Moolenaar36f5ac02007-05-06 12:40:34 +00005581 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0,
5582 &bad);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 if (bad)
5584 {
5585 ip->bw_conv_error = TRUE;
5586 return FAIL;
5587 }
5588 }
5589 }
5590# endif
5591
Bram Moolenaar56718732006-03-15 22:53:57 +00005592# ifdef MACOS_CONVERT
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 else if (flags & FIO_MACROMAN)
5594 {
5595 /*
5596 * Convert UTF-8 or latin1 to Apple MacRoman.
5597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 char_u *from;
5599 size_t fromlen;
5600
5601 if (ip->bw_restlen > 0)
5602 {
5603 /* Need to concatenate the remainder of the previous call and
5604 * the bytes of the current call. Use the end of the
5605 * conversion buffer for this. */
5606 fromlen = len + ip->bw_restlen;
5607 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5608 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
5609 mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
5610 }
5611 else
5612 {
5613 from = buf;
5614 fromlen = len;
5615 }
5616
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005617 if (enc2macroman(from, fromlen,
5618 ip->bw_conv_buf, &len, ip->bw_conv_buflen,
5619 ip->bw_rest, &ip->bw_restlen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 {
5621 ip->bw_conv_error = TRUE;
5622 return FAIL;
5623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 buf = ip->bw_conv_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626# endif
5627
5628# ifdef USE_ICONV
5629 if (ip->bw_iconv_fd != (iconv_t)-1)
5630 {
5631 const char *from;
5632 size_t fromlen;
5633 char *to;
5634 size_t tolen;
5635
5636 /* Convert with iconv(). */
5637 if (ip->bw_restlen > 0)
5638 {
Bram Moolenaar5d294d12009-03-11 12:11:02 +00005639 char *fp;
5640
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 /* Need to concatenate the remainder of the previous call and
5642 * the bytes of the current call. Use the end of the
5643 * conversion buffer for this. */
5644 fromlen = len + ip->bw_restlen;
Bram Moolenaar5d294d12009-03-11 12:11:02 +00005645 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
5646 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen);
5647 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len);
5648 from = fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 tolen = ip->bw_conv_buflen - fromlen;
5650 }
5651 else
5652 {
5653 from = (const char *)buf;
5654 fromlen = len;
5655 tolen = ip->bw_conv_buflen;
5656 }
5657 to = (char *)ip->bw_conv_buf;
5658
5659 if (ip->bw_first)
5660 {
5661 size_t save_len = tolen;
5662
5663 /* output the initial shift state sequence */
5664 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen);
5665
5666 /* There is a bug in iconv() on Linux (which appears to be
5667 * wide-spread) which sets "to" to NULL and messes up "tolen".
5668 */
5669 if (to == NULL)
5670 {
5671 to = (char *)ip->bw_conv_buf;
5672 tolen = save_len;
5673 }
5674 ip->bw_first = FALSE;
5675 }
5676
5677 /*
5678 * If iconv() has an error or there is not enough room, fail.
5679 */
5680 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen)
5681 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
5682 || fromlen > CONV_RESTLEN)
5683 {
5684 ip->bw_conv_error = TRUE;
5685 return FAIL;
5686 }
5687
5688 /* copy remainder to ip->bw_rest[] to be used for the next call. */
5689 if (fromlen > 0)
5690 mch_memmove(ip->bw_rest, (void *)from, fromlen);
5691 ip->bw_restlen = (int)fromlen;
5692
5693 buf = ip->bw_conv_buf;
5694 len = (int)((char_u *)to - ip->bw_conv_buf);
5695 }
5696# endif
5697 }
5698#endif /* FEAT_MBYTE */
5699
5700#ifdef FEAT_CRYPT
5701 if (flags & FIO_ENCRYPTED) /* encrypt the data */
Bram Moolenaar04c9baf2010-06-01 23:37:39 +02005702 crypt_encode(buf, len, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703#endif
5704
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01005705 wlen = write_eintr(ip->bw_fd, buf, len);
5706 return (wlen < len) ? FAIL : OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707}
5708
5709#ifdef FEAT_MBYTE
5710/*
5711 * Convert a Unicode character to bytes.
Bram Moolenaar32b485f2009-07-29 16:06:27 +00005712 * Return TRUE for an error, FALSE when it's OK.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 */
5714 static int
5715ucs2bytes(c, pp, flags)
5716 unsigned c; /* in: character */
5717 char_u **pp; /* in/out: pointer to result */
5718 int flags; /* FIO_ flags */
5719{
5720 char_u *p = *pp;
5721 int error = FALSE;
5722 int cc;
5723
5724
5725 if (flags & FIO_UCS4)
5726 {
5727 if (flags & FIO_ENDIAN_L)
5728 {
5729 *p++ = c;
5730 *p++ = (c >> 8);
5731 *p++ = (c >> 16);
5732 *p++ = (c >> 24);
5733 }
5734 else
5735 {
5736 *p++ = (c >> 24);
5737 *p++ = (c >> 16);
5738 *p++ = (c >> 8);
5739 *p++ = c;
5740 }
5741 }
5742 else if (flags & (FIO_UCS2 | FIO_UTF16))
5743 {
5744 if (c >= 0x10000)
5745 {
5746 if (flags & FIO_UTF16)
5747 {
5748 /* Make two words, ten bits of the character in each. First
5749 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */
5750 c -= 0x10000;
5751 if (c >= 0x100000)
5752 error = TRUE;
5753 cc = ((c >> 10) & 0x3ff) + 0xd800;
5754 if (flags & FIO_ENDIAN_L)
5755 {
5756 *p++ = cc;
5757 *p++ = ((unsigned)cc >> 8);
5758 }
5759 else
5760 {
5761 *p++ = ((unsigned)cc >> 8);
5762 *p++ = cc;
5763 }
5764 c = (c & 0x3ff) + 0xdc00;
5765 }
5766 else
5767 error = TRUE;
5768 }
5769 if (flags & FIO_ENDIAN_L)
5770 {
5771 *p++ = c;
5772 *p++ = (c >> 8);
5773 }
5774 else
5775 {
5776 *p++ = (c >> 8);
5777 *p++ = c;
5778 }
5779 }
5780 else /* Latin1 */
5781 {
5782 if (c >= 0x100)
5783 {
5784 error = TRUE;
5785 *p++ = 0xBF;
5786 }
5787 else
5788 *p++ = c;
5789 }
5790
5791 *pp = p;
5792 return error;
5793}
5794
5795/*
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00005796 * Return TRUE if file encoding "fenc" requires conversion from or to
5797 * 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 */
5799 static int
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00005800need_conversion(fenc)
5801 char_u *fenc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00005803 int same_encoding;
5804 int enc_flags;
5805 int fenc_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00005807 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0)
Bram Moolenaar442b4222010-05-24 21:34:22 +02005808 {
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00005809 same_encoding = TRUE;
Bram Moolenaar442b4222010-05-24 21:34:22 +02005810 fenc_flags = 0;
5811 }
Bram Moolenaarb5cdf2e2009-07-29 16:25:31 +00005812 else
5813 {
5814 /* Ignore difference between "ansi" and "latin1", "ucs-4" and
5815 * "ucs-4be", etc. */
5816 enc_flags = get_fio_flags(p_enc);
5817 fenc_flags = get_fio_flags(fenc);
5818 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags);
5819 }
5820 if (same_encoding)
5821 {
5822 /* Specified encoding matches with 'encoding'. This requires
5823 * conversion when 'encoding' is Unicode but not UTF-8. */
5824 return enc_unicode != 0;
5825 }
5826
5827 /* Encodings differ. However, conversion is not needed when 'enc' is any
5828 * Unicode encoding and the file is UTF-8. */
5829 return !(enc_utf8 && fenc_flags == FIO_UTF8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830}
5831
5832/*
5833 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the
5834 * internal conversion.
5835 * if "ptr" is an empty string, use 'encoding'.
5836 */
5837 static int
5838get_fio_flags(ptr)
5839 char_u *ptr;
5840{
5841 int prop;
5842
5843 if (*ptr == NUL)
5844 ptr = p_enc;
5845
5846 prop = enc_canon_props(ptr);
5847 if (prop & ENC_UNICODE)
5848 {
5849 if (prop & ENC_2BYTE)
5850 {
5851 if (prop & ENC_ENDIAN_L)
5852 return FIO_UCS2 | FIO_ENDIAN_L;
5853 return FIO_UCS2;
5854 }
5855 if (prop & ENC_4BYTE)
5856 {
5857 if (prop & ENC_ENDIAN_L)
5858 return FIO_UCS4 | FIO_ENDIAN_L;
5859 return FIO_UCS4;
5860 }
5861 if (prop & ENC_2WORD)
5862 {
5863 if (prop & ENC_ENDIAN_L)
5864 return FIO_UTF16 | FIO_ENDIAN_L;
5865 return FIO_UTF16;
5866 }
5867 return FIO_UTF8;
5868 }
5869 if (prop & ENC_LATIN1)
5870 return FIO_LATIN1;
5871 /* must be ENC_DBCS, requires iconv() */
5872 return 0;
5873}
5874
5875#ifdef WIN3264
5876/*
5877 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed
5878 * for the conversion MS-Windows can do for us. Also accept "utf-8".
5879 * Used for conversion between 'encoding' and 'fileencoding'.
5880 */
5881 static int
5882get_win_fio_flags(ptr)
5883 char_u *ptr;
5884{
5885 int cp;
5886
5887 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */
5888 if (!enc_utf8 && enc_codepage <= 0)
5889 return 0;
5890
5891 cp = encname2codepage(ptr);
5892 if (cp == 0)
5893 {
5894# ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */
5895 if (STRCMP(ptr, "utf-8") == 0)
5896 cp = CP_UTF8;
5897 else
5898# endif
5899 return 0;
5900 }
5901 return FIO_PUT_CP(cp) | FIO_CODEPAGE;
5902}
5903#endif
5904
5905#ifdef MACOS_X
5906/*
5907 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags
5908 * needed for the internal conversion to/from utf-8 or latin1.
5909 */
5910 static int
5911get_mac_fio_flags(ptr)
5912 char_u *ptr;
5913{
5914 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
5915 && (enc_canon_props(ptr) & ENC_MACROMAN))
5916 return FIO_MACROMAN;
5917 return 0;
5918}
5919#endif
5920
5921/*
5922 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size].
5923 * "size" must be at least 2.
5924 * Return the name of the encoding and set "*lenp" to the length.
5925 * Returns NULL when no BOM found.
5926 */
5927 static char_u *
5928check_for_bom(p, size, lenp, flags)
5929 char_u *p;
5930 long size;
5931 int *lenp;
5932 int flags;
5933{
5934 char *name = NULL;
5935 int len = 2;
5936
5937 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
Bram Moolenaaree0f5a62008-07-24 20:09:16 +00005938 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {
5940 name = "utf-8"; /* EF BB BF */
5941 len = 3;
5942 }
5943 else if (p[0] == 0xff && p[1] == 0xfe)
5944 {
5945 if (size >= 4 && p[2] == 0 && p[3] == 0
5946 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
5947 {
5948 name = "ucs-4le"; /* FF FE 00 00 */
5949 len = 4;
5950 }
Bram Moolenaar223a1892008-11-11 20:57:11 +00005951 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 name = "ucs-2le"; /* FF FE */
Bram Moolenaar223a1892008-11-11 20:57:11 +00005953 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
5954 /* utf-16le is preferred, it also works for ucs-2le text */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 name = "utf-16le"; /* FF FE */
5956 }
5957 else if (p[0] == 0xfe && p[1] == 0xff
5958 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
5959 {
Bram Moolenaarffd82c52008-02-20 17:15:26 +00005960 /* Default to utf-16, it works also for ucs-2 text. */
5961 if (flags == FIO_UCS2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 name = "ucs-2"; /* FE FF */
Bram Moolenaarffd82c52008-02-20 17:15:26 +00005963 else
5964 name = "utf-16"; /* FE FF */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 }
5966 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
5967 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
5968 {
5969 name = "ucs-4"; /* 00 00 FE FF */
5970 len = 4;
5971 }
5972
5973 *lenp = len;
5974 return (char_u *)name;
5975}
5976
5977/*
5978 * Generate a BOM in "buf[4]" for encoding "name".
5979 * Return the length of the BOM (zero when no BOM).
5980 */
5981 static int
5982make_bom(buf, name)
5983 char_u *buf;
5984 char_u *name;
5985{
5986 int flags;
5987 char_u *p;
5988
5989 flags = get_fio_flags(name);
5990
5991 /* Can't put a BOM in a non-Unicode file. */
5992 if (flags == FIO_LATIN1 || flags == 0)
5993 return 0;
5994
5995 if (flags == FIO_UTF8) /* UTF-8 */
5996 {
5997 buf[0] = 0xef;
5998 buf[1] = 0xbb;
5999 buf[2] = 0xbf;
6000 return 3;
6001 }
6002 p = buf;
6003 (void)ucs2bytes(0xfeff, &p, flags);
6004 return (int)(p - buf);
6005}
6006#endif
6007
Bram Moolenaard4cacdf2007-10-03 10:50:10 +00006008#if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \
Bram Moolenaara0174af2008-01-02 20:08:25 +00006009 defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010/*
6011 * Try to find a shortname by comparing the fullname with the current
6012 * directory.
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006013 * Returns "full_path" or pointer into "full_path" if shortened.
6014 */
6015 char_u *
6016shorten_fname1(full_path)
6017 char_u *full_path;
6018{
Bram Moolenaard9462e32011-04-11 21:35:11 +02006019 char_u *dirname;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006020 char_u *p = full_path;
6021
Bram Moolenaard9462e32011-04-11 21:35:11 +02006022 dirname = alloc(MAXPATHL);
6023 if (dirname == NULL)
6024 return full_path;
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006025 if (mch_dirname(dirname, MAXPATHL) == OK)
6026 {
6027 p = shorten_fname(full_path, dirname);
6028 if (p == NULL || *p == NUL)
6029 p = full_path;
6030 }
Bram Moolenaard9462e32011-04-11 21:35:11 +02006031 vim_free(dirname);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006032 return p;
6033}
Bram Moolenaard4cacdf2007-10-03 10:50:10 +00006034#endif
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006035
6036/*
6037 * Try to find a shortname by comparing the fullname with the current
6038 * directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 * Returns NULL if not shorter name possible, pointer into "full_path"
6040 * otherwise.
6041 */
6042 char_u *
6043shorten_fname(full_path, dir_name)
6044 char_u *full_path;
6045 char_u *dir_name;
6046{
6047 int len;
6048 char_u *p;
6049
6050 if (full_path == NULL)
6051 return NULL;
6052 len = (int)STRLEN(dir_name);
6053 if (fnamencmp(dir_name, full_path, len) == 0)
6054 {
6055 p = full_path + len;
6056#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
6057 /*
6058 * MSDOS: when a file is in the root directory, dir_name will end in a
6059 * slash, since C: by itself does not define a specific dir. In this
6060 * case p may already be correct. <negri>
6061 */
6062 if (!((len > 2) && (*(p - 2) == ':')))
6063#endif
6064 {
6065 if (vim_ispathsep(*p))
6066 ++p;
6067#ifndef VMS /* the path separator is always part of the path */
6068 else
6069 p = NULL;
6070#endif
6071 }
6072 }
6073#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
6074 /*
6075 * When using a file in the current drive, remove the drive name:
6076 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on
6077 * a floppy from "A:\dir" to "B:\dir".
6078 */
6079 else if (len > 3
6080 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
6081 && full_path[1] == ':'
6082 && vim_ispathsep(full_path[2]))
6083 p = full_path + 2;
6084#endif
6085 else
6086 p = NULL;
6087 return p;
6088}
6089
6090/*
6091 * Shorten filenames for all buffers.
6092 * When "force" is TRUE: Use full path from now on for files currently being
6093 * edited, both for file name and swap file name. Try to shorten the file
6094 * names a bit, if safe to do so.
6095 * When "force" is FALSE: Only try to shorten absolute file names.
6096 * For buffers that have buftype "nofile" or "scratch": never change the file
6097 * name.
6098 */
6099 void
6100shorten_fnames(force)
6101 int force;
6102{
6103 char_u dirname[MAXPATHL];
6104 buf_T *buf;
6105 char_u *p;
6106
6107 mch_dirname(dirname, MAXPATHL);
6108 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6109 {
6110 if (buf->b_fname != NULL
6111#ifdef FEAT_QUICKFIX
6112 && !bt_nofile(buf)
6113#endif
6114 && !path_with_url(buf->b_fname)
6115 && (force
6116 || buf->b_sfname == NULL
6117 || mch_isFullName(buf->b_sfname)))
6118 {
6119 vim_free(buf->b_sfname);
6120 buf->b_sfname = NULL;
6121 p = shorten_fname(buf->b_ffname, dirname);
6122 if (p != NULL)
6123 {
6124 buf->b_sfname = vim_strsave(p);
6125 buf->b_fname = buf->b_sfname;
6126 }
6127 if (p == NULL || buf->b_fname == NULL)
6128 buf->b_fname = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006130
6131 /* Always make the swap file name a full path, a "nofile" buffer may
6132 * also have a swap file. */
6133 mf_fullname(buf->b_ml.ml_mfp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 }
6135#ifdef FEAT_WINDOWS
6136 status_redraw_all();
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006137 redraw_tabline = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138#endif
6139}
6140
6141#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
6142 || defined(FEAT_GUI_MSWIN) \
6143 || defined(FEAT_GUI_MAC) \
6144 || defined(PROTO)
6145/*
6146 * Shorten all filenames in "fnames[count]" by current directory.
6147 */
6148 void
6149shorten_filenames(fnames, count)
6150 char_u **fnames;
6151 int count;
6152{
6153 int i;
6154 char_u dirname[MAXPATHL];
6155 char_u *p;
6156
6157 if (fnames == NULL || count < 1)
6158 return;
6159 mch_dirname(dirname, sizeof(dirname));
6160 for (i = 0; i < count; ++i)
6161 {
6162 if ((p = shorten_fname(fnames[i], dirname)) != NULL)
6163 {
6164 /* shorten_fname() returns pointer in given "fnames[i]". If free
6165 * "fnames[i]" first, "p" becomes invalid. So we need to copy
6166 * "p" first then free fnames[i]. */
6167 p = vim_strsave(p);
6168 vim_free(fnames[i]);
6169 fnames[i] = p;
6170 }
6171 }
6172}
6173#endif
6174
6175/*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00006176 * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 * fo_o_h.ext for MSDOS or when shortname option set.
6178 *
6179 * Assumed that fname is a valid name found in the filesystem we assure that
6180 * the return value is a different name and ends in 'ext'.
6181 * "ext" MUST be at most 4 characters long if it starts with a dot, 3
6182 * characters otherwise.
6183 * Space for the returned name is allocated, must be freed later.
6184 * Returns NULL when out of memory.
6185 */
6186 char_u *
6187modname(fname, ext, prepend_dot)
6188 char_u *fname, *ext;
6189 int prepend_dot; /* may prepend a '.' to file name */
6190{
6191 return buf_modname(
6192#ifdef SHORT_FNAME
6193 TRUE,
6194#else
6195 (curbuf->b_p_sn || curbuf->b_shortname),
6196#endif
6197 fname, ext, prepend_dot);
6198}
6199
6200 char_u *
6201buf_modname(shortname, fname, ext, prepend_dot)
6202 int shortname; /* use 8.3 file name */
6203 char_u *fname, *ext;
6204 int prepend_dot; /* may prepend a '.' to file name */
6205{
6206 char_u *retval;
6207 char_u *s;
6208 char_u *e;
6209 char_u *ptr;
6210 int fnamelen, extlen;
6211
6212 extlen = (int)STRLEN(ext);
6213
6214 /*
6215 * If there is no file name we must get the name of the current directory
6216 * (we need the full path in case :cd is used).
6217 */
6218 if (fname == NULL || *fname == NUL)
6219 {
6220 retval = alloc((unsigned)(MAXPATHL + extlen + 3));
6221 if (retval == NULL)
6222 return NULL;
6223 if (mch_dirname(retval, MAXPATHL) == FAIL ||
6224 (fnamelen = (int)STRLEN(retval)) == 0)
6225 {
6226 vim_free(retval);
6227 return NULL;
6228 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006229 if (!after_pathsep(retval, retval + fnamelen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
6231 retval[fnamelen++] = PATHSEP;
6232 retval[fnamelen] = NUL;
6233 }
6234#ifndef SHORT_FNAME
6235 prepend_dot = FALSE; /* nothing to prepend a dot to */
6236#endif
6237 }
6238 else
6239 {
6240 fnamelen = (int)STRLEN(fname);
6241 retval = alloc((unsigned)(fnamelen + extlen + 3));
6242 if (retval == NULL)
6243 return NULL;
6244 STRCPY(retval, fname);
6245#ifdef VMS
6246 vms_remove_version(retval); /* we do not need versions here */
6247#endif
6248 }
6249
6250 /*
6251 * search backwards until we hit a '/', '\' or ':' replacing all '.'
6252 * by '_' for MSDOS or when shortname option set and ext starts with a dot.
6253 * Then truncate what is after the '/', '\' or ':' to 8 characters for
6254 * MSDOS and 26 characters for AMIGA, a lot more for UNIX.
6255 */
Bram Moolenaar53180ce2005-07-05 21:48:14 +00006256 for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (*ext == '.'
Bram Moolenaare60acc12011-05-10 16:41:25 +02006259#ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 && (!USE_LONG_FNAME || shortname)
Bram Moolenaare60acc12011-05-10 16:41:25 +02006261#else
6262# ifndef SHORT_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 && shortname
6264# endif
Bram Moolenaare60acc12011-05-10 16:41:25 +02006265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 )
6267 if (*ptr == '.') /* replace '.' by '_' */
6268 *ptr = '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 if (vim_ispathsep(*ptr))
Bram Moolenaar53180ce2005-07-05 21:48:14 +00006270 {
6271 ++ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 break;
Bram Moolenaar53180ce2005-07-05 21:48:14 +00006273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
6276 /* the file name has at most BASENAMELEN characters. */
6277#ifndef SHORT_FNAME
6278 if (STRLEN(ptr) > (unsigned)BASENAMELEN)
6279 ptr[BASENAMELEN] = '\0';
6280#endif
6281
6282 s = ptr + STRLEN(ptr);
6283
6284 /*
6285 * For 8.3 file names we may have to reduce the length.
6286 */
6287#ifdef USE_LONG_FNAME
6288 if (!USE_LONG_FNAME || shortname)
6289#else
6290# ifndef SHORT_FNAME
6291 if (shortname)
6292# endif
6293#endif
6294 {
6295 /*
6296 * If there is no file name, or the file name ends in '/', and the
6297 * extension starts with '.', put a '_' before the dot, because just
6298 * ".ext" is invalid.
6299 */
6300 if (fname == NULL || *fname == NUL
6301 || vim_ispathsep(fname[STRLEN(fname) - 1]))
6302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 *s++ = '_';
6305 }
6306 /*
6307 * If the extension starts with '.', truncate the base name at 8
6308 * characters
6309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 else if (*ext == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
Bram Moolenaar78a15312009-05-15 19:33:18 +00006312 if ((size_t)(s - ptr) > (size_t)8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
6314 s = ptr + 8;
6315 *s = '\0';
6316 }
6317 }
6318 /*
6319 * If the extension doesn't start with '.', and the file name
6320 * doesn't have an extension yet, append a '.'
6321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 else if ((e = vim_strchr(ptr, '.')) == NULL)
6323 *s++ = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 /*
6325 * If the extension doesn't start with '.', and there already is an
Bram Moolenaar7263a772007-05-10 17:35:54 +00006326 * extension, it may need to be truncated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 */
6328 else if ((int)STRLEN(e) + extlen > 4)
6329 s = e + 4 - extlen;
6330 }
6331#if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264)
6332 /*
6333 * If there is no file name, and the extension starts with '.', put a
6334 * '_' before the dot, because just ".ext" may be invalid if it's on a
6335 * FAT partition, and on HPFS it doesn't matter.
6336 */
6337 else if ((fname == NULL || *fname == NUL) && *ext == '.')
6338 *s++ = '_';
6339#endif
6340
6341 /*
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00006342 * Append the extension.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 * ext can start with '.' and cannot exceed 3 more characters.
6344 */
6345 STRCPY(s, ext);
6346
6347#ifndef SHORT_FNAME
6348 /*
6349 * Prepend the dot.
6350 */
Bram Moolenaare60acc12011-05-10 16:41:25 +02006351 if (prepend_dot && !shortname && *(e = gettail(retval)) != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352#ifdef USE_LONG_FNAME
6353 && USE_LONG_FNAME
6354#endif
6355 )
6356 {
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00006357 STRMOVE(e + 1, e);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 *e = '.';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 }
6360#endif
6361
6362 /*
6363 * Check that, after appending the extension, the file name is really
6364 * different.
6365 */
6366 if (fname != NULL && STRCMP(fname, retval) == 0)
6367 {
6368 /* we search for a character that can be replaced by '_' */
6369 while (--s >= ptr)
6370 {
6371 if (*s != '_')
6372 {
6373 *s = '_';
6374 break;
6375 }
6376 }
6377 if (s < ptr) /* fname was "________.<ext>", how tricky! */
6378 *ptr = 'v';
6379 }
6380 return retval;
6381}
6382
6383/*
6384 * Like fgets(), but if the file line is too long, it is truncated and the
6385 * rest of the line is thrown away. Returns TRUE for end-of-file.
6386 */
6387 int
6388vim_fgets(buf, size, fp)
6389 char_u *buf;
6390 int size;
6391 FILE *fp;
6392{
6393 char *eof;
6394#define FGETS_SIZE 200
6395 char tbuf[FGETS_SIZE];
6396
6397 buf[size - 2] = NUL;
6398#ifdef USE_CR
6399 eof = fgets_cr((char *)buf, size, fp);
6400#else
6401 eof = fgets((char *)buf, size, fp);
6402#endif
6403 if (buf[size - 2] != NUL && buf[size - 2] != '\n')
6404 {
6405 buf[size - 1] = NUL; /* Truncate the line */
6406
6407 /* Now throw away the rest of the line: */
6408 do
6409 {
6410 tbuf[FGETS_SIZE - 2] = NUL;
6411#ifdef USE_CR
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00006412 ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413#else
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00006414 ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415#endif
6416 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
6417 }
6418 return (eof == NULL);
6419}
6420
6421#if defined(USE_CR) || defined(PROTO)
6422/*
6423 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF.
6424 * Returns TRUE for end-of-file.
6425 * Only used for the Mac, because it's much slower than vim_fgets().
6426 */
6427 int
6428tag_fgets(buf, size, fp)
6429 char_u *buf;
6430 int size;
6431 FILE *fp;
6432{
6433 int i = 0;
6434 int c;
6435 int eof = FALSE;
6436
6437 for (;;)
6438 {
6439 c = fgetc(fp);
6440 if (c == EOF)
6441 {
6442 eof = TRUE;
6443 break;
6444 }
6445 if (c == '\r')
6446 {
6447 /* Always store a NL for end-of-line. */
6448 if (i < size - 1)
6449 buf[i++] = '\n';
6450 c = fgetc(fp);
6451 if (c != '\n') /* Macintosh format: single CR. */
6452 ungetc(c, fp);
6453 break;
6454 }
6455 if (i < size - 1)
6456 buf[i++] = c;
6457 if (c == '\n')
6458 break;
6459 }
6460 buf[i] = NUL;
6461 return eof;
6462}
6463#endif
6464
6465/*
6466 * rename() only works if both files are on the same file system, this
6467 * function will (attempts to?) copy the file across if rename fails -- webb
6468 * Return -1 for failure, 0 for success.
6469 */
6470 int
6471vim_rename(from, to)
6472 char_u *from;
6473 char_u *to;
6474{
6475 int fd_in;
6476 int fd_out;
6477 int n;
6478 char *errmsg = NULL;
6479 char *buffer;
6480#ifdef AMIGA
6481 BPTR flock;
6482#endif
6483 struct stat st;
Bram Moolenaar9be038d2005-03-08 22:34:32 +00006484 long perm;
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00006485#ifdef HAVE_ACL
6486 vim_acl_T acl; /* ACL from original file */
6487#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006488#if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME)
6489 int use_tmp_file = FALSE;
6490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491
6492 /*
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006493 * When the names are identical, there is nothing to do. When they refer
6494 * to the same file (ignoring case and slash/backslash differences) but
6495 * the file name differs we need to go through a temp file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 */
6497 if (fnamecmp(from, to) == 0)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006498 {
6499#ifdef CASE_INSENSITIVE_FILENAME
6500 if (STRCMP(gettail(from), gettail(to)) != 0)
6501 use_tmp_file = TRUE;
6502 else
6503#endif
6504 return 0;
6505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 /*
6508 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted.
6509 */
6510 if (mch_stat((char *)from, &st) < 0)
6511 return -1;
6512
Bram Moolenaar3576da72008-12-30 15:15:57 +00006513#ifdef UNIX
6514 {
6515 struct stat st_to;
Bram Moolenaar3576da72008-12-30 15:15:57 +00006516
6517 /* It's possible for the source and destination to be the same file.
6518 * This happens when "from" and "to" differ in case and are on a FAT32
6519 * filesystem. In that case go through a temp file name. */
6520 if (mch_stat((char *)to, &st_to) >= 0
6521 && st.st_dev == st_to.st_dev
6522 && st.st_ino == st_to.st_ino)
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006523 use_tmp_file = TRUE;
6524 }
6525#endif
Bram Moolenaar1c32dff2011-05-05 16:41:24 +02006526#ifdef WIN3264
6527 {
6528 BY_HANDLE_FILE_INFORMATION info1, info2;
6529
6530 /* It's possible for the source and destination to be the same file.
6531 * In that case go through a temp file name. This makes rename("foo",
6532 * "./foo") a no-op (in a complicated way). */
6533 if (win32_fileinfo(from, &info1) == FILEINFO_OK
6534 && win32_fileinfo(to, &info2) == FILEINFO_OK
6535 && info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
6536 && info1.nFileIndexHigh == info2.nFileIndexHigh
6537 && info1.nFileIndexLow == info2.nFileIndexLow)
6538 use_tmp_file = TRUE;
6539 }
6540#endif
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006541
6542#if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME)
6543 if (use_tmp_file)
6544 {
6545 char tempname[MAXPATHL + 1];
6546
6547 /*
6548 * Find a name that doesn't exist and is in the same directory.
6549 * Rename "from" to "tempname" and then rename "tempname" to "to".
6550 */
6551 if (STRLEN(from) >= MAXPATHL - 5)
6552 return -1;
6553 STRCPY(tempname, from);
6554 for (n = 123; n < 99999; ++n)
Bram Moolenaar3576da72008-12-30 15:15:57 +00006555 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006556 sprintf((char *)gettail((char_u *)tempname), "%d", n);
6557 if (mch_stat(tempname, &st) < 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00006558 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006559 if (mch_rename((char *)from, tempname) == 0)
Bram Moolenaar3576da72008-12-30 15:15:57 +00006560 {
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006561 if (mch_rename(tempname, (char *)to) == 0)
6562 return 0;
6563 /* Strange, the second step failed. Try moving the
6564 * file back and return failure. */
6565 mch_rename(tempname, (char *)from);
Bram Moolenaar3576da72008-12-30 15:15:57 +00006566 return -1;
6567 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006568 /* If it fails for one temp name it will most likely fail
6569 * for any temp name, give up. */
6570 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00006571 }
Bram Moolenaar3576da72008-12-30 15:15:57 +00006572 }
Bram Moolenaare0e6f992008-12-31 15:21:32 +00006573 return -1;
Bram Moolenaar3576da72008-12-30 15:15:57 +00006574 }
6575#endif
6576
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 /*
6578 * Delete the "to" file, this is required on some systems to make the
6579 * mch_rename() work, on other systems it makes sure that we don't have
6580 * two files when the mch_rename() fails.
6581 */
6582
6583#ifdef AMIGA
6584 /*
6585 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible
6586 * that the name of the "to" file is the same as the "from" file, even
Bram Moolenaar7263a772007-05-10 17:35:54 +00006587 * though the names are different. To avoid the chance of accidentally
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 * deleting the "from" file (horror!) we lock it during the remove.
6589 *
6590 * When used for making a backup before writing the file: This should not
6591 * happen with ":w", because startscript() should detect this problem and
6592 * set buf->b_shortname, causing modname() to return a correct ".bak" file
6593 * name. This problem does exist with ":w filename", but then the
6594 * original file will be somewhere else so the backup isn't really
6595 * important. If autoscripting is off the rename may fail.
6596 */
6597 flock = Lock((UBYTE *)from, (long)ACCESS_READ);
6598#endif
6599 mch_remove(to);
6600#ifdef AMIGA
6601 if (flock)
6602 UnLock(flock);
6603#endif
6604
6605 /*
6606 * First try a normal rename, return if it works.
6607 */
6608 if (mch_rename((char *)from, (char *)to) == 0)
6609 return 0;
6610
6611 /*
6612 * Rename() failed, try copying the file.
6613 */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00006614 perm = mch_getperm(from);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00006615#ifdef HAVE_ACL
6616 /* For systems that support ACL: get the ACL from the original file. */
6617 acl = mch_get_acl(from);
6618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
6620 if (fd_in == -1)
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00006621 {
6622#ifdef HAVE_ACL
6623 mch_free_acl(acl);
6624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 return -1;
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00006626 }
Bram Moolenaar9be038d2005-03-08 22:34:32 +00006627
6628 /* Create the new file with same permissions as the original. */
Bram Moolenaara5792f52005-11-23 21:25:05 +00006629 fd_out = mch_open((char *)to,
6630 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (fd_out == -1)
6632 {
6633 close(fd_in);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00006634#ifdef HAVE_ACL
6635 mch_free_acl(acl);
6636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 return -1;
6638 }
6639
6640 buffer = (char *)alloc(BUFSIZE);
6641 if (buffer == NULL)
6642 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 close(fd_out);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00006644 close(fd_in);
6645#ifdef HAVE_ACL
6646 mch_free_acl(acl);
6647#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 return -1;
6649 }
6650
Bram Moolenaar540fc6f2010-12-17 16:27:16 +01006651 while ((n = read_eintr(fd_in, buffer, BUFSIZE)) > 0)
6652 if (write_eintr(fd_out, buffer, n) != n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 {
6654 errmsg = _("E208: Error writing to \"%s\"");
6655 break;
6656 }
6657
6658 vim_free(buffer);
6659 close(fd_in);
6660 if (close(fd_out) < 0)
6661 errmsg = _("E209: Error closing \"%s\"");
6662 if (n < 0)
6663 {
6664 errmsg = _("E210: Error reading \"%s\"");
6665 to = from;
6666 }
Bram Moolenaar7263a772007-05-10 17:35:54 +00006667#ifndef UNIX /* for Unix mch_open() already set the permission */
Bram Moolenaar9be038d2005-03-08 22:34:32 +00006668 mch_setperm(to, perm);
Bram Moolenaarc6039d82005-12-02 00:44:04 +00006669#endif
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00006670#ifdef HAVE_ACL
6671 mch_set_acl(to, acl);
Bram Moolenaarb23a7e82008-06-27 18:42:32 +00006672 mch_free_acl(acl);
Bram Moolenaarcd71fa32005-03-11 22:46:48 +00006673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 if (errmsg != NULL)
6675 {
6676 EMSG2(errmsg, to);
6677 return -1;
6678 }
6679 mch_remove(from);
6680 return 0;
6681}
6682
6683static int already_warned = FALSE;
6684
6685/*
6686 * Check if any not hidden buffer has been changed.
6687 * Postpone the check if there are characters in the stuff buffer, a global
6688 * command is being executed, a mapping is being executed or an autocommand is
6689 * busy.
6690 * Returns TRUE if some message was written (screen should be redrawn and
6691 * cursor positioned).
6692 */
6693 int
6694check_timestamps(focus)
6695 int focus; /* called for GUI focus event */
6696{
6697 buf_T *buf;
6698 int didit = 0;
6699 int n;
6700
6701 /* Don't check timestamps while system() or another low-level function may
6702 * cause us to lose and gain focus. */
6703 if (no_check_timestamps > 0)
6704 return FALSE;
6705
6706 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus
6707 * event and we would keep on checking if the file is steadily growing.
6708 * Do check again after typing something. */
6709 if (focus && did_check_timestamps)
6710 {
6711 need_check_timestamps = TRUE;
6712 return FALSE;
6713 }
6714
6715 if (!stuff_empty() || global_busy || !typebuf_typed()
6716#ifdef FEAT_AUTOCMD
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00006717 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718#endif
6719 )
6720 need_check_timestamps = TRUE; /* check later */
6721 else
6722 {
6723 ++no_wait_return;
6724 did_check_timestamps = TRUE;
6725 already_warned = FALSE;
6726 for (buf = firstbuf; buf != NULL; )
6727 {
6728 /* Only check buffers in a window. */
6729 if (buf->b_nwindows > 0)
6730 {
6731 n = buf_check_timestamp(buf, focus);
6732 if (didit < n)
6733 didit = n;
6734 if (n > 0 && !buf_valid(buf))
6735 {
6736 /* Autocommands have removed the buffer, start at the
6737 * first one again. */
6738 buf = firstbuf;
6739 continue;
6740 }
6741 }
6742 buf = buf->b_next;
6743 }
6744 --no_wait_return;
6745 need_check_timestamps = FALSE;
6746 if (need_wait_return && didit == 2)
6747 {
6748 /* make sure msg isn't overwritten */
6749 msg_puts((char_u *)"\n");
6750 out_flush();
6751 }
6752 }
6753 return didit;
6754}
6755
6756/*
6757 * Move all the lines from buffer "frombuf" to buffer "tobuf".
6758 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not
6759 * empty.
6760 */
6761 static int
6762move_lines(frombuf, tobuf)
6763 buf_T *frombuf;
6764 buf_T *tobuf;
6765{
6766 buf_T *tbuf = curbuf;
6767 int retval = OK;
6768 linenr_T lnum;
6769 char_u *p;
6770
6771 /* Copy the lines in "frombuf" to "tobuf". */
6772 curbuf = tobuf;
6773 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
6774 {
6775 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
6776 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
6777 {
6778 vim_free(p);
6779 retval = FAIL;
6780 break;
6781 }
6782 vim_free(p);
6783 }
6784
6785 /* Delete all the lines in "frombuf". */
6786 if (retval != FAIL)
6787 {
6788 curbuf = frombuf;
Bram Moolenaar9460b9d2007-01-09 14:37:01 +00006789 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
6790 if (ml_delete(lnum, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 {
6792 /* Oops! We could try putting back the saved lines, but that
6793 * might fail again... */
6794 retval = FAIL;
6795 break;
6796 }
6797 }
6798
6799 curbuf = tbuf;
6800 return retval;
6801}
6802
6803/*
6804 * Check if buffer "buf" has been changed.
6805 * Also check if the file for a new buffer unexpectedly appeared.
6806 * return 1 if a changed buffer was found.
6807 * return 2 if a message has been displayed.
6808 * return 0 otherwise.
6809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 int
6811buf_check_timestamp(buf, focus)
6812 buf_T *buf;
Bram Moolenaar78a15312009-05-15 19:33:18 +00006813 int focus UNUSED; /* called for GUI focus event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814{
6815 struct stat st;
6816 int stat_res;
6817 int retval = 0;
6818 char_u *path;
6819 char_u *tbuf;
6820 char *mesg = NULL;
Bram Moolenaar44ecf652005-03-07 23:09:59 +00006821 char *mesg2 = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 int helpmesg = FALSE;
6823 int reload = FALSE;
6824#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6825 int can_reload = FALSE;
6826#endif
Bram Moolenaar914703b2010-05-31 21:59:46 +02006827 off_t orig_size = buf->b_orig_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 int orig_mode = buf->b_orig_mode;
6829#ifdef FEAT_GUI
6830 int save_mouse_correct = need_mouse_correct;
6831#endif
6832#ifdef FEAT_AUTOCMD
6833 static int busy = FALSE;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006834 int n;
6835 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836#endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006837 char *reason;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838
6839 /* If there is no file name, the buffer is not loaded, 'buftype' is
6840 * set, we are in the middle of a save or being called recursively: ignore
6841 * this buffer. */
6842 if (buf->b_ffname == NULL
6843 || buf->b_ml.ml_mfp == NULL
6844#if defined(FEAT_QUICKFIX)
6845 || *buf->b_p_bt != NUL
6846#endif
6847 || buf->b_saving
6848#ifdef FEAT_AUTOCMD
6849 || busy
6850#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +00006851#ifdef FEAT_NETBEANS_INTG
6852 || isNetbeansBuffer(buf)
6853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 )
6855 return 0;
6856
6857 if ( !(buf->b_flags & BF_NOTEDITED)
6858 && buf->b_mtime != 0
6859 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
6860 || time_differs((long)st.st_mtime, buf->b_mtime)
6861#ifdef HAVE_ST_MODE
6862 || (int)st.st_mode != buf->b_orig_mode
6863#else
6864 || mch_getperm(buf->b_ffname) != buf->b_orig_mode
6865#endif
6866 ))
6867 {
6868 retval = 1;
6869
Bram Moolenaar316059c2006-01-14 21:18:42 +00006870 /* set b_mtime to stop further warnings (e.g., when executing
6871 * FileChangedShell autocmd) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (stat_res < 0)
6873 {
6874 buf->b_mtime = 0;
6875 buf->b_orig_size = 0;
6876 buf->b_orig_mode = 0;
6877 }
6878 else
6879 buf_store_time(buf, &st, buf->b_ffname);
6880
6881 /* Don't do anything for a directory. Might contain the file
6882 * explorer. */
6883 if (mch_isdir(buf->b_fname))
6884 ;
6885
6886 /*
6887 * If 'autoread' is set, the buffer has no changes and the file still
6888 * exists, reload the buffer. Use the buffer-local option value if it
6889 * was set, the global option value otherwise.
6890 */
6891 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
6892 && !bufIsChanged(buf) && stat_res >= 0)
6893 reload = TRUE;
6894 else
6895 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006896 if (stat_res < 0)
6897 reason = "deleted";
6898 else if (bufIsChanged(buf))
6899 reason = "conflict";
6900 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
6901 reason = "changed";
6902 else if (orig_mode != buf->b_orig_mode)
6903 reason = "mode";
6904 else
6905 reason = "time";
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006907#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 /*
6909 * Only give the warning if there are no FileChangedShell
6910 * autocommands.
6911 * Avoid being called recursively by setting "busy".
6912 */
6913 busy = TRUE;
Bram Moolenaar1e015462005-09-25 22:16:38 +00006914# ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006915 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
6916 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00006917# endif
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00006918 ++allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 n = apply_autocmds(EVENT_FILECHANGEDSHELL,
6920 buf->b_fname, buf->b_fname, FALSE, buf);
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00006921 --allbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 busy = FALSE;
6923 if (n)
6924 {
6925 if (!buf_valid(buf))
6926 EMSG(_("E246: FileChangedShell autocommand deleted buffer"));
Bram Moolenaar1e015462005-09-25 22:16:38 +00006927# ifdef FEAT_EVAL
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006928 s = get_vim_var_str(VV_FCS_CHOICE);
6929 if (STRCMP(s, "reload") == 0 && *reason != 'd')
6930 reload = TRUE;
6931 else if (STRCMP(s, "ask") == 0)
6932 n = FALSE;
6933 else
Bram Moolenaar1e015462005-09-25 22:16:38 +00006934# endif
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006935 return 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006937 if (!n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938#endif
6939 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006940 if (*reason == 'd')
6941 mesg = _("E211: File \"%s\" no longer available");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 else
6943 {
6944 helpmesg = TRUE;
6945#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6946 can_reload = TRUE;
6947#endif
6948 /*
6949 * Check if the file contents really changed to avoid
6950 * giving a warning when only the timestamp was set (e.g.,
6951 * checked out of CVS). Always warn when the buffer was
6952 * changed.
6953 */
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006954 if (reason[2] == 'n')
6955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006957 mesg2 = _("See \":help W12\" for more info.");
6958 }
6959 else if (reason[1] == 'h')
6960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 mesg = _("W11: Warning: File \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006962 mesg2 = _("See \":help W11\" for more info.");
6963 }
6964 else if (*reason == 'm')
6965 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006967 mesg2 = _("See \":help W16\" for more info.");
6968 }
Bram Moolenaar85388b52009-06-24 09:58:32 +00006969 else
6970 /* Only timestamp changed, store it to avoid a warning
6971 * in check_mtime() later. */
6972 buf->b_mtime_read = buf->b_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 }
6974 }
6975 }
6976
6977 }
6978 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
6979 && vim_fexists(buf->b_ffname))
6980 {
6981 retval = 1;
6982 mesg = _("W13: Warning: File \"%s\" has been created after editing started");
6983 buf->b_flags |= BF_NEW_W;
6984#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
6985 can_reload = TRUE;
6986#endif
6987 }
6988
6989 if (mesg != NULL)
6990 {
6991 path = home_replace_save(buf, buf->b_fname);
6992 if (path != NULL)
6993 {
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006994 if (!helpmesg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 mesg2 = "";
6996 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg)
6997 + STRLEN(mesg2) + 2));
6998 sprintf((char *)tbuf, mesg, path);
Bram Moolenaar496c5262009-03-18 14:42:00 +00006999#ifdef FEAT_EVAL
7000 /* Set warningmsg here, before the unimportant and output-specific
7001 * mesg2 has been appended. */
7002 set_vim_var_string(VV_WARNINGMSG, tbuf, -1);
7003#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
7005 if (can_reload)
7006 {
7007 if (*mesg2 != NUL)
7008 {
7009 STRCAT(tbuf, "\n");
7010 STRCAT(tbuf, mesg2);
7011 }
7012 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01007013 (char_u *)_("&OK\n&Load File"), 1, NULL, TRUE) == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 reload = TRUE;
7015 }
7016 else
7017#endif
7018 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
7019 {
7020 if (*mesg2 != NUL)
7021 {
7022 STRCAT(tbuf, "; ");
7023 STRCAT(tbuf, mesg2);
7024 }
7025 EMSG(tbuf);
7026 retval = 2;
7027 }
7028 else
7029 {
Bram Moolenaared203462004-06-16 11:19:22 +00007030# ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 if (!autocmd_busy)
Bram Moolenaared203462004-06-16 11:19:22 +00007032# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {
7034 msg_start();
7035 msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST);
7036 if (*mesg2 != NUL)
7037 msg_puts_attr((char_u *)mesg2,
7038 hl_attr(HLF_W) + MSG_HIST);
7039 msg_clr_eos();
7040 (void)msg_end();
7041 if (emsg_silent == 0)
7042 {
7043 out_flush();
Bram Moolenaared203462004-06-16 11:19:22 +00007044# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 if (!focus)
Bram Moolenaared203462004-06-16 11:19:22 +00007046# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 /* give the user some time to think about it */
7048 ui_delay(1000L, TRUE);
7049
7050 /* don't redraw and erase the message */
7051 redraw_cmdline = FALSE;
7052 }
7053 }
7054 already_warned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 }
7056
7057 vim_free(path);
7058 vim_free(tbuf);
7059 }
7060 }
7061
7062 if (reload)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007063 /* Reload the buffer. */
Bram Moolenaar316059c2006-01-14 21:18:42 +00007064 buf_reload(buf, orig_mode);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
Bram Moolenaar56718732006-03-15 22:53:57 +00007066#ifdef FEAT_AUTOCMD
Bram Moolenaar3577c6f2008-06-24 21:16:56 +00007067 /* Trigger FileChangedShell when the file was changed in any way. */
7068 if (buf_valid(buf) && retval != 0)
Bram Moolenaar56718732006-03-15 22:53:57 +00007069 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
7070 buf->b_fname, buf->b_fname, FALSE, buf);
7071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072#ifdef FEAT_GUI
7073 /* restore this in case an autocommand has set it; it would break
7074 * 'mousefocus' */
7075 need_mouse_correct = save_mouse_correct;
7076#endif
7077
7078 return retval;
7079}
7080
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007081/*
7082 * Reload a buffer that is already loaded.
7083 * Used when the file was changed outside of Vim.
Bram Moolenaar316059c2006-01-14 21:18:42 +00007084 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected.
7085 * buf->b_orig_mode may have been reset already.
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007086 */
7087 void
Bram Moolenaar316059c2006-01-14 21:18:42 +00007088buf_reload(buf, orig_mode)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007089 buf_T *buf;
Bram Moolenaar316059c2006-01-14 21:18:42 +00007090 int orig_mode;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007091{
7092 exarg_T ea;
7093 pos_T old_cursor;
7094 linenr_T old_topline;
7095 int old_ro = buf->b_p_ro;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007096 buf_T *savebuf;
7097 int saved = OK;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007098 aco_save_T aco;
Bram Moolenaar59f931e2010-07-24 20:27:03 +02007099 int flags = READ_NEW;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007100
7101 /* set curwin/curbuf for "buf" and save some things */
7102 aucmd_prepbuf(&aco, buf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007103
7104 /* We only want to read the text from the file, not reset the syntax
7105 * highlighting, clear marks, diff status, etc. Force the fileformat
7106 * and encoding to be the same. */
7107 if (prep_exarg(&ea, buf) == OK)
7108 {
7109 old_cursor = curwin->w_cursor;
7110 old_topline = curwin->w_topline;
7111
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02007112 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
Bram Moolenaar59f931e2010-07-24 20:27:03 +02007113 {
7114 /* Save all the text, so that the reload can be undone.
7115 * Sync first so that this is a separate undo-able action. */
7116 u_sync(FALSE);
7117 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE);
7118 flags |= READ_KEEP_UNDO;
7119 }
7120
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007121 /*
7122 * To behave like when a new file is edited (matters for
7123 * BufReadPost autocommands) we first need to delete the current
7124 * buffer contents. But if reading the file fails we should keep
7125 * the old contents. Can't use memory only, the file might be
7126 * too big. Use a hidden buffer to move the buffer contents to.
7127 */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02007128 if (bufempty() || saved == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007129 savebuf = NULL;
7130 else
7131 {
7132 /* Allocate a buffer without putting it in the buffer list. */
7133 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
Bram Moolenaar8424a622006-04-19 21:23:36 +00007134 if (savebuf != NULL && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007135 {
7136 /* Open the memline. */
7137 curbuf = savebuf;
7138 curwin->w_buffer = savebuf;
Bram Moolenaar4770d092006-01-12 23:22:24 +00007139 saved = ml_open(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007140 curbuf = buf;
7141 curwin->w_buffer = buf;
7142 }
Bram Moolenaar8424a622006-04-19 21:23:36 +00007143 if (savebuf == NULL || saved == FAIL || buf != curbuf
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007144 || move_lines(buf, savebuf) == FAIL)
7145 {
7146 EMSG2(_("E462: Could not prepare for reloading \"%s\""),
7147 buf->b_fname);
7148 saved = FAIL;
7149 }
7150 }
7151
7152 if (saved == OK)
7153 {
7154 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */
7155#ifdef FEAT_AUTOCMD
7156 keep_filetype = TRUE; /* don't detect 'filetype' */
7157#endif
7158 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
7159 (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +02007160 (linenr_T)MAXLNUM, &ea, flags) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007161 {
7162#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7163 if (!aborting())
7164#endif
7165 EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname);
Bram Moolenaar8424a622006-04-19 21:23:36 +00007166 if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007167 {
7168 /* Put the text back from the save buffer. First
7169 * delete any lines that readfile() added. */
7170 while (!bufempty())
Bram Moolenaar8424a622006-04-19 21:23:36 +00007171 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007172 break;
7173 (void)move_lines(savebuf, buf);
7174 }
7175 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +02007176 else if (buf == curbuf) /* "buf" still valid */
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007177 {
7178 /* Mark the buffer as unmodified and free undo info. */
7179 unchanged(buf, TRUE);
Bram Moolenaar59f931e2010-07-24 20:27:03 +02007180 if ((flags & READ_KEEP_UNDO) == 0)
7181 {
7182 u_blockfree(buf);
7183 u_clearall(buf);
7184 }
7185 else
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02007186 {
Bram Moolenaar59f931e2010-07-24 20:27:03 +02007187 /* Mark all undo states as changed. */
7188 u_unchanged(curbuf);
Bram Moolenaarf9bb7342010-08-04 14:29:54 +02007189 }
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007190 }
7191 }
7192 vim_free(ea.cmd);
7193
Bram Moolenaar8424a622006-04-19 21:23:36 +00007194 if (savebuf != NULL && buf_valid(savebuf))
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007195 wipe_buffer(savebuf, FALSE);
7196
7197#ifdef FEAT_DIFF
7198 /* Invalidate diff info if necessary. */
Bram Moolenaar8424a622006-04-19 21:23:36 +00007199 diff_invalidate(curbuf);
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007200#endif
7201
7202 /* Restore the topline and cursor position and check it (lines may
7203 * have been removed). */
7204 if (old_topline > curbuf->b_ml.ml_line_count)
7205 curwin->w_topline = curbuf->b_ml.ml_line_count;
7206 else
7207 curwin->w_topline = old_topline;
7208 curwin->w_cursor = old_cursor;
7209 check_cursor();
7210 update_topline();
7211#ifdef FEAT_AUTOCMD
7212 keep_filetype = FALSE;
7213#endif
7214#ifdef FEAT_FOLDING
7215 {
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00007216 win_T *wp;
7217 tabpage_T *tp;
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007218
7219 /* Update folds unless they are defined manually. */
Bram Moolenaarbd1e5d22009-04-29 09:02:44 +00007220 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007221 if (wp->w_buffer == curwin->w_buffer
7222 && !foldmethodIsManual(wp))
7223 foldUpdateAll(wp);
7224 }
7225#endif
7226 /* If the mode didn't change and 'readonly' was set, keep the old
7227 * value; the user probably used the ":view" command. But don't
7228 * reset it, might have had a read error. */
7229 if (orig_mode == curbuf->b_orig_mode)
7230 curbuf->b_p_ro |= old_ro;
7231 }
7232
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007233 /* restore curwin/curbuf and a few other things */
7234 aucmd_restbuf(&aco);
7235 /* Careful: autocommands may have made "buf" invalid! */
Bram Moolenaar631d6f62005-06-07 21:02:10 +00007236}
7237
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 void
7239buf_store_time(buf, st, fname)
7240 buf_T *buf;
7241 struct stat *st;
Bram Moolenaar78a15312009-05-15 19:33:18 +00007242 char_u *fname UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
7244 buf->b_mtime = (long)st->st_mtime;
Bram Moolenaar914703b2010-05-31 21:59:46 +02007245 buf->b_orig_size = st->st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246#ifdef HAVE_ST_MODE
7247 buf->b_orig_mode = (int)st->st_mode;
7248#else
7249 buf->b_orig_mode = mch_getperm(fname);
7250#endif
7251}
7252
7253/*
7254 * Adjust the line with missing eol, used for the next write.
7255 * Used for do_filter(), when the input lines for the filter are deleted.
7256 */
7257 void
7258write_lnum_adjust(offset)
7259 linenr_T offset;
7260{
Bram Moolenaarcab35ad2011-02-15 17:39:22 +01007261 if (curbuf->b_no_eol_lnum != 0) /* only if there is a missing eol */
7262 curbuf->b_no_eol_lnum += offset;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263}
7264
7265#if defined(TEMPDIRNAMES) || defined(PROTO)
7266static long temp_count = 0; /* Temp filename counter. */
7267
7268/*
7269 * Delete the temp directory and all files it contains.
7270 */
7271 void
7272vim_deltempdir()
7273{
7274 char_u **files;
7275 int file_count;
7276 int i;
7277
7278 if (vim_tempdir != NULL)
7279 {
7280 sprintf((char *)NameBuff, "%s*", vim_tempdir);
7281 if (gen_expand_wildcards(1, &NameBuff, &file_count, &files,
7282 EW_DIR|EW_FILE|EW_SILENT) == OK)
7283 {
7284 for (i = 0; i < file_count; ++i)
7285 mch_remove(files[i]);
7286 FreeWild(file_count, files);
7287 }
7288 gettail(NameBuff)[-1] = NUL;
7289 (void)mch_rmdir(NameBuff);
7290
7291 vim_free(vim_tempdir);
7292 vim_tempdir = NULL;
7293 }
7294}
7295#endif
7296
Bram Moolenaar4592dee2009-11-18 19:11:58 +00007297#ifdef TEMPDIRNAMES
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298/*
Bram Moolenaareaf03392009-11-17 11:08:52 +00007299 * Directory "tempdir" was created. Expand this name to a full path and put
7300 * it in "vim_tempdir". This avoids that using ":cd" would confuse us.
7301 * "tempdir" must be no longer than MAXPATHL.
7302 */
7303 static void
7304vim_settempdir(tempdir)
7305 char_u *tempdir;
7306{
7307 char_u *buf;
7308
7309 buf = alloc((unsigned)MAXPATHL + 2);
7310 if (buf != NULL)
7311 {
7312 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL)
7313 STRCPY(buf, tempdir);
7314# ifdef __EMX__
7315 if (vim_strchr(buf, '/') != NULL)
7316 STRCAT(buf, "/");
7317 else
7318# endif
7319 add_pathsep(buf);
7320 vim_tempdir = vim_strsave(buf);
7321 vim_free(buf);
7322 }
7323}
Bram Moolenaar4592dee2009-11-18 19:11:58 +00007324#endif
Bram Moolenaareaf03392009-11-17 11:08:52 +00007325
7326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 * vim_tempname(): Return a unique name that can be used for a temp file.
7328 *
7329 * The temp file is NOT created.
7330 *
7331 * The returned pointer is to allocated memory.
7332 * The returned pointer is NULL if no valid name was found.
7333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 char_u *
7335vim_tempname(extra_char)
Bram Moolenaar78a15312009-05-15 19:33:18 +00007336 int extra_char UNUSED; /* char to use in the name instead of '?' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337{
7338#ifdef USE_TMPNAM
7339 char_u itmp[L_tmpnam]; /* use tmpnam() */
7340#else
7341 char_u itmp[TEMPNAMELEN];
7342#endif
7343
7344#ifdef TEMPDIRNAMES
7345 static char *(tempdirs[]) = {TEMPDIRNAMES};
7346 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347# ifndef EEXIST
7348 struct stat st;
7349# endif
7350
7351 /*
7352 * This will create a directory for private use by this instance of Vim.
7353 * This is done once, and the same directory is used for all temp files.
7354 * This method avoids security problems because of symlink attacks et al.
7355 * It's also a bit faster, because we only need to check for an existing
7356 * file when creating the directory and not for each temp file.
7357 */
7358 if (vim_tempdir == NULL)
7359 {
7360 /*
7361 * Try the entries in TEMPDIRNAMES to create the temp directory.
7362 */
Bram Moolenaar78a15312009-05-15 19:33:18 +00007363 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00007365# ifndef HAVE_MKDTEMP
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01007366 size_t itmplen;
Bram Moolenaareaf03392009-11-17 11:08:52 +00007367 long nr;
7368 long off;
7369# endif
7370
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 /* expand $TMP, leave room for "/v1100000/999999999" */
7372 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
7373 if (mch_isdir(itmp)) /* directory exists */
7374 {
7375# ifdef __EMX__
7376 /* If $TMP contains a forward slash (perhaps using bash or
7377 * tcsh), don't add a backslash, use a forward slash!
7378 * Adding 2 backslashes didn't work. */
7379 if (vim_strchr(itmp, '/') != NULL)
7380 STRCAT(itmp, "/");
7381 else
7382# endif
7383 add_pathsep(itmp);
7384
Bram Moolenaareaf03392009-11-17 11:08:52 +00007385# ifdef HAVE_MKDTEMP
7386 /* Leave room for filename */
7387 STRCAT(itmp, "vXXXXXX");
7388 if (mkdtemp((char *)itmp) != NULL)
7389 vim_settempdir(itmp);
7390# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 /* Get an arbitrary number of up to 6 digits. When it's
7392 * unlikely that it already exists it will be faster,
7393 * otherwise it doesn't matter. The use of mkdir() avoids any
7394 * security problems because of the predictable number. */
7395 nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01007396 itmplen = STRLEN(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
7398 /* Try up to 10000 different values until we find a name that
7399 * doesn't exist. */
7400 for (off = 0; off < 10000L; ++off)
7401 {
7402 int r;
Bram Moolenaareaf03392009-11-17 11:08:52 +00007403# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 mode_t umask_save;
Bram Moolenaareaf03392009-11-17 11:08:52 +00007405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
Bram Moolenaareaf03392009-11-17 11:08:52 +00007407 sprintf((char *)itmp + itmplen, "v%ld", nr + off);
7408# ifndef EEXIST
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 /* If mkdir() does not set errno to EEXIST, check for
7410 * existing file here. There is a race condition then,
7411 * although it's fail-safe. */
7412 if (mch_stat((char *)itmp, &st) >= 0)
7413 continue;
Bram Moolenaareaf03392009-11-17 11:08:52 +00007414# endif
7415# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 /* Make sure the umask doesn't remove the executable bit.
7417 * "repl" has been reported to use "177". */
7418 umask_save = umask(077);
Bram Moolenaareaf03392009-11-17 11:08:52 +00007419# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 r = vim_mkdir(itmp, 0700);
Bram Moolenaareaf03392009-11-17 11:08:52 +00007421# if defined(UNIX) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 (void)umask(umask_save);
Bram Moolenaareaf03392009-11-17 11:08:52 +00007423# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 if (r == 0)
7425 {
Bram Moolenaareaf03392009-11-17 11:08:52 +00007426 vim_settempdir(itmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 break;
7428 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00007429# ifdef EEXIST
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 /* If the mkdir() didn't fail because the file/dir exists,
7431 * we probably can't create any dir here, try another
7432 * place. */
7433 if (errno != EEXIST)
Bram Moolenaareaf03392009-11-17 11:08:52 +00007434# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 break;
7436 }
Bram Moolenaareaf03392009-11-17 11:08:52 +00007437# endif /* HAVE_MKDTEMP */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 if (vim_tempdir != NULL)
7439 break;
7440 }
7441 }
7442 }
7443
7444 if (vim_tempdir != NULL)
7445 {
7446 /* There is no need to check if the file exists, because we own the
7447 * directory and nobody else creates a file in it. */
7448 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
7449 return vim_strsave(itmp);
7450 }
7451
7452 return NULL;
7453
7454#else /* TEMPDIRNAMES */
7455
7456# ifdef WIN3264
7457 char szTempFile[_MAX_PATH + 1];
7458 char buf4[4];
7459 char_u *retval;
7460 char_u *p;
7461
7462 STRCPY(itmp, "");
7463 if (GetTempPath(_MAX_PATH, szTempFile) == 0)
Bram Moolenaarb1891912011-02-09 14:47:03 +01007464 {
7465 szTempFile[0] = '.'; /* GetTempPath() failed, use current dir */
7466 szTempFile[1] = NUL;
7467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 strcpy(buf4, "VIM");
7469 buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */
7470 if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0)
7471 return NULL;
7472 /* GetTempFileName() will create the file, we don't want that */
7473 (void)DeleteFile(itmp);
7474
7475 /* Backslashes in a temp file name cause problems when filtering with
7476 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who
7477 * didn't set 'shellslash'. */
7478 retval = vim_strsave(itmp);
7479 if (*p_shcf == '-' || p_ssl)
7480 for (p = retval; *p; ++p)
7481 if (*p == '\\')
7482 *p = '/';
7483 return retval;
7484
7485# else /* WIN3264 */
7486
7487# ifdef USE_TMPNAM
Bram Moolenaar95474ca2011-02-09 16:44:51 +01007488 char_u *p;
7489
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 /* tmpnam() will make its own name */
Bram Moolenaar95474ca2011-02-09 16:44:51 +01007491 p = tmpnam((char *)itmp);
7492 if (p == NULL || *p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 return NULL;
7494# else
7495 char_u *p;
7496
7497# ifdef VMS_TEMPNAM
7498 /* mktemp() is not working on VMS. It seems to be
7499 * a do-nothing function. Therefore we use tempnam().
7500 */
7501 sprintf((char *)itmp, "VIM%c", extra_char);
7502 p = (char_u *)tempnam("tmp:", (char *)itmp);
7503 if (p != NULL)
7504 {
7505 /* VMS will use '.LOG' if we don't explicitly specify an extension,
7506 * and VIM will then be unable to find the file later */
7507 STRCPY(itmp, p);
7508 STRCAT(itmp, ".txt");
7509 free(p);
7510 }
7511 else
7512 return NULL;
7513# else
7514 STRCPY(itmp, TEMPNAME);
7515 if ((p = vim_strchr(itmp, '?')) != NULL)
7516 *p = extra_char;
7517 if (mktemp((char *)itmp) == NULL)
7518 return NULL;
7519# endif
7520# endif
7521
7522 return vim_strsave(itmp);
7523# endif /* WIN3264 */
7524#endif /* TEMPDIRNAMES */
7525}
7526
7527#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
7528/*
7529 * Convert all backslashes in fname to forward slashes in-place.
7530 */
7531 void
7532forward_slash(fname)
7533 char_u *fname;
7534{
7535 char_u *p;
7536
7537 for (p = fname; *p != NUL; ++p)
7538# ifdef FEAT_MBYTE
7539 /* The Big5 encoding can have '\' in the trail byte. */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007540 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 ++p;
7542 else
7543# endif
7544 if (*p == '\\')
7545 *p = '/';
7546}
7547#endif
7548
7549
7550/*
7551 * Code for automatic commands.
7552 *
7553 * Only included when "FEAT_AUTOCMD" has been defined.
7554 */
7555
7556#if defined(FEAT_AUTOCMD) || defined(PROTO)
7557
7558/*
7559 * The autocommands are stored in a list for each event.
7560 * Autocommands for the same pattern, that are consecutive, are joined
7561 * together, to avoid having to match the pattern too often.
7562 * The result is an array of Autopat lists, which point to AutoCmd lists:
7563 *
7564 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL
7565 * Autopat.cmds Autopat.cmds
7566 * | |
7567 * V V
7568 * AutoCmd.next AutoCmd.next
7569 * | |
7570 * V V
7571 * AutoCmd.next NULL
7572 * |
7573 * V
7574 * NULL
7575 *
7576 * first_autopat[1] --> Autopat.next --> NULL
7577 * Autopat.cmds
7578 * |
7579 * V
7580 * AutoCmd.next
7581 * |
7582 * V
7583 * NULL
7584 * etc.
7585 *
7586 * The order of AutoCmds is important, this is the order in which they were
7587 * defined and will have to be executed.
7588 */
7589typedef struct AutoCmd
7590{
7591 char_u *cmd; /* The command to be executed (NULL
7592 when command has been removed) */
7593 char nested; /* If autocommands nest here */
7594 char last; /* last command in list */
7595#ifdef FEAT_EVAL
7596 scid_T scriptID; /* script ID where defined */
7597#endif
7598 struct AutoCmd *next; /* Next AutoCmd in list */
7599} AutoCmd;
7600
7601typedef struct AutoPat
7602{
7603 int group; /* group ID */
7604 char_u *pat; /* pattern as typed (NULL when pattern
7605 has been removed) */
7606 int patlen; /* strlen() of pat */
Bram Moolenaar748bf032005-02-02 23:04:36 +00007607 regprog_T *reg_prog; /* compiled regprog for pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 char allow_dirs; /* Pattern may match whole path */
7609 char last; /* last pattern for apply_autocmds() */
7610 AutoCmd *cmds; /* list of commands to do */
7611 struct AutoPat *next; /* next AutoPat in AutoPat list */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007612 int buflocal_nr; /* !=0 for buffer-local AutoPat */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613} AutoPat;
7614
7615static struct event_name
7616{
7617 char *name; /* event name */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007618 event_T event; /* event number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619} event_names[] =
7620{
7621 {"BufAdd", EVENT_BUFADD},
7622 {"BufCreate", EVENT_BUFADD},
7623 {"BufDelete", EVENT_BUFDELETE},
7624 {"BufEnter", EVENT_BUFENTER},
7625 {"BufFilePost", EVENT_BUFFILEPOST},
7626 {"BufFilePre", EVENT_BUFFILEPRE},
7627 {"BufHidden", EVENT_BUFHIDDEN},
7628 {"BufLeave", EVENT_BUFLEAVE},
7629 {"BufNew", EVENT_BUFNEW},
7630 {"BufNewFile", EVENT_BUFNEWFILE},
7631 {"BufRead", EVENT_BUFREADPOST},
7632 {"BufReadCmd", EVENT_BUFREADCMD},
7633 {"BufReadPost", EVENT_BUFREADPOST},
7634 {"BufReadPre", EVENT_BUFREADPRE},
7635 {"BufUnload", EVENT_BUFUNLOAD},
7636 {"BufWinEnter", EVENT_BUFWINENTER},
7637 {"BufWinLeave", EVENT_BUFWINLEAVE},
7638 {"BufWipeout", EVENT_BUFWIPEOUT},
7639 {"BufWrite", EVENT_BUFWRITEPRE},
7640 {"BufWritePost", EVENT_BUFWRITEPOST},
7641 {"BufWritePre", EVENT_BUFWRITEPRE},
7642 {"BufWriteCmd", EVENT_BUFWRITECMD},
7643 {"CmdwinEnter", EVENT_CMDWINENTER},
7644 {"CmdwinLeave", EVENT_CMDWINLEAVE},
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00007645 {"ColorScheme", EVENT_COLORSCHEME},
Bram Moolenaar754b5602006-02-09 23:53:20 +00007646 {"CursorHold", EVENT_CURSORHOLD},
7647 {"CursorHoldI", EVENT_CURSORHOLDI},
7648 {"CursorMoved", EVENT_CURSORMOVED},
7649 {"CursorMovedI", EVENT_CURSORMOVEDI},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 {"EncodingChanged", EVENT_ENCODINGCHANGED},
7651 {"FileEncoding", EVENT_ENCODINGCHANGED},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {"FileAppendPost", EVENT_FILEAPPENDPOST},
7653 {"FileAppendPre", EVENT_FILEAPPENDPRE},
7654 {"FileAppendCmd", EVENT_FILEAPPENDCMD},
7655 {"FileChangedShell",EVENT_FILECHANGEDSHELL},
Bram Moolenaar56718732006-03-15 22:53:57 +00007656 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {"FileChangedRO", EVENT_FILECHANGEDRO},
7658 {"FileReadPost", EVENT_FILEREADPOST},
7659 {"FileReadPre", EVENT_FILEREADPRE},
7660 {"FileReadCmd", EVENT_FILEREADCMD},
7661 {"FileType", EVENT_FILETYPE},
7662 {"FileWritePost", EVENT_FILEWRITEPOST},
7663 {"FileWritePre", EVENT_FILEWRITEPRE},
7664 {"FileWriteCmd", EVENT_FILEWRITECMD},
7665 {"FilterReadPost", EVENT_FILTERREADPOST},
7666 {"FilterReadPre", EVENT_FILTERREADPRE},
7667 {"FilterWritePost", EVENT_FILTERWRITEPOST},
7668 {"FilterWritePre", EVENT_FILTERWRITEPRE},
7669 {"FocusGained", EVENT_FOCUSGAINED},
7670 {"FocusLost", EVENT_FOCUSLOST},
7671 {"FuncUndefined", EVENT_FUNCUNDEFINED},
7672 {"GUIEnter", EVENT_GUIENTER},
Bram Moolenaar265e5072006-08-29 16:13:22 +00007673 {"GUIFailed", EVENT_GUIFAILED},
Bram Moolenaar843ee412004-06-30 16:16:41 +00007674 {"InsertChange", EVENT_INSERTCHANGE},
7675 {"InsertEnter", EVENT_INSERTENTER},
7676 {"InsertLeave", EVENT_INSERTLEAVE},
Bram Moolenaare659c952011-05-19 17:25:41 +02007677 {"InsertCharPre", EVENT_INSERTCHARPRE},
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00007678 {"MenuPopup", EVENT_MENUPOPUP},
Bram Moolenaar7c626922005-02-07 22:01:03 +00007679 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST},
7680 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {"RemoteReply", EVENT_REMOTEREPLY},
Bram Moolenaar9372a112005-12-06 19:59:18 +00007682 {"SessionLoadPost", EVENT_SESSIONLOADPOST},
Bram Moolenaar5c4bab02006-03-10 21:37:46 +00007683 {"ShellCmdPost", EVENT_SHELLCMDPOST},
7684 {"ShellFilterPost", EVENT_SHELLFILTERPOST},
Bram Moolenaara2031822006-03-07 22:29:51 +00007685 {"SourcePre", EVENT_SOURCEPRE},
Bram Moolenaar8dd1aa52007-01-16 20:33:19 +00007686 {"SourceCmd", EVENT_SOURCECMD},
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007687 {"SpellFileMissing",EVENT_SPELLFILEMISSING},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 {"StdinReadPost", EVENT_STDINREADPOST},
7689 {"StdinReadPre", EVENT_STDINREADPRE},
Bram Moolenaarb815dac2005-12-07 20:59:24 +00007690 {"SwapExists", EVENT_SWAPEXISTS},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00007691 {"Syntax", EVENT_SYNTAX},
Bram Moolenaar70836c82006-02-20 21:28:49 +00007692 {"TabEnter", EVENT_TABENTER},
7693 {"TabLeave", EVENT_TABLEAVE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 {"TermChanged", EVENT_TERMCHANGED},
7695 {"TermResponse", EVENT_TERMRESPONSE},
7696 {"User", EVENT_USER},
7697 {"VimEnter", EVENT_VIMENTER},
7698 {"VimLeave", EVENT_VIMLEAVE},
7699 {"VimLeavePre", EVENT_VIMLEAVEPRE},
7700 {"WinEnter", EVENT_WINENTER},
7701 {"WinLeave", EVENT_WINLEAVE},
Bram Moolenaar56718732006-03-15 22:53:57 +00007702 {"VimResized", EVENT_VIMRESIZED},
Bram Moolenaar754b5602006-02-09 23:53:20 +00007703 {NULL, (event_T)0}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704};
7705
7706static AutoPat *first_autopat[NUM_EVENTS] =
7707{
7708 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7709 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7710 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7711 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007712 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
7713 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714};
7715
7716/*
7717 * struct used to keep status while executing autocommands for an event.
7718 */
7719typedef struct AutoPatCmd
7720{
7721 AutoPat *curpat; /* next AutoPat to examine */
7722 AutoCmd *nextcmd; /* next AutoCmd to execute */
7723 int group; /* group being used */
7724 char_u *fname; /* fname to match with */
7725 char_u *sfname; /* sfname to match with */
7726 char_u *tail; /* tail of fname */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007727 event_T event; /* current event */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007728 int arg_bufnr; /* initially equal to <abuf>, set to zero when
7729 buf is deleted */
7730 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731} AutoPatCmd;
7732
Bram Moolenaard6f676d2005-06-01 21:51:55 +00007733static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007734
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735/*
7736 * augroups stores a list of autocmd group names.
7737 */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00007738static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL};
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739#define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i])
7740
7741/*
7742 * The ID of the current group. Group 0 is the default one.
7743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744static int current_augroup = AUGROUP_DEFAULT;
7745
7746static int au_need_clean = FALSE; /* need to delete marked patterns */
7747
Bram Moolenaar754b5602006-02-09 23:53:20 +00007748static void show_autocmd __ARGS((AutoPat *ap, event_T event));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749static void au_remove_pat __ARGS((AutoPat *ap));
7750static void au_remove_cmds __ARGS((AutoPat *ap));
7751static void au_cleanup __ARGS((void));
7752static int au_new_group __ARGS((char_u *name));
7753static void au_del_group __ARGS((char_u *name));
Bram Moolenaar754b5602006-02-09 23:53:20 +00007754static event_T event_name2nr __ARGS((char_u *start, char_u **end));
7755static char_u *event_nr2name __ARGS((event_T event));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756static char_u *find_end_event __ARGS((char_u *arg, int have_group));
Bram Moolenaar754b5602006-02-09 23:53:20 +00007757static int event_ignored __ARGS((event_T event));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758static int au_get_grouparg __ARGS((char_u **argp));
Bram Moolenaar754b5602006-02-09 23:53:20 +00007759static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760static char_u *getnextac __ARGS((int c, void *cookie, int indent));
Bram Moolenaar754b5602006-02-09 23:53:20 +00007761static int apply_autocmds_group __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last));
7763
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007764
Bram Moolenaar754b5602006-02-09 23:53:20 +00007765static event_T last_event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766static int last_group;
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007767static int autocmd_blocked = 0; /* block all autocmds */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768
7769/*
7770 * Show the autocommands for one AutoPat.
7771 */
7772 static void
7773show_autocmd(ap, event)
7774 AutoPat *ap;
Bram Moolenaar754b5602006-02-09 23:53:20 +00007775 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 AutoCmd *ac;
7778
7779 /* Check for "got_int" (here and at various places below), which is set
7780 * when "q" has been hit for the "--more--" prompt */
7781 if (got_int)
7782 return;
7783 if (ap->pat == NULL) /* pattern has been removed */
7784 return;
7785
7786 msg_putchar('\n');
7787 if (got_int)
7788 return;
7789 if (event != last_event || ap->group != last_group)
7790 {
7791 if (ap->group != AUGROUP_DEFAULT)
7792 {
7793 if (AUGROUP_NAME(ap->group) == NULL)
7794 msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E));
7795 else
7796 msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T));
7797 msg_puts((char_u *)" ");
7798 }
7799 msg_puts_attr(event_nr2name(event), hl_attr(HLF_T));
7800 last_event = event;
7801 last_group = ap->group;
7802 msg_putchar('\n');
7803 if (got_int)
7804 return;
7805 }
7806 msg_col = 4;
7807 msg_outtrans(ap->pat);
7808
7809 for (ac = ap->cmds; ac != NULL; ac = ac->next)
7810 {
7811 if (ac->cmd != NULL) /* skip removed commands */
7812 {
7813 if (msg_col >= 14)
7814 msg_putchar('\n');
7815 msg_col = 14;
7816 if (got_int)
7817 return;
7818 msg_outtrans(ac->cmd);
Bram Moolenaarac6e65f2005-08-29 22:25:38 +00007819#ifdef FEAT_EVAL
7820 if (p_verbose > 0)
7821 last_set_msg(ac->scriptID);
7822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (got_int)
7824 return;
7825 if (ac->next != NULL)
7826 {
7827 msg_putchar('\n');
7828 if (got_int)
7829 return;
7830 }
7831 }
7832 }
7833}
7834
7835/*
7836 * Mark an autocommand pattern for deletion.
7837 */
7838 static void
7839au_remove_pat(ap)
7840 AutoPat *ap;
7841{
7842 vim_free(ap->pat);
7843 ap->pat = NULL;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007844 ap->buflocal_nr = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 au_need_clean = TRUE;
7846}
7847
7848/*
7849 * Mark all commands for a pattern for deletion.
7850 */
7851 static void
7852au_remove_cmds(ap)
7853 AutoPat *ap;
7854{
7855 AutoCmd *ac;
7856
7857 for (ac = ap->cmds; ac != NULL; ac = ac->next)
7858 {
7859 vim_free(ac->cmd);
7860 ac->cmd = NULL;
7861 }
7862 au_need_clean = TRUE;
7863}
7864
7865/*
7866 * Cleanup autocommands and patterns that have been deleted.
7867 * This is only done when not executing autocommands.
7868 */
7869 static void
7870au_cleanup()
7871{
7872 AutoPat *ap, **prev_ap;
7873 AutoCmd *ac, **prev_ac;
Bram Moolenaar754b5602006-02-09 23:53:20 +00007874 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875
7876 if (autocmd_busy || !au_need_clean)
7877 return;
7878
7879 /* loop over all events */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007880 for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
7881 event = (event_T)((int)event + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {
7883 /* loop over all autocommand patterns */
7884 prev_ap = &(first_autopat[(int)event]);
7885 for (ap = *prev_ap; ap != NULL; ap = *prev_ap)
7886 {
7887 /* loop over all commands for this pattern */
7888 prev_ac = &(ap->cmds);
7889 for (ac = *prev_ac; ac != NULL; ac = *prev_ac)
7890 {
7891 /* remove the command if the pattern is to be deleted or when
7892 * the command has been marked for deletion */
7893 if (ap->pat == NULL || ac->cmd == NULL)
7894 {
7895 *prev_ac = ac->next;
7896 vim_free(ac->cmd);
7897 vim_free(ac);
7898 }
7899 else
7900 prev_ac = &(ac->next);
7901 }
7902
7903 /* remove the pattern if it has been marked for deletion */
7904 if (ap->pat == NULL)
7905 {
7906 *prev_ap = ap->next;
Bram Moolenaar748bf032005-02-02 23:04:36 +00007907 vim_free(ap->reg_prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908 vim_free(ap);
7909 }
7910 else
7911 prev_ap = &(ap->next);
7912 }
7913 }
7914
7915 au_need_clean = FALSE;
7916}
7917
7918/*
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007919 * Called when buffer is freed, to remove/invalidate related buffer-local
7920 * autocmds.
7921 */
7922 void
7923aubuflocal_remove(buf)
7924 buf_T *buf;
7925{
7926 AutoPat *ap;
Bram Moolenaar754b5602006-02-09 23:53:20 +00007927 event_T event;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007928 AutoPatCmd *apc;
7929
7930 /* invalidate currently executing autocommands */
7931 for (apc = active_apc_list; apc; apc = apc->next)
7932 if (buf->b_fnum == apc->arg_bufnr)
7933 apc->arg_bufnr = 0;
7934
7935 /* invalidate buflocals looping through events */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007936 for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
7937 event = (event_T)((int)event + 1))
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007938 /* loop over all autocommand patterns */
7939 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
7940 if (ap->buflocal_nr == buf->b_fnum)
7941 {
7942 au_remove_pat(ap);
7943 if (p_verbose >= 6)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007944 {
7945 verbose_enter();
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007946 smsg((char_u *)
7947 _("auto-removing autocommand: %s <buffer=%d>"),
7948 event_nr2name(event), buf->b_fnum);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00007949 verbose_leave();
7950 }
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00007951 }
7952 au_cleanup();
7953}
7954
7955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 * Add an autocmd group name.
7957 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error.
7958 */
7959 static int
7960au_new_group(name)
7961 char_u *name;
7962{
7963 int i;
7964
7965 i = au_find_group(name);
7966 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */
7967 {
7968 /* First try using a free entry. */
7969 for (i = 0; i < augroups.ga_len; ++i)
7970 if (AUGROUP_NAME(i) == NULL)
7971 break;
7972 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL)
7973 return AUGROUP_ERROR;
7974
7975 AUGROUP_NAME(i) = vim_strsave(name);
7976 if (AUGROUP_NAME(i) == NULL)
7977 return AUGROUP_ERROR;
7978 if (i == augroups.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 ++augroups.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 }
7981
7982 return i;
7983}
7984
7985 static void
7986au_del_group(name)
7987 char_u *name;
7988{
7989 int i;
7990
7991 i = au_find_group(name);
7992 if (i == AUGROUP_ERROR) /* the group doesn't exist */
7993 EMSG2(_("E367: No such group: \"%s\""), name);
7994 else
7995 {
7996 vim_free(AUGROUP_NAME(i));
7997 AUGROUP_NAME(i) = NULL;
7998 }
7999}
8000
8001/*
8002 * Find the ID of an autocmd group name.
8003 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error.
8004 */
8005 static int
8006au_find_group(name)
8007 char_u *name;
8008{
8009 int i;
8010
8011 for (i = 0; i < augroups.ga_len; ++i)
8012 if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0)
8013 return i;
8014 return AUGROUP_ERROR;
8015}
8016
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008017/*
8018 * Return TRUE if augroup "name" exists.
8019 */
8020 int
8021au_has_group(name)
8022 char_u *name;
8023{
8024 return au_find_group(name) != AUGROUP_ERROR;
8025}
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00008026
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027/*
8028 * ":augroup {name}".
8029 */
8030 void
8031do_augroup(arg, del_group)
8032 char_u *arg;
8033 int del_group;
8034{
8035 int i;
8036
8037 if (del_group)
8038 {
8039 if (*arg == NUL)
8040 EMSG(_(e_argreq));
8041 else
8042 au_del_group(arg);
8043 }
8044 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */
8045 current_augroup = AUGROUP_DEFAULT;
8046 else if (*arg) /* ":aug xxx": switch to group xxx */
8047 {
8048 i = au_new_group(arg);
8049 if (i != AUGROUP_ERROR)
8050 current_augroup = i;
8051 }
8052 else /* ":aug": list the group names */
8053 {
8054 msg_start();
8055 for (i = 0; i < augroups.ga_len; ++i)
8056 {
8057 if (AUGROUP_NAME(i) != NULL)
8058 {
8059 msg_puts(AUGROUP_NAME(i));
8060 msg_puts((char_u *)" ");
8061 }
8062 }
8063 msg_clr_eos();
8064 msg_end();
8065 }
8066}
8067
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008068#if defined(EXITFREE) || defined(PROTO)
8069 void
8070free_all_autocmds()
8071{
8072 for (current_augroup = -1; current_augroup < augroups.ga_len;
8073 ++current_augroup)
8074 do_autocmd((char_u *)"", TRUE);
8075 ga_clear_strings(&augroups);
8076}
8077#endif
8078
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079/*
8080 * Return the event number for event name "start".
8081 * Return NUM_EVENTS if the event name was not found.
8082 * Return a pointer to the next event name in "end".
8083 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008084 static event_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085event_name2nr(start, end)
8086 char_u *start;
8087 char_u **end;
8088{
8089 char_u *p;
8090 int i;
8091 int len;
8092
8093 /* the event name ends with end of line, a blank or a comma */
8094 for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p)
8095 ;
8096 for (i = 0; event_names[i].name != NULL; ++i)
8097 {
8098 len = (int)STRLEN(event_names[i].name);
8099 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0)
8100 break;
8101 }
8102 if (*p == ',')
8103 ++p;
8104 *end = p;
8105 if (event_names[i].name == NULL)
8106 return NUM_EVENTS;
8107 return event_names[i].event;
8108}
8109
8110/*
8111 * Return the name for event "event".
8112 */
8113 static char_u *
8114event_nr2name(event)
Bram Moolenaar754b5602006-02-09 23:53:20 +00008115 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 int i;
8118
8119 for (i = 0; event_names[i].name != NULL; ++i)
8120 if (event_names[i].event == event)
8121 return (char_u *)event_names[i].name;
8122 return (char_u *)"Unknown";
8123}
8124
8125/*
8126 * Scan over the events. "*" stands for all events.
8127 */
8128 static char_u *
8129find_end_event(arg, have_group)
8130 char_u *arg;
8131 int have_group; /* TRUE when group name was found */
8132{
8133 char_u *pat;
8134 char_u *p;
8135
8136 if (*arg == '*')
8137 {
8138 if (arg[1] && !vim_iswhite(arg[1]))
8139 {
8140 EMSG2(_("E215: Illegal character after *: %s"), arg);
8141 return NULL;
8142 }
8143 pat = arg + 1;
8144 }
8145 else
8146 {
8147 for (pat = arg; *pat && !vim_iswhite(*pat); pat = p)
8148 {
8149 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS)
8150 {
8151 if (have_group)
8152 EMSG2(_("E216: No such event: %s"), pat);
8153 else
8154 EMSG2(_("E216: No such group or event: %s"), pat);
8155 return NULL;
8156 }
8157 }
8158 }
8159 return pat;
8160}
8161
8162/*
8163 * Return TRUE if "event" is included in 'eventignore'.
8164 */
8165 static int
8166event_ignored(event)
Bram Moolenaar754b5602006-02-09 23:53:20 +00008167 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
8169 char_u *p = p_ei;
8170
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008171 while (*p != NUL)
8172 {
8173 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ','))
8174 return TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 if (event_name2nr(p, &p) == event)
8176 return TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178
8179 return FALSE;
8180}
8181
8182/*
8183 * Return OK when the contents of p_ei is valid, FAIL otherwise.
8184 */
8185 int
8186check_ei()
8187{
8188 char_u *p = p_ei;
8189
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 while (*p)
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008191 {
8192 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ','))
8193 {
8194 p += 3;
8195 if (*p == ',')
8196 ++p;
8197 }
8198 else if (event_name2nr(p, &p) == NUM_EVENTS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 return FAIL;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201
8202 return OK;
8203}
8204
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00008205# if defined(FEAT_SYN_HL) || defined(PROTO)
8206
8207/*
8208 * Add "what" to 'eventignore' to skip loading syntax highlighting for every
8209 * buffer loaded into the window. "what" must start with a comma.
8210 * Returns the old value of 'eventignore' in allocated memory.
8211 */
8212 char_u *
8213au_event_disable(what)
8214 char *what;
8215{
8216 char_u *new_ei;
8217 char_u *save_ei;
8218
8219 save_ei = vim_strsave(p_ei);
8220 if (save_ei != NULL)
8221 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00008222 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what)));
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00008223 if (new_ei != NULL)
8224 {
Bram Moolenaar8cac9fd2010-03-02 12:48:05 +01008225 if (*what == ',' && *p_ei == NUL)
8226 STRCPY(new_ei, what + 1);
8227 else
8228 STRCAT(new_ei, what);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008229 set_string_option_direct((char_u *)"ei", -1, new_ei,
8230 OPT_FREE, SID_NONE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00008231 vim_free(new_ei);
8232 }
8233 }
8234 return save_ei;
8235}
8236
8237 void
8238au_event_restore(old_ei)
8239 char_u *old_ei;
8240{
8241 if (old_ei != NULL)
8242 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00008243 set_string_option_direct((char_u *)"ei", -1, old_ei,
8244 OPT_FREE, SID_NONE);
Bram Moolenaardcaf10e2005-01-21 11:55:25 +00008245 vim_free(old_ei);
8246 }
8247}
8248# endif /* FEAT_SYN_HL */
8249
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250/*
8251 * do_autocmd() -- implements the :autocmd command. Can be used in the
8252 * following ways:
8253 *
8254 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that
8255 * will be automatically executed for <event>
8256 * when editing a file matching <pat>, in
8257 * the current group.
8258 * :autocmd <event> <pat> Show the auto-commands associated with
8259 * <event> and <pat>.
8260 * :autocmd <event> Show the auto-commands associated with
8261 * <event>.
8262 * :autocmd Show all auto-commands.
8263 * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with
8264 * <event> and <pat>, and add the command
8265 * <cmd>, for the current group.
8266 * :autocmd! <event> <pat> Remove all auto-commands associated with
8267 * <event> and <pat> for the current group.
8268 * :autocmd! <event> Remove all auto-commands associated with
8269 * <event> for the current group.
8270 * :autocmd! Remove ALL auto-commands for the current
8271 * group.
8272 *
8273 * Multiple events and patterns may be given separated by commas. Here are
8274 * some examples:
8275 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic
8276 * :autocmd bufleave * set tw=79 nosmartindent ic infercase
8277 *
8278 * :autocmd * *.c show all autocommands for *.c files.
Bram Moolenaard35f9712005-12-18 22:02:33 +00008279 *
8280 * Mostly a {group} argument can optionally appear before <event>.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 */
8282 void
8283do_autocmd(arg, forceit)
8284 char_u *arg;
8285 int forceit;
8286{
8287 char_u *pat;
8288 char_u *envpat = NULL;
8289 char_u *cmd;
Bram Moolenaar754b5602006-02-09 23:53:20 +00008290 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 int need_free = FALSE;
8292 int nested = FALSE;
8293 int group;
8294
8295 /*
8296 * Check for a legal group name. If not, use AUGROUP_ALL.
8297 */
8298 group = au_get_grouparg(&arg);
8299 if (arg == NULL) /* out of memory */
8300 return;
8301
8302 /*
8303 * Scan over the events.
8304 * If we find an illegal name, return here, don't do anything.
8305 */
8306 pat = find_end_event(arg, group != AUGROUP_ALL);
8307 if (pat == NULL)
8308 return;
8309
8310 /*
8311 * Scan over the pattern. Put a NUL at the end.
8312 */
8313 pat = skipwhite(pat);
8314 cmd = pat;
8315 while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\'))
8316 cmd++;
8317 if (*cmd)
8318 *cmd++ = NUL;
8319
8320 /* Expand environment variables in the pattern. Set 'shellslash', we want
8321 * forward slashes here. */
8322 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL)
8323 {
8324#ifdef BACKSLASH_IN_FILENAME
8325 int p_ssl_save = p_ssl;
8326
8327 p_ssl = TRUE;
8328#endif
8329 envpat = expand_env_save(pat);
8330#ifdef BACKSLASH_IN_FILENAME
8331 p_ssl = p_ssl_save;
8332#endif
8333 if (envpat != NULL)
8334 pat = envpat;
8335 }
8336
8337 /*
8338 * Check for "nested" flag.
8339 */
8340 cmd = skipwhite(cmd);
8341 if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6]))
8342 {
8343 nested = TRUE;
8344 cmd = skipwhite(cmd + 6);
8345 }
8346
8347 /*
8348 * Find the start of the commands.
8349 * Expand <sfile> in it.
8350 */
8351 if (*cmd != NUL)
8352 {
8353 cmd = expand_sfile(cmd);
8354 if (cmd == NULL) /* some error */
8355 return;
8356 need_free = TRUE;
8357 }
8358
8359 /*
8360 * Print header when showing autocommands.
8361 */
8362 if (!forceit && *cmd == NUL)
8363 {
8364 /* Highlight title */
8365 MSG_PUTS_TITLE(_("\n--- Auto-Commands ---"));
8366 }
8367
8368 /*
8369 * Loop over the events.
8370 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008371 last_event = (event_T)-1; /* for listing the event name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 last_group = AUGROUP_ERROR; /* for listing the group name */
8373 if (*arg == '*' || *arg == NUL)
8374 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00008375 for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
8376 event = (event_T)((int)event + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 if (do_autocmd_event(event, pat,
8378 nested, cmd, forceit, group) == FAIL)
8379 break;
8380 }
8381 else
8382 {
8383 while (*arg && !vim_iswhite(*arg))
8384 if (do_autocmd_event(event_name2nr(arg, &arg), pat,
8385 nested, cmd, forceit, group) == FAIL)
8386 break;
8387 }
8388
8389 if (need_free)
8390 vim_free(cmd);
8391 vim_free(envpat);
8392}
8393
8394/*
8395 * Find the group ID in a ":autocmd" or ":doautocmd" argument.
8396 * The "argp" argument is advanced to the following argument.
8397 *
8398 * Returns the group ID, AUGROUP_ERROR for error (out of memory).
8399 */
8400 static int
8401au_get_grouparg(argp)
8402 char_u **argp;
8403{
8404 char_u *group_name;
8405 char_u *p;
8406 char_u *arg = *argp;
8407 int group = AUGROUP_ALL;
8408
8409 p = skiptowhite(arg);
8410 if (p > arg)
8411 {
8412 group_name = vim_strnsave(arg, (int)(p - arg));
8413 if (group_name == NULL) /* out of memory */
8414 return AUGROUP_ERROR;
8415 group = au_find_group(group_name);
8416 if (group == AUGROUP_ERROR)
8417 group = AUGROUP_ALL; /* no match, use all groups */
8418 else
8419 *argp = skipwhite(p); /* match, skip over group name */
8420 vim_free(group_name);
8421 }
8422 return group;
8423}
8424
8425/*
8426 * do_autocmd() for one event.
8427 * If *pat == NUL do for all patterns.
8428 * If *cmd == NUL show entries.
8429 * If forceit == TRUE delete entries.
8430 * If group is not AUGROUP_ALL, only use this group.
8431 */
8432 static int
8433do_autocmd_event(event, pat, nested, cmd, forceit, group)
Bram Moolenaar754b5602006-02-09 23:53:20 +00008434 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 char_u *pat;
8436 int nested;
8437 char_u *cmd;
8438 int forceit;
8439 int group;
8440{
8441 AutoPat *ap;
8442 AutoPat **prev_ap;
8443 AutoCmd *ac;
8444 AutoCmd **prev_ac;
8445 int brace_level;
8446 char_u *endpat;
8447 int findgroup;
8448 int allgroups;
8449 int patlen;
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008450 int is_buflocal;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008451 int buflocal_nr;
8452 char_u buflocal_pat[25]; /* for "<buffer=X>" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453
8454 if (group == AUGROUP_ALL)
8455 findgroup = current_augroup;
8456 else
8457 findgroup = group;
8458 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL);
8459
8460 /*
8461 * Show or delete all patterns for an event.
8462 */
8463 if (*pat == NUL)
8464 {
8465 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
8466 {
8467 if (forceit) /* delete the AutoPat, if it's in the current group */
8468 {
8469 if (ap->group == findgroup)
8470 au_remove_pat(ap);
8471 }
8472 else if (group == AUGROUP_ALL || ap->group == group)
8473 show_autocmd(ap, event);
8474 }
8475 }
8476
8477 /*
8478 * Loop through all the specified patterns.
8479 */
8480 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat))
8481 {
8482 /*
8483 * Find end of the pattern.
8484 * Watch out for a comma in braces, like "*.\{obj,o\}".
8485 */
8486 brace_level = 0;
8487 for (endpat = pat; *endpat && (*endpat != ',' || brace_level
8488 || endpat[-1] == '\\'); ++endpat)
8489 {
8490 if (*endpat == '{')
8491 brace_level++;
8492 else if (*endpat == '}')
8493 brace_level--;
8494 }
8495 if (pat == endpat) /* ignore single comma */
8496 continue;
8497 patlen = (int)(endpat - pat);
8498
8499 /*
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008500 * detect special <buflocal[=X]> buffer-local patterns
8501 */
8502 is_buflocal = FALSE;
8503 buflocal_nr = 0;
8504
8505 if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0
8506 && pat[patlen - 1] == '>')
8507 {
8508 /* Error will be printed only for addition. printing and removing
8509 * will proceed silently. */
8510 is_buflocal = TRUE;
8511 if (patlen == 8)
8512 buflocal_nr = curbuf->b_fnum;
8513 else if (patlen > 9 && pat[7] == '=')
8514 {
8515 /* <buffer=abuf> */
8516 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13))
8517 buflocal_nr = autocmd_bufnr;
8518 /* <buffer=123> */
8519 else if (skipdigits(pat + 8) == pat + patlen - 1)
8520 buflocal_nr = atoi((char *)pat + 8);
8521 }
8522 }
8523
8524 if (is_buflocal)
8525 {
8526 /* normalize pat into standard "<buffer>#N" form */
8527 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr);
8528 pat = buflocal_pat; /* can modify pat and patlen */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008529 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008530 }
8531
8532 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 * Find AutoPat entries with this pattern.
8534 */
8535 prev_ap = &first_autopat[(int)event];
8536 while ((ap = *prev_ap) != NULL)
8537 {
8538 if (ap->pat != NULL)
8539 {
8540 /* Accept a pattern when:
8541 * - a group was specified and it's that group, or a group was
8542 * not specified and it's the current group, or a group was
8543 * not specified and we are listing
8544 * - the length of the pattern matches
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008545 * - the pattern matches.
8546 * For <buffer[=X]>, this condition works because we normalize
8547 * all buffer-local patterns.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 */
8549 if ((allgroups || ap->group == findgroup)
8550 && ap->patlen == patlen
8551 && STRNCMP(pat, ap->pat, patlen) == 0)
8552 {
8553 /*
8554 * Remove existing autocommands.
8555 * If adding any new autocmd's for this AutoPat, don't
8556 * delete the pattern from the autopat list, append to
8557 * this list.
8558 */
8559 if (forceit)
8560 {
8561 if (*cmd != NUL && ap->next == NULL)
8562 {
8563 au_remove_cmds(ap);
8564 break;
8565 }
8566 au_remove_pat(ap);
8567 }
8568
8569 /*
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008570 * Show autocmd's for this autopat, or buflocals <buffer=X>
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 */
8572 else if (*cmd == NUL)
8573 show_autocmd(ap, event);
8574
8575 /*
8576 * Add autocmd to this autopat, if it's the last one.
8577 */
8578 else if (ap->next == NULL)
8579 break;
8580 }
8581 }
8582 prev_ap = &ap->next;
8583 }
8584
8585 /*
8586 * Add a new command.
8587 */
8588 if (*cmd != NUL)
8589 {
8590 /*
8591 * If the pattern we want to add a command to does appear at the
8592 * end of the list (or not is not in the list at all), add the
8593 * pattern at the end of the list.
8594 */
8595 if (ap == NULL)
8596 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008597 /* refuse to add buffer-local ap if buffer number is invalid */
8598 if (is_buflocal && (buflocal_nr == 0
8599 || buflist_findnr(buflocal_nr) == NULL))
8600 {
8601 EMSGN(_("E680: <buffer=%d>: invalid buffer number "),
8602 buflocal_nr);
8603 return FAIL;
8604 }
8605
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat));
8607 if (ap == NULL)
8608 return FAIL;
8609 ap->pat = vim_strnsave(pat, patlen);
8610 ap->patlen = patlen;
8611 if (ap->pat == NULL)
8612 {
8613 vim_free(ap);
8614 return FAIL;
8615 }
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008616
8617 if (is_buflocal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008619 ap->buflocal_nr = buflocal_nr;
Bram Moolenaar748bf032005-02-02 23:04:36 +00008620 ap->reg_prog = NULL;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008621 }
8622 else
8623 {
Bram Moolenaar748bf032005-02-02 23:04:36 +00008624 char_u *reg_pat;
8625
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008626 ap->buflocal_nr = 0;
Bram Moolenaar748bf032005-02-02 23:04:36 +00008627 reg_pat = file_pat_to_reg_pat(pat, endpat,
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008628 &ap->allow_dirs, TRUE);
Bram Moolenaar748bf032005-02-02 23:04:36 +00008629 if (reg_pat != NULL)
8630 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar0a5fe212005-06-24 23:01:23 +00008631 vim_free(reg_pat);
Bram Moolenaar748bf032005-02-02 23:04:36 +00008632 if (reg_pat == NULL || ap->reg_prog == NULL)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00008633 {
8634 vim_free(ap->pat);
8635 vim_free(ap);
8636 return FAIL;
8637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 }
8639 ap->cmds = NULL;
8640 *prev_ap = ap;
8641 ap->next = NULL;
8642 if (group == AUGROUP_ALL)
8643 ap->group = current_augroup;
8644 else
8645 ap->group = group;
8646 }
8647
8648 /*
8649 * Add the autocmd at the end of the AutoCmd list.
8650 */
8651 prev_ac = &(ap->cmds);
8652 while ((ac = *prev_ac) != NULL)
8653 prev_ac = &ac->next;
8654 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd));
8655 if (ac == NULL)
8656 return FAIL;
8657 ac->cmd = vim_strsave(cmd);
8658#ifdef FEAT_EVAL
8659 ac->scriptID = current_SID;
8660#endif
8661 if (ac->cmd == NULL)
8662 {
8663 vim_free(ac);
8664 return FAIL;
8665 }
8666 ac->next = NULL;
8667 *prev_ac = ac;
8668 ac->nested = nested;
8669 }
8670 }
8671
8672 au_cleanup(); /* may really delete removed patterns/commands now */
8673 return OK;
8674}
8675
8676/*
8677 * Implementation of ":doautocmd [group] event [fname]".
8678 * Return OK for success, FAIL for failure;
8679 */
8680 int
8681do_doautocmd(arg, do_msg)
8682 char_u *arg;
8683 int do_msg; /* give message for no matching autocmds? */
8684{
8685 char_u *fname;
8686 int nothing_done = TRUE;
8687 int group;
8688
8689 /*
8690 * Check for a legal group name. If not, use AUGROUP_ALL.
8691 */
8692 group = au_get_grouparg(&arg);
8693 if (arg == NULL) /* out of memory */
8694 return FAIL;
8695
8696 if (*arg == '*')
8697 {
8698 EMSG(_("E217: Can't execute autocommands for ALL events"));
8699 return FAIL;
8700 }
8701
8702 /*
8703 * Scan over the events.
8704 * If we find an illegal name, return here, don't do anything.
8705 */
8706 fname = find_end_event(arg, group != AUGROUP_ALL);
8707 if (fname == NULL)
8708 return FAIL;
8709
8710 fname = skipwhite(fname);
8711
8712 /*
8713 * Loop over the events.
8714 */
8715 while (*arg && !vim_iswhite(*arg))
8716 if (apply_autocmds_group(event_name2nr(arg, &arg),
8717 fname, NULL, TRUE, group, curbuf, NULL))
8718 nothing_done = FALSE;
8719
8720 if (nothing_done && do_msg)
8721 MSG(_("No matching autocommands"));
8722
8723#ifdef FEAT_EVAL
8724 return aborting() ? FAIL : OK;
8725#else
8726 return OK;
8727#endif
8728}
8729
8730/*
8731 * ":doautoall": execute autocommands for each loaded buffer.
8732 */
8733 void
8734ex_doautoall(eap)
8735 exarg_T *eap;
8736{
8737 int retval;
8738 aco_save_T aco;
8739 buf_T *buf;
Bram Moolenaara61d5fb2012-02-12 00:18:58 +01008740 char_u *arg = eap->arg;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01008741 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742
8743 /*
8744 * This is a bit tricky: For some commands curwin->w_buffer needs to be
8745 * equal to curbuf, but for some buffers there may not be a window.
8746 * So we change the buffer for the current window for a moment. This
8747 * gives problems when the autocommands make changes to the list of
8748 * buffers or windows...
8749 */
8750 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8751 {
Bram Moolenaar3a847972008-07-08 09:36:58 +00008752 if (buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 {
8754 /* find a window for this buffer and save some values */
8755 aucmd_prepbuf(&aco, buf);
8756
8757 /* execute the autocommands for this buffer */
Bram Moolenaara61d5fb2012-02-12 00:18:58 +01008758 retval = do_doautocmd(arg, FALSE);
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008759
Bram Moolenaara61d5fb2012-02-12 00:18:58 +01008760 if (call_do_modelines)
8761 {
8762 /* Execute the modeline settings, but don't set window-local
8763 * options if we are using the current window for another
8764 * buffer. */
8765 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0);
8766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767
8768 /* restore the current window */
8769 aucmd_restbuf(&aco);
8770
8771 /* stop if there is some error or buffer was deleted */
8772 if (retval == FAIL || !buf_valid(buf))
8773 break;
8774 }
8775 }
8776
8777 check_cursor(); /* just in case lines got deleted */
8778}
8779
8780/*
Bram Moolenaar60542ac2012-02-12 20:14:01 +01008781 * Check *argp for <nomodeline>. When it is present return FALSE, otherwise
8782 * return TRUE and advance *argp to after it.
8783 * Thus return TRUE when do_modelines() should be called.
8784 */
8785 int
8786check_nomodeline(argp)
8787 char_u **argp;
8788{
8789 if (STRNCMP(*argp, "<nomodeline>", 12) == 0)
8790 {
8791 *argp = skipwhite(*argp + 12);
8792 return FALSE;
8793 }
8794 return TRUE;
8795}
8796
8797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 * Prepare for executing autocommands for (hidden) buffer "buf".
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008799 * Search for a visible window containing the current buffer. If there isn't
8800 * one then use "aucmd_win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 * Set "curbuf" and "curwin" to match "buf".
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00008802 * When FEAT_AUTOCMD is not defined another version is used, see below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 */
8804 void
8805aucmd_prepbuf(aco, buf)
8806 aco_save_T *aco; /* structure to save values in */
8807 buf_T *buf; /* new curbuf */
8808{
8809 win_T *win;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008810#ifdef FEAT_WINDOWS
8811 int save_ea;
8812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813
8814 /* Find a window that is for the new buffer */
8815 if (buf == curbuf) /* be quick when buf is curbuf */
8816 win = curwin;
8817 else
8818#ifdef FEAT_WINDOWS
8819 for (win = firstwin; win != NULL; win = win->w_next)
8820 if (win->w_buffer == buf)
8821 break;
8822#else
8823 win = NULL;
8824#endif
8825
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008826 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall
8827 * back to using the current window. */
8828 if (win == NULL && aucmd_win == NULL)
8829 {
8830 win_alloc_aucmd_win();
8831 if (aucmd_win == NULL)
8832 win = curwin;
8833 }
Bram Moolenaar6bef63c2009-07-29 10:10:29 +00008834 if (win == NULL && aucmd_win_used)
8835 /* Strange recursive autocommand, fall back to using the current
8836 * window. Expect a few side effects... */
8837 win = curwin;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008838
8839 aco->save_curwin = curwin;
8840 aco->save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 if (win != NULL)
8842 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008843 /* There is a window for "buf" in the current tab page, make it the
8844 * curwin. This is preferred, it has the least side effects (esp. if
8845 * "buf" is curbuf). */
Bram Moolenaar6bef63c2009-07-29 10:10:29 +00008846 aco->use_aucmd_win = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 curwin = win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 }
8849 else
8850 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008851 /* There is no window for "buf", use "aucmd_win". To minimize the side
8852 * effects, insert it in a the current tab page.
8853 * Anything related to a window (e.g., setting folds) may have
8854 * unexpected results. */
Bram Moolenaar6bef63c2009-07-29 10:10:29 +00008855 aco->use_aucmd_win = TRUE;
8856 aucmd_win_used = TRUE;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00008857 aucmd_win->w_buffer = buf;
Bram Moolenaarcdddaa42010-06-07 23:07:44 +02008858 aucmd_win->w_s = &buf->b_s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 ++buf->b_nwindows;
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00008860 win_init_empty(aucmd_win); /* set cursor and topline to safe values */
Bram Moolenaar6bef63c2009-07-29 10:10:29 +00008861 vim_free(aucmd_win->w_localdir);
8862 aucmd_win->w_localdir = NULL;
8863
8864 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in
8865 * win_enter_ext(). */
8866 aucmd_win->w_localdir = NULL;
8867 aco->globaldir = globaldir;
8868 globaldir = NULL;
8869
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008871#ifdef FEAT_WINDOWS
Bram Moolenaar2bc76e62009-07-01 15:13:56 +00008872 /* Split the current window, put the aucmd_win in the upper half.
8873 * We don't want the BufEnter or WinEnter autocommands. */
8874 block_autocmds();
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008875 make_snapshot(SNAP_AUCMD_IDX);
8876 save_ea = p_ea;
8877 p_ea = FALSE;
8878 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0);
8879 (void)win_comp_pos(); /* recompute window positions */
8880 p_ea = save_ea;
Bram Moolenaar2bc76e62009-07-01 15:13:56 +00008881 unblock_autocmds();
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008882#endif
Bram Moolenaarf061e0b2009-06-24 15:32:01 +00008883 curwin = aucmd_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 curbuf = buf;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008886 aco->new_curwin = curwin;
8887 aco->new_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888}
8889
8890/*
8891 * Cleanup after executing autocommands for a (hidden) buffer.
8892 * Restore the window as it was (if possible).
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00008893 * When FEAT_AUTOCMD is not defined another version is used, see below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 */
8895 void
8896aucmd_restbuf(aco)
8897 aco_save_T *aco; /* structure holding saved values */
8898{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008899#ifdef FEAT_WINDOWS
8900 int dummy;
8901#endif
8902
Bram Moolenaar6bef63c2009-07-29 10:10:29 +00008903 if (aco->use_aucmd_win)
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008904 {
8905 --curbuf->b_nwindows;
8906#ifdef FEAT_WINDOWS
8907 /* Find "aucmd_win", it can't be closed, but it may be in another tab
Bram Moolenaar2bc76e62009-07-01 15:13:56 +00008908 * page. Do not trigger autocommands here. */
8909 block_autocmds();
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008910 if (curwin != aucmd_win)
8911 {
8912 tabpage_T *tp;
8913 win_T *wp;
8914
8915 FOR_ALL_TAB_WINDOWS(tp, wp)
8916 {
8917 if (wp == aucmd_win)
8918 {
8919 if (tp != curtab)
8920 goto_tabpage_tp(tp);
8921 win_goto(aucmd_win);
Bram Moolenaar28f29082012-02-11 23:45:37 +01008922 goto win_found;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008923 }
8924 }
8925 }
Bram Moolenaar28f29082012-02-11 23:45:37 +01008926win_found:
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008927
8928 /* Remove the window and frame from the tree of frames. */
8929 (void)winframe_remove(curwin, &dummy, NULL);
8930 win_remove(curwin, NULL);
Bram Moolenaar6bef63c2009-07-29 10:10:29 +00008931 aucmd_win_used = FALSE;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008932 last_status(FALSE); /* may need to remove last status line */
8933 restore_snapshot(SNAP_AUCMD_IDX, FALSE);
8934 (void)win_comp_pos(); /* recompute window positions */
Bram Moolenaar2bc76e62009-07-01 15:13:56 +00008935 unblock_autocmds();
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008936
8937 if (win_valid(aco->save_curwin))
8938 curwin = aco->save_curwin;
8939 else
8940 /* Hmm, original window disappeared. Just use the first one. */
8941 curwin = firstwin;
8942# ifdef FEAT_EVAL
8943 vars_clear(&aucmd_win->w_vars.dv_hashtab); /* free all w: variables */
Bram Moolenaar50daf402009-11-17 13:57:22 +00008944 hash_init(&aucmd_win->w_vars.dv_hashtab); /* re-use the hashtab */
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008945# endif
8946#else
8947 curwin = aco->save_curwin;
8948#endif
8949 curbuf = curwin->w_buffer;
8950
Bram Moolenaar6bef63c2009-07-29 10:10:29 +00008951 vim_free(globaldir);
8952 globaldir = aco->globaldir;
8953
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008954 /* the buffer contents may have changed */
8955 check_cursor();
8956 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
8957 {
8958 curwin->w_topline = curbuf->b_ml.ml_line_count;
8959#ifdef FEAT_DIFF
8960 curwin->w_topfill = 0;
8961#endif
8962 }
8963#if defined(FEAT_GUI)
8964 /* Hide the scrollbars from the aucmd_win and update. */
8965 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE);
8966 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE);
8967 gui_may_update_scrollbars();
8968#endif
8969 }
8970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 {
8972 /* restore curwin */
8973#ifdef FEAT_WINDOWS
8974 if (win_valid(aco->save_curwin))
8975#endif
8976 {
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008977 /* Restore the buffer which was previously edited by curwin, if
Bram Moolenaar6bef63c2009-07-29 10:10:29 +00008978 * it was changed, we are still the same window and the buffer is
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008979 * valid. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 if (curwin == aco->new_curwin
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008981 && curbuf != aco->new_curbuf
8982 && buf_valid(aco->new_curbuf)
8983 && aco->new_curbuf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 {
8985 --curbuf->b_nwindows;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00008986 curbuf = aco->new_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 curwin->w_buffer = curbuf;
8988 ++curbuf->b_nwindows;
8989 }
8990
8991 curwin = aco->save_curwin;
8992 curbuf = curwin->w_buffer;
8993 }
8994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995}
8996
8997static int autocmd_nested = FALSE;
8998
8999/*
9000 * Execute autocommands for "event" and file name "fname".
9001 * Return TRUE if some commands were executed.
9002 */
9003 int
9004apply_autocmds(event, fname, fname_io, force, buf)
Bram Moolenaar754b5602006-02-09 23:53:20 +00009005 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 char_u *fname; /* NULL or empty means use actual file name */
9007 char_u *fname_io; /* fname to use for <afile> on cmdline */
9008 int force; /* when TRUE, ignore autocmd_busy */
9009 buf_T *buf; /* buffer for <abuf> */
9010{
9011 return apply_autocmds_group(event, fname, fname_io, force,
9012 AUGROUP_ALL, buf, NULL);
9013}
9014
9015/*
9016 * Like apply_autocmds(), but with extra "eap" argument. This takes care of
9017 * setting v:filearg.
9018 */
9019 static int
9020apply_autocmds_exarg(event, fname, fname_io, force, buf, eap)
Bram Moolenaar754b5602006-02-09 23:53:20 +00009021 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 char_u *fname;
9023 char_u *fname_io;
9024 int force;
9025 buf_T *buf;
9026 exarg_T *eap;
9027{
9028 return apply_autocmds_group(event, fname, fname_io, force,
9029 AUGROUP_ALL, buf, eap);
9030}
9031
9032/*
9033 * Like apply_autocmds(), but handles the caller's retval. If the script
9034 * processing is being aborted or if retval is FAIL when inside a try
9035 * conditional, no autocommands are executed. If otherwise the autocommands
9036 * cause the script to be aborted, retval is set to FAIL.
9037 */
9038 int
9039apply_autocmds_retval(event, fname, fname_io, force, buf, retval)
Bram Moolenaar754b5602006-02-09 23:53:20 +00009040 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 char_u *fname; /* NULL or empty means use actual file name */
9042 char_u *fname_io; /* fname to use for <afile> on cmdline */
9043 int force; /* when TRUE, ignore autocmd_busy */
9044 buf_T *buf; /* buffer for <abuf> */
9045 int *retval; /* pointer to caller's retval */
9046{
9047 int did_cmd;
9048
Bram Moolenaar1e015462005-09-25 22:16:38 +00009049#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 if (should_abort(*retval))
9051 return FALSE;
Bram Moolenaar1e015462005-09-25 22:16:38 +00009052#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
9054 did_cmd = apply_autocmds_group(event, fname, fname_io, force,
9055 AUGROUP_ALL, buf, NULL);
Bram Moolenaar1e015462005-09-25 22:16:38 +00009056 if (did_cmd
9057#ifdef FEAT_EVAL
9058 && aborting()
9059#endif
9060 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 *retval = FAIL;
9062 return did_cmd;
9063}
9064
Bram Moolenaard35f9712005-12-18 22:02:33 +00009065/*
9066 * Return TRUE when there is a CursorHold autocommand defined.
9067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 int
9069has_cursorhold()
9070{
Bram Moolenaar754b5602006-02-09 23:53:20 +00009071 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY
9072 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073}
Bram Moolenaard35f9712005-12-18 22:02:33 +00009074
9075/*
9076 * Return TRUE if the CursorHold event can be triggered.
9077 */
9078 int
9079trigger_cursorhold()
9080{
Bram Moolenaar754b5602006-02-09 23:53:20 +00009081 int state;
9082
Bram Moolenaar05334432011-07-20 18:29:39 +02009083 if (!did_cursorhold
9084 && has_cursorhold()
9085 && !Recording
9086 && typebuf.tb_len == 0
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00009087#ifdef FEAT_INS_EXPAND
9088 && !ins_compl_active()
9089#endif
9090 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00009091 {
9092 state = get_real_state();
9093 if (state == NORMAL_BUSY || (state & INSERT) != 0)
9094 return TRUE;
9095 }
9096 return FALSE;
Bram Moolenaard35f9712005-12-18 22:02:33 +00009097}
Bram Moolenaar754b5602006-02-09 23:53:20 +00009098
9099/*
9100 * Return TRUE when there is a CursorMoved autocommand defined.
9101 */
9102 int
9103has_cursormoved()
9104{
9105 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL);
9106}
9107
9108/*
9109 * Return TRUE when there is a CursorMovedI autocommand defined.
9110 */
9111 int
9112has_cursormovedI()
9113{
9114 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
9115}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116
Bram Moolenaarf5876f12012-02-29 18:22:08 +01009117/*
9118 * Return TRUE when there is an InsertCharPre autocommand defined.
9119 */
9120 int
9121has_insertcharpre()
9122{
9123 return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL);
9124}
9125
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 static int
9127apply_autocmds_group(event, fname, fname_io, force, group, buf, eap)
Bram Moolenaar754b5602006-02-09 23:53:20 +00009128 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 char_u *fname; /* NULL or empty means use actual file name */
9130 char_u *fname_io; /* fname to use for <afile> on cmdline, NULL means
9131 use fname */
9132 int force; /* when TRUE, ignore autocmd_busy */
9133 int group; /* group ID, or AUGROUP_ALL */
9134 buf_T *buf; /* buffer for <abuf> */
9135 exarg_T *eap; /* command arguments */
9136{
9137 char_u *sfname = NULL; /* short file name */
9138 char_u *tail;
9139 int save_changed;
9140 buf_T *old_curbuf;
9141 int retval = FALSE;
9142 char_u *save_sourcing_name;
9143 linenr_T save_sourcing_lnum;
9144 char_u *save_autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009145 int save_autocmd_fname_full;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 int save_autocmd_bufnr;
9147 char_u *save_autocmd_match;
9148 int save_autocmd_busy;
9149 int save_autocmd_nested;
9150 static int nesting = 0;
9151 AutoPatCmd patcmd;
9152 AutoPat *ap;
9153#ifdef FEAT_EVAL
9154 scid_T save_current_SID;
9155 void *save_funccalp;
9156 char_u *save_cmdarg;
9157 long save_cmdbang;
9158#endif
9159 static int filechangeshell_busy = FALSE;
Bram Moolenaar05159a02005-02-26 23:04:13 +00009160#ifdef FEAT_PROFILE
9161 proftime_T wait_time;
9162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163
9164 /*
9165 * Quickly return if there are no autocommands for this event or
9166 * autocommands are blocked.
9167 */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00009168 if (first_autopat[(int)event] == NULL || autocmd_blocked > 0)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009169 goto BYPASS_AU;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170
9171 /*
9172 * When autocommands are busy, new autocommands are only executed when
9173 * explicitly enabled with the "nested" flag.
9174 */
9175 if (autocmd_busy && !(force || autocmd_nested))
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009176 goto BYPASS_AU;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177
9178#ifdef FEAT_EVAL
9179 /*
Bram Moolenaar7263a772007-05-10 17:35:54 +00009180 * Quickly return when immediately aborting on error, or when an interrupt
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 * occurred or an exception was thrown but not caught.
9182 */
9183 if (aborting())
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009184 goto BYPASS_AU;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185#endif
9186
9187 /*
9188 * FileChangedShell never nests, because it can create an endless loop.
9189 */
Bram Moolenaar56718732006-03-15 22:53:57 +00009190 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL
9191 || event == EVENT_FILECHANGEDSHELLPOST))
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009192 goto BYPASS_AU;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193
9194 /*
9195 * Ignore events in 'eventignore'.
9196 */
9197 if (event_ignored(event))
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009198 goto BYPASS_AU;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199
9200 /*
9201 * Allow nesting of autocommands, but restrict the depth, because it's
9202 * possible to create an endless loop.
9203 */
9204 if (nesting == 10)
9205 {
9206 EMSG(_("E218: autocommand nesting too deep"));
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009207 goto BYPASS_AU;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 }
9209
9210 /*
9211 * Check if these autocommands are disabled. Used when doing ":all" or
9212 * ":ball".
9213 */
9214 if ( (autocmd_no_enter
9215 && (event == EVENT_WINENTER || event == EVENT_BUFENTER))
9216 || (autocmd_no_leave
9217 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE)))
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009218 goto BYPASS_AU;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219
9220 /*
9221 * Save the autocmd_* variables and info about the current buffer.
9222 */
9223 save_autocmd_fname = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009224 save_autocmd_fname_full = autocmd_fname_full;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 save_autocmd_bufnr = autocmd_bufnr;
9226 save_autocmd_match = autocmd_match;
9227 save_autocmd_busy = autocmd_busy;
9228 save_autocmd_nested = autocmd_nested;
9229 save_changed = curbuf->b_changed;
9230 old_curbuf = curbuf;
9231
9232 /*
9233 * Set the file name to be used for <afile>.
Bram Moolenaara0174af2008-01-02 20:08:25 +00009234 * Make a copy to avoid that changing a buffer name or directory makes it
9235 * invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236 */
9237 if (fname_io == NULL)
9238 {
9239 if (fname != NULL && *fname != NUL)
9240 autocmd_fname = fname;
9241 else if (buf != NULL)
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009242 autocmd_fname = buf->b_ffname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 else
9244 autocmd_fname = NULL;
9245 }
9246 else
9247 autocmd_fname = fname_io;
Bram Moolenaara0174af2008-01-02 20:08:25 +00009248 if (autocmd_fname != NULL)
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009249 autocmd_fname = vim_strsave(autocmd_fname);
9250 autocmd_fname_full = FALSE; /* call FullName_save() later */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251
9252 /*
9253 * Set the buffer number to be used for <abuf>.
9254 */
9255 if (buf == NULL)
9256 autocmd_bufnr = 0;
9257 else
9258 autocmd_bufnr = buf->b_fnum;
9259
9260 /*
9261 * When the file name is NULL or empty, use the file name of buffer "buf".
9262 * Always use the full path of the file name to match with, in case
9263 * "allow_dirs" is set.
9264 */
9265 if (fname == NULL || *fname == NUL)
9266 {
9267 if (buf == NULL)
9268 fname = NULL;
9269 else
9270 {
9271#ifdef FEAT_SYN_HL
9272 if (event == EVENT_SYNTAX)
9273 fname = buf->b_p_syn;
9274 else
9275#endif
9276 if (event == EVENT_FILETYPE)
9277 fname = buf->b_p_ft;
9278 else
9279 {
9280 if (buf->b_sfname != NULL)
9281 sfname = vim_strsave(buf->b_sfname);
9282 fname = buf->b_ffname;
9283 }
9284 }
9285 if (fname == NULL)
9286 fname = (char_u *)"";
9287 fname = vim_strsave(fname); /* make a copy, so we can change it */
9288 }
9289 else
9290 {
9291 sfname = vim_strsave(fname);
Bram Moolenaar5135d462009-04-29 16:03:38 +00009292 /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID or
9293 * QuickFixCmd* */
Bram Moolenaar7c626922005-02-07 22:01:03 +00009294 if (event == EVENT_FILETYPE
9295 || event == EVENT_SYNTAX
Bram Moolenaar5135d462009-04-29 16:03:38 +00009296 || event == EVENT_FUNCUNDEFINED
Bram Moolenaar7c626922005-02-07 22:01:03 +00009297 || event == EVENT_REMOTEREPLY
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009298 || event == EVENT_SPELLFILEMISSING
Bram Moolenaar7c626922005-02-07 22:01:03 +00009299 || event == EVENT_QUICKFIXCMDPRE
9300 || event == EVENT_QUICKFIXCMDPOST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 fname = vim_strsave(fname);
9302 else
9303 fname = FullName_save(fname, FALSE);
9304 }
9305 if (fname == NULL) /* out of memory */
9306 {
9307 vim_free(sfname);
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009308 retval = FALSE;
9309 goto BYPASS_AU;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 }
9311
9312#ifdef BACKSLASH_IN_FILENAME
9313 /*
9314 * Replace all backslashes with forward slashes. This makes the
9315 * autocommand patterns portable between Unix and MS-DOS.
9316 */
9317 if (sfname != NULL)
9318 forward_slash(sfname);
9319 forward_slash(fname);
9320#endif
9321
9322#ifdef VMS
9323 /* remove version for correct match */
9324 if (sfname != NULL)
9325 vms_remove_version(sfname);
9326 vms_remove_version(fname);
9327#endif
9328
9329 /*
9330 * Set the name to be used for <amatch>.
9331 */
9332 autocmd_match = fname;
9333
9334
9335 /* Don't redraw while doing auto commands. */
9336 ++RedrawingDisabled;
9337 save_sourcing_name = sourcing_name;
9338 sourcing_name = NULL; /* don't free this one */
9339 save_sourcing_lnum = sourcing_lnum;
9340 sourcing_lnum = 0; /* no line number here */
9341
9342#ifdef FEAT_EVAL
9343 save_current_SID = current_SID;
9344
Bram Moolenaar05159a02005-02-26 23:04:13 +00009345# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +00009346 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00009347 prof_child_enter(&wait_time); /* doesn't count for the caller itself */
9348# endif
9349
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 /* Don't use local function variables, if called from a function */
9351 save_funccalp = save_funccal();
9352#endif
9353
9354 /*
9355 * When starting to execute autocommands, save the search patterns.
9356 */
9357 if (!autocmd_busy)
9358 {
9359 save_search_patterns();
9360 saveRedobuff();
9361 did_filetype = keep_filetype;
9362 }
9363
9364 /*
9365 * Note that we are applying autocmds. Some commands need to know.
9366 */
9367 autocmd_busy = TRUE;
9368 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL);
9369 ++nesting; /* see matching decrement below */
9370
9371 /* Remember that FileType was triggered. Used for did_filetype(). */
9372 if (event == EVENT_FILETYPE)
9373 did_filetype = TRUE;
9374
9375 tail = gettail(fname);
9376
9377 /* Find first autocommand that matches */
9378 patcmd.curpat = first_autopat[(int)event];
9379 patcmd.nextcmd = NULL;
9380 patcmd.group = group;
9381 patcmd.fname = fname;
9382 patcmd.sfname = sfname;
9383 patcmd.tail = tail;
9384 patcmd.event = event;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009385 patcmd.arg_bufnr = autocmd_bufnr;
9386 patcmd.next = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 auto_next_pat(&patcmd, FALSE);
9388
9389 /* found one, start executing the autocommands */
9390 if (patcmd.curpat != NULL)
9391 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009392 /* add to active_apc_list */
9393 patcmd.next = active_apc_list;
9394 active_apc_list = &patcmd;
9395
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396#ifdef FEAT_EVAL
9397 /* set v:cmdarg (only when there is a matching pattern) */
9398 save_cmdbang = get_vim_var_nr(VV_CMDBANG);
9399 if (eap != NULL)
9400 {
9401 save_cmdarg = set_cmdarg(eap, NULL);
9402 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit);
9403 }
9404 else
9405 save_cmdarg = NULL; /* avoid gcc warning */
9406#endif
9407 retval = TRUE;
9408 /* mark the last pattern, to avoid an endless loop when more patterns
9409 * are added when executing autocommands */
9410 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next)
9411 ap->last = FALSE;
9412 ap->last = TRUE;
9413 check_lnums(TRUE); /* make sure cursor and topline are valid */
9414 do_cmdline(NULL, getnextac, (void *)&patcmd,
9415 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
9416#ifdef FEAT_EVAL
9417 if (eap != NULL)
9418 {
9419 (void)set_cmdarg(NULL, save_cmdarg);
9420 set_vim_var_nr(VV_CMDBANG, save_cmdbang);
9421 }
9422#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009423 /* delete from active_apc_list */
9424 if (active_apc_list == &patcmd) /* just in case */
9425 active_apc_list = patcmd.next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 }
9427
9428 --RedrawingDisabled;
9429 autocmd_busy = save_autocmd_busy;
9430 filechangeshell_busy = FALSE;
9431 autocmd_nested = save_autocmd_nested;
9432 vim_free(sourcing_name);
9433 sourcing_name = save_sourcing_name;
9434 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaara0174af2008-01-02 20:08:25 +00009435 vim_free(autocmd_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 autocmd_fname = save_autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009437 autocmd_fname_full = save_autocmd_fname_full;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438 autocmd_bufnr = save_autocmd_bufnr;
9439 autocmd_match = save_autocmd_match;
9440#ifdef FEAT_EVAL
9441 current_SID = save_current_SID;
9442 restore_funccal(save_funccalp);
Bram Moolenaar05159a02005-02-26 23:04:13 +00009443# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +00009444 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +00009445 prof_child_exit(&wait_time);
9446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447#endif
9448 vim_free(fname);
9449 vim_free(sfname);
9450 --nesting; /* see matching increment above */
9451
9452 /*
9453 * When stopping to execute autocommands, restore the search patterns and
9454 * the redo buffer.
9455 */
9456 if (!autocmd_busy)
9457 {
9458 restore_search_patterns();
9459 restoreRedobuff();
9460 did_filetype = FALSE;
9461 }
9462
9463 /*
9464 * Some events don't set or reset the Changed flag.
9465 * Check if still in the same buffer!
9466 */
9467 if (curbuf == old_curbuf
9468 && (event == EVENT_BUFREADPOST
9469 || event == EVENT_BUFWRITEPOST
9470 || event == EVENT_FILEAPPENDPOST
9471 || event == EVENT_VIMLEAVE
9472 || event == EVENT_VIMLEAVEPRE))
9473 {
9474#ifdef FEAT_TITLE
9475 if (curbuf->b_changed != save_changed)
9476 need_maketitle = TRUE;
9477#endif
9478 curbuf->b_changed = save_changed;
9479 }
9480
9481 au_cleanup(); /* may really delete removed patterns/commands now */
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009482
9483BYPASS_AU:
9484 /* When wiping out a buffer make sure all its buffer-local autocommands
9485 * are deleted. */
9486 if (event == EVENT_BUFWIPEOUT && buf != NULL)
9487 aubuflocal_remove(buf);
9488
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 return retval;
9490}
9491
Bram Moolenaar78ab3312007-09-29 12:16:41 +00009492# ifdef FEAT_EVAL
9493static char_u *old_termresponse = NULL;
9494# endif
9495
9496/*
9497 * Block triggering autocommands until unblock_autocmd() is called.
9498 * Can be used recursively, so long as it's symmetric.
9499 */
9500 void
9501block_autocmds()
9502{
9503# ifdef FEAT_EVAL
9504 /* Remember the value of v:termresponse. */
9505 if (autocmd_blocked == 0)
9506 old_termresponse = get_vim_var_str(VV_TERMRESPONSE);
9507# endif
9508 ++autocmd_blocked;
9509}
9510
9511 void
9512unblock_autocmds()
9513{
9514 --autocmd_blocked;
9515
9516# ifdef FEAT_EVAL
9517 /* When v:termresponse was set while autocommands were blocked, trigger
9518 * the autocommands now. Esp. useful when executing a shell command
9519 * during startup (vimdiff). */
9520 if (autocmd_blocked == 0
9521 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse)
9522 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf);
9523# endif
9524}
9525
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526/*
9527 * Find next autocommand pattern that matches.
9528 */
9529 static void
9530auto_next_pat(apc, stop_at_last)
9531 AutoPatCmd *apc;
9532 int stop_at_last; /* stop when 'last' flag is set */
9533{
9534 AutoPat *ap;
9535 AutoCmd *cp;
9536 char_u *name;
9537 char *s;
9538
9539 vim_free(sourcing_name);
9540 sourcing_name = NULL;
9541
9542 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next)
9543 {
9544 apc->curpat = NULL;
9545
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009546 /* Only use a pattern when it has not been removed, has commands and
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009547 * the group matches. For buffer-local autocommands only check the
9548 * buffer number. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 if (ap->pat != NULL && ap->cmds != NULL
9550 && (apc->group == AUGROUP_ALL || apc->group == ap->group))
9551 {
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009552 /* execution-condition */
9553 if (ap->buflocal_nr == 0
Bram Moolenaar748bf032005-02-02 23:04:36 +00009554 ? (match_file_pat(NULL, ap->reg_prog, apc->fname,
9555 apc->sfname, apc->tail, ap->allow_dirs))
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009556 : ap->buflocal_nr == apc->arg_bufnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 {
9558 name = event_nr2name(apc->event);
9559 s = _("%s Auto commands for \"%s\"");
9560 sourcing_name = alloc((unsigned)(STRLEN(s)
9561 + STRLEN(name) + ap->patlen + 1));
9562 if (sourcing_name != NULL)
9563 {
9564 sprintf((char *)sourcing_name, s,
9565 (char *)name, (char *)ap->pat);
9566 if (p_verbose >= 8)
Bram Moolenaara04f10b2005-05-31 22:09:46 +00009567 {
9568 verbose_enter();
Bram Moolenaar051b7822005-05-19 21:00:46 +00009569 smsg((char_u *)_("Executing %s"), sourcing_name);
Bram Moolenaara04f10b2005-05-31 22:09:46 +00009570 verbose_leave();
9571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572 }
9573
9574 apc->curpat = ap;
9575 apc->nextcmd = ap->cmds;
9576 /* mark last command */
9577 for (cp = ap->cmds; cp->next != NULL; cp = cp->next)
9578 cp->last = FALSE;
9579 cp->last = TRUE;
9580 }
9581 line_breakcheck();
9582 if (apc->curpat != NULL) /* found a match */
9583 break;
9584 }
9585 if (stop_at_last && ap->last)
9586 break;
9587 }
9588}
9589
9590/*
9591 * Get next autocommand command.
9592 * Called by do_cmdline() to get the next line for ":if".
9593 * Returns allocated string, or NULL for end of autocommands.
9594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 static char_u *
9596getnextac(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009597 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009599 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600{
9601 AutoPatCmd *acp = (AutoPatCmd *)cookie;
9602 char_u *retval;
9603 AutoCmd *ac;
9604
9605 /* Can be called again after returning the last line. */
9606 if (acp->curpat == NULL)
9607 return NULL;
9608
9609 /* repeat until we find an autocommand to execute */
9610 for (;;)
9611 {
9612 /* skip removed commands */
9613 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL)
9614 if (acp->nextcmd->last)
9615 acp->nextcmd = NULL;
9616 else
9617 acp->nextcmd = acp->nextcmd->next;
9618
9619 if (acp->nextcmd != NULL)
9620 break;
9621
9622 /* at end of commands, find next pattern that matches */
9623 if (acp->curpat->last)
9624 acp->curpat = NULL;
9625 else
9626 acp->curpat = acp->curpat->next;
9627 if (acp->curpat != NULL)
9628 auto_next_pat(acp, TRUE);
9629 if (acp->curpat == NULL)
9630 return NULL;
9631 }
9632
9633 ac = acp->nextcmd;
9634
9635 if (p_verbose >= 9)
9636 {
Bram Moolenaara04f10b2005-05-31 22:09:46 +00009637 verbose_enter_scroll();
Bram Moolenaar051b7822005-05-19 21:00:46 +00009638 smsg((char_u *)_("autocommand %s"), ac->cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaara04f10b2005-05-31 22:09:46 +00009640 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 }
9642 retval = vim_strsave(ac->cmd);
9643 autocmd_nested = ac->nested;
9644#ifdef FEAT_EVAL
9645 current_SID = ac->scriptID;
9646#endif
9647 if (ac->last)
9648 acp->nextcmd = NULL;
9649 else
9650 acp->nextcmd = ac->next;
9651 return retval;
9652}
9653
9654/*
9655 * Return TRUE if there is a matching autocommand for "fname".
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009656 * To account for buffer-local autocommands, function needs to know
9657 * in which buffer the file will be opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 */
9659 int
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009660has_autocmd(event, sfname, buf)
Bram Moolenaar754b5602006-02-09 23:53:20 +00009661 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 char_u *sfname;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009663 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664{
9665 AutoPat *ap;
9666 char_u *fname;
9667 char_u *tail = gettail(sfname);
9668 int retval = FALSE;
9669
9670 fname = FullName_save(sfname, FALSE);
9671 if (fname == NULL)
9672 return FALSE;
9673
9674#ifdef BACKSLASH_IN_FILENAME
9675 /*
9676 * Replace all backslashes with forward slashes. This makes the
9677 * autocommand patterns portable between Unix and MS-DOS.
9678 */
9679 sfname = vim_strsave(sfname);
9680 if (sfname != NULL)
9681 forward_slash(sfname);
9682 forward_slash(fname);
9683#endif
9684
9685 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
9686 if (ap->pat != NULL && ap->cmds != NULL
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009687 && (ap->buflocal_nr == 0
Bram Moolenaar748bf032005-02-02 23:04:36 +00009688 ? match_file_pat(NULL, ap->reg_prog,
9689 fname, sfname, tail, ap->allow_dirs)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00009690 : buf != NULL && ap->buflocal_nr == buf->b_fnum
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009691 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692 {
9693 retval = TRUE;
9694 break;
9695 }
9696
9697 vim_free(fname);
9698#ifdef BACKSLASH_IN_FILENAME
9699 vim_free(sfname);
9700#endif
9701
9702 return retval;
9703}
9704
9705#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
9706/*
9707 * Function given to ExpandGeneric() to obtain the list of autocommand group
9708 * names.
9709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 char_u *
9711get_augroup_name(xp, idx)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009712 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 int idx;
9714{
9715 if (idx == augroups.ga_len) /* add "END" add the end */
9716 return (char_u *)"END";
9717 if (idx >= augroups.ga_len) /* end of list */
9718 return NULL;
9719 if (AUGROUP_NAME(idx) == NULL) /* skip deleted entries */
9720 return (char_u *)"";
9721 return AUGROUP_NAME(idx); /* return a name */
9722}
9723
9724static int include_groups = FALSE;
9725
9726 char_u *
9727set_context_in_autocmd(xp, arg, doautocmd)
9728 expand_T *xp;
9729 char_u *arg;
Bram Moolenaard812df62008-11-09 12:46:09 +00009730 int doautocmd; /* TRUE for :doauto*, FALSE for :autocmd */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731{
9732 char_u *p;
9733 int group;
9734
9735 /* check for a group name, skip it if present */
9736 include_groups = FALSE;
9737 p = arg;
9738 group = au_get_grouparg(&arg);
9739 if (group == AUGROUP_ERROR)
9740 return NULL;
9741 /* If there only is a group name that's what we expand. */
9742 if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1]))
9743 {
9744 arg = p;
9745 group = AUGROUP_ALL;
9746 }
9747
9748 /* skip over event name */
9749 for (p = arg; *p != NUL && !vim_iswhite(*p); ++p)
9750 if (*p == ',')
9751 arg = p + 1;
9752 if (*p == NUL)
9753 {
9754 if (group == AUGROUP_ALL)
9755 include_groups = TRUE;
9756 xp->xp_context = EXPAND_EVENTS; /* expand event name */
9757 xp->xp_pattern = arg;
9758 return NULL;
9759 }
9760
9761 /* skip over pattern */
9762 arg = skipwhite(p);
9763 while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\'))
9764 arg++;
9765 if (*arg)
9766 return arg; /* expand (next) command */
9767
9768 if (doautocmd)
9769 xp->xp_context = EXPAND_FILES; /* expand file names */
9770 else
9771 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */
9772 return NULL;
9773}
9774
9775/*
9776 * Function given to ExpandGeneric() to obtain the list of event names.
9777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778 char_u *
9779get_event_name(xp, idx)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009780 expand_T *xp UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 int idx;
9782{
9783 if (idx < augroups.ga_len) /* First list group names, if wanted */
9784 {
9785 if (!include_groups || AUGROUP_NAME(idx) == NULL)
9786 return (char_u *)""; /* skip deleted entries */
9787 return AUGROUP_NAME(idx); /* return a name */
9788 }
9789 return (char_u *)event_names[idx - augroups.ga_len].name;
9790}
9791
9792#endif /* FEAT_CMDL_COMPL */
9793
9794/*
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009795 * Return TRUE if autocmd is supported.
9796 */
9797 int
9798autocmd_supported(name)
9799 char_u *name;
9800{
9801 char_u *p;
9802
9803 return (event_name2nr(name, &p) != NUM_EVENTS);
9804}
9805
9806/*
Bram Moolenaar195d6352005-12-19 22:08:24 +00009807 * Return TRUE if an autocommand is defined for a group, event and
9808 * pattern: The group can be omitted to accept any group. "event" and "pattern"
9809 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept
9810 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted.
9811 * Used for:
9812 * exists("#Group") or
9813 * exists("#Group#Event") or
9814 * exists("#Group#Event#pat") or
9815 * exists("#Event") or
9816 * exists("#Event#pat")
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 */
9818 int
Bram Moolenaar195d6352005-12-19 22:08:24 +00009819au_exists(arg)
9820 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
Bram Moolenaar195d6352005-12-19 22:08:24 +00009822 char_u *arg_save;
9823 char_u *pattern = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824 char_u *event_name;
9825 char_u *p;
Bram Moolenaar754b5602006-02-09 23:53:20 +00009826 event_T event;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 AutoPat *ap;
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009828 buf_T *buflocal_buf = NULL;
Bram Moolenaar195d6352005-12-19 22:08:24 +00009829 int group;
9830 int retval = FALSE;
9831
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009832 /* Make a copy so that we can change the '#' chars to a NUL. */
Bram Moolenaar195d6352005-12-19 22:08:24 +00009833 arg_save = vim_strsave(arg);
9834 if (arg_save == NULL)
9835 return FALSE;
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00009836 p = vim_strchr(arg_save, '#');
Bram Moolenaar195d6352005-12-19 22:08:24 +00009837 if (p != NULL)
9838 *p++ = NUL;
9839
9840 /* First, look for an autocmd group name */
9841 group = au_find_group(arg_save);
9842 if (group == AUGROUP_ERROR)
9843 {
9844 /* Didn't match a group name, assume the first argument is an event. */
9845 group = AUGROUP_ALL;
9846 event_name = arg_save;
9847 }
9848 else
9849 {
9850 if (p == NULL)
9851 {
9852 /* "Group": group name is present and it's recognized */
9853 retval = TRUE;
9854 goto theend;
9855 }
9856
9857 /* Must be "Group#Event" or "Group#Event#pat". */
9858 event_name = p;
9859 p = vim_strchr(event_name, '#');
9860 if (p != NULL)
9861 *p++ = NUL; /* "Group#Event#pat" */
9862 }
9863
9864 pattern = p; /* "pattern" is NULL when there is no pattern */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865
9866 /* find the index (enum) for the event name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 event = event_name2nr(event_name, &p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009868
9869 /* return FALSE if the event name is not recognized */
Bram Moolenaar195d6352005-12-19 22:08:24 +00009870 if (event == NUM_EVENTS)
9871 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872
9873 /* Find the first autocommand for this event.
9874 * If there isn't any, return FALSE;
9875 * If there is one and no pattern given, return TRUE; */
9876 ap = first_autopat[(int)event];
9877 if (ap == NULL)
Bram Moolenaar195d6352005-12-19 22:08:24 +00009878 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009880 /* if pattern is "<buffer>", special handling is needed which uses curbuf */
9881 /* for pattern "<buffer=N>, fnamecmp() will work fine */
Bram Moolenaar5b7880d2009-09-11 15:24:31 +00009882 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009883 buflocal_buf = curbuf;
9884
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885 /* Check if there is an autocommand with the given pattern. */
9886 for ( ; ap != NULL; ap = ap->next)
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009887 /* only use a pattern when it has not been removed and has commands. */
9888 /* For buffer-local autocommands, fnamecmp() works fine. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 if (ap->pat != NULL && ap->cmds != NULL
Bram Moolenaar195d6352005-12-19 22:08:24 +00009890 && (group == AUGROUP_ALL || ap->group == group)
Bram Moolenaar5b7880d2009-09-11 15:24:31 +00009891 && (pattern == NULL
9892 || (buflocal_buf == NULL
9893 ? fnamecmp(ap->pat, pattern) == 0
9894 : ap->buflocal_nr == buflocal_buf->b_fnum)))
Bram Moolenaar195d6352005-12-19 22:08:24 +00009895 {
9896 retval = TRUE;
9897 break;
9898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899
Bram Moolenaar195d6352005-12-19 22:08:24 +00009900theend:
9901 vim_free(arg_save);
9902 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903}
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00009904
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00009905#else /* FEAT_AUTOCMD */
9906
9907/*
9908 * Prepare for executing commands for (hidden) buffer "buf".
9909 * This is the non-autocommand version, it simply saves "curbuf" and sets
9910 * "curbuf" and "curwin" to match "buf".
9911 */
9912 void
9913aucmd_prepbuf(aco, buf)
9914 aco_save_T *aco; /* structure to save values in */
9915 buf_T *buf; /* new curbuf */
9916{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009917 aco->save_curbuf = curbuf;
9918 --curbuf->b_nwindows;
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00009919 curbuf = buf;
9920 curwin->w_buffer = buf;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009921 ++curbuf->b_nwindows;
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00009922}
9923
9924/*
9925 * Restore after executing commands for a (hidden) buffer.
9926 * This is the non-autocommand version.
9927 */
9928 void
9929aucmd_restbuf(aco)
9930 aco_save_T *aco; /* structure holding saved values */
9931{
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009932 --curbuf->b_nwindows;
9933 curbuf = aco->save_curbuf;
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00009934 curwin->w_buffer = curbuf;
Bram Moolenaar746ebd32009-06-16 14:01:43 +00009935 ++curbuf->b_nwindows;
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00009936}
9937
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938#endif /* FEAT_AUTOCMD */
9939
Bram Moolenaarf30e74c2006-08-16 17:35:00 +00009940
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941#if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO)
9942/*
Bram Moolenaar748bf032005-02-02 23:04:36 +00009943 * Try matching a filename with a "pattern" ("prog" is NULL), or use the
9944 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling
9945 * vim_regcomp() often.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 * Used for autocommands and 'wildignore'.
9947 * Returns TRUE if there is a match, FALSE otherwise.
9948 */
9949 int
Bram Moolenaar748bf032005-02-02 23:04:36 +00009950match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951 char_u *pattern; /* pattern to match with */
Bram Moolenaar748bf032005-02-02 23:04:36 +00009952 regprog_T *prog; /* pre-compiled regprog or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953 char_u *fname; /* full path of file name */
9954 char_u *sfname; /* short file name or NULL */
9955 char_u *tail; /* tail of path */
9956 int allow_dirs; /* allow matching with dir */
9957{
9958 regmatch_T regmatch;
9959 int result = FALSE;
9960#ifdef FEAT_OSFILETYPE
9961 int no_pattern = FALSE; /* TRUE if check is filetype only */
9962 char_u *type_start;
9963 char_u c;
9964 int match = FALSE;
9965#endif
9966
9967#ifdef CASE_INSENSITIVE_FILENAME
9968 regmatch.rm_ic = TRUE; /* Always ignore case */
9969#else
9970 regmatch.rm_ic = FALSE; /* Don't ever ignore case */
9971#endif
9972#ifdef FEAT_OSFILETYPE
9973 if (*pattern == '<')
9974 {
9975 /* There is a filetype condition specified with this pattern.
9976 * Check the filetype matches first. If not, don't bother with the
9977 * pattern (set regprog to NULL).
9978 * Always use magic for the regexp.
9979 */
9980
9981 for (type_start = pattern + 1; (c = *pattern); pattern++)
9982 {
9983 if ((c == ';' || c == '>') && match == FALSE)
9984 {
9985 *pattern = NUL; /* Terminate the string */
Bram Moolenaar05334432011-07-20 18:29:39 +02009986 /* TODO: match with 'filetype' of buffer that "fname" comes
9987 * from. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 match = mch_check_filetype(fname, type_start);
9989 *pattern = c; /* Restore the terminator */
9990 type_start = pattern + 1;
9991 }
9992 if (c == '>')
9993 break;
9994 }
9995
9996 /* (c should never be NUL, but check anyway) */
9997 if (match == FALSE || c == NUL)
9998 regmatch.regprog = NULL; /* Doesn't match - don't check pat. */
9999 else if (*pattern == NUL)
10000 {
10001 regmatch.regprog = NULL; /* Vim will try to free regprog later */
10002 no_pattern = TRUE; /* Always matches - don't check pat. */
10003 }
10004 else
10005 regmatch.regprog = vim_regcomp(pattern + 1, RE_MAGIC);
10006 }
10007 else
10008#endif
Bram Moolenaar748bf032005-02-02 23:04:36 +000010009 {
10010 if (prog != NULL)
10011 regmatch.regprog = prog;
10012 else
10013 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
10014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015
10016 /*
10017 * Try for a match with the pattern with:
10018 * 1. the full file name, when the pattern has a '/'.
10019 * 2. the short file name, when the pattern has a '/'.
10020 * 3. the tail of the file name, when the pattern has no '/'.
10021 */
10022 if (
10023#ifdef FEAT_OSFILETYPE
10024 /* If the check is for a filetype only and we don't care
10025 * about the path then skip all the regexp stuff.
10026 */
10027 no_pattern ||
10028#endif
10029 (regmatch.regprog != NULL
10030 && ((allow_dirs
10031 && (vim_regexec(&regmatch, fname, (colnr_T)0)
10032 || (sfname != NULL
10033 && vim_regexec(&regmatch, sfname, (colnr_T)0))))
10034 || (!allow_dirs && vim_regexec(&regmatch, tail, (colnr_T)0)))))
10035 result = TRUE;
10036
Bram Moolenaar748bf032005-02-02 23:04:36 +000010037 if (prog == NULL)
10038 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 return result;
10040}
10041#endif
10042
10043#if defined(FEAT_WILDIGN) || defined(PROTO)
10044/*
10045 * Return TRUE if a file matches with a pattern in "list".
10046 * "list" is a comma-separated list of patterns, like 'wildignore'.
10047 * "sfname" is the short file name or NULL, "ffname" the long file name.
10048 */
10049 int
10050match_file_list(list, sfname, ffname)
10051 char_u *list;
10052 char_u *sfname;
10053 char_u *ffname;
10054{
10055 char_u buf[100];
10056 char_u *tail;
10057 char_u *regpat;
10058 char allow_dirs;
10059 int match;
10060 char_u *p;
10061
10062 tail = gettail(sfname);
10063
10064 /* try all patterns in 'wildignore' */
10065 p = list;
10066 while (*p)
10067 {
10068 copy_option_part(&p, buf, 100, ",");
10069 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
10070 if (regpat == NULL)
10071 break;
Bram Moolenaar748bf032005-02-02 23:04:36 +000010072 match = match_file_pat(regpat, NULL, ffname, sfname,
10073 tail, (int)allow_dirs);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 vim_free(regpat);
10075 if (match)
10076 return TRUE;
10077 }
10078 return FALSE;
10079}
10080#endif
10081
10082/*
10083 * Convert the given pattern "pat" which has shell style wildcards in it, into
10084 * a regular expression, and return the result in allocated memory. If there
10085 * is a directory path separator to be matched, then TRUE is put in
10086 * allow_dirs, otherwise FALSE is put there -- webb.
10087 * Handle backslashes before special characters, like "\*" and "\ ".
10088 *
10089 * If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg:
10090 * '<html>myfile' becomes '<html>^myfile$' -- leonard.
10091 *
10092 * Returns NULL when out of memory.
10093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 char_u *
10095file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash)
10096 char_u *pat;
10097 char_u *pat_end; /* first char after pattern or NULL */
10098 char *allow_dirs; /* Result passed back out in here */
Bram Moolenaar78a15312009-05-15 19:33:18 +000010099 int no_bslash UNUSED; /* Don't use a backward slash as pathsep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 int size;
10102 char_u *endp;
10103 char_u *reg_pat;
10104 char_u *p;
10105 int i;
10106 int nested = 0;
10107 int add_dollar = TRUE;
10108#ifdef FEAT_OSFILETYPE
10109 int check_length = 0;
10110#endif
10111
10112 if (allow_dirs != NULL)
10113 *allow_dirs = FALSE;
10114 if (pat_end == NULL)
10115 pat_end = pat + STRLEN(pat);
10116
10117#ifdef FEAT_OSFILETYPE
10118 /* Find out how much of the string is the filetype check */
10119 if (*pat == '<')
10120 {
10121 /* Count chars until the next '>' */
10122 for (p = pat + 1; p < pat_end && *p != '>'; p++)
10123 ;
10124 if (p < pat_end)
10125 {
10126 /* Pattern is of the form <.*>.* */
10127 check_length = p - pat + 1;
10128 if (p + 1 >= pat_end)
10129 {
10130 /* The 'pattern' is a filetype check ONLY */
10131 reg_pat = (char_u *)alloc(check_length + 1);
10132 if (reg_pat != NULL)
10133 {
10134 mch_memmove(reg_pat, pat, (size_t)check_length);
10135 reg_pat[check_length] = NUL;
10136 }
10137 return reg_pat;
10138 }
10139 }
10140 /* else: there was no closing '>' - assume it was a normal pattern */
10141
10142 }
10143 pat += check_length;
10144 size = 2 + check_length;
10145#else
10146 size = 2; /* '^' at start, '$' at end */
10147#endif
10148
10149 for (p = pat; p < pat_end; p++)
10150 {
10151 switch (*p)
10152 {
10153 case '*':
10154 case '.':
10155 case ',':
10156 case '{':
10157 case '}':
10158 case '~':
10159 size += 2; /* extra backslash */
10160 break;
10161#ifdef BACKSLASH_IN_FILENAME
10162 case '\\':
10163 case '/':
10164 size += 4; /* could become "[\/]" */
10165 break;
10166#endif
10167 default:
10168 size++;
10169# ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010170 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 {
10172 ++p;
10173 ++size;
10174 }
10175# endif
10176 break;
10177 }
10178 }
10179 reg_pat = alloc(size + 1);
10180 if (reg_pat == NULL)
10181 return NULL;
10182
10183#ifdef FEAT_OSFILETYPE
10184 /* Copy the type check in to the start. */
10185 if (check_length)
10186 mch_memmove(reg_pat, pat - check_length, (size_t)check_length);
10187 i = check_length;
10188#else
10189 i = 0;
10190#endif
10191
10192 if (pat[0] == '*')
10193 while (pat[0] == '*' && pat < pat_end - 1)
10194 pat++;
10195 else
10196 reg_pat[i++] = '^';
10197 endp = pat_end - 1;
10198 if (*endp == '*')
10199 {
10200 while (endp - pat > 0 && *endp == '*')
10201 endp--;
10202 add_dollar = FALSE;
10203 }
10204 for (p = pat; *p && nested >= 0 && p <= endp; p++)
10205 {
10206 switch (*p)
10207 {
10208 case '*':
10209 reg_pat[i++] = '.';
10210 reg_pat[i++] = '*';
Bram Moolenaar02743632005-07-25 20:42:36 +000010211 while (p[1] == '*') /* "**" matches like "*" */
10212 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 break;
10214 case '.':
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 case '~':
10216 reg_pat[i++] = '\\';
10217 reg_pat[i++] = *p;
10218 break;
10219 case '?':
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 reg_pat[i++] = '.';
10221 break;
10222 case '\\':
10223 if (p[1] == NUL)
10224 break;
10225#ifdef BACKSLASH_IN_FILENAME
10226 if (!no_bslash)
10227 {
10228 /* translate:
10229 * "\x" to "\\x" e.g., "dir\file"
10230 * "\*" to "\\.*" e.g., "dir\*.c"
10231 * "\?" to "\\." e.g., "dir\??.c"
10232 * "\+" to "\+" e.g., "fileX\+.c"
10233 */
10234 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
10235 && p[1] != '+')
10236 {
10237 reg_pat[i++] = '[';
10238 reg_pat[i++] = '\\';
10239 reg_pat[i++] = '/';
10240 reg_pat[i++] = ']';
10241 if (allow_dirs != NULL)
10242 *allow_dirs = TRUE;
10243 break;
10244 }
10245 }
10246#endif
Bram Moolenaar8cd213c2010-06-01 21:57:09 +020010247 /* Undo escaping from ExpandEscape():
10248 * foo\?bar -> foo?bar
10249 * foo\%bar -> foo%bar
10250 * foo\,bar -> foo,bar
10251 * foo\ bar -> foo bar
10252 * Don't unescape \, * and others that are also special in a
10253 * regexp. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254 if (*++p == '?'
10255#ifdef BACKSLASH_IN_FILENAME
10256 && no_bslash
10257#endif
10258 )
10259 reg_pat[i++] = '?';
10260 else
Bram Moolenaar8cd213c2010-06-01 21:57:09 +020010261 if (*p == ',' || *p == '%' || *p == '#' || *p == ' ')
10262 reg_pat[i++] = *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263 else
10264 {
10265 if (allow_dirs != NULL && vim_ispathsep(*p)
10266#ifdef BACKSLASH_IN_FILENAME
10267 && (!no_bslash || *p != '\\')
10268#endif
10269 )
10270 *allow_dirs = TRUE;
10271 reg_pat[i++] = '\\';
10272 reg_pat[i++] = *p;
10273 }
10274 break;
10275#ifdef BACKSLASH_IN_FILENAME
10276 case '/':
10277 reg_pat[i++] = '[';
10278 reg_pat[i++] = '\\';
10279 reg_pat[i++] = '/';
10280 reg_pat[i++] = ']';
10281 if (allow_dirs != NULL)
10282 *allow_dirs = TRUE;
10283 break;
10284#endif
10285 case '{':
10286 reg_pat[i++] = '\\';
10287 reg_pat[i++] = '(';
10288 nested++;
10289 break;
10290 case '}':
10291 reg_pat[i++] = '\\';
10292 reg_pat[i++] = ')';
10293 --nested;
10294 break;
10295 case ',':
10296 if (nested)
10297 {
10298 reg_pat[i++] = '\\';
10299 reg_pat[i++] = '|';
10300 }
10301 else
10302 reg_pat[i++] = ',';
10303 break;
10304 default:
10305# ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010306 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307 reg_pat[i++] = *p++;
10308 else
10309# endif
10310 if (allow_dirs != NULL && vim_ispathsep(*p))
10311 *allow_dirs = TRUE;
10312 reg_pat[i++] = *p;
10313 break;
10314 }
10315 }
10316 if (add_dollar)
10317 reg_pat[i++] = '$';
10318 reg_pat[i] = NUL;
10319 if (nested != 0)
10320 {
10321 if (nested < 0)
10322 EMSG(_("E219: Missing {."));
10323 else
10324 EMSG(_("E220: Missing }."));
10325 vim_free(reg_pat);
10326 reg_pat = NULL;
10327 }
10328 return reg_pat;
10329}
Bram Moolenaar540fc6f2010-12-17 16:27:16 +010010330
10331#if defined(EINTR) || defined(PROTO)
10332/*
10333 * Version of read() that retries when interrupted by EINTR (possibly
10334 * by a SIGWINCH).
10335 */
10336 long
10337read_eintr(fd, buf, bufsize)
10338 int fd;
10339 void *buf;
10340 size_t bufsize;
10341{
10342 long ret;
10343
10344 for (;;)
10345 {
10346 ret = vim_read(fd, buf, bufsize);
10347 if (ret >= 0 || errno != EINTR)
10348 break;
10349 }
10350 return ret;
10351}
10352
10353/*
10354 * Version of write() that retries when interrupted by EINTR (possibly
10355 * by a SIGWINCH).
10356 */
10357 long
10358write_eintr(fd, buf, bufsize)
10359 int fd;
10360 void *buf;
10361 size_t bufsize;
10362{
10363 long ret = 0;
10364 long wlen;
10365
10366 /* Repeat the write() so long it didn't fail, other than being interrupted
10367 * by a signal. */
10368 while (ret < (long)bufsize)
10369 {
Bram Moolenaar9c263032010-12-17 18:06:06 +010010370 wlen = vim_write(fd, (char *)buf + ret, bufsize - ret);
Bram Moolenaar540fc6f2010-12-17 16:27:16 +010010371 if (wlen < 0)
10372 {
10373 if (errno != EINTR)
10374 break;
10375 }
10376 else
10377 ret += wlen;
10378 }
10379 return ret;
10380}
10381#endif