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