Mercurial > vim
annotate src/fileio.c @ 2281:e41433ea71df vim73
Added ":earlier 1f" and ":later 1f".
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 27 Jun 2010 05:18:54 +0200 |
parents | 941ff1cd317a |
children | ccda151dde4e |
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! */ |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
40 |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
41 /* For blowfish, after the magic header, we store 8 bytes of salt and then 8 |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
42 * bytes of seed (initialisation vector). */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
43 static int crypt_salt_len[] = {0, 8}; |
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
|
44 static int crypt_seed_len[] = {0, 8}; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
45 #define CRYPT_SALT_LEN_MAX 8 |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
46 #define CRYPT_SEED_LEN_MAX 8 |
7 | 47 #endif |
48 | |
49 /* Is there any system that doesn't have access()? */ | |
574 | 50 #define USE_MCH_ACCESS |
7 | 51 |
1313 | 52 #if defined(sun) && defined(S_ISCHR) |
53 # define OPEN_CHR_FILES | |
54 static int is_dev_fd_file(char_u *fname); | |
55 #endif | |
7 | 56 #ifdef FEAT_MBYTE |
57 static char_u *next_fenc __ARGS((char_u **pp)); | |
58 # ifdef FEAT_EVAL | |
59 static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp)); | |
60 # endif | |
61 #endif | |
62 #ifdef FEAT_VIMINFO | |
63 static void check_marks_read __ARGS((void)); | |
64 #endif | |
65 #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
|
66 static int get_crypt_method __ARGS((char *ptr, int len)); |
2267 | 67 static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, off_t *filesizep, int newfile, char_u *fname, int *did_ask)); |
7 | 68 #endif |
69 #ifdef UNIX | |
70 static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime)); | |
71 #endif | |
633 | 72 static int set_rw_fname __ARGS((char_u *fname, char_u *sfname)); |
7 | 73 static int msg_add_fileformat __ARGS((int eol_type)); |
74 static void msg_add_eol __ARGS((void)); | |
75 static int check_mtime __ARGS((buf_T *buf, struct stat *s)); | |
76 static int time_differs __ARGS((long t1, long t2)); | |
77 #ifdef FEAT_AUTOCMD | |
661 | 78 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 | 79 static int au_find_group __ARGS((char_u *name)); |
80 | |
81 # define AUGROUP_DEFAULT -1 /* default autocmd group */ | |
1834 | 82 # define AUGROUP_ERROR -2 /* erroneous autocmd group */ |
676 | 83 # define AUGROUP_ALL -3 /* all autocmd groups */ |
7 | 84 #endif |
85 | |
86 #if defined(FEAT_CRYPT) || defined(FEAT_MBYTE) | |
87 # define HAS_BW_FLAGS | |
88 # define FIO_LATIN1 0x01 /* convert Latin1 */ | |
89 # define FIO_UTF8 0x02 /* convert UTF-8 */ | |
90 # define FIO_UCS2 0x04 /* convert UCS-2 */ | |
91 # define FIO_UCS4 0x08 /* convert UCS-4 */ | |
92 # define FIO_UTF16 0x10 /* convert UTF-16 */ | |
93 # ifdef WIN3264 | |
94 # define FIO_CODEPAGE 0x20 /* convert MS-Windows codepage */ | |
95 # define FIO_PUT_CP(x) (((x) & 0xffff) << 16) /* put codepage in top word */ | |
96 # define FIO_GET_CP(x) (((x)>>16) & 0xffff) /* get codepage from top word */ | |
97 # endif | |
98 # ifdef MACOS_X | |
99 # define FIO_MACROMAN 0x20 /* convert MacRoman */ | |
100 # endif | |
101 # define FIO_ENDIAN_L 0x80 /* little endian */ | |
102 # define FIO_ENCRYPTED 0x1000 /* encrypt written bytes */ | |
103 # define FIO_NOCONVERT 0x2000 /* skip encoding conversion */ | |
104 # define FIO_UCSBOM 0x4000 /* check for BOM at start of file */ | |
105 # define FIO_ALL -1 /* allow all formats */ | |
106 #endif | |
107 | |
108 /* When converting, a read() or write() may leave some bytes to be converted | |
109 * for the next call. The value is guessed... */ | |
110 #define CONV_RESTLEN 30 | |
111 | |
112 /* We have to guess how much a sequence of bytes may expand when converting | |
113 * with iconv() to be able to allocate a buffer. */ | |
114 #define ICONV_MULT 8 | |
115 | |
116 /* | |
117 * Structure to pass arguments from buf_write() to buf_write_bytes(). | |
118 */ | |
119 struct bw_info | |
120 { | |
121 int bw_fd; /* file descriptor */ | |
122 char_u *bw_buf; /* buffer with data to be written */ | |
1411 | 123 int bw_len; /* length of data */ |
7 | 124 #ifdef HAS_BW_FLAGS |
125 int bw_flags; /* FIO_ flags */ | |
126 #endif | |
127 #ifdef FEAT_MBYTE | |
128 char_u bw_rest[CONV_RESTLEN]; /* not converted bytes */ | |
129 int bw_restlen; /* nr of bytes in bw_rest[] */ | |
130 int bw_first; /* first write call */ | |
131 char_u *bw_conv_buf; /* buffer for writing converted chars */ | |
132 int bw_conv_buflen; /* size of bw_conv_buf */ | |
133 int bw_conv_error; /* set for conversion error */ | |
1947 | 134 linenr_T bw_conv_error_lnum; /* first line with error or zero */ |
135 linenr_T bw_start_lnum; /* line number at start of buffer */ | |
7 | 136 # ifdef USE_ICONV |
137 iconv_t bw_iconv_fd; /* descriptor for iconv() or -1 */ | |
138 # endif | |
139 #endif | |
140 }; | |
141 | |
142 static int buf_write_bytes __ARGS((struct bw_info *ip)); | |
143 | |
144 #ifdef FEAT_MBYTE | |
595 | 145 static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp)); |
7 | 146 static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags)); |
1948 | 147 static int need_conversion __ARGS((char_u *fenc)); |
7 | 148 static int get_fio_flags __ARGS((char_u *ptr)); |
149 static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags)); | |
150 static int make_bom __ARGS((char_u *buf, char_u *name)); | |
151 # ifdef WIN3264 | |
152 static int get_win_fio_flags __ARGS((char_u *ptr)); | |
153 # endif | |
154 # ifdef MACOS_X | |
155 static int get_mac_fio_flags __ARGS((char_u *ptr)); | |
156 # endif | |
157 #endif | |
158 static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf)); | |
2006 | 159 #ifdef TEMPDIRNAMES |
1997 | 160 static void vim_settempdir __ARGS((char_u *tempdir)); |
2006 | 161 #endif |
1834 | 162 #ifdef FEAT_AUTOCMD |
163 static char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); | |
164 #endif | |
595 | 165 |
7 | 166 void |
167 filemess(buf, name, s, attr) | |
168 buf_T *buf; | |
169 char_u *name; | |
170 char_u *s; | |
171 int attr; | |
172 { | |
173 int msg_scroll_save; | |
174 | |
175 if (msg_silent != 0) | |
176 return; | |
177 msg_add_fname(buf, name); /* put file name in IObuff with quotes */ | |
178 /* If it's extremely long, truncate it. */ | |
179 if (STRLEN(IObuff) > IOSIZE - 80) | |
180 IObuff[IOSIZE - 80] = NUL; | |
181 STRCAT(IObuff, s); | |
182 /* | |
183 * For the first message may have to start a new line. | |
184 * For further ones overwrite the previous one, reset msg_scroll before | |
185 * calling filemess(). | |
186 */ | |
187 msg_scroll_save = msg_scroll; | |
188 if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0) | |
189 msg_scroll = FALSE; | |
190 if (!msg_scroll) /* wait a bit when overwriting an error msg */ | |
191 check_for_delay(FALSE); | |
192 msg_start(); | |
193 msg_scroll = msg_scroll_save; | |
194 msg_scrolled_ign = TRUE; | |
195 /* may truncate the message to avoid a hit-return prompt */ | |
196 msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr); | |
197 msg_clr_eos(); | |
198 out_flush(); | |
199 msg_scrolled_ign = FALSE; | |
200 } | |
201 | |
202 /* | |
203 * Read lines from file "fname" into the buffer after line "from". | |
204 * | |
205 * 1. We allocate blocks with lalloc, as big as possible. | |
206 * 2. Each block is filled with characters from the file with a single read(). | |
207 * 3. The lines are inserted in the buffer with ml_append(). | |
208 * | |
209 * (caller must check that fname != NULL, unless READ_STDIN is used) | |
210 * | |
211 * "lines_to_skip" is the number of lines that must be skipped | |
212 * "lines_to_read" is the number of lines that are appended | |
213 * When not recovering lines_to_skip is 0 and lines_to_read MAXLNUM. | |
214 * | |
215 * flags: | |
216 * READ_NEW starting to edit a new buffer | |
217 * READ_FILTER reading filter output | |
218 * READ_STDIN read from stdin instead of a file | |
219 * READ_BUFFER read from curbuf instead of a file (converting after reading | |
220 * stdin) | |
221 * READ_DUMMY read into a dummy buffer (to check if file contents changed) | |
222 * | |
223 * return FAIL for failure, OK otherwise | |
224 */ | |
225 int | |
226 readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags) | |
227 char_u *fname; | |
228 char_u *sfname; | |
229 linenr_T from; | |
230 linenr_T lines_to_skip; | |
231 linenr_T lines_to_read; | |
232 exarg_T *eap; /* can be NULL! */ | |
233 int flags; | |
234 { | |
235 int fd = 0; | |
236 int newfile = (flags & READ_NEW); | |
237 int check_readonly; | |
238 int filtering = (flags & READ_FILTER); | |
239 int read_stdin = (flags & READ_STDIN); | |
240 int read_buffer = (flags & READ_BUFFER); | |
1486 | 241 int set_options = newfile || read_buffer |
242 || (eap != NULL && eap->read_edit); | |
7 | 243 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
244 colnr_T read_buf_col = 0; /* next char to read from this line */ | |
245 char_u c; | |
246 linenr_T lnum = from; | |
247 char_u *ptr = NULL; /* pointer into read buffer */ | |
248 char_u *buffer = NULL; /* read buffer */ | |
249 char_u *new_buffer = NULL; /* init to shut up gcc */ | |
250 char_u *line_start = NULL; /* init to shut up gcc */ | |
251 int wasempty; /* buffer was empty before reading */ | |
252 colnr_T len; | |
253 long size = 0; | |
254 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
|
255 off_t filesize = 0; |
7 | 256 int skip_read = FALSE; |
257 #ifdef FEAT_CRYPT | |
258 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
|
259 int did_ask_for_key = FALSE; |
7 | 260 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
261 #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
|
262 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
|
263 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
|
264 #endif |
7 | 265 int split = 0; /* number of split lines */ |
266 #define UNKNOWN 0x0fffffff /* file size is unknown */ | |
267 linenr_T linecnt; | |
268 int error = FALSE; /* errors encountered */ | |
269 int ff_error = EOL_UNKNOWN; /* file format with errors */ | |
270 long linerest = 0; /* remaining chars in line */ | |
271 #ifdef UNIX | |
272 int perm = 0; | |
273 int swap_mode = -1; /* protection bits for swap file */ | |
274 #else | |
275 int perm; | |
276 #endif | |
277 int fileformat = 0; /* end-of-line format */ | |
278 int keep_fileformat = FALSE; | |
279 struct stat st; | |
280 int file_readonly; | |
281 linenr_T skip_count = 0; | |
282 linenr_T read_count = 0; | |
283 int msg_save = msg_scroll; | |
284 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | |
285 * last read was missing the eol */ | |
286 int try_mac = (vim_strchr(p_ffs, 'm') != NULL); | |
287 int try_dos = (vim_strchr(p_ffs, 'd') != NULL); | |
288 int try_unix = (vim_strchr(p_ffs, 'x') != NULL); | |
289 int file_rewind = FALSE; | |
290 #ifdef FEAT_MBYTE | |
291 int can_retry; | |
595 | 292 linenr_T conv_error = 0; /* line nr with conversion error */ |
293 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | |
7 | 294 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
295 in destination encoding */ | |
595 | 296 int bad_char_behavior = BAD_REPLACE; |
297 /* BAD_KEEP, BAD_DROP or character to | |
298 * replace with */ | |
7 | 299 char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
300 int fio_flags = 0; | |
301 char_u *fenc; /* fileencoding to use */ | |
302 int fenc_alloced; /* fenc_next is in allocated memory */ | |
303 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | |
304 int advance_fenc = FALSE; | |
305 long real_size = 0; | |
306 # ifdef USE_ICONV | |
307 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | |
308 # ifdef FEAT_EVAL | |
309 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | |
310 'charconvert' next */ | |
311 # endif | |
312 # endif | |
313 int converted = FALSE; /* TRUE if conversion done */ | |
314 int notconverted = FALSE; /* TRUE if conversion wanted but it | |
315 wasn't possible */ | |
316 char_u conv_rest[CONV_RESTLEN]; | |
317 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | |
318 #endif | |
319 | |
1834 | 320 #ifdef FEAT_AUTOCMD |
321 /* Remember the initial values of curbuf, curbuf->b_ffname and | |
322 * curbuf->b_fname to detect whether they are altered as a result of | |
323 * executing nasty autocommands. Also check if "fname" and "sfname" | |
324 * point to one of these values. */ | |
325 buf_T *old_curbuf = curbuf; | |
326 char_u *old_b_ffname = curbuf->b_ffname; | |
327 char_u *old_b_fname = curbuf->b_fname; | |
328 int using_b_ffname = (fname == curbuf->b_ffname) | |
329 || (sfname == curbuf->b_ffname); | |
330 int using_b_fname = (fname == curbuf->b_fname) | |
331 || (sfname == curbuf->b_fname); | |
332 #endif | |
7 | 333 write_no_eol_lnum = 0; /* in case it was set by the previous read */ |
334 | |
335 /* | |
336 * If there is no file name yet, use the one for the read file. | |
337 * BF_NOTEDITED is set to reflect this. | |
338 * Don't do this for a read from a filter. | |
339 * Only do this when 'cpoptions' contains the 'f' flag. | |
340 */ | |
341 if (curbuf->b_ffname == NULL | |
342 && !filtering | |
343 && fname != NULL | |
344 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
345 && !(flags & READ_DUMMY)) | |
346 { | |
633 | 347 if (set_rw_fname(fname, sfname) == FAIL) |
348 return FAIL; | |
7 | 349 } |
350 | |
167 | 351 /* After reading a file the cursor line changes but we don't want to |
352 * display the line. */ | |
353 ex_no_reprint = TRUE; | |
354 | |
968 | 355 /* don't display the file info for another buffer now */ |
356 need_fileinfo = FALSE; | |
357 | |
7 | 358 /* |
359 * For Unix: Use the short file name whenever possible. | |
360 * Avoids problems with networks and when directory names are changed. | |
361 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
362 * another directory, which we don't detect. | |
363 */ | |
364 if (sfname == NULL) | |
365 sfname = fname; | |
366 #if defined(UNIX) || defined(__EMX__) | |
367 fname = sfname; | |
368 #endif | |
369 | |
370 #ifdef FEAT_AUTOCMD | |
371 /* | |
372 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
373 * executing the associated commands instead. | |
374 */ | |
375 if (!filtering && !read_stdin && !read_buffer) | |
376 { | |
377 pos_T pos; | |
378 | |
379 pos = curbuf->b_op_start; | |
380 | |
381 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | |
382 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
383 curbuf->b_op_start.col = 0; | |
384 | |
385 if (newfile) | |
386 { | |
387 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
388 FALSE, curbuf, eap)) | |
389 #ifdef FEAT_EVAL | |
390 return aborting() ? FAIL : OK; | |
391 #else | |
392 return OK; | |
393 #endif | |
394 } | |
395 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
396 FALSE, NULL, eap)) | |
397 #ifdef FEAT_EVAL | |
398 return aborting() ? FAIL : OK; | |
399 #else | |
400 return OK; | |
401 #endif | |
402 | |
403 curbuf->b_op_start = pos; | |
404 } | |
405 #endif | |
406 | |
407 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
408 msg_scroll = FALSE; /* overwrite previous file message */ | |
409 else | |
410 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
411 | |
412 /* | |
413 * If the name ends in a path separator, we can't open it. Check here, | |
414 * because reading the file may actually work, but then creating the swap | |
415 * file may destroy it! Reported on MS-DOS and Win 95. | |
416 * If the name is too long we might crash further on, quit here. | |
417 */ | |
23 | 418 if (fname != NULL && *fname != NUL) |
419 { | |
39 | 420 p = fname + STRLEN(fname); |
421 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 422 { |
423 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
424 msg_end(); | |
425 msg_scroll = msg_save; | |
426 return FAIL; | |
427 } | |
7 | 428 } |
429 | |
430 #ifdef UNIX | |
431 /* | |
432 * On Unix it is possible to read a directory, so we have to | |
433 * check for it before the mch_open(). | |
434 */ | |
435 if (!read_stdin && !read_buffer) | |
436 { | |
437 perm = mch_getperm(fname); | |
438 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | |
439 # ifdef S_ISFIFO | |
440 && !S_ISFIFO(perm) /* ... or fifo */ | |
441 # endif | |
442 # ifdef S_ISSOCK | |
443 && !S_ISSOCK(perm) /* ... or socket */ | |
444 # endif | |
1313 | 445 # ifdef OPEN_CHR_FILES |
446 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
447 /* ... or a character special file named /dev/fd/<n> */ | |
448 # endif | |
7 | 449 ) |
450 { | |
451 if (S_ISDIR(perm)) | |
452 filemess(curbuf, fname, (char_u *)_("is a directory"), 0); | |
453 else | |
454 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
455 msg_end(); | |
456 msg_scroll = msg_save; | |
457 return FAIL; | |
458 } | |
1006 | 459 |
460 # if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
461 /* | |
462 * MS-Windows allows opening a device, but we will probably get stuck | |
463 * trying to read it. | |
464 */ | |
465 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
466 { | |
1303 | 467 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 468 msg_end(); |
469 msg_scroll = msg_save; | |
470 return FAIL; | |
471 } | |
472 # endif | |
1004 | 473 } |
474 #endif | |
475 | |
7 | 476 /* set default 'fileformat' */ |
819 | 477 if (set_options) |
7 | 478 { |
479 if (eap != NULL && eap->force_ff != 0) | |
480 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); | |
481 else if (*p_ffs != NUL) | |
482 set_fileformat(default_fileformat(), OPT_LOCAL); | |
483 } | |
484 | |
485 /* set or reset 'binary' */ | |
486 if (eap != NULL && eap->force_bin != 0) | |
487 { | |
488 int oldval = curbuf->b_p_bin; | |
489 | |
490 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); | |
491 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL); | |
492 } | |
493 | |
494 /* | |
495 * When opening a new file we take the readonly flag from the file. | |
496 * Default is r/w, can be set to r/o below. | |
497 * Don't reset it when in readonly mode | |
498 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
499 */ | |
500 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 501 if (check_readonly && !readonlymode) |
7 | 502 curbuf->b_p_ro = FALSE; |
503 | |
504 if (newfile && !read_stdin && !read_buffer) | |
505 { | |
506 /* Remember time of file. | |
507 * For RISCOS, also remember the filetype. | |
508 */ | |
509 if (mch_stat((char *)fname, &st) >= 0) | |
510 { | |
511 buf_store_time(curbuf, &st, fname); | |
512 curbuf->b_mtime_read = curbuf->b_mtime; | |
513 | |
514 #if defined(RISCOS) && defined(FEAT_OSFILETYPE) | |
515 /* Read the filetype into the buffer local filetype option. */ | |
516 mch_read_filetype(fname); | |
517 #endif | |
518 #ifdef UNIX | |
519 /* | |
520 * Use the protection bits of the original file for the swap file. | |
521 * This makes it possible for others to read the name of the | |
522 * edited file from the swapfile, but only if they can read the | |
523 * edited file. | |
524 * Remove the "write" and "execute" bits for group and others | |
525 * (they must not write the swapfile). | |
526 * Add the "read" and "write" bits for the user, otherwise we may | |
527 * not be able to write to the file ourselves. | |
528 * Setting the bits is done below, after creating the swap file. | |
529 */ | |
530 swap_mode = (st.st_mode & 0644) | 0600; | |
531 #endif | |
532 #ifdef FEAT_CW_EDITOR | |
533 /* Get the FSSpec on MacOS | |
534 * TODO: Update it properly when the buffer name changes | |
535 */ | |
536 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | |
537 #endif | |
538 #ifdef VMS | |
539 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 540 curbuf->b_fab_rat = st.st_fab_rat; |
541 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 542 #endif |
543 } | |
544 else | |
545 { | |
546 curbuf->b_mtime = 0; | |
547 curbuf->b_mtime_read = 0; | |
548 curbuf->b_orig_size = 0; | |
549 curbuf->b_orig_mode = 0; | |
550 } | |
551 | |
552 /* Reset the "new file" flag. It will be set again below when the | |
553 * file doesn't exist. */ | |
554 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | |
555 } | |
556 | |
557 /* | |
558 * for UNIX: check readonly with perm and mch_access() | |
559 * for RISCOS: same as Unix, otherwise file gets re-datestamped! | |
560 * for MSDOS and Amiga: check readonly by trying to open the file for writing | |
561 */ | |
562 file_readonly = FALSE; | |
563 if (read_stdin) | |
564 { | |
565 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
566 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ | |
567 setmode(0, O_BINARY); | |
568 #endif | |
569 } | |
570 else if (!read_buffer) | |
571 { | |
572 #ifdef USE_MCH_ACCESS | |
573 if ( | |
574 # ifdef UNIX | |
575 !(perm & 0222) || | |
576 # endif | |
577 mch_access((char *)fname, W_OK)) | |
578 file_readonly = TRUE; | |
579 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
580 #else | |
581 if (!newfile | |
582 || readonlymode | |
583 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
584 { | |
585 file_readonly = TRUE; | |
586 /* try to open ro */ | |
587 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
588 } | |
589 #endif | |
590 } | |
591 | |
592 if (fd < 0) /* cannot open at all */ | |
593 { | |
594 #ifndef UNIX | |
595 int isdir_f; | |
596 #endif | |
597 msg_scroll = msg_save; | |
598 #ifndef UNIX | |
599 /* | |
600 * On MSDOS and Amiga we can't open a directory, check here. | |
601 */ | |
602 isdir_f = (mch_isdir(fname)); | |
603 perm = mch_getperm(fname); /* check if the file exists */ | |
604 if (isdir_f) | |
605 { | |
606 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
607 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
608 } | |
609 else | |
610 #endif | |
611 if (newfile) | |
612 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
613 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
614 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
615 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
616 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
617 ) |
7 | 618 { |
619 /* | |
620 * Set the 'new-file' flag, so that when the file has | |
621 * been created by someone else, a ":w" will complain. | |
622 */ | |
623 curbuf->b_flags |= BF_NEW; | |
624 | |
625 /* Create a swap file now, so that other Vims are warned | |
626 * that we are editing this file. Don't do this for a | |
627 * "nofile" or "nowrite" buffer type. */ | |
628 #ifdef FEAT_QUICKFIX | |
629 if (!bt_dontwrite(curbuf)) | |
630 #endif | |
1834 | 631 { |
7 | 632 check_need_swap(newfile); |
1834 | 633 #ifdef FEAT_AUTOCMD |
634 /* SwapExists autocommand may mess things up */ | |
635 if (curbuf != old_curbuf | |
636 || (using_b_ffname | |
637 && (old_b_ffname != curbuf->b_ffname)) | |
638 || (using_b_fname | |
639 && (old_b_fname != curbuf->b_fname))) | |
640 { | |
641 EMSG(_(e_auchangedbuf)); | |
642 return FAIL; | |
643 } | |
644 #endif | |
645 } | |
592 | 646 if (dir_of_file_exists(fname)) |
647 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
648 else | |
649 filemess(curbuf, sfname, | |
650 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 651 #ifdef FEAT_VIMINFO |
652 /* Even though this is a new file, it might have been | |
653 * edited before and deleted. Get the old marks. */ | |
654 check_marks_read(); | |
655 #endif | |
656 #ifdef FEAT_MBYTE | |
657 if (eap != NULL && eap->force_enc != 0) | |
658 { | |
659 /* set forced 'fileencoding' */ | |
660 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
661 if (fenc != NULL) | |
662 set_string_option_direct((char_u *)"fenc", -1, | |
694 | 663 fenc, OPT_FREE|OPT_LOCAL, 0); |
7 | 664 vim_free(fenc); |
665 } | |
666 #endif | |
667 #ifdef FEAT_AUTOCMD | |
668 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | |
669 FALSE, curbuf, eap); | |
670 #endif | |
671 /* remember the current fileformat */ | |
672 save_file_ff(curbuf); | |
673 | |
674 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
675 if (aborting()) /* autocmds may abort script processing */ | |
676 return FAIL; | |
677 #endif | |
678 return OK; /* a new file is not an error */ | |
679 } | |
680 else | |
681 { | |
550 | 682 filemess(curbuf, sfname, (char_u *)( |
683 # ifdef EFBIG | |
684 (errno == EFBIG) ? _("[File too big]") : | |
685 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
686 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
687 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
688 # endif |
550 | 689 _("[Permission Denied]")), 0); |
7 | 690 curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
691 } | |
692 } | |
693 | |
694 return FAIL; | |
695 } | |
696 | |
697 /* | |
698 * Only set the 'ro' flag for readonly files the first time they are | |
699 * loaded. Help files always get readonly mode | |
700 */ | |
701 if ((check_readonly && file_readonly) || curbuf->b_help) | |
702 curbuf->b_p_ro = TRUE; | |
703 | |
819 | 704 if (set_options) |
7 | 705 { |
1486 | 706 /* Don't change 'eol' if reading from buffer as it will already be |
707 * correctly set when reading stdin. */ | |
708 if (!read_buffer) | |
709 { | |
710 curbuf->b_p_eol = TRUE; | |
711 curbuf->b_start_eol = TRUE; | |
712 } | |
7 | 713 #ifdef FEAT_MBYTE |
714 curbuf->b_p_bomb = FALSE; | |
1352 | 715 curbuf->b_start_bomb = FALSE; |
7 | 716 #endif |
717 } | |
718 | |
719 /* Create a swap file now, so that other Vims are warned that we are | |
720 * editing this file. | |
721 * Don't do this for a "nofile" or "nowrite" buffer type. */ | |
722 #ifdef FEAT_QUICKFIX | |
723 if (!bt_dontwrite(curbuf)) | |
724 #endif | |
725 { | |
726 check_need_swap(newfile); | |
1834 | 727 #ifdef FEAT_AUTOCMD |
728 if (!read_stdin && (curbuf != old_curbuf | |
729 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
730 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
731 { | |
732 EMSG(_(e_auchangedbuf)); | |
733 if (!read_buffer) | |
734 close(fd); | |
735 return FAIL; | |
736 } | |
737 #endif | |
7 | 738 #ifdef UNIX |
739 /* Set swap file protection bits after creating it. */ | |
1918 | 740 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
741 && curbuf->b_ml.ml_mfp->mf_fname != NULL) | |
7 | 742 (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode); |
743 #endif | |
744 } | |
745 | |
578 | 746 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 747 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
748 if (swap_exists_action == SEA_QUIT) | |
749 { | |
750 if (!read_buffer && !read_stdin) | |
751 close(fd); | |
752 return FAIL; | |
753 } | |
754 #endif | |
755 | |
756 ++no_wait_return; /* don't wait for return yet */ | |
757 | |
758 /* | |
759 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
760 */ | |
761 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
762 curbuf->b_op_start.col = 0; | |
763 | |
764 #ifdef FEAT_AUTOCMD | |
765 if (!read_buffer) | |
766 { | |
767 int m = msg_scroll; | |
768 int n = msg_scrolled; | |
769 | |
770 /* | |
771 * The file must be closed again, the autocommands may want to change | |
772 * the file before reading it. | |
773 */ | |
774 if (!read_stdin) | |
775 close(fd); /* ignore errors */ | |
776 | |
777 /* | |
778 * The output from the autocommands should not overwrite anything and | |
779 * should not be overwritten: Set msg_scroll, restore its value if no | |
780 * output was done. | |
781 */ | |
782 msg_scroll = TRUE; | |
783 if (filtering) | |
784 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
785 FALSE, curbuf, eap); | |
786 else if (read_stdin) | |
787 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
788 FALSE, curbuf, eap); | |
789 else if (newfile) | |
790 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
791 FALSE, curbuf, eap); | |
792 else | |
793 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
794 FALSE, NULL, eap); | |
795 if (msg_scrolled == n) | |
796 msg_scroll = m; | |
797 | |
798 #ifdef FEAT_EVAL | |
799 if (aborting()) /* autocmds may abort script processing */ | |
800 { | |
801 --no_wait_return; | |
802 msg_scroll = msg_save; | |
803 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
804 return FAIL; | |
805 } | |
806 #endif | |
807 /* | |
808 * Don't allow the autocommands to change the current buffer. | |
809 * Try to re-open the file. | |
1834 | 810 * |
811 * Don't allow the autocommands to change the buffer name either | |
812 * (cd for example) if it invalidates fname or sfname. | |
7 | 813 */ |
814 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 815 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
816 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 817 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
818 { | |
819 --no_wait_return; | |
820 msg_scroll = msg_save; | |
821 if (fd < 0) | |
822 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
823 else | |
824 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
825 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
826 return FAIL; | |
827 } | |
828 } | |
829 #endif /* FEAT_AUTOCMD */ | |
830 | |
831 /* Autocommands may add lines to the file, need to check if it is empty */ | |
832 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
833 | |
834 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
835 { | |
836 /* | |
837 * Show the user that we are busy reading the input. Sometimes this | |
838 * may take a while. When reading from stdin another program may | |
839 * still be running, don't move the cursor to the last line, unless | |
840 * always using the GUI. | |
841 */ | |
842 if (read_stdin) | |
843 { | |
844 #ifndef ALWAYS_USE_GUI | |
845 mch_msg(_("Vim: Reading from stdin...\n")); | |
846 #endif | |
847 #ifdef FEAT_GUI | |
848 /* Also write a message in the GUI window, if there is one. */ | |
849 if (gui.in_use && !gui.dying && !gui.starting) | |
850 { | |
851 p = (char_u *)_("Reading from stdin..."); | |
852 gui_write(p, (int)STRLEN(p)); | |
853 } | |
854 #endif | |
855 } | |
856 else if (!read_buffer) | |
857 filemess(curbuf, sfname, (char_u *)"", 0); | |
858 } | |
859 | |
860 msg_scroll = FALSE; /* overwrite the file message */ | |
861 | |
862 /* | |
863 * Set linecnt now, before the "retry" caused by a wrong guess for | |
864 * fileformat, and after the autocommands, which may change them. | |
865 */ | |
866 linecnt = curbuf->b_ml.ml_line_count; | |
867 | |
868 #ifdef FEAT_MBYTE | |
595 | 869 /* "++bad=" argument. */ |
870 if (eap != NULL && eap->bad_char != 0) | |
612 | 871 { |
595 | 872 bad_char_behavior = eap->bad_char; |
819 | 873 if (set_options) |
612 | 874 curbuf->b_bad_char = eap->bad_char; |
875 } | |
876 else | |
877 curbuf->b_bad_char = 0; | |
595 | 878 |
7 | 879 /* |
595 | 880 * Decide which 'encoding' to use or use first. |
7 | 881 */ |
882 if (eap != NULL && eap->force_enc != 0) | |
883 { | |
884 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
885 fenc_alloced = TRUE; | |
595 | 886 keep_dest_enc = TRUE; |
7 | 887 } |
888 else if (curbuf->b_p_bin) | |
889 { | |
890 fenc = (char_u *)""; /* binary: don't convert */ | |
891 fenc_alloced = FALSE; | |
892 } | |
893 else if (curbuf->b_help) | |
894 { | |
895 char_u firstline[80]; | |
301 | 896 int fc; |
7 | 897 |
898 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
899 * fails it must be latin1. | |
900 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
901 * this when needed to avoid [converted] remarks all the time. | |
902 * It is needed when the first line contains non-ASCII characters. | |
903 * That is only in *.??x files. */ | |
904 fenc = (char_u *)"latin1"; | |
905 c = enc_utf8; | |
301 | 906 if (!c && !read_stdin) |
907 { | |
908 fc = fname[STRLEN(fname) - 1]; | |
909 if (TOLOWER_ASC(fc) == 'x') | |
910 { | |
911 /* Read the first line (and a bit more). Immediately rewind to | |
912 * the start of the file. If the read() fails "len" is -1. */ | |
913 len = vim_read(fd, firstline, 80); | |
914 lseek(fd, (off_t)0L, SEEK_SET); | |
915 for (p = firstline; p < firstline + len; ++p) | |
916 if (*p >= 0x80) | |
917 { | |
918 c = TRUE; | |
919 break; | |
920 } | |
921 } | |
7 | 922 } |
923 | |
924 if (c) | |
925 { | |
926 fenc_next = fenc; | |
927 fenc = (char_u *)"utf-8"; | |
928 | |
929 /* When the file is utf-8 but a character doesn't fit in | |
930 * 'encoding' don't retry. In help text editing utf-8 bytes | |
931 * doesn't make sense. */ | |
844 | 932 if (!enc_utf8) |
933 keep_dest_enc = TRUE; | |
7 | 934 } |
935 fenc_alloced = FALSE; | |
936 } | |
937 else if (*p_fencs == NUL) | |
938 { | |
939 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
940 fenc_alloced = FALSE; | |
941 } | |
942 else | |
943 { | |
944 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
945 fenc = next_fenc(&fenc_next); | |
946 fenc_alloced = TRUE; | |
947 } | |
948 #endif | |
949 | |
950 /* | |
951 * Jump back here to retry reading the file in different ways. | |
952 * Reasons to retry: | |
953 * - encoding conversion failed: try another one from "fenc_next" | |
954 * - BOM detected and fenc was set, need to setup conversion | |
955 * - "fileformat" check failed: try another | |
956 * | |
957 * Variables set for special retry actions: | |
958 * "file_rewind" Rewind the file to start reading it again. | |
959 * "advance_fenc" Advance "fenc" using "fenc_next". | |
960 * "skip_read" Re-use already read bytes (BOM detected). | |
961 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
962 * "keep_fileformat" Don't reset "fileformat". | |
963 * | |
964 * Other status indicators: | |
965 * "tmpname" When != NULL did conversion with 'charconvert'. | |
966 * Output file has to be deleted afterwards. | |
967 * "iconv_fd" When != -1 did conversion with iconv(). | |
968 */ | |
969 retry: | |
970 | |
971 if (file_rewind) | |
972 { | |
973 if (read_buffer) | |
974 { | |
975 read_buf_lnum = 1; | |
976 read_buf_col = 0; | |
977 } | |
978 else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0) | |
979 { | |
980 /* Can't rewind the file, give up. */ | |
981 error = TRUE; | |
982 goto failed; | |
983 } | |
984 /* Delete the previously read lines. */ | |
985 while (lnum > from) | |
986 ml_delete(lnum--, FALSE); | |
987 file_rewind = FALSE; | |
988 #ifdef FEAT_MBYTE | |
819 | 989 if (set_options) |
1352 | 990 { |
7 | 991 curbuf->b_p_bomb = FALSE; |
1352 | 992 curbuf->b_start_bomb = FALSE; |
993 } | |
595 | 994 conv_error = 0; |
7 | 995 #endif |
996 } | |
997 | |
2267 | 998 #ifdef FEAT_CRYPT |
999 if (cryptkey != NULL) | |
1000 /* Need to reset the state, but keep the key, don't want to ask for it | |
1001 * again. */ | |
1002 crypt_pop_state(); | |
1003 #endif | |
1004 | |
7 | 1005 /* |
1006 * When retrying with another "fenc" and the first time "fileformat" | |
1007 * will be reset. | |
1008 */ | |
1009 if (keep_fileformat) | |
1010 keep_fileformat = FALSE; | |
1011 else | |
1012 { | |
1013 if (eap != NULL && eap->force_ff != 0) | |
1742 | 1014 { |
7 | 1015 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 1016 try_unix = try_dos = try_mac = FALSE; |
1017 } | |
7 | 1018 else if (curbuf->b_p_bin) |
1019 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
1020 else if (*p_ffs == NUL) | |
1021 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
1022 else | |
1023 fileformat = EOL_UNKNOWN; /* detect from file */ | |
1024 } | |
1025 | |
1026 #ifdef FEAT_MBYTE | |
1027 # ifdef USE_ICONV | |
1028 if (iconv_fd != (iconv_t)-1) | |
1029 { | |
1030 /* aborted conversion with iconv(), close the descriptor */ | |
1031 iconv_close(iconv_fd); | |
1032 iconv_fd = (iconv_t)-1; | |
1033 } | |
1034 # endif | |
1035 | |
1036 if (advance_fenc) | |
1037 { | |
1038 /* | |
1039 * Try the next entry in 'fileencodings'. | |
1040 */ | |
1041 advance_fenc = FALSE; | |
1042 | |
1043 if (eap != NULL && eap->force_enc != 0) | |
1044 { | |
1045 /* Conversion given with "++cc=" wasn't possible, read | |
1046 * without conversion. */ | |
1047 notconverted = TRUE; | |
595 | 1048 conv_error = 0; |
7 | 1049 if (fenc_alloced) |
1050 vim_free(fenc); | |
1051 fenc = (char_u *)""; | |
1052 fenc_alloced = FALSE; | |
1053 } | |
1054 else | |
1055 { | |
1056 if (fenc_alloced) | |
1057 vim_free(fenc); | |
1058 if (fenc_next != NULL) | |
1059 { | |
1060 fenc = next_fenc(&fenc_next); | |
1061 fenc_alloced = (fenc_next != NULL); | |
1062 } | |
1063 else | |
1064 { | |
1065 fenc = (char_u *)""; | |
1066 fenc_alloced = FALSE; | |
1067 } | |
1068 } | |
1069 if (tmpname != NULL) | |
1070 { | |
1071 mch_remove(tmpname); /* delete converted file */ | |
1072 vim_free(tmpname); | |
1073 tmpname = NULL; | |
1074 } | |
1075 } | |
1076 | |
1077 /* | |
1948 | 1078 * Conversion may be required when the encoding of the file is different |
1079 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1080 */ |
1081 fio_flags = 0; | |
1948 | 1082 converted = need_conversion(fenc); |
1083 if (converted) | |
7 | 1084 { |
1085 | |
1086 /* "ucs-bom" means we need to check the first bytes of the file | |
1087 * for a BOM. */ | |
1088 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1089 fio_flags = FIO_UCSBOM; | |
1090 | |
1091 /* | |
1092 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1093 * done. This is handled below after read(). Prepare the | |
1094 * fio_flags to avoid having to parse the string each time. | |
1095 * Also check for Unicode to Latin1 conversion, because iconv() | |
1096 * appears not to handle this correctly. This works just like | |
1097 * conversion to UTF-8 except how the resulting character is put in | |
1098 * the buffer. | |
1099 */ | |
1100 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1101 fio_flags = get_fio_flags(fenc); | |
1102 | |
1103 # ifdef WIN3264 | |
1104 /* | |
1105 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1106 * is handled with MultiByteToWideChar(). | |
1107 */ | |
1108 if (fio_flags == 0) | |
1109 fio_flags = get_win_fio_flags(fenc); | |
1110 # endif | |
1111 | |
1112 # ifdef MACOS_X | |
1113 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ | |
1114 if (fio_flags == 0) | |
1115 fio_flags = get_mac_fio_flags(fenc); | |
1116 # endif | |
1117 | |
1118 # ifdef USE_ICONV | |
1119 /* | |
1120 * Try using iconv() if we can't convert internally. | |
1121 */ | |
1122 if (fio_flags == 0 | |
1123 # ifdef FEAT_EVAL | |
1124 && !did_iconv | |
1125 # endif | |
1126 ) | |
1127 iconv_fd = (iconv_t)my_iconv_open( | |
1128 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1129 # endif | |
1130 | |
1131 # ifdef FEAT_EVAL | |
1132 /* | |
1133 * Use the 'charconvert' expression when conversion is required | |
1134 * and we can't do it internally or with iconv(). | |
1135 */ | |
1136 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL | |
1137 # ifdef USE_ICONV | |
1138 && iconv_fd == (iconv_t)-1 | |
1139 # endif | |
1140 ) | |
1141 { | |
1142 # ifdef USE_ICONV | |
1143 did_iconv = FALSE; | |
1144 # endif | |
1145 /* Skip conversion when it's already done (retry for wrong | |
1146 * "fileformat"). */ | |
1147 if (tmpname == NULL) | |
1148 { | |
1149 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1150 if (tmpname == NULL) | |
1151 { | |
1152 /* Conversion failed. Try another one. */ | |
1153 advance_fenc = TRUE; | |
1154 if (fd < 0) | |
1155 { | |
1156 /* Re-opening the original file failed! */ | |
1157 EMSG(_("E202: Conversion made file unreadable!")); | |
1158 error = TRUE; | |
1159 goto failed; | |
1160 } | |
1161 goto retry; | |
1162 } | |
1163 } | |
1164 } | |
1165 else | |
1166 # endif | |
1167 { | |
1168 if (fio_flags == 0 | |
1169 # ifdef USE_ICONV | |
1170 && iconv_fd == (iconv_t)-1 | |
1171 # endif | |
1172 ) | |
1173 { | |
1174 /* Conversion wanted but we can't. | |
1175 * Try the next conversion in 'fileencodings' */ | |
1176 advance_fenc = TRUE; | |
1177 goto retry; | |
1178 } | |
1179 } | |
1180 } | |
1181 | |
595 | 1182 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1183 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1184 * stdin or fixed at a specific encoding. */ |
1185 can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc); | |
7 | 1186 #endif |
1187 | |
1188 if (!skip_read) | |
1189 { | |
1190 linerest = 0; | |
1191 filesize = 0; | |
1192 skip_count = lines_to_skip; | |
1193 read_count = lines_to_read; | |
1194 #ifdef FEAT_MBYTE | |
1195 conv_restlen = 0; | |
1196 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1197 #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
|
1198 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
|
1199 && !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
|
1200 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
|
1201 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
|
1202 #endif |
7 | 1203 } |
1204 | |
1205 while (!error && !got_int) | |
1206 { | |
1207 /* | |
1208 * We allocate as much space for the file as we can get, plus | |
1209 * space for the old line plus room for one terminating NUL. | |
1210 * The amount is limited by the fact that read() only can read | |
1211 * upto max_unsigned characters (and other things). | |
1212 */ | |
1213 #if SIZEOF_INT <= 2 | |
1214 if (linerest >= 0x7ff0) | |
1215 { | |
1216 ++split; | |
1217 *ptr = NL; /* split line by inserting a NL */ | |
1218 size = 1; | |
1219 } | |
1220 else | |
1221 #endif | |
1222 { | |
1223 if (!skip_read) | |
1224 { | |
1225 #if SIZEOF_INT > 2 | |
1076 | 1226 # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
7 | 1227 size = SSIZE_MAX; /* use max I/O size, 52K */ |
1228 # else | |
1229 size = 0x10000L; /* use buffer >= 64K */ | |
1230 # endif | |
1231 #else | |
1232 size = 0x7ff0L - linerest; /* limit buffer to 32K */ | |
1233 #endif | |
1234 | |
836 | 1235 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1236 { |
1237 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1238 FALSE)) != NULL) | |
1239 break; | |
1240 } | |
1241 if (new_buffer == NULL) | |
1242 { | |
1243 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1244 error = TRUE; | |
1245 break; | |
1246 } | |
1247 if (linerest) /* copy characters from the previous buffer */ | |
1248 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1249 vim_free(buffer); | |
1250 buffer = new_buffer; | |
1251 ptr = buffer + linerest; | |
1252 line_start = buffer; | |
1253 | |
1254 #ifdef FEAT_MBYTE | |
1255 /* May need room to translate into. | |
1256 * For iconv() we don't really know the required space, use a | |
1257 * factor ICONV_MULT. | |
1258 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1259 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1260 * become up to 4 bytes, size must be multiple of 2 | |
1261 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1262 * multiple of 2 | |
1263 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1264 * multiple of 4 */ | |
835 | 1265 real_size = (int)size; |
7 | 1266 # ifdef USE_ICONV |
1267 if (iconv_fd != (iconv_t)-1) | |
1268 size = size / ICONV_MULT; | |
1269 else | |
1270 # endif | |
1271 if (fio_flags & FIO_LATIN1) | |
1272 size = size / 2; | |
1273 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1274 size = (size * 2 / 3) & ~1; | |
1275 else if (fio_flags & FIO_UCS4) | |
1276 size = (size * 2 / 3) & ~3; | |
1277 else if (fio_flags == FIO_UCSBOM) | |
1278 size = size / ICONV_MULT; /* worst case */ | |
1279 # ifdef WIN3264 | |
1280 else if (fio_flags & FIO_CODEPAGE) | |
1281 size = size / ICONV_MULT; /* also worst case */ | |
1282 # endif | |
1283 # ifdef MACOS_X | |
1284 else if (fio_flags & FIO_MACROMAN) | |
1285 size = size / ICONV_MULT; /* also worst case */ | |
1286 # endif | |
1287 #endif | |
1288 | |
1289 #ifdef FEAT_MBYTE | |
1290 if (conv_restlen > 0) | |
1291 { | |
1292 /* Insert unconverted bytes from previous line. */ | |
1293 mch_memmove(ptr, conv_rest, conv_restlen); | |
1294 ptr += conv_restlen; | |
1295 size -= conv_restlen; | |
1296 } | |
1297 #endif | |
1298 | |
1299 if (read_buffer) | |
1300 { | |
1301 /* | |
1302 * Read bytes from curbuf. Used for converting text read | |
1303 * from stdin. | |
1304 */ | |
1305 if (read_buf_lnum > from) | |
1306 size = 0; | |
1307 else | |
1308 { | |
1309 int n, ni; | |
1310 long tlen; | |
1311 | |
1312 tlen = 0; | |
1313 for (;;) | |
1314 { | |
1315 p = ml_get(read_buf_lnum) + read_buf_col; | |
1316 n = (int)STRLEN(p); | |
1317 if ((int)tlen + n + 1 > size) | |
1318 { | |
1319 /* Filled up to "size", append partial line. | |
1320 * Change NL to NUL to reverse the effect done | |
1321 * below. */ | |
835 | 1322 n = (int)(size - tlen); |
7 | 1323 for (ni = 0; ni < n; ++ni) |
1324 { | |
1325 if (p[ni] == NL) | |
1326 ptr[tlen++] = NUL; | |
1327 else | |
1328 ptr[tlen++] = p[ni]; | |
1329 } | |
1330 read_buf_col += n; | |
1331 break; | |
1332 } | |
1333 else | |
1334 { | |
1335 /* Append whole line and new-line. Change NL | |
1336 * to NUL to reverse the effect done below. */ | |
1337 for (ni = 0; ni < n; ++ni) | |
1338 { | |
1339 if (p[ni] == NL) | |
1340 ptr[tlen++] = NUL; | |
1341 else | |
1342 ptr[tlen++] = p[ni]; | |
1343 } | |
1344 ptr[tlen++] = NL; | |
1345 read_buf_col = 0; | |
1346 if (++read_buf_lnum > from) | |
1347 { | |
1348 /* When the last line didn't have an | |
1349 * end-of-line don't add it now either. */ | |
1350 if (!curbuf->b_p_eol) | |
1351 --tlen; | |
1352 size = tlen; | |
1353 break; | |
1354 } | |
1355 } | |
1356 } | |
1357 } | |
1358 } | |
1359 else | |
1360 { | |
1361 /* | |
1362 * Read bytes from the file. | |
1363 */ | |
1364 size = vim_read(fd, ptr, size); | |
1365 } | |
1366 | |
1367 if (size <= 0) | |
1368 { | |
1369 if (size < 0) /* read error */ | |
1370 error = TRUE; | |
1371 #ifdef FEAT_MBYTE | |
1372 else if (conv_restlen > 0) | |
595 | 1373 { |
1597 | 1374 /* |
1375 * Reached end-of-file but some trailing bytes could | |
1376 * not be converted. Truncated file? | |
1377 */ | |
1378 | |
1379 /* When we did a conversion report an error. */ | |
1380 if (fio_flags != 0 | |
1381 # ifdef USE_ICONV | |
1382 || iconv_fd != (iconv_t)-1 | |
1383 # endif | |
1384 ) | |
1385 { | |
1386 if (conv_error == 0) | |
1387 conv_error = curbuf->b_ml.ml_line_count | |
1388 - linecnt + 1; | |
1389 } | |
1390 /* Remember the first linenr with an illegal byte */ | |
1391 else if (illegal_byte == 0) | |
1392 illegal_byte = curbuf->b_ml.ml_line_count | |
1393 - linecnt + 1; | |
1394 if (bad_char_behavior == BAD_DROP) | |
595 | 1395 { |
1597 | 1396 *(ptr - conv_restlen) = NUL; |
1397 conv_restlen = 0; | |
1398 } | |
1399 else | |
1400 { | |
1401 /* Replace the trailing bytes with the replacement | |
1402 * character if we were converting; if we weren't, | |
1403 * leave the UTF8 checking code to do it, as it | |
1404 * works slightly differently. */ | |
1405 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1406 # ifdef USE_ICONV | |
1407 || iconv_fd != (iconv_t)-1 | |
1408 # endif | |
1409 )) | |
1410 { | |
1411 while (conv_restlen > 0) | |
1412 { | |
1413 *(--ptr) = bad_char_behavior; | |
1414 --conv_restlen; | |
1415 } | |
1416 } | |
595 | 1417 fio_flags = 0; /* don't convert this */ |
834 | 1418 # ifdef USE_ICONV |
1419 if (iconv_fd != (iconv_t)-1) | |
1420 { | |
1421 iconv_close(iconv_fd); | |
1422 iconv_fd = (iconv_t)-1; | |
1423 } | |
1424 # endif | |
595 | 1425 } |
1426 } | |
7 | 1427 #endif |
1428 } | |
1429 | |
1430 #ifdef FEAT_CRYPT | |
1431 /* | |
1432 * At start of file: Check for magic number of encryption. | |
1433 */ | |
1434 if (filesize == 0) | |
1435 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
2267 | 1436 &filesize, newfile, sfname, |
1437 &did_ask_for_key); | |
7 | 1438 /* |
1439 * Decrypt the read bytes. | |
1440 */ | |
1441 if (cryptkey != NULL && size > 0) | |
2244 | 1442 crypt_decode(ptr, size); |
7 | 1443 #endif |
1444 } | |
1445 skip_read = FALSE; | |
1446 | |
1447 #ifdef FEAT_MBYTE | |
1448 /* | |
1449 * At start of file (or after crypt magic number): Check for BOM. | |
1450 * Also check for a BOM for other Unicode encodings, but not after | |
1451 * converting with 'charconvert' or when a BOM has already been | |
1452 * found. | |
1453 */ | |
1454 if ((filesize == 0 | |
1455 # 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
|
1456 || (filesize == (CRYPT_MAGIC_LEN |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
1457 + crypt_salt_len[use_crypt_method] |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
1458 + 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
|
1459 && cryptkey != NULL) |
7 | 1460 # endif |
1461 ) | |
1462 && (fio_flags == FIO_UCSBOM | |
1463 || (!curbuf->b_p_bomb | |
1464 && tmpname == NULL | |
1465 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1466 { | |
1467 char_u *ccname; | |
1468 int blen; | |
1469 | |
1470 /* no BOM detection in a short file or in binary mode */ | |
1471 if (size < 2 || curbuf->b_p_bin) | |
1472 ccname = NULL; | |
1473 else | |
1474 ccname = check_for_bom(ptr, size, &blen, | |
1475 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1476 if (ccname != NULL) | |
1477 { | |
1478 /* Remove BOM from the text */ | |
1479 filesize += blen; | |
1480 size -= blen; | |
1481 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1482 if (set_options) |
1352 | 1483 { |
7 | 1484 curbuf->b_p_bomb = TRUE; |
1352 | 1485 curbuf->b_start_bomb = TRUE; |
1486 } | |
7 | 1487 } |
1488 | |
1489 if (fio_flags == FIO_UCSBOM) | |
1490 { | |
1491 if (ccname == NULL) | |
1492 { | |
1493 /* No BOM detected: retry with next encoding. */ | |
1494 advance_fenc = TRUE; | |
1495 } | |
1496 else | |
1497 { | |
1498 /* BOM detected: set "fenc" and jump back */ | |
1499 if (fenc_alloced) | |
1500 vim_free(fenc); | |
1501 fenc = ccname; | |
1502 fenc_alloced = FALSE; | |
1503 } | |
1504 /* retry reading without getting new bytes or rewinding */ | |
1505 skip_read = TRUE; | |
1506 goto retry; | |
1507 } | |
1508 } | |
1597 | 1509 |
1510 /* Include not converted bytes. */ | |
1511 ptr -= conv_restlen; | |
1512 size += conv_restlen; | |
1513 conv_restlen = 0; | |
7 | 1514 #endif |
1515 /* | |
1516 * Break here for a read error or end-of-file. | |
1517 */ | |
1518 if (size <= 0) | |
1519 break; | |
1520 | |
1521 #ifdef FEAT_MBYTE | |
1522 | |
1523 # ifdef USE_ICONV | |
1524 if (iconv_fd != (iconv_t)-1) | |
1525 { | |
1526 /* | |
1527 * Attempt conversion of the read bytes to 'encoding' using | |
1528 * iconv(). | |
1529 */ | |
1530 const char *fromp; | |
1531 char *top; | |
1532 size_t from_size; | |
1533 size_t to_size; | |
1534 | |
1535 fromp = (char *)ptr; | |
1536 from_size = size; | |
1537 ptr += size; | |
1538 top = (char *)ptr; | |
1539 to_size = real_size - size; | |
1540 | |
1541 /* | |
1542 * If there is conversion error or not enough room try using | |
179 | 1543 * another conversion. Except for when there is no |
1544 * alternative (help files). | |
7 | 1545 */ |
177 | 1546 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1547 &top, &to_size) | |
7 | 1548 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1549 || from_size > CONV_RESTLEN) | |
177 | 1550 { |
595 | 1551 if (can_retry) |
375 | 1552 goto rewind_retry; |
595 | 1553 if (conv_error == 0) |
1554 conv_error = readfile_linenr(linecnt, | |
1555 ptr, (char_u *)top); | |
1556 | |
1557 /* Deal with a bad byte and continue with the next. */ | |
177 | 1558 ++fromp; |
1559 --from_size; | |
595 | 1560 if (bad_char_behavior == BAD_KEEP) |
1561 { | |
1562 *top++ = *(fromp - 1); | |
1563 --to_size; | |
1564 } | |
1565 else if (bad_char_behavior != BAD_DROP) | |
1566 { | |
1567 *top++ = bad_char_behavior; | |
1568 --to_size; | |
1569 } | |
177 | 1570 } |
7 | 1571 |
1572 if (from_size > 0) | |
1573 { | |
1574 /* Some remaining characters, keep them for the next | |
1575 * round. */ | |
1576 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1577 conv_restlen = (int)from_size; | |
1578 } | |
1579 | |
1580 /* move the linerest to before the converted characters */ | |
1581 line_start = ptr - linerest; | |
1582 mch_memmove(line_start, buffer, (size_t)linerest); | |
1583 size = (long)((char_u *)top - ptr); | |
1584 } | |
1585 # endif | |
1586 | |
1587 # ifdef WIN3264 | |
1588 if (fio_flags & FIO_CODEPAGE) | |
1589 { | |
595 | 1590 char_u *src, *dst; |
1591 WCHAR ucs2buf[3]; | |
1592 int ucs2len; | |
1593 int codepage = FIO_GET_CP(fio_flags); | |
1594 int bytelen; | |
1595 int found_bad; | |
1596 char replstr[2]; | |
1597 | |
7 | 1598 /* |
1599 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1600 * a codepage, using standard MS-Windows functions. This |
1601 * requires two steps: | |
1602 * 1. convert from 'fileencoding' to ucs-2 | |
1603 * 2. convert from ucs-2 to 'encoding' | |
1604 * | |
1605 * Because there may be illegal bytes AND an incomplete byte | |
1606 * sequence at the end, we may have to do the conversion one | |
1607 * character at a time to get it right. | |
7 | 1608 */ |
595 | 1609 |
1610 /* Replacement string for WideCharToMultiByte(). */ | |
1611 if (bad_char_behavior > 0) | |
1612 replstr[0] = bad_char_behavior; | |
1613 else | |
1614 replstr[0] = '?'; | |
1615 replstr[1] = NUL; | |
1616 | |
1617 /* | |
1618 * Move the bytes to the end of the buffer, so that we have | |
1619 * room to put the result at the start. | |
1620 */ | |
1621 src = ptr + real_size - size; | |
1622 mch_memmove(src, ptr, size); | |
7 | 1623 |
1624 /* | |
595 | 1625 * Do the conversion. |
7 | 1626 */ |
595 | 1627 dst = ptr; |
1628 size = size; | |
1629 while (size > 0) | |
7 | 1630 { |
595 | 1631 found_bad = FALSE; |
7 | 1632 |
1633 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1634 if (codepage == CP_UTF8) |
7 | 1635 { |
595 | 1636 /* Handle CP_UTF8 input ourselves to be able to handle |
1637 * trailing bytes properly. | |
1638 * Get one UTF-8 character from src. */ | |
835 | 1639 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1640 if (bytelen > size) |
1641 { | |
1642 /* Only got some bytes of a character. Normally | |
1643 * it's put in "conv_rest", but if it's too long | |
1644 * deal with it as if they were illegal bytes. */ | |
1645 if (bytelen <= CONV_RESTLEN) | |
1646 break; | |
1647 | |
1648 /* weird overlong byte sequence */ | |
1649 bytelen = size; | |
1650 found_bad = TRUE; | |
1651 } | |
1652 else | |
1653 { | |
799 | 1654 int u8c = utf_ptr2char(src); |
1655 | |
622 | 1656 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1657 found_bad = TRUE; |
1658 ucs2buf[0] = u8c; | |
1659 ucs2len = 1; | |
1660 } | |
7 | 1661 } |
595 | 1662 else |
7 | 1663 # endif |
595 | 1664 { |
1665 /* We don't know how long the byte sequence is, try | |
1666 * from one to three bytes. */ | |
1667 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1668 ++bytelen) | |
1669 { | |
1670 ucs2len = MultiByteToWideChar(codepage, | |
1671 MB_ERR_INVALID_CHARS, | |
1672 (LPCSTR)src, bytelen, | |
1673 ucs2buf, 3); | |
1674 if (ucs2len > 0) | |
1675 break; | |
1676 } | |
1677 if (ucs2len == 0) | |
1678 { | |
1679 /* If we have only one byte then it's probably an | |
1680 * incomplete byte sequence. Otherwise discard | |
1681 * one byte as a bad character. */ | |
1682 if (size == 1) | |
1683 break; | |
1684 found_bad = TRUE; | |
1685 bytelen = 1; | |
1686 } | |
1687 } | |
1688 | |
1689 if (!found_bad) | |
7 | 1690 { |
595 | 1691 int i; |
1692 | |
1693 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1694 if (enc_utf8) | |
1695 { | |
1696 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1697 for (i = 0; i < ucs2len; ++i) | |
1698 dst += utf_char2bytes(ucs2buf[i], dst); | |
1699 } | |
1700 else | |
1701 { | |
1702 BOOL bad = FALSE; | |
1703 int dstlen; | |
1704 | |
1705 /* From UCS-2 to "enc_codepage". If the | |
1706 * conversion uses the default character "?", | |
1707 * the data doesn't fit in this encoding. */ | |
1708 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1709 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1710 (LPSTR)dst, (int)(src - dst), |
595 | 1711 replstr, &bad); |
1712 if (bad) | |
1713 found_bad = TRUE; | |
1714 else | |
1715 dst += dstlen; | |
1716 } | |
7 | 1717 } |
595 | 1718 |
1719 if (found_bad) | |
1720 { | |
1721 /* Deal with bytes we can't convert. */ | |
1722 if (can_retry) | |
1723 goto rewind_retry; | |
1724 if (conv_error == 0) | |
1725 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1726 if (bad_char_behavior != BAD_DROP) | |
1727 { | |
1728 if (bad_char_behavior == BAD_KEEP) | |
1729 { | |
1730 mch_memmove(dst, src, bytelen); | |
1731 dst += bytelen; | |
1732 } | |
1733 else | |
1734 *dst++ = bad_char_behavior; | |
1735 } | |
1736 } | |
1737 | |
1738 src += bytelen; | |
1739 size -= bytelen; | |
7 | 1740 } |
595 | 1741 |
1742 if (size > 0) | |
7 | 1743 { |
595 | 1744 /* An incomplete byte sequence remaining. */ |
1745 mch_memmove(conv_rest, src, size); | |
1746 conv_restlen = size; | |
7 | 1747 } |
595 | 1748 |
1749 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1750 size = (long)(dst - ptr); |
7 | 1751 } |
1752 else | |
1753 # endif | |
765 | 1754 # ifdef MACOS_CONVERT |
7 | 1755 if (fio_flags & FIO_MACROMAN) |
1756 { | |
1757 /* | |
1758 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1759 * latin1. This is in os_mac_conv.c. |
7 | 1760 */ |
18 | 1761 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1762 goto rewind_retry; |
1763 } | |
1764 else | |
1765 # endif | |
1766 if (fio_flags != 0) | |
1767 { | |
1768 int u8c; | |
1769 char_u *dest; | |
1770 char_u *tail = NULL; | |
1771 | |
1772 /* | |
1773 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1774 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1775 * Go from end to start through the buffer, because the number | |
1776 * of bytes may increase. | |
1777 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1778 * to after the next character to convert. | |
1779 */ | |
1780 dest = ptr + real_size; | |
1781 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1782 { | |
1783 p = ptr + size; | |
1784 if (fio_flags == FIO_UTF8) | |
1785 { | |
1786 /* Check for a trailing incomplete UTF-8 sequence */ | |
1787 tail = ptr + size - 1; | |
1788 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1789 --tail; | |
1790 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1791 tail = NULL; | |
1792 else | |
1793 p = tail; | |
1794 } | |
1795 } | |
1796 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1797 { | |
1798 /* Check for a trailing byte */ | |
1799 p = ptr + (size & ~1); | |
1800 if (size & 1) | |
1801 tail = p; | |
1802 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1803 { | |
1804 /* Check for a trailing leading word */ | |
1805 if (fio_flags & FIO_ENDIAN_L) | |
1806 { | |
1807 u8c = (*--p << 8); | |
1808 u8c += *--p; | |
1809 } | |
1810 else | |
1811 { | |
1812 u8c = *--p; | |
1813 u8c += (*--p << 8); | |
1814 } | |
1815 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1816 tail = p; | |
1817 else | |
1818 p += 2; | |
1819 } | |
1820 } | |
1821 else /* FIO_UCS4 */ | |
1822 { | |
1823 /* Check for trailing 1, 2 or 3 bytes */ | |
1824 p = ptr + (size & ~3); | |
1825 if (size & 3) | |
1826 tail = p; | |
1827 } | |
1828 | |
1829 /* If there is a trailing incomplete sequence move it to | |
1830 * conv_rest[]. */ | |
1831 if (tail != NULL) | |
1832 { | |
1833 conv_restlen = (int)((ptr + size) - tail); | |
1834 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1835 size -= conv_restlen; | |
1836 } | |
1837 | |
1838 | |
1839 while (p > ptr) | |
1840 { | |
1841 if (fio_flags & FIO_LATIN1) | |
1842 u8c = *--p; | |
1843 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1844 { | |
1845 if (fio_flags & FIO_ENDIAN_L) | |
1846 { | |
1847 u8c = (*--p << 8); | |
1848 u8c += *--p; | |
1849 } | |
1850 else | |
1851 { | |
1852 u8c = *--p; | |
1853 u8c += (*--p << 8); | |
1854 } | |
1855 if ((fio_flags & FIO_UTF16) | |
1856 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1857 { | |
1858 int u16c; | |
1859 | |
1860 if (p == ptr) | |
1861 { | |
1862 /* Missing leading word. */ | |
1863 if (can_retry) | |
1864 goto rewind_retry; | |
595 | 1865 if (conv_error == 0) |
1866 conv_error = readfile_linenr(linecnt, | |
1867 ptr, p); | |
1868 if (bad_char_behavior == BAD_DROP) | |
1869 continue; | |
1870 if (bad_char_behavior != BAD_KEEP) | |
1871 u8c = bad_char_behavior; | |
7 | 1872 } |
1873 | |
1874 /* found second word of double-word, get the first | |
1875 * word and compute the resulting character */ | |
1876 if (fio_flags & FIO_ENDIAN_L) | |
1877 { | |
1878 u16c = (*--p << 8); | |
1879 u16c += *--p; | |
1880 } | |
1881 else | |
1882 { | |
1883 u16c = *--p; | |
1884 u16c += (*--p << 8); | |
1885 } | |
595 | 1886 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1887 + (u8c & 0x3ff); | |
1888 | |
7 | 1889 /* Check if the word is indeed a leading word. */ |
1890 if (u16c < 0xd800 || u16c > 0xdbff) | |
1891 { | |
1892 if (can_retry) | |
1893 goto rewind_retry; | |
595 | 1894 if (conv_error == 0) |
1895 conv_error = readfile_linenr(linecnt, | |
1896 ptr, p); | |
1897 if (bad_char_behavior == BAD_DROP) | |
1898 continue; | |
1899 if (bad_char_behavior != BAD_KEEP) | |
1900 u8c = bad_char_behavior; | |
7 | 1901 } |
1902 } | |
1903 } | |
1904 else if (fio_flags & FIO_UCS4) | |
1905 { | |
1906 if (fio_flags & FIO_ENDIAN_L) | |
1907 { | |
1908 u8c = (*--p << 24); | |
1909 u8c += (*--p << 16); | |
1910 u8c += (*--p << 8); | |
1911 u8c += *--p; | |
1912 } | |
1913 else /* big endian */ | |
1914 { | |
1915 u8c = *--p; | |
1916 u8c += (*--p << 8); | |
1917 u8c += (*--p << 16); | |
1918 u8c += (*--p << 24); | |
1919 } | |
1920 } | |
1921 else /* UTF-8 */ | |
1922 { | |
1923 if (*--p < 0x80) | |
1924 u8c = *p; | |
1925 else | |
1926 { | |
1927 len = utf_head_off(ptr, p); | |
595 | 1928 p -= len; |
1929 u8c = utf_ptr2char(p); | |
7 | 1930 if (len == 0) |
1931 { | |
1932 /* Not a valid UTF-8 character, retry with | |
1933 * another fenc when possible, otherwise just | |
1934 * report the error. */ | |
1935 if (can_retry) | |
1936 goto rewind_retry; | |
595 | 1937 if (conv_error == 0) |
1938 conv_error = readfile_linenr(linecnt, | |
1939 ptr, p); | |
1940 if (bad_char_behavior == BAD_DROP) | |
1941 continue; | |
1942 if (bad_char_behavior != BAD_KEEP) | |
1943 u8c = bad_char_behavior; | |
7 | 1944 } |
1945 } | |
1946 } | |
1947 if (enc_utf8) /* produce UTF-8 */ | |
1948 { | |
1949 dest -= utf_char2len(u8c); | |
1950 (void)utf_char2bytes(u8c, dest); | |
1951 } | |
1952 else /* produce Latin1 */ | |
1953 { | |
1954 --dest; | |
1955 if (u8c >= 0x100) | |
1956 { | |
1957 /* character doesn't fit in latin1, retry with | |
1958 * another fenc when possible, otherwise just | |
1959 * report the error. */ | |
595 | 1960 if (can_retry) |
7 | 1961 goto rewind_retry; |
595 | 1962 if (conv_error == 0) |
1963 conv_error = readfile_linenr(linecnt, ptr, p); | |
1964 if (bad_char_behavior == BAD_DROP) | |
1965 ++dest; | |
1966 else if (bad_char_behavior == BAD_KEEP) | |
1967 *dest = u8c; | |
1968 else if (eap != NULL && eap->bad_char != 0) | |
1969 *dest = bad_char_behavior; | |
1970 else | |
1971 *dest = 0xBF; | |
7 | 1972 } |
1973 else | |
1974 *dest = u8c; | |
1975 } | |
1976 } | |
1977 | |
1978 /* move the linerest to before the converted characters */ | |
1979 line_start = dest - linerest; | |
1980 mch_memmove(line_start, buffer, (size_t)linerest); | |
1981 size = (long)((ptr + real_size) - dest); | |
1982 ptr = dest; | |
1983 } | |
1597 | 1984 else if (enc_utf8 && !curbuf->b_p_bin) |
1985 { | |
1986 int incomplete_tail = FALSE; | |
1987 | |
1988 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
1989 for (p = ptr; ; ++p) | |
7 | 1990 { |
835 | 1991 int todo = (int)((ptr + size) - p); |
595 | 1992 int l; |
1993 | |
1994 if (todo <= 0) | |
1995 break; | |
7 | 1996 if (*p >= 0x80) |
1997 { | |
1998 /* A length of 1 means it's an illegal byte. Accept | |
1999 * an incomplete character at the end though, the next | |
2000 * read() will get the next bytes, we'll check it | |
2001 * then. */ | |
595 | 2002 l = utf_ptr2len_len(p, todo); |
1597 | 2003 if (l > todo && !incomplete_tail) |
7 | 2004 { |
1597 | 2005 /* Avoid retrying with a different encoding when |
2006 * a truncated file is more likely, or attempting | |
2007 * to read the rest of an incomplete sequence when | |
2008 * we have already done so. */ | |
2009 if (p > ptr || filesize > 0) | |
2010 incomplete_tail = TRUE; | |
2011 /* Incomplete byte sequence, move it to conv_rest[] | |
2012 * and try to read the rest of it, unless we've | |
2013 * already done so. */ | |
2014 if (p > ptr) | |
2015 { | |
2016 conv_restlen = todo; | |
2017 mch_memmove(conv_rest, p, conv_restlen); | |
2018 size -= conv_restlen; | |
2019 break; | |
2020 } | |
7 | 2021 } |
1597 | 2022 if (l == 1 || l > todo) |
595 | 2023 { |
2024 /* Illegal byte. If we can try another encoding | |
1597 | 2025 * do that, unless at EOF where a truncated |
2026 * file is more likely than a conversion error. */ | |
2027 if (can_retry && !incomplete_tail) | |
595 | 2028 break; |
2029 # ifdef USE_ICONV | |
2030 /* When we did a conversion report an error. */ | |
2031 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2032 conv_error = readfile_linenr(linecnt, ptr, p); | |
2033 # endif | |
1597 | 2034 /* Remember the first linenr with an illegal byte */ |
2035 if (conv_error == 0 && illegal_byte == 0) | |
2036 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2037 |
2038 /* Drop, keep or replace the bad byte. */ | |
2039 if (bad_char_behavior == BAD_DROP) | |
2040 { | |
1597 | 2041 mch_memmove(p, p + 1, todo - 1); |
595 | 2042 --p; |
2043 --size; | |
2044 } | |
2045 else if (bad_char_behavior != BAD_KEEP) | |
2046 *p = bad_char_behavior; | |
2047 } | |
1597 | 2048 else |
2049 p += l - 1; | |
7 | 2050 } |
2051 } | |
1597 | 2052 if (p < ptr + size && !incomplete_tail) |
7 | 2053 { |
2054 /* Detected a UTF-8 error. */ | |
2055 rewind_retry: | |
595 | 2056 /* Retry reading with another conversion. */ |
7 | 2057 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2058 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2059 /* iconv() failed, try 'charconvert' */ | |
2060 did_iconv = TRUE; | |
7 | 2061 else |
2062 # endif | |
595 | 2063 /* use next item from 'fileencodings' */ |
2064 advance_fenc = TRUE; | |
2065 file_rewind = TRUE; | |
2066 goto retry; | |
7 | 2067 } |
2068 } | |
2069 #endif | |
2070 | |
2071 /* count the number of characters (after conversion!) */ | |
2072 filesize += size; | |
2073 | |
2074 /* | |
2075 * when reading the first part of a file: guess EOL type | |
2076 */ | |
2077 if (fileformat == EOL_UNKNOWN) | |
2078 { | |
2079 /* First try finding a NL, for Dos and Unix */ | |
2080 if (try_dos || try_unix) | |
2081 { | |
2082 for (p = ptr; p < ptr + size; ++p) | |
2083 { | |
2084 if (*p == NL) | |
2085 { | |
2086 if (!try_unix | |
2087 || (try_dos && p > ptr && p[-1] == CAR)) | |
2088 fileformat = EOL_DOS; | |
2089 else | |
2090 fileformat = EOL_UNIX; | |
2091 break; | |
2092 } | |
2093 } | |
2094 | |
2095 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2096 if (fileformat == EOL_UNIX && try_mac) | |
2097 { | |
2098 /* Need to reset the counters when retrying fenc. */ | |
2099 try_mac = 1; | |
2100 try_unix = 1; | |
2101 for (; p >= ptr && *p != CAR; p--) | |
2102 ; | |
2103 if (p >= ptr) | |
2104 { | |
2105 for (p = ptr; p < ptr + size; ++p) | |
2106 { | |
2107 if (*p == NL) | |
2108 try_unix++; | |
2109 else if (*p == CAR) | |
2110 try_mac++; | |
2111 } | |
2112 if (try_mac > try_unix) | |
2113 fileformat = EOL_MAC; | |
2114 } | |
2115 } | |
2116 } | |
2117 | |
2118 /* No NL found: may use Mac format */ | |
2119 if (fileformat == EOL_UNKNOWN && try_mac) | |
2120 fileformat = EOL_MAC; | |
2121 | |
2122 /* Still nothing found? Use first format in 'ffs' */ | |
2123 if (fileformat == EOL_UNKNOWN) | |
2124 fileformat = default_fileformat(); | |
2125 | |
2126 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2127 if (set_options) |
7 | 2128 set_fileformat(fileformat, OPT_LOCAL); |
2129 } | |
2130 } | |
2131 | |
2132 /* | |
2133 * This loop is executed once for every character read. | |
2134 * Keep it fast! | |
2135 */ | |
2136 if (fileformat == EOL_MAC) | |
2137 { | |
2138 --ptr; | |
2139 while (++ptr, --size >= 0) | |
2140 { | |
2141 /* catch most common case first */ | |
2142 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2143 continue; | |
2144 if (c == NUL) | |
2145 *ptr = NL; /* NULs are replaced by newlines! */ | |
2146 else if (c == NL) | |
2147 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2148 else | |
2149 { | |
2150 if (skip_count == 0) | |
2151 { | |
2152 *ptr = NUL; /* end of line */ | |
2153 len = (colnr_T) (ptr - line_start + 1); | |
2154 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2155 { | |
2156 error = TRUE; | |
2157 break; | |
2158 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2159 #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
|
2160 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
|
2161 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
|
2162 #endif |
7 | 2163 ++lnum; |
2164 if (--read_count == 0) | |
2165 { | |
2166 error = TRUE; /* break loop */ | |
2167 line_start = ptr; /* nothing left to write */ | |
2168 break; | |
2169 } | |
2170 } | |
2171 else | |
2172 --skip_count; | |
2173 line_start = ptr + 1; | |
2174 } | |
2175 } | |
2176 } | |
2177 else | |
2178 { | |
2179 --ptr; | |
2180 while (++ptr, --size >= 0) | |
2181 { | |
2182 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2183 continue; | |
2184 if (c == NUL) | |
2185 *ptr = NL; /* NULs are replaced by newlines! */ | |
2186 else | |
2187 { | |
2188 if (skip_count == 0) | |
2189 { | |
2190 *ptr = NUL; /* end of line */ | |
2191 len = (colnr_T)(ptr - line_start + 1); | |
2192 if (fileformat == EOL_DOS) | |
2193 { | |
2194 if (ptr[-1] == CAR) /* remove CR */ | |
2195 { | |
2196 ptr[-1] = NUL; | |
2197 --len; | |
2198 } | |
2199 /* | |
2200 * Reading in Dos format, but no CR-LF found! | |
2201 * When 'fileformats' includes "unix", delete all | |
2202 * the lines read so far and start all over again. | |
2203 * Otherwise give an error message later. | |
2204 */ | |
2205 else if (ff_error != EOL_DOS) | |
2206 { | |
2207 if ( try_unix | |
2208 && !read_stdin | |
2209 && (read_buffer | |
2210 || lseek(fd, (off_t)0L, SEEK_SET) == 0)) | |
2211 { | |
2212 fileformat = EOL_UNIX; | |
819 | 2213 if (set_options) |
7 | 2214 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2215 file_rewind = TRUE; | |
2216 keep_fileformat = TRUE; | |
2217 goto retry; | |
2218 } | |
2219 ff_error = EOL_DOS; | |
2220 } | |
2221 } | |
2222 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2223 { | |
2224 error = TRUE; | |
2225 break; | |
2226 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2227 #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
|
2228 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
|
2229 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
|
2230 #endif |
7 | 2231 ++lnum; |
2232 if (--read_count == 0) | |
2233 { | |
2234 error = TRUE; /* break loop */ | |
2235 line_start = ptr; /* nothing left to write */ | |
2236 break; | |
2237 } | |
2238 } | |
2239 else | |
2240 --skip_count; | |
2241 line_start = ptr + 1; | |
2242 } | |
2243 } | |
2244 } | |
2245 linerest = (long)(ptr - line_start); | |
2246 ui_breakcheck(); | |
2247 } | |
2248 | |
2249 failed: | |
2250 /* not an error, max. number of lines reached */ | |
2251 if (error && read_count == 0) | |
2252 error = FALSE; | |
2253 | |
2254 /* | |
2255 * If we get EOF in the middle of a line, note the fact and | |
2256 * complete the line ourselves. | |
2257 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2258 */ | |
2259 if (!error | |
2260 && !got_int | |
2261 && linerest != 0 | |
2262 && !(!curbuf->b_p_bin | |
2263 && fileformat == EOL_DOS | |
2264 && *line_start == Ctrl_Z | |
2265 && ptr == line_start + 1)) | |
2266 { | |
819 | 2267 /* remember for when writing */ |
2268 if (set_options) | |
7 | 2269 curbuf->b_p_eol = FALSE; |
2270 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2271 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
|
2272 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2273 error = TRUE; |
2274 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2275 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2276 #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
|
2277 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
|
2278 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
|
2279 #endif |
7 | 2280 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
|
2281 } |
7 | 2282 } |
2283 | |
819 | 2284 if (set_options) |
7 | 2285 save_file_ff(curbuf); /* remember the current file format */ |
2286 | |
2287 #ifdef FEAT_CRYPT | |
2267 | 2288 if (cryptkey != NULL) |
2289 { | |
2290 crypt_pop_state(); | |
2291 if (cryptkey != curbuf->b_p_key) | |
2292 free_crypt_key(cryptkey); | |
2293 /* don't set cryptkey to NULL, it's used below as a flag that | |
2294 * encryption was used */ | |
2295 } | |
7 | 2296 #endif |
2297 | |
2298 #ifdef FEAT_MBYTE | |
819 | 2299 /* If editing a new file: set 'fenc' for the current buffer. |
2300 * Also for ":read ++edit file". */ | |
2301 if (set_options) | |
7 | 2302 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2303 OPT_FREE|OPT_LOCAL, 0); |
7 | 2304 if (fenc_alloced) |
2305 vim_free(fenc); | |
2306 # ifdef USE_ICONV | |
2307 if (iconv_fd != (iconv_t)-1) | |
2308 { | |
2309 iconv_close(iconv_fd); | |
2310 iconv_fd = (iconv_t)-1; | |
2311 } | |
2312 # endif | |
2313 #endif | |
2314 | |
2315 if (!read_buffer && !read_stdin) | |
2316 close(fd); /* errors are ignored */ | |
2003 | 2317 #ifdef HAVE_FD_CLOEXEC |
2318 else | |
2319 { | |
2320 int fdflags = fcntl(fd, F_GETFD); | |
2321 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) | |
2322 fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); | |
2323 } | |
2324 #endif | |
7 | 2325 vim_free(buffer); |
2326 | |
2327 #ifdef HAVE_DUP | |
2328 if (read_stdin) | |
2329 { | |
2330 /* Use stderr for stdin, makes shell commands work. */ | |
2331 close(0); | |
1757 | 2332 ignored = dup(2); |
7 | 2333 } |
2334 #endif | |
2335 | |
2336 #ifdef FEAT_MBYTE | |
2337 if (tmpname != NULL) | |
2338 { | |
2339 mch_remove(tmpname); /* delete converted file */ | |
2340 vim_free(tmpname); | |
2341 } | |
2342 #endif | |
2343 --no_wait_return; /* may wait for return now */ | |
2344 | |
2345 /* | |
2346 * In recovery mode everything but autocommands is skipped. | |
2347 */ | |
2348 if (!recoverymode) | |
2349 { | |
2350 /* need to delete the last line, which comes from the empty buffer */ | |
2351 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2352 { | |
2353 #ifdef FEAT_NETBEANS_INTG | |
2354 netbeansFireChanges = 0; | |
2355 #endif | |
2356 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2357 #ifdef FEAT_NETBEANS_INTG | |
2358 netbeansFireChanges = 1; | |
2359 #endif | |
2360 --linecnt; | |
2361 } | |
2362 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2363 if (filesize == 0) | |
2364 linecnt = 0; | |
2365 if (newfile || read_buffer) | |
1201 | 2366 { |
7 | 2367 redraw_curbuf_later(NOT_VALID); |
1201 | 2368 #ifdef FEAT_DIFF |
2369 /* After reading the text into the buffer the diff info needs to | |
2370 * be updated. */ | |
2371 diff_invalidate(curbuf); | |
2372 #endif | |
2373 #ifdef FEAT_FOLDING | |
2374 /* All folds in the window are invalid now. Mark them for update | |
2375 * before triggering autocommands. */ | |
2376 foldUpdateAll(curwin); | |
2377 #endif | |
2378 } | |
7 | 2379 else if (linecnt) /* appended at least one line */ |
2380 appended_lines_mark(from, linecnt); | |
2381 | |
2382 #ifndef ALWAYS_USE_GUI | |
2383 /* | |
2384 * If we were reading from the same terminal as where messages go, | |
2385 * the screen will have been messed up. | |
2386 * Switch on raw mode now and clear the screen. | |
2387 */ | |
2388 if (read_stdin) | |
2389 { | |
2390 settmode(TMODE_RAW); /* set to raw mode */ | |
2391 starttermcap(); | |
2392 screenclear(); | |
2393 } | |
2394 #endif | |
2395 | |
2396 if (got_int) | |
2397 { | |
2398 if (!(flags & READ_DUMMY)) | |
2399 { | |
2400 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2401 if (newfile) | |
2402 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2403 } | |
2404 msg_scroll = msg_save; | |
2405 #ifdef FEAT_VIMINFO | |
2406 check_marks_read(); | |
2407 #endif | |
2408 return OK; /* an interrupt isn't really an error */ | |
2409 } | |
2410 | |
2411 if (!filtering && !(flags & READ_DUMMY)) | |
2412 { | |
2413 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2414 c = FALSE; | |
2415 | |
2416 #ifdef UNIX | |
2417 # ifdef S_ISFIFO | |
2418 if (S_ISFIFO(perm)) /* fifo or socket */ | |
2419 { | |
2420 STRCAT(IObuff, _("[fifo/socket]")); | |
2421 c = TRUE; | |
2422 } | |
2423 # else | |
2424 # ifdef S_IFIFO | |
2425 if ((perm & S_IFMT) == S_IFIFO) /* fifo */ | |
2426 { | |
2427 STRCAT(IObuff, _("[fifo]")); | |
2428 c = TRUE; | |
2429 } | |
2430 # endif | |
2431 # ifdef S_IFSOCK | |
2432 if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ | |
2433 { | |
2434 STRCAT(IObuff, _("[socket]")); | |
2435 c = TRUE; | |
2436 } | |
2437 # endif | |
2438 # endif | |
1313 | 2439 # ifdef OPEN_CHR_FILES |
2440 if (S_ISCHR(perm)) /* or character special */ | |
2441 { | |
2442 STRCAT(IObuff, _("[character special]")); | |
2443 c = TRUE; | |
2444 } | |
2445 # endif | |
7 | 2446 #endif |
2447 if (curbuf->b_p_ro) | |
2448 { | |
2449 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2450 c = TRUE; | |
2451 } | |
2452 if (read_no_eol_lnum) | |
2453 { | |
2454 msg_add_eol(); | |
2455 c = TRUE; | |
2456 } | |
2457 if (ff_error == EOL_DOS) | |
2458 { | |
2459 STRCAT(IObuff, _("[CR missing]")); | |
2460 c = TRUE; | |
2461 } | |
2462 if (split) | |
2463 { | |
2464 STRCAT(IObuff, _("[long lines split]")); | |
2465 c = TRUE; | |
2466 } | |
2467 #ifdef FEAT_MBYTE | |
2468 if (notconverted) | |
2469 { | |
2470 STRCAT(IObuff, _("[NOT converted]")); | |
2471 c = TRUE; | |
2472 } | |
2473 else if (converted) | |
2474 { | |
2475 STRCAT(IObuff, _("[converted]")); | |
2476 c = TRUE; | |
2477 } | |
2478 #endif | |
2479 #ifdef FEAT_CRYPT | |
2480 if (cryptkey != NULL) | |
2481 { | |
2482 STRCAT(IObuff, _("[crypted]")); | |
2483 c = TRUE; | |
2484 } | |
2485 #endif | |
2486 #ifdef FEAT_MBYTE | |
595 | 2487 if (conv_error != 0) |
2488 { | |
2489 sprintf((char *)IObuff + STRLEN(IObuff), | |
2490 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2491 c = TRUE; |
2492 } | |
2493 else if (illegal_byte > 0) | |
2494 { | |
2495 sprintf((char *)IObuff + STRLEN(IObuff), | |
2496 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2497 c = TRUE; | |
2498 } | |
2499 else | |
2500 #endif | |
2501 if (error) | |
2502 { | |
2503 STRCAT(IObuff, _("[READ ERRORS]")); | |
2504 c = TRUE; | |
2505 } | |
2506 if (msg_add_fileformat(fileformat)) | |
2507 c = TRUE; | |
2508 #ifdef FEAT_CRYPT | |
2509 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
|
2510 msg_add_lines(c, (long)linecnt, filesize |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2511 - CRYPT_MAGIC_LEN |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2512 - crypt_salt_len[use_crypt_method] |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2513 - crypt_seed_len[use_crypt_method]); |
7 | 2514 else |
2515 #endif | |
2516 msg_add_lines(c, (long)linecnt, filesize); | |
2517 | |
2518 vim_free(keep_msg); | |
2519 keep_msg = NULL; | |
2520 msg_scrolled_ign = TRUE; | |
2521 #ifdef ALWAYS_USE_GUI | |
2522 /* Don't show the message when reading stdin, it would end up in a | |
2523 * message box (which might be shown when exiting!) */ | |
2524 if (read_stdin || read_buffer) | |
2525 p = msg_may_trunc(FALSE, IObuff); | |
2526 else | |
2527 #endif | |
2528 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2529 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2530 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2531 /* Need to repeat the message after redrawing when: |
2532 * - When reading from stdin (the screen will be cleared next). | |
2533 * - When restart_edit is set (otherwise there will be a delay | |
2534 * before redrawing). | |
2535 * - When the screen was scrolled but there is no wait-return | |
2536 * prompt. */ | |
679 | 2537 set_keep_msg(p, 0); |
7 | 2538 msg_scrolled_ign = FALSE; |
2539 } | |
2540 | |
2541 /* with errors writing the file requires ":w!" */ | |
2542 if (newfile && (error | |
2543 #ifdef FEAT_MBYTE | |
595 | 2544 || conv_error != 0 |
679 | 2545 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2546 #endif |
2547 )) | |
2548 curbuf->b_p_ro = TRUE; | |
2549 | |
2550 u_clearline(); /* cannot use "U" command after adding lines */ | |
2551 | |
2552 /* | |
2553 * In Ex mode: cursor at last new line. | |
2554 * Otherwise: cursor at first new line. | |
2555 */ | |
2556 if (exmode_active) | |
2557 curwin->w_cursor.lnum = from + linecnt; | |
2558 else | |
2559 curwin->w_cursor.lnum = from + 1; | |
2560 check_cursor_lnum(); | |
2561 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2562 | |
2563 /* | |
2564 * Set '[ and '] marks to the newly read lines. | |
2565 */ | |
2566 curbuf->b_op_start.lnum = from + 1; | |
2567 curbuf->b_op_start.col = 0; | |
2568 curbuf->b_op_end.lnum = from + linecnt; | |
2569 curbuf->b_op_end.col = 0; | |
696 | 2570 |
2571 #ifdef WIN32 | |
2572 /* | |
2573 * Work around a weird problem: When a file has two links (only | |
2574 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2575 * through the other link, the timestamp information may be wrong. |
696 | 2576 * It's correct again after reading the file, thus reset the timestamp |
2577 * here. | |
2578 */ | |
2579 if (newfile && !read_stdin && !read_buffer | |
2580 && mch_stat((char *)fname, &st) >= 0) | |
2581 { | |
2582 buf_store_time(curbuf, &st, fname); | |
2583 curbuf->b_mtime_read = curbuf->b_mtime; | |
2584 } | |
2585 #endif | |
7 | 2586 } |
2587 msg_scroll = msg_save; | |
2588 | |
2589 #ifdef FEAT_VIMINFO | |
2590 /* | |
2591 * Get the marks before executing autocommands, so they can be used there. | |
2592 */ | |
2593 check_marks_read(); | |
2594 #endif | |
2595 | |
2596 /* | |
2597 * Trick: We remember if the last line of the read didn't have | |
2598 * an eol for when writing it again. This is required for | |
2599 * ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | |
2600 */ | |
2601 write_no_eol_lnum = read_no_eol_lnum; | |
2602 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2603 #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
|
2604 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2605 * 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
|
2606 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2607 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
|
2608 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2609 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
|
2610 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2611 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
|
2612 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
|
2613 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2614 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2615 |
167 | 2616 #ifdef FEAT_AUTOCMD |
7 | 2617 if (!read_stdin && !read_buffer) |
2618 { | |
2619 int m = msg_scroll; | |
2620 int n = msg_scrolled; | |
2621 | |
2622 /* Save the fileformat now, otherwise the buffer will be considered | |
2623 * modified if the format/encoding was automatically detected. */ | |
819 | 2624 if (set_options) |
7 | 2625 save_file_ff(curbuf); |
2626 | |
2627 /* | |
2628 * The output from the autocommands should not overwrite anything and | |
2629 * should not be overwritten: Set msg_scroll, restore its value if no | |
2630 * output was done. | |
2631 */ | |
2632 msg_scroll = TRUE; | |
2633 if (filtering) | |
2634 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2635 FALSE, curbuf, eap); | |
2636 else if (newfile) | |
2637 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, | |
2638 FALSE, curbuf, eap); | |
2639 else | |
2640 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2641 FALSE, NULL, eap); | |
2642 if (msg_scrolled == n) | |
2643 msg_scroll = m; | |
2644 #ifdef FEAT_EVAL | |
2645 if (aborting()) /* autocmds may abort script processing */ | |
2646 return FAIL; | |
2647 #endif | |
2648 } | |
2649 #endif | |
2650 | |
2651 if (recoverymode && error) | |
2652 return FAIL; | |
2653 return OK; | |
2654 } | |
2655 | |
1313 | 2656 #ifdef OPEN_CHR_FILES |
2657 /* | |
2658 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2659 * which is the name of files used for process substitution output by | |
2660 * some shells on some operating systems, e.g., bash on SunOS. | |
2661 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2662 */ | |
2663 static int | |
2664 is_dev_fd_file(fname) | |
2665 char_u *fname; | |
2666 { | |
2667 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2668 && VIM_ISDIGIT(fname[8]) | |
2669 && *skipdigits(fname + 9) == NUL | |
2670 && (fname[9] != NUL | |
2671 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2672 } | |
2673 #endif | |
2674 | |
595 | 2675 #ifdef FEAT_MBYTE |
2676 | |
2677 /* | |
2678 * From the current line count and characters read after that, estimate the | |
2679 * line number where we are now. | |
2680 * Used for error messages that include a line number. | |
2681 */ | |
2682 static linenr_T | |
2683 readfile_linenr(linecnt, p, endp) | |
2684 linenr_T linecnt; /* line count before reading more bytes */ | |
2685 char_u *p; /* start of more bytes read */ | |
2686 char_u *endp; /* end of more bytes read */ | |
2687 { | |
2688 char_u *s; | |
2689 linenr_T lnum; | |
2690 | |
2691 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2692 for (s = p; s < endp; ++s) | |
2693 if (*s == '\n') | |
2694 ++lnum; | |
2695 return lnum; | |
2696 } | |
2697 #endif | |
2698 | |
7 | 2699 /* |
612 | 2700 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2701 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2702 * Returns OK or FAIL. |
2703 */ | |
2704 int | |
2705 prep_exarg(eap, buf) | |
2706 exarg_T *eap; | |
2707 buf_T *buf; | |
2708 { | |
2709 eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) | |
2710 #ifdef FEAT_MBYTE | |
2711 + STRLEN(buf->b_p_fenc) | |
2712 #endif | |
2713 + 15)); | |
2714 if (eap->cmd == NULL) | |
2715 return FAIL; | |
2716 | |
2717 #ifdef FEAT_MBYTE | |
2718 sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); | |
2719 eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); | |
612 | 2720 eap->bad_char = buf->b_bad_char; |
7 | 2721 #else |
2722 sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); | |
2723 #endif | |
2724 eap->force_ff = 7; | |
612 | 2725 |
2726 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2727 eap->read_edit = FALSE; |
612 | 2728 eap->forceit = FALSE; |
7 | 2729 return OK; |
2730 } | |
2731 | |
2732 #ifdef FEAT_MBYTE | |
2733 /* | |
2734 * Find next fileencoding to use from 'fileencodings'. | |
2735 * "pp" points to fenc_next. It's advanced to the next item. | |
2736 * When there are no more items, an empty string is returned and *pp is set to | |
2737 * NULL. | |
2738 * When *pp is not set to NULL, the result is in allocated memory. | |
2739 */ | |
2740 static char_u * | |
2741 next_fenc(pp) | |
2742 char_u **pp; | |
2743 { | |
2744 char_u *p; | |
2745 char_u *r; | |
2746 | |
2747 if (**pp == NUL) | |
2748 { | |
2749 *pp = NULL; | |
2750 return (char_u *)""; | |
2751 } | |
2752 p = vim_strchr(*pp, ','); | |
2753 if (p == NULL) | |
2754 { | |
2755 r = enc_canonize(*pp); | |
2756 *pp += STRLEN(*pp); | |
2757 } | |
2758 else | |
2759 { | |
2760 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2761 *pp = p + 1; | |
2762 if (r != NULL) | |
2763 { | |
2764 p = enc_canonize(r); | |
2765 vim_free(r); | |
2766 r = p; | |
2767 } | |
2768 } | |
2769 if (r == NULL) /* out of memory */ | |
2770 { | |
2771 r = (char_u *)""; | |
2772 *pp = NULL; | |
2773 } | |
2774 return r; | |
2775 } | |
2776 | |
2777 # ifdef FEAT_EVAL | |
2778 /* | |
2779 * Convert a file with the 'charconvert' expression. | |
2780 * This closes the file which is to be read, converts it and opens the | |
2781 * resulting file for reading. | |
2782 * Returns name of the resulting converted file (the caller should delete it | |
2783 * after reading it). | |
2784 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2785 */ | |
2786 static char_u * | |
2787 readfile_charconvert(fname, fenc, fdp) | |
2788 char_u *fname; /* name of input file */ | |
2789 char_u *fenc; /* converted from */ | |
2790 int *fdp; /* in/out: file descriptor of file */ | |
2791 { | |
2792 char_u *tmpname; | |
2793 char_u *errmsg = NULL; | |
2794 | |
2795 tmpname = vim_tempname('r'); | |
2796 if (tmpname == NULL) | |
2797 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2798 else | |
2799 { | |
2800 close(*fdp); /* close the input file, ignore errors */ | |
2801 *fdp = -1; | |
2802 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2803 fname, tmpname) == FAIL) | |
2804 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2805 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2806 O_RDONLY | O_EXTRA, 0)) < 0) | |
2807 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2808 } | |
2809 | |
2810 if (errmsg != NULL) | |
2811 { | |
2812 /* Don't use emsg(), it breaks mappings, the retry with | |
2813 * another type of conversion might still work. */ | |
2814 MSG(errmsg); | |
2815 if (tmpname != NULL) | |
2816 { | |
2817 mch_remove(tmpname); /* delete converted file */ | |
2818 vim_free(tmpname); | |
2819 tmpname = NULL; | |
2820 } | |
2821 } | |
2822 | |
2823 /* If the input file is closed, open it (caller should check for error). */ | |
2824 if (*fdp < 0) | |
2825 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2826 | |
2827 return tmpname; | |
2828 } | |
2829 # endif | |
2830 | |
2831 #endif | |
2832 | |
2833 #ifdef FEAT_VIMINFO | |
2834 /* | |
2835 * Read marks for the current buffer from the viminfo file, when we support | |
2836 * buffer marks and the buffer has a name. | |
2837 */ | |
2838 static void | |
2839 check_marks_read() | |
2840 { | |
2841 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2842 && curbuf->b_ffname != NULL) | |
1733 | 2843 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2844 |
2845 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2846 * the ' parameter after opening a buffer. */ | |
2847 curbuf->b_marks_read = TRUE; | |
2848 } | |
2849 #endif | |
2850 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2851 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2852 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2853 * 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
|
2854 * 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
|
2855 * 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
|
2856 */ |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2857 static int |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2858 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
|
2859 char *ptr; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2860 int len; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2861 { |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2862 int i; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2863 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2864 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
|
2865 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2866 if (len < (CRYPT_MAGIC_LEN + crypt_salt_len[i] + crypt_seed_len[i])) |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2867 continue; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2868 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
|
2869 return i; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2870 } |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
2871 |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
2872 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
|
2873 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
|
2874 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
|
2875 |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2876 return -1; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2877 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2878 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2879 /* |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2880 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2881 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2882 * *filesizep are updated. | |
2883 * Return the (new) encryption key, NULL for no encryption. | |
2884 */ | |
2885 static char_u * | |
2267 | 2886 check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask) |
7 | 2887 char_u *cryptkey; /* previous encryption key or NULL */ |
2888 char_u *ptr; /* pointer to read bytes */ | |
2889 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
|
2890 off_t *filesizep; /* nr of bytes used from file */ |
7 | 2891 int newfile; /* editing a new buffer */ |
2267 | 2892 char_u *fname; /* file name to display */ |
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
|
2893 int *did_ask; /* flag: whether already asked for key */ |
7 | 2894 { |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2895 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
|
2896 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2897 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2898 { |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2899 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
|
2900 if (method > 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2901 (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
|
2902 if (cryptkey == NULL && !*did_ask) |
7 | 2903 { |
2904 if (*curbuf->b_p_key) | |
2905 cryptkey = curbuf->b_p_key; | |
2906 else | |
2907 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2908 /* 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
|
2909 * 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
|
2910 * 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
|
2911 * Happens when retrying to detect encoding. */ |
2267 | 2912 smsg((char_u *)_(need_key_msg), fname); |
2913 msg_scroll = TRUE; | |
7 | 2914 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
|
2915 *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
|
2916 |
7 | 2917 /* check if empty key entered */ |
2918 if (cryptkey != NULL && *cryptkey == NUL) | |
2919 { | |
2920 if (cryptkey != curbuf->b_p_key) | |
2921 vim_free(cryptkey); | |
2922 cryptkey = NULL; | |
2923 } | |
2924 } | |
2925 } | |
2926 | |
2927 if (cryptkey != NULL) | |
2928 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2929 int seed_len = crypt_seed_len[method]; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2930 int salt_len = crypt_salt_len[method]; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2931 |
2267 | 2932 crypt_push_state(); |
2933 use_crypt_method = method; | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2934 if (method == 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2935 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
|
2936 else |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2937 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2938 bf_key_init(cryptkey, ptr + CRYPT_MAGIC_LEN, salt_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2939 bf_ofb_init(ptr + CRYPT_MAGIC_LEN + salt_len, seed_len); |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2940 } |
7 | 2941 |
2942 /* Remove magic number from the text */ | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2943 *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2944 *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len; |
2267 | 2945 mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len, |
2946 (size_t)*sizep); | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2947 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2948 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2949 /* 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
|
2950 * the 'key' option, except when starting up (called with -x argument) */ |
7 | 2951 else if (newfile && *curbuf->b_p_key && !starting) |
2952 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); | |
2953 | |
2954 return cryptkey; | |
2955 } | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2956 |
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 * 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
|
2959 * 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
|
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 int |
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_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
|
2963 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
|
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 int method; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2966 char_u buffer[CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2967 + CRYPT_SEED_LEN_MAX + 2]; |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2968 |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2969 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
|
2970 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
|
2971 method = get_crypt_method((char *)buffer, |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2972 CRYPT_MAGIC_LEN + |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2973 CRYPT_SEED_LEN_MAX + |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2974 CRYPT_SALT_LEN_MAX); |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2975 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
|
2976 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
|
2977 |
2267 | 2978 crypt_push_state(); |
2239
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 (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
|
2980 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
|
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 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2983 int salt_len = crypt_salt_len[method]; |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2984 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
|
2985 |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2986 if (fread(buffer, salt_len + seed_len, 1, fp) != 1) |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2987 return FAIL; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2988 bf_key_init(curbuf->b_p_key, buffer, salt_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2989 bf_ofb_init(buffer + salt_len, seed_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
|
2990 } |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2991 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
|
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 /* |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2995 * 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
|
2996 * Returns a pointer to an allocated header of length "*lenp". |
2267 | 2997 * When out of memory returns NULL. |
2998 * Otherwise calls crypt_push_state(), call crypt_pop_state() later. | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2999 */ |
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 * |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3001 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
|
3002 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
|
3003 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
|
3004 { |
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 *header; |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3006 int seed_len = crypt_seed_len[buf->b_p_cm]; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3007 int salt_len = crypt_salt_len[buf->b_p_cm]; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3008 char_u *salt; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3009 char_u *seed; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3010 |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3011 header = alloc_clear(CRYPT_MAGIC_LEN + CRYPT_SALT_LEN_MAX |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3012 + CRYPT_SEED_LEN_MAX + 2); |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3013 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
|
3014 { |
2267 | 3015 crypt_push_state(); |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3016 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
|
3017 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
|
3018 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
|
3019 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
|
3020 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
|
3021 else |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3022 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3023 /* Using blowfish, add salt and seed. */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3024 salt = header + CRYPT_MAGIC_LEN; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3025 seed = salt + salt_len; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3026 sha2_seed(salt, salt_len, seed, seed_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3027 bf_key_init(buf->b_p_key, salt, salt_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3028 bf_ofb_init(seed, seed_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3029 } |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3030 } |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3031 *lenp = CRYPT_MAGIC_LEN + salt_len + seed_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
|
3032 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
|
3033 } |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3034 |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3035 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3036 |
7 | 3037 #ifdef UNIX |
3038 static void | |
3039 set_file_time(fname, atime, mtime) | |
3040 char_u *fname; | |
3041 time_t atime; /* access time */ | |
3042 time_t mtime; /* modification time */ | |
3043 { | |
3044 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3045 struct utimbuf buf; | |
3046 | |
3047 buf.actime = atime; | |
3048 buf.modtime = mtime; | |
3049 (void)utime((char *)fname, &buf); | |
3050 # else | |
3051 # if defined(HAVE_UTIMES) | |
3052 struct timeval tvp[2]; | |
3053 | |
3054 tvp[0].tv_sec = atime; | |
3055 tvp[0].tv_usec = 0; | |
3056 tvp[1].tv_sec = mtime; | |
3057 tvp[1].tv_usec = 0; | |
3058 # ifdef NeXT | |
3059 (void)utimes((char *)fname, tvp); | |
3060 # else | |
3061 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3062 # endif | |
3063 # endif | |
3064 # endif | |
3065 } | |
3066 #endif /* UNIX */ | |
3067 | |
22 | 3068 #if defined(VMS) && !defined(MIN) |
3069 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3070 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3071 #endif | |
3072 | |
7 | 3073 /* |
1303 | 3074 * Return TRUE if a file appears to be read-only from the file permissions. |
3075 */ | |
3076 int | |
3077 check_file_readonly(fname, perm) | |
3078 char_u *fname; /* full path to file */ | |
3079 int perm; /* known permissions on file */ | |
3080 { | |
3081 #ifndef USE_MCH_ACCESS | |
3082 int fd = 0; | |
3083 #endif | |
3084 | |
3085 return ( | |
3086 #ifdef USE_MCH_ACCESS | |
3087 # ifdef UNIX | |
3088 (perm & 0222) == 0 || | |
3089 # endif | |
3090 mch_access((char *)fname, W_OK) | |
3091 #else | |
3092 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3093 ? TRUE : (close(fd), FALSE) | |
3094 #endif | |
3095 ); | |
3096 } | |
3097 | |
3098 | |
3099 /* | |
589 | 3100 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3101 * |
3102 * We do our own buffering here because fwrite() is so slow. | |
3103 * | |
589 | 3104 * If "forceit" is true, we don't care for errors when attempting backups. |
3105 * In case of an error everything possible is done to restore the original | |
1698 | 3106 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3107 * |
3108 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3109 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3110 * |
3111 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3112 * | |
3113 * return FAIL for failure, OK otherwise | |
3114 */ | |
3115 int | |
3116 buf_write(buf, fname, sfname, start, end, eap, append, forceit, | |
3117 reset_changed, filtering) | |
3118 buf_T *buf; | |
3119 char_u *fname; | |
3120 char_u *sfname; | |
3121 linenr_T start, end; | |
3122 exarg_T *eap; /* for forced 'ff' and 'fenc', can be | |
3123 NULL! */ | |
589 | 3124 int append; /* append to the file */ |
7 | 3125 int forceit; |
3126 int reset_changed; | |
3127 int filtering; | |
3128 { | |
3129 int fd; | |
3130 char_u *backup = NULL; | |
3131 int backup_copy = FALSE; /* copy the original file? */ | |
3132 int dobackup; | |
3133 char_u *ffname; | |
3134 char_u *wfname = NULL; /* name of file to write to */ | |
3135 char_u *s; | |
3136 char_u *ptr; | |
3137 char_u c; | |
3138 int len; | |
3139 linenr_T lnum; | |
3140 long nchars; | |
3141 char_u *errmsg = NULL; | |
1947 | 3142 int errmsg_allocated = FALSE; |
7 | 3143 char_u *errnum = NULL; |
3144 char_u *buffer; | |
3145 char_u smallbuf[SMBUFSIZE]; | |
3146 char_u *backup_ext; | |
3147 int bufsize; | |
3148 long perm; /* file permissions */ | |
3149 int retval = OK; | |
3150 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3151 int msg_save = msg_scroll; | |
3152 int overwriting; /* TRUE if writing over original */ | |
3153 int no_eol = FALSE; /* no end-of-line written */ | |
3154 int device = FALSE; /* writing to a device */ | |
3155 struct stat st_old; | |
3156 int prev_got_int = got_int; | |
3157 int file_readonly = FALSE; /* overwritten file is read-only */ | |
3158 static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; | |
3159 #if defined(UNIX) || defined(__EMX__XX) /*XXX fix me sometime? */ | |
3160 int made_writable = FALSE; /* 'w' bit has been set */ | |
3161 #endif | |
3162 /* writing everything */ | |
3163 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3164 #ifdef FEAT_AUTOCMD | |
3165 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3166 #endif | |
3167 int attr; | |
3168 int fileformat; | |
3169 int write_bin; | |
3170 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3171 #ifdef FEAT_MBYTE | |
3172 int converted = FALSE; | |
3173 int notconverted = FALSE; | |
3174 char_u *fenc; /* effective 'fileencoding' */ | |
3175 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3176 #endif | |
3177 #ifdef HAS_BW_FLAGS | |
3178 int wb_flags = 0; | |
3179 #endif | |
3180 #ifdef HAVE_ACL | |
3181 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3182 backup or new file */ | |
3183 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3184 #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
|
3185 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
|
3186 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
|
3187 #endif |
7 | 3188 |
3189 if (fname == NULL || *fname == NUL) /* safety check */ | |
3190 return FAIL; | |
2028 | 3191 if (buf->b_ml.ml_mfp == NULL) |
3192 { | |
3193 /* This can happen during startup when there is a stray "w" in the | |
3194 * vimrc file. */ | |
3195 EMSG(_(e_emptybuf)); | |
3196 return FAIL; | |
3197 } | |
7 | 3198 |
3199 /* | |
3200 * Disallow writing from .exrc and .vimrc in current directory for | |
3201 * security reasons. | |
3202 */ | |
3203 if (check_secure()) | |
3204 return FAIL; | |
3205 | |
3206 /* Avoid a crash for a long name. */ | |
3207 if (STRLEN(fname) >= MAXPATHL) | |
3208 { | |
3209 EMSG(_(e_longname)); | |
3210 return FAIL; | |
3211 } | |
3212 | |
3213 #ifdef FEAT_MBYTE | |
3214 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3215 write_info.bw_conv_buf = NULL; | |
3216 write_info.bw_conv_error = FALSE; | |
1947 | 3217 write_info.bw_conv_error_lnum = 0; |
7 | 3218 write_info.bw_restlen = 0; |
3219 # ifdef USE_ICONV | |
3220 write_info.bw_iconv_fd = (iconv_t)-1; | |
3221 # endif | |
3222 #endif | |
3223 | |
167 | 3224 /* After writing a file changedtick changes but we don't want to display |
3225 * the line. */ | |
3226 ex_no_reprint = TRUE; | |
3227 | |
7 | 3228 /* |
3229 * If there is no file name yet, use the one for the written file. | |
3230 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3231 * Don't do this when the write is for a filter command. | |
589 | 3232 * Don't do this when appending. |
3233 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3234 */ |
633 | 3235 if (buf->b_ffname == NULL |
3236 && reset_changed | |
7 | 3237 && whole |
3238 && buf == curbuf | |
236 | 3239 #ifdef FEAT_QUICKFIX |
3240 && !bt_nofile(buf) | |
3241 #endif | |
7 | 3242 && !filtering |
589 | 3243 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3244 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3245 { | |
633 | 3246 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3247 return FAIL; |
633 | 3248 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3249 } |
3250 | |
3251 if (sfname == NULL) | |
3252 sfname = fname; | |
3253 /* | |
3254 * For Unix: Use the short file name whenever possible. | |
3255 * Avoids problems with networks and when directory names are changed. | |
3256 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3257 * another directory, which we don't detect | |
3258 */ | |
3259 ffname = fname; /* remember full fname */ | |
3260 #ifdef UNIX | |
3261 fname = sfname; | |
3262 #endif | |
3263 | |
3264 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3265 overwriting = TRUE; | |
3266 else | |
3267 overwriting = FALSE; | |
3268 | |
3269 if (exiting) | |
3270 settmode(TMODE_COOK); /* when exiting allow typahead now */ | |
3271 | |
3272 ++no_wait_return; /* don't wait for return yet */ | |
3273 | |
3274 /* | |
3275 * Set '[ and '] marks to the lines to be written. | |
3276 */ | |
3277 buf->b_op_start.lnum = start; | |
3278 buf->b_op_start.col = 0; | |
3279 buf->b_op_end.lnum = end; | |
3280 buf->b_op_end.col = 0; | |
3281 | |
3282 #ifdef FEAT_AUTOCMD | |
3283 { | |
3284 aco_save_T aco; | |
3285 int buf_ffname = FALSE; | |
3286 int buf_sfname = FALSE; | |
3287 int buf_fname_f = FALSE; | |
3288 int buf_fname_s = FALSE; | |
3289 int did_cmd = FALSE; | |
17 | 3290 int nofile_err = FALSE; |
161 | 3291 int empty_memline = (buf->b_ml.ml_mfp == NULL); |
7 | 3292 |
3293 /* | |
3294 * Apply PRE aucocommands. | |
3295 * Set curbuf to the buffer to be written. | |
3296 * Careful: The autocommands may call buf_write() recursively! | |
3297 */ | |
3298 if (ffname == buf->b_ffname) | |
3299 buf_ffname = TRUE; | |
3300 if (sfname == buf->b_sfname) | |
3301 buf_sfname = TRUE; | |
3302 if (fname == buf->b_ffname) | |
3303 buf_fname_f = TRUE; | |
3304 if (fname == buf->b_sfname) | |
3305 buf_fname_s = TRUE; | |
3306 | |
3307 /* set curwin/curbuf to buf and save a few things */ | |
3308 aucmd_prepbuf(&aco, buf); | |
3309 | |
3310 if (append) | |
3311 { | |
3312 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3313 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3314 { |
635 | 3315 #ifdef FEAT_QUICKFIX |
18 | 3316 if (overwriting && bt_nofile(curbuf)) |
17 | 3317 nofile_err = TRUE; |
3318 else | |
635 | 3319 #endif |
17 | 3320 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3321 sfname, sfname, FALSE, curbuf, eap); |
17 | 3322 } |
7 | 3323 } |
3324 else if (filtering) | |
3325 { | |
3326 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3327 NULL, sfname, FALSE, curbuf, eap); | |
3328 } | |
3329 else if (reset_changed && whole) | |
3330 { | |
3331 if (!(did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3332 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3333 { |
635 | 3334 #ifdef FEAT_QUICKFIX |
179 | 3335 if (overwriting && bt_nofile(curbuf)) |
17 | 3336 nofile_err = TRUE; |
3337 else | |
635 | 3338 #endif |
17 | 3339 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3340 sfname, sfname, FALSE, curbuf, eap); |
17 | 3341 } |
7 | 3342 } |
3343 else | |
3344 { | |
3345 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3346 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3347 { |
635 | 3348 #ifdef FEAT_QUICKFIX |
179 | 3349 if (overwriting && bt_nofile(curbuf)) |
17 | 3350 nofile_err = TRUE; |
3351 else | |
635 | 3352 #endif |
17 | 3353 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3354 sfname, sfname, FALSE, curbuf, eap); |
17 | 3355 } |
7 | 3356 } |
3357 | |
3358 /* restore curwin/curbuf and a few other things */ | |
3359 aucmd_restbuf(&aco); | |
3360 | |
3361 /* | |
3362 * In three situations we return here and don't write the file: | |
3363 * 1. the autocommands deleted or unloaded the buffer. | |
3364 * 2. The autocommands abort script processing. | |
3365 * 3. If one of the "Cmd" autocommands was executed. | |
3366 */ | |
3367 if (!buf_valid(buf)) | |
3368 buf = NULL; | |
161 | 3369 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3370 || did_cmd || nofile_err |
3371 #ifdef FEAT_EVAL | |
3372 || aborting() | |
3373 #endif | |
3374 ) | |
7 | 3375 { |
3376 --no_wait_return; | |
3377 msg_scroll = msg_save; | |
17 | 3378 if (nofile_err) |
3379 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3380 | |
532 | 3381 if (nofile_err |
3382 #ifdef FEAT_EVAL | |
3383 || aborting() | |
3384 #endif | |
3385 ) | |
7 | 3386 /* An aborting error, interrupt or exception in the |
3387 * autocommands. */ | |
3388 return FAIL; | |
3389 if (did_cmd) | |
3390 { | |
3391 if (buf == NULL) | |
3392 /* The buffer was deleted. We assume it was written | |
3393 * (can't retry anyway). */ | |
3394 return OK; | |
3395 if (overwriting) | |
3396 { | |
3397 /* Assume the buffer was written, update the timestamp. */ | |
3398 ml_timestamp(buf); | |
589 | 3399 if (append) |
3400 buf->b_flags &= ~BF_NEW; | |
3401 else | |
3402 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3403 } |
589 | 3404 if (reset_changed && buf->b_changed && !append |
39 | 3405 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3406 /* Buffer still changed, the autocommands didn't work |
3407 * properly. */ | |
3408 return FAIL; | |
3409 return OK; | |
3410 } | |
3411 #ifdef FEAT_EVAL | |
3412 if (!aborting()) | |
3413 #endif | |
3414 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3415 return FAIL; | |
3416 } | |
3417 | |
3418 /* | |
3419 * The autocommands may have changed the number of lines in the file. | |
3420 * When writing the whole file, adjust the end. | |
3421 * When writing part of the file, assume that the autocommands only | |
3422 * changed the number of lines that are to be written (tricky!). | |
3423 */ | |
3424 if (buf->b_ml.ml_line_count != old_line_count) | |
3425 { | |
3426 if (whole) /* write all */ | |
3427 end = buf->b_ml.ml_line_count; | |
3428 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3429 end += buf->b_ml.ml_line_count - old_line_count; | |
3430 else /* less lines */ | |
3431 { | |
3432 end -= old_line_count - buf->b_ml.ml_line_count; | |
3433 if (end < start) | |
3434 { | |
3435 --no_wait_return; | |
3436 msg_scroll = msg_save; | |
3437 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3438 return FAIL; | |
3439 } | |
3440 } | |
3441 } | |
3442 | |
3443 /* | |
3444 * The autocommands may have changed the name of the buffer, which may | |
3445 * be kept in fname, ffname and sfname. | |
3446 */ | |
3447 if (buf_ffname) | |
3448 ffname = buf->b_ffname; | |
3449 if (buf_sfname) | |
3450 sfname = buf->b_sfname; | |
3451 if (buf_fname_f) | |
3452 fname = buf->b_ffname; | |
3453 if (buf_fname_s) | |
3454 fname = buf->b_sfname; | |
3455 } | |
3456 #endif | |
3457 | |
3458 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3459 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3460 { |
3461 if (whole) | |
3462 { | |
3463 /* | |
3464 * b_changed can be 0 after an undo, but we still need to write | |
3465 * the buffer to NetBeans. | |
3466 */ | |
3467 if (buf->b_changed || isNetbeansModified(buf)) | |
3468 { | |
33 | 3469 --no_wait_return; /* may wait for return now */ |
3470 msg_scroll = msg_save; | |
3471 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3472 return retval; |
3473 } | |
3474 else | |
3475 { | |
3476 errnum = (char_u *)"E656: "; | |
1653 | 3477 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3478 buffer = NULL; |
3479 goto fail; | |
3480 } | |
3481 } | |
3482 else | |
3483 { | |
3484 errnum = (char_u *)"E657: "; | |
3485 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3486 buffer = NULL; | |
3487 goto fail; | |
3488 } | |
3489 } | |
3490 #endif | |
3491 | |
3492 if (shortmess(SHM_OVER) && !exiting) | |
3493 msg_scroll = FALSE; /* overwrite previous file message */ | |
3494 else | |
3495 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3496 if (!filtering) | |
3497 filemess(buf, | |
3498 #ifndef UNIX | |
3499 sfname, | |
3500 #else | |
3501 fname, | |
3502 #endif | |
3503 (char_u *)"", 0); /* show that we are busy */ | |
3504 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3505 | |
3506 buffer = alloc(BUFSIZE); | |
3507 if (buffer == NULL) /* can't allocate big buffer, use small | |
3508 * one (to be able to write when out of | |
3509 * memory) */ | |
3510 { | |
3511 buffer = smallbuf; | |
3512 bufsize = SMBUFSIZE; | |
3513 } | |
3514 else | |
3515 bufsize = BUFSIZE; | |
3516 | |
3517 /* | |
3518 * Get information about original file (if there is one). | |
3519 */ | |
3520 #if defined(UNIX) && !defined(ARCHIE) | |
1438 | 3521 st_old.st_dev = 0; |
3522 st_old.st_ino = 0; | |
7 | 3523 perm = -1; |
3524 if (mch_stat((char *)fname, &st_old) < 0) | |
3525 newfile = TRUE; | |
3526 else | |
3527 { | |
3528 perm = st_old.st_mode; | |
3529 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3530 { | |
3531 if (S_ISDIR(st_old.st_mode)) | |
3532 { | |
3533 errnum = (char_u *)"E502: "; | |
3534 errmsg = (char_u *)_("is a directory"); | |
3535 goto fail; | |
3536 } | |
3537 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3538 { | |
3539 errnum = (char_u *)"E503: "; | |
3540 errmsg = (char_u *)_("is not a file or writable device"); | |
3541 goto fail; | |
3542 } | |
3543 /* It's a device of some kind (or a fifo) which we can write to | |
3544 * but for which we can't make a backup. */ | |
3545 device = TRUE; | |
3546 newfile = TRUE; | |
3547 perm = -1; | |
3548 } | |
3549 } | |
3550 #else /* !UNIX */ | |
3551 /* | |
3552 * Check for a writable device name. | |
3553 */ | |
3554 c = mch_nodetype(fname); | |
3555 if (c == NODE_OTHER) | |
3556 { | |
3557 errnum = (char_u *)"E503: "; | |
3558 errmsg = (char_u *)_("is not a file or writable device"); | |
3559 goto fail; | |
3560 } | |
3561 if (c == NODE_WRITABLE) | |
3562 { | |
1004 | 3563 # if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
3564 /* MS-Windows allows opening a device, but we will probably get stuck | |
3565 * trying to write to it. */ | |
3566 if (!p_odev) | |
3567 { | |
3568 errnum = (char_u *)"E796: "; | |
3569 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3570 goto fail; | |
3571 } | |
3572 # endif | |
7 | 3573 device = TRUE; |
3574 newfile = TRUE; | |
3575 perm = -1; | |
3576 } | |
3577 else | |
3578 { | |
3579 perm = mch_getperm(fname); | |
3580 if (perm < 0) | |
3581 newfile = TRUE; | |
3582 else if (mch_isdir(fname)) | |
3583 { | |
3584 errnum = (char_u *)"E502: "; | |
3585 errmsg = (char_u *)_("is a directory"); | |
3586 goto fail; | |
3587 } | |
3588 if (overwriting) | |
3589 (void)mch_stat((char *)fname, &st_old); | |
3590 } | |
3591 #endif /* !UNIX */ | |
3592 | |
3593 if (!device && !newfile) | |
3594 { | |
3595 /* | |
3596 * Check if the file is really writable (when renaming the file to | |
3597 * make a backup we won't discover it later). | |
3598 */ | |
1303 | 3599 file_readonly = check_file_readonly(fname, (int)perm); |
3600 | |
7 | 3601 if (!forceit && file_readonly) |
3602 { | |
3603 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3604 { | |
3605 errnum = (char_u *)"E504: "; | |
3606 errmsg = (char_u *)_(err_readonly); | |
3607 } | |
3608 else | |
3609 { | |
3610 errnum = (char_u *)"E505: "; | |
3611 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3612 } | |
3613 goto fail; | |
3614 } | |
3615 | |
3616 /* | |
3617 * Check if the timestamp hasn't changed since reading the file. | |
3618 */ | |
3619 if (overwriting) | |
3620 { | |
3621 retval = check_mtime(buf, &st_old); | |
3622 if (retval == FAIL) | |
3623 goto fail; | |
3624 } | |
3625 } | |
3626 | |
3627 #ifdef HAVE_ACL | |
3628 /* | |
3629 * For systems that support ACL: get the ACL from the original file. | |
3630 */ | |
3631 if (!newfile) | |
3632 acl = mch_get_acl(fname); | |
3633 #endif | |
3634 | |
3635 /* | |
3636 * If 'backupskip' is not empty, don't make a backup for some files. | |
3637 */ | |
3638 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3639 #ifdef FEAT_WILDIGN | |
3640 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3641 dobackup = FALSE; | |
3642 #endif | |
3643 | |
3644 /* | |
3645 * Save the value of got_int and reset it. We don't want a previous | |
3646 * interruption cancel writing, only hitting CTRL-C while writing should | |
3647 * abort it. | |
3648 */ | |
3649 prev_got_int = got_int; | |
3650 got_int = FALSE; | |
3651 | |
3652 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3653 buf->b_saving = TRUE; | |
3654 | |
3655 /* | |
3656 * If we are not appending or filtering, the file exists, and the | |
3657 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3658 * When 'patchmode' is set also make a backup when appending. | |
3659 * | |
3660 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3661 * off. This helps when editing large files on almost-full disks. | |
3662 */ | |
3663 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3664 { | |
3665 #if defined(UNIX) || defined(WIN32) | |
3666 struct stat st; | |
3667 #endif | |
3668 | |
3669 if ((bkc_flags & BKC_YES) || append) /* "yes" */ | |
3670 backup_copy = TRUE; | |
3671 #if defined(UNIX) || defined(WIN32) | |
3672 else if ((bkc_flags & BKC_AUTO)) /* "auto" */ | |
3673 { | |
3674 int i; | |
3675 | |
3676 # ifdef UNIX | |
3677 /* | |
3678 * Don't rename the file when: | |
3679 * - it's a hard link | |
3680 * - it's a symbolic link | |
3681 * - we don't have write permission in the directory | |
3682 * - we can't set the owner/group of the new file | |
3683 */ | |
3684 if (st_old.st_nlink > 1 | |
3685 || mch_lstat((char *)fname, &st) < 0 | |
3686 || st.st_dev != st_old.st_dev | |
557 | 3687 || st.st_ino != st_old.st_ino |
3688 # ifndef HAVE_FCHOWN | |
3689 || st.st_uid != st_old.st_uid | |
3690 || st.st_gid != st_old.st_gid | |
3691 # endif | |
3692 ) | |
7 | 3693 backup_copy = TRUE; |
3694 else | |
696 | 3695 # else |
3696 # ifdef WIN32 | |
3697 /* On NTFS file systems hard links are possible. */ | |
3698 if (mch_is_linked(fname)) | |
3699 backup_copy = TRUE; | |
3700 else | |
3701 # endif | |
7 | 3702 # endif |
3703 { | |
3704 /* | |
3705 * Check if we can create a file and set the owner/group to | |
3706 * the ones from the original file. | |
3707 * First find a file name that doesn't exist yet (use some | |
3708 * arbitrary numbers). | |
3709 */ | |
3710 STRCPY(IObuff, fname); | |
3711 for (i = 4913; ; i += 123) | |
3712 { | |
3713 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3714 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3715 break; |
3716 } | |
557 | 3717 fd = mch_open((char *)IObuff, |
3718 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3719 if (fd < 0) /* can't write in directory */ |
3720 backup_copy = TRUE; | |
3721 else | |
3722 { | |
3723 # ifdef UNIX | |
557 | 3724 # ifdef HAVE_FCHOWN |
1757 | 3725 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3726 # endif |
7 | 3727 if (mch_stat((char *)IObuff, &st) < 0 |
3728 || st.st_uid != st_old.st_uid | |
3729 || st.st_gid != st_old.st_gid | |
1877 | 3730 || (long)st.st_mode != perm) |
7 | 3731 backup_copy = TRUE; |
3732 # endif | |
567 | 3733 /* Close the file before removing it, on MS-Windows we |
3734 * can't delete an open file. */ | |
3735 close(fd); | |
7 | 3736 mch_remove(IObuff); |
3737 } | |
3738 } | |
3739 } | |
3740 | |
3741 # ifdef UNIX | |
3742 /* | |
3743 * Break symlinks and/or hardlinks if we've been asked to. | |
3744 */ | |
3745 if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK)) | |
3746 { | |
3747 int lstat_res; | |
3748 | |
3749 lstat_res = mch_lstat((char *)fname, &st); | |
3750 | |
3751 /* Symlinks. */ | |
3752 if ((bkc_flags & BKC_BREAKSYMLINK) | |
3753 && lstat_res == 0 | |
3754 && st.st_ino != st_old.st_ino) | |
3755 backup_copy = FALSE; | |
3756 | |
3757 /* Hardlinks. */ | |
3758 if ((bkc_flags & BKC_BREAKHARDLINK) | |
3759 && st_old.st_nlink > 1 | |
3760 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3761 backup_copy = FALSE; | |
3762 } | |
3763 #endif | |
3764 | |
3765 #endif | |
3766 | |
3767 /* make sure we have a valid backup extension to use */ | |
3768 if (*p_bex == NUL) | |
3769 { | |
3770 #ifdef RISCOS | |
3771 backup_ext = (char_u *)"/bak"; | |
3772 #else | |
3773 backup_ext = (char_u *)".bak"; | |
3774 #endif | |
3775 } | |
3776 else | |
3777 backup_ext = p_bex; | |
3778 | |
3779 if (backup_copy | |
3780 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3781 { | |
3782 int bfd; | |
3783 char_u *copybuf, *wp; | |
3784 int some_error = FALSE; | |
3785 struct stat st_new; | |
3786 char_u *dirp; | |
3787 char_u *rootname; | |
344 | 3788 #if defined(UNIX) && !defined(SHORT_FNAME) |
7 | 3789 int did_set_shortname; |
3790 #endif | |
3791 | |
3792 copybuf = alloc(BUFSIZE + 1); | |
3793 if (copybuf == NULL) | |
3794 { | |
3795 some_error = TRUE; /* out of memory */ | |
3796 goto nobackup; | |
3797 } | |
3798 | |
3799 /* | |
3800 * Try to make the backup in each directory in the 'bdir' option. | |
3801 * | |
3802 * Unix semantics has it, that we may have a writable file, | |
3803 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3804 * - the directory is not writable, | |
3805 * - the file may be a symbolic link, | |
3806 * - the file may belong to another user/group, etc. | |
3807 * | |
3808 * For these reasons, the existing writable file must be truncated | |
3809 * and reused. Creation of a backup COPY will be attempted. | |
3810 */ | |
3811 dirp = p_bdir; | |
3812 while (*dirp) | |
3813 { | |
3814 #ifdef UNIX | |
3815 st_new.st_ino = 0; | |
3816 st_new.st_dev = 0; | |
3817 st_new.st_gid = 0; | |
3818 #endif | |
3819 | |
3820 /* | |
3821 * Isolate one directory name, using an entry in 'bdir'. | |
3822 */ | |
3823 (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); | |
3824 rootname = get_file_in_dir(fname, copybuf); | |
3825 if (rootname == NULL) | |
3826 { | |
3827 some_error = TRUE; /* out of memory */ | |
3828 goto nobackup; | |
3829 } | |
3830 | |
344 | 3831 #if defined(UNIX) && !defined(SHORT_FNAME) |
7 | 3832 did_set_shortname = FALSE; |
3833 #endif | |
3834 | |
3835 /* | |
3836 * May try twice if 'shortname' not set. | |
3837 */ | |
3838 for (;;) | |
3839 { | |
3840 /* | |
3841 * Make backup file name. | |
3842 */ | |
3843 backup = buf_modname( | |
3844 #ifdef SHORT_FNAME | |
3845 TRUE, | |
3846 #else | |
3847 (buf->b_p_sn || buf->b_shortname), | |
3848 #endif | |
3849 rootname, backup_ext, FALSE); | |
3850 if (backup == NULL) | |
3851 { | |
3852 vim_free(rootname); | |
3853 some_error = TRUE; /* out of memory */ | |
3854 goto nobackup; | |
3855 } | |
3856 | |
3857 /* | |
3858 * Check if backup file already exists. | |
3859 */ | |
3860 if (mch_stat((char *)backup, &st_new) >= 0) | |
3861 { | |
3862 #ifdef UNIX | |
3863 /* | |
3864 * Check if backup file is same as original file. | |
3865 * May happen when modname() gave the same file back. | |
3866 * E.g. silly link, or file name-length reached. | |
3867 * If we don't check here, we either ruin the file | |
3868 * when copying or erase it after writing. jw. | |
3869 */ | |
3870 if (st_new.st_dev == st_old.st_dev | |
3871 && st_new.st_ino == st_old.st_ino) | |
3872 { | |
3873 vim_free(backup); | |
3874 backup = NULL; /* no backup file to delete */ | |
3875 # ifndef SHORT_FNAME | |
3876 /* | |
3877 * may try again with 'shortname' set | |
3878 */ | |
3879 if (!(buf->b_shortname || buf->b_p_sn)) | |
3880 { | |
3881 buf->b_shortname = TRUE; | |
3882 did_set_shortname = TRUE; | |
3883 continue; | |
3884 } | |
3885 /* setting shortname didn't help */ | |
3886 if (did_set_shortname) | |
3887 buf->b_shortname = FALSE; | |
3888 # endif | |
3889 break; | |
3890 } | |
3891 #endif | |
3892 | |
3893 /* | |
3894 * If we are not going to keep the backup file, don't | |
3895 * delete an existing one, try to use another name. | |
3896 * Change one character, just before the extension. | |
3897 */ | |
3898 if (!p_bk) | |
3899 { | |
3900 wp = backup + STRLEN(backup) - 1 | |
3901 - STRLEN(backup_ext); | |
3902 if (wp < backup) /* empty file name ??? */ | |
3903 wp = backup; | |
3904 *wp = 'z'; | |
3905 while (*wp > 'a' | |
3906 && mch_stat((char *)backup, &st_new) >= 0) | |
3907 --*wp; | |
3908 /* They all exist??? Must be something wrong. */ | |
3909 if (*wp == 'a') | |
3910 { | |
3911 vim_free(backup); | |
3912 backup = NULL; | |
3913 } | |
3914 } | |
3915 } | |
3916 break; | |
3917 } | |
3918 vim_free(rootname); | |
3919 | |
3920 /* | |
3921 * Try to create the backup file | |
3922 */ | |
3923 if (backup != NULL) | |
3924 { | |
3925 /* remove old backup, if present */ | |
3926 mch_remove(backup); | |
3927 /* Open with O_EXCL to avoid the file being created while | |
3928 * we were sleeping (symlink hacker attack?) */ | |
3929 bfd = mch_open((char *)backup, | |
557 | 3930 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
3931 perm & 0777); | |
7 | 3932 if (bfd < 0) |
3933 { | |
3934 vim_free(backup); | |
3935 backup = NULL; | |
3936 } | |
3937 else | |
3938 { | |
3939 /* set file protection same as original file, but | |
3940 * strip s-bit */ | |
3941 (void)mch_setperm(backup, perm & 0777); | |
3942 | |
3943 #ifdef UNIX | |
3944 /* | |
3945 * Try to set the group of the backup same as the | |
3946 * original file. If this fails, set the protection | |
3947 * bits for the group same as the protection bits for | |
3948 * others. | |
3949 */ | |
557 | 3950 if (st_new.st_gid != st_old.st_gid |
7 | 3951 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 3952 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 3953 # endif |
3954 ) | |
3955 mch_setperm(backup, | |
3956 (perm & 0707) | ((perm & 07) << 3)); | |
1583 | 3957 # ifdef HAVE_SELINUX |
3958 mch_copy_sec(fname, backup); | |
3959 # endif | |
7 | 3960 #endif |
3961 | |
3962 /* | |
3963 * copy the file. | |
3964 */ | |
3965 write_info.bw_fd = bfd; | |
3966 write_info.bw_buf = copybuf; | |
3967 #ifdef HAS_BW_FLAGS | |
3968 write_info.bw_flags = FIO_NOCONVERT; | |
3969 #endif | |
3970 while ((write_info.bw_len = vim_read(fd, copybuf, | |
3971 BUFSIZE)) > 0) | |
3972 { | |
3973 if (buf_write_bytes(&write_info) == FAIL) | |
3974 { | |
3975 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
3976 break; | |
3977 } | |
3978 ui_breakcheck(); | |
3979 if (got_int) | |
3980 { | |
3981 errmsg = (char_u *)_(e_interr); | |
3982 break; | |
3983 } | |
3984 } | |
3985 | |
3986 if (close(bfd) < 0 && errmsg == NULL) | |
3987 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
3988 if (write_info.bw_len < 0) | |
3989 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
3990 #ifdef UNIX | |
3991 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
3992 #endif | |
3993 #ifdef HAVE_ACL | |
3994 mch_set_acl(backup, acl); | |
3995 #endif | |
1583 | 3996 #ifdef HAVE_SELINUX |
3997 mch_copy_sec(fname, backup); | |
3998 #endif | |
7 | 3999 break; |
4000 } | |
4001 } | |
4002 } | |
4003 nobackup: | |
4004 close(fd); /* ignore errors for closing read file */ | |
4005 vim_free(copybuf); | |
4006 | |
4007 if (backup == NULL && errmsg == NULL) | |
4008 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4009 /* ignore errors when forceit is TRUE */ | |
4010 if ((some_error || errmsg != NULL) && !forceit) | |
4011 { | |
4012 retval = FAIL; | |
4013 goto fail; | |
4014 } | |
4015 errmsg = NULL; | |
4016 } | |
4017 else | |
4018 { | |
4019 char_u *dirp; | |
4020 char_u *p; | |
4021 char_u *rootname; | |
4022 | |
4023 /* | |
4024 * Make a backup by renaming the original file. | |
4025 */ | |
4026 /* | |
4027 * If 'cpoptions' includes the "W" flag, we don't want to | |
4028 * overwrite a read-only file. But rename may be possible | |
4029 * anyway, thus we need an extra check here. | |
4030 */ | |
4031 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4032 { | |
4033 errnum = (char_u *)"E504: "; | |
4034 errmsg = (char_u *)_(err_readonly); | |
4035 goto fail; | |
4036 } | |
4037 | |
4038 /* | |
4039 * | |
4040 * Form the backup file name - change path/fo.o.h to | |
4041 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4042 * that works is used. | |
4043 */ | |
4044 dirp = p_bdir; | |
4045 while (*dirp) | |
4046 { | |
4047 /* | |
4048 * Isolate one directory name and make the backup file name. | |
4049 */ | |
4050 (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); | |
4051 rootname = get_file_in_dir(fname, IObuff); | |
4052 if (rootname == NULL) | |
4053 backup = NULL; | |
4054 else | |
4055 { | |
4056 backup = buf_modname( | |
4057 #ifdef SHORT_FNAME | |
4058 TRUE, | |
4059 #else | |
4060 (buf->b_p_sn || buf->b_shortname), | |
4061 #endif | |
4062 rootname, backup_ext, FALSE); | |
4063 vim_free(rootname); | |
4064 } | |
4065 | |
4066 if (backup != NULL) | |
4067 { | |
4068 /* | |
4069 * If we are not going to keep the backup file, don't | |
4070 * delete an existing one, try to use another name. | |
4071 * Change one character, just before the extension. | |
4072 */ | |
4073 if (!p_bk && mch_getperm(backup) >= 0) | |
4074 { | |
4075 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4076 if (p < backup) /* empty file name ??? */ | |
4077 p = backup; | |
4078 *p = 'z'; | |
4079 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4080 --*p; | |
4081 /* They all exist??? Must be something wrong! */ | |
4082 if (*p == 'a') | |
4083 { | |
4084 vim_free(backup); | |
4085 backup = NULL; | |
4086 } | |
4087 } | |
4088 } | |
4089 if (backup != NULL) | |
4090 { | |
4091 /* | |
531 | 4092 * Delete any existing backup and move the current version |
4093 * to the backup. For safety, we don't remove the backup | |
4094 * until the write has finished successfully. And if the | |
4095 * 'backup' option is set, leave it around. | |
7 | 4096 */ |
4097 /* | |
4098 * If the renaming of the original file to the backup file | |
4099 * works, quit here. | |
4100 */ | |
4101 if (vim_rename(fname, backup) == 0) | |
4102 break; | |
4103 | |
4104 vim_free(backup); /* don't do the rename below */ | |
4105 backup = NULL; | |
4106 } | |
4107 } | |
4108 if (backup == NULL && !forceit) | |
4109 { | |
4110 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4111 goto fail; | |
4112 } | |
4113 } | |
4114 } | |
4115 | |
4116 #if defined(UNIX) && !defined(ARCHIE) | |
4117 /* When using ":w!" and the file was read-only: make it writable */ | |
4118 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4119 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4120 { | |
4121 perm |= 0200; | |
4122 (void)mch_setperm(fname, perm); | |
4123 made_writable = TRUE; | |
4124 } | |
4125 #endif | |
4126 | |
819 | 4127 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4128 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4129 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4130 { |
4131 buf->b_p_ro = FALSE; | |
4132 #ifdef FEAT_TITLE | |
4133 need_maketitle = TRUE; /* set window title later */ | |
4134 #endif | |
4135 #ifdef FEAT_WINDOWS | |
4136 status_redraw_all(); /* redraw status lines later */ | |
4137 #endif | |
4138 } | |
4139 | |
4140 if (end > buf->b_ml.ml_line_count) | |
4141 end = buf->b_ml.ml_line_count; | |
4142 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4143 start = end + 1; | |
4144 | |
4145 /* | |
4146 * If the original file is being overwritten, there is a small chance that | |
4147 * we crash in the middle of writing. Therefore the file is preserved now. | |
4148 * This makes all block numbers positive so that recovery does not need | |
4149 * the original file. | |
4150 * Don't do this if there is a backup file and we are exiting. | |
4151 */ | |
39 | 4152 if (reset_changed && !newfile && overwriting |
7 | 4153 && !(exiting && backup != NULL)) |
4154 { | |
4155 ml_preserve(buf, FALSE); | |
4156 if (got_int) | |
4157 { | |
4158 errmsg = (char_u *)_(e_interr); | |
4159 goto restore_backup; | |
4160 } | |
4161 } | |
4162 | |
4163 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
4164 /* | |
4165 * Before risking to lose the original file verify if there's | |
4166 * a resource fork to preserve, and if cannot be done warn | |
4167 * the users. This happens when overwriting without backups. | |
4168 */ | |
4169 if (backup == NULL && overwriting && !append) | |
4170 if (mch_has_resource_fork(fname)) | |
4171 { | |
4172 errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)"); | |
4173 goto restore_backup; | |
4174 } | |
4175 #endif | |
4176 | |
4177 #ifdef VMS | |
4178 vms_remove_version(fname); /* remove version */ | |
4179 #endif | |
1651 | 4180 /* Default: write the file directly. May write to a temp file for |
7 | 4181 * multi-byte conversion. */ |
4182 wfname = fname; | |
4183 | |
4184 #ifdef FEAT_MBYTE | |
4185 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4186 if (eap != NULL && eap->force_enc != 0) | |
4187 { | |
4188 fenc = eap->cmd + eap->force_enc; | |
4189 fenc = enc_canonize(fenc); | |
4190 fenc_tofree = fenc; | |
4191 } | |
4192 else | |
4193 fenc = buf->b_p_fenc; | |
4194 | |
4195 /* | |
1948 | 4196 * Check if the file needs to be converted. |
7 | 4197 */ |
1948 | 4198 converted = need_conversion(fenc); |
7 | 4199 |
4200 /* | |
4201 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4202 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4203 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4204 */ | |
4205 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4206 { | |
4207 wb_flags = get_fio_flags(fenc); | |
4208 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4209 { | |
4210 /* Need to allocate a buffer to translate into. */ | |
4211 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4212 write_info.bw_conv_buflen = bufsize * 2; | |
4213 else /* FIO_UCS4 */ | |
4214 write_info.bw_conv_buflen = bufsize * 4; | |
4215 write_info.bw_conv_buf | |
4216 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4217 if (write_info.bw_conv_buf == NULL) | |
4218 end = 0; | |
4219 } | |
4220 } | |
4221 | |
4222 # ifdef WIN3264 | |
4223 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4224 { | |
4225 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4226 write_info.bw_conv_buflen = bufsize * 4; | |
4227 write_info.bw_conv_buf | |
4228 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4229 if (write_info.bw_conv_buf == NULL) | |
4230 end = 0; | |
4231 } | |
4232 # endif | |
4233 | |
4234 # ifdef MACOS_X | |
4235 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) | |
4236 { | |
4237 write_info.bw_conv_buflen = bufsize * 3; | |
4238 write_info.bw_conv_buf | |
4239 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4240 if (write_info.bw_conv_buf == NULL) | |
4241 end = 0; | |
4242 } | |
4243 # endif | |
4244 | |
4245 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4246 if (converted && wb_flags == 0) | |
4247 { | |
4248 # ifdef USE_ICONV | |
4249 /* | |
4250 * Use iconv() conversion when conversion is needed and it's not done | |
4251 * internally. | |
4252 */ | |
4253 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4254 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4255 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4256 { | |
4257 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4258 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4259 write_info.bw_conv_buf | |
4260 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4261 if (write_info.bw_conv_buf == NULL) | |
4262 end = 0; | |
4263 write_info.bw_first = TRUE; | |
4264 } | |
4265 # ifdef FEAT_EVAL | |
4266 else | |
4267 # endif | |
4268 # endif | |
4269 | |
4270 # ifdef FEAT_EVAL | |
4271 /* | |
4272 * When the file needs to be converted with 'charconvert' after | |
4273 * writing, write to a temp file instead and let the conversion | |
4274 * overwrite the original file. | |
4275 */ | |
4276 if (*p_ccv != NUL) | |
4277 { | |
4278 wfname = vim_tempname('w'); | |
4279 if (wfname == NULL) /* Can't write without a tempfile! */ | |
4280 { | |
4281 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4282 goto restore_backup; | |
4283 } | |
4284 } | |
4285 # endif | |
4286 } | |
4287 # endif | |
4288 if (converted && wb_flags == 0 | |
4289 # ifdef USE_ICONV | |
4290 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4291 # endif | |
4292 # ifdef FEAT_EVAL | |
4293 && wfname == fname | |
4294 # endif | |
4295 ) | |
4296 { | |
4297 if (!forceit) | |
4298 { | |
4299 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4300 goto restore_backup; | |
4301 } | |
4302 notconverted = TRUE; | |
4303 } | |
4304 #endif | |
4305 | |
4306 /* | |
4307 * Open the file "wfname" for writing. | |
4308 * We may try to open the file twice: If we can't write to the | |
4309 * file and forceit is TRUE we delete the existing file and try to create | |
4310 * a new one. If this still fails we may have lost the original file! | |
4311 * (this may happen when the user reached his quotum for number of files). | |
4312 * Appending will fail if the file does not exist and forceit is FALSE. | |
4313 */ | |
4314 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append | |
4315 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) | |
4316 : (O_CREAT | O_TRUNC)) | |
216 | 4317 , perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4318 { |
4319 /* | |
4320 * A forced write will try to create a new file if the old one is | |
4321 * still readonly. This may also happen when the directory is | |
4322 * read-only. In that case the mch_remove() will fail. | |
4323 */ | |
4324 if (errmsg == NULL) | |
4325 { | |
4326 #ifdef UNIX | |
4327 struct stat st; | |
4328 | |
4329 /* Don't delete the file when it's a hard or symbolic link. */ | |
4330 if ((!newfile && st_old.st_nlink > 1) | |
4331 || (mch_lstat((char *)fname, &st) == 0 | |
4332 && (st.st_dev != st_old.st_dev | |
4333 || st.st_ino != st_old.st_ino))) | |
4334 errmsg = (char_u *)_("E166: Can't open linked file for writing"); | |
4335 else | |
4336 #endif | |
4337 { | |
4338 errmsg = (char_u *)_("E212: Can't open file for writing"); | |
4339 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL | |
4340 && perm >= 0) | |
4341 { | |
4342 #ifdef UNIX | |
4343 /* we write to the file, thus it should be marked | |
4344 writable after all */ | |
4345 if (!(perm & 0200)) | |
4346 made_writable = TRUE; | |
4347 perm |= 0200; | |
4348 if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) | |
4349 perm &= 0777; | |
4350 #endif | |
4351 if (!append) /* don't remove when appending */ | |
4352 mch_remove(wfname); | |
4353 continue; | |
4354 } | |
4355 } | |
4356 } | |
4357 | |
4358 restore_backup: | |
4359 { | |
4360 struct stat st; | |
4361 | |
4362 /* | |
4363 * If we failed to open the file, we don't need a backup. Throw it | |
4364 * away. If we moved or removed the original file try to put the | |
4365 * backup in its place. | |
4366 */ | |
4367 if (backup != NULL && wfname == fname) | |
4368 { | |
4369 if (backup_copy) | |
4370 { | |
4371 /* | |
4372 * There is a small chance that we removed the original, | |
4373 * try to move the copy in its place. | |
4374 * This may not work if the vim_rename() fails. | |
4375 * In that case we leave the copy around. | |
4376 */ | |
4377 /* If file does not exist, put the copy in its place */ | |
4378 if (mch_stat((char *)fname, &st) < 0) | |
4379 vim_rename(backup, fname); | |
4380 /* if original file does exist throw away the copy */ | |
4381 if (mch_stat((char *)fname, &st) >= 0) | |
4382 mch_remove(backup); | |
4383 } | |
4384 else | |
4385 { | |
4386 /* try to put the original file back */ | |
4387 vim_rename(backup, fname); | |
4388 } | |
4389 } | |
4390 | |
4391 /* if original file no longer exists give an extra warning */ | |
4392 if (!newfile && mch_stat((char *)fname, &st) < 0) | |
4393 end = 0; | |
4394 } | |
4395 | |
4396 #ifdef FEAT_MBYTE | |
4397 if (wfname != fname) | |
4398 vim_free(wfname); | |
4399 #endif | |
4400 goto fail; | |
4401 } | |
4402 errmsg = NULL; | |
4403 | |
4404 #if defined(MACOS_CLASSIC) || defined(WIN3264) | |
4405 /* TODO: Is it need for MACOS_X? (Dany) */ | |
4406 /* | |
4407 * On macintosh copy the original files attributes (i.e. the backup) | |
1201 | 4408 * This is done in order to preserve the resource fork and the |
4409 * Finder attribute (label, comments, custom icons, file creator) | |
7 | 4410 */ |
4411 if (backup != NULL && overwriting && !append) | |
4412 { | |
4413 if (backup_copy) | |
4414 (void)mch_copy_file_attribute(wfname, backup); | |
4415 else | |
4416 (void)mch_copy_file_attribute(backup, wfname); | |
4417 } | |
4418 | |
4419 if (!overwriting && !append) | |
4420 { | |
4421 if (buf->b_ffname != NULL) | |
4422 (void)mch_copy_file_attribute(buf->b_ffname, wfname); | |
1201 | 4423 /* Should copy resource fork */ |
7 | 4424 } |
4425 #endif | |
4426 | |
4427 write_info.bw_fd = fd; | |
4428 | |
4429 #ifdef FEAT_CRYPT | |
2267 | 4430 if (*buf->b_p_key != NUL && !filtering) |
7 | 4431 { |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4432 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
|
4433 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
|
4434 |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4435 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
|
4436 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
|
4437 end = 0; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4438 else |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
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 /* 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
|
4441 * 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
|
4442 * 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
|
4443 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
|
4444 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
|
4445 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
|
4446 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
|
4447 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
|
4448 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
|
4449 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
|
4450 } |
7 | 4451 } |
4452 #endif | |
4453 | |
4454 write_info.bw_buf = buffer; | |
4455 nchars = 0; | |
4456 | |
4457 /* use "++bin", "++nobin" or 'binary' */ | |
4458 if (eap != NULL && eap->force_bin != 0) | |
4459 write_bin = (eap->force_bin == FORCE_BIN); | |
4460 else | |
4461 write_bin = buf->b_p_bin; | |
4462 | |
4463 #ifdef FEAT_MBYTE | |
4464 /* | |
4465 * The BOM is written just after the encryption magic number. | |
24 | 4466 * Skip it when appending and the file already existed, the BOM only makes |
4467 * sense at the start of the file. | |
7 | 4468 */ |
24 | 4469 if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
7 | 4470 { |
4471 write_info.bw_len = make_bom(buffer, fenc); | |
4472 if (write_info.bw_len > 0) | |
4473 { | |
4474 /* don't convert, do encryption */ | |
4475 write_info.bw_flags = FIO_NOCONVERT | wb_flags; | |
4476 if (buf_write_bytes(&write_info) == FAIL) | |
4477 end = 0; | |
4478 else | |
4479 nchars += write_info.bw_len; | |
4480 } | |
4481 } | |
1947 | 4482 write_info.bw_start_lnum = start; |
7 | 4483 #endif |
4484 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4485 #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
|
4486 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
|
4487 && !filtering && reset_changed); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4488 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
|
4489 /* 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
|
4490 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
|
4491 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4492 |
7 | 4493 write_info.bw_len = bufsize; |
4494 #ifdef HAS_BW_FLAGS | |
4495 write_info.bw_flags = wb_flags; | |
4496 #endif | |
4497 fileformat = get_fileformat_force(buf, eap); | |
4498 s = buffer; | |
4499 len = 0; | |
4500 for (lnum = start; lnum <= end; ++lnum) | |
4501 { | |
4502 /* | |
4503 * The next while loop is done once for each character written. | |
4504 * Keep it fast! | |
4505 */ | |
4506 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
|
4507 #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
|
4508 if (write_undo_file) |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
4509 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
|
4510 #endif |
7 | 4511 while ((c = *++ptr) != NUL) |
4512 { | |
4513 if (c == NL) | |
4514 *s = NUL; /* replace newlines with NULs */ | |
4515 else if (c == CAR && fileformat == EOL_MAC) | |
4516 *s = NL; /* Mac: replace CRs with NLs */ | |
4517 else | |
4518 *s = c; | |
4519 ++s; | |
4520 if (++len != bufsize) | |
4521 continue; | |
4522 if (buf_write_bytes(&write_info) == FAIL) | |
4523 { | |
4524 end = 0; /* write error: break loop */ | |
4525 break; | |
4526 } | |
4527 nchars += bufsize; | |
4528 s = buffer; | |
4529 len = 0; | |
1947 | 4530 #ifdef FEAT_MBYTE |
4531 write_info.bw_start_lnum = lnum; | |
4532 #endif | |
7 | 4533 } |
4534 /* write failed or last line has no EOL: stop here */ | |
4535 if (end == 0 | |
4536 || (lnum == end | |
4537 && write_bin | |
4538 && (lnum == write_no_eol_lnum | |
4539 || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) | |
4540 { | |
4541 ++lnum; /* written the line, count it */ | |
4542 no_eol = TRUE; | |
4543 break; | |
4544 } | |
4545 if (fileformat == EOL_UNIX) | |
4546 *s++ = NL; | |
4547 else | |
4548 { | |
4549 *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ | |
4550 if (fileformat == EOL_DOS) /* write CR-NL */ | |
4551 { | |
4552 if (++len == bufsize) | |
4553 { | |
4554 if (buf_write_bytes(&write_info) == FAIL) | |
4555 { | |
4556 end = 0; /* write error: break loop */ | |
4557 break; | |
4558 } | |
4559 nchars += bufsize; | |
4560 s = buffer; | |
4561 len = 0; | |
4562 } | |
4563 *s++ = NL; | |
4564 } | |
4565 } | |
4566 if (++len == bufsize && end) | |
4567 { | |
4568 if (buf_write_bytes(&write_info) == FAIL) | |
4569 { | |
4570 end = 0; /* write error: break loop */ | |
4571 break; | |
4572 } | |
4573 nchars += bufsize; | |
4574 s = buffer; | |
4575 len = 0; | |
4576 | |
4577 ui_breakcheck(); | |
4578 if (got_int) | |
4579 { | |
4580 end = 0; /* Interrupted, break loop */ | |
4581 break; | |
4582 } | |
4583 } | |
4584 #ifdef VMS | |
4585 /* | |
4586 * On VMS there is a problem: newlines get added when writing blocks | |
4587 * at a time. Fix it by writing a line at a time. | |
4588 * This is much slower! | |
22 | 4589 * Explanation: VAX/DECC RTL insists that records in some RMS |
4590 * structures end with a newline (carriage return) character, and if | |
4591 * they don't it adds one. | |
4592 * With other RMS structures it works perfect without this fix. | |
7 | 4593 */ |
1431 | 4594 if (buf->b_fab_rfm == FAB$C_VFC |
4595 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) | |
22 | 4596 { |
4597 int b2write; | |
4598 | |
4599 buf->b_fab_mrs = (buf->b_fab_mrs == 0 | |
4600 ? MIN(4096, bufsize) | |
4601 : MIN(buf->b_fab_mrs, bufsize)); | |
4602 | |
4603 b2write = len; | |
4604 while (b2write > 0) | |
7 | 4605 { |
22 | 4606 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
4607 if (buf_write_bytes(&write_info) == FAIL) | |
4608 { | |
4609 end = 0; | |
4610 break; | |
4611 } | |
4612 b2write -= MIN(b2write, buf->b_fab_mrs); | |
7 | 4613 } |
4614 write_info.bw_len = bufsize; | |
4615 nchars += len; | |
4616 s = buffer; | |
4617 len = 0; | |
4618 } | |
4619 #endif | |
4620 } | |
4621 if (len > 0 && end > 0) | |
4622 { | |
4623 write_info.bw_len = len; | |
4624 if (buf_write_bytes(&write_info) == FAIL) | |
4625 end = 0; /* write error */ | |
4626 nchars += len; | |
4627 } | |
4628 | |
4629 #if defined(UNIX) && defined(HAVE_FSYNC) | |
4630 /* On many journalling file systems there is a bug that causes both the | |
4631 * original and the backup file to be lost when halting the system right | |
4632 * after writing the file. That's because only the meta-data is | |
4633 * journalled. Syncing the file slows down the system, but assures it has | |
10 | 4634 * been written to disk and we don't lose it. |
4635 * For a device do try the fsync() but don't complain if it does not work | |
36 | 4636 * (could be a pipe). |
4637 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */ | |
4638 if (p_fs && fsync(fd) != 0 && !device) | |
7 | 4639 { |
4640 errmsg = (char_u *)_("E667: Fsync failed"); | |
4641 end = 0; | |
4642 } | |
4643 #endif | |
4644 | |
1583 | 4645 #ifdef HAVE_SELINUX |
4646 /* Probably need to set the security context. */ | |
4647 if (!backup_copy) | |
4648 mch_copy_sec(backup, wfname); | |
4649 #endif | |
4650 | |
557 | 4651 #ifdef UNIX |
4652 /* When creating a new file, set its owner/group to that of the original | |
4653 * file. Get the new device and inode number. */ | |
4654 if (backup != NULL && !backup_copy) | |
4655 { | |
4656 # ifdef HAVE_FCHOWN | |
4657 struct stat st; | |
4658 | |
4659 /* don't change the owner when it's already OK, some systems remove | |
4660 * permission or ACL stuff */ | |
4661 if (mch_stat((char *)wfname, &st) < 0 | |
4662 || st.st_uid != st_old.st_uid | |
4663 || st.st_gid != st_old.st_gid) | |
4664 { | |
1757 | 4665 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 4666 if (perm >= 0) /* set permission again, may have changed */ |
4667 (void)mch_setperm(wfname, perm); | |
4668 } | |
4669 # endif | |
4670 buf_setino(buf); | |
4671 } | |
1873 | 4672 else if (!buf->b_dev_valid) |
617 | 4673 /* Set the inode when creating a new file. */ |
4674 buf_setino(buf); | |
557 | 4675 #endif |
4676 | |
7 | 4677 if (close(fd) != 0) |
4678 { | |
4679 errmsg = (char_u *)_("E512: Close failed"); | |
4680 end = 0; | |
4681 } | |
4682 | |
4683 #ifdef UNIX | |
4684 if (made_writable) | |
4685 perm &= ~0200; /* reset 'w' bit for security reasons */ | |
4686 #endif | |
4687 if (perm >= 0) /* set perm. of new file same as old file */ | |
4688 (void)mch_setperm(wfname, perm); | |
4689 #ifdef RISCOS | |
4690 if (!append && !filtering) | |
4691 /* Set the filetype after writing the file. */ | |
4692 mch_set_filetype(wfname, buf->b_p_oft); | |
4693 #endif | |
4694 #ifdef HAVE_ACL | |
4695 /* Probably need to set the ACL before changing the user (can't set the | |
4696 * ACL on a file the user doesn't own). */ | |
4697 if (!backup_copy) | |
4698 mch_set_acl(wfname, acl); | |
4699 #endif | |
2267 | 4700 #ifdef FEAT_CRYPT |
4701 if (wb_flags & FIO_ENCRYPTED) | |
4702 crypt_pop_state(); | |
4703 #endif | |
7 | 4704 |
4705 | |
4706 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) | |
4707 if (wfname != fname) | |
4708 { | |
4709 /* | |
4710 * The file was written to a temp file, now it needs to be converted | |
4711 * with 'charconvert' to (overwrite) the output file. | |
4712 */ | |
4713 if (end != 0) | |
4714 { | |
4715 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc, | |
4716 wfname, fname) == FAIL) | |
4717 { | |
4718 write_info.bw_conv_error = TRUE; | |
4719 end = 0; | |
4720 } | |
4721 } | |
4722 mch_remove(wfname); | |
4723 vim_free(wfname); | |
4724 } | |
4725 #endif | |
4726 | |
4727 if (end == 0) | |
4728 { | |
4729 if (errmsg == NULL) | |
4730 { | |
4731 #ifdef FEAT_MBYTE | |
4732 if (write_info.bw_conv_error) | |
1947 | 4733 { |
4734 if (write_info.bw_conv_error_lnum == 0) | |
4735 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4736 else | |
4737 { | |
4738 errmsg_allocated = TRUE; | |
4739 errmsg = alloc(300); | |
4740 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4741 (long)write_info.bw_conv_error_lnum); | |
4742 } | |
4743 } | |
7 | 4744 else |
4745 #endif | |
4746 if (got_int) | |
4747 errmsg = (char_u *)_(e_interr); | |
4748 else | |
4749 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4750 } | |
4751 | |
4752 /* | |
4753 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4754 * because the new file is probably corrupt. This avoids losing the |
7 | 4755 * original file when trying to make a backup when writing the file a |
4756 * second time. | |
4757 * When "backup_copy" is set we need to copy the backup over the new | |
4758 * file. Otherwise rename the backup file. | |
4759 * If this is OK, don't give the extra warning message. | |
4760 */ | |
4761 if (backup != NULL) | |
4762 { | |
4763 if (backup_copy) | |
4764 { | |
4765 /* This may take a while, if we were interrupted let the user | |
4766 * know we got the message. */ | |
4767 if (got_int) | |
4768 { | |
4769 MSG(_(e_interr)); | |
4770 out_flush(); | |
4771 } | |
4772 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4773 { | |
4774 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4775 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4776 perm & 0777)) >= 0) | |
7 | 4777 { |
4778 /* copy the file. */ | |
4779 write_info.bw_buf = smallbuf; | |
4780 #ifdef HAS_BW_FLAGS | |
4781 write_info.bw_flags = FIO_NOCONVERT; | |
4782 #endif | |
4783 while ((write_info.bw_len = vim_read(fd, smallbuf, | |
4784 SMBUFSIZE)) > 0) | |
4785 if (buf_write_bytes(&write_info) == FAIL) | |
4786 break; | |
4787 | |
4788 if (close(write_info.bw_fd) >= 0 | |
4789 && write_info.bw_len == 0) | |
4790 end = 1; /* success */ | |
4791 } | |
4792 close(fd); /* ignore errors for closing read file */ | |
4793 } | |
4794 } | |
4795 else | |
4796 { | |
4797 if (vim_rename(backup, fname) == 0) | |
4798 end = 1; | |
4799 } | |
4800 } | |
4801 goto fail; | |
4802 } | |
4803 | |
4804 lnum -= start; /* compute number of written lines */ | |
4805 --no_wait_return; /* may wait for return now */ | |
4806 | |
4807 #if !(defined(UNIX) || defined(VMS)) | |
4808 fname = sfname; /* use shortname now, for the messages */ | |
4809 #endif | |
4810 if (!filtering) | |
4811 { | |
4812 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4813 c = FALSE; | |
4814 #ifdef FEAT_MBYTE | |
4815 if (write_info.bw_conv_error) | |
4816 { | |
4817 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4818 c = TRUE; | |
1947 | 4819 if (write_info.bw_conv_error_lnum != 0) |
2280
941ff1cd317a
Add file save counter to undo information. Add undotree() function.
Bram Moolenaar <bram@vim.org>
parents:
2267
diff
changeset
|
4820 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4821 (long)write_info.bw_conv_error_lnum); |
7 | 4822 } |
4823 else if (notconverted) | |
4824 { | |
4825 STRCAT(IObuff, _("[NOT converted]")); | |
4826 c = TRUE; | |
4827 } | |
4828 else if (converted) | |
4829 { | |
4830 STRCAT(IObuff, _("[converted]")); | |
4831 c = TRUE; | |
4832 } | |
4833 #endif | |
4834 if (device) | |
4835 { | |
4836 STRCAT(IObuff, _("[Device]")); | |
4837 c = TRUE; | |
4838 } | |
4839 else if (newfile) | |
4840 { | |
4841 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
4842 c = TRUE; | |
4843 } | |
4844 if (no_eol) | |
4845 { | |
4846 msg_add_eol(); | |
4847 c = TRUE; | |
4848 } | |
4849 /* may add [unix/dos/mac] */ | |
4850 if (msg_add_fileformat(fileformat)) | |
4851 c = TRUE; | |
4852 #ifdef FEAT_CRYPT | |
4853 if (wb_flags & FIO_ENCRYPTED) | |
4854 { | |
4855 STRCAT(IObuff, _("[crypted]")); | |
4856 c = TRUE; | |
4857 } | |
4858 #endif | |
4859 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
4860 if (!shortmess(SHM_WRITE)) | |
4861 { | |
4862 if (append) | |
4863 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
4864 else | |
4865 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
4866 } | |
4867 | |
679 | 4868 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 4869 } |
4870 | |
39 | 4871 /* When written everything correctly: reset 'modified'. Unless not |
4872 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 4873 if (reset_changed && whole && !append |
7 | 4874 #ifdef FEAT_MBYTE |
4875 && !write_info.bw_conv_error | |
4876 #endif | |
39 | 4877 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
4878 ) | |
7 | 4879 { |
4880 unchanged(buf, TRUE); | |
4881 u_unchanged(buf); | |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
4882 u_update_save_nr(buf); |
7 | 4883 } |
4884 | |
4885 /* | |
4886 * If written to the current file, update the timestamp of the swap file | |
4887 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
4888 */ | |
4889 if (overwriting) | |
4890 { | |
4891 ml_timestamp(buf); | |
589 | 4892 if (append) |
4893 buf->b_flags &= ~BF_NEW; | |
4894 else | |
4895 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 4896 } |
4897 | |
4898 /* | |
4899 * If we kept a backup until now, and we are in patch mode, then we make | |
4900 * the backup file our 'original' file. | |
4901 */ | |
4902 if (*p_pm && dobackup) | |
4903 { | |
4904 char *org = (char *)buf_modname( | |
4905 #ifdef SHORT_FNAME | |
4906 TRUE, | |
4907 #else | |
4908 (buf->b_p_sn || buf->b_shortname), | |
4909 #endif | |
4910 fname, p_pm, FALSE); | |
4911 | |
4912 if (backup != NULL) | |
4913 { | |
4914 struct stat st; | |
4915 | |
4916 /* | |
4917 * If the original file does not exist yet | |
4918 * the current backup file becomes the original file | |
4919 */ | |
4920 if (org == NULL) | |
4921 EMSG(_("E205: Patchmode: can't save original file")); | |
4922 else if (mch_stat(org, &st) < 0) | |
4923 { | |
4924 vim_rename(backup, (char_u *)org); | |
4925 vim_free(backup); /* don't delete the file */ | |
4926 backup = NULL; | |
4927 #ifdef UNIX | |
4928 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
4929 #endif | |
4930 } | |
4931 } | |
4932 /* | |
4933 * If there is no backup file, remember that a (new) file was | |
4934 * created. | |
4935 */ | |
4936 else | |
4937 { | |
4938 int empty_fd; | |
4939 | |
4940 if (org == NULL | |
557 | 4941 || (empty_fd = mch_open(org, |
4942 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 4943 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4944 EMSG(_("E206: patchmode: can't touch empty original file")); |
4945 else | |
4946 close(empty_fd); | |
4947 } | |
4948 if (org != NULL) | |
4949 { | |
4950 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
4951 vim_free(org); | |
4952 } | |
4953 } | |
4954 | |
4955 /* | |
4956 * Remove the backup unless 'backup' option is set | |
4957 */ | |
4958 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
4959 EMSG(_("E207: Can't delete backup file")); | |
4960 | |
4961 #ifdef FEAT_SUN_WORKSHOP | |
4962 if (usingSunWorkShop) | |
4963 workshop_file_saved((char *) ffname); | |
4964 #endif | |
4965 | |
4966 goto nofail; | |
4967 | |
4968 /* | |
4969 * Finish up. We get here either after failure or success. | |
4970 */ | |
4971 fail: | |
4972 --no_wait_return; /* may wait for return now */ | |
4973 nofail: | |
4974 | |
4975 /* Done saving, we accept changed buffer warnings again */ | |
4976 buf->b_saving = FALSE; | |
4977 | |
4978 vim_free(backup); | |
4979 if (buffer != smallbuf) | |
4980 vim_free(buffer); | |
4981 #ifdef FEAT_MBYTE | |
4982 vim_free(fenc_tofree); | |
4983 vim_free(write_info.bw_conv_buf); | |
4984 # ifdef USE_ICONV | |
4985 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4986 { | |
4987 iconv_close(write_info.bw_iconv_fd); | |
4988 write_info.bw_iconv_fd = (iconv_t)-1; | |
4989 } | |
4990 # endif | |
4991 #endif | |
4992 #ifdef HAVE_ACL | |
4993 mch_free_acl(acl); | |
4994 #endif | |
4995 | |
4996 if (errmsg != NULL) | |
4997 { | |
835 | 4998 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 4999 |
5000 attr = hl_attr(HLF_E); /* set highlight for error messages */ | |
5001 msg_add_fname(buf, | |
5002 #ifndef UNIX | |
5003 sfname | |
5004 #else | |
5005 fname | |
5006 #endif | |
5007 ); /* put file name in IObuff with quotes */ | |
5008 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5009 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5010 /* If the error message has the form "is ...", put the error number in | |
5011 * front of the file name. */ | |
5012 if (errnum != NULL) | |
5013 { | |
1620 | 5014 STRMOVE(IObuff + numlen, IObuff); |
7 | 5015 mch_memmove(IObuff, errnum, (size_t)numlen); |
5016 } | |
5017 STRCAT(IObuff, errmsg); | |
5018 emsg(IObuff); | |
1947 | 5019 if (errmsg_allocated) |
5020 vim_free(errmsg); | |
7 | 5021 |
5022 retval = FAIL; | |
5023 if (end == 0) | |
5024 { | |
5025 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5026 attr | MSG_HIST); | |
5027 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5028 attr | MSG_HIST); | |
5029 | |
5030 /* Update the timestamp to avoid an "overwrite changed file" | |
5031 * prompt when writing again. */ | |
5032 if (mch_stat((char *)fname, &st_old) >= 0) | |
5033 { | |
5034 buf_store_time(buf, &st_old, fname); | |
5035 buf->b_mtime_read = buf->b_mtime; | |
5036 } | |
5037 } | |
5038 } | |
5039 msg_scroll = msg_save; | |
5040 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5041 #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
|
5042 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5043 * 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
|
5044 * file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5045 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5046 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
|
5047 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5048 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
|
5049 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5050 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
|
5051 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
|
5052 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5053 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5054 |
7 | 5055 #ifdef FEAT_AUTOCMD |
5056 #ifdef FEAT_EVAL | |
5057 if (!should_abort(retval)) | |
5058 #else | |
5059 if (!got_int) | |
5060 #endif | |
5061 { | |
5062 aco_save_T aco; | |
5063 | |
5064 write_no_eol_lnum = 0; /* in case it was set by the previous read */ | |
5065 | |
5066 /* | |
5067 * Apply POST autocommands. | |
5068 * Careful: The autocommands may call buf_write() recursively! | |
5069 */ | |
5070 aucmd_prepbuf(&aco, buf); | |
5071 | |
5072 if (append) | |
5073 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5074 FALSE, curbuf, eap); | |
5075 else if (filtering) | |
5076 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5077 FALSE, curbuf, eap); | |
5078 else if (reset_changed && whole) | |
5079 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5080 FALSE, curbuf, eap); | |
5081 else | |
5082 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5083 FALSE, curbuf, eap); | |
5084 | |
5085 /* restore curwin/curbuf and a few other things */ | |
5086 aucmd_restbuf(&aco); | |
5087 | |
5088 #ifdef FEAT_EVAL | |
5089 if (aborting()) /* autocmds may abort script processing */ | |
5090 retval = FALSE; | |
5091 #endif | |
5092 } | |
5093 #endif | |
5094 | |
5095 got_int |= prev_got_int; | |
5096 | |
5097 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
5098 /* Update machine specific information. */ | |
5099 mch_post_buffer_write(buf); | |
5100 #endif | |
5101 return retval; | |
5102 } | |
5103 | |
5104 /* | |
633 | 5105 * Set the name of the current buffer. Use when the buffer doesn't have a |
5106 * name and a ":r" or ":w" command with a file name is used. | |
5107 */ | |
5108 static int | |
5109 set_rw_fname(fname, sfname) | |
5110 char_u *fname; | |
5111 char_u *sfname; | |
5112 { | |
5113 #ifdef FEAT_AUTOCMD | |
1905 | 5114 buf_T *buf = curbuf; |
5115 | |
633 | 5116 /* It's like the unnamed buffer is deleted.... */ |
5117 if (curbuf->b_p_bl) | |
5118 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5119 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
5120 # ifdef FEAT_EVAL | |
5121 if (aborting()) /* autocmds may abort script processing */ | |
5122 return FAIL; | |
5123 # endif | |
1905 | 5124 if (curbuf != buf) |
5125 { | |
5126 /* We are in another buffer now, don't do the renaming. */ | |
5127 EMSG(_(e_auchangedbuf)); | |
5128 return FAIL; | |
5129 } | |
633 | 5130 #endif |
5131 | |
5132 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5133 curbuf->b_flags |= BF_NOTEDITED; | |
5134 | |
5135 #ifdef FEAT_AUTOCMD | |
5136 /* ....and a new named one is created */ | |
5137 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5138 if (curbuf->b_p_bl) | |
5139 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5140 # ifdef FEAT_EVAL | |
5141 if (aborting()) /* autocmds may abort script processing */ | |
5142 return FAIL; | |
5143 # endif | |
5144 | |
5145 /* Do filetype detection now if 'filetype' is empty. */ | |
5146 if (*curbuf->b_p_ft == NUL) | |
5147 { | |
819 | 5148 if (au_has_group((char_u *)"filetypedetect")) |
676 | 5149 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE); |
717 | 5150 do_modelines(0); |
633 | 5151 } |
5152 #endif | |
5153 | |
5154 return OK; | |
5155 } | |
5156 | |
5157 /* | |
7 | 5158 * Put file name into IObuff with quotes. |
5159 */ | |
33 | 5160 void |
7 | 5161 msg_add_fname(buf, fname) |
5162 buf_T *buf; | |
5163 char_u *fname; | |
5164 { | |
5165 if (fname == NULL) | |
5166 fname = (char_u *)"-stdin-"; | |
5167 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5168 IObuff[0] = '"'; | |
5169 STRCAT(IObuff, "\" "); | |
5170 } | |
5171 | |
5172 /* | |
5173 * Append message for text mode to IObuff. | |
5174 * Return TRUE if something appended. | |
5175 */ | |
5176 static int | |
5177 msg_add_fileformat(eol_type) | |
5178 int eol_type; | |
5179 { | |
5180 #ifndef USE_CRNL | |
5181 if (eol_type == EOL_DOS) | |
5182 { | |
5183 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5184 return TRUE; | |
5185 } | |
5186 #endif | |
5187 #ifndef USE_CR | |
5188 if (eol_type == EOL_MAC) | |
5189 { | |
5190 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5191 return TRUE; | |
5192 } | |
5193 #endif | |
5194 #if defined(USE_CRNL) || defined(USE_CR) | |
5195 if (eol_type == EOL_UNIX) | |
5196 { | |
5197 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5198 return TRUE; | |
5199 } | |
5200 #endif | |
5201 return FALSE; | |
5202 } | |
5203 | |
5204 /* | |
5205 * Append line and character count to IObuff. | |
5206 */ | |
33 | 5207 void |
7 | 5208 msg_add_lines(insert_space, lnum, nchars) |
5209 int insert_space; | |
5210 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
|
5211 off_t nchars; |
7 | 5212 { |
5213 char_u *p; | |
5214 | |
5215 p = IObuff + STRLEN(IObuff); | |
5216 | |
5217 if (insert_space) | |
5218 *p++ = ' '; | |
5219 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
|
5220 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
|
5221 #ifdef LONG_LONG_OFF_T |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5222 "%ldL, %lldC", lnum, nchars |
2241
60da25e3aab7
Correct use of long instead of off_t for file size. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2239
diff
changeset
|
5223 #else |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5224 /* Explicit typecast avoids warning on Mac OS X 10.6 */ |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5225 "%ldL, %ldC", lnum, (long)nchars |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5226 #endif |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5227 ); |
7 | 5228 else |
5229 { | |
5230 if (lnum == 1) | |
5231 STRCPY(p, _("1 line, ")); | |
5232 else | |
5233 sprintf((char *)p, _("%ld lines, "), lnum); | |
5234 p += STRLEN(p); | |
5235 if (nchars == 1) | |
5236 STRCPY(p, _("1 character")); | |
5237 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
|
5238 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
|
5239 #ifdef LONG_LONG_OFF_T |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5240 _("%lld characters"), nchars |
2241
60da25e3aab7
Correct use of long instead of off_t for file size. (James Vega)
Bram Moolenaar <bram@vim.org>
parents:
2239
diff
changeset
|
5241 #else |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5242 /* Explicit typecast avoids warning on Mac OS X 10.6 */ |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5243 _("%ld characters"), (long)nchars |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5244 #endif |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5245 ); |
7 | 5246 } |
5247 } | |
5248 | |
5249 /* | |
5250 * Append message for missing line separator to IObuff. | |
5251 */ | |
5252 static void | |
5253 msg_add_eol() | |
5254 { | |
5255 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5256 } | |
5257 | |
5258 /* | |
5259 * Check modification time of file, before writing to it. | |
5260 * The size isn't checked, because using a tool like "gzip" takes care of | |
5261 * using the same timestamp but can't set the size. | |
5262 */ | |
5263 static int | |
5264 check_mtime(buf, st) | |
5265 buf_T *buf; | |
5266 struct stat *st; | |
5267 { | |
5268 if (buf->b_mtime_read != 0 | |
5269 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5270 { | |
5271 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5272 msg_silent = 0; /* must give this prompt */ | |
5273 /* don't use emsg() here, don't want to flush the buffers */ | |
5274 MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), | |
5275 hl_attr(HLF_E)); | |
5276 if (ask_yesno((char_u *)_("Do you really want to write to it"), | |
5277 TRUE) == 'n') | |
5278 return FAIL; | |
5279 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5280 } | |
5281 return OK; | |
5282 } | |
5283 | |
5284 static int | |
5285 time_differs(t1, t2) | |
5286 long t1, t2; | |
5287 { | |
5288 #if defined(__linux__) || defined(MSDOS) || defined(MSWIN) | |
5289 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store | |
5290 * the seconds. Since the roundoff is done when flushing the inode, the | |
5291 * time may change unexpectedly by one second!!! */ | |
5292 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5293 #else | |
5294 return (t1 != t2); | |
5295 #endif | |
5296 } | |
5297 | |
5298 /* | |
5299 * Call write() to write a number of bytes to the file. | |
5300 * Also handles encryption and 'encoding' conversion. | |
5301 * | |
5302 * Return FAIL for failure, OK otherwise. | |
5303 */ | |
5304 static int | |
5305 buf_write_bytes(ip) | |
5306 struct bw_info *ip; | |
5307 { | |
5308 int wlen; | |
5309 char_u *buf = ip->bw_buf; /* data to write */ | |
5310 int len = ip->bw_len; /* length of data */ | |
5311 #ifdef HAS_BW_FLAGS | |
5312 int flags = ip->bw_flags; /* extra flags */ | |
5313 #endif | |
5314 | |
5315 #ifdef FEAT_MBYTE | |
5316 /* | |
5317 * Skip conversion when writing the crypt magic number or the BOM. | |
5318 */ | |
5319 if (!(flags & FIO_NOCONVERT)) | |
5320 { | |
5321 char_u *p; | |
5322 unsigned c; | |
5323 int n; | |
5324 | |
5325 if (flags & FIO_UTF8) | |
5326 { | |
5327 /* | |
5328 * Convert latin1 in the buffer to UTF-8 in the file. | |
5329 */ | |
5330 p = ip->bw_conv_buf; /* translate to buffer */ | |
5331 for (wlen = 0; wlen < len; ++wlen) | |
5332 p += utf_char2bytes(buf[wlen], p); | |
5333 buf = ip->bw_conv_buf; | |
5334 len = (int)(p - ip->bw_conv_buf); | |
5335 } | |
5336 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5337 { | |
5338 /* | |
5339 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5340 * Latin1 chars in the file. | |
5341 */ | |
5342 if (flags & FIO_LATIN1) | |
5343 p = buf; /* translate in-place (can only get shorter) */ | |
5344 else | |
5345 p = ip->bw_conv_buf; /* translate to buffer */ | |
5346 for (wlen = 0; wlen < len; wlen += n) | |
5347 { | |
5348 if (wlen == 0 && ip->bw_restlen != 0) | |
5349 { | |
5350 int l; | |
5351 | |
5352 /* Use remainder of previous call. Append the start of | |
5353 * buf[] to get a full sequence. Might still be too | |
5354 * short! */ | |
5355 l = CONV_RESTLEN - ip->bw_restlen; | |
5356 if (l > len) | |
5357 l = len; | |
5358 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5359 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5360 if (n > ip->bw_restlen + len) |
5361 { | |
5362 /* We have an incomplete byte sequence at the end to | |
5363 * be written. We can't convert it without the | |
5364 * remaining bytes. Keep them for the next call. */ | |
5365 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5366 return FAIL; | |
5367 ip->bw_restlen += len; | |
5368 break; | |
5369 } | |
5370 if (n > 1) | |
5371 c = utf_ptr2char(ip->bw_rest); | |
5372 else | |
5373 c = ip->bw_rest[0]; | |
5374 if (n >= ip->bw_restlen) | |
5375 { | |
5376 n -= ip->bw_restlen; | |
5377 ip->bw_restlen = 0; | |
5378 } | |
5379 else | |
5380 { | |
5381 ip->bw_restlen -= n; | |
5382 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5383 (size_t)ip->bw_restlen); | |
5384 n = 0; | |
5385 } | |
5386 } | |
5387 else | |
5388 { | |
474 | 5389 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5390 if (n > len - wlen) |
5391 { | |
5392 /* We have an incomplete byte sequence at the end to | |
5393 * be written. We can't convert it without the | |
5394 * remaining bytes. Keep them for the next call. */ | |
5395 if (len - wlen > CONV_RESTLEN) | |
5396 return FAIL; | |
5397 ip->bw_restlen = len - wlen; | |
5398 mch_memmove(ip->bw_rest, buf + wlen, | |
5399 (size_t)ip->bw_restlen); | |
5400 break; | |
5401 } | |
5402 if (n > 1) | |
5403 c = utf_ptr2char(buf + wlen); | |
5404 else | |
5405 c = buf[wlen]; | |
5406 } | |
5407 | |
1947 | 5408 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5409 { | |
5410 ip->bw_conv_error = TRUE; | |
5411 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5412 } | |
5413 if (c == NL) | |
5414 ++ip->bw_start_lnum; | |
7 | 5415 } |
5416 if (flags & FIO_LATIN1) | |
5417 len = (int)(p - buf); | |
5418 else | |
5419 { | |
5420 buf = ip->bw_conv_buf; | |
5421 len = (int)(p - ip->bw_conv_buf); | |
5422 } | |
5423 } | |
5424 | |
5425 # ifdef WIN3264 | |
5426 else if (flags & FIO_CODEPAGE) | |
5427 { | |
5428 /* | |
5429 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5430 * codepage. | |
5431 */ | |
5432 char_u *from; | |
5433 size_t fromlen; | |
5434 char_u *to; | |
5435 int u8c; | |
5436 BOOL bad = FALSE; | |
5437 int needed; | |
5438 | |
5439 if (ip->bw_restlen > 0) | |
5440 { | |
5441 /* Need to concatenate the remainder of the previous call and | |
5442 * the bytes of the current call. Use the end of the | |
5443 * conversion buffer for this. */ | |
5444 fromlen = len + ip->bw_restlen; | |
5445 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5446 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5447 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5448 } | |
5449 else | |
5450 { | |
5451 from = buf; | |
5452 fromlen = len; | |
5453 } | |
5454 | |
5455 to = ip->bw_conv_buf; | |
5456 if (enc_utf8) | |
5457 { | |
5458 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5459 * The buffer has been allocated to be big enough. */ | |
5460 while (fromlen > 0) | |
5461 { | |
835 | 5462 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5463 if (n > (int)fromlen) /* incomplete byte sequence */ |
5464 break; | |
5465 u8c = utf_ptr2char(from); | |
5466 *to++ = (u8c & 0xff); | |
5467 *to++ = (u8c >> 8); | |
5468 fromlen -= n; | |
5469 from += n; | |
5470 } | |
5471 | |
5472 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5473 * call. */ | |
5474 if (fromlen > CONV_RESTLEN) | |
5475 { | |
5476 /* weird overlong sequence */ | |
5477 ip->bw_conv_error = TRUE; | |
5478 return FAIL; | |
5479 } | |
5480 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5481 ip->bw_restlen = (int)fromlen; |
7 | 5482 } |
5483 else | |
5484 { | |
5485 /* Convert from enc_codepage to UCS-2, to the start of the | |
5486 * buffer. The buffer has been allocated to be big enough. */ | |
5487 ip->bw_restlen = 0; | |
5488 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5489 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5490 NULL, 0); |
5491 if (needed == 0) | |
5492 { | |
5493 /* When conversion fails there may be a trailing byte. */ | |
5494 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5495 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5496 NULL, 0); |
5497 if (needed == 0) | |
5498 { | |
5499 /* Conversion doesn't work. */ | |
5500 ip->bw_conv_error = TRUE; | |
5501 return FAIL; | |
5502 } | |
5503 /* Save the trailing byte for the next call. */ | |
5504 ip->bw_rest[0] = from[fromlen - 1]; | |
5505 ip->bw_restlen = 1; | |
5506 } | |
5507 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5508 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5509 (LPWSTR)to, needed); |
5510 if (needed == 0) | |
5511 { | |
5512 /* Safety check: Conversion doesn't work. */ | |
5513 ip->bw_conv_error = TRUE; | |
5514 return FAIL; | |
5515 } | |
5516 to += needed * 2; | |
5517 } | |
5518 | |
5519 fromlen = to - ip->bw_conv_buf; | |
5520 buf = to; | |
5521 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5522 if (FIO_GET_CP(flags) == CP_UTF8) | |
5523 { | |
5524 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5525 * conversion buffer. Fails when out of space. */ | |
5526 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5527 { | |
5528 u8c = *from++; | |
5529 u8c += (*from++ << 8); | |
5530 to += utf_char2bytes(u8c, to); | |
5531 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5532 { | |
5533 ip->bw_conv_error = TRUE; | |
5534 return FAIL; | |
5535 } | |
5536 } | |
835 | 5537 len = (int)(to - buf); |
7 | 5538 } |
5539 else | |
5540 #endif | |
5541 { | |
5542 /* Convert from UCS-2 to the codepage, using the remainder of | |
5543 * the conversion buffer. If the conversion uses the default | |
5544 * character "0", the data doesn't fit in this encoding, so | |
5545 * fail. */ | |
5546 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5547 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5548 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5549 &bad); | |
7 | 5550 if (bad) |
5551 { | |
5552 ip->bw_conv_error = TRUE; | |
5553 return FAIL; | |
5554 } | |
5555 } | |
5556 } | |
5557 # endif | |
5558 | |
765 | 5559 # ifdef MACOS_CONVERT |
7 | 5560 else if (flags & FIO_MACROMAN) |
5561 { | |
5562 /* | |
5563 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5564 */ | |
5565 char_u *from; | |
5566 size_t fromlen; | |
5567 | |
5568 if (ip->bw_restlen > 0) | |
5569 { | |
5570 /* Need to concatenate the remainder of the previous call and | |
5571 * the bytes of the current call. Use the end of the | |
5572 * conversion buffer for this. */ | |
5573 fromlen = len + ip->bw_restlen; | |
5574 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5575 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5576 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5577 } | |
5578 else | |
5579 { | |
5580 from = buf; | |
5581 fromlen = len; | |
5582 } | |
5583 | |
18 | 5584 if (enc2macroman(from, fromlen, |
5585 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5586 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5587 { |
5588 ip->bw_conv_error = TRUE; | |
5589 return FAIL; | |
5590 } | |
5591 buf = ip->bw_conv_buf; | |
5592 } | |
5593 # endif | |
5594 | |
5595 # ifdef USE_ICONV | |
5596 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5597 { | |
5598 const char *from; | |
5599 size_t fromlen; | |
5600 char *to; | |
5601 size_t tolen; | |
5602 | |
5603 /* Convert with iconv(). */ | |
5604 if (ip->bw_restlen > 0) | |
5605 { | |
1836 | 5606 char *fp; |
5607 | |
7 | 5608 /* Need to concatenate the remainder of the previous call and |
5609 * the bytes of the current call. Use the end of the | |
5610 * conversion buffer for this. */ | |
5611 fromlen = len + ip->bw_restlen; | |
1836 | 5612 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5613 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5614 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5615 from = fp; | |
7 | 5616 tolen = ip->bw_conv_buflen - fromlen; |
5617 } | |
5618 else | |
5619 { | |
5620 from = (const char *)buf; | |
5621 fromlen = len; | |
5622 tolen = ip->bw_conv_buflen; | |
5623 } | |
5624 to = (char *)ip->bw_conv_buf; | |
5625 | |
5626 if (ip->bw_first) | |
5627 { | |
5628 size_t save_len = tolen; | |
5629 | |
5630 /* output the initial shift state sequence */ | |
5631 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5632 | |
5633 /* There is a bug in iconv() on Linux (which appears to be | |
5634 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5635 */ | |
5636 if (to == NULL) | |
5637 { | |
5638 to = (char *)ip->bw_conv_buf; | |
5639 tolen = save_len; | |
5640 } | |
5641 ip->bw_first = FALSE; | |
5642 } | |
5643 | |
5644 /* | |
5645 * If iconv() has an error or there is not enough room, fail. | |
5646 */ | |
5647 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5648 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5649 || fromlen > CONV_RESTLEN) | |
5650 { | |
5651 ip->bw_conv_error = TRUE; | |
5652 return FAIL; | |
5653 } | |
5654 | |
5655 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5656 if (fromlen > 0) | |
5657 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5658 ip->bw_restlen = (int)fromlen; | |
5659 | |
5660 buf = ip->bw_conv_buf; | |
5661 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5662 } | |
5663 # endif | |
5664 } | |
5665 #endif /* FEAT_MBYTE */ | |
5666 | |
5667 #ifdef FEAT_CRYPT | |
5668 if (flags & FIO_ENCRYPTED) /* encrypt the data */ | |
2244 | 5669 crypt_encode(buf, len, buf); |
7 | 5670 #endif |
5671 | |
5672 /* Repeat the write(), it may be interrupted by a signal. */ | |
1142 | 5673 while (len > 0) |
7 | 5674 { |
5675 wlen = vim_write(ip->bw_fd, buf, len); | |
5676 if (wlen <= 0) /* error! */ | |
5677 return FAIL; | |
5678 len -= wlen; | |
5679 buf += wlen; | |
5680 } | |
5681 return OK; | |
5682 } | |
5683 | |
5684 #ifdef FEAT_MBYTE | |
5685 /* | |
5686 * Convert a Unicode character to bytes. | |
1947 | 5687 * Return TRUE for an error, FALSE when it's OK. |
7 | 5688 */ |
5689 static int | |
5690 ucs2bytes(c, pp, flags) | |
5691 unsigned c; /* in: character */ | |
5692 char_u **pp; /* in/out: pointer to result */ | |
5693 int flags; /* FIO_ flags */ | |
5694 { | |
5695 char_u *p = *pp; | |
5696 int error = FALSE; | |
5697 int cc; | |
5698 | |
5699 | |
5700 if (flags & FIO_UCS4) | |
5701 { | |
5702 if (flags & FIO_ENDIAN_L) | |
5703 { | |
5704 *p++ = c; | |
5705 *p++ = (c >> 8); | |
5706 *p++ = (c >> 16); | |
5707 *p++ = (c >> 24); | |
5708 } | |
5709 else | |
5710 { | |
5711 *p++ = (c >> 24); | |
5712 *p++ = (c >> 16); | |
5713 *p++ = (c >> 8); | |
5714 *p++ = c; | |
5715 } | |
5716 } | |
5717 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5718 { | |
5719 if (c >= 0x10000) | |
5720 { | |
5721 if (flags & FIO_UTF16) | |
5722 { | |
5723 /* Make two words, ten bits of the character in each. First | |
5724 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5725 c -= 0x10000; | |
5726 if (c >= 0x100000) | |
5727 error = TRUE; | |
5728 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5729 if (flags & FIO_ENDIAN_L) | |
5730 { | |
5731 *p++ = cc; | |
5732 *p++ = ((unsigned)cc >> 8); | |
5733 } | |
5734 else | |
5735 { | |
5736 *p++ = ((unsigned)cc >> 8); | |
5737 *p++ = cc; | |
5738 } | |
5739 c = (c & 0x3ff) + 0xdc00; | |
5740 } | |
5741 else | |
5742 error = TRUE; | |
5743 } | |
5744 if (flags & FIO_ENDIAN_L) | |
5745 { | |
5746 *p++ = c; | |
5747 *p++ = (c >> 8); | |
5748 } | |
5749 else | |
5750 { | |
5751 *p++ = (c >> 8); | |
5752 *p++ = c; | |
5753 } | |
5754 } | |
5755 else /* Latin1 */ | |
5756 { | |
5757 if (c >= 0x100) | |
5758 { | |
5759 error = TRUE; | |
5760 *p++ = 0xBF; | |
5761 } | |
5762 else | |
5763 *p++ = c; | |
5764 } | |
5765 | |
5766 *pp = p; | |
5767 return error; | |
5768 } | |
5769 | |
5770 /* | |
1948 | 5771 * Return TRUE if file encoding "fenc" requires conversion from or to |
5772 * 'encoding'. | |
7 | 5773 */ |
5774 static int | |
1948 | 5775 need_conversion(fenc) |
5776 char_u *fenc; | |
7 | 5777 { |
1948 | 5778 int same_encoding; |
5779 int enc_flags; | |
5780 int fenc_flags; | |
5781 | |
5782 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5783 { |
1948 | 5784 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5785 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5786 } |
1948 | 5787 else |
5788 { | |
5789 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5790 * "ucs-4be", etc. */ | |
5791 enc_flags = get_fio_flags(p_enc); | |
5792 fenc_flags = get_fio_flags(fenc); | |
5793 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5794 } | |
5795 if (same_encoding) | |
5796 { | |
5797 /* Specified encoding matches with 'encoding'. This requires | |
5798 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5799 return enc_unicode != 0; | |
5800 } | |
5801 | |
5802 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5803 * Unicode encoding and the file is UTF-8. */ | |
5804 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5805 } |
5806 | |
5807 /* | |
5808 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5809 * internal conversion. | |
5810 * if "ptr" is an empty string, use 'encoding'. | |
5811 */ | |
5812 static int | |
5813 get_fio_flags(ptr) | |
5814 char_u *ptr; | |
5815 { | |
5816 int prop; | |
5817 | |
5818 if (*ptr == NUL) | |
5819 ptr = p_enc; | |
5820 | |
5821 prop = enc_canon_props(ptr); | |
5822 if (prop & ENC_UNICODE) | |
5823 { | |
5824 if (prop & ENC_2BYTE) | |
5825 { | |
5826 if (prop & ENC_ENDIAN_L) | |
5827 return FIO_UCS2 | FIO_ENDIAN_L; | |
5828 return FIO_UCS2; | |
5829 } | |
5830 if (prop & ENC_4BYTE) | |
5831 { | |
5832 if (prop & ENC_ENDIAN_L) | |
5833 return FIO_UCS4 | FIO_ENDIAN_L; | |
5834 return FIO_UCS4; | |
5835 } | |
5836 if (prop & ENC_2WORD) | |
5837 { | |
5838 if (prop & ENC_ENDIAN_L) | |
5839 return FIO_UTF16 | FIO_ENDIAN_L; | |
5840 return FIO_UTF16; | |
5841 } | |
5842 return FIO_UTF8; | |
5843 } | |
5844 if (prop & ENC_LATIN1) | |
5845 return FIO_LATIN1; | |
5846 /* must be ENC_DBCS, requires iconv() */ | |
5847 return 0; | |
5848 } | |
5849 | |
5850 #ifdef WIN3264 | |
5851 /* | |
5852 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5853 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5854 * Used for conversion between 'encoding' and 'fileencoding'. | |
5855 */ | |
5856 static int | |
5857 get_win_fio_flags(ptr) | |
5858 char_u *ptr; | |
5859 { | |
5860 int cp; | |
5861 | |
5862 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
5863 if (!enc_utf8 && enc_codepage <= 0) | |
5864 return 0; | |
5865 | |
5866 cp = encname2codepage(ptr); | |
5867 if (cp == 0) | |
5868 { | |
5869 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5870 if (STRCMP(ptr, "utf-8") == 0) | |
5871 cp = CP_UTF8; | |
5872 else | |
5873 # endif | |
5874 return 0; | |
5875 } | |
5876 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
5877 } | |
5878 #endif | |
5879 | |
5880 #ifdef MACOS_X | |
5881 /* | |
5882 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
5883 * needed for the internal conversion to/from utf-8 or latin1. | |
5884 */ | |
5885 static int | |
5886 get_mac_fio_flags(ptr) | |
5887 char_u *ptr; | |
5888 { | |
5889 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
5890 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
5891 return FIO_MACROMAN; | |
5892 return 0; | |
5893 } | |
5894 #endif | |
5895 | |
5896 /* | |
5897 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
5898 * "size" must be at least 2. | |
5899 * Return the name of the encoding and set "*lenp" to the length. | |
5900 * Returns NULL when no BOM found. | |
5901 */ | |
5902 static char_u * | |
5903 check_for_bom(p, size, lenp, flags) | |
5904 char_u *p; | |
5905 long size; | |
5906 int *lenp; | |
5907 int flags; | |
5908 { | |
5909 char *name = NULL; | |
5910 int len = 2; | |
5911 | |
5912 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 5913 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 5914 { |
5915 name = "utf-8"; /* EF BB BF */ | |
5916 len = 3; | |
5917 } | |
5918 else if (p[0] == 0xff && p[1] == 0xfe) | |
5919 { | |
5920 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
5921 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
5922 { | |
5923 name = "ucs-4le"; /* FF FE 00 00 */ | |
5924 len = 4; | |
5925 } | |
1735 | 5926 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 5927 name = "ucs-2le"; /* FF FE */ |
1735 | 5928 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
5929 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 5930 name = "utf-16le"; /* FF FE */ |
5931 } | |
5932 else if (p[0] == 0xfe && p[1] == 0xff | |
5933 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
5934 { | |
1547 | 5935 /* Default to utf-16, it works also for ucs-2 text. */ |
5936 if (flags == FIO_UCS2) | |
5937 name = "ucs-2"; /* FE FF */ | |
5938 else | |
7 | 5939 name = "utf-16"; /* FE FF */ |
5940 } | |
5941 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
5942 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
5943 { | |
5944 name = "ucs-4"; /* 00 00 FE FF */ | |
5945 len = 4; | |
5946 } | |
5947 | |
5948 *lenp = len; | |
5949 return (char_u *)name; | |
5950 } | |
5951 | |
5952 /* | |
5953 * Generate a BOM in "buf[4]" for encoding "name". | |
5954 * Return the length of the BOM (zero when no BOM). | |
5955 */ | |
5956 static int | |
5957 make_bom(buf, name) | |
5958 char_u *buf; | |
5959 char_u *name; | |
5960 { | |
5961 int flags; | |
5962 char_u *p; | |
5963 | |
5964 flags = get_fio_flags(name); | |
5965 | |
5966 /* Can't put a BOM in a non-Unicode file. */ | |
5967 if (flags == FIO_LATIN1 || flags == 0) | |
5968 return 0; | |
5969 | |
5970 if (flags == FIO_UTF8) /* UTF-8 */ | |
5971 { | |
5972 buf[0] = 0xef; | |
5973 buf[1] = 0xbb; | |
5974 buf[2] = 0xbf; | |
5975 return 3; | |
5976 } | |
5977 p = buf; | |
5978 (void)ucs2bytes(0xfeff, &p, flags); | |
5979 return (int)(p - buf); | |
5980 } | |
5981 #endif | |
5982 | |
1418 | 5983 #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
1471 | 5984 defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
7 | 5985 /* |
5986 * Try to find a shortname by comparing the fullname with the current | |
5987 * directory. | |
1411 | 5988 * Returns "full_path" or pointer into "full_path" if shortened. |
5989 */ | |
5990 char_u * | |
5991 shorten_fname1(full_path) | |
5992 char_u *full_path; | |
5993 { | |
5994 char_u dirname[MAXPATHL]; | |
5995 char_u *p = full_path; | |
5996 | |
5997 if (mch_dirname(dirname, MAXPATHL) == OK) | |
5998 { | |
5999 p = shorten_fname(full_path, dirname); | |
6000 if (p == NULL || *p == NUL) | |
6001 p = full_path; | |
6002 } | |
6003 return p; | |
6004 } | |
1418 | 6005 #endif |
1411 | 6006 |
6007 /* | |
6008 * Try to find a shortname by comparing the fullname with the current | |
6009 * directory. | |
7 | 6010 * Returns NULL if not shorter name possible, pointer into "full_path" |
6011 * otherwise. | |
6012 */ | |
6013 char_u * | |
6014 shorten_fname(full_path, dir_name) | |
6015 char_u *full_path; | |
6016 char_u *dir_name; | |
6017 { | |
6018 int len; | |
6019 char_u *p; | |
6020 | |
6021 if (full_path == NULL) | |
6022 return NULL; | |
6023 len = (int)STRLEN(dir_name); | |
6024 if (fnamencmp(dir_name, full_path, len) == 0) | |
6025 { | |
6026 p = full_path + len; | |
6027 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
6028 /* | |
6029 * MSDOS: when a file is in the root directory, dir_name will end in a | |
6030 * slash, since C: by itself does not define a specific dir. In this | |
6031 * case p may already be correct. <negri> | |
6032 */ | |
6033 if (!((len > 2) && (*(p - 2) == ':'))) | |
6034 #endif | |
6035 { | |
6036 if (vim_ispathsep(*p)) | |
6037 ++p; | |
6038 #ifndef VMS /* the path separator is always part of the path */ | |
6039 else | |
6040 p = NULL; | |
6041 #endif | |
6042 } | |
6043 } | |
6044 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
6045 /* | |
6046 * When using a file in the current drive, remove the drive name: | |
6047 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6048 * a floppy from "A:\dir" to "B:\dir". | |
6049 */ | |
6050 else if (len > 3 | |
6051 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6052 && full_path[1] == ':' | |
6053 && vim_ispathsep(full_path[2])) | |
6054 p = full_path + 2; | |
6055 #endif | |
6056 else | |
6057 p = NULL; | |
6058 return p; | |
6059 } | |
6060 | |
6061 /* | |
6062 * Shorten filenames for all buffers. | |
6063 * When "force" is TRUE: Use full path from now on for files currently being | |
6064 * edited, both for file name and swap file name. Try to shorten the file | |
6065 * names a bit, if safe to do so. | |
6066 * When "force" is FALSE: Only try to shorten absolute file names. | |
6067 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6068 * name. | |
6069 */ | |
6070 void | |
6071 shorten_fnames(force) | |
6072 int force; | |
6073 { | |
6074 char_u dirname[MAXPATHL]; | |
6075 buf_T *buf; | |
6076 char_u *p; | |
6077 | |
6078 mch_dirname(dirname, MAXPATHL); | |
6079 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
6080 { | |
6081 if (buf->b_fname != NULL | |
6082 #ifdef FEAT_QUICKFIX | |
6083 && !bt_nofile(buf) | |
6084 #endif | |
6085 && !path_with_url(buf->b_fname) | |
6086 && (force | |
6087 || buf->b_sfname == NULL | |
6088 || mch_isFullName(buf->b_sfname))) | |
6089 { | |
6090 vim_free(buf->b_sfname); | |
6091 buf->b_sfname = NULL; | |
6092 p = shorten_fname(buf->b_ffname, dirname); | |
6093 if (p != NULL) | |
6094 { | |
6095 buf->b_sfname = vim_strsave(p); | |
6096 buf->b_fname = buf->b_sfname; | |
6097 } | |
6098 if (p == NULL || buf->b_fname == NULL) | |
6099 buf->b_fname = buf->b_ffname; | |
9 | 6100 } |
6101 | |
6102 /* Always make the swap file name a full path, a "nofile" buffer may | |
6103 * also have a swap file. */ | |
6104 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6105 } |
6106 #ifdef FEAT_WINDOWS | |
6107 status_redraw_all(); | |
672 | 6108 redraw_tabline = TRUE; |
7 | 6109 #endif |
6110 } | |
6111 | |
6112 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6113 || defined(FEAT_GUI_MSWIN) \ | |
6114 || defined(FEAT_GUI_MAC) \ | |
6115 || defined(PROTO) | |
6116 /* | |
6117 * Shorten all filenames in "fnames[count]" by current directory. | |
6118 */ | |
6119 void | |
6120 shorten_filenames(fnames, count) | |
6121 char_u **fnames; | |
6122 int count; | |
6123 { | |
6124 int i; | |
6125 char_u dirname[MAXPATHL]; | |
6126 char_u *p; | |
6127 | |
6128 if (fnames == NULL || count < 1) | |
6129 return; | |
6130 mch_dirname(dirname, sizeof(dirname)); | |
6131 for (i = 0; i < count; ++i) | |
6132 { | |
6133 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6134 { | |
6135 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6136 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6137 * "p" first then free fnames[i]. */ | |
6138 p = vim_strsave(p); | |
6139 vim_free(fnames[i]); | |
6140 fnames[i] = p; | |
6141 } | |
6142 } | |
6143 } | |
6144 #endif | |
6145 | |
6146 /* | |
1651 | 6147 * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6148 * fo_o_h.ext for MSDOS or when shortname option set. |
6149 * | |
6150 * Assumed that fname is a valid name found in the filesystem we assure that | |
6151 * the return value is a different name and ends in 'ext'. | |
6152 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6153 * characters otherwise. | |
6154 * Space for the returned name is allocated, must be freed later. | |
6155 * Returns NULL when out of memory. | |
6156 */ | |
6157 char_u * | |
6158 modname(fname, ext, prepend_dot) | |
6159 char_u *fname, *ext; | |
6160 int prepend_dot; /* may prepend a '.' to file name */ | |
6161 { | |
6162 return buf_modname( | |
6163 #ifdef SHORT_FNAME | |
6164 TRUE, | |
6165 #else | |
6166 (curbuf->b_p_sn || curbuf->b_shortname), | |
6167 #endif | |
6168 fname, ext, prepend_dot); | |
6169 } | |
6170 | |
6171 char_u * | |
6172 buf_modname(shortname, fname, ext, prepend_dot) | |
6173 int shortname; /* use 8.3 file name */ | |
6174 char_u *fname, *ext; | |
6175 int prepend_dot; /* may prepend a '.' to file name */ | |
6176 { | |
6177 char_u *retval; | |
6178 char_u *s; | |
6179 char_u *e; | |
6180 char_u *ptr; | |
6181 int fnamelen, extlen; | |
6182 | |
6183 extlen = (int)STRLEN(ext); | |
6184 | |
6185 /* | |
6186 * If there is no file name we must get the name of the current directory | |
6187 * (we need the full path in case :cd is used). | |
6188 */ | |
6189 if (fname == NULL || *fname == NUL) | |
6190 { | |
6191 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6192 if (retval == NULL) | |
6193 return NULL; | |
6194 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6195 (fnamelen = (int)STRLEN(retval)) == 0) | |
6196 { | |
6197 vim_free(retval); | |
6198 return NULL; | |
6199 } | |
39 | 6200 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6201 { |
6202 retval[fnamelen++] = PATHSEP; | |
6203 retval[fnamelen] = NUL; | |
6204 } | |
6205 #ifndef SHORT_FNAME | |
6206 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6207 #endif | |
6208 } | |
6209 else | |
6210 { | |
6211 fnamelen = (int)STRLEN(fname); | |
6212 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6213 if (retval == NULL) | |
6214 return NULL; | |
6215 STRCPY(retval, fname); | |
6216 #ifdef VMS | |
6217 vms_remove_version(retval); /* we do not need versions here */ | |
6218 #endif | |
6219 } | |
6220 | |
6221 /* | |
6222 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6223 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6224 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6225 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6226 */ | |
391 | 6227 for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) |
7 | 6228 { |
6229 #ifndef RISCOS | |
6230 if (*ext == '.' | |
391 | 6231 # ifdef USE_LONG_FNAME |
7 | 6232 && (!USE_LONG_FNAME || shortname) |
391 | 6233 # else |
6234 # ifndef SHORT_FNAME | |
7 | 6235 && shortname |
391 | 6236 # endif |
7 | 6237 # endif |
6238 ) | |
6239 if (*ptr == '.') /* replace '.' by '_' */ | |
6240 *ptr = '_'; | |
391 | 6241 #endif |
7 | 6242 if (vim_ispathsep(*ptr)) |
391 | 6243 { |
6244 ++ptr; | |
7 | 6245 break; |
391 | 6246 } |
6247 } | |
7 | 6248 |
6249 /* the file name has at most BASENAMELEN characters. */ | |
6250 #ifndef SHORT_FNAME | |
6251 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6252 ptr[BASENAMELEN] = '\0'; | |
6253 #endif | |
6254 | |
6255 s = ptr + STRLEN(ptr); | |
6256 | |
6257 /* | |
6258 * For 8.3 file names we may have to reduce the length. | |
6259 */ | |
6260 #ifdef USE_LONG_FNAME | |
6261 if (!USE_LONG_FNAME || shortname) | |
6262 #else | |
6263 # ifndef SHORT_FNAME | |
6264 if (shortname) | |
6265 # endif | |
6266 #endif | |
6267 { | |
6268 /* | |
6269 * If there is no file name, or the file name ends in '/', and the | |
6270 * extension starts with '.', put a '_' before the dot, because just | |
6271 * ".ext" is invalid. | |
6272 */ | |
6273 if (fname == NULL || *fname == NUL | |
6274 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6275 { | |
6276 #ifdef RISCOS | |
6277 if (*ext == '/') | |
6278 #else | |
6279 if (*ext == '.') | |
6280 #endif | |
6281 *s++ = '_'; | |
6282 } | |
6283 /* | |
6284 * If the extension starts with '.', truncate the base name at 8 | |
6285 * characters | |
6286 */ | |
6287 #ifdef RISCOS | |
6288 /* We normally use '/', but swap files are '_' */ | |
6289 else if (*ext == '/' || *ext == '_') | |
6290 #else | |
6291 else if (*ext == '.') | |
6292 #endif | |
6293 { | |
1877 | 6294 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6295 { |
6296 s = ptr + 8; | |
6297 *s = '\0'; | |
6298 } | |
6299 } | |
6300 /* | |
6301 * If the extension doesn't start with '.', and the file name | |
6302 * doesn't have an extension yet, append a '.' | |
6303 */ | |
6304 #ifdef RISCOS | |
6305 else if ((e = vim_strchr(ptr, '/')) == NULL) | |
6306 *s++ = '/'; | |
6307 #else | |
6308 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6309 *s++ = '.'; | |
6310 #endif | |
6311 /* | |
6312 * If the extension doesn't start with '.', and there already is an | |
1201 | 6313 * extension, it may need to be truncated |
7 | 6314 */ |
6315 else if ((int)STRLEN(e) + extlen > 4) | |
6316 s = e + 4 - extlen; | |
6317 } | |
6318 #if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264) | |
6319 /* | |
6320 * If there is no file name, and the extension starts with '.', put a | |
6321 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6322 * FAT partition, and on HPFS it doesn't matter. | |
6323 */ | |
6324 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6325 *s++ = '_'; | |
6326 #endif | |
6327 | |
6328 /* | |
1651 | 6329 * Append the extension. |
7 | 6330 * ext can start with '.' and cannot exceed 3 more characters. |
6331 */ | |
6332 STRCPY(s, ext); | |
6333 | |
6334 #ifndef SHORT_FNAME | |
6335 /* | |
6336 * Prepend the dot. | |
6337 */ | |
6338 if (prepend_dot && !shortname && *(e = gettail(retval)) != | |
6339 #ifdef RISCOS | |
6340 '/' | |
6341 #else | |
6342 '.' | |
6343 #endif | |
6344 #ifdef USE_LONG_FNAME | |
6345 && USE_LONG_FNAME | |
6346 #endif | |
6347 ) | |
6348 { | |
1620 | 6349 STRMOVE(e + 1, e); |
7 | 6350 #ifdef RISCOS |
6351 *e = '/'; | |
6352 #else | |
6353 *e = '.'; | |
6354 #endif | |
6355 } | |
6356 #endif | |
6357 | |
6358 /* | |
6359 * Check that, after appending the extension, the file name is really | |
6360 * different. | |
6361 */ | |
6362 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6363 { | |
6364 /* we search for a character that can be replaced by '_' */ | |
6365 while (--s >= ptr) | |
6366 { | |
6367 if (*s != '_') | |
6368 { | |
6369 *s = '_'; | |
6370 break; | |
6371 } | |
6372 } | |
6373 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6374 *ptr = 'v'; | |
6375 } | |
6376 return retval; | |
6377 } | |
6378 | |
6379 /* | |
6380 * Like fgets(), but if the file line is too long, it is truncated and the | |
6381 * rest of the line is thrown away. Returns TRUE for end-of-file. | |
6382 */ | |
6383 int | |
6384 vim_fgets(buf, size, fp) | |
6385 char_u *buf; | |
6386 int size; | |
6387 FILE *fp; | |
6388 { | |
6389 char *eof; | |
6390 #define FGETS_SIZE 200 | |
6391 char tbuf[FGETS_SIZE]; | |
6392 | |
6393 buf[size - 2] = NUL; | |
6394 #ifdef USE_CR | |
6395 eof = fgets_cr((char *)buf, size, fp); | |
6396 #else | |
6397 eof = fgets((char *)buf, size, fp); | |
6398 #endif | |
6399 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6400 { | |
6401 buf[size - 1] = NUL; /* Truncate the line */ | |
6402 | |
6403 /* Now throw away the rest of the line: */ | |
6404 do | |
6405 { | |
6406 tbuf[FGETS_SIZE - 2] = NUL; | |
6407 #ifdef USE_CR | |
1757 | 6408 ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6409 #else |
1757 | 6410 ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6411 #endif |
6412 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6413 } | |
6414 return (eof == NULL); | |
6415 } | |
6416 | |
6417 #if defined(USE_CR) || defined(PROTO) | |
6418 /* | |
6419 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6420 * Returns TRUE for end-of-file. | |
6421 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6422 */ | |
6423 int | |
6424 tag_fgets(buf, size, fp) | |
6425 char_u *buf; | |
6426 int size; | |
6427 FILE *fp; | |
6428 { | |
6429 int i = 0; | |
6430 int c; | |
6431 int eof = FALSE; | |
6432 | |
6433 for (;;) | |
6434 { | |
6435 c = fgetc(fp); | |
6436 if (c == EOF) | |
6437 { | |
6438 eof = TRUE; | |
6439 break; | |
6440 } | |
6441 if (c == '\r') | |
6442 { | |
6443 /* Always store a NL for end-of-line. */ | |
6444 if (i < size - 1) | |
6445 buf[i++] = '\n'; | |
6446 c = fgetc(fp); | |
6447 if (c != '\n') /* Macintosh format: single CR. */ | |
6448 ungetc(c, fp); | |
6449 break; | |
6450 } | |
6451 if (i < size - 1) | |
6452 buf[i++] = c; | |
6453 if (c == '\n') | |
6454 break; | |
6455 } | |
6456 buf[i] = NUL; | |
6457 return eof; | |
6458 } | |
6459 #endif | |
6460 | |
6461 /* | |
6462 * rename() only works if both files are on the same file system, this | |
6463 * function will (attempts to?) copy the file across if rename fails -- webb | |
6464 * Return -1 for failure, 0 for success. | |
6465 */ | |
6466 int | |
6467 vim_rename(from, to) | |
6468 char_u *from; | |
6469 char_u *to; | |
6470 { | |
6471 int fd_in; | |
6472 int fd_out; | |
6473 int n; | |
6474 char *errmsg = NULL; | |
6475 char *buffer; | |
6476 #ifdef AMIGA | |
6477 BPTR flock; | |
6478 #endif | |
6479 struct stat st; | |
194 | 6480 long perm; |
199 | 6481 #ifdef HAVE_ACL |
6482 vim_acl_T acl; /* ACL from original file */ | |
6483 #endif | |
1779 | 6484 #if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME) |
6485 int use_tmp_file = FALSE; | |
6486 #endif | |
7 | 6487 |
6488 /* | |
1779 | 6489 * When the names are identical, there is nothing to do. When they refer |
6490 * to the same file (ignoring case and slash/backslash differences) but | |
6491 * the file name differs we need to go through a temp file. | |
7 | 6492 */ |
6493 if (fnamecmp(from, to) == 0) | |
1779 | 6494 { |
6495 #ifdef CASE_INSENSITIVE_FILENAME | |
6496 if (STRCMP(gettail(from), gettail(to)) != 0) | |
6497 use_tmp_file = TRUE; | |
6498 else | |
6499 #endif | |
6500 return 0; | |
6501 } | |
7 | 6502 |
6503 /* | |
6504 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6505 */ | |
6506 if (mch_stat((char *)from, &st) < 0) | |
6507 return -1; | |
6508 | |
1778 | 6509 #ifdef UNIX |
6510 { | |
6511 struct stat st_to; | |
6512 | |
6513 /* It's possible for the source and destination to be the same file. | |
6514 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6515 * filesystem. In that case go through a temp file name. */ | |
6516 if (mch_stat((char *)to, &st_to) >= 0 | |
6517 && st.st_dev == st_to.st_dev | |
6518 && st.st_ino == st_to.st_ino) | |
1779 | 6519 use_tmp_file = TRUE; |
6520 } | |
6521 #endif | |
6522 | |
6523 #if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME) | |
6524 if (use_tmp_file) | |
6525 { | |
6526 char tempname[MAXPATHL + 1]; | |
6527 | |
6528 /* | |
6529 * Find a name that doesn't exist and is in the same directory. | |
6530 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6531 */ | |
6532 if (STRLEN(from) >= MAXPATHL - 5) | |
6533 return -1; | |
6534 STRCPY(tempname, from); | |
6535 for (n = 123; n < 99999; ++n) | |
6536 { | |
6537 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6538 if (mch_stat(tempname, &st) < 0) | |
6539 { | |
6540 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6541 { |
1779 | 6542 if (mch_rename(tempname, (char *)to) == 0) |
6543 return 0; | |
6544 /* Strange, the second step failed. Try moving the | |
6545 * file back and return failure. */ | |
6546 mch_rename(tempname, (char *)from); | |
1778 | 6547 return -1; |
6548 } | |
1779 | 6549 /* If it fails for one temp name it will most likely fail |
6550 * for any temp name, give up. */ | |
6551 return -1; | |
6552 } | |
6553 } | |
6554 return -1; | |
1778 | 6555 } |
6556 #endif | |
6557 | |
7 | 6558 /* |
6559 * Delete the "to" file, this is required on some systems to make the | |
6560 * mch_rename() work, on other systems it makes sure that we don't have | |
6561 * two files when the mch_rename() fails. | |
6562 */ | |
6563 | |
6564 #ifdef AMIGA | |
6565 /* | |
6566 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6567 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6568 * though the names are different. To avoid the chance of accidentally |
7 | 6569 * deleting the "from" file (horror!) we lock it during the remove. |
6570 * | |
6571 * When used for making a backup before writing the file: This should not | |
6572 * happen with ":w", because startscript() should detect this problem and | |
6573 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6574 * name. This problem does exist with ":w filename", but then the | |
6575 * original file will be somewhere else so the backup isn't really | |
6576 * important. If autoscripting is off the rename may fail. | |
6577 */ | |
6578 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6579 #endif | |
6580 mch_remove(to); | |
6581 #ifdef AMIGA | |
6582 if (flock) | |
6583 UnLock(flock); | |
6584 #endif | |
6585 | |
6586 /* | |
6587 * First try a normal rename, return if it works. | |
6588 */ | |
6589 if (mch_rename((char *)from, (char *)to) == 0) | |
6590 return 0; | |
6591 | |
6592 /* | |
6593 * Rename() failed, try copying the file. | |
6594 */ | |
194 | 6595 perm = mch_getperm(from); |
199 | 6596 #ifdef HAVE_ACL |
6597 /* For systems that support ACL: get the ACL from the original file. */ | |
6598 acl = mch_get_acl(from); | |
6599 #endif | |
7 | 6600 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6601 if (fd_in == -1) | |
1651 | 6602 { |
6603 #ifdef HAVE_ACL | |
6604 mch_free_acl(acl); | |
6605 #endif | |
7 | 6606 return -1; |
1651 | 6607 } |
194 | 6608 |
6609 /* Create the new file with same permissions as the original. */ | |
557 | 6610 fd_out = mch_open((char *)to, |
6611 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6612 if (fd_out == -1) |
6613 { | |
6614 close(fd_in); | |
1651 | 6615 #ifdef HAVE_ACL |
6616 mch_free_acl(acl); | |
6617 #endif | |
7 | 6618 return -1; |
6619 } | |
6620 | |
6621 buffer = (char *)alloc(BUFSIZE); | |
6622 if (buffer == NULL) | |
6623 { | |
1651 | 6624 close(fd_out); |
7 | 6625 close(fd_in); |
1651 | 6626 #ifdef HAVE_ACL |
6627 mch_free_acl(acl); | |
6628 #endif | |
7 | 6629 return -1; |
6630 } | |
6631 | |
6632 while ((n = vim_read(fd_in, buffer, BUFSIZE)) > 0) | |
6633 if (vim_write(fd_out, buffer, n) != n) | |
6634 { | |
6635 errmsg = _("E208: Error writing to \"%s\""); | |
6636 break; | |
6637 } | |
6638 | |
6639 vim_free(buffer); | |
6640 close(fd_in); | |
6641 if (close(fd_out) < 0) | |
6642 errmsg = _("E209: Error closing \"%s\""); | |
6643 if (n < 0) | |
6644 { | |
6645 errmsg = _("E210: Error reading \"%s\""); | |
6646 to = from; | |
6647 } | |
1201 | 6648 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6649 mch_setperm(to, perm); |
570 | 6650 #endif |
199 | 6651 #ifdef HAVE_ACL |
6652 mch_set_acl(to, acl); | |
1651 | 6653 mch_free_acl(acl); |
199 | 6654 #endif |
7 | 6655 if (errmsg != NULL) |
6656 { | |
6657 EMSG2(errmsg, to); | |
6658 return -1; | |
6659 } | |
6660 mch_remove(from); | |
6661 return 0; | |
6662 } | |
6663 | |
6664 static int already_warned = FALSE; | |
6665 | |
6666 /* | |
6667 * Check if any not hidden buffer has been changed. | |
6668 * Postpone the check if there are characters in the stuff buffer, a global | |
6669 * command is being executed, a mapping is being executed or an autocommand is | |
6670 * busy. | |
6671 * Returns TRUE if some message was written (screen should be redrawn and | |
6672 * cursor positioned). | |
6673 */ | |
6674 int | |
6675 check_timestamps(focus) | |
6676 int focus; /* called for GUI focus event */ | |
6677 { | |
6678 buf_T *buf; | |
6679 int didit = 0; | |
6680 int n; | |
6681 | |
6682 /* Don't check timestamps while system() or another low-level function may | |
6683 * cause us to lose and gain focus. */ | |
6684 if (no_check_timestamps > 0) | |
6685 return FALSE; | |
6686 | |
6687 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6688 * event and we would keep on checking if the file is steadily growing. | |
6689 * Do check again after typing something. */ | |
6690 if (focus && did_check_timestamps) | |
6691 { | |
6692 need_check_timestamps = TRUE; | |
6693 return FALSE; | |
6694 } | |
6695 | |
6696 if (!stuff_empty() || global_busy || !typebuf_typed() | |
6697 #ifdef FEAT_AUTOCMD | |
1834 | 6698 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
7 | 6699 #endif |
6700 ) | |
6701 need_check_timestamps = TRUE; /* check later */ | |
6702 else | |
6703 { | |
6704 ++no_wait_return; | |
6705 did_check_timestamps = TRUE; | |
6706 already_warned = FALSE; | |
6707 for (buf = firstbuf; buf != NULL; ) | |
6708 { | |
6709 /* Only check buffers in a window. */ | |
6710 if (buf->b_nwindows > 0) | |
6711 { | |
6712 n = buf_check_timestamp(buf, focus); | |
6713 if (didit < n) | |
6714 didit = n; | |
6715 if (n > 0 && !buf_valid(buf)) | |
6716 { | |
6717 /* Autocommands have removed the buffer, start at the | |
6718 * first one again. */ | |
6719 buf = firstbuf; | |
6720 continue; | |
6721 } | |
6722 } | |
6723 buf = buf->b_next; | |
6724 } | |
6725 --no_wait_return; | |
6726 need_check_timestamps = FALSE; | |
6727 if (need_wait_return && didit == 2) | |
6728 { | |
6729 /* make sure msg isn't overwritten */ | |
6730 msg_puts((char_u *)"\n"); | |
6731 out_flush(); | |
6732 } | |
6733 } | |
6734 return didit; | |
6735 } | |
6736 | |
6737 /* | |
6738 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6739 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6740 * empty. | |
6741 */ | |
6742 static int | |
6743 move_lines(frombuf, tobuf) | |
6744 buf_T *frombuf; | |
6745 buf_T *tobuf; | |
6746 { | |
6747 buf_T *tbuf = curbuf; | |
6748 int retval = OK; | |
6749 linenr_T lnum; | |
6750 char_u *p; | |
6751 | |
6752 /* Copy the lines in "frombuf" to "tobuf". */ | |
6753 curbuf = tobuf; | |
6754 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6755 { | |
6756 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6757 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6758 { | |
6759 vim_free(p); | |
6760 retval = FAIL; | |
6761 break; | |
6762 } | |
6763 vim_free(p); | |
6764 } | |
6765 | |
6766 /* Delete all the lines in "frombuf". */ | |
6767 if (retval != FAIL) | |
6768 { | |
6769 curbuf = frombuf; | |
1055 | 6770 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6771 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6772 { |
6773 /* Oops! We could try putting back the saved lines, but that | |
6774 * might fail again... */ | |
6775 retval = FAIL; | |
6776 break; | |
6777 } | |
6778 } | |
6779 | |
6780 curbuf = tbuf; | |
6781 return retval; | |
6782 } | |
6783 | |
6784 /* | |
6785 * Check if buffer "buf" has been changed. | |
6786 * Also check if the file for a new buffer unexpectedly appeared. | |
6787 * return 1 if a changed buffer was found. | |
6788 * return 2 if a message has been displayed. | |
6789 * return 0 otherwise. | |
6790 */ | |
6791 int | |
6792 buf_check_timestamp(buf, focus) | |
6793 buf_T *buf; | |
1877 | 6794 int focus UNUSED; /* called for GUI focus event */ |
7 | 6795 { |
6796 struct stat st; | |
6797 int stat_res; | |
6798 int retval = 0; | |
6799 char_u *path; | |
6800 char_u *tbuf; | |
6801 char *mesg = NULL; | |
188 | 6802 char *mesg2 = ""; |
7 | 6803 int helpmesg = FALSE; |
6804 int reload = FALSE; | |
6805 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6806 int can_reload = FALSE; | |
6807 #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
|
6808 off_t orig_size = buf->b_orig_size; |
7 | 6809 int orig_mode = buf->b_orig_mode; |
6810 #ifdef FEAT_GUI | |
6811 int save_mouse_correct = need_mouse_correct; | |
6812 #endif | |
6813 #ifdef FEAT_AUTOCMD | |
6814 static int busy = FALSE; | |
179 | 6815 int n; |
6816 char_u *s; | |
6817 #endif | |
6818 char *reason; | |
7 | 6819 |
6820 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6821 * set, we are in the middle of a save or being called recursively: ignore | |
6822 * this buffer. */ | |
6823 if (buf->b_ffname == NULL | |
6824 || buf->b_ml.ml_mfp == NULL | |
6825 #if defined(FEAT_QUICKFIX) | |
6826 || *buf->b_p_bt != NUL | |
6827 #endif | |
6828 || buf->b_saving | |
6829 #ifdef FEAT_AUTOCMD | |
6830 || busy | |
6831 #endif | |
33 | 6832 #ifdef FEAT_NETBEANS_INTG |
6833 || isNetbeansBuffer(buf) | |
6834 #endif | |
7 | 6835 ) |
6836 return 0; | |
6837 | |
6838 if ( !(buf->b_flags & BF_NOTEDITED) | |
6839 && buf->b_mtime != 0 | |
6840 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6841 || time_differs((long)st.st_mtime, buf->b_mtime) | |
6842 #ifdef HAVE_ST_MODE | |
6843 || (int)st.st_mode != buf->b_orig_mode | |
6844 #else | |
6845 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6846 #endif | |
6847 )) | |
6848 { | |
6849 retval = 1; | |
6850 | |
628 | 6851 /* set b_mtime to stop further warnings (e.g., when executing |
6852 * FileChangedShell autocmd) */ | |
7 | 6853 if (stat_res < 0) |
6854 { | |
6855 buf->b_mtime = 0; | |
6856 buf->b_orig_size = 0; | |
6857 buf->b_orig_mode = 0; | |
6858 } | |
6859 else | |
6860 buf_store_time(buf, &st, buf->b_ffname); | |
6861 | |
6862 /* Don't do anything for a directory. Might contain the file | |
6863 * explorer. */ | |
6864 if (mch_isdir(buf->b_fname)) | |
6865 ; | |
6866 | |
6867 /* | |
6868 * If 'autoread' is set, the buffer has no changes and the file still | |
6869 * exists, reload the buffer. Use the buffer-local option value if it | |
6870 * was set, the global option value otherwise. | |
6871 */ | |
6872 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6873 && !bufIsChanged(buf) && stat_res >= 0) | |
6874 reload = TRUE; | |
6875 else | |
6876 { | |
179 | 6877 if (stat_res < 0) |
6878 reason = "deleted"; | |
6879 else if (bufIsChanged(buf)) | |
6880 reason = "conflict"; | |
6881 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6882 reason = "changed"; | |
6883 else if (orig_mode != buf->b_orig_mode) | |
6884 reason = "mode"; | |
6885 else | |
6886 reason = "time"; | |
6887 | |
7 | 6888 #ifdef FEAT_AUTOCMD |
6889 /* | |
6890 * Only give the warning if there are no FileChangedShell | |
6891 * autocommands. | |
6892 * Avoid being called recursively by setting "busy". | |
6893 */ | |
6894 busy = TRUE; | |
532 | 6895 # ifdef FEAT_EVAL |
179 | 6896 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
6897 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); | |
532 | 6898 # endif |
1834 | 6899 ++allbuf_lock; |
7 | 6900 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
6901 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 6902 --allbuf_lock; |
7 | 6903 busy = FALSE; |
6904 if (n) | |
6905 { | |
6906 if (!buf_valid(buf)) | |
6907 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); | |
532 | 6908 # ifdef FEAT_EVAL |
179 | 6909 s = get_vim_var_str(VV_FCS_CHOICE); |
6910 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
6911 reload = TRUE; | |
6912 else if (STRCMP(s, "ask") == 0) | |
6913 n = FALSE; | |
6914 else | |
532 | 6915 # endif |
179 | 6916 return 2; |
7 | 6917 } |
179 | 6918 if (!n) |
7 | 6919 #endif |
6920 { | |
179 | 6921 if (*reason == 'd') |
6922 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 6923 else |
6924 { | |
6925 helpmesg = TRUE; | |
6926 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6927 can_reload = TRUE; | |
6928 #endif | |
6929 /* | |
6930 * Check if the file contents really changed to avoid | |
6931 * giving a warning when only the timestamp was set (e.g., | |
6932 * checked out of CVS). Always warn when the buffer was | |
6933 * changed. | |
6934 */ | |
179 | 6935 if (reason[2] == 'n') |
6936 { | |
7 | 6937 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 6938 mesg2 = _("See \":help W12\" for more info."); |
6939 } | |
6940 else if (reason[1] == 'h') | |
6941 { | |
7 | 6942 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 6943 mesg2 = _("See \":help W11\" for more info."); |
6944 } | |
6945 else if (*reason == 'm') | |
6946 { | |
7 | 6947 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 6948 mesg2 = _("See \":help W16\" for more info."); |
6949 } | |
1913 | 6950 else |
6951 /* Only timestamp changed, store it to avoid a warning | |
6952 * in check_mtime() later. */ | |
6953 buf->b_mtime_read = buf->b_mtime; | |
7 | 6954 } |
6955 } | |
6956 } | |
6957 | |
6958 } | |
6959 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
6960 && vim_fexists(buf->b_ffname)) | |
6961 { | |
6962 retval = 1; | |
6963 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
6964 buf->b_flags |= BF_NEW_W; | |
6965 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6966 can_reload = TRUE; | |
6967 #endif | |
6968 } | |
6969 | |
6970 if (mesg != NULL) | |
6971 { | |
6972 path = home_replace_save(buf, buf->b_fname); | |
6973 if (path != NULL) | |
6974 { | |
179 | 6975 if (!helpmesg) |
7 | 6976 mesg2 = ""; |
6977 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
6978 + STRLEN(mesg2) + 2)); | |
6979 sprintf((char *)tbuf, mesg, path); | |
1848 | 6980 #ifdef FEAT_EVAL |
6981 /* Set warningmsg here, before the unimportant and output-specific | |
6982 * mesg2 has been appended. */ | |
6983 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
6984 #endif | |
7 | 6985 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
6986 if (can_reload) | |
6987 { | |
6988 if (*mesg2 != NUL) | |
6989 { | |
6990 STRCAT(tbuf, "\n"); | |
6991 STRCAT(tbuf, mesg2); | |
6992 } | |
6993 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
6994 (char_u *)_("&OK\n&Load File"), 1, NULL) == 2) | |
6995 reload = TRUE; | |
6996 } | |
6997 else | |
6998 #endif | |
6999 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
7000 { | |
7001 if (*mesg2 != NUL) | |
7002 { | |
7003 STRCAT(tbuf, "; "); | |
7004 STRCAT(tbuf, mesg2); | |
7005 } | |
7006 EMSG(tbuf); | |
7007 retval = 2; | |
7008 } | |
7009 else | |
7010 { | |
8 | 7011 # ifdef FEAT_AUTOCMD |
7 | 7012 if (!autocmd_busy) |
8 | 7013 # endif |
7 | 7014 { |
7015 msg_start(); | |
7016 msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST); | |
7017 if (*mesg2 != NUL) | |
7018 msg_puts_attr((char_u *)mesg2, | |
7019 hl_attr(HLF_W) + MSG_HIST); | |
7020 msg_clr_eos(); | |
7021 (void)msg_end(); | |
7022 if (emsg_silent == 0) | |
7023 { | |
7024 out_flush(); | |
8 | 7025 # ifdef FEAT_GUI |
7 | 7026 if (!focus) |
8 | 7027 # endif |
7 | 7028 /* give the user some time to think about it */ |
7029 ui_delay(1000L, TRUE); | |
7030 | |
7031 /* don't redraw and erase the message */ | |
7032 redraw_cmdline = FALSE; | |
7033 } | |
7034 } | |
7035 already_warned = TRUE; | |
7036 } | |
7037 | |
7038 vim_free(path); | |
7039 vim_free(tbuf); | |
7040 } | |
7041 } | |
7042 | |
7043 if (reload) | |
311 | 7044 /* Reload the buffer. */ |
628 | 7045 buf_reload(buf, orig_mode); |
7 | 7046 |
765 | 7047 #ifdef FEAT_AUTOCMD |
1620 | 7048 /* Trigger FileChangedShell when the file was changed in any way. */ |
7049 if (buf_valid(buf) && retval != 0) | |
765 | 7050 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7051 buf->b_fname, buf->b_fname, FALSE, buf); | |
7052 #endif | |
7 | 7053 #ifdef FEAT_GUI |
7054 /* restore this in case an autocommand has set it; it would break | |
7055 * 'mousefocus' */ | |
7056 need_mouse_correct = save_mouse_correct; | |
7057 #endif | |
7058 | |
7059 return retval; | |
7060 } | |
7061 | |
311 | 7062 /* |
7063 * Reload a buffer that is already loaded. | |
7064 * Used when the file was changed outside of Vim. | |
628 | 7065 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7066 * buf->b_orig_mode may have been reset already. | |
311 | 7067 */ |
7068 void | |
628 | 7069 buf_reload(buf, orig_mode) |
311 | 7070 buf_T *buf; |
628 | 7071 int orig_mode; |
311 | 7072 { |
7073 exarg_T ea; | |
7074 pos_T old_cursor; | |
7075 linenr_T old_topline; | |
7076 int old_ro = buf->b_p_ro; | |
7077 buf_T *savebuf; | |
7078 int saved = OK; | |
7079 aco_save_T aco; | |
7080 | |
7081 /* set curwin/curbuf for "buf" and save some things */ | |
7082 aucmd_prepbuf(&aco, buf); | |
7083 | |
7084 /* We only want to read the text from the file, not reset the syntax | |
7085 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7086 * and encoding to be the same. */ | |
7087 if (prep_exarg(&ea, buf) == OK) | |
7088 { | |
7089 old_cursor = curwin->w_cursor; | |
7090 old_topline = curwin->w_topline; | |
7091 | |
7092 /* | |
7093 * To behave like when a new file is edited (matters for | |
7094 * BufReadPost autocommands) we first need to delete the current | |
7095 * buffer contents. But if reading the file fails we should keep | |
7096 * the old contents. Can't use memory only, the file might be | |
7097 * too big. Use a hidden buffer to move the buffer contents to. | |
7098 */ | |
7099 if (bufempty()) | |
7100 savebuf = NULL; | |
7101 else | |
7102 { | |
7103 /* Allocate a buffer without putting it in the buffer list. */ | |
7104 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | |
837 | 7105 if (savebuf != NULL && buf == curbuf) |
311 | 7106 { |
7107 /* Open the memline. */ | |
7108 curbuf = savebuf; | |
7109 curwin->w_buffer = savebuf; | |
625 | 7110 saved = ml_open(curbuf); |
311 | 7111 curbuf = buf; |
7112 curwin->w_buffer = buf; | |
7113 } | |
837 | 7114 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7115 || move_lines(buf, savebuf) == FAIL) |
7116 { | |
7117 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7118 buf->b_fname); | |
7119 saved = FAIL; | |
7120 } | |
7121 } | |
7122 | |
7123 if (saved == OK) | |
7124 { | |
7125 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7126 #ifdef FEAT_AUTOCMD | |
7127 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7128 #endif | |
7129 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7130 (linenr_T)0, | |
7131 (linenr_T)MAXLNUM, &ea, READ_NEW) == FAIL) | |
7132 { | |
7133 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
7134 if (!aborting()) | |
7135 #endif | |
7136 EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); | |
837 | 7137 if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf) |
311 | 7138 { |
7139 /* Put the text back from the save buffer. First | |
7140 * delete any lines that readfile() added. */ | |
7141 while (!bufempty()) | |
837 | 7142 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7143 break; |
7144 (void)move_lines(savebuf, buf); | |
7145 } | |
7146 } | |
837 | 7147 else if (buf == curbuf) |
311 | 7148 { |
7149 /* Mark the buffer as unmodified and free undo info. */ | |
7150 unchanged(buf, TRUE); | |
7151 u_blockfree(buf); | |
7152 u_clearall(buf); | |
7153 } | |
7154 } | |
7155 vim_free(ea.cmd); | |
7156 | |
837 | 7157 if (savebuf != NULL && buf_valid(savebuf)) |
311 | 7158 wipe_buffer(savebuf, FALSE); |
7159 | |
7160 #ifdef FEAT_DIFF | |
7161 /* Invalidate diff info if necessary. */ | |
837 | 7162 diff_invalidate(curbuf); |
311 | 7163 #endif |
7164 | |
7165 /* Restore the topline and cursor position and check it (lines may | |
7166 * have been removed). */ | |
7167 if (old_topline > curbuf->b_ml.ml_line_count) | |
7168 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7169 else | |
7170 curwin->w_topline = old_topline; | |
7171 curwin->w_cursor = old_cursor; | |
7172 check_cursor(); | |
7173 update_topline(); | |
7174 #ifdef FEAT_AUTOCMD | |
7175 keep_filetype = FALSE; | |
7176 #endif | |
7177 #ifdef FEAT_FOLDING | |
7178 { | |
1863 | 7179 win_T *wp; |
7180 tabpage_T *tp; | |
311 | 7181 |
7182 /* Update folds unless they are defined manually. */ | |
1863 | 7183 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7184 if (wp->w_buffer == curwin->w_buffer |
7185 && !foldmethodIsManual(wp)) | |
7186 foldUpdateAll(wp); | |
7187 } | |
7188 #endif | |
7189 /* If the mode didn't change and 'readonly' was set, keep the old | |
7190 * value; the user probably used the ":view" command. But don't | |
7191 * reset it, might have had a read error. */ | |
7192 if (orig_mode == curbuf->b_orig_mode) | |
7193 curbuf->b_p_ro |= old_ro; | |
7194 } | |
7195 | |
7196 /* restore curwin/curbuf and a few other things */ | |
7197 aucmd_restbuf(&aco); | |
7198 /* Careful: autocommands may have made "buf" invalid! */ | |
7199 } | |
7200 | |
7 | 7201 void |
7202 buf_store_time(buf, st, fname) | |
7203 buf_T *buf; | |
7204 struct stat *st; | |
1877 | 7205 char_u *fname UNUSED; |
7 | 7206 { |
7207 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
|
7208 buf->b_orig_size = st->st_size; |
7 | 7209 #ifdef HAVE_ST_MODE |
7210 buf->b_orig_mode = (int)st->st_mode; | |
7211 #else | |
7212 buf->b_orig_mode = mch_getperm(fname); | |
7213 #endif | |
7214 } | |
7215 | |
7216 /* | |
7217 * Adjust the line with missing eol, used for the next write. | |
7218 * Used for do_filter(), when the input lines for the filter are deleted. | |
7219 */ | |
7220 void | |
7221 write_lnum_adjust(offset) | |
7222 linenr_T offset; | |
7223 { | |
167 | 7224 if (write_no_eol_lnum != 0) /* only if there is a missing eol */ |
7 | 7225 write_no_eol_lnum += offset; |
7226 } | |
7227 | |
7228 #if defined(TEMPDIRNAMES) || defined(PROTO) | |
7229 static long temp_count = 0; /* Temp filename counter. */ | |
7230 | |
7231 /* | |
7232 * Delete the temp directory and all files it contains. | |
7233 */ | |
7234 void | |
7235 vim_deltempdir() | |
7236 { | |
7237 char_u **files; | |
7238 int file_count; | |
7239 int i; | |
7240 | |
7241 if (vim_tempdir != NULL) | |
7242 { | |
7243 sprintf((char *)NameBuff, "%s*", vim_tempdir); | |
7244 if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, | |
7245 EW_DIR|EW_FILE|EW_SILENT) == OK) | |
7246 { | |
7247 for (i = 0; i < file_count; ++i) | |
7248 mch_remove(files[i]); | |
7249 FreeWild(file_count, files); | |
7250 } | |
7251 gettail(NameBuff)[-1] = NUL; | |
7252 (void)mch_rmdir(NameBuff); | |
7253 | |
7254 vim_free(vim_tempdir); | |
7255 vim_tempdir = NULL; | |
7256 } | |
7257 } | |
7258 #endif | |
7259 | |
2006 | 7260 #ifdef TEMPDIRNAMES |
7 | 7261 /* |
1997 | 7262 * Directory "tempdir" was created. Expand this name to a full path and put |
7263 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7264 * "tempdir" must be no longer than MAXPATHL. | |
7265 */ | |
7266 static void | |
7267 vim_settempdir(tempdir) | |
7268 char_u *tempdir; | |
7269 { | |
7270 char_u *buf; | |
7271 | |
7272 buf = alloc((unsigned)MAXPATHL + 2); | |
7273 if (buf != NULL) | |
7274 { | |
7275 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7276 STRCPY(buf, tempdir); | |
7277 # ifdef __EMX__ | |
7278 if (vim_strchr(buf, '/') != NULL) | |
7279 STRCAT(buf, "/"); | |
7280 else | |
7281 # endif | |
7282 add_pathsep(buf); | |
7283 vim_tempdir = vim_strsave(buf); | |
7284 vim_free(buf); | |
7285 } | |
7286 } | |
2006 | 7287 #endif |
1997 | 7288 |
7289 /* | |
7 | 7290 * vim_tempname(): Return a unique name that can be used for a temp file. |
7291 * | |
7292 * The temp file is NOT created. | |
7293 * | |
7294 * The returned pointer is to allocated memory. | |
7295 * The returned pointer is NULL if no valid name was found. | |
7296 */ | |
7297 char_u * | |
7298 vim_tempname(extra_char) | |
1877 | 7299 int extra_char UNUSED; /* char to use in the name instead of '?' */ |
7 | 7300 { |
7301 #ifdef USE_TMPNAM | |
7302 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7303 #else | |
7304 char_u itmp[TEMPNAMELEN]; | |
7305 #endif | |
7306 | |
7307 #ifdef TEMPDIRNAMES | |
7308 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7309 int i; | |
7310 # ifndef EEXIST | |
7311 struct stat st; | |
7312 # endif | |
7313 | |
7314 /* | |
7315 * This will create a directory for private use by this instance of Vim. | |
7316 * This is done once, and the same directory is used for all temp files. | |
7317 * This method avoids security problems because of symlink attacks et al. | |
7318 * It's also a bit faster, because we only need to check for an existing | |
7319 * file when creating the directory and not for each temp file. | |
7320 */ | |
7321 if (vim_tempdir == NULL) | |
7322 { | |
7323 /* | |
7324 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7325 */ | |
1877 | 7326 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7327 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7328 # ifndef HAVE_MKDTEMP |
1997 | 7329 size_t itmplen; |
7330 long nr; | |
7331 long off; | |
7332 # endif | |
7333 | |
7 | 7334 /* expand $TMP, leave room for "/v1100000/999999999" */ |
7335 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); | |
7336 if (mch_isdir(itmp)) /* directory exists */ | |
7337 { | |
7338 # ifdef __EMX__ | |
7339 /* If $TMP contains a forward slash (perhaps using bash or | |
7340 * tcsh), don't add a backslash, use a forward slash! | |
7341 * Adding 2 backslashes didn't work. */ | |
7342 if (vim_strchr(itmp, '/') != NULL) | |
7343 STRCAT(itmp, "/"); | |
7344 else | |
7345 # endif | |
7346 add_pathsep(itmp); | |
1997 | 7347 |
7348 # ifdef HAVE_MKDTEMP | |
7349 /* Leave room for filename */ | |
7350 STRCAT(itmp, "vXXXXXX"); | |
7351 if (mkdtemp((char *)itmp) != NULL) | |
7352 vim_settempdir(itmp); | |
7353 # else | |
7 | 7354 /* Get an arbitrary number of up to 6 digits. When it's |
7355 * unlikely that it already exists it will be faster, | |
7356 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7357 * security problems because of the predictable number. */ | |
7358 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
|
7359 itmplen = STRLEN(itmp); |
7 | 7360 |
7361 /* Try up to 10000 different values until we find a name that | |
7362 * doesn't exist. */ | |
7363 for (off = 0; off < 10000L; ++off) | |
7364 { | |
7365 int r; | |
1997 | 7366 # if defined(UNIX) || defined(VMS) |
7 | 7367 mode_t umask_save; |
1997 | 7368 # endif |
7369 | |
7370 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7371 # ifndef EEXIST | |
7 | 7372 /* If mkdir() does not set errno to EEXIST, check for |
7373 * existing file here. There is a race condition then, | |
7374 * although it's fail-safe. */ | |
7375 if (mch_stat((char *)itmp, &st) >= 0) | |
7376 continue; | |
1997 | 7377 # endif |
7378 # if defined(UNIX) || defined(VMS) | |
7 | 7379 /* Make sure the umask doesn't remove the executable bit. |
7380 * "repl" has been reported to use "177". */ | |
7381 umask_save = umask(077); | |
1997 | 7382 # endif |
7 | 7383 r = vim_mkdir(itmp, 0700); |
1997 | 7384 # if defined(UNIX) || defined(VMS) |
7 | 7385 (void)umask(umask_save); |
1997 | 7386 # endif |
7 | 7387 if (r == 0) |
7388 { | |
1997 | 7389 vim_settempdir(itmp); |
7 | 7390 break; |
7391 } | |
1997 | 7392 # ifdef EEXIST |
7 | 7393 /* If the mkdir() didn't fail because the file/dir exists, |
7394 * we probably can't create any dir here, try another | |
7395 * place. */ | |
7396 if (errno != EEXIST) | |
1997 | 7397 # endif |
7 | 7398 break; |
7399 } | |
1997 | 7400 # endif /* HAVE_MKDTEMP */ |
7 | 7401 if (vim_tempdir != NULL) |
7402 break; | |
7403 } | |
7404 } | |
7405 } | |
7406 | |
7407 if (vim_tempdir != NULL) | |
7408 { | |
7409 /* There is no need to check if the file exists, because we own the | |
7410 * directory and nobody else creates a file in it. */ | |
7411 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7412 return vim_strsave(itmp); | |
7413 } | |
7414 | |
7415 return NULL; | |
7416 | |
7417 #else /* TEMPDIRNAMES */ | |
7418 | |
7419 # ifdef WIN3264 | |
7420 char szTempFile[_MAX_PATH + 1]; | |
7421 char buf4[4]; | |
7422 char_u *retval; | |
7423 char_u *p; | |
7424 | |
7425 STRCPY(itmp, ""); | |
7426 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
7427 szTempFile[0] = NUL; /* GetTempPath() failed, use current dir */ | |
7428 strcpy(buf4, "VIM"); | |
7429 buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ | |
7430 if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0) | |
7431 return NULL; | |
7432 /* GetTempFileName() will create the file, we don't want that */ | |
7433 (void)DeleteFile(itmp); | |
7434 | |
7435 /* Backslashes in a temp file name cause problems when filtering with | |
7436 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7437 * didn't set 'shellslash'. */ | |
7438 retval = vim_strsave(itmp); | |
7439 if (*p_shcf == '-' || p_ssl) | |
7440 for (p = retval; *p; ++p) | |
7441 if (*p == '\\') | |
7442 *p = '/'; | |
7443 return retval; | |
7444 | |
7445 # else /* WIN3264 */ | |
7446 | |
7447 # ifdef USE_TMPNAM | |
7448 /* tmpnam() will make its own name */ | |
7449 if (*tmpnam((char *)itmp) == NUL) | |
7450 return NULL; | |
7451 # else | |
7452 char_u *p; | |
7453 | |
7454 # ifdef VMS_TEMPNAM | |
7455 /* mktemp() is not working on VMS. It seems to be | |
7456 * a do-nothing function. Therefore we use tempnam(). | |
7457 */ | |
7458 sprintf((char *)itmp, "VIM%c", extra_char); | |
7459 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7460 if (p != NULL) | |
7461 { | |
7462 /* VMS will use '.LOG' if we don't explicitly specify an extension, | |
7463 * and VIM will then be unable to find the file later */ | |
7464 STRCPY(itmp, p); | |
7465 STRCAT(itmp, ".txt"); | |
7466 free(p); | |
7467 } | |
7468 else | |
7469 return NULL; | |
7470 # else | |
7471 STRCPY(itmp, TEMPNAME); | |
7472 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7473 *p = extra_char; | |
7474 if (mktemp((char *)itmp) == NULL) | |
7475 return NULL; | |
7476 # endif | |
7477 # endif | |
7478 | |
7479 return vim_strsave(itmp); | |
7480 # endif /* WIN3264 */ | |
7481 #endif /* TEMPDIRNAMES */ | |
7482 } | |
7483 | |
7484 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7485 /* | |
7486 * Convert all backslashes in fname to forward slashes in-place. | |
7487 */ | |
7488 void | |
7489 forward_slash(fname) | |
7490 char_u *fname; | |
7491 { | |
7492 char_u *p; | |
7493 | |
7494 for (p = fname; *p != NUL; ++p) | |
7495 # ifdef FEAT_MBYTE | |
7496 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7497 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7498 ++p; |
7499 else | |
7500 # endif | |
7501 if (*p == '\\') | |
7502 *p = '/'; | |
7503 } | |
7504 #endif | |
7505 | |
7506 | |
7507 /* | |
7508 * Code for automatic commands. | |
7509 * | |
7510 * Only included when "FEAT_AUTOCMD" has been defined. | |
7511 */ | |
7512 | |
7513 #if defined(FEAT_AUTOCMD) || defined(PROTO) | |
7514 | |
7515 /* | |
7516 * The autocommands are stored in a list for each event. | |
7517 * Autocommands for the same pattern, that are consecutive, are joined | |
7518 * together, to avoid having to match the pattern too often. | |
7519 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7520 * | |
7521 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL | |
7522 * Autopat.cmds Autopat.cmds | |
7523 * | | | |
7524 * V V | |
7525 * AutoCmd.next AutoCmd.next | |
7526 * | | | |
7527 * V V | |
7528 * AutoCmd.next NULL | |
7529 * | | |
7530 * V | |
7531 * NULL | |
7532 * | |
7533 * first_autopat[1] --> Autopat.next --> NULL | |
7534 * Autopat.cmds | |
7535 * | | |
7536 * V | |
7537 * AutoCmd.next | |
7538 * | | |
7539 * V | |
7540 * NULL | |
7541 * etc. | |
7542 * | |
7543 * The order of AutoCmds is important, this is the order in which they were | |
7544 * defined and will have to be executed. | |
7545 */ | |
7546 typedef struct AutoCmd | |
7547 { | |
7548 char_u *cmd; /* The command to be executed (NULL | |
7549 when command has been removed) */ | |
7550 char nested; /* If autocommands nest here */ | |
7551 char last; /* last command in list */ | |
7552 #ifdef FEAT_EVAL | |
7553 scid_T scriptID; /* script ID where defined */ | |
7554 #endif | |
7555 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7556 } AutoCmd; | |
7557 | |
7558 typedef struct AutoPat | |
7559 { | |
7560 int group; /* group ID */ | |
7561 char_u *pat; /* pattern as typed (NULL when pattern | |
7562 has been removed) */ | |
7563 int patlen; /* strlen() of pat */ | |
153 | 7564 regprog_T *reg_prog; /* compiled regprog for pattern */ |
7 | 7565 char allow_dirs; /* Pattern may match whole path */ |
7566 char last; /* last pattern for apply_autocmds() */ | |
7567 AutoCmd *cmds; /* list of commands to do */ | |
7568 struct AutoPat *next; /* next AutoPat in AutoPat list */ | |
40 | 7569 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7570 } AutoPat; |
7571 | |
7572 static struct event_name | |
7573 { | |
7574 char *name; /* event name */ | |
661 | 7575 event_T event; /* event number */ |
7 | 7576 } event_names[] = |
7577 { | |
7578 {"BufAdd", EVENT_BUFADD}, | |
7579 {"BufCreate", EVENT_BUFADD}, | |
7580 {"BufDelete", EVENT_BUFDELETE}, | |
7581 {"BufEnter", EVENT_BUFENTER}, | |
7582 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7583 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7584 {"BufHidden", EVENT_BUFHIDDEN}, | |
7585 {"BufLeave", EVENT_BUFLEAVE}, | |
7586 {"BufNew", EVENT_BUFNEW}, | |
7587 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7588 {"BufRead", EVENT_BUFREADPOST}, | |
7589 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7590 {"BufReadPost", EVENT_BUFREADPOST}, | |
7591 {"BufReadPre", EVENT_BUFREADPRE}, | |
7592 {"BufUnload", EVENT_BUFUNLOAD}, | |
7593 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7594 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7595 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7596 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7597 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7598 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7599 {"BufWriteCmd", EVENT_BUFWRITECMD}, | |
7600 {"CmdwinEnter", EVENT_CMDWINENTER}, | |
7601 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
12 | 7602 {"ColorScheme", EVENT_COLORSCHEME}, |
661 | 7603 {"CursorHold", EVENT_CURSORHOLD}, |
7604 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7605 {"CursorMoved", EVENT_CURSORMOVED}, | |
7606 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
7 | 7607 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
7608 {"FileEncoding", EVENT_ENCODINGCHANGED}, | |
7609 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7610 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7611 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7612 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7613 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7614 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7615 {"FileReadPost", EVENT_FILEREADPOST}, | |
7616 {"FileReadPre", EVENT_FILEREADPRE}, | |
7617 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7618 {"FileType", EVENT_FILETYPE}, | |
7619 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7620 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7621 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7622 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7623 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7624 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7625 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7626 {"FocusGained", EVENT_FOCUSGAINED}, | |
7627 {"FocusLost", EVENT_FOCUSLOST}, | |
7628 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7629 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7630 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7631 {"InsertChange", EVENT_INSERTCHANGE}, |
7632 {"InsertEnter", EVENT_INSERTENTER}, | |
7633 {"InsertLeave", EVENT_INSERTLEAVE}, | |
434 | 7634 {"MenuPopup", EVENT_MENUPOPUP}, |
161 | 7635 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7636 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
7 | 7637 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7638 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7639 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7640 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7641 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7642 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7643 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7644 {"StdinReadPost", EVENT_STDINREADPOST}, |
7645 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7646 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7647 {"Syntax", EVENT_SYNTAX}, |
676 | 7648 {"TabEnter", EVENT_TABENTER}, |
7649 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7650 {"TermChanged", EVENT_TERMCHANGED}, |
7651 {"TermResponse", EVENT_TERMRESPONSE}, | |
7652 {"User", EVENT_USER}, | |
7653 {"VimEnter", EVENT_VIMENTER}, | |
7654 {"VimLeave", EVENT_VIMLEAVE}, | |
7655 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
7656 {"WinEnter", EVENT_WINENTER}, | |
7657 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7658 {"VimResized", EVENT_VIMRESIZED}, |
661 | 7659 {NULL, (event_T)0} |
7 | 7660 }; |
7661 | |
7662 static AutoPat *first_autopat[NUM_EVENTS] = | |
7663 { | |
7664 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7665 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7666 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7667 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7668 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
7669 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
7 | 7670 }; |
7671 | |
7672 /* | |
7673 * struct used to keep status while executing autocommands for an event. | |
7674 */ | |
7675 typedef struct AutoPatCmd | |
7676 { | |
7677 AutoPat *curpat; /* next AutoPat to examine */ | |
7678 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7679 int group; /* group being used */ | |
7680 char_u *fname; /* fname to match with */ | |
7681 char_u *sfname; /* sfname to match with */ | |
7682 char_u *tail; /* tail of fname */ | |
661 | 7683 event_T event; /* current event */ |
40 | 7684 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7685 buf is deleted */ | |
7686 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7687 } AutoPatCmd; |
7688 | |
297 | 7689 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7690 |
7 | 7691 /* |
7692 * augroups stores a list of autocmd group names. | |
7693 */ | |
297 | 7694 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7695 #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
7696 | |
7697 /* | |
7698 * The ID of the current group. Group 0 is the default one. | |
7699 */ | |
7700 static int current_augroup = AUGROUP_DEFAULT; | |
7701 | |
7702 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7703 | |
661 | 7704 static void show_autocmd __ARGS((AutoPat *ap, event_T event)); |
7 | 7705 static void au_remove_pat __ARGS((AutoPat *ap)); |
7706 static void au_remove_cmds __ARGS((AutoPat *ap)); | |
7707 static void au_cleanup __ARGS((void)); | |
7708 static int au_new_group __ARGS((char_u *name)); | |
7709 static void au_del_group __ARGS((char_u *name)); | |
661 | 7710 static event_T event_name2nr __ARGS((char_u *start, char_u **end)); |
7711 static char_u *event_nr2name __ARGS((event_T event)); | |
7 | 7712 static char_u *find_end_event __ARGS((char_u *arg, int have_group)); |
661 | 7713 static int event_ignored __ARGS((event_T event)); |
7 | 7714 static int au_get_grouparg __ARGS((char_u **argp)); |
661 | 7715 static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group)); |
7 | 7716 static char_u *getnextac __ARGS((int c, void *cookie, int indent)); |
661 | 7717 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 | 7718 static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last)); |
7719 | |
40 | 7720 |
661 | 7721 static event_T last_event; |
7 | 7722 static int last_group; |
1410 | 7723 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7724 |
7725 /* | |
7726 * Show the autocommands for one AutoPat. | |
7727 */ | |
7728 static void | |
7729 show_autocmd(ap, event) | |
7730 AutoPat *ap; | |
661 | 7731 event_T event; |
7 | 7732 { |
7733 AutoCmd *ac; | |
7734 | |
7735 /* Check for "got_int" (here and at various places below), which is set | |
7736 * when "q" has been hit for the "--more--" prompt */ | |
7737 if (got_int) | |
7738 return; | |
7739 if (ap->pat == NULL) /* pattern has been removed */ | |
7740 return; | |
7741 | |
7742 msg_putchar('\n'); | |
7743 if (got_int) | |
7744 return; | |
7745 if (event != last_event || ap->group != last_group) | |
7746 { | |
7747 if (ap->group != AUGROUP_DEFAULT) | |
7748 { | |
7749 if (AUGROUP_NAME(ap->group) == NULL) | |
7750 msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E)); | |
7751 else | |
7752 msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T)); | |
7753 msg_puts((char_u *)" "); | |
7754 } | |
7755 msg_puts_attr(event_nr2name(event), hl_attr(HLF_T)); | |
7756 last_event = event; | |
7757 last_group = ap->group; | |
7758 msg_putchar('\n'); | |
7759 if (got_int) | |
7760 return; | |
7761 } | |
7762 msg_col = 4; | |
7763 msg_outtrans(ap->pat); | |
7764 | |
7765 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7766 { | |
7767 if (ac->cmd != NULL) /* skip removed commands */ | |
7768 { | |
7769 if (msg_col >= 14) | |
7770 msg_putchar('\n'); | |
7771 msg_col = 14; | |
7772 if (got_int) | |
7773 return; | |
7774 msg_outtrans(ac->cmd); | |
500 | 7775 #ifdef FEAT_EVAL |
7776 if (p_verbose > 0) | |
7777 last_set_msg(ac->scriptID); | |
7778 #endif | |
7 | 7779 if (got_int) |
7780 return; | |
7781 if (ac->next != NULL) | |
7782 { | |
7783 msg_putchar('\n'); | |
7784 if (got_int) | |
7785 return; | |
7786 } | |
7787 } | |
7788 } | |
7789 } | |
7790 | |
7791 /* | |
7792 * Mark an autocommand pattern for deletion. | |
7793 */ | |
7794 static void | |
7795 au_remove_pat(ap) | |
7796 AutoPat *ap; | |
7797 { | |
7798 vim_free(ap->pat); | |
7799 ap->pat = NULL; | |
40 | 7800 ap->buflocal_nr = -1; |
7 | 7801 au_need_clean = TRUE; |
7802 } | |
7803 | |
7804 /* | |
7805 * Mark all commands for a pattern for deletion. | |
7806 */ | |
7807 static void | |
7808 au_remove_cmds(ap) | |
7809 AutoPat *ap; | |
7810 { | |
7811 AutoCmd *ac; | |
7812 | |
7813 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7814 { | |
7815 vim_free(ac->cmd); | |
7816 ac->cmd = NULL; | |
7817 } | |
7818 au_need_clean = TRUE; | |
7819 } | |
7820 | |
7821 /* | |
7822 * Cleanup autocommands and patterns that have been deleted. | |
7823 * This is only done when not executing autocommands. | |
7824 */ | |
7825 static void | |
7826 au_cleanup() | |
7827 { | |
7828 AutoPat *ap, **prev_ap; | |
7829 AutoCmd *ac, **prev_ac; | |
661 | 7830 event_T event; |
7 | 7831 |
7832 if (autocmd_busy || !au_need_clean) | |
7833 return; | |
7834 | |
7835 /* loop over all events */ | |
661 | 7836 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7837 event = (event_T)((int)event + 1)) | |
7 | 7838 { |
7839 /* loop over all autocommand patterns */ | |
7840 prev_ap = &(first_autopat[(int)event]); | |
7841 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
7842 { | |
7843 /* loop over all commands for this pattern */ | |
7844 prev_ac = &(ap->cmds); | |
7845 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
7846 { | |
7847 /* remove the command if the pattern is to be deleted or when | |
7848 * the command has been marked for deletion */ | |
7849 if (ap->pat == NULL || ac->cmd == NULL) | |
7850 { | |
7851 *prev_ac = ac->next; | |
7852 vim_free(ac->cmd); | |
7853 vim_free(ac); | |
7854 } | |
7855 else | |
7856 prev_ac = &(ac->next); | |
7857 } | |
7858 | |
7859 /* remove the pattern if it has been marked for deletion */ | |
7860 if (ap->pat == NULL) | |
7861 { | |
7862 *prev_ap = ap->next; | |
153 | 7863 vim_free(ap->reg_prog); |
7 | 7864 vim_free(ap); |
7865 } | |
7866 else | |
7867 prev_ap = &(ap->next); | |
7868 } | |
7869 } | |
7870 | |
7871 au_need_clean = FALSE; | |
7872 } | |
7873 | |
7874 /* | |
40 | 7875 * Called when buffer is freed, to remove/invalidate related buffer-local |
7876 * autocmds. | |
7877 */ | |
7878 void | |
7879 aubuflocal_remove(buf) | |
7880 buf_T *buf; | |
7881 { | |
7882 AutoPat *ap; | |
661 | 7883 event_T event; |
40 | 7884 AutoPatCmd *apc; |
7885 | |
7886 /* invalidate currently executing autocommands */ | |
7887 for (apc = active_apc_list; apc; apc = apc->next) | |
7888 if (buf->b_fnum == apc->arg_bufnr) | |
7889 apc->arg_bufnr = 0; | |
7890 | |
7891 /* invalidate buflocals looping through events */ | |
661 | 7892 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7893 event = (event_T)((int)event + 1)) | |
40 | 7894 /* loop over all autocommand patterns */ |
7895 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
7896 if (ap->buflocal_nr == buf->b_fnum) | |
7897 { | |
7898 au_remove_pat(ap); | |
7899 if (p_verbose >= 6) | |
292 | 7900 { |
7901 verbose_enter(); | |
40 | 7902 smsg((char_u *) |
7903 _("auto-removing autocommand: %s <buffer=%d>"), | |
7904 event_nr2name(event), buf->b_fnum); | |
292 | 7905 verbose_leave(); |
7906 } | |
40 | 7907 } |
7908 au_cleanup(); | |
7909 } | |
7910 | |
7911 /* | |
7 | 7912 * Add an autocmd group name. |
7913 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
7914 */ | |
7915 static int | |
7916 au_new_group(name) | |
7917 char_u *name; | |
7918 { | |
7919 int i; | |
7920 | |
7921 i = au_find_group(name); | |
7922 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
7923 { | |
7924 /* First try using a free entry. */ | |
7925 for (i = 0; i < augroups.ga_len; ++i) | |
7926 if (AUGROUP_NAME(i) == NULL) | |
7927 break; | |
7928 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
7929 return AUGROUP_ERROR; | |
7930 | |
7931 AUGROUP_NAME(i) = vim_strsave(name); | |
7932 if (AUGROUP_NAME(i) == NULL) | |
7933 return AUGROUP_ERROR; | |
7934 if (i == augroups.ga_len) | |
7935 ++augroups.ga_len; | |
7936 } | |
7937 | |
7938 return i; | |
7939 } | |
7940 | |
7941 static void | |
7942 au_del_group(name) | |
7943 char_u *name; | |
7944 { | |
7945 int i; | |
7946 | |
7947 i = au_find_group(name); | |
7948 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
7949 EMSG2(_("E367: No such group: \"%s\""), name); | |
7950 else | |
7951 { | |
7952 vim_free(AUGROUP_NAME(i)); | |
7953 AUGROUP_NAME(i) = NULL; | |
7954 } | |
7955 } | |
7956 | |
7957 /* | |
7958 * Find the ID of an autocmd group name. | |
7959 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
7960 */ | |
7961 static int | |
7962 au_find_group(name) | |
7963 char_u *name; | |
7964 { | |
7965 int i; | |
7966 | |
7967 for (i = 0; i < augroups.ga_len; ++i) | |
7968 if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0) | |
7969 return i; | |
7970 return AUGROUP_ERROR; | |
7971 } | |
7972 | |
461 | 7973 /* |
7974 * Return TRUE if augroup "name" exists. | |
7975 */ | |
7976 int | |
7977 au_has_group(name) | |
7978 char_u *name; | |
7979 { | |
7980 return au_find_group(name) != AUGROUP_ERROR; | |
7981 } | |
7982 | |
7 | 7983 /* |
7984 * ":augroup {name}". | |
7985 */ | |
7986 void | |
7987 do_augroup(arg, del_group) | |
7988 char_u *arg; | |
7989 int del_group; | |
7990 { | |
7991 int i; | |
7992 | |
7993 if (del_group) | |
7994 { | |
7995 if (*arg == NUL) | |
7996 EMSG(_(e_argreq)); | |
7997 else | |
7998 au_del_group(arg); | |
7999 } | |
8000 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8001 current_augroup = AUGROUP_DEFAULT; | |
8002 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8003 { | |
8004 i = au_new_group(arg); | |
8005 if (i != AUGROUP_ERROR) | |
8006 current_augroup = i; | |
8007 } | |
8008 else /* ":aug": list the group names */ | |
8009 { | |
8010 msg_start(); | |
8011 for (i = 0; i < augroups.ga_len; ++i) | |
8012 { | |
8013 if (AUGROUP_NAME(i) != NULL) | |
8014 { | |
8015 msg_puts(AUGROUP_NAME(i)); | |
8016 msg_puts((char_u *)" "); | |
8017 } | |
8018 } | |
8019 msg_clr_eos(); | |
8020 msg_end(); | |
8021 } | |
8022 } | |
8023 | |
355 | 8024 #if defined(EXITFREE) || defined(PROTO) |
8025 void | |
8026 free_all_autocmds() | |
8027 { | |
8028 for (current_augroup = -1; current_augroup < augroups.ga_len; | |
8029 ++current_augroup) | |
8030 do_autocmd((char_u *)"", TRUE); | |
8031 ga_clear_strings(&augroups); | |
8032 } | |
8033 #endif | |
8034 | |
7 | 8035 /* |
8036 * Return the event number for event name "start". | |
8037 * Return NUM_EVENTS if the event name was not found. | |
8038 * Return a pointer to the next event name in "end". | |
8039 */ | |
661 | 8040 static event_T |
7 | 8041 event_name2nr(start, end) |
8042 char_u *start; | |
8043 char_u **end; | |
8044 { | |
8045 char_u *p; | |
8046 int i; | |
8047 int len; | |
8048 | |
8049 /* the event name ends with end of line, a blank or a comma */ | |
8050 for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p) | |
8051 ; | |
8052 for (i = 0; event_names[i].name != NULL; ++i) | |
8053 { | |
8054 len = (int)STRLEN(event_names[i].name); | |
8055 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8056 break; | |
8057 } | |
8058 if (*p == ',') | |
8059 ++p; | |
8060 *end = p; | |
8061 if (event_names[i].name == NULL) | |
8062 return NUM_EVENTS; | |
8063 return event_names[i].event; | |
8064 } | |
8065 | |
8066 /* | |
8067 * Return the name for event "event". | |
8068 */ | |
8069 static char_u * | |
8070 event_nr2name(event) | |
661 | 8071 event_T event; |
7 | 8072 { |
8073 int i; | |
8074 | |
8075 for (i = 0; event_names[i].name != NULL; ++i) | |
8076 if (event_names[i].event == event) | |
8077 return (char_u *)event_names[i].name; | |
8078 return (char_u *)"Unknown"; | |
8079 } | |
8080 | |
8081 /* | |
8082 * Scan over the events. "*" stands for all events. | |
8083 */ | |
8084 static char_u * | |
8085 find_end_event(arg, have_group) | |
8086 char_u *arg; | |
8087 int have_group; /* TRUE when group name was found */ | |
8088 { | |
8089 char_u *pat; | |
8090 char_u *p; | |
8091 | |
8092 if (*arg == '*') | |
8093 { | |
8094 if (arg[1] && !vim_iswhite(arg[1])) | |
8095 { | |
8096 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8097 return NULL; | |
8098 } | |
8099 pat = arg + 1; | |
8100 } | |
8101 else | |
8102 { | |
8103 for (pat = arg; *pat && !vim_iswhite(*pat); pat = p) | |
8104 { | |
8105 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8106 { | |
8107 if (have_group) | |
8108 EMSG2(_("E216: No such event: %s"), pat); | |
8109 else | |
8110 EMSG2(_("E216: No such group or event: %s"), pat); | |
8111 return NULL; | |
8112 } | |
8113 } | |
8114 } | |
8115 return pat; | |
8116 } | |
8117 | |
8118 /* | |
8119 * Return TRUE if "event" is included in 'eventignore'. | |
8120 */ | |
8121 static int | |
8122 event_ignored(event) | |
661 | 8123 event_T event; |
7 | 8124 { |
8125 char_u *p = p_ei; | |
8126 | |
844 | 8127 while (*p != NUL) |
8128 { | |
8129 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8130 return TRUE; | |
7 | 8131 if (event_name2nr(p, &p) == event) |
8132 return TRUE; | |
844 | 8133 } |
7 | 8134 |
8135 return FALSE; | |
8136 } | |
8137 | |
8138 /* | |
8139 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8140 */ | |
8141 int | |
8142 check_ei() | |
8143 { | |
8144 char_u *p = p_ei; | |
8145 | |
8146 while (*p) | |
844 | 8147 { |
8148 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8149 { | |
8150 p += 3; | |
8151 if (*p == ',') | |
8152 ++p; | |
8153 } | |
8154 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8155 return FAIL; |
844 | 8156 } |
7 | 8157 |
8158 return OK; | |
8159 } | |
8160 | |
123 | 8161 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8162 | |
8163 /* | |
8164 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8165 * buffer loaded into the window. "what" must start with a comma. | |
8166 * Returns the old value of 'eventignore' in allocated memory. | |
8167 */ | |
8168 char_u * | |
8169 au_event_disable(what) | |
8170 char *what; | |
8171 { | |
8172 char_u *new_ei; | |
8173 char_u *save_ei; | |
8174 | |
8175 save_ei = vim_strsave(p_ei); | |
8176 if (save_ei != NULL) | |
8177 { | |
557 | 8178 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8179 if (new_ei != NULL) |
8180 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8181 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8182 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8183 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8184 STRCAT(new_ei, what); |
694 | 8185 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8186 OPT_FREE, SID_NONE); | |
123 | 8187 vim_free(new_ei); |
8188 } | |
8189 } | |
8190 return save_ei; | |
8191 } | |
8192 | |
8193 void | |
8194 au_event_restore(old_ei) | |
8195 char_u *old_ei; | |
8196 { | |
8197 if (old_ei != NULL) | |
8198 { | |
694 | 8199 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8200 OPT_FREE, SID_NONE); | |
123 | 8201 vim_free(old_ei); |
8202 } | |
8203 } | |
8204 # endif /* FEAT_SYN_HL */ | |
8205 | |
7 | 8206 /* |
8207 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8208 * following ways: | |
8209 * | |
8210 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8211 * will be automatically executed for <event> | |
8212 * when editing a file matching <pat>, in | |
8213 * the current group. | |
8214 * :autocmd <event> <pat> Show the auto-commands associated with | |
8215 * <event> and <pat>. | |
8216 * :autocmd <event> Show the auto-commands associated with | |
8217 * <event>. | |
8218 * :autocmd Show all auto-commands. | |
8219 * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with | |
8220 * <event> and <pat>, and add the command | |
8221 * <cmd>, for the current group. | |
8222 * :autocmd! <event> <pat> Remove all auto-commands associated with | |
8223 * <event> and <pat> for the current group. | |
8224 * :autocmd! <event> Remove all auto-commands associated with | |
8225 * <event> for the current group. | |
8226 * :autocmd! Remove ALL auto-commands for the current | |
8227 * group. | |
8228 * | |
8229 * Multiple events and patterns may be given separated by commas. Here are | |
8230 * some examples: | |
8231 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8232 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8233 * | |
8234 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8235 * |
8236 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8237 */ |
8238 void | |
8239 do_autocmd(arg, forceit) | |
8240 char_u *arg; | |
8241 int forceit; | |
8242 { | |
8243 char_u *pat; | |
8244 char_u *envpat = NULL; | |
8245 char_u *cmd; | |
661 | 8246 event_T event; |
7 | 8247 int need_free = FALSE; |
8248 int nested = FALSE; | |
8249 int group; | |
8250 | |
8251 /* | |
8252 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8253 */ | |
8254 group = au_get_grouparg(&arg); | |
8255 if (arg == NULL) /* out of memory */ | |
8256 return; | |
8257 | |
8258 /* | |
8259 * Scan over the events. | |
8260 * If we find an illegal name, return here, don't do anything. | |
8261 */ | |
8262 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8263 if (pat == NULL) | |
8264 return; | |
8265 | |
8266 /* | |
8267 * Scan over the pattern. Put a NUL at the end. | |
8268 */ | |
8269 pat = skipwhite(pat); | |
8270 cmd = pat; | |
8271 while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\')) | |
8272 cmd++; | |
8273 if (*cmd) | |
8274 *cmd++ = NUL; | |
8275 | |
8276 /* Expand environment variables in the pattern. Set 'shellslash', we want | |
8277 * forward slashes here. */ | |
8278 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) | |
8279 { | |
8280 #ifdef BACKSLASH_IN_FILENAME | |
8281 int p_ssl_save = p_ssl; | |
8282 | |
8283 p_ssl = TRUE; | |
8284 #endif | |
8285 envpat = expand_env_save(pat); | |
8286 #ifdef BACKSLASH_IN_FILENAME | |
8287 p_ssl = p_ssl_save; | |
8288 #endif | |
8289 if (envpat != NULL) | |
8290 pat = envpat; | |
8291 } | |
8292 | |
8293 /* | |
8294 * Check for "nested" flag. | |
8295 */ | |
8296 cmd = skipwhite(cmd); | |
8297 if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6])) | |
8298 { | |
8299 nested = TRUE; | |
8300 cmd = skipwhite(cmd + 6); | |
8301 } | |
8302 | |
8303 /* | |
8304 * Find the start of the commands. | |
8305 * Expand <sfile> in it. | |
8306 */ | |
8307 if (*cmd != NUL) | |
8308 { | |
8309 cmd = expand_sfile(cmd); | |
8310 if (cmd == NULL) /* some error */ | |
8311 return; | |
8312 need_free = TRUE; | |
8313 } | |
8314 | |
8315 /* | |
8316 * Print header when showing autocommands. | |
8317 */ | |
8318 if (!forceit && *cmd == NUL) | |
8319 { | |
8320 /* Highlight title */ | |
8321 MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); | |
8322 } | |
8323 | |
8324 /* | |
8325 * Loop over the events. | |
8326 */ | |
661 | 8327 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8328 last_group = AUGROUP_ERROR; /* for listing the group name */ |
8329 if (*arg == '*' || *arg == NUL) | |
8330 { | |
661 | 8331 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8332 event = (event_T)((int)event + 1)) | |
7 | 8333 if (do_autocmd_event(event, pat, |
8334 nested, cmd, forceit, group) == FAIL) | |
8335 break; | |
8336 } | |
8337 else | |
8338 { | |
8339 while (*arg && !vim_iswhite(*arg)) | |
8340 if (do_autocmd_event(event_name2nr(arg, &arg), pat, | |
8341 nested, cmd, forceit, group) == FAIL) | |
8342 break; | |
8343 } | |
8344 | |
8345 if (need_free) | |
8346 vim_free(cmd); | |
8347 vim_free(envpat); | |
8348 } | |
8349 | |
8350 /* | |
8351 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8352 * The "argp" argument is advanced to the following argument. | |
8353 * | |
8354 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8355 */ | |
8356 static int | |
8357 au_get_grouparg(argp) | |
8358 char_u **argp; | |
8359 { | |
8360 char_u *group_name; | |
8361 char_u *p; | |
8362 char_u *arg = *argp; | |
8363 int group = AUGROUP_ALL; | |
8364 | |
8365 p = skiptowhite(arg); | |
8366 if (p > arg) | |
8367 { | |
8368 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8369 if (group_name == NULL) /* out of memory */ | |
8370 return AUGROUP_ERROR; | |
8371 group = au_find_group(group_name); | |
8372 if (group == AUGROUP_ERROR) | |
8373 group = AUGROUP_ALL; /* no match, use all groups */ | |
8374 else | |
8375 *argp = skipwhite(p); /* match, skip over group name */ | |
8376 vim_free(group_name); | |
8377 } | |
8378 return group; | |
8379 } | |
8380 | |
8381 /* | |
8382 * do_autocmd() for one event. | |
8383 * If *pat == NUL do for all patterns. | |
8384 * If *cmd == NUL show entries. | |
8385 * If forceit == TRUE delete entries. | |
8386 * If group is not AUGROUP_ALL, only use this group. | |
8387 */ | |
8388 static int | |
8389 do_autocmd_event(event, pat, nested, cmd, forceit, group) | |
661 | 8390 event_T event; |
7 | 8391 char_u *pat; |
8392 int nested; | |
8393 char_u *cmd; | |
8394 int forceit; | |
8395 int group; | |
8396 { | |
8397 AutoPat *ap; | |
8398 AutoPat **prev_ap; | |
8399 AutoCmd *ac; | |
8400 AutoCmd **prev_ac; | |
8401 int brace_level; | |
8402 char_u *endpat; | |
8403 int findgroup; | |
8404 int allgroups; | |
8405 int patlen; | |
856 | 8406 int is_buflocal; |
40 | 8407 int buflocal_nr; |
8408 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8409 |
8410 if (group == AUGROUP_ALL) | |
8411 findgroup = current_augroup; | |
8412 else | |
8413 findgroup = group; | |
8414 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8415 | |
8416 /* | |
8417 * Show or delete all patterns for an event. | |
8418 */ | |
8419 if (*pat == NUL) | |
8420 { | |
8421 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8422 { | |
8423 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8424 { | |
8425 if (ap->group == findgroup) | |
8426 au_remove_pat(ap); | |
8427 } | |
8428 else if (group == AUGROUP_ALL || ap->group == group) | |
8429 show_autocmd(ap, event); | |
8430 } | |
8431 } | |
8432 | |
8433 /* | |
8434 * Loop through all the specified patterns. | |
8435 */ | |
8436 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8437 { | |
8438 /* | |
8439 * Find end of the pattern. | |
8440 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8441 */ | |
8442 brace_level = 0; | |
8443 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
8444 || endpat[-1] == '\\'); ++endpat) | |
8445 { | |
8446 if (*endpat == '{') | |
8447 brace_level++; | |
8448 else if (*endpat == '}') | |
8449 brace_level--; | |
8450 } | |
8451 if (pat == endpat) /* ignore single comma */ | |
8452 continue; | |
8453 patlen = (int)(endpat - pat); | |
8454 | |
8455 /* | |
40 | 8456 * detect special <buflocal[=X]> buffer-local patterns |
8457 */ | |
8458 is_buflocal = FALSE; | |
8459 buflocal_nr = 0; | |
8460 | |
8461 if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0 | |
8462 && pat[patlen - 1] == '>') | |
8463 { | |
8464 /* Error will be printed only for addition. printing and removing | |
8465 * will proceed silently. */ | |
8466 is_buflocal = TRUE; | |
8467 if (patlen == 8) | |
8468 buflocal_nr = curbuf->b_fnum; | |
8469 else if (patlen > 9 && pat[7] == '=') | |
8470 { | |
8471 /* <buffer=abuf> */ | |
8472 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13)) | |
8473 buflocal_nr = autocmd_bufnr; | |
8474 /* <buffer=123> */ | |
8475 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
8476 buflocal_nr = atoi((char *)pat + 8); | |
8477 } | |
8478 } | |
8479 | |
8480 if (is_buflocal) | |
8481 { | |
8482 /* normalize pat into standard "<buffer>#N" form */ | |
8483 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8484 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8485 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8486 } |
8487 | |
8488 /* | |
7 | 8489 * Find AutoPat entries with this pattern. |
8490 */ | |
8491 prev_ap = &first_autopat[(int)event]; | |
8492 while ((ap = *prev_ap) != NULL) | |
8493 { | |
8494 if (ap->pat != NULL) | |
8495 { | |
8496 /* Accept a pattern when: | |
8497 * - a group was specified and it's that group, or a group was | |
8498 * not specified and it's the current group, or a group was | |
8499 * not specified and we are listing | |
8500 * - the length of the pattern matches | |
40 | 8501 * - the pattern matches. |
8502 * For <buffer[=X]>, this condition works because we normalize | |
8503 * all buffer-local patterns. | |
7 | 8504 */ |
8505 if ((allgroups || ap->group == findgroup) | |
8506 && ap->patlen == patlen | |
8507 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8508 { | |
8509 /* | |
8510 * Remove existing autocommands. | |
8511 * If adding any new autocmd's for this AutoPat, don't | |
8512 * delete the pattern from the autopat list, append to | |
8513 * this list. | |
8514 */ | |
8515 if (forceit) | |
8516 { | |
8517 if (*cmd != NUL && ap->next == NULL) | |
8518 { | |
8519 au_remove_cmds(ap); | |
8520 break; | |
8521 } | |
8522 au_remove_pat(ap); | |
8523 } | |
8524 | |
8525 /* | |
40 | 8526 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8527 */ |
8528 else if (*cmd == NUL) | |
8529 show_autocmd(ap, event); | |
8530 | |
8531 /* | |
8532 * Add autocmd to this autopat, if it's the last one. | |
8533 */ | |
8534 else if (ap->next == NULL) | |
8535 break; | |
8536 } | |
8537 } | |
8538 prev_ap = &ap->next; | |
8539 } | |
8540 | |
8541 /* | |
8542 * Add a new command. | |
8543 */ | |
8544 if (*cmd != NUL) | |
8545 { | |
8546 /* | |
8547 * If the pattern we want to add a command to does appear at the | |
8548 * end of the list (or not is not in the list at all), add the | |
8549 * pattern at the end of the list. | |
8550 */ | |
8551 if (ap == NULL) | |
8552 { | |
40 | 8553 /* refuse to add buffer-local ap if buffer number is invalid */ |
8554 if (is_buflocal && (buflocal_nr == 0 | |
8555 || buflist_findnr(buflocal_nr) == NULL)) | |
8556 { | |
8557 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8558 buflocal_nr); | |
8559 return FAIL; | |
8560 } | |
8561 | |
7 | 8562 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8563 if (ap == NULL) | |
8564 return FAIL; | |
8565 ap->pat = vim_strnsave(pat, patlen); | |
8566 ap->patlen = patlen; | |
8567 if (ap->pat == NULL) | |
8568 { | |
8569 vim_free(ap); | |
8570 return FAIL; | |
8571 } | |
40 | 8572 |
8573 if (is_buflocal) | |
8574 { | |
8575 ap->buflocal_nr = buflocal_nr; | |
153 | 8576 ap->reg_prog = NULL; |
40 | 8577 } |
8578 else | |
7 | 8579 { |
153 | 8580 char_u *reg_pat; |
8581 | |
40 | 8582 ap->buflocal_nr = 0; |
153 | 8583 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8584 &ap->allow_dirs, TRUE); |
153 | 8585 if (reg_pat != NULL) |
8586 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8587 vim_free(reg_pat); |
153 | 8588 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8589 { |
8590 vim_free(ap->pat); | |
8591 vim_free(ap); | |
8592 return FAIL; | |
8593 } | |
7 | 8594 } |
8595 ap->cmds = NULL; | |
8596 *prev_ap = ap; | |
8597 ap->next = NULL; | |
8598 if (group == AUGROUP_ALL) | |
8599 ap->group = current_augroup; | |
8600 else | |
8601 ap->group = group; | |
8602 } | |
8603 | |
8604 /* | |
8605 * Add the autocmd at the end of the AutoCmd list. | |
8606 */ | |
8607 prev_ac = &(ap->cmds); | |
8608 while ((ac = *prev_ac) != NULL) | |
8609 prev_ac = &ac->next; | |
8610 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8611 if (ac == NULL) | |
8612 return FAIL; | |
8613 ac->cmd = vim_strsave(cmd); | |
8614 #ifdef FEAT_EVAL | |
8615 ac->scriptID = current_SID; | |
8616 #endif | |
8617 if (ac->cmd == NULL) | |
8618 { | |
8619 vim_free(ac); | |
8620 return FAIL; | |
8621 } | |
8622 ac->next = NULL; | |
8623 *prev_ac = ac; | |
8624 ac->nested = nested; | |
8625 } | |
8626 } | |
8627 | |
8628 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8629 return OK; | |
8630 } | |
8631 | |
8632 /* | |
8633 * Implementation of ":doautocmd [group] event [fname]". | |
8634 * Return OK for success, FAIL for failure; | |
8635 */ | |
8636 int | |
8637 do_doautocmd(arg, do_msg) | |
8638 char_u *arg; | |
8639 int do_msg; /* give message for no matching autocmds? */ | |
8640 { | |
8641 char_u *fname; | |
8642 int nothing_done = TRUE; | |
8643 int group; | |
8644 | |
8645 /* | |
8646 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8647 */ | |
8648 group = au_get_grouparg(&arg); | |
8649 if (arg == NULL) /* out of memory */ | |
8650 return FAIL; | |
8651 | |
8652 if (*arg == '*') | |
8653 { | |
8654 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8655 return FAIL; | |
8656 } | |
8657 | |
8658 /* | |
8659 * Scan over the events. | |
8660 * If we find an illegal name, return here, don't do anything. | |
8661 */ | |
8662 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8663 if (fname == NULL) | |
8664 return FAIL; | |
8665 | |
8666 fname = skipwhite(fname); | |
8667 | |
8668 /* | |
8669 * Loop over the events. | |
8670 */ | |
8671 while (*arg && !vim_iswhite(*arg)) | |
8672 if (apply_autocmds_group(event_name2nr(arg, &arg), | |
8673 fname, NULL, TRUE, group, curbuf, NULL)) | |
8674 nothing_done = FALSE; | |
8675 | |
8676 if (nothing_done && do_msg) | |
8677 MSG(_("No matching autocommands")); | |
8678 | |
8679 #ifdef FEAT_EVAL | |
8680 return aborting() ? FAIL : OK; | |
8681 #else | |
8682 return OK; | |
8683 #endif | |
8684 } | |
8685 | |
8686 /* | |
8687 * ":doautoall": execute autocommands for each loaded buffer. | |
8688 */ | |
8689 void | |
8690 ex_doautoall(eap) | |
8691 exarg_T *eap; | |
8692 { | |
8693 int retval; | |
8694 aco_save_T aco; | |
8695 buf_T *buf; | |
8696 | |
8697 /* | |
8698 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8699 * equal to curbuf, but for some buffers there may not be a window. | |
8700 * So we change the buffer for the current window for a moment. This | |
8701 * gives problems when the autocommands make changes to the list of | |
8702 * buffers or windows... | |
8703 */ | |
8704 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
8705 { | |
1665 | 8706 if (buf->b_ml.ml_mfp != NULL) |
7 | 8707 { |
8708 /* find a window for this buffer and save some values */ | |
8709 aucmd_prepbuf(&aco, buf); | |
8710 | |
8711 /* execute the autocommands for this buffer */ | |
8712 retval = do_doautocmd(eap->arg, FALSE); | |
1113 | 8713 |
8714 /* Execute the modeline settings, but don't set window-local | |
8715 * options if we are using the current window for another buffer. */ | |
1906 | 8716 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); |
7 | 8717 |
8718 /* restore the current window */ | |
8719 aucmd_restbuf(&aco); | |
8720 | |
8721 /* stop if there is some error or buffer was deleted */ | |
8722 if (retval == FAIL || !buf_valid(buf)) | |
8723 break; | |
8724 } | |
8725 } | |
8726 | |
8727 check_cursor(); /* just in case lines got deleted */ | |
8728 } | |
8729 | |
8730 /* | |
8731 * Prepare for executing autocommands for (hidden) buffer "buf". | |
1906 | 8732 * Search for a visible window containing the current buffer. If there isn't |
8733 * one then use "aucmd_win". | |
7 | 8734 * Set "curbuf" and "curwin" to match "buf". |
934 | 8735 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8736 */ |
8737 void | |
8738 aucmd_prepbuf(aco, buf) | |
8739 aco_save_T *aco; /* structure to save values in */ | |
8740 buf_T *buf; /* new curbuf */ | |
8741 { | |
8742 win_T *win; | |
1906 | 8743 #ifdef FEAT_WINDOWS |
8744 int save_ea; | |
8745 #endif | |
7 | 8746 |
8747 /* Find a window that is for the new buffer */ | |
8748 if (buf == curbuf) /* be quick when buf is curbuf */ | |
8749 win = curwin; | |
8750 else | |
8751 #ifdef FEAT_WINDOWS | |
8752 for (win = firstwin; win != NULL; win = win->w_next) | |
8753 if (win->w_buffer == buf) | |
8754 break; | |
8755 #else | |
8756 win = NULL; | |
8757 #endif | |
8758 | |
1906 | 8759 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
8760 * back to using the current window. */ | |
8761 if (win == NULL && aucmd_win == NULL) | |
8762 { | |
8763 win_alloc_aucmd_win(); | |
8764 if (aucmd_win == NULL) | |
8765 win = curwin; | |
8766 } | |
1944 | 8767 if (win == NULL && aucmd_win_used) |
8768 /* Strange recursive autocommand, fall back to using the current | |
8769 * window. Expect a few side effects... */ | |
8770 win = curwin; | |
1906 | 8771 |
8772 aco->save_curwin = curwin; | |
8773 aco->save_curbuf = curbuf; | |
7 | 8774 if (win != NULL) |
8775 { | |
1906 | 8776 /* There is a window for "buf" in the current tab page, make it the |
8777 * curwin. This is preferred, it has the least side effects (esp. if | |
8778 * "buf" is curbuf). */ | |
1944 | 8779 aco->use_aucmd_win = FALSE; |
7 | 8780 curwin = win; |
8781 } | |
8782 else | |
8783 { | |
1906 | 8784 /* There is no window for "buf", use "aucmd_win". To minimize the side |
8785 * effects, insert it in a the current tab page. | |
8786 * Anything related to a window (e.g., setting folds) may have | |
8787 * unexpected results. */ | |
1944 | 8788 aco->use_aucmd_win = TRUE; |
8789 aucmd_win_used = TRUE; | |
1918 | 8790 aucmd_win->w_buffer = buf; |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
8791 aucmd_win->w_s = &buf->b_s; |
7 | 8792 ++buf->b_nwindows; |
1918 | 8793 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 8794 vim_free(aucmd_win->w_localdir); |
8795 aucmd_win->w_localdir = NULL; | |
8796 | |
8797 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
8798 * win_enter_ext(). */ | |
8799 aucmd_win->w_localdir = NULL; | |
8800 aco->globaldir = globaldir; | |
8801 globaldir = NULL; | |
8802 | |
7 | 8803 |
1906 | 8804 #ifdef FEAT_WINDOWS |
1923 | 8805 /* Split the current window, put the aucmd_win in the upper half. |
8806 * We don't want the BufEnter or WinEnter autocommands. */ | |
8807 block_autocmds(); | |
1906 | 8808 make_snapshot(SNAP_AUCMD_IDX); |
8809 save_ea = p_ea; | |
8810 p_ea = FALSE; | |
8811 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); | |
8812 (void)win_comp_pos(); /* recompute window positions */ | |
8813 p_ea = save_ea; | |
1923 | 8814 unblock_autocmds(); |
1906 | 8815 #endif |
1918 | 8816 curwin = aucmd_win; |
1906 | 8817 } |
7 | 8818 curbuf = buf; |
1906 | 8819 aco->new_curwin = curwin; |
8820 aco->new_curbuf = curbuf; | |
7 | 8821 } |
8822 | |
8823 /* | |
8824 * Cleanup after executing autocommands for a (hidden) buffer. | |
8825 * Restore the window as it was (if possible). | |
934 | 8826 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8827 */ |
8828 void | |
8829 aucmd_restbuf(aco) | |
8830 aco_save_T *aco; /* structure holding saved values */ | |
8831 { | |
1906 | 8832 #ifdef FEAT_WINDOWS |
8833 int dummy; | |
8834 #endif | |
8835 | |
1944 | 8836 if (aco->use_aucmd_win) |
1906 | 8837 { |
8838 --curbuf->b_nwindows; | |
8839 #ifdef FEAT_WINDOWS | |
8840 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 8841 * page. Do not trigger autocommands here. */ |
8842 block_autocmds(); | |
1906 | 8843 if (curwin != aucmd_win) |
8844 { | |
8845 tabpage_T *tp; | |
8846 win_T *wp; | |
8847 | |
8848 FOR_ALL_TAB_WINDOWS(tp, wp) | |
8849 { | |
8850 if (wp == aucmd_win) | |
8851 { | |
8852 if (tp != curtab) | |
8853 goto_tabpage_tp(tp); | |
8854 win_goto(aucmd_win); | |
8855 break; | |
8856 } | |
8857 } | |
8858 } | |
8859 | |
8860 /* Remove the window and frame from the tree of frames. */ | |
8861 (void)winframe_remove(curwin, &dummy, NULL); | |
8862 win_remove(curwin, NULL); | |
1944 | 8863 aucmd_win_used = FALSE; |
1906 | 8864 last_status(FALSE); /* may need to remove last status line */ |
8865 restore_snapshot(SNAP_AUCMD_IDX, FALSE); | |
8866 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 8867 unblock_autocmds(); |
1906 | 8868 |
8869 if (win_valid(aco->save_curwin)) | |
8870 curwin = aco->save_curwin; | |
8871 else | |
8872 /* Hmm, original window disappeared. Just use the first one. */ | |
8873 curwin = firstwin; | |
8874 # ifdef FEAT_EVAL | |
8875 vars_clear(&aucmd_win->w_vars.dv_hashtab); /* free all w: variables */ | |
2001 | 8876 hash_init(&aucmd_win->w_vars.dv_hashtab); /* re-use the hashtab */ |
1906 | 8877 # endif |
8878 #else | |
8879 curwin = aco->save_curwin; | |
8880 #endif | |
8881 curbuf = curwin->w_buffer; | |
8882 | |
1944 | 8883 vim_free(globaldir); |
8884 globaldir = aco->globaldir; | |
8885 | |
1906 | 8886 /* the buffer contents may have changed */ |
8887 check_cursor(); | |
8888 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
8889 { | |
8890 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
8891 #ifdef FEAT_DIFF | |
8892 curwin->w_topfill = 0; | |
8893 #endif | |
8894 } | |
8895 #if defined(FEAT_GUI) | |
8896 /* Hide the scrollbars from the aucmd_win and update. */ | |
8897 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
8898 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
8899 gui_may_update_scrollbars(); | |
8900 #endif | |
8901 } | |
8902 else | |
7 | 8903 { |
8904 /* restore curwin */ | |
8905 #ifdef FEAT_WINDOWS | |
8906 if (win_valid(aco->save_curwin)) | |
8907 #endif | |
8908 { | |
1906 | 8909 /* Restore the buffer which was previously edited by curwin, if |
1944 | 8910 * it was changed, we are still the same window and the buffer is |
1906 | 8911 * valid. */ |
7 | 8912 if (curwin == aco->new_curwin |
1906 | 8913 && curbuf != aco->new_curbuf |
8914 && buf_valid(aco->new_curbuf) | |
8915 && aco->new_curbuf->b_ml.ml_mfp != NULL) | |
7 | 8916 { |
8917 --curbuf->b_nwindows; | |
1906 | 8918 curbuf = aco->new_curbuf; |
7 | 8919 curwin->w_buffer = curbuf; |
8920 ++curbuf->b_nwindows; | |
8921 } | |
8922 | |
8923 curwin = aco->save_curwin; | |
8924 curbuf = curwin->w_buffer; | |
8925 } | |
8926 } | |
8927 } | |
8928 | |
8929 static int autocmd_nested = FALSE; | |
8930 | |
8931 /* | |
8932 * Execute autocommands for "event" and file name "fname". | |
8933 * Return TRUE if some commands were executed. | |
8934 */ | |
8935 int | |
8936 apply_autocmds(event, fname, fname_io, force, buf) | |
661 | 8937 event_T event; |
7 | 8938 char_u *fname; /* NULL or empty means use actual file name */ |
8939 char_u *fname_io; /* fname to use for <afile> on cmdline */ | |
8940 int force; /* when TRUE, ignore autocmd_busy */ | |
8941 buf_T *buf; /* buffer for <abuf> */ | |
8942 { | |
8943 return apply_autocmds_group(event, fname, fname_io, force, | |
8944 AUGROUP_ALL, buf, NULL); | |
8945 } | |
8946 | |
8947 /* | |
8948 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
8949 * setting v:filearg. | |
8950 */ | |
8951 static int | |
8952 apply_autocmds_exarg(event, fname, fname_io, force, buf, eap) | |
661 | 8953 event_T event; |
7 | 8954 char_u *fname; |
8955 char_u *fname_io; | |
8956 int force; | |
8957 buf_T *buf; | |
8958 exarg_T *eap; | |
8959 { | |
8960 return apply_autocmds_group(event, fname, fname_io, force, | |
8961 AUGROUP_ALL, buf, eap); | |
8962 } | |
8963 | |
8964 /* | |
8965 * Like apply_autocmds(), but handles the caller's retval. If the script | |
8966 * processing is being aborted or if retval is FAIL when inside a try | |
8967 * conditional, no autocommands are executed. If otherwise the autocommands | |
8968 * cause the script to be aborted, retval is set to FAIL. | |
8969 */ | |
8970 int | |
8971 apply_autocmds_retval(event, fname, fname_io, force, buf, retval) | |
661 | 8972 event_T event; |
7 | 8973 char_u *fname; /* NULL or empty means use actual file name */ |
8974 char_u *fname_io; /* fname to use for <afile> on cmdline */ | |
8975 int force; /* when TRUE, ignore autocmd_busy */ | |
8976 buf_T *buf; /* buffer for <abuf> */ | |
8977 int *retval; /* pointer to caller's retval */ | |
8978 { | |
8979 int did_cmd; | |
8980 | |
532 | 8981 #ifdef FEAT_EVAL |
7 | 8982 if (should_abort(*retval)) |
8983 return FALSE; | |
532 | 8984 #endif |
7 | 8985 |
8986 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
8987 AUGROUP_ALL, buf, NULL); | |
532 | 8988 if (did_cmd |
8989 #ifdef FEAT_EVAL | |
8990 && aborting() | |
8991 #endif | |
8992 ) | |
7 | 8993 *retval = FAIL; |
8994 return did_cmd; | |
8995 } | |
8996 | |
609 | 8997 /* |
8998 * Return TRUE when there is a CursorHold autocommand defined. | |
8999 */ | |
7 | 9000 int |
9001 has_cursorhold() | |
9002 { | |
661 | 9003 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9004 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9005 } |
609 | 9006 |
9007 /* | |
9008 * Return TRUE if the CursorHold event can be triggered. | |
9009 */ | |
9010 int | |
9011 trigger_cursorhold() | |
9012 { | |
661 | 9013 int state; |
9014 | |
978 | 9015 if (!did_cursorhold && has_cursorhold() && !Recording |
9016 #ifdef FEAT_INS_EXPAND | |
9017 && !ins_compl_active() | |
9018 #endif | |
9019 ) | |
661 | 9020 { |
9021 state = get_real_state(); | |
9022 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9023 return TRUE; | |
9024 } | |
9025 return FALSE; | |
609 | 9026 } |
661 | 9027 |
9028 /* | |
9029 * Return TRUE when there is a CursorMoved autocommand defined. | |
9030 */ | |
9031 int | |
9032 has_cursormoved() | |
9033 { | |
9034 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9035 } | |
9036 | |
9037 /* | |
9038 * Return TRUE when there is a CursorMovedI autocommand defined. | |
9039 */ | |
9040 int | |
9041 has_cursormovedI() | |
9042 { | |
9043 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9044 } | |
7 | 9045 |
9046 static int | |
9047 apply_autocmds_group(event, fname, fname_io, force, group, buf, eap) | |
661 | 9048 event_T event; |
7 | 9049 char_u *fname; /* NULL or empty means use actual file name */ |
9050 char_u *fname_io; /* fname to use for <afile> on cmdline, NULL means | |
9051 use fname */ | |
9052 int force; /* when TRUE, ignore autocmd_busy */ | |
9053 int group; /* group ID, or AUGROUP_ALL */ | |
9054 buf_T *buf; /* buffer for <abuf> */ | |
9055 exarg_T *eap; /* command arguments */ | |
9056 { | |
9057 char_u *sfname = NULL; /* short file name */ | |
9058 char_u *tail; | |
9059 int save_changed; | |
9060 buf_T *old_curbuf; | |
9061 int retval = FALSE; | |
9062 char_u *save_sourcing_name; | |
9063 linenr_T save_sourcing_lnum; | |
9064 char_u *save_autocmd_fname; | |
1723 | 9065 int save_autocmd_fname_full; |
7 | 9066 int save_autocmd_bufnr; |
9067 char_u *save_autocmd_match; | |
9068 int save_autocmd_busy; | |
9069 int save_autocmd_nested; | |
9070 static int nesting = 0; | |
9071 AutoPatCmd patcmd; | |
9072 AutoPat *ap; | |
9073 #ifdef FEAT_EVAL | |
9074 scid_T save_current_SID; | |
9075 void *save_funccalp; | |
9076 char_u *save_cmdarg; | |
9077 long save_cmdbang; | |
9078 #endif | |
9079 static int filechangeshell_busy = FALSE; | |
170 | 9080 #ifdef FEAT_PROFILE |
9081 proftime_T wait_time; | |
9082 #endif | |
7 | 9083 |
9084 /* | |
9085 * Quickly return if there are no autocommands for this event or | |
9086 * autocommands are blocked. | |
9087 */ | |
1410 | 9088 if (first_autopat[(int)event] == NULL || autocmd_blocked > 0) |
40 | 9089 goto BYPASS_AU; |
7 | 9090 |
9091 /* | |
9092 * When autocommands are busy, new autocommands are only executed when | |
9093 * explicitly enabled with the "nested" flag. | |
9094 */ | |
9095 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9096 goto BYPASS_AU; |
7 | 9097 |
9098 #ifdef FEAT_EVAL | |
9099 /* | |
1201 | 9100 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9101 * occurred or an exception was thrown but not caught. |
9102 */ | |
9103 if (aborting()) | |
40 | 9104 goto BYPASS_AU; |
7 | 9105 #endif |
9106 | |
9107 /* | |
9108 * FileChangedShell never nests, because it can create an endless loop. | |
9109 */ | |
765 | 9110 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9111 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9112 goto BYPASS_AU; |
7 | 9113 |
9114 /* | |
9115 * Ignore events in 'eventignore'. | |
9116 */ | |
9117 if (event_ignored(event)) | |
40 | 9118 goto BYPASS_AU; |
7 | 9119 |
9120 /* | |
9121 * Allow nesting of autocommands, but restrict the depth, because it's | |
9122 * possible to create an endless loop. | |
9123 */ | |
9124 if (nesting == 10) | |
9125 { | |
9126 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9127 goto BYPASS_AU; |
7 | 9128 } |
9129 | |
9130 /* | |
9131 * Check if these autocommands are disabled. Used when doing ":all" or | |
9132 * ":ball". | |
9133 */ | |
9134 if ( (autocmd_no_enter | |
9135 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9136 || (autocmd_no_leave | |
9137 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9138 goto BYPASS_AU; |
7 | 9139 |
9140 /* | |
9141 * Save the autocmd_* variables and info about the current buffer. | |
9142 */ | |
9143 save_autocmd_fname = autocmd_fname; | |
1723 | 9144 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9145 save_autocmd_bufnr = autocmd_bufnr; |
9146 save_autocmd_match = autocmd_match; | |
9147 save_autocmd_busy = autocmd_busy; | |
9148 save_autocmd_nested = autocmd_nested; | |
9149 save_changed = curbuf->b_changed; | |
9150 old_curbuf = curbuf; | |
9151 | |
9152 /* | |
9153 * Set the file name to be used for <afile>. | |
1471 | 9154 * Make a copy to avoid that changing a buffer name or directory makes it |
9155 * invalid. | |
7 | 9156 */ |
9157 if (fname_io == NULL) | |
9158 { | |
9159 if (fname != NULL && *fname != NUL) | |
9160 autocmd_fname = fname; | |
9161 else if (buf != NULL) | |
1723 | 9162 autocmd_fname = buf->b_ffname; |
7 | 9163 else |
9164 autocmd_fname = NULL; | |
9165 } | |
9166 else | |
9167 autocmd_fname = fname_io; | |
1471 | 9168 if (autocmd_fname != NULL) |
1723 | 9169 autocmd_fname = vim_strsave(autocmd_fname); |
9170 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9171 |
9172 /* | |
9173 * Set the buffer number to be used for <abuf>. | |
9174 */ | |
9175 if (buf == NULL) | |
9176 autocmd_bufnr = 0; | |
9177 else | |
9178 autocmd_bufnr = buf->b_fnum; | |
9179 | |
9180 /* | |
9181 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9182 * Always use the full path of the file name to match with, in case | |
9183 * "allow_dirs" is set. | |
9184 */ | |
9185 if (fname == NULL || *fname == NUL) | |
9186 { | |
9187 if (buf == NULL) | |
9188 fname = NULL; | |
9189 else | |
9190 { | |
9191 #ifdef FEAT_SYN_HL | |
9192 if (event == EVENT_SYNTAX) | |
9193 fname = buf->b_p_syn; | |
9194 else | |
9195 #endif | |
9196 if (event == EVENT_FILETYPE) | |
9197 fname = buf->b_p_ft; | |
9198 else | |
9199 { | |
9200 if (buf->b_sfname != NULL) | |
9201 sfname = vim_strsave(buf->b_sfname); | |
9202 fname = buf->b_ffname; | |
9203 } | |
9204 } | |
9205 if (fname == NULL) | |
9206 fname = (char_u *)""; | |
9207 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9208 } | |
9209 else | |
9210 { | |
9211 sfname = vim_strsave(fname); | |
1867 | 9212 /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID or |
9213 * QuickFixCmd* */ | |
161 | 9214 if (event == EVENT_FILETYPE |
9215 || event == EVENT_SYNTAX | |
1867 | 9216 || event == EVENT_FUNCUNDEFINED |
161 | 9217 || event == EVENT_REMOTEREPLY |
649 | 9218 || event == EVENT_SPELLFILEMISSING |
161 | 9219 || event == EVENT_QUICKFIXCMDPRE |
9220 || event == EVENT_QUICKFIXCMDPOST) | |
7 | 9221 fname = vim_strsave(fname); |
9222 else | |
9223 fname = FullName_save(fname, FALSE); | |
9224 } | |
9225 if (fname == NULL) /* out of memory */ | |
9226 { | |
9227 vim_free(sfname); | |
40 | 9228 retval = FALSE; |
9229 goto BYPASS_AU; | |
7 | 9230 } |
9231 | |
9232 #ifdef BACKSLASH_IN_FILENAME | |
9233 /* | |
9234 * Replace all backslashes with forward slashes. This makes the | |
9235 * autocommand patterns portable between Unix and MS-DOS. | |
9236 */ | |
9237 if (sfname != NULL) | |
9238 forward_slash(sfname); | |
9239 forward_slash(fname); | |
9240 #endif | |
9241 | |
9242 #ifdef VMS | |
9243 /* remove version for correct match */ | |
9244 if (sfname != NULL) | |
9245 vms_remove_version(sfname); | |
9246 vms_remove_version(fname); | |
9247 #endif | |
9248 | |
9249 /* | |
9250 * Set the name to be used for <amatch>. | |
9251 */ | |
9252 autocmd_match = fname; | |
9253 | |
9254 | |
9255 /* Don't redraw while doing auto commands. */ | |
9256 ++RedrawingDisabled; | |
9257 save_sourcing_name = sourcing_name; | |
9258 sourcing_name = NULL; /* don't free this one */ | |
9259 save_sourcing_lnum = sourcing_lnum; | |
9260 sourcing_lnum = 0; /* no line number here */ | |
9261 | |
9262 #ifdef FEAT_EVAL | |
9263 save_current_SID = current_SID; | |
9264 | |
170 | 9265 # ifdef FEAT_PROFILE |
788 | 9266 if (do_profiling == PROF_YES) |
170 | 9267 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9268 # endif | |
9269 | |
7 | 9270 /* Don't use local function variables, if called from a function */ |
9271 save_funccalp = save_funccal(); | |
9272 #endif | |
9273 | |
9274 /* | |
9275 * When starting to execute autocommands, save the search patterns. | |
9276 */ | |
9277 if (!autocmd_busy) | |
9278 { | |
9279 save_search_patterns(); | |
9280 saveRedobuff(); | |
9281 did_filetype = keep_filetype; | |
9282 } | |
9283 | |
9284 /* | |
9285 * Note that we are applying autocmds. Some commands need to know. | |
9286 */ | |
9287 autocmd_busy = TRUE; | |
9288 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9289 ++nesting; /* see matching decrement below */ | |
9290 | |
9291 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9292 if (event == EVENT_FILETYPE) | |
9293 did_filetype = TRUE; | |
9294 | |
9295 tail = gettail(fname); | |
9296 | |
9297 /* Find first autocommand that matches */ | |
9298 patcmd.curpat = first_autopat[(int)event]; | |
9299 patcmd.nextcmd = NULL; | |
9300 patcmd.group = group; | |
9301 patcmd.fname = fname; | |
9302 patcmd.sfname = sfname; | |
9303 patcmd.tail = tail; | |
9304 patcmd.event = event; | |
40 | 9305 patcmd.arg_bufnr = autocmd_bufnr; |
9306 patcmd.next = NULL; | |
7 | 9307 auto_next_pat(&patcmd, FALSE); |
9308 | |
9309 /* found one, start executing the autocommands */ | |
9310 if (patcmd.curpat != NULL) | |
9311 { | |
40 | 9312 /* add to active_apc_list */ |
9313 patcmd.next = active_apc_list; | |
9314 active_apc_list = &patcmd; | |
9315 | |
7 | 9316 #ifdef FEAT_EVAL |
9317 /* set v:cmdarg (only when there is a matching pattern) */ | |
9318 save_cmdbang = get_vim_var_nr(VV_CMDBANG); | |
9319 if (eap != NULL) | |
9320 { | |
9321 save_cmdarg = set_cmdarg(eap, NULL); | |
9322 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9323 } | |
9324 else | |
9325 save_cmdarg = NULL; /* avoid gcc warning */ | |
9326 #endif | |
9327 retval = TRUE; | |
9328 /* mark the last pattern, to avoid an endless loop when more patterns | |
9329 * are added when executing autocommands */ | |
9330 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9331 ap->last = FALSE; | |
9332 ap->last = TRUE; | |
9333 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9334 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9335 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9336 #ifdef FEAT_EVAL | |
9337 if (eap != NULL) | |
9338 { | |
9339 (void)set_cmdarg(NULL, save_cmdarg); | |
9340 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9341 } | |
9342 #endif | |
40 | 9343 /* delete from active_apc_list */ |
9344 if (active_apc_list == &patcmd) /* just in case */ | |
9345 active_apc_list = patcmd.next; | |
7 | 9346 } |
9347 | |
9348 --RedrawingDisabled; | |
9349 autocmd_busy = save_autocmd_busy; | |
9350 filechangeshell_busy = FALSE; | |
9351 autocmd_nested = save_autocmd_nested; | |
9352 vim_free(sourcing_name); | |
9353 sourcing_name = save_sourcing_name; | |
9354 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9355 vim_free(autocmd_fname); |
7 | 9356 autocmd_fname = save_autocmd_fname; |
1723 | 9357 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9358 autocmd_bufnr = save_autocmd_bufnr; |
9359 autocmd_match = save_autocmd_match; | |
9360 #ifdef FEAT_EVAL | |
9361 current_SID = save_current_SID; | |
9362 restore_funccal(save_funccalp); | |
170 | 9363 # ifdef FEAT_PROFILE |
788 | 9364 if (do_profiling == PROF_YES) |
170 | 9365 prof_child_exit(&wait_time); |
9366 # endif | |
7 | 9367 #endif |
9368 vim_free(fname); | |
9369 vim_free(sfname); | |
9370 --nesting; /* see matching increment above */ | |
9371 | |
9372 /* | |
9373 * When stopping to execute autocommands, restore the search patterns and | |
9374 * the redo buffer. | |
9375 */ | |
9376 if (!autocmd_busy) | |
9377 { | |
9378 restore_search_patterns(); | |
9379 restoreRedobuff(); | |
9380 did_filetype = FALSE; | |
9381 } | |
9382 | |
9383 /* | |
9384 * Some events don't set or reset the Changed flag. | |
9385 * Check if still in the same buffer! | |
9386 */ | |
9387 if (curbuf == old_curbuf | |
9388 && (event == EVENT_BUFREADPOST | |
9389 || event == EVENT_BUFWRITEPOST | |
9390 || event == EVENT_FILEAPPENDPOST | |
9391 || event == EVENT_VIMLEAVE | |
9392 || event == EVENT_VIMLEAVEPRE)) | |
9393 { | |
9394 #ifdef FEAT_TITLE | |
9395 if (curbuf->b_changed != save_changed) | |
9396 need_maketitle = TRUE; | |
9397 #endif | |
9398 curbuf->b_changed = save_changed; | |
9399 } | |
9400 | |
9401 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9402 |
9403 BYPASS_AU: | |
9404 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9405 * are deleted. */ | |
9406 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9407 aubuflocal_remove(buf); | |
9408 | |
7 | 9409 return retval; |
9410 } | |
9411 | |
1410 | 9412 # ifdef FEAT_EVAL |
9413 static char_u *old_termresponse = NULL; | |
9414 # endif | |
9415 | |
9416 /* | |
9417 * Block triggering autocommands until unblock_autocmd() is called. | |
9418 * Can be used recursively, so long as it's symmetric. | |
9419 */ | |
9420 void | |
9421 block_autocmds() | |
9422 { | |
9423 # ifdef FEAT_EVAL | |
9424 /* Remember the value of v:termresponse. */ | |
9425 if (autocmd_blocked == 0) | |
9426 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9427 # endif | |
9428 ++autocmd_blocked; | |
9429 } | |
9430 | |
9431 void | |
9432 unblock_autocmds() | |
9433 { | |
9434 --autocmd_blocked; | |
9435 | |
9436 # ifdef FEAT_EVAL | |
9437 /* When v:termresponse was set while autocommands were blocked, trigger | |
9438 * the autocommands now. Esp. useful when executing a shell command | |
9439 * during startup (vimdiff). */ | |
9440 if (autocmd_blocked == 0 | |
9441 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9442 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9443 # endif | |
9444 } | |
9445 | |
7 | 9446 /* |
9447 * Find next autocommand pattern that matches. | |
9448 */ | |
9449 static void | |
9450 auto_next_pat(apc, stop_at_last) | |
9451 AutoPatCmd *apc; | |
9452 int stop_at_last; /* stop when 'last' flag is set */ | |
9453 { | |
9454 AutoPat *ap; | |
9455 AutoCmd *cp; | |
9456 char_u *name; | |
9457 char *s; | |
9458 | |
9459 vim_free(sourcing_name); | |
9460 sourcing_name = NULL; | |
9461 | |
9462 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9463 { | |
9464 apc->curpat = NULL; | |
9465 | |
1723 | 9466 /* Only use a pattern when it has not been removed, has commands and |
40 | 9467 * the group matches. For buffer-local autocommands only check the |
9468 * buffer number. */ | |
7 | 9469 if (ap->pat != NULL && ap->cmds != NULL |
9470 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9471 { | |
40 | 9472 /* execution-condition */ |
9473 if (ap->buflocal_nr == 0 | |
153 | 9474 ? (match_file_pat(NULL, ap->reg_prog, apc->fname, |
9475 apc->sfname, apc->tail, ap->allow_dirs)) | |
40 | 9476 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9477 { |
9478 name = event_nr2name(apc->event); | |
9479 s = _("%s Auto commands for \"%s\""); | |
9480 sourcing_name = alloc((unsigned)(STRLEN(s) | |
9481 + STRLEN(name) + ap->patlen + 1)); | |
9482 if (sourcing_name != NULL) | |
9483 { | |
9484 sprintf((char *)sourcing_name, s, | |
9485 (char *)name, (char *)ap->pat); | |
9486 if (p_verbose >= 8) | |
292 | 9487 { |
9488 verbose_enter(); | |
273 | 9489 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9490 verbose_leave(); |
9491 } | |
7 | 9492 } |
9493 | |
9494 apc->curpat = ap; | |
9495 apc->nextcmd = ap->cmds; | |
9496 /* mark last command */ | |
9497 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9498 cp->last = FALSE; | |
9499 cp->last = TRUE; | |
9500 } | |
9501 line_breakcheck(); | |
9502 if (apc->curpat != NULL) /* found a match */ | |
9503 break; | |
9504 } | |
9505 if (stop_at_last && ap->last) | |
9506 break; | |
9507 } | |
9508 } | |
9509 | |
9510 /* | |
9511 * Get next autocommand command. | |
9512 * Called by do_cmdline() to get the next line for ":if". | |
9513 * Returns allocated string, or NULL for end of autocommands. | |
9514 */ | |
9515 static char_u * | |
9516 getnextac(c, cookie, indent) | |
1880 | 9517 int c UNUSED; |
7 | 9518 void *cookie; |
1880 | 9519 int indent UNUSED; |
7 | 9520 { |
9521 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9522 char_u *retval; | |
9523 AutoCmd *ac; | |
9524 | |
9525 /* Can be called again after returning the last line. */ | |
9526 if (acp->curpat == NULL) | |
9527 return NULL; | |
9528 | |
9529 /* repeat until we find an autocommand to execute */ | |
9530 for (;;) | |
9531 { | |
9532 /* skip removed commands */ | |
9533 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9534 if (acp->nextcmd->last) | |
9535 acp->nextcmd = NULL; | |
9536 else | |
9537 acp->nextcmd = acp->nextcmd->next; | |
9538 | |
9539 if (acp->nextcmd != NULL) | |
9540 break; | |
9541 | |
9542 /* at end of commands, find next pattern that matches */ | |
9543 if (acp->curpat->last) | |
9544 acp->curpat = NULL; | |
9545 else | |
9546 acp->curpat = acp->curpat->next; | |
9547 if (acp->curpat != NULL) | |
9548 auto_next_pat(acp, TRUE); | |
9549 if (acp->curpat == NULL) | |
9550 return NULL; | |
9551 } | |
9552 | |
9553 ac = acp->nextcmd; | |
9554 | |
9555 if (p_verbose >= 9) | |
9556 { | |
292 | 9557 verbose_enter_scroll(); |
273 | 9558 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9559 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9560 verbose_leave_scroll(); |
7 | 9561 } |
9562 retval = vim_strsave(ac->cmd); | |
9563 autocmd_nested = ac->nested; | |
9564 #ifdef FEAT_EVAL | |
9565 current_SID = ac->scriptID; | |
9566 #endif | |
9567 if (ac->last) | |
9568 acp->nextcmd = NULL; | |
9569 else | |
9570 acp->nextcmd = ac->next; | |
9571 return retval; | |
9572 } | |
9573 | |
9574 /* | |
9575 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9576 * To account for buffer-local autocommands, function needs to know |
9577 * in which buffer the file will be opened. | |
7 | 9578 */ |
9579 int | |
40 | 9580 has_autocmd(event, sfname, buf) |
661 | 9581 event_T event; |
7 | 9582 char_u *sfname; |
40 | 9583 buf_T *buf; |
7 | 9584 { |
9585 AutoPat *ap; | |
9586 char_u *fname; | |
9587 char_u *tail = gettail(sfname); | |
9588 int retval = FALSE; | |
9589 | |
9590 fname = FullName_save(sfname, FALSE); | |
9591 if (fname == NULL) | |
9592 return FALSE; | |
9593 | |
9594 #ifdef BACKSLASH_IN_FILENAME | |
9595 /* | |
9596 * Replace all backslashes with forward slashes. This makes the | |
9597 * autocommand patterns portable between Unix and MS-DOS. | |
9598 */ | |
9599 sfname = vim_strsave(sfname); | |
9600 if (sfname != NULL) | |
9601 forward_slash(sfname); | |
9602 forward_slash(fname); | |
9603 #endif | |
9604 | |
9605 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
9606 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 9607 && (ap->buflocal_nr == 0 |
153 | 9608 ? match_file_pat(NULL, ap->reg_prog, |
9609 fname, sfname, tail, ap->allow_dirs) | |
856 | 9610 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 9611 )) |
7 | 9612 { |
9613 retval = TRUE; | |
9614 break; | |
9615 } | |
9616 | |
9617 vim_free(fname); | |
9618 #ifdef BACKSLASH_IN_FILENAME | |
9619 vim_free(sfname); | |
9620 #endif | |
9621 | |
9622 return retval; | |
9623 } | |
9624 | |
9625 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
9626 /* | |
9627 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
9628 * names. | |
9629 */ | |
9630 char_u * | |
9631 get_augroup_name(xp, idx) | |
1880 | 9632 expand_T *xp UNUSED; |
7 | 9633 int idx; |
9634 { | |
9635 if (idx == augroups.ga_len) /* add "END" add the end */ | |
9636 return (char_u *)"END"; | |
9637 if (idx >= augroups.ga_len) /* end of list */ | |
9638 return NULL; | |
9639 if (AUGROUP_NAME(idx) == NULL) /* skip deleted entries */ | |
9640 return (char_u *)""; | |
9641 return AUGROUP_NAME(idx); /* return a name */ | |
9642 } | |
9643 | |
9644 static int include_groups = FALSE; | |
9645 | |
9646 char_u * | |
9647 set_context_in_autocmd(xp, arg, doautocmd) | |
9648 expand_T *xp; | |
9649 char_u *arg; | |
1733 | 9650 int doautocmd; /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 9651 { |
9652 char_u *p; | |
9653 int group; | |
9654 | |
9655 /* check for a group name, skip it if present */ | |
9656 include_groups = FALSE; | |
9657 p = arg; | |
9658 group = au_get_grouparg(&arg); | |
9659 if (group == AUGROUP_ERROR) | |
9660 return NULL; | |
9661 /* If there only is a group name that's what we expand. */ | |
9662 if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1])) | |
9663 { | |
9664 arg = p; | |
9665 group = AUGROUP_ALL; | |
9666 } | |
9667 | |
9668 /* skip over event name */ | |
9669 for (p = arg; *p != NUL && !vim_iswhite(*p); ++p) | |
9670 if (*p == ',') | |
9671 arg = p + 1; | |
9672 if (*p == NUL) | |
9673 { | |
9674 if (group == AUGROUP_ALL) | |
9675 include_groups = TRUE; | |
9676 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
9677 xp->xp_pattern = arg; | |
9678 return NULL; | |
9679 } | |
9680 | |
9681 /* skip over pattern */ | |
9682 arg = skipwhite(p); | |
9683 while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\')) | |
9684 arg++; | |
9685 if (*arg) | |
9686 return arg; /* expand (next) command */ | |
9687 | |
9688 if (doautocmd) | |
9689 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
9690 else | |
9691 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
9692 return NULL; | |
9693 } | |
9694 | |
9695 /* | |
9696 * Function given to ExpandGeneric() to obtain the list of event names. | |
9697 */ | |
9698 char_u * | |
9699 get_event_name(xp, idx) | |
1880 | 9700 expand_T *xp UNUSED; |
7 | 9701 int idx; |
9702 { | |
9703 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
9704 { | |
9705 if (!include_groups || AUGROUP_NAME(idx) == NULL) | |
9706 return (char_u *)""; /* skip deleted entries */ | |
9707 return AUGROUP_NAME(idx); /* return a name */ | |
9708 } | |
9709 return (char_u *)event_names[idx - augroups.ga_len].name; | |
9710 } | |
9711 | |
9712 #endif /* FEAT_CMDL_COMPL */ | |
9713 | |
9714 /* | |
615 | 9715 * Return TRUE if autocmd is supported. |
9716 */ | |
9717 int | |
9718 autocmd_supported(name) | |
9719 char_u *name; | |
9720 { | |
9721 char_u *p; | |
9722 | |
9723 return (event_name2nr(name, &p) != NUM_EVENTS); | |
9724 } | |
9725 | |
9726 /* | |
612 | 9727 * Return TRUE if an autocommand is defined for a group, event and |
9728 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
9729 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
9730 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
9731 * Used for: | |
9732 * exists("#Group") or | |
9733 * exists("#Group#Event") or | |
9734 * exists("#Group#Event#pat") or | |
9735 * exists("#Event") or | |
9736 * exists("#Event#pat") | |
7 | 9737 */ |
9738 int | |
612 | 9739 au_exists(arg) |
9740 char_u *arg; | |
7 | 9741 { |
612 | 9742 char_u *arg_save; |
9743 char_u *pattern = NULL; | |
7 | 9744 char_u *event_name; |
9745 char_u *p; | |
661 | 9746 event_T event; |
7 | 9747 AutoPat *ap; |
40 | 9748 buf_T *buflocal_buf = NULL; |
612 | 9749 int group; |
9750 int retval = FALSE; | |
9751 | |
615 | 9752 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 9753 arg_save = vim_strsave(arg); |
9754 if (arg_save == NULL) | |
9755 return FALSE; | |
615 | 9756 p = vim_strchr(arg_save, '#'); |
612 | 9757 if (p != NULL) |
9758 *p++ = NUL; | |
9759 | |
9760 /* First, look for an autocmd group name */ | |
9761 group = au_find_group(arg_save); | |
9762 if (group == AUGROUP_ERROR) | |
9763 { | |
9764 /* Didn't match a group name, assume the first argument is an event. */ | |
9765 group = AUGROUP_ALL; | |
9766 event_name = arg_save; | |
9767 } | |
9768 else | |
9769 { | |
9770 if (p == NULL) | |
9771 { | |
9772 /* "Group": group name is present and it's recognized */ | |
9773 retval = TRUE; | |
9774 goto theend; | |
9775 } | |
9776 | |
9777 /* Must be "Group#Event" or "Group#Event#pat". */ | |
9778 event_name = p; | |
9779 p = vim_strchr(event_name, '#'); | |
9780 if (p != NULL) | |
9781 *p++ = NUL; /* "Group#Event#pat" */ | |
9782 } | |
9783 | |
9784 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 9785 |
9786 /* find the index (enum) for the event name */ | |
9787 event = event_name2nr(event_name, &p); | |
9788 | |
9789 /* return FALSE if the event name is not recognized */ | |
612 | 9790 if (event == NUM_EVENTS) |
9791 goto theend; | |
7 | 9792 |
9793 /* Find the first autocommand for this event. | |
9794 * If there isn't any, return FALSE; | |
9795 * If there is one and no pattern given, return TRUE; */ | |
9796 ap = first_autopat[(int)event]; | |
9797 if (ap == NULL) | |
612 | 9798 goto theend; |
7 | 9799 |
40 | 9800 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
9801 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 9802 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 9803 buflocal_buf = curbuf; |
9804 | |
7 | 9805 /* Check if there is an autocommand with the given pattern. */ |
9806 for ( ; ap != NULL; ap = ap->next) | |
40 | 9807 /* only use a pattern when it has not been removed and has commands. */ |
9808 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 9809 if (ap->pat != NULL && ap->cmds != NULL |
612 | 9810 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 9811 && (pattern == NULL |
9812 || (buflocal_buf == NULL | |
9813 ? fnamecmp(ap->pat, pattern) == 0 | |
9814 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 9815 { |
9816 retval = TRUE; | |
9817 break; | |
9818 } | |
9819 | |
9820 theend: | |
9821 vim_free(arg_save); | |
9822 return retval; | |
7 | 9823 } |
40 | 9824 |
934 | 9825 #else /* FEAT_AUTOCMD */ |
9826 | |
9827 /* | |
9828 * Prepare for executing commands for (hidden) buffer "buf". | |
9829 * This is the non-autocommand version, it simply saves "curbuf" and sets | |
9830 * "curbuf" and "curwin" to match "buf". | |
9831 */ | |
9832 void | |
9833 aucmd_prepbuf(aco, buf) | |
9834 aco_save_T *aco; /* structure to save values in */ | |
9835 buf_T *buf; /* new curbuf */ | |
9836 { | |
1906 | 9837 aco->save_curbuf = curbuf; |
9838 --curbuf->b_nwindows; | |
934 | 9839 curbuf = buf; |
9840 curwin->w_buffer = buf; | |
1906 | 9841 ++curbuf->b_nwindows; |
934 | 9842 } |
9843 | |
9844 /* | |
9845 * Restore after executing commands for a (hidden) buffer. | |
9846 * This is the non-autocommand version. | |
9847 */ | |
9848 void | |
9849 aucmd_restbuf(aco) | |
9850 aco_save_T *aco; /* structure holding saved values */ | |
9851 { | |
1906 | 9852 --curbuf->b_nwindows; |
9853 curbuf = aco->save_curbuf; | |
934 | 9854 curwin->w_buffer = curbuf; |
1906 | 9855 ++curbuf->b_nwindows; |
934 | 9856 } |
9857 | |
7 | 9858 #endif /* FEAT_AUTOCMD */ |
9859 | |
934 | 9860 |
7 | 9861 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
9862 /* | |
153 | 9863 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
9864 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
9865 * vim_regcomp() often. | |
7 | 9866 * Used for autocommands and 'wildignore'. |
9867 * Returns TRUE if there is a match, FALSE otherwise. | |
9868 */ | |
9869 int | |
153 | 9870 match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs) |
7 | 9871 char_u *pattern; /* pattern to match with */ |
153 | 9872 regprog_T *prog; /* pre-compiled regprog or NULL */ |
7 | 9873 char_u *fname; /* full path of file name */ |
9874 char_u *sfname; /* short file name or NULL */ | |
9875 char_u *tail; /* tail of path */ | |
9876 int allow_dirs; /* allow matching with dir */ | |
9877 { | |
9878 regmatch_T regmatch; | |
9879 int result = FALSE; | |
9880 #ifdef FEAT_OSFILETYPE | |
9881 int no_pattern = FALSE; /* TRUE if check is filetype only */ | |
9882 char_u *type_start; | |
9883 char_u c; | |
9884 int match = FALSE; | |
9885 #endif | |
9886 | |
9887 #ifdef CASE_INSENSITIVE_FILENAME | |
9888 regmatch.rm_ic = TRUE; /* Always ignore case */ | |
9889 #else | |
9890 regmatch.rm_ic = FALSE; /* Don't ever ignore case */ | |
9891 #endif | |
9892 #ifdef FEAT_OSFILETYPE | |
9893 if (*pattern == '<') | |
9894 { | |
9895 /* There is a filetype condition specified with this pattern. | |
9896 * Check the filetype matches first. If not, don't bother with the | |
9897 * pattern (set regprog to NULL). | |
9898 * Always use magic for the regexp. | |
9899 */ | |
9900 | |
9901 for (type_start = pattern + 1; (c = *pattern); pattern++) | |
9902 { | |
9903 if ((c == ';' || c == '>') && match == FALSE) | |
9904 { | |
9905 *pattern = NUL; /* Terminate the string */ | |
9906 match = mch_check_filetype(fname, type_start); | |
9907 *pattern = c; /* Restore the terminator */ | |
9908 type_start = pattern + 1; | |
9909 } | |
9910 if (c == '>') | |
9911 break; | |
9912 } | |
9913 | |
9914 /* (c should never be NUL, but check anyway) */ | |
9915 if (match == FALSE || c == NUL) | |
9916 regmatch.regprog = NULL; /* Doesn't match - don't check pat. */ | |
9917 else if (*pattern == NUL) | |
9918 { | |
9919 regmatch.regprog = NULL; /* Vim will try to free regprog later */ | |
9920 no_pattern = TRUE; /* Always matches - don't check pat. */ | |
9921 } | |
9922 else | |
9923 regmatch.regprog = vim_regcomp(pattern + 1, RE_MAGIC); | |
9924 } | |
9925 else | |
9926 #endif | |
153 | 9927 { |
9928 if (prog != NULL) | |
9929 regmatch.regprog = prog; | |
9930 else | |
9931 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); | |
9932 } | |
7 | 9933 |
9934 /* | |
9935 * Try for a match with the pattern with: | |
9936 * 1. the full file name, when the pattern has a '/'. | |
9937 * 2. the short file name, when the pattern has a '/'. | |
9938 * 3. the tail of the file name, when the pattern has no '/'. | |
9939 */ | |
9940 if ( | |
9941 #ifdef FEAT_OSFILETYPE | |
9942 /* If the check is for a filetype only and we don't care | |
9943 * about the path then skip all the regexp stuff. | |
9944 */ | |
9945 no_pattern || | |
9946 #endif | |
9947 (regmatch.regprog != NULL | |
9948 && ((allow_dirs | |
9949 && (vim_regexec(®match, fname, (colnr_T)0) | |
9950 || (sfname != NULL | |
9951 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
9952 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0))))) | |
9953 result = TRUE; | |
9954 | |
153 | 9955 if (prog == NULL) |
9956 vim_free(regmatch.regprog); | |
7 | 9957 return result; |
9958 } | |
9959 #endif | |
9960 | |
9961 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
9962 /* | |
9963 * Return TRUE if a file matches with a pattern in "list". | |
9964 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
9965 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
9966 */ | |
9967 int | |
9968 match_file_list(list, sfname, ffname) | |
9969 char_u *list; | |
9970 char_u *sfname; | |
9971 char_u *ffname; | |
9972 { | |
9973 char_u buf[100]; | |
9974 char_u *tail; | |
9975 char_u *regpat; | |
9976 char allow_dirs; | |
9977 int match; | |
9978 char_u *p; | |
9979 | |
9980 tail = gettail(sfname); | |
9981 | |
9982 /* try all patterns in 'wildignore' */ | |
9983 p = list; | |
9984 while (*p) | |
9985 { | |
9986 copy_option_part(&p, buf, 100, ","); | |
9987 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
9988 if (regpat == NULL) | |
9989 break; | |
153 | 9990 match = match_file_pat(regpat, NULL, ffname, sfname, |
9991 tail, (int)allow_dirs); | |
7 | 9992 vim_free(regpat); |
9993 if (match) | |
9994 return TRUE; | |
9995 } | |
9996 return FALSE; | |
9997 } | |
9998 #endif | |
9999 | |
10000 /* | |
10001 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10002 * a regular expression, and return the result in allocated memory. If there | |
10003 * is a directory path separator to be matched, then TRUE is put in | |
10004 * allow_dirs, otherwise FALSE is put there -- webb. | |
10005 * Handle backslashes before special characters, like "\*" and "\ ". | |
10006 * | |
10007 * If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg: | |
10008 * '<html>myfile' becomes '<html>^myfile$' -- leonard. | |
10009 * | |
10010 * Returns NULL when out of memory. | |
10011 */ | |
10012 char_u * | |
10013 file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash) | |
10014 char_u *pat; | |
10015 char_u *pat_end; /* first char after pattern or NULL */ | |
10016 char *allow_dirs; /* Result passed back out in here */ | |
1877 | 10017 int no_bslash UNUSED; /* Don't use a backward slash as pathsep */ |
7 | 10018 { |
10019 int size; | |
10020 char_u *endp; | |
10021 char_u *reg_pat; | |
10022 char_u *p; | |
10023 int i; | |
10024 int nested = 0; | |
10025 int add_dollar = TRUE; | |
10026 #ifdef FEAT_OSFILETYPE | |
10027 int check_length = 0; | |
10028 #endif | |
10029 | |
10030 if (allow_dirs != NULL) | |
10031 *allow_dirs = FALSE; | |
10032 if (pat_end == NULL) | |
10033 pat_end = pat + STRLEN(pat); | |
10034 | |
10035 #ifdef FEAT_OSFILETYPE | |
10036 /* Find out how much of the string is the filetype check */ | |
10037 if (*pat == '<') | |
10038 { | |
10039 /* Count chars until the next '>' */ | |
10040 for (p = pat + 1; p < pat_end && *p != '>'; p++) | |
10041 ; | |
10042 if (p < pat_end) | |
10043 { | |
10044 /* Pattern is of the form <.*>.* */ | |
10045 check_length = p - pat + 1; | |
10046 if (p + 1 >= pat_end) | |
10047 { | |
10048 /* The 'pattern' is a filetype check ONLY */ | |
10049 reg_pat = (char_u *)alloc(check_length + 1); | |
10050 if (reg_pat != NULL) | |
10051 { | |
10052 mch_memmove(reg_pat, pat, (size_t)check_length); | |
10053 reg_pat[check_length] = NUL; | |
10054 } | |
10055 return reg_pat; | |
10056 } | |
10057 } | |
10058 /* else: there was no closing '>' - assume it was a normal pattern */ | |
10059 | |
10060 } | |
10061 pat += check_length; | |
10062 size = 2 + check_length; | |
10063 #else | |
10064 size = 2; /* '^' at start, '$' at end */ | |
10065 #endif | |
10066 | |
10067 for (p = pat; p < pat_end; p++) | |
10068 { | |
10069 switch (*p) | |
10070 { | |
10071 case '*': | |
10072 case '.': | |
10073 case ',': | |
10074 case '{': | |
10075 case '}': | |
10076 case '~': | |
10077 size += 2; /* extra backslash */ | |
10078 break; | |
10079 #ifdef BACKSLASH_IN_FILENAME | |
10080 case '\\': | |
10081 case '/': | |
10082 size += 4; /* could become "[\/]" */ | |
10083 break; | |
10084 #endif | |
10085 default: | |
10086 size++; | |
10087 # ifdef FEAT_MBYTE | |
474 | 10088 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10089 { |
10090 ++p; | |
10091 ++size; | |
10092 } | |
10093 # endif | |
10094 break; | |
10095 } | |
10096 } | |
10097 reg_pat = alloc(size + 1); | |
10098 if (reg_pat == NULL) | |
10099 return NULL; | |
10100 | |
10101 #ifdef FEAT_OSFILETYPE | |
10102 /* Copy the type check in to the start. */ | |
10103 if (check_length) | |
10104 mch_memmove(reg_pat, pat - check_length, (size_t)check_length); | |
10105 i = check_length; | |
10106 #else | |
10107 i = 0; | |
10108 #endif | |
10109 | |
10110 if (pat[0] == '*') | |
10111 while (pat[0] == '*' && pat < pat_end - 1) | |
10112 pat++; | |
10113 else | |
10114 reg_pat[i++] = '^'; | |
10115 endp = pat_end - 1; | |
10116 if (*endp == '*') | |
10117 { | |
10118 while (endp - pat > 0 && *endp == '*') | |
10119 endp--; | |
10120 add_dollar = FALSE; | |
10121 } | |
10122 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10123 { | |
10124 switch (*p) | |
10125 { | |
10126 case '*': | |
10127 reg_pat[i++] = '.'; | |
10128 reg_pat[i++] = '*'; | |
444 | 10129 while (p[1] == '*') /* "**" matches like "*" */ |
10130 ++p; | |
7 | 10131 break; |
10132 case '.': | |
10133 #ifdef RISCOS | |
10134 if (allow_dirs != NULL) | |
10135 *allow_dirs = TRUE; | |
10136 /* FALLTHROUGH */ | |
10137 #endif | |
10138 case '~': | |
10139 reg_pat[i++] = '\\'; | |
10140 reg_pat[i++] = *p; | |
10141 break; | |
10142 case '?': | |
10143 #ifdef RISCOS | |
10144 case '#': | |
10145 #endif | |
10146 reg_pat[i++] = '.'; | |
10147 break; | |
10148 case '\\': | |
10149 if (p[1] == NUL) | |
10150 break; | |
10151 #ifdef BACKSLASH_IN_FILENAME | |
10152 if (!no_bslash) | |
10153 { | |
10154 /* translate: | |
10155 * "\x" to "\\x" e.g., "dir\file" | |
10156 * "\*" to "\\.*" e.g., "dir\*.c" | |
10157 * "\?" to "\\." e.g., "dir\??.c" | |
10158 * "\+" to "\+" e.g., "fileX\+.c" | |
10159 */ | |
10160 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10161 && p[1] != '+') | |
10162 { | |
10163 reg_pat[i++] = '['; | |
10164 reg_pat[i++] = '\\'; | |
10165 reg_pat[i++] = '/'; | |
10166 reg_pat[i++] = ']'; | |
10167 if (allow_dirs != NULL) | |
10168 *allow_dirs = TRUE; | |
10169 break; | |
10170 } | |
10171 } | |
10172 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10173 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10174 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10175 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10176 * foo\,bar -> foo,bar |
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 * 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
|
10179 * regexp. */ |
7 | 10180 if (*++p == '?' |
10181 #ifdef BACKSLASH_IN_FILENAME | |
10182 && no_bslash | |
10183 #endif | |
10184 ) | |
10185 reg_pat[i++] = '?'; | |
10186 else | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10187 if (*p == ',' || *p == '%' || *p == '#' || *p == ' ') |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10188 reg_pat[i++] = *p; |
7 | 10189 else |
10190 { | |
10191 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10192 #ifdef BACKSLASH_IN_FILENAME | |
10193 && (!no_bslash || *p != '\\') | |
10194 #endif | |
10195 ) | |
10196 *allow_dirs = TRUE; | |
10197 reg_pat[i++] = '\\'; | |
10198 reg_pat[i++] = *p; | |
10199 } | |
10200 break; | |
10201 #ifdef BACKSLASH_IN_FILENAME | |
10202 case '/': | |
10203 reg_pat[i++] = '['; | |
10204 reg_pat[i++] = '\\'; | |
10205 reg_pat[i++] = '/'; | |
10206 reg_pat[i++] = ']'; | |
10207 if (allow_dirs != NULL) | |
10208 *allow_dirs = TRUE; | |
10209 break; | |
10210 #endif | |
10211 case '{': | |
10212 reg_pat[i++] = '\\'; | |
10213 reg_pat[i++] = '('; | |
10214 nested++; | |
10215 break; | |
10216 case '}': | |
10217 reg_pat[i++] = '\\'; | |
10218 reg_pat[i++] = ')'; | |
10219 --nested; | |
10220 break; | |
10221 case ',': | |
10222 if (nested) | |
10223 { | |
10224 reg_pat[i++] = '\\'; | |
10225 reg_pat[i++] = '|'; | |
10226 } | |
10227 else | |
10228 reg_pat[i++] = ','; | |
10229 break; | |
10230 default: | |
10231 # ifdef FEAT_MBYTE | |
474 | 10232 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10233 reg_pat[i++] = *p++; |
10234 else | |
10235 # endif | |
10236 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10237 *allow_dirs = TRUE; | |
10238 reg_pat[i++] = *p; | |
10239 break; | |
10240 } | |
10241 } | |
10242 if (add_dollar) | |
10243 reg_pat[i++] = '$'; | |
10244 reg_pat[i] = NUL; | |
10245 if (nested != 0) | |
10246 { | |
10247 if (nested < 0) | |
10248 EMSG(_("E219: Missing {.")); | |
10249 else | |
10250 EMSG(_("E220: Missing }.")); | |
10251 vim_free(reg_pat); | |
10252 reg_pat = NULL; | |
10253 } | |
10254 return reg_pat; | |
10255 } |