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