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