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