Mercurial > vim
annotate src/fileio.c @ 2523:0dffdd8f8871 vim73
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
behind.
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Sun, 08 Aug 2010 18:46:06 +0200 |
parents | 88751831fa0a |
children | 5769dc787ec5 |
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 | |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
66 static int crypt_method_from_magic __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) | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
222 * READ_KEEP_UNDO don't clear undo info or read it from a file |
7 | 223 * |
224 * return FAIL for failure, OK otherwise | |
225 */ | |
226 int | |
227 readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags) | |
228 char_u *fname; | |
229 char_u *sfname; | |
230 linenr_T from; | |
231 linenr_T lines_to_skip; | |
232 linenr_T lines_to_read; | |
233 exarg_T *eap; /* can be NULL! */ | |
234 int flags; | |
235 { | |
236 int fd = 0; | |
237 int newfile = (flags & READ_NEW); | |
238 int check_readonly; | |
239 int filtering = (flags & READ_FILTER); | |
240 int read_stdin = (flags & READ_STDIN); | |
241 int read_buffer = (flags & READ_BUFFER); | |
1486 | 242 int set_options = newfile || read_buffer |
243 || (eap != NULL && eap->read_edit); | |
7 | 244 linenr_T read_buf_lnum = 1; /* next line to read from curbuf */ |
245 colnr_T read_buf_col = 0; /* next char to read from this line */ | |
246 char_u c; | |
247 linenr_T lnum = from; | |
248 char_u *ptr = NULL; /* pointer into read buffer */ | |
249 char_u *buffer = NULL; /* read buffer */ | |
250 char_u *new_buffer = NULL; /* init to shut up gcc */ | |
251 char_u *line_start = NULL; /* init to shut up gcc */ | |
252 int wasempty; /* buffer was empty before reading */ | |
253 colnr_T len; | |
254 long size = 0; | |
255 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
|
256 off_t filesize = 0; |
7 | 257 int skip_read = FALSE; |
258 #ifdef FEAT_CRYPT | |
259 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
|
260 int did_ask_for_key = FALSE; |
7 | 261 #endif |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
262 #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
|
263 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
|
264 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
|
265 #endif |
7 | 266 int split = 0; /* number of split lines */ |
267 #define UNKNOWN 0x0fffffff /* file size is unknown */ | |
268 linenr_T linecnt; | |
269 int error = FALSE; /* errors encountered */ | |
270 int ff_error = EOL_UNKNOWN; /* file format with errors */ | |
271 long linerest = 0; /* remaining chars in line */ | |
272 #ifdef UNIX | |
273 int perm = 0; | |
274 int swap_mode = -1; /* protection bits for swap file */ | |
275 #else | |
276 int perm; | |
277 #endif | |
278 int fileformat = 0; /* end-of-line format */ | |
279 int keep_fileformat = FALSE; | |
280 struct stat st; | |
281 int file_readonly; | |
282 linenr_T skip_count = 0; | |
283 linenr_T read_count = 0; | |
284 int msg_save = msg_scroll; | |
285 linenr_T read_no_eol_lnum = 0; /* non-zero lnum when last line of | |
286 * last read was missing the eol */ | |
287 int try_mac = (vim_strchr(p_ffs, 'm') != NULL); | |
288 int try_dos = (vim_strchr(p_ffs, 'd') != NULL); | |
289 int try_unix = (vim_strchr(p_ffs, 'x') != NULL); | |
290 int file_rewind = FALSE; | |
291 #ifdef FEAT_MBYTE | |
292 int can_retry; | |
595 | 293 linenr_T conv_error = 0; /* line nr with conversion error */ |
294 linenr_T illegal_byte = 0; /* line nr with illegal byte */ | |
7 | 295 int keep_dest_enc = FALSE; /* don't retry when char doesn't fit |
296 in destination encoding */ | |
595 | 297 int bad_char_behavior = BAD_REPLACE; |
298 /* BAD_KEEP, BAD_DROP or character to | |
299 * replace with */ | |
7 | 300 char_u *tmpname = NULL; /* name of 'charconvert' output file */ |
301 int fio_flags = 0; | |
302 char_u *fenc; /* fileencoding to use */ | |
303 int fenc_alloced; /* fenc_next is in allocated memory */ | |
304 char_u *fenc_next = NULL; /* next item in 'fencs' or NULL */ | |
305 int advance_fenc = FALSE; | |
306 long real_size = 0; | |
307 # ifdef USE_ICONV | |
308 iconv_t iconv_fd = (iconv_t)-1; /* descriptor for iconv() or -1 */ | |
309 # ifdef FEAT_EVAL | |
310 int did_iconv = FALSE; /* TRUE when iconv() failed and trying | |
311 'charconvert' next */ | |
312 # endif | |
313 # endif | |
314 int converted = FALSE; /* TRUE if conversion done */ | |
315 int notconverted = FALSE; /* TRUE if conversion wanted but it | |
316 wasn't possible */ | |
317 char_u conv_rest[CONV_RESTLEN]; | |
318 int conv_restlen = 0; /* nr of bytes in conv_rest[] */ | |
319 #endif | |
320 | |
1834 | 321 #ifdef FEAT_AUTOCMD |
322 /* Remember the initial values of curbuf, curbuf->b_ffname and | |
323 * curbuf->b_fname to detect whether they are altered as a result of | |
324 * executing nasty autocommands. Also check if "fname" and "sfname" | |
325 * point to one of these values. */ | |
326 buf_T *old_curbuf = curbuf; | |
327 char_u *old_b_ffname = curbuf->b_ffname; | |
328 char_u *old_b_fname = curbuf->b_fname; | |
329 int using_b_ffname = (fname == curbuf->b_ffname) | |
330 || (sfname == curbuf->b_ffname); | |
331 int using_b_fname = (fname == curbuf->b_fname) | |
332 || (sfname == curbuf->b_fname); | |
333 #endif | |
7 | 334 write_no_eol_lnum = 0; /* in case it was set by the previous read */ |
335 | |
336 /* | |
337 * If there is no file name yet, use the one for the read file. | |
338 * BF_NOTEDITED is set to reflect this. | |
339 * Don't do this for a read from a filter. | |
340 * Only do this when 'cpoptions' contains the 'f' flag. | |
341 */ | |
342 if (curbuf->b_ffname == NULL | |
343 && !filtering | |
344 && fname != NULL | |
345 && vim_strchr(p_cpo, CPO_FNAMER) != NULL | |
346 && !(flags & READ_DUMMY)) | |
347 { | |
633 | 348 if (set_rw_fname(fname, sfname) == FAIL) |
349 return FAIL; | |
7 | 350 } |
351 | |
167 | 352 /* After reading a file the cursor line changes but we don't want to |
353 * display the line. */ | |
354 ex_no_reprint = TRUE; | |
355 | |
968 | 356 /* don't display the file info for another buffer now */ |
357 need_fileinfo = FALSE; | |
358 | |
7 | 359 /* |
360 * For Unix: Use the short file name whenever possible. | |
361 * Avoids problems with networks and when directory names are changed. | |
362 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
363 * another directory, which we don't detect. | |
364 */ | |
365 if (sfname == NULL) | |
366 sfname = fname; | |
367 #if defined(UNIX) || defined(__EMX__) | |
368 fname = sfname; | |
369 #endif | |
370 | |
371 #ifdef FEAT_AUTOCMD | |
372 /* | |
373 * The BufReadCmd and FileReadCmd events intercept the reading process by | |
374 * executing the associated commands instead. | |
375 */ | |
376 if (!filtering && !read_stdin && !read_buffer) | |
377 { | |
378 pos_T pos; | |
379 | |
380 pos = curbuf->b_op_start; | |
381 | |
382 /* Set '[ mark to the line above where the lines go (line 1 if zero). */ | |
383 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
384 curbuf->b_op_start.col = 0; | |
385 | |
386 if (newfile) | |
387 { | |
388 if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname, | |
389 FALSE, curbuf, eap)) | |
390 #ifdef FEAT_EVAL | |
391 return aborting() ? FAIL : OK; | |
392 #else | |
393 return OK; | |
394 #endif | |
395 } | |
396 else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname, | |
397 FALSE, NULL, eap)) | |
398 #ifdef FEAT_EVAL | |
399 return aborting() ? FAIL : OK; | |
400 #else | |
401 return OK; | |
402 #endif | |
403 | |
404 curbuf->b_op_start = pos; | |
405 } | |
406 #endif | |
407 | |
408 if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0) | |
409 msg_scroll = FALSE; /* overwrite previous file message */ | |
410 else | |
411 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
412 | |
413 /* | |
414 * If the name ends in a path separator, we can't open it. Check here, | |
415 * because reading the file may actually work, but then creating the swap | |
416 * file may destroy it! Reported on MS-DOS and Win 95. | |
417 * If the name is too long we might crash further on, quit here. | |
418 */ | |
23 | 419 if (fname != NULL && *fname != NUL) |
420 { | |
39 | 421 p = fname + STRLEN(fname); |
422 if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL) | |
23 | 423 { |
424 filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0); | |
425 msg_end(); | |
426 msg_scroll = msg_save; | |
427 return FAIL; | |
428 } | |
7 | 429 } |
430 | |
431 #ifdef UNIX | |
432 /* | |
433 * On Unix it is possible to read a directory, so we have to | |
434 * check for it before the mch_open(). | |
435 */ | |
436 if (!read_stdin && !read_buffer) | |
437 { | |
438 perm = mch_getperm(fname); | |
439 if (perm >= 0 && !S_ISREG(perm) /* not a regular file ... */ | |
440 # ifdef S_ISFIFO | |
441 && !S_ISFIFO(perm) /* ... or fifo */ | |
442 # endif | |
443 # ifdef S_ISSOCK | |
444 && !S_ISSOCK(perm) /* ... or socket */ | |
445 # endif | |
1313 | 446 # ifdef OPEN_CHR_FILES |
447 && !(S_ISCHR(perm) && is_dev_fd_file(fname)) | |
448 /* ... or a character special file named /dev/fd/<n> */ | |
449 # endif | |
7 | 450 ) |
451 { | |
452 if (S_ISDIR(perm)) | |
453 filemess(curbuf, fname, (char_u *)_("is a directory"), 0); | |
454 else | |
455 filemess(curbuf, fname, (char_u *)_("is not a file"), 0); | |
456 msg_end(); | |
457 msg_scroll = msg_save; | |
458 return FAIL; | |
459 } | |
1006 | 460 |
461 # if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
462 /* | |
463 * MS-Windows allows opening a device, but we will probably get stuck | |
464 * trying to read it. | |
465 */ | |
466 if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE) | |
467 { | |
1303 | 468 filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0); |
1006 | 469 msg_end(); |
470 msg_scroll = msg_save; | |
471 return FAIL; | |
472 } | |
473 # endif | |
1004 | 474 } |
475 #endif | |
476 | |
7 | 477 /* set default 'fileformat' */ |
819 | 478 if (set_options) |
7 | 479 { |
480 if (eap != NULL && eap->force_ff != 0) | |
481 set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL); | |
482 else if (*p_ffs != NUL) | |
483 set_fileformat(default_fileformat(), OPT_LOCAL); | |
484 } | |
485 | |
486 /* set or reset 'binary' */ | |
487 if (eap != NULL && eap->force_bin != 0) | |
488 { | |
489 int oldval = curbuf->b_p_bin; | |
490 | |
491 curbuf->b_p_bin = (eap->force_bin == FORCE_BIN); | |
492 set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL); | |
493 } | |
494 | |
495 /* | |
496 * When opening a new file we take the readonly flag from the file. | |
497 * Default is r/w, can be set to r/o below. | |
498 * Don't reset it when in readonly mode | |
499 * Only set/reset b_p_ro when BF_CHECK_RO is set. | |
500 */ | |
501 check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO)); | |
164 | 502 if (check_readonly && !readonlymode) |
7 | 503 curbuf->b_p_ro = FALSE; |
504 | |
505 if (newfile && !read_stdin && !read_buffer) | |
506 { | |
507 /* Remember time of file. | |
508 * For RISCOS, also remember the filetype. | |
509 */ | |
510 if (mch_stat((char *)fname, &st) >= 0) | |
511 { | |
512 buf_store_time(curbuf, &st, fname); | |
513 curbuf->b_mtime_read = curbuf->b_mtime; | |
514 | |
515 #if defined(RISCOS) && defined(FEAT_OSFILETYPE) | |
516 /* Read the filetype into the buffer local filetype option. */ | |
517 mch_read_filetype(fname); | |
518 #endif | |
519 #ifdef UNIX | |
520 /* | |
521 * Use the protection bits of the original file for the swap file. | |
522 * This makes it possible for others to read the name of the | |
523 * edited file from the swapfile, but only if they can read the | |
524 * edited file. | |
525 * Remove the "write" and "execute" bits for group and others | |
526 * (they must not write the swapfile). | |
527 * Add the "read" and "write" bits for the user, otherwise we may | |
528 * not be able to write to the file ourselves. | |
529 * Setting the bits is done below, after creating the swap file. | |
530 */ | |
531 swap_mode = (st.st_mode & 0644) | 0600; | |
532 #endif | |
533 #ifdef FEAT_CW_EDITOR | |
534 /* Get the FSSpec on MacOS | |
535 * TODO: Update it properly when the buffer name changes | |
536 */ | |
537 (void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec); | |
538 #endif | |
539 #ifdef VMS | |
540 curbuf->b_fab_rfm = st.st_fab_rfm; | |
22 | 541 curbuf->b_fab_rat = st.st_fab_rat; |
542 curbuf->b_fab_mrs = st.st_fab_mrs; | |
7 | 543 #endif |
544 } | |
545 else | |
546 { | |
547 curbuf->b_mtime = 0; | |
548 curbuf->b_mtime_read = 0; | |
549 curbuf->b_orig_size = 0; | |
550 curbuf->b_orig_mode = 0; | |
551 } | |
552 | |
553 /* Reset the "new file" flag. It will be set again below when the | |
554 * file doesn't exist. */ | |
555 curbuf->b_flags &= ~(BF_NEW | BF_NEW_W); | |
556 } | |
557 | |
558 /* | |
559 * for UNIX: check readonly with perm and mch_access() | |
560 * for RISCOS: same as Unix, otherwise file gets re-datestamped! | |
561 * for MSDOS and Amiga: check readonly by trying to open the file for writing | |
562 */ | |
563 file_readonly = FALSE; | |
564 if (read_stdin) | |
565 { | |
566 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
567 /* Force binary I/O on stdin to avoid CR-LF -> LF conversion. */ | |
568 setmode(0, O_BINARY); | |
569 #endif | |
570 } | |
571 else if (!read_buffer) | |
572 { | |
573 #ifdef USE_MCH_ACCESS | |
574 if ( | |
575 # ifdef UNIX | |
576 !(perm & 0222) || | |
577 # endif | |
578 mch_access((char *)fname, W_OK)) | |
579 file_readonly = TRUE; | |
580 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
581 #else | |
582 if (!newfile | |
583 || readonlymode | |
584 || (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0) | |
585 { | |
586 file_readonly = TRUE; | |
587 /* try to open ro */ | |
588 fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
589 } | |
590 #endif | |
591 } | |
592 | |
593 if (fd < 0) /* cannot open at all */ | |
594 { | |
595 #ifndef UNIX | |
596 int isdir_f; | |
597 #endif | |
598 msg_scroll = msg_save; | |
599 #ifndef UNIX | |
600 /* | |
601 * On MSDOS and Amiga we can't open a directory, check here. | |
602 */ | |
603 isdir_f = (mch_isdir(fname)); | |
604 perm = mch_getperm(fname); /* check if the file exists */ | |
605 if (isdir_f) | |
606 { | |
607 filemess(curbuf, sfname, (char_u *)_("is a directory"), 0); | |
608 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
609 } | |
610 else | |
611 #endif | |
612 if (newfile) | |
613 { | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
614 if (perm < 0 |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
615 #ifdef ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
616 && errno == ENOENT |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
617 #endif |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
618 ) |
7 | 619 { |
620 /* | |
621 * Set the 'new-file' flag, so that when the file has | |
622 * been created by someone else, a ":w" will complain. | |
623 */ | |
624 curbuf->b_flags |= BF_NEW; | |
625 | |
626 /* Create a swap file now, so that other Vims are warned | |
627 * that we are editing this file. Don't do this for a | |
628 * "nofile" or "nowrite" buffer type. */ | |
629 #ifdef FEAT_QUICKFIX | |
630 if (!bt_dontwrite(curbuf)) | |
631 #endif | |
1834 | 632 { |
7 | 633 check_need_swap(newfile); |
1834 | 634 #ifdef FEAT_AUTOCMD |
635 /* SwapExists autocommand may mess things up */ | |
636 if (curbuf != old_curbuf | |
637 || (using_b_ffname | |
638 && (old_b_ffname != curbuf->b_ffname)) | |
639 || (using_b_fname | |
640 && (old_b_fname != curbuf->b_fname))) | |
641 { | |
642 EMSG(_(e_auchangedbuf)); | |
643 return FAIL; | |
644 } | |
645 #endif | |
646 } | |
592 | 647 if (dir_of_file_exists(fname)) |
648 filemess(curbuf, sfname, (char_u *)_("[New File]"), 0); | |
649 else | |
650 filemess(curbuf, sfname, | |
651 (char_u *)_("[New DIRECTORY]"), 0); | |
7 | 652 #ifdef FEAT_VIMINFO |
653 /* Even though this is a new file, it might have been | |
654 * edited before and deleted. Get the old marks. */ | |
655 check_marks_read(); | |
656 #endif | |
657 #ifdef FEAT_MBYTE | |
658 if (eap != NULL && eap->force_enc != 0) | |
659 { | |
660 /* set forced 'fileencoding' */ | |
661 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
662 if (fenc != NULL) | |
663 set_string_option_direct((char_u *)"fenc", -1, | |
694 | 664 fenc, OPT_FREE|OPT_LOCAL, 0); |
7 | 665 vim_free(fenc); |
666 } | |
667 #endif | |
668 #ifdef FEAT_AUTOCMD | |
669 apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname, | |
670 FALSE, curbuf, eap); | |
671 #endif | |
672 /* remember the current fileformat */ | |
673 save_file_ff(curbuf); | |
674 | |
675 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
676 if (aborting()) /* autocmds may abort script processing */ | |
677 return FAIL; | |
678 #endif | |
679 return OK; /* a new file is not an error */ | |
680 } | |
681 else | |
682 { | |
550 | 683 filemess(curbuf, sfname, (char_u *)( |
684 # ifdef EFBIG | |
685 (errno == EFBIG) ? _("[File too big]") : | |
686 # endif | |
2147
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
687 # ifdef EOVERFLOW |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
688 (errno == EOVERFLOW) ? _("[File too big]") : |
2bd29808d1f6
updated for version 7.2.429
Bram Moolenaar <bram@zimbu.org>
parents:
2095
diff
changeset
|
689 # endif |
550 | 690 _("[Permission Denied]")), 0); |
7 | 691 curbuf->b_p_ro = TRUE; /* must use "w!" now */ |
692 } | |
693 } | |
694 | |
695 return FAIL; | |
696 } | |
697 | |
698 /* | |
699 * Only set the 'ro' flag for readonly files the first time they are | |
700 * loaded. Help files always get readonly mode | |
701 */ | |
702 if ((check_readonly && file_readonly) || curbuf->b_help) | |
703 curbuf->b_p_ro = TRUE; | |
704 | |
819 | 705 if (set_options) |
7 | 706 { |
1486 | 707 /* Don't change 'eol' if reading from buffer as it will already be |
708 * correctly set when reading stdin. */ | |
709 if (!read_buffer) | |
710 { | |
711 curbuf->b_p_eol = TRUE; | |
712 curbuf->b_start_eol = TRUE; | |
713 } | |
7 | 714 #ifdef FEAT_MBYTE |
715 curbuf->b_p_bomb = FALSE; | |
1352 | 716 curbuf->b_start_bomb = FALSE; |
7 | 717 #endif |
718 } | |
719 | |
720 /* Create a swap file now, so that other Vims are warned that we are | |
721 * editing this file. | |
722 * Don't do this for a "nofile" or "nowrite" buffer type. */ | |
723 #ifdef FEAT_QUICKFIX | |
724 if (!bt_dontwrite(curbuf)) | |
725 #endif | |
726 { | |
727 check_need_swap(newfile); | |
1834 | 728 #ifdef FEAT_AUTOCMD |
729 if (!read_stdin && (curbuf != old_curbuf | |
730 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) | |
731 || (using_b_fname && (old_b_fname != curbuf->b_fname)))) | |
732 { | |
733 EMSG(_(e_auchangedbuf)); | |
734 if (!read_buffer) | |
735 close(fd); | |
736 return FAIL; | |
737 } | |
738 #endif | |
7 | 739 #ifdef UNIX |
740 /* Set swap file protection bits after creating it. */ | |
1918 | 741 if (swap_mode > 0 && curbuf->b_ml.ml_mfp != NULL |
742 && curbuf->b_ml.ml_mfp->mf_fname != NULL) | |
7 | 743 (void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode); |
744 #endif | |
745 } | |
746 | |
578 | 747 #if defined(HAS_SWAP_EXISTS_ACTION) |
7 | 748 /* If "Quit" selected at ATTENTION dialog, don't load the file */ |
749 if (swap_exists_action == SEA_QUIT) | |
750 { | |
751 if (!read_buffer && !read_stdin) | |
752 close(fd); | |
753 return FAIL; | |
754 } | |
755 #endif | |
756 | |
757 ++no_wait_return; /* don't wait for return yet */ | |
758 | |
759 /* | |
760 * Set '[ mark to the line above where the lines go (line 1 if zero). | |
761 */ | |
762 curbuf->b_op_start.lnum = ((from == 0) ? 1 : from); | |
763 curbuf->b_op_start.col = 0; | |
764 | |
765 #ifdef FEAT_AUTOCMD | |
766 if (!read_buffer) | |
767 { | |
768 int m = msg_scroll; | |
769 int n = msg_scrolled; | |
770 | |
771 /* | |
772 * The file must be closed again, the autocommands may want to change | |
773 * the file before reading it. | |
774 */ | |
775 if (!read_stdin) | |
776 close(fd); /* ignore errors */ | |
777 | |
778 /* | |
779 * The output from the autocommands should not overwrite anything and | |
780 * should not be overwritten: Set msg_scroll, restore its value if no | |
781 * output was done. | |
782 */ | |
783 msg_scroll = TRUE; | |
784 if (filtering) | |
785 apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname, | |
786 FALSE, curbuf, eap); | |
787 else if (read_stdin) | |
788 apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname, | |
789 FALSE, curbuf, eap); | |
790 else if (newfile) | |
791 apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname, | |
792 FALSE, curbuf, eap); | |
793 else | |
794 apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname, | |
795 FALSE, NULL, eap); | |
796 if (msg_scrolled == n) | |
797 msg_scroll = m; | |
798 | |
799 #ifdef FEAT_EVAL | |
800 if (aborting()) /* autocmds may abort script processing */ | |
801 { | |
802 --no_wait_return; | |
803 msg_scroll = msg_save; | |
804 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
805 return FAIL; | |
806 } | |
807 #endif | |
808 /* | |
809 * Don't allow the autocommands to change the current buffer. | |
810 * Try to re-open the file. | |
1834 | 811 * |
812 * Don't allow the autocommands to change the buffer name either | |
813 * (cd for example) if it invalidates fname or sfname. | |
7 | 814 */ |
815 if (!read_stdin && (curbuf != old_curbuf | |
1834 | 816 || (using_b_ffname && (old_b_ffname != curbuf->b_ffname)) |
817 || (using_b_fname && (old_b_fname != curbuf->b_fname)) | |
7 | 818 || (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0)) |
819 { | |
820 --no_wait_return; | |
821 msg_scroll = msg_save; | |
822 if (fd < 0) | |
823 EMSG(_("E200: *ReadPre autocommands made the file unreadable")); | |
824 else | |
825 EMSG(_("E201: *ReadPre autocommands must not change current buffer")); | |
826 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
827 return FAIL; | |
828 } | |
829 } | |
830 #endif /* FEAT_AUTOCMD */ | |
831 | |
832 /* Autocommands may add lines to the file, need to check if it is empty */ | |
833 wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY); | |
834 | |
835 if (!recoverymode && !filtering && !(flags & READ_DUMMY)) | |
836 { | |
837 /* | |
838 * Show the user that we are busy reading the input. Sometimes this | |
839 * may take a while. When reading from stdin another program may | |
840 * still be running, don't move the cursor to the last line, unless | |
841 * always using the GUI. | |
842 */ | |
843 if (read_stdin) | |
844 { | |
845 #ifndef ALWAYS_USE_GUI | |
846 mch_msg(_("Vim: Reading from stdin...\n")); | |
847 #endif | |
848 #ifdef FEAT_GUI | |
849 /* Also write a message in the GUI window, if there is one. */ | |
850 if (gui.in_use && !gui.dying && !gui.starting) | |
851 { | |
852 p = (char_u *)_("Reading from stdin..."); | |
853 gui_write(p, (int)STRLEN(p)); | |
854 } | |
855 #endif | |
856 } | |
857 else if (!read_buffer) | |
858 filemess(curbuf, sfname, (char_u *)"", 0); | |
859 } | |
860 | |
861 msg_scroll = FALSE; /* overwrite the file message */ | |
862 | |
863 /* | |
864 * Set linecnt now, before the "retry" caused by a wrong guess for | |
865 * fileformat, and after the autocommands, which may change them. | |
866 */ | |
867 linecnt = curbuf->b_ml.ml_line_count; | |
868 | |
869 #ifdef FEAT_MBYTE | |
595 | 870 /* "++bad=" argument. */ |
871 if (eap != NULL && eap->bad_char != 0) | |
612 | 872 { |
595 | 873 bad_char_behavior = eap->bad_char; |
819 | 874 if (set_options) |
612 | 875 curbuf->b_bad_char = eap->bad_char; |
876 } | |
877 else | |
878 curbuf->b_bad_char = 0; | |
595 | 879 |
7 | 880 /* |
595 | 881 * Decide which 'encoding' to use or use first. |
7 | 882 */ |
883 if (eap != NULL && eap->force_enc != 0) | |
884 { | |
885 fenc = enc_canonize(eap->cmd + eap->force_enc); | |
886 fenc_alloced = TRUE; | |
595 | 887 keep_dest_enc = TRUE; |
7 | 888 } |
889 else if (curbuf->b_p_bin) | |
890 { | |
891 fenc = (char_u *)""; /* binary: don't convert */ | |
892 fenc_alloced = FALSE; | |
893 } | |
894 else if (curbuf->b_help) | |
895 { | |
896 char_u firstline[80]; | |
301 | 897 int fc; |
7 | 898 |
899 /* Help files are either utf-8 or latin1. Try utf-8 first, if this | |
900 * fails it must be latin1. | |
901 * Always do this when 'encoding' is "utf-8". Otherwise only do | |
902 * this when needed to avoid [converted] remarks all the time. | |
903 * It is needed when the first line contains non-ASCII characters. | |
904 * That is only in *.??x files. */ | |
905 fenc = (char_u *)"latin1"; | |
906 c = enc_utf8; | |
301 | 907 if (!c && !read_stdin) |
908 { | |
909 fc = fname[STRLEN(fname) - 1]; | |
910 if (TOLOWER_ASC(fc) == 'x') | |
911 { | |
912 /* Read the first line (and a bit more). Immediately rewind to | |
913 * the start of the file. If the read() fails "len" is -1. */ | |
914 len = vim_read(fd, firstline, 80); | |
915 lseek(fd, (off_t)0L, SEEK_SET); | |
916 for (p = firstline; p < firstline + len; ++p) | |
917 if (*p >= 0x80) | |
918 { | |
919 c = TRUE; | |
920 break; | |
921 } | |
922 } | |
7 | 923 } |
924 | |
925 if (c) | |
926 { | |
927 fenc_next = fenc; | |
928 fenc = (char_u *)"utf-8"; | |
929 | |
930 /* When the file is utf-8 but a character doesn't fit in | |
931 * 'encoding' don't retry. In help text editing utf-8 bytes | |
932 * doesn't make sense. */ | |
844 | 933 if (!enc_utf8) |
934 keep_dest_enc = TRUE; | |
7 | 935 } |
936 fenc_alloced = FALSE; | |
937 } | |
938 else if (*p_fencs == NUL) | |
939 { | |
940 fenc = curbuf->b_p_fenc; /* use format from buffer */ | |
941 fenc_alloced = FALSE; | |
942 } | |
943 else | |
944 { | |
945 fenc_next = p_fencs; /* try items in 'fileencodings' */ | |
946 fenc = next_fenc(&fenc_next); | |
947 fenc_alloced = TRUE; | |
948 } | |
949 #endif | |
950 | |
951 /* | |
952 * Jump back here to retry reading the file in different ways. | |
953 * Reasons to retry: | |
954 * - encoding conversion failed: try another one from "fenc_next" | |
955 * - BOM detected and fenc was set, need to setup conversion | |
956 * - "fileformat" check failed: try another | |
957 * | |
958 * Variables set for special retry actions: | |
959 * "file_rewind" Rewind the file to start reading it again. | |
960 * "advance_fenc" Advance "fenc" using "fenc_next". | |
961 * "skip_read" Re-use already read bytes (BOM detected). | |
962 * "did_iconv" iconv() conversion failed, try 'charconvert'. | |
963 * "keep_fileformat" Don't reset "fileformat". | |
964 * | |
965 * Other status indicators: | |
966 * "tmpname" When != NULL did conversion with 'charconvert'. | |
967 * Output file has to be deleted afterwards. | |
968 * "iconv_fd" When != -1 did conversion with iconv(). | |
969 */ | |
970 retry: | |
971 | |
972 if (file_rewind) | |
973 { | |
974 if (read_buffer) | |
975 { | |
976 read_buf_lnum = 1; | |
977 read_buf_col = 0; | |
978 } | |
979 else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0) | |
980 { | |
981 /* Can't rewind the file, give up. */ | |
982 error = TRUE; | |
983 goto failed; | |
984 } | |
985 /* Delete the previously read lines. */ | |
986 while (lnum > from) | |
987 ml_delete(lnum--, FALSE); | |
988 file_rewind = FALSE; | |
989 #ifdef FEAT_MBYTE | |
819 | 990 if (set_options) |
1352 | 991 { |
7 | 992 curbuf->b_p_bomb = FALSE; |
1352 | 993 curbuf->b_start_bomb = FALSE; |
994 } | |
595 | 995 conv_error = 0; |
7 | 996 #endif |
997 } | |
998 | |
2267 | 999 #ifdef FEAT_CRYPT |
1000 if (cryptkey != NULL) | |
1001 /* Need to reset the state, but keep the key, don't want to ask for it | |
1002 * again. */ | |
1003 crypt_pop_state(); | |
1004 #endif | |
1005 | |
7 | 1006 /* |
1007 * When retrying with another "fenc" and the first time "fileformat" | |
1008 * will be reset. | |
1009 */ | |
1010 if (keep_fileformat) | |
1011 keep_fileformat = FALSE; | |
1012 else | |
1013 { | |
1014 if (eap != NULL && eap->force_ff != 0) | |
1742 | 1015 { |
7 | 1016 fileformat = get_fileformat_force(curbuf, eap); |
1742 | 1017 try_unix = try_dos = try_mac = FALSE; |
1018 } | |
7 | 1019 else if (curbuf->b_p_bin) |
1020 fileformat = EOL_UNIX; /* binary: use Unix format */ | |
1021 else if (*p_ffs == NUL) | |
1022 fileformat = get_fileformat(curbuf);/* use format from buffer */ | |
1023 else | |
1024 fileformat = EOL_UNKNOWN; /* detect from file */ | |
1025 } | |
1026 | |
1027 #ifdef FEAT_MBYTE | |
1028 # ifdef USE_ICONV | |
1029 if (iconv_fd != (iconv_t)-1) | |
1030 { | |
1031 /* aborted conversion with iconv(), close the descriptor */ | |
1032 iconv_close(iconv_fd); | |
1033 iconv_fd = (iconv_t)-1; | |
1034 } | |
1035 # endif | |
1036 | |
1037 if (advance_fenc) | |
1038 { | |
1039 /* | |
1040 * Try the next entry in 'fileencodings'. | |
1041 */ | |
1042 advance_fenc = FALSE; | |
1043 | |
1044 if (eap != NULL && eap->force_enc != 0) | |
1045 { | |
1046 /* Conversion given with "++cc=" wasn't possible, read | |
1047 * without conversion. */ | |
1048 notconverted = TRUE; | |
595 | 1049 conv_error = 0; |
7 | 1050 if (fenc_alloced) |
1051 vim_free(fenc); | |
1052 fenc = (char_u *)""; | |
1053 fenc_alloced = FALSE; | |
1054 } | |
1055 else | |
1056 { | |
1057 if (fenc_alloced) | |
1058 vim_free(fenc); | |
1059 if (fenc_next != NULL) | |
1060 { | |
1061 fenc = next_fenc(&fenc_next); | |
1062 fenc_alloced = (fenc_next != NULL); | |
1063 } | |
1064 else | |
1065 { | |
1066 fenc = (char_u *)""; | |
1067 fenc_alloced = FALSE; | |
1068 } | |
1069 } | |
1070 if (tmpname != NULL) | |
1071 { | |
1072 mch_remove(tmpname); /* delete converted file */ | |
1073 vim_free(tmpname); | |
1074 tmpname = NULL; | |
1075 } | |
1076 } | |
1077 | |
1078 /* | |
1948 | 1079 * Conversion may be required when the encoding of the file is different |
1080 * from 'encoding' or 'encoding' is UTF-16, UCS-2 or UCS-4. | |
7 | 1081 */ |
1082 fio_flags = 0; | |
1948 | 1083 converted = need_conversion(fenc); |
1084 if (converted) | |
7 | 1085 { |
1086 | |
1087 /* "ucs-bom" means we need to check the first bytes of the file | |
1088 * for a BOM. */ | |
1089 if (STRCMP(fenc, ENC_UCSBOM) == 0) | |
1090 fio_flags = FIO_UCSBOM; | |
1091 | |
1092 /* | |
1093 * Check if UCS-2/4 or Latin1 to UTF-8 conversion needs to be | |
1094 * done. This is handled below after read(). Prepare the | |
1095 * fio_flags to avoid having to parse the string each time. | |
1096 * Also check for Unicode to Latin1 conversion, because iconv() | |
1097 * appears not to handle this correctly. This works just like | |
1098 * conversion to UTF-8 except how the resulting character is put in | |
1099 * the buffer. | |
1100 */ | |
1101 else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
1102 fio_flags = get_fio_flags(fenc); | |
1103 | |
1104 # ifdef WIN3264 | |
1105 /* | |
1106 * Conversion from an MS-Windows codepage to UTF-8 or another codepage | |
1107 * is handled with MultiByteToWideChar(). | |
1108 */ | |
1109 if (fio_flags == 0) | |
1110 fio_flags = get_win_fio_flags(fenc); | |
1111 # endif | |
1112 | |
1113 # ifdef MACOS_X | |
1114 /* Conversion from Apple MacRoman to latin1 or UTF-8 */ | |
1115 if (fio_flags == 0) | |
1116 fio_flags = get_mac_fio_flags(fenc); | |
1117 # endif | |
1118 | |
1119 # ifdef USE_ICONV | |
1120 /* | |
1121 * Try using iconv() if we can't convert internally. | |
1122 */ | |
1123 if (fio_flags == 0 | |
1124 # ifdef FEAT_EVAL | |
1125 && !did_iconv | |
1126 # endif | |
1127 ) | |
1128 iconv_fd = (iconv_t)my_iconv_open( | |
1129 enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc); | |
1130 # endif | |
1131 | |
1132 # ifdef FEAT_EVAL | |
1133 /* | |
1134 * Use the 'charconvert' expression when conversion is required | |
1135 * and we can't do it internally or with iconv(). | |
1136 */ | |
1137 if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL | |
1138 # ifdef USE_ICONV | |
1139 && iconv_fd == (iconv_t)-1 | |
1140 # endif | |
1141 ) | |
1142 { | |
1143 # ifdef USE_ICONV | |
1144 did_iconv = FALSE; | |
1145 # endif | |
1146 /* Skip conversion when it's already done (retry for wrong | |
1147 * "fileformat"). */ | |
1148 if (tmpname == NULL) | |
1149 { | |
1150 tmpname = readfile_charconvert(fname, fenc, &fd); | |
1151 if (tmpname == NULL) | |
1152 { | |
1153 /* Conversion failed. Try another one. */ | |
1154 advance_fenc = TRUE; | |
1155 if (fd < 0) | |
1156 { | |
1157 /* Re-opening the original file failed! */ | |
1158 EMSG(_("E202: Conversion made file unreadable!")); | |
1159 error = TRUE; | |
1160 goto failed; | |
1161 } | |
1162 goto retry; | |
1163 } | |
1164 } | |
1165 } | |
1166 else | |
1167 # endif | |
1168 { | |
1169 if (fio_flags == 0 | |
1170 # ifdef USE_ICONV | |
1171 && iconv_fd == (iconv_t)-1 | |
1172 # endif | |
1173 ) | |
1174 { | |
1175 /* Conversion wanted but we can't. | |
1176 * Try the next conversion in 'fileencodings' */ | |
1177 advance_fenc = TRUE; | |
1178 goto retry; | |
1179 } | |
1180 } | |
1181 } | |
1182 | |
595 | 1183 /* Set "can_retry" when it's possible to rewind the file and try with |
7 | 1184 * another "fenc" value. It's FALSE when no other "fenc" to try, reading |
595 | 1185 * stdin or fixed at a specific encoding. */ |
1186 can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc); | |
7 | 1187 #endif |
1188 | |
1189 if (!skip_read) | |
1190 { | |
1191 linerest = 0; | |
1192 filesize = 0; | |
1193 skip_count = lines_to_skip; | |
1194 read_count = lines_to_read; | |
1195 #ifdef FEAT_MBYTE | |
1196 conv_restlen = 0; | |
1197 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1198 #ifdef FEAT_PERSISTENT_UNDO |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1199 read_undo_file = (newfile && (flags & READ_KEEP_UNDO) == 0 |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1200 && curbuf->b_ffname != NULL |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1201 && curbuf->b_p_udf |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1202 && !filtering |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1203 && !read_stdin |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
1204 && !read_buffer); |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
1205 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
|
1206 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
|
1207 #endif |
7 | 1208 } |
1209 | |
1210 while (!error && !got_int) | |
1211 { | |
1212 /* | |
1213 * We allocate as much space for the file as we can get, plus | |
1214 * space for the old line plus room for one terminating NUL. | |
1215 * The amount is limited by the fact that read() only can read | |
1216 * upto max_unsigned characters (and other things). | |
1217 */ | |
1218 #if SIZEOF_INT <= 2 | |
1219 if (linerest >= 0x7ff0) | |
1220 { | |
1221 ++split; | |
1222 *ptr = NL; /* split line by inserting a NL */ | |
1223 size = 1; | |
1224 } | |
1225 else | |
1226 #endif | |
1227 { | |
1228 if (!skip_read) | |
1229 { | |
1230 #if SIZEOF_INT > 2 | |
1076 | 1231 # if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L) |
7 | 1232 size = SSIZE_MAX; /* use max I/O size, 52K */ |
1233 # else | |
1234 size = 0x10000L; /* use buffer >= 64K */ | |
1235 # endif | |
1236 #else | |
1237 size = 0x7ff0L - linerest; /* limit buffer to 32K */ | |
1238 #endif | |
1239 | |
836 | 1240 for ( ; size >= 10; size = (long)((long_u)size >> 1)) |
7 | 1241 { |
1242 if ((new_buffer = lalloc((long_u)(size + linerest + 1), | |
1243 FALSE)) != NULL) | |
1244 break; | |
1245 } | |
1246 if (new_buffer == NULL) | |
1247 { | |
1248 do_outofmem_msg((long_u)(size * 2 + linerest + 1)); | |
1249 error = TRUE; | |
1250 break; | |
1251 } | |
1252 if (linerest) /* copy characters from the previous buffer */ | |
1253 mch_memmove(new_buffer, ptr - linerest, (size_t)linerest); | |
1254 vim_free(buffer); | |
1255 buffer = new_buffer; | |
1256 ptr = buffer + linerest; | |
1257 line_start = buffer; | |
1258 | |
1259 #ifdef FEAT_MBYTE | |
1260 /* May need room to translate into. | |
1261 * For iconv() we don't really know the required space, use a | |
1262 * factor ICONV_MULT. | |
1263 * latin1 to utf-8: 1 byte becomes up to 2 bytes | |
1264 * utf-16 to utf-8: 2 bytes become up to 3 bytes, 4 bytes | |
1265 * become up to 4 bytes, size must be multiple of 2 | |
1266 * ucs-2 to utf-8: 2 bytes become up to 3 bytes, size must be | |
1267 * multiple of 2 | |
1268 * ucs-4 to utf-8: 4 bytes become up to 6 bytes, size must be | |
1269 * multiple of 4 */ | |
835 | 1270 real_size = (int)size; |
7 | 1271 # ifdef USE_ICONV |
1272 if (iconv_fd != (iconv_t)-1) | |
1273 size = size / ICONV_MULT; | |
1274 else | |
1275 # endif | |
1276 if (fio_flags & FIO_LATIN1) | |
1277 size = size / 2; | |
1278 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1279 size = (size * 2 / 3) & ~1; | |
1280 else if (fio_flags & FIO_UCS4) | |
1281 size = (size * 2 / 3) & ~3; | |
1282 else if (fio_flags == FIO_UCSBOM) | |
1283 size = size / ICONV_MULT; /* worst case */ | |
1284 # ifdef WIN3264 | |
1285 else if (fio_flags & FIO_CODEPAGE) | |
1286 size = size / ICONV_MULT; /* also worst case */ | |
1287 # endif | |
1288 # ifdef MACOS_X | |
1289 else if (fio_flags & FIO_MACROMAN) | |
1290 size = size / ICONV_MULT; /* also worst case */ | |
1291 # endif | |
1292 #endif | |
1293 | |
1294 #ifdef FEAT_MBYTE | |
1295 if (conv_restlen > 0) | |
1296 { | |
1297 /* Insert unconverted bytes from previous line. */ | |
1298 mch_memmove(ptr, conv_rest, conv_restlen); | |
1299 ptr += conv_restlen; | |
1300 size -= conv_restlen; | |
1301 } | |
1302 #endif | |
1303 | |
1304 if (read_buffer) | |
1305 { | |
1306 /* | |
1307 * Read bytes from curbuf. Used for converting text read | |
1308 * from stdin. | |
1309 */ | |
1310 if (read_buf_lnum > from) | |
1311 size = 0; | |
1312 else | |
1313 { | |
1314 int n, ni; | |
1315 long tlen; | |
1316 | |
1317 tlen = 0; | |
1318 for (;;) | |
1319 { | |
1320 p = ml_get(read_buf_lnum) + read_buf_col; | |
1321 n = (int)STRLEN(p); | |
1322 if ((int)tlen + n + 1 > size) | |
1323 { | |
1324 /* Filled up to "size", append partial line. | |
1325 * Change NL to NUL to reverse the effect done | |
1326 * below. */ | |
835 | 1327 n = (int)(size - tlen); |
7 | 1328 for (ni = 0; ni < n; ++ni) |
1329 { | |
1330 if (p[ni] == NL) | |
1331 ptr[tlen++] = NUL; | |
1332 else | |
1333 ptr[tlen++] = p[ni]; | |
1334 } | |
1335 read_buf_col += n; | |
1336 break; | |
1337 } | |
1338 else | |
1339 { | |
1340 /* Append whole line and new-line. Change NL | |
1341 * to NUL to reverse the effect done below. */ | |
1342 for (ni = 0; ni < n; ++ni) | |
1343 { | |
1344 if (p[ni] == NL) | |
1345 ptr[tlen++] = NUL; | |
1346 else | |
1347 ptr[tlen++] = p[ni]; | |
1348 } | |
1349 ptr[tlen++] = NL; | |
1350 read_buf_col = 0; | |
1351 if (++read_buf_lnum > from) | |
1352 { | |
1353 /* When the last line didn't have an | |
1354 * end-of-line don't add it now either. */ | |
1355 if (!curbuf->b_p_eol) | |
1356 --tlen; | |
1357 size = tlen; | |
1358 break; | |
1359 } | |
1360 } | |
1361 } | |
1362 } | |
1363 } | |
1364 else | |
1365 { | |
1366 /* | |
1367 * Read bytes from the file. | |
1368 */ | |
1369 size = vim_read(fd, ptr, size); | |
1370 } | |
1371 | |
1372 if (size <= 0) | |
1373 { | |
1374 if (size < 0) /* read error */ | |
1375 error = TRUE; | |
1376 #ifdef FEAT_MBYTE | |
1377 else if (conv_restlen > 0) | |
595 | 1378 { |
1597 | 1379 /* |
1380 * Reached end-of-file but some trailing bytes could | |
1381 * not be converted. Truncated file? | |
1382 */ | |
1383 | |
1384 /* When we did a conversion report an error. */ | |
1385 if (fio_flags != 0 | |
1386 # ifdef USE_ICONV | |
1387 || iconv_fd != (iconv_t)-1 | |
1388 # endif | |
1389 ) | |
1390 { | |
1391 if (conv_error == 0) | |
1392 conv_error = curbuf->b_ml.ml_line_count | |
1393 - linecnt + 1; | |
1394 } | |
1395 /* Remember the first linenr with an illegal byte */ | |
1396 else if (illegal_byte == 0) | |
1397 illegal_byte = curbuf->b_ml.ml_line_count | |
1398 - linecnt + 1; | |
1399 if (bad_char_behavior == BAD_DROP) | |
595 | 1400 { |
1597 | 1401 *(ptr - conv_restlen) = NUL; |
1402 conv_restlen = 0; | |
1403 } | |
1404 else | |
1405 { | |
1406 /* Replace the trailing bytes with the replacement | |
1407 * character if we were converting; if we weren't, | |
1408 * leave the UTF8 checking code to do it, as it | |
1409 * works slightly differently. */ | |
1410 if (bad_char_behavior != BAD_KEEP && (fio_flags != 0 | |
1411 # ifdef USE_ICONV | |
1412 || iconv_fd != (iconv_t)-1 | |
1413 # endif | |
1414 )) | |
1415 { | |
1416 while (conv_restlen > 0) | |
1417 { | |
1418 *(--ptr) = bad_char_behavior; | |
1419 --conv_restlen; | |
1420 } | |
1421 } | |
595 | 1422 fio_flags = 0; /* don't convert this */ |
834 | 1423 # ifdef USE_ICONV |
1424 if (iconv_fd != (iconv_t)-1) | |
1425 { | |
1426 iconv_close(iconv_fd); | |
1427 iconv_fd = (iconv_t)-1; | |
1428 } | |
1429 # endif | |
595 | 1430 } |
1431 } | |
7 | 1432 #endif |
1433 } | |
1434 | |
1435 #ifdef FEAT_CRYPT | |
1436 /* | |
1437 * At start of file: Check for magic number of encryption. | |
1438 */ | |
1439 if (filesize == 0) | |
1440 cryptkey = check_for_cryptkey(cryptkey, ptr, &size, | |
2267 | 1441 &filesize, newfile, sfname, |
1442 &did_ask_for_key); | |
7 | 1443 /* |
1444 * Decrypt the read bytes. | |
1445 */ | |
1446 if (cryptkey != NULL && size > 0) | |
2244 | 1447 crypt_decode(ptr, size); |
7 | 1448 #endif |
1449 } | |
1450 skip_read = FALSE; | |
1451 | |
1452 #ifdef FEAT_MBYTE | |
1453 /* | |
1454 * At start of file (or after crypt magic number): Check for BOM. | |
1455 * Also check for a BOM for other Unicode encodings, but not after | |
1456 * converting with 'charconvert' or when a BOM has already been | |
1457 * found. | |
1458 */ | |
1459 if ((filesize == 0 | |
1460 # 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
|
1461 || (filesize == (CRYPT_MAGIC_LEN |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
1462 + 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
|
1463 + 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
|
1464 && cryptkey != NULL) |
7 | 1465 # endif |
1466 ) | |
1467 && (fio_flags == FIO_UCSBOM | |
1468 || (!curbuf->b_p_bomb | |
1469 && tmpname == NULL | |
1470 && (*fenc == 'u' || (*fenc == NUL && enc_utf8))))) | |
1471 { | |
1472 char_u *ccname; | |
1473 int blen; | |
1474 | |
1475 /* no BOM detection in a short file or in binary mode */ | |
1476 if (size < 2 || curbuf->b_p_bin) | |
1477 ccname = NULL; | |
1478 else | |
1479 ccname = check_for_bom(ptr, size, &blen, | |
1480 fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc)); | |
1481 if (ccname != NULL) | |
1482 { | |
1483 /* Remove BOM from the text */ | |
1484 filesize += blen; | |
1485 size -= blen; | |
1486 mch_memmove(ptr, ptr + blen, (size_t)size); | |
819 | 1487 if (set_options) |
1352 | 1488 { |
7 | 1489 curbuf->b_p_bomb = TRUE; |
1352 | 1490 curbuf->b_start_bomb = TRUE; |
1491 } | |
7 | 1492 } |
1493 | |
1494 if (fio_flags == FIO_UCSBOM) | |
1495 { | |
1496 if (ccname == NULL) | |
1497 { | |
1498 /* No BOM detected: retry with next encoding. */ | |
1499 advance_fenc = TRUE; | |
1500 } | |
1501 else | |
1502 { | |
1503 /* BOM detected: set "fenc" and jump back */ | |
1504 if (fenc_alloced) | |
1505 vim_free(fenc); | |
1506 fenc = ccname; | |
1507 fenc_alloced = FALSE; | |
1508 } | |
1509 /* retry reading without getting new bytes or rewinding */ | |
1510 skip_read = TRUE; | |
1511 goto retry; | |
1512 } | |
1513 } | |
1597 | 1514 |
1515 /* Include not converted bytes. */ | |
1516 ptr -= conv_restlen; | |
1517 size += conv_restlen; | |
1518 conv_restlen = 0; | |
7 | 1519 #endif |
1520 /* | |
1521 * Break here for a read error or end-of-file. | |
1522 */ | |
1523 if (size <= 0) | |
1524 break; | |
1525 | |
1526 #ifdef FEAT_MBYTE | |
1527 | |
1528 # ifdef USE_ICONV | |
1529 if (iconv_fd != (iconv_t)-1) | |
1530 { | |
1531 /* | |
1532 * Attempt conversion of the read bytes to 'encoding' using | |
1533 * iconv(). | |
1534 */ | |
1535 const char *fromp; | |
1536 char *top; | |
1537 size_t from_size; | |
1538 size_t to_size; | |
1539 | |
1540 fromp = (char *)ptr; | |
1541 from_size = size; | |
1542 ptr += size; | |
1543 top = (char *)ptr; | |
1544 to_size = real_size - size; | |
1545 | |
1546 /* | |
1547 * If there is conversion error or not enough room try using | |
179 | 1548 * another conversion. Except for when there is no |
1549 * alternative (help files). | |
7 | 1550 */ |
177 | 1551 while ((iconv(iconv_fd, (void *)&fromp, &from_size, |
1552 &top, &to_size) | |
7 | 1553 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) |
1554 || from_size > CONV_RESTLEN) | |
177 | 1555 { |
595 | 1556 if (can_retry) |
375 | 1557 goto rewind_retry; |
595 | 1558 if (conv_error == 0) |
1559 conv_error = readfile_linenr(linecnt, | |
1560 ptr, (char_u *)top); | |
1561 | |
1562 /* Deal with a bad byte and continue with the next. */ | |
177 | 1563 ++fromp; |
1564 --from_size; | |
595 | 1565 if (bad_char_behavior == BAD_KEEP) |
1566 { | |
1567 *top++ = *(fromp - 1); | |
1568 --to_size; | |
1569 } | |
1570 else if (bad_char_behavior != BAD_DROP) | |
1571 { | |
1572 *top++ = bad_char_behavior; | |
1573 --to_size; | |
1574 } | |
177 | 1575 } |
7 | 1576 |
1577 if (from_size > 0) | |
1578 { | |
1579 /* Some remaining characters, keep them for the next | |
1580 * round. */ | |
1581 mch_memmove(conv_rest, (char_u *)fromp, from_size); | |
1582 conv_restlen = (int)from_size; | |
1583 } | |
1584 | |
1585 /* move the linerest to before the converted characters */ | |
1586 line_start = ptr - linerest; | |
1587 mch_memmove(line_start, buffer, (size_t)linerest); | |
1588 size = (long)((char_u *)top - ptr); | |
1589 } | |
1590 # endif | |
1591 | |
1592 # ifdef WIN3264 | |
1593 if (fio_flags & FIO_CODEPAGE) | |
1594 { | |
595 | 1595 char_u *src, *dst; |
1596 WCHAR ucs2buf[3]; | |
1597 int ucs2len; | |
1598 int codepage = FIO_GET_CP(fio_flags); | |
1599 int bytelen; | |
1600 int found_bad; | |
1601 char replstr[2]; | |
1602 | |
7 | 1603 /* |
1604 * Conversion from an MS-Windows codepage or UTF-8 to UTF-8 or | |
595 | 1605 * a codepage, using standard MS-Windows functions. This |
1606 * requires two steps: | |
1607 * 1. convert from 'fileencoding' to ucs-2 | |
1608 * 2. convert from ucs-2 to 'encoding' | |
1609 * | |
1610 * Because there may be illegal bytes AND an incomplete byte | |
1611 * sequence at the end, we may have to do the conversion one | |
1612 * character at a time to get it right. | |
7 | 1613 */ |
595 | 1614 |
1615 /* Replacement string for WideCharToMultiByte(). */ | |
1616 if (bad_char_behavior > 0) | |
1617 replstr[0] = bad_char_behavior; | |
1618 else | |
1619 replstr[0] = '?'; | |
1620 replstr[1] = NUL; | |
1621 | |
1622 /* | |
1623 * Move the bytes to the end of the buffer, so that we have | |
1624 * room to put the result at the start. | |
1625 */ | |
1626 src = ptr + real_size - size; | |
1627 mch_memmove(src, ptr, size); | |
7 | 1628 |
1629 /* | |
595 | 1630 * Do the conversion. |
7 | 1631 */ |
595 | 1632 dst = ptr; |
1633 size = size; | |
1634 while (size > 0) | |
7 | 1635 { |
595 | 1636 found_bad = FALSE; |
7 | 1637 |
1638 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
595 | 1639 if (codepage == CP_UTF8) |
7 | 1640 { |
595 | 1641 /* Handle CP_UTF8 input ourselves to be able to handle |
1642 * trailing bytes properly. | |
1643 * Get one UTF-8 character from src. */ | |
835 | 1644 bytelen = (int)utf_ptr2len_len(src, size); |
595 | 1645 if (bytelen > size) |
1646 { | |
1647 /* Only got some bytes of a character. Normally | |
1648 * it's put in "conv_rest", but if it's too long | |
1649 * deal with it as if they were illegal bytes. */ | |
1650 if (bytelen <= CONV_RESTLEN) | |
1651 break; | |
1652 | |
1653 /* weird overlong byte sequence */ | |
1654 bytelen = size; | |
1655 found_bad = TRUE; | |
1656 } | |
1657 else | |
1658 { | |
799 | 1659 int u8c = utf_ptr2char(src); |
1660 | |
622 | 1661 if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1)) |
595 | 1662 found_bad = TRUE; |
1663 ucs2buf[0] = u8c; | |
1664 ucs2len = 1; | |
1665 } | |
7 | 1666 } |
595 | 1667 else |
7 | 1668 # endif |
595 | 1669 { |
1670 /* We don't know how long the byte sequence is, try | |
1671 * from one to three bytes. */ | |
1672 for (bytelen = 1; bytelen <= size && bytelen <= 3; | |
1673 ++bytelen) | |
1674 { | |
1675 ucs2len = MultiByteToWideChar(codepage, | |
1676 MB_ERR_INVALID_CHARS, | |
1677 (LPCSTR)src, bytelen, | |
1678 ucs2buf, 3); | |
1679 if (ucs2len > 0) | |
1680 break; | |
1681 } | |
1682 if (ucs2len == 0) | |
1683 { | |
1684 /* If we have only one byte then it's probably an | |
1685 * incomplete byte sequence. Otherwise discard | |
1686 * one byte as a bad character. */ | |
1687 if (size == 1) | |
1688 break; | |
1689 found_bad = TRUE; | |
1690 bytelen = 1; | |
1691 } | |
1692 } | |
1693 | |
1694 if (!found_bad) | |
7 | 1695 { |
595 | 1696 int i; |
1697 | |
1698 /* Convert "ucs2buf[ucs2len]" to 'enc' in "dst". */ | |
1699 if (enc_utf8) | |
1700 { | |
1701 /* From UCS-2 to UTF-8. Cannot fail. */ | |
1702 for (i = 0; i < ucs2len; ++i) | |
1703 dst += utf_char2bytes(ucs2buf[i], dst); | |
1704 } | |
1705 else | |
1706 { | |
1707 BOOL bad = FALSE; | |
1708 int dstlen; | |
1709 | |
1710 /* From UCS-2 to "enc_codepage". If the | |
1711 * conversion uses the default character "?", | |
1712 * the data doesn't fit in this encoding. */ | |
1713 dstlen = WideCharToMultiByte(enc_codepage, 0, | |
1714 (LPCWSTR)ucs2buf, ucs2len, | |
835 | 1715 (LPSTR)dst, (int)(src - dst), |
595 | 1716 replstr, &bad); |
1717 if (bad) | |
1718 found_bad = TRUE; | |
1719 else | |
1720 dst += dstlen; | |
1721 } | |
7 | 1722 } |
595 | 1723 |
1724 if (found_bad) | |
1725 { | |
1726 /* Deal with bytes we can't convert. */ | |
1727 if (can_retry) | |
1728 goto rewind_retry; | |
1729 if (conv_error == 0) | |
1730 conv_error = readfile_linenr(linecnt, ptr, dst); | |
1731 if (bad_char_behavior != BAD_DROP) | |
1732 { | |
1733 if (bad_char_behavior == BAD_KEEP) | |
1734 { | |
1735 mch_memmove(dst, src, bytelen); | |
1736 dst += bytelen; | |
1737 } | |
1738 else | |
1739 *dst++ = bad_char_behavior; | |
1740 } | |
1741 } | |
1742 | |
1743 src += bytelen; | |
1744 size -= bytelen; | |
7 | 1745 } |
595 | 1746 |
1747 if (size > 0) | |
7 | 1748 { |
595 | 1749 /* An incomplete byte sequence remaining. */ |
1750 mch_memmove(conv_rest, src, size); | |
1751 conv_restlen = size; | |
7 | 1752 } |
595 | 1753 |
1754 /* The new size is equal to how much "dst" was advanced. */ | |
835 | 1755 size = (long)(dst - ptr); |
7 | 1756 } |
1757 else | |
1758 # endif | |
765 | 1759 # ifdef MACOS_CONVERT |
7 | 1760 if (fio_flags & FIO_MACROMAN) |
1761 { | |
1762 /* | |
1763 * Conversion from Apple MacRoman char encoding to UTF-8 or | |
18 | 1764 * latin1. This is in os_mac_conv.c. |
7 | 1765 */ |
18 | 1766 if (macroman2enc(ptr, &size, real_size) == FAIL) |
7 | 1767 goto rewind_retry; |
1768 } | |
1769 else | |
1770 # endif | |
1771 if (fio_flags != 0) | |
1772 { | |
1773 int u8c; | |
1774 char_u *dest; | |
1775 char_u *tail = NULL; | |
1776 | |
1777 /* | |
1778 * "enc_utf8" set: Convert Unicode or Latin1 to UTF-8. | |
1779 * "enc_utf8" not set: Convert Unicode to Latin1. | |
1780 * Go from end to start through the buffer, because the number | |
1781 * of bytes may increase. | |
1782 * "dest" points to after where the UTF-8 bytes go, "p" points | |
1783 * to after the next character to convert. | |
1784 */ | |
1785 dest = ptr + real_size; | |
1786 if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8) | |
1787 { | |
1788 p = ptr + size; | |
1789 if (fio_flags == FIO_UTF8) | |
1790 { | |
1791 /* Check for a trailing incomplete UTF-8 sequence */ | |
1792 tail = ptr + size - 1; | |
1793 while (tail > ptr && (*tail & 0xc0) == 0x80) | |
1794 --tail; | |
1795 if (tail + utf_byte2len(*tail) <= ptr + size) | |
1796 tail = NULL; | |
1797 else | |
1798 p = tail; | |
1799 } | |
1800 } | |
1801 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1802 { | |
1803 /* Check for a trailing byte */ | |
1804 p = ptr + (size & ~1); | |
1805 if (size & 1) | |
1806 tail = p; | |
1807 if ((fio_flags & FIO_UTF16) && p > ptr) | |
1808 { | |
1809 /* Check for a trailing leading word */ | |
1810 if (fio_flags & FIO_ENDIAN_L) | |
1811 { | |
1812 u8c = (*--p << 8); | |
1813 u8c += *--p; | |
1814 } | |
1815 else | |
1816 { | |
1817 u8c = *--p; | |
1818 u8c += (*--p << 8); | |
1819 } | |
1820 if (u8c >= 0xd800 && u8c <= 0xdbff) | |
1821 tail = p; | |
1822 else | |
1823 p += 2; | |
1824 } | |
1825 } | |
1826 else /* FIO_UCS4 */ | |
1827 { | |
1828 /* Check for trailing 1, 2 or 3 bytes */ | |
1829 p = ptr + (size & ~3); | |
1830 if (size & 3) | |
1831 tail = p; | |
1832 } | |
1833 | |
1834 /* If there is a trailing incomplete sequence move it to | |
1835 * conv_rest[]. */ | |
1836 if (tail != NULL) | |
1837 { | |
1838 conv_restlen = (int)((ptr + size) - tail); | |
1839 mch_memmove(conv_rest, (char_u *)tail, conv_restlen); | |
1840 size -= conv_restlen; | |
1841 } | |
1842 | |
1843 | |
1844 while (p > ptr) | |
1845 { | |
1846 if (fio_flags & FIO_LATIN1) | |
1847 u8c = *--p; | |
1848 else if (fio_flags & (FIO_UCS2 | FIO_UTF16)) | |
1849 { | |
1850 if (fio_flags & FIO_ENDIAN_L) | |
1851 { | |
1852 u8c = (*--p << 8); | |
1853 u8c += *--p; | |
1854 } | |
1855 else | |
1856 { | |
1857 u8c = *--p; | |
1858 u8c += (*--p << 8); | |
1859 } | |
1860 if ((fio_flags & FIO_UTF16) | |
1861 && u8c >= 0xdc00 && u8c <= 0xdfff) | |
1862 { | |
1863 int u16c; | |
1864 | |
1865 if (p == ptr) | |
1866 { | |
1867 /* Missing leading word. */ | |
1868 if (can_retry) | |
1869 goto rewind_retry; | |
595 | 1870 if (conv_error == 0) |
1871 conv_error = readfile_linenr(linecnt, | |
1872 ptr, p); | |
1873 if (bad_char_behavior == BAD_DROP) | |
1874 continue; | |
1875 if (bad_char_behavior != BAD_KEEP) | |
1876 u8c = bad_char_behavior; | |
7 | 1877 } |
1878 | |
1879 /* found second word of double-word, get the first | |
1880 * word and compute the resulting character */ | |
1881 if (fio_flags & FIO_ENDIAN_L) | |
1882 { | |
1883 u16c = (*--p << 8); | |
1884 u16c += *--p; | |
1885 } | |
1886 else | |
1887 { | |
1888 u16c = *--p; | |
1889 u16c += (*--p << 8); | |
1890 } | |
595 | 1891 u8c = 0x10000 + ((u16c & 0x3ff) << 10) |
1892 + (u8c & 0x3ff); | |
1893 | |
7 | 1894 /* Check if the word is indeed a leading word. */ |
1895 if (u16c < 0xd800 || u16c > 0xdbff) | |
1896 { | |
1897 if (can_retry) | |
1898 goto rewind_retry; | |
595 | 1899 if (conv_error == 0) |
1900 conv_error = readfile_linenr(linecnt, | |
1901 ptr, p); | |
1902 if (bad_char_behavior == BAD_DROP) | |
1903 continue; | |
1904 if (bad_char_behavior != BAD_KEEP) | |
1905 u8c = bad_char_behavior; | |
7 | 1906 } |
1907 } | |
1908 } | |
1909 else if (fio_flags & FIO_UCS4) | |
1910 { | |
1911 if (fio_flags & FIO_ENDIAN_L) | |
1912 { | |
1913 u8c = (*--p << 24); | |
1914 u8c += (*--p << 16); | |
1915 u8c += (*--p << 8); | |
1916 u8c += *--p; | |
1917 } | |
1918 else /* big endian */ | |
1919 { | |
1920 u8c = *--p; | |
1921 u8c += (*--p << 8); | |
1922 u8c += (*--p << 16); | |
1923 u8c += (*--p << 24); | |
1924 } | |
1925 } | |
1926 else /* UTF-8 */ | |
1927 { | |
1928 if (*--p < 0x80) | |
1929 u8c = *p; | |
1930 else | |
1931 { | |
1932 len = utf_head_off(ptr, p); | |
595 | 1933 p -= len; |
1934 u8c = utf_ptr2char(p); | |
7 | 1935 if (len == 0) |
1936 { | |
1937 /* Not a valid UTF-8 character, retry with | |
1938 * another fenc when possible, otherwise just | |
1939 * report the error. */ | |
1940 if (can_retry) | |
1941 goto rewind_retry; | |
595 | 1942 if (conv_error == 0) |
1943 conv_error = readfile_linenr(linecnt, | |
1944 ptr, p); | |
1945 if (bad_char_behavior == BAD_DROP) | |
1946 continue; | |
1947 if (bad_char_behavior != BAD_KEEP) | |
1948 u8c = bad_char_behavior; | |
7 | 1949 } |
1950 } | |
1951 } | |
1952 if (enc_utf8) /* produce UTF-8 */ | |
1953 { | |
1954 dest -= utf_char2len(u8c); | |
1955 (void)utf_char2bytes(u8c, dest); | |
1956 } | |
1957 else /* produce Latin1 */ | |
1958 { | |
1959 --dest; | |
1960 if (u8c >= 0x100) | |
1961 { | |
1962 /* character doesn't fit in latin1, retry with | |
1963 * another fenc when possible, otherwise just | |
1964 * report the error. */ | |
595 | 1965 if (can_retry) |
7 | 1966 goto rewind_retry; |
595 | 1967 if (conv_error == 0) |
1968 conv_error = readfile_linenr(linecnt, ptr, p); | |
1969 if (bad_char_behavior == BAD_DROP) | |
1970 ++dest; | |
1971 else if (bad_char_behavior == BAD_KEEP) | |
1972 *dest = u8c; | |
1973 else if (eap != NULL && eap->bad_char != 0) | |
1974 *dest = bad_char_behavior; | |
1975 else | |
1976 *dest = 0xBF; | |
7 | 1977 } |
1978 else | |
1979 *dest = u8c; | |
1980 } | |
1981 } | |
1982 | |
1983 /* move the linerest to before the converted characters */ | |
1984 line_start = dest - linerest; | |
1985 mch_memmove(line_start, buffer, (size_t)linerest); | |
1986 size = (long)((ptr + real_size) - dest); | |
1987 ptr = dest; | |
1988 } | |
1597 | 1989 else if (enc_utf8 && !curbuf->b_p_bin) |
1990 { | |
1991 int incomplete_tail = FALSE; | |
1992 | |
1993 /* Reading UTF-8: Check if the bytes are valid UTF-8. */ | |
1994 for (p = ptr; ; ++p) | |
7 | 1995 { |
835 | 1996 int todo = (int)((ptr + size) - p); |
595 | 1997 int l; |
1998 | |
1999 if (todo <= 0) | |
2000 break; | |
7 | 2001 if (*p >= 0x80) |
2002 { | |
2003 /* A length of 1 means it's an illegal byte. Accept | |
2004 * an incomplete character at the end though, the next | |
2005 * read() will get the next bytes, we'll check it | |
2006 * then. */ | |
595 | 2007 l = utf_ptr2len_len(p, todo); |
1597 | 2008 if (l > todo && !incomplete_tail) |
7 | 2009 { |
1597 | 2010 /* Avoid retrying with a different encoding when |
2011 * a truncated file is more likely, or attempting | |
2012 * to read the rest of an incomplete sequence when | |
2013 * we have already done so. */ | |
2014 if (p > ptr || filesize > 0) | |
2015 incomplete_tail = TRUE; | |
2016 /* Incomplete byte sequence, move it to conv_rest[] | |
2017 * and try to read the rest of it, unless we've | |
2018 * already done so. */ | |
2019 if (p > ptr) | |
2020 { | |
2021 conv_restlen = todo; | |
2022 mch_memmove(conv_rest, p, conv_restlen); | |
2023 size -= conv_restlen; | |
2024 break; | |
2025 } | |
7 | 2026 } |
1597 | 2027 if (l == 1 || l > todo) |
595 | 2028 { |
2029 /* Illegal byte. If we can try another encoding | |
1597 | 2030 * do that, unless at EOF where a truncated |
2031 * file is more likely than a conversion error. */ | |
2032 if (can_retry && !incomplete_tail) | |
595 | 2033 break; |
2034 # ifdef USE_ICONV | |
2035 /* When we did a conversion report an error. */ | |
2036 if (iconv_fd != (iconv_t)-1 && conv_error == 0) | |
2037 conv_error = readfile_linenr(linecnt, ptr, p); | |
2038 # endif | |
1597 | 2039 /* Remember the first linenr with an illegal byte */ |
2040 if (conv_error == 0 && illegal_byte == 0) | |
2041 illegal_byte = readfile_linenr(linecnt, ptr, p); | |
595 | 2042 |
2043 /* Drop, keep or replace the bad byte. */ | |
2044 if (bad_char_behavior == BAD_DROP) | |
2045 { | |
1597 | 2046 mch_memmove(p, p + 1, todo - 1); |
595 | 2047 --p; |
2048 --size; | |
2049 } | |
2050 else if (bad_char_behavior != BAD_KEEP) | |
2051 *p = bad_char_behavior; | |
2052 } | |
1597 | 2053 else |
2054 p += l - 1; | |
7 | 2055 } |
2056 } | |
1597 | 2057 if (p < ptr + size && !incomplete_tail) |
7 | 2058 { |
2059 /* Detected a UTF-8 error. */ | |
2060 rewind_retry: | |
595 | 2061 /* Retry reading with another conversion. */ |
7 | 2062 # if defined(FEAT_EVAL) && defined(USE_ICONV) |
595 | 2063 if (*p_ccv != NUL && iconv_fd != (iconv_t)-1) |
2064 /* iconv() failed, try 'charconvert' */ | |
2065 did_iconv = TRUE; | |
7 | 2066 else |
2067 # endif | |
595 | 2068 /* use next item from 'fileencodings' */ |
2069 advance_fenc = TRUE; | |
2070 file_rewind = TRUE; | |
2071 goto retry; | |
7 | 2072 } |
2073 } | |
2074 #endif | |
2075 | |
2076 /* count the number of characters (after conversion!) */ | |
2077 filesize += size; | |
2078 | |
2079 /* | |
2080 * when reading the first part of a file: guess EOL type | |
2081 */ | |
2082 if (fileformat == EOL_UNKNOWN) | |
2083 { | |
2084 /* First try finding a NL, for Dos and Unix */ | |
2085 if (try_dos || try_unix) | |
2086 { | |
2087 for (p = ptr; p < ptr + size; ++p) | |
2088 { | |
2089 if (*p == NL) | |
2090 { | |
2091 if (!try_unix | |
2092 || (try_dos && p > ptr && p[-1] == CAR)) | |
2093 fileformat = EOL_DOS; | |
2094 else | |
2095 fileformat = EOL_UNIX; | |
2096 break; | |
2097 } | |
2098 } | |
2099 | |
2100 /* Don't give in to EOL_UNIX if EOL_MAC is more likely */ | |
2101 if (fileformat == EOL_UNIX && try_mac) | |
2102 { | |
2103 /* Need to reset the counters when retrying fenc. */ | |
2104 try_mac = 1; | |
2105 try_unix = 1; | |
2106 for (; p >= ptr && *p != CAR; p--) | |
2107 ; | |
2108 if (p >= ptr) | |
2109 { | |
2110 for (p = ptr; p < ptr + size; ++p) | |
2111 { | |
2112 if (*p == NL) | |
2113 try_unix++; | |
2114 else if (*p == CAR) | |
2115 try_mac++; | |
2116 } | |
2117 if (try_mac > try_unix) | |
2118 fileformat = EOL_MAC; | |
2119 } | |
2120 } | |
2121 } | |
2122 | |
2123 /* No NL found: may use Mac format */ | |
2124 if (fileformat == EOL_UNKNOWN && try_mac) | |
2125 fileformat = EOL_MAC; | |
2126 | |
2127 /* Still nothing found? Use first format in 'ffs' */ | |
2128 if (fileformat == EOL_UNKNOWN) | |
2129 fileformat = default_fileformat(); | |
2130 | |
2131 /* if editing a new file: may set p_tx and p_ff */ | |
819 | 2132 if (set_options) |
7 | 2133 set_fileformat(fileformat, OPT_LOCAL); |
2134 } | |
2135 } | |
2136 | |
2137 /* | |
2138 * This loop is executed once for every character read. | |
2139 * Keep it fast! | |
2140 */ | |
2141 if (fileformat == EOL_MAC) | |
2142 { | |
2143 --ptr; | |
2144 while (++ptr, --size >= 0) | |
2145 { | |
2146 /* catch most common case first */ | |
2147 if ((c = *ptr) != NUL && c != CAR && c != NL) | |
2148 continue; | |
2149 if (c == NUL) | |
2150 *ptr = NL; /* NULs are replaced by newlines! */ | |
2151 else if (c == NL) | |
2152 *ptr = CAR; /* NLs are replaced by CRs! */ | |
2153 else | |
2154 { | |
2155 if (skip_count == 0) | |
2156 { | |
2157 *ptr = NUL; /* end of line */ | |
2158 len = (colnr_T) (ptr - line_start + 1); | |
2159 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2160 { | |
2161 error = TRUE; | |
2162 break; | |
2163 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2164 #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
|
2165 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
|
2166 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
|
2167 #endif |
7 | 2168 ++lnum; |
2169 if (--read_count == 0) | |
2170 { | |
2171 error = TRUE; /* break loop */ | |
2172 line_start = ptr; /* nothing left to write */ | |
2173 break; | |
2174 } | |
2175 } | |
2176 else | |
2177 --skip_count; | |
2178 line_start = ptr + 1; | |
2179 } | |
2180 } | |
2181 } | |
2182 else | |
2183 { | |
2184 --ptr; | |
2185 while (++ptr, --size >= 0) | |
2186 { | |
2187 if ((c = *ptr) != NUL && c != NL) /* catch most common case */ | |
2188 continue; | |
2189 if (c == NUL) | |
2190 *ptr = NL; /* NULs are replaced by newlines! */ | |
2191 else | |
2192 { | |
2193 if (skip_count == 0) | |
2194 { | |
2195 *ptr = NUL; /* end of line */ | |
2196 len = (colnr_T)(ptr - line_start + 1); | |
2197 if (fileformat == EOL_DOS) | |
2198 { | |
2199 if (ptr[-1] == CAR) /* remove CR */ | |
2200 { | |
2201 ptr[-1] = NUL; | |
2202 --len; | |
2203 } | |
2204 /* | |
2205 * Reading in Dos format, but no CR-LF found! | |
2206 * When 'fileformats' includes "unix", delete all | |
2207 * the lines read so far and start all over again. | |
2208 * Otherwise give an error message later. | |
2209 */ | |
2210 else if (ff_error != EOL_DOS) | |
2211 { | |
2212 if ( try_unix | |
2213 && !read_stdin | |
2214 && (read_buffer | |
2215 || lseek(fd, (off_t)0L, SEEK_SET) == 0)) | |
2216 { | |
2217 fileformat = EOL_UNIX; | |
819 | 2218 if (set_options) |
7 | 2219 set_fileformat(EOL_UNIX, OPT_LOCAL); |
2220 file_rewind = TRUE; | |
2221 keep_fileformat = TRUE; | |
2222 goto retry; | |
2223 } | |
2224 ff_error = EOL_DOS; | |
2225 } | |
2226 } | |
2227 if (ml_append(lnum, line_start, len, newfile) == FAIL) | |
2228 { | |
2229 error = TRUE; | |
2230 break; | |
2231 } | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2232 #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
|
2233 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
|
2234 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
|
2235 #endif |
7 | 2236 ++lnum; |
2237 if (--read_count == 0) | |
2238 { | |
2239 error = TRUE; /* break loop */ | |
2240 line_start = ptr; /* nothing left to write */ | |
2241 break; | |
2242 } | |
2243 } | |
2244 else | |
2245 --skip_count; | |
2246 line_start = ptr + 1; | |
2247 } | |
2248 } | |
2249 } | |
2250 linerest = (long)(ptr - line_start); | |
2251 ui_breakcheck(); | |
2252 } | |
2253 | |
2254 failed: | |
2255 /* not an error, max. number of lines reached */ | |
2256 if (error && read_count == 0) | |
2257 error = FALSE; | |
2258 | |
2259 /* | |
2260 * If we get EOF in the middle of a line, note the fact and | |
2261 * complete the line ourselves. | |
2262 * In Dos format ignore a trailing CTRL-Z, unless 'binary' set. | |
2263 */ | |
2264 if (!error | |
2265 && !got_int | |
2266 && linerest != 0 | |
2267 && !(!curbuf->b_p_bin | |
2268 && fileformat == EOL_DOS | |
2269 && *line_start == Ctrl_Z | |
2270 && ptr == line_start + 1)) | |
2271 { | |
819 | 2272 /* remember for when writing */ |
2273 if (set_options) | |
7 | 2274 curbuf->b_p_eol = FALSE; |
2275 *ptr = NUL; | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2276 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
|
2277 if (ml_append(lnum, line_start, len, newfile) == FAIL) |
7 | 2278 error = TRUE; |
2279 else | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2280 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2281 #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
|
2282 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
|
2283 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
|
2284 #endif |
7 | 2285 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
|
2286 } |
7 | 2287 } |
2288 | |
819 | 2289 if (set_options) |
7 | 2290 save_file_ff(curbuf); /* remember the current file format */ |
2291 | |
2292 #ifdef FEAT_CRYPT | |
2267 | 2293 if (cryptkey != NULL) |
2294 { | |
2295 crypt_pop_state(); | |
2296 if (cryptkey != curbuf->b_p_key) | |
2297 free_crypt_key(cryptkey); | |
2298 /* don't set cryptkey to NULL, it's used below as a flag that | |
2299 * encryption was used */ | |
2300 } | |
7 | 2301 #endif |
2302 | |
2303 #ifdef FEAT_MBYTE | |
819 | 2304 /* If editing a new file: set 'fenc' for the current buffer. |
2305 * Also for ":read ++edit file". */ | |
2306 if (set_options) | |
7 | 2307 set_string_option_direct((char_u *)"fenc", -1, fenc, |
694 | 2308 OPT_FREE|OPT_LOCAL, 0); |
7 | 2309 if (fenc_alloced) |
2310 vim_free(fenc); | |
2311 # ifdef USE_ICONV | |
2312 if (iconv_fd != (iconv_t)-1) | |
2313 { | |
2314 iconv_close(iconv_fd); | |
2315 iconv_fd = (iconv_t)-1; | |
2316 } | |
2317 # endif | |
2318 #endif | |
2319 | |
2320 if (!read_buffer && !read_stdin) | |
2321 close(fd); /* errors are ignored */ | |
2003 | 2322 #ifdef HAVE_FD_CLOEXEC |
2323 else | |
2324 { | |
2325 int fdflags = fcntl(fd, F_GETFD); | |
2326 if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) | |
2327 fcntl(fd, F_SETFD, fdflags | FD_CLOEXEC); | |
2328 } | |
2329 #endif | |
7 | 2330 vim_free(buffer); |
2331 | |
2332 #ifdef HAVE_DUP | |
2333 if (read_stdin) | |
2334 { | |
2335 /* Use stderr for stdin, makes shell commands work. */ | |
2336 close(0); | |
1757 | 2337 ignored = dup(2); |
7 | 2338 } |
2339 #endif | |
2340 | |
2341 #ifdef FEAT_MBYTE | |
2342 if (tmpname != NULL) | |
2343 { | |
2344 mch_remove(tmpname); /* delete converted file */ | |
2345 vim_free(tmpname); | |
2346 } | |
2347 #endif | |
2348 --no_wait_return; /* may wait for return now */ | |
2349 | |
2350 /* | |
2351 * In recovery mode everything but autocommands is skipped. | |
2352 */ | |
2353 if (!recoverymode) | |
2354 { | |
2355 /* need to delete the last line, which comes from the empty buffer */ | |
2356 if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY)) | |
2357 { | |
2358 #ifdef FEAT_NETBEANS_INTG | |
2359 netbeansFireChanges = 0; | |
2360 #endif | |
2361 ml_delete(curbuf->b_ml.ml_line_count, FALSE); | |
2362 #ifdef FEAT_NETBEANS_INTG | |
2363 netbeansFireChanges = 1; | |
2364 #endif | |
2365 --linecnt; | |
2366 } | |
2367 linecnt = curbuf->b_ml.ml_line_count - linecnt; | |
2368 if (filesize == 0) | |
2369 linecnt = 0; | |
2370 if (newfile || read_buffer) | |
1201 | 2371 { |
7 | 2372 redraw_curbuf_later(NOT_VALID); |
1201 | 2373 #ifdef FEAT_DIFF |
2374 /* After reading the text into the buffer the diff info needs to | |
2375 * be updated. */ | |
2376 diff_invalidate(curbuf); | |
2377 #endif | |
2378 #ifdef FEAT_FOLDING | |
2379 /* All folds in the window are invalid now. Mark them for update | |
2380 * before triggering autocommands. */ | |
2381 foldUpdateAll(curwin); | |
2382 #endif | |
2383 } | |
7 | 2384 else if (linecnt) /* appended at least one line */ |
2385 appended_lines_mark(from, linecnt); | |
2386 | |
2387 #ifndef ALWAYS_USE_GUI | |
2388 /* | |
2389 * If we were reading from the same terminal as where messages go, | |
2390 * the screen will have been messed up. | |
2391 * Switch on raw mode now and clear the screen. | |
2392 */ | |
2393 if (read_stdin) | |
2394 { | |
2395 settmode(TMODE_RAW); /* set to raw mode */ | |
2396 starttermcap(); | |
2397 screenclear(); | |
2398 } | |
2399 #endif | |
2400 | |
2401 if (got_int) | |
2402 { | |
2403 if (!(flags & READ_DUMMY)) | |
2404 { | |
2405 filemess(curbuf, sfname, (char_u *)_(e_interr), 0); | |
2406 if (newfile) | |
2407 curbuf->b_p_ro = TRUE; /* must use "w!" now */ | |
2408 } | |
2409 msg_scroll = msg_save; | |
2410 #ifdef FEAT_VIMINFO | |
2411 check_marks_read(); | |
2412 #endif | |
2413 return OK; /* an interrupt isn't really an error */ | |
2414 } | |
2415 | |
2416 if (!filtering && !(flags & READ_DUMMY)) | |
2417 { | |
2418 msg_add_fname(curbuf, sfname); /* fname in IObuff with quotes */ | |
2419 c = FALSE; | |
2420 | |
2421 #ifdef UNIX | |
2422 # ifdef S_ISFIFO | |
2423 if (S_ISFIFO(perm)) /* fifo or socket */ | |
2424 { | |
2425 STRCAT(IObuff, _("[fifo/socket]")); | |
2426 c = TRUE; | |
2427 } | |
2428 # else | |
2429 # ifdef S_IFIFO | |
2430 if ((perm & S_IFMT) == S_IFIFO) /* fifo */ | |
2431 { | |
2432 STRCAT(IObuff, _("[fifo]")); | |
2433 c = TRUE; | |
2434 } | |
2435 # endif | |
2436 # ifdef S_IFSOCK | |
2437 if ((perm & S_IFMT) == S_IFSOCK) /* or socket */ | |
2438 { | |
2439 STRCAT(IObuff, _("[socket]")); | |
2440 c = TRUE; | |
2441 } | |
2442 # endif | |
2443 # endif | |
1313 | 2444 # ifdef OPEN_CHR_FILES |
2445 if (S_ISCHR(perm)) /* or character special */ | |
2446 { | |
2447 STRCAT(IObuff, _("[character special]")); | |
2448 c = TRUE; | |
2449 } | |
2450 # endif | |
7 | 2451 #endif |
2452 if (curbuf->b_p_ro) | |
2453 { | |
2454 STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]")); | |
2455 c = TRUE; | |
2456 } | |
2457 if (read_no_eol_lnum) | |
2458 { | |
2459 msg_add_eol(); | |
2460 c = TRUE; | |
2461 } | |
2462 if (ff_error == EOL_DOS) | |
2463 { | |
2464 STRCAT(IObuff, _("[CR missing]")); | |
2465 c = TRUE; | |
2466 } | |
2467 if (split) | |
2468 { | |
2469 STRCAT(IObuff, _("[long lines split]")); | |
2470 c = TRUE; | |
2471 } | |
2472 #ifdef FEAT_MBYTE | |
2473 if (notconverted) | |
2474 { | |
2475 STRCAT(IObuff, _("[NOT converted]")); | |
2476 c = TRUE; | |
2477 } | |
2478 else if (converted) | |
2479 { | |
2480 STRCAT(IObuff, _("[converted]")); | |
2481 c = TRUE; | |
2482 } | |
2483 #endif | |
2484 #ifdef FEAT_CRYPT | |
2485 if (cryptkey != NULL) | |
2486 { | |
2487 STRCAT(IObuff, _("[crypted]")); | |
2488 c = TRUE; | |
2489 } | |
2490 #endif | |
2491 #ifdef FEAT_MBYTE | |
595 | 2492 if (conv_error != 0) |
2493 { | |
2494 sprintf((char *)IObuff + STRLEN(IObuff), | |
2495 _("[CONVERSION ERROR in line %ld]"), (long)conv_error); | |
7 | 2496 c = TRUE; |
2497 } | |
2498 else if (illegal_byte > 0) | |
2499 { | |
2500 sprintf((char *)IObuff + STRLEN(IObuff), | |
2501 _("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte); | |
2502 c = TRUE; | |
2503 } | |
2504 else | |
2505 #endif | |
2506 if (error) | |
2507 { | |
2508 STRCAT(IObuff, _("[READ ERRORS]")); | |
2509 c = TRUE; | |
2510 } | |
2511 if (msg_add_fileformat(fileformat)) | |
2512 c = TRUE; | |
2513 #ifdef FEAT_CRYPT | |
2514 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
|
2515 msg_add_lines(c, (long)linecnt, filesize |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2516 - CRYPT_MAGIC_LEN |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2517 - crypt_salt_len[use_crypt_method] |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2518 - crypt_seed_len[use_crypt_method]); |
7 | 2519 else |
2520 #endif | |
2521 msg_add_lines(c, (long)linecnt, filesize); | |
2522 | |
2523 vim_free(keep_msg); | |
2524 keep_msg = NULL; | |
2525 msg_scrolled_ign = TRUE; | |
2526 #ifdef ALWAYS_USE_GUI | |
2527 /* Don't show the message when reading stdin, it would end up in a | |
2528 * message box (which might be shown when exiting!) */ | |
2529 if (read_stdin || read_buffer) | |
2530 p = msg_may_trunc(FALSE, IObuff); | |
2531 else | |
2532 #endif | |
2533 p = msg_trunc_attr(IObuff, FALSE, 0); | |
2534 if (read_stdin || read_buffer || restart_edit != 0 | |
540 | 2535 || (msg_scrolled != 0 && !need_wait_return)) |
7 | 2536 /* Need to repeat the message after redrawing when: |
2537 * - When reading from stdin (the screen will be cleared next). | |
2538 * - When restart_edit is set (otherwise there will be a delay | |
2539 * before redrawing). | |
2540 * - When the screen was scrolled but there is no wait-return | |
2541 * prompt. */ | |
679 | 2542 set_keep_msg(p, 0); |
7 | 2543 msg_scrolled_ign = FALSE; |
2544 } | |
2545 | |
2546 /* with errors writing the file requires ":w!" */ | |
2547 if (newfile && (error | |
2548 #ifdef FEAT_MBYTE | |
595 | 2549 || conv_error != 0 |
679 | 2550 || (illegal_byte > 0 && bad_char_behavior != BAD_KEEP) |
7 | 2551 #endif |
2552 )) | |
2553 curbuf->b_p_ro = TRUE; | |
2554 | |
2555 u_clearline(); /* cannot use "U" command after adding lines */ | |
2556 | |
2557 /* | |
2558 * In Ex mode: cursor at last new line. | |
2559 * Otherwise: cursor at first new line. | |
2560 */ | |
2561 if (exmode_active) | |
2562 curwin->w_cursor.lnum = from + linecnt; | |
2563 else | |
2564 curwin->w_cursor.lnum = from + 1; | |
2565 check_cursor_lnum(); | |
2566 beginline(BL_WHITE | BL_FIX); /* on first non-blank */ | |
2567 | |
2568 /* | |
2569 * Set '[ and '] marks to the newly read lines. | |
2570 */ | |
2571 curbuf->b_op_start.lnum = from + 1; | |
2572 curbuf->b_op_start.col = 0; | |
2573 curbuf->b_op_end.lnum = from + linecnt; | |
2574 curbuf->b_op_end.col = 0; | |
696 | 2575 |
2576 #ifdef WIN32 | |
2577 /* | |
2578 * Work around a weird problem: When a file has two links (only | |
2579 * possible on NTFS) and we write through one link, then stat() it | |
1651 | 2580 * through the other link, the timestamp information may be wrong. |
696 | 2581 * It's correct again after reading the file, thus reset the timestamp |
2582 * here. | |
2583 */ | |
2584 if (newfile && !read_stdin && !read_buffer | |
2585 && mch_stat((char *)fname, &st) >= 0) | |
2586 { | |
2587 buf_store_time(curbuf, &st, fname); | |
2588 curbuf->b_mtime_read = curbuf->b_mtime; | |
2589 } | |
2590 #endif | |
7 | 2591 } |
2592 msg_scroll = msg_save; | |
2593 | |
2594 #ifdef FEAT_VIMINFO | |
2595 /* | |
2596 * Get the marks before executing autocommands, so they can be used there. | |
2597 */ | |
2598 check_marks_read(); | |
2599 #endif | |
2600 | |
2601 /* | |
2602 * Trick: We remember if the last line of the read didn't have | |
2603 * an eol for when writing it again. This is required for | |
2604 * ":autocmd FileReadPost *.gz set bin|'[,']!gunzip" to work. | |
2605 */ | |
2606 write_no_eol_lnum = read_no_eol_lnum; | |
2607 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2608 /* When reloading a buffer put the cursor at the first line that is |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2609 * different. */ |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2610 if (flags & READ_KEEP_UNDO) |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2611 u_find_first_changed(); |
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
2612 |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2613 #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
|
2614 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2615 * 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
|
2616 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2617 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
|
2618 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2619 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
|
2620 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2621 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
|
2622 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
|
2623 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2624 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
2625 |
167 | 2626 #ifdef FEAT_AUTOCMD |
7 | 2627 if (!read_stdin && !read_buffer) |
2628 { | |
2629 int m = msg_scroll; | |
2630 int n = msg_scrolled; | |
2631 | |
2632 /* Save the fileformat now, otherwise the buffer will be considered | |
2633 * modified if the format/encoding was automatically detected. */ | |
819 | 2634 if (set_options) |
7 | 2635 save_file_ff(curbuf); |
2636 | |
2637 /* | |
2638 * The output from the autocommands should not overwrite anything and | |
2639 * should not be overwritten: Set msg_scroll, restore its value if no | |
2640 * output was done. | |
2641 */ | |
2642 msg_scroll = TRUE; | |
2643 if (filtering) | |
2644 apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname, | |
2645 FALSE, curbuf, eap); | |
2646 else if (newfile) | |
2647 apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname, | |
2648 FALSE, curbuf, eap); | |
2649 else | |
2650 apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname, | |
2651 FALSE, NULL, eap); | |
2652 if (msg_scrolled == n) | |
2653 msg_scroll = m; | |
2654 #ifdef FEAT_EVAL | |
2655 if (aborting()) /* autocmds may abort script processing */ | |
2656 return FAIL; | |
2657 #endif | |
2658 } | |
2659 #endif | |
2660 | |
2661 if (recoverymode && error) | |
2662 return FAIL; | |
2663 return OK; | |
2664 } | |
2665 | |
1313 | 2666 #ifdef OPEN_CHR_FILES |
2667 /* | |
2668 * Returns TRUE if the file name argument is of the form "/dev/fd/\d\+", | |
2669 * which is the name of files used for process substitution output by | |
2670 * some shells on some operating systems, e.g., bash on SunOS. | |
2671 * Do not accept "/dev/fd/[012]", opening these may hang Vim. | |
2672 */ | |
2673 static int | |
2674 is_dev_fd_file(fname) | |
2675 char_u *fname; | |
2676 { | |
2677 return (STRNCMP(fname, "/dev/fd/", 8) == 0 | |
2678 && VIM_ISDIGIT(fname[8]) | |
2679 && *skipdigits(fname + 9) == NUL | |
2680 && (fname[9] != NUL | |
2681 || (fname[8] != '0' && fname[8] != '1' && fname[8] != '2'))); | |
2682 } | |
2683 #endif | |
2684 | |
595 | 2685 #ifdef FEAT_MBYTE |
2686 | |
2687 /* | |
2688 * From the current line count and characters read after that, estimate the | |
2689 * line number where we are now. | |
2690 * Used for error messages that include a line number. | |
2691 */ | |
2692 static linenr_T | |
2693 readfile_linenr(linecnt, p, endp) | |
2694 linenr_T linecnt; /* line count before reading more bytes */ | |
2695 char_u *p; /* start of more bytes read */ | |
2696 char_u *endp; /* end of more bytes read */ | |
2697 { | |
2698 char_u *s; | |
2699 linenr_T lnum; | |
2700 | |
2701 lnum = curbuf->b_ml.ml_line_count - linecnt + 1; | |
2702 for (s = p; s < endp; ++s) | |
2703 if (*s == '\n') | |
2704 ++lnum; | |
2705 return lnum; | |
2706 } | |
2707 #endif | |
2708 | |
7 | 2709 /* |
612 | 2710 * Fill "*eap" to force the 'fileencoding', 'fileformat' and 'binary to be |
2711 * equal to the buffer "buf". Used for calling readfile(). | |
7 | 2712 * Returns OK or FAIL. |
2713 */ | |
2714 int | |
2715 prep_exarg(eap, buf) | |
2716 exarg_T *eap; | |
2717 buf_T *buf; | |
2718 { | |
2719 eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff) | |
2720 #ifdef FEAT_MBYTE | |
2721 + STRLEN(buf->b_p_fenc) | |
2722 #endif | |
2723 + 15)); | |
2724 if (eap->cmd == NULL) | |
2725 return FAIL; | |
2726 | |
2727 #ifdef FEAT_MBYTE | |
2728 sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc); | |
2729 eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff); | |
612 | 2730 eap->bad_char = buf->b_bad_char; |
7 | 2731 #else |
2732 sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff); | |
2733 #endif | |
2734 eap->force_ff = 7; | |
612 | 2735 |
2736 eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN; | |
819 | 2737 eap->read_edit = FALSE; |
612 | 2738 eap->forceit = FALSE; |
7 | 2739 return OK; |
2740 } | |
2741 | |
2742 #ifdef FEAT_MBYTE | |
2743 /* | |
2744 * Find next fileencoding to use from 'fileencodings'. | |
2745 * "pp" points to fenc_next. It's advanced to the next item. | |
2746 * When there are no more items, an empty string is returned and *pp is set to | |
2747 * NULL. | |
2748 * When *pp is not set to NULL, the result is in allocated memory. | |
2749 */ | |
2750 static char_u * | |
2751 next_fenc(pp) | |
2752 char_u **pp; | |
2753 { | |
2754 char_u *p; | |
2755 char_u *r; | |
2756 | |
2757 if (**pp == NUL) | |
2758 { | |
2759 *pp = NULL; | |
2760 return (char_u *)""; | |
2761 } | |
2762 p = vim_strchr(*pp, ','); | |
2763 if (p == NULL) | |
2764 { | |
2765 r = enc_canonize(*pp); | |
2766 *pp += STRLEN(*pp); | |
2767 } | |
2768 else | |
2769 { | |
2770 r = vim_strnsave(*pp, (int)(p - *pp)); | |
2771 *pp = p + 1; | |
2772 if (r != NULL) | |
2773 { | |
2774 p = enc_canonize(r); | |
2775 vim_free(r); | |
2776 r = p; | |
2777 } | |
2778 } | |
2779 if (r == NULL) /* out of memory */ | |
2780 { | |
2781 r = (char_u *)""; | |
2782 *pp = NULL; | |
2783 } | |
2784 return r; | |
2785 } | |
2786 | |
2787 # ifdef FEAT_EVAL | |
2788 /* | |
2789 * Convert a file with the 'charconvert' expression. | |
2790 * This closes the file which is to be read, converts it and opens the | |
2791 * resulting file for reading. | |
2792 * Returns name of the resulting converted file (the caller should delete it | |
2793 * after reading it). | |
2794 * Returns NULL if the conversion failed ("*fdp" is not set) . | |
2795 */ | |
2796 static char_u * | |
2797 readfile_charconvert(fname, fenc, fdp) | |
2798 char_u *fname; /* name of input file */ | |
2799 char_u *fenc; /* converted from */ | |
2800 int *fdp; /* in/out: file descriptor of file */ | |
2801 { | |
2802 char_u *tmpname; | |
2803 char_u *errmsg = NULL; | |
2804 | |
2805 tmpname = vim_tempname('r'); | |
2806 if (tmpname == NULL) | |
2807 errmsg = (char_u *)_("Can't find temp file for conversion"); | |
2808 else | |
2809 { | |
2810 close(*fdp); /* close the input file, ignore errors */ | |
2811 *fdp = -1; | |
2812 if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc, | |
2813 fname, tmpname) == FAIL) | |
2814 errmsg = (char_u *)_("Conversion with 'charconvert' failed"); | |
2815 if (errmsg == NULL && (*fdp = mch_open((char *)tmpname, | |
2816 O_RDONLY | O_EXTRA, 0)) < 0) | |
2817 errmsg = (char_u *)_("can't read output of 'charconvert'"); | |
2818 } | |
2819 | |
2820 if (errmsg != NULL) | |
2821 { | |
2822 /* Don't use emsg(), it breaks mappings, the retry with | |
2823 * another type of conversion might still work. */ | |
2824 MSG(errmsg); | |
2825 if (tmpname != NULL) | |
2826 { | |
2827 mch_remove(tmpname); /* delete converted file */ | |
2828 vim_free(tmpname); | |
2829 tmpname = NULL; | |
2830 } | |
2831 } | |
2832 | |
2833 /* If the input file is closed, open it (caller should check for error). */ | |
2834 if (*fdp < 0) | |
2835 *fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0); | |
2836 | |
2837 return tmpname; | |
2838 } | |
2839 # endif | |
2840 | |
2841 #endif | |
2842 | |
2843 #ifdef FEAT_VIMINFO | |
2844 /* | |
2845 * Read marks for the current buffer from the viminfo file, when we support | |
2846 * buffer marks and the buffer has a name. | |
2847 */ | |
2848 static void | |
2849 check_marks_read() | |
2850 { | |
2851 if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0 | |
2852 && curbuf->b_ffname != NULL) | |
1733 | 2853 read_viminfo(NULL, VIF_WANT_MARKS); |
7 | 2854 |
2855 /* Always set b_marks_read; needed when 'viminfo' is changed to include | |
2856 * the ' parameter after opening a buffer. */ | |
2857 curbuf->b_marks_read = TRUE; | |
2858 } | |
2859 #endif | |
2860 | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2861 #if defined(FEAT_CRYPT) || defined(PROTO) |
7 | 2862 /* |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2863 * 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
|
2864 * 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
|
2865 * 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
|
2866 */ |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2867 static int |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2868 crypt_method_from_magic(ptr, len) |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2869 char *ptr; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2870 int len; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2871 { |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2872 int i; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2873 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2874 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
|
2875 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2876 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
|
2877 continue; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2878 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
|
2879 return i; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2880 } |
2204
c493d6bfde09
A few more changes for encryption. Add test that encrypted file can be read.
Bram Moolenaar <bram@vim.org>
parents:
2180
diff
changeset
|
2881 |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
2882 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
|
2883 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
|
2884 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
|
2885 |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2886 return -1; |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2887 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2888 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2889 /* |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2890 * Check for magic number used for encryption. Applies to the current buffer. |
7 | 2891 * If found, the magic number is removed from ptr[*sizep] and *sizep and |
2892 * *filesizep are updated. | |
2893 * Return the (new) encryption key, NULL for no encryption. | |
2894 */ | |
2895 static char_u * | |
2267 | 2896 check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile, fname, did_ask) |
7 | 2897 char_u *cryptkey; /* previous encryption key or NULL */ |
2898 char_u *ptr; /* pointer to read bytes */ | |
2899 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
|
2900 off_t *filesizep; /* nr of bytes used from file */ |
7 | 2901 int newfile; /* editing a new buffer */ |
2267 | 2902 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
|
2903 int *did_ask; /* flag: whether already asked for key */ |
7 | 2904 { |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2905 int method = crypt_method_from_magic((char *)ptr, *sizep); |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2906 |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2907 if (method >= 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2908 { |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2909 set_crypt_method(curbuf, method); |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2910 if (method > 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2911 (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
|
2912 if (cryptkey == NULL && !*did_ask) |
7 | 2913 { |
2914 if (*curbuf->b_p_key) | |
2915 cryptkey = curbuf->b_p_key; | |
2916 else | |
2917 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2918 /* 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
|
2919 * 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
|
2920 * 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
|
2921 * Happens when retrying to detect encoding. */ |
2267 | 2922 smsg((char_u *)_(need_key_msg), fname); |
2923 msg_scroll = TRUE; | |
7 | 2924 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
|
2925 *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
|
2926 |
7 | 2927 /* check if empty key entered */ |
2928 if (cryptkey != NULL && *cryptkey == NUL) | |
2929 { | |
2930 if (cryptkey != curbuf->b_p_key) | |
2931 vim_free(cryptkey); | |
2932 cryptkey = NULL; | |
2933 } | |
2934 } | |
2935 } | |
2936 | |
2937 if (cryptkey != NULL) | |
2938 { | |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2939 int seed_len = crypt_seed_len[method]; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2940 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
|
2941 |
2267 | 2942 crypt_push_state(); |
2943 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
|
2944 if (method == 0) |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2945 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
|
2946 else |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2947 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2948 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
|
2949 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
|
2950 } |
7 | 2951 |
2952 /* Remove magic number from the text */ | |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2953 *filesizep += CRYPT_MAGIC_LEN + salt_len + seed_len; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2954 *sizep -= CRYPT_MAGIC_LEN + salt_len + seed_len; |
2267 | 2955 mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN + salt_len + seed_len, |
2956 (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
|
2957 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2958 } |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
2959 /* 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
|
2960 * the 'key' option, except when starting up (called with -x argument) */ |
2410
8f6106dd3d12
Fix: editing a not encrypted file after a crypted file messed up reading the
Bram Moolenaar <bram@vim.org>
parents:
2394
diff
changeset
|
2961 else if (newfile && *curbuf->b_p_key != NUL && !starting) |
7 | 2962 set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL); |
2963 | |
2964 return cryptkey; | |
2965 } | |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2966 |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2967 /* |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2968 * 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
|
2969 * 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
|
2970 */ |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2971 int |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2972 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
|
2973 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
|
2974 { |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2975 int method; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2976 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
|
2977 + 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
|
2978 |
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 (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
|
2980 return FAIL; |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2981 method = crypt_method_from_magic((char *)buffer, |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2982 CRYPT_MAGIC_LEN + |
2311
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2281
diff
changeset
|
2983 CRYPT_SEED_LEN_MAX + |
ccda151dde4e
Support completion for ":find". (Nazri Ramliy)
Bram Moolenaar <bram@vim.org>
parents:
2281
diff
changeset
|
2984 CRYPT_SALT_LEN_MAX); |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
2985 if (method < 0 || method != get_crypt_method(curbuf)) |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2986 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
|
2987 |
2267 | 2988 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
|
2989 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
|
2990 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
|
2991 else |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
2992 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2993 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
|
2994 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
|
2995 |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2996 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
|
2997 return FAIL; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
2998 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
|
2999 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
|
3000 } |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3001 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
|
3002 } |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3003 |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3004 /* |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3005 * 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
|
3006 * Returns a pointer to an allocated header of length "*lenp". |
2267 | 3007 * When out of memory returns NULL. |
3008 * 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
|
3009 */ |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3010 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
|
3011 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
|
3012 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
|
3013 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
|
3014 { |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3015 char_u *header; |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3016 int seed_len = crypt_seed_len[get_crypt_method(buf)]; |
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3017 int salt_len = crypt_salt_len[get_crypt_method(buf)]; |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3018 char_u *salt; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3019 char_u *seed; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3020 |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3021 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
|
3022 + 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
|
3023 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
|
3024 { |
2267 | 3025 crypt_push_state(); |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3026 use_crypt_method = get_crypt_method(buf); /* select zip or blowfish */ |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3027 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
|
3028 CRYPT_MAGIC_LEN); |
2360
d8e4b27cef80
Change 'cryptmethod' from a number to a string option. Make it global-local.
Bram Moolenaar <bram@vim.org>
parents:
2311
diff
changeset
|
3029 if (use_crypt_method == 0) |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3030 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
|
3031 else |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3032 { |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3033 /* Using blowfish, add salt and seed. */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3034 salt = header + CRYPT_MAGIC_LEN; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3035 seed = salt + salt_len; |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3036 sha2_seed(salt, salt_len, seed, seed_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3037 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
|
3038 bf_ofb_init(seed, seed_len); |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3039 } |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3040 } |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3041 *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
|
3042 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
|
3043 } |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
3044 |
2265
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3045 #endif /* FEAT_CRYPT */ |
b7cb69ab616d
Added salt to blowfish encryption.
Bram Moolenaar <bram@vim.org>
parents:
2261
diff
changeset
|
3046 |
7 | 3047 #ifdef UNIX |
3048 static void | |
3049 set_file_time(fname, atime, mtime) | |
3050 char_u *fname; | |
3051 time_t atime; /* access time */ | |
3052 time_t mtime; /* modification time */ | |
3053 { | |
3054 # if defined(HAVE_UTIME) && defined(HAVE_UTIME_H) | |
3055 struct utimbuf buf; | |
3056 | |
3057 buf.actime = atime; | |
3058 buf.modtime = mtime; | |
3059 (void)utime((char *)fname, &buf); | |
3060 # else | |
3061 # if defined(HAVE_UTIMES) | |
3062 struct timeval tvp[2]; | |
3063 | |
3064 tvp[0].tv_sec = atime; | |
3065 tvp[0].tv_usec = 0; | |
3066 tvp[1].tv_sec = mtime; | |
3067 tvp[1].tv_usec = 0; | |
3068 # ifdef NeXT | |
3069 (void)utimes((char *)fname, tvp); | |
3070 # else | |
3071 (void)utimes((char *)fname, (const struct timeval *)&tvp); | |
3072 # endif | |
3073 # endif | |
3074 # endif | |
3075 } | |
3076 #endif /* UNIX */ | |
3077 | |
22 | 3078 #if defined(VMS) && !defined(MIN) |
3079 /* Older DECC compiler for VAX doesn't define MIN() */ | |
3080 # define MIN(a, b) ((a) < (b) ? (a) : (b)) | |
3081 #endif | |
3082 | |
7 | 3083 /* |
1303 | 3084 * Return TRUE if a file appears to be read-only from the file permissions. |
3085 */ | |
3086 int | |
3087 check_file_readonly(fname, perm) | |
3088 char_u *fname; /* full path to file */ | |
3089 int perm; /* known permissions on file */ | |
3090 { | |
3091 #ifndef USE_MCH_ACCESS | |
3092 int fd = 0; | |
3093 #endif | |
3094 | |
3095 return ( | |
3096 #ifdef USE_MCH_ACCESS | |
3097 # ifdef UNIX | |
3098 (perm & 0222) == 0 || | |
3099 # endif | |
3100 mch_access((char *)fname, W_OK) | |
3101 #else | |
3102 (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0 | |
3103 ? TRUE : (close(fd), FALSE) | |
3104 #endif | |
3105 ); | |
3106 } | |
3107 | |
3108 | |
3109 /* | |
589 | 3110 * buf_write() - write to file "fname" lines "start" through "end" |
7 | 3111 * |
3112 * We do our own buffering here because fwrite() is so slow. | |
3113 * | |
589 | 3114 * If "forceit" is true, we don't care for errors when attempting backups. |
3115 * In case of an error everything possible is done to restore the original | |
1698 | 3116 * file. But when "forceit" is TRUE, we risk losing it. |
589 | 3117 * |
3118 * When "reset_changed" is TRUE and "append" == FALSE and "start" == 1 and | |
3119 * "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. | |
7 | 3120 * |
3121 * This function must NOT use NameBuff (because it's called by autowrite()). | |
3122 * | |
3123 * return FAIL for failure, OK otherwise | |
3124 */ | |
3125 int | |
3126 buf_write(buf, fname, sfname, start, end, eap, append, forceit, | |
3127 reset_changed, filtering) | |
3128 buf_T *buf; | |
3129 char_u *fname; | |
3130 char_u *sfname; | |
3131 linenr_T start, end; | |
3132 exarg_T *eap; /* for forced 'ff' and 'fenc', can be | |
3133 NULL! */ | |
589 | 3134 int append; /* append to the file */ |
7 | 3135 int forceit; |
3136 int reset_changed; | |
3137 int filtering; | |
3138 { | |
3139 int fd; | |
3140 char_u *backup = NULL; | |
3141 int backup_copy = FALSE; /* copy the original file? */ | |
3142 int dobackup; | |
3143 char_u *ffname; | |
3144 char_u *wfname = NULL; /* name of file to write to */ | |
3145 char_u *s; | |
3146 char_u *ptr; | |
3147 char_u c; | |
3148 int len; | |
3149 linenr_T lnum; | |
3150 long nchars; | |
3151 char_u *errmsg = NULL; | |
1947 | 3152 int errmsg_allocated = FALSE; |
7 | 3153 char_u *errnum = NULL; |
3154 char_u *buffer; | |
3155 char_u smallbuf[SMBUFSIZE]; | |
3156 char_u *backup_ext; | |
3157 int bufsize; | |
3158 long perm; /* file permissions */ | |
3159 int retval = OK; | |
3160 int newfile = FALSE; /* TRUE if file doesn't exist yet */ | |
3161 int msg_save = msg_scroll; | |
3162 int overwriting; /* TRUE if writing over original */ | |
3163 int no_eol = FALSE; /* no end-of-line written */ | |
3164 int device = FALSE; /* writing to a device */ | |
3165 struct stat st_old; | |
3166 int prev_got_int = got_int; | |
3167 int file_readonly = FALSE; /* overwritten file is read-only */ | |
3168 static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; | |
3169 #if defined(UNIX) || defined(__EMX__XX) /*XXX fix me sometime? */ | |
3170 int made_writable = FALSE; /* 'w' bit has been set */ | |
3171 #endif | |
3172 /* writing everything */ | |
3173 int whole = (start == 1 && end == buf->b_ml.ml_line_count); | |
3174 #ifdef FEAT_AUTOCMD | |
3175 linenr_T old_line_count = buf->b_ml.ml_line_count; | |
3176 #endif | |
3177 int attr; | |
3178 int fileformat; | |
3179 int write_bin; | |
3180 struct bw_info write_info; /* info for buf_write_bytes() */ | |
3181 #ifdef FEAT_MBYTE | |
3182 int converted = FALSE; | |
3183 int notconverted = FALSE; | |
3184 char_u *fenc; /* effective 'fileencoding' */ | |
3185 char_u *fenc_tofree = NULL; /* allocated "fenc" */ | |
3186 #endif | |
3187 #ifdef HAS_BW_FLAGS | |
3188 int wb_flags = 0; | |
3189 #endif | |
3190 #ifdef HAVE_ACL | |
3191 vim_acl_T acl = NULL; /* ACL copied from original file to | |
3192 backup or new file */ | |
3193 #endif | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
3194 #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
|
3195 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
|
3196 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
|
3197 #endif |
7 | 3198 |
3199 if (fname == NULL || *fname == NUL) /* safety check */ | |
3200 return FAIL; | |
2028 | 3201 if (buf->b_ml.ml_mfp == NULL) |
3202 { | |
3203 /* This can happen during startup when there is a stray "w" in the | |
3204 * vimrc file. */ | |
3205 EMSG(_(e_emptybuf)); | |
3206 return FAIL; | |
3207 } | |
7 | 3208 |
3209 /* | |
3210 * Disallow writing from .exrc and .vimrc in current directory for | |
3211 * security reasons. | |
3212 */ | |
3213 if (check_secure()) | |
3214 return FAIL; | |
3215 | |
3216 /* Avoid a crash for a long name. */ | |
3217 if (STRLEN(fname) >= MAXPATHL) | |
3218 { | |
3219 EMSG(_(e_longname)); | |
3220 return FAIL; | |
3221 } | |
3222 | |
3223 #ifdef FEAT_MBYTE | |
3224 /* must init bw_conv_buf and bw_iconv_fd before jumping to "fail" */ | |
3225 write_info.bw_conv_buf = NULL; | |
3226 write_info.bw_conv_error = FALSE; | |
1947 | 3227 write_info.bw_conv_error_lnum = 0; |
7 | 3228 write_info.bw_restlen = 0; |
3229 # ifdef USE_ICONV | |
3230 write_info.bw_iconv_fd = (iconv_t)-1; | |
3231 # endif | |
3232 #endif | |
3233 | |
167 | 3234 /* After writing a file changedtick changes but we don't want to display |
3235 * the line. */ | |
3236 ex_no_reprint = TRUE; | |
3237 | |
7 | 3238 /* |
3239 * If there is no file name yet, use the one for the written file. | |
3240 * BF_NOTEDITED is set to reflect this (in case the write fails). | |
3241 * Don't do this when the write is for a filter command. | |
589 | 3242 * Don't do this when appending. |
3243 * Only do this when 'cpoptions' contains the 'F' flag. | |
7 | 3244 */ |
633 | 3245 if (buf->b_ffname == NULL |
3246 && reset_changed | |
7 | 3247 && whole |
3248 && buf == curbuf | |
236 | 3249 #ifdef FEAT_QUICKFIX |
3250 && !bt_nofile(buf) | |
3251 #endif | |
7 | 3252 && !filtering |
589 | 3253 && (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL) |
7 | 3254 && vim_strchr(p_cpo, CPO_FNAMEW) != NULL) |
3255 { | |
633 | 3256 if (set_rw_fname(fname, sfname) == FAIL) |
7 | 3257 return FAIL; |
633 | 3258 buf = curbuf; /* just in case autocmds made "buf" invalid */ |
7 | 3259 } |
3260 | |
3261 if (sfname == NULL) | |
3262 sfname = fname; | |
3263 /* | |
3264 * For Unix: Use the short file name whenever possible. | |
3265 * Avoids problems with networks and when directory names are changed. | |
3266 * Don't do this for MS-DOS, a "cd" in a sub-shell may have moved us to | |
3267 * another directory, which we don't detect | |
3268 */ | |
3269 ffname = fname; /* remember full fname */ | |
3270 #ifdef UNIX | |
3271 fname = sfname; | |
3272 #endif | |
3273 | |
3274 if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0) | |
3275 overwriting = TRUE; | |
3276 else | |
3277 overwriting = FALSE; | |
3278 | |
3279 if (exiting) | |
3280 settmode(TMODE_COOK); /* when exiting allow typahead now */ | |
3281 | |
3282 ++no_wait_return; /* don't wait for return yet */ | |
3283 | |
3284 /* | |
3285 * Set '[ and '] marks to the lines to be written. | |
3286 */ | |
3287 buf->b_op_start.lnum = start; | |
3288 buf->b_op_start.col = 0; | |
3289 buf->b_op_end.lnum = end; | |
3290 buf->b_op_end.col = 0; | |
3291 | |
3292 #ifdef FEAT_AUTOCMD | |
3293 { | |
3294 aco_save_T aco; | |
3295 int buf_ffname = FALSE; | |
3296 int buf_sfname = FALSE; | |
3297 int buf_fname_f = FALSE; | |
3298 int buf_fname_s = FALSE; | |
3299 int did_cmd = FALSE; | |
17 | 3300 int nofile_err = FALSE; |
161 | 3301 int empty_memline = (buf->b_ml.ml_mfp == NULL); |
7 | 3302 |
3303 /* | |
3304 * Apply PRE aucocommands. | |
3305 * Set curbuf to the buffer to be written. | |
3306 * Careful: The autocommands may call buf_write() recursively! | |
3307 */ | |
3308 if (ffname == buf->b_ffname) | |
3309 buf_ffname = TRUE; | |
3310 if (sfname == buf->b_sfname) | |
3311 buf_sfname = TRUE; | |
3312 if (fname == buf->b_ffname) | |
3313 buf_fname_f = TRUE; | |
3314 if (fname == buf->b_sfname) | |
3315 buf_fname_s = TRUE; | |
3316 | |
3317 /* set curwin/curbuf to buf and save a few things */ | |
3318 aucmd_prepbuf(&aco, buf); | |
3319 | |
3320 if (append) | |
3321 { | |
3322 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, | |
3323 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3324 { |
635 | 3325 #ifdef FEAT_QUICKFIX |
18 | 3326 if (overwriting && bt_nofile(curbuf)) |
17 | 3327 nofile_err = TRUE; |
3328 else | |
635 | 3329 #endif |
17 | 3330 apply_autocmds_exarg(EVENT_FILEAPPENDPRE, |
7 | 3331 sfname, sfname, FALSE, curbuf, eap); |
17 | 3332 } |
7 | 3333 } |
3334 else if (filtering) | |
3335 { | |
3336 apply_autocmds_exarg(EVENT_FILTERWRITEPRE, | |
3337 NULL, sfname, FALSE, curbuf, eap); | |
3338 } | |
3339 else if (reset_changed && whole) | |
3340 { | |
3341 if (!(did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, | |
3342 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3343 { |
635 | 3344 #ifdef FEAT_QUICKFIX |
179 | 3345 if (overwriting && bt_nofile(curbuf)) |
17 | 3346 nofile_err = TRUE; |
3347 else | |
635 | 3348 #endif |
17 | 3349 apply_autocmds_exarg(EVENT_BUFWRITEPRE, |
7 | 3350 sfname, sfname, FALSE, curbuf, eap); |
17 | 3351 } |
7 | 3352 } |
3353 else | |
3354 { | |
3355 if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, | |
3356 sfname, sfname, FALSE, curbuf, eap))) | |
17 | 3357 { |
635 | 3358 #ifdef FEAT_QUICKFIX |
179 | 3359 if (overwriting && bt_nofile(curbuf)) |
17 | 3360 nofile_err = TRUE; |
3361 else | |
635 | 3362 #endif |
17 | 3363 apply_autocmds_exarg(EVENT_FILEWRITEPRE, |
7 | 3364 sfname, sfname, FALSE, curbuf, eap); |
17 | 3365 } |
7 | 3366 } |
3367 | |
3368 /* restore curwin/curbuf and a few other things */ | |
3369 aucmd_restbuf(&aco); | |
3370 | |
3371 /* | |
3372 * In three situations we return here and don't write the file: | |
3373 * 1. the autocommands deleted or unloaded the buffer. | |
3374 * 2. The autocommands abort script processing. | |
3375 * 3. If one of the "Cmd" autocommands was executed. | |
3376 */ | |
3377 if (!buf_valid(buf)) | |
3378 buf = NULL; | |
161 | 3379 if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) |
532 | 3380 || did_cmd || nofile_err |
3381 #ifdef FEAT_EVAL | |
3382 || aborting() | |
3383 #endif | |
3384 ) | |
7 | 3385 { |
3386 --no_wait_return; | |
3387 msg_scroll = msg_save; | |
17 | 3388 if (nofile_err) |
3389 EMSG(_("E676: No matching autocommands for acwrite buffer")); | |
3390 | |
532 | 3391 if (nofile_err |
3392 #ifdef FEAT_EVAL | |
3393 || aborting() | |
3394 #endif | |
3395 ) | |
7 | 3396 /* An aborting error, interrupt or exception in the |
3397 * autocommands. */ | |
3398 return FAIL; | |
3399 if (did_cmd) | |
3400 { | |
3401 if (buf == NULL) | |
3402 /* The buffer was deleted. We assume it was written | |
3403 * (can't retry anyway). */ | |
3404 return OK; | |
3405 if (overwriting) | |
3406 { | |
3407 /* Assume the buffer was written, update the timestamp. */ | |
3408 ml_timestamp(buf); | |
589 | 3409 if (append) |
3410 buf->b_flags &= ~BF_NEW; | |
3411 else | |
3412 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 3413 } |
589 | 3414 if (reset_changed && buf->b_changed && !append |
39 | 3415 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) |
7 | 3416 /* Buffer still changed, the autocommands didn't work |
3417 * properly. */ | |
3418 return FAIL; | |
3419 return OK; | |
3420 } | |
3421 #ifdef FEAT_EVAL | |
3422 if (!aborting()) | |
3423 #endif | |
3424 EMSG(_("E203: Autocommands deleted or unloaded buffer to be written")); | |
3425 return FAIL; | |
3426 } | |
3427 | |
3428 /* | |
3429 * The autocommands may have changed the number of lines in the file. | |
3430 * When writing the whole file, adjust the end. | |
3431 * When writing part of the file, assume that the autocommands only | |
3432 * changed the number of lines that are to be written (tricky!). | |
3433 */ | |
3434 if (buf->b_ml.ml_line_count != old_line_count) | |
3435 { | |
3436 if (whole) /* write all */ | |
3437 end = buf->b_ml.ml_line_count; | |
3438 else if (buf->b_ml.ml_line_count > old_line_count) /* more lines */ | |
3439 end += buf->b_ml.ml_line_count - old_line_count; | |
3440 else /* less lines */ | |
3441 { | |
3442 end -= old_line_count - buf->b_ml.ml_line_count; | |
3443 if (end < start) | |
3444 { | |
3445 --no_wait_return; | |
3446 msg_scroll = msg_save; | |
3447 EMSG(_("E204: Autocommand changed number of lines in unexpected way")); | |
3448 return FAIL; | |
3449 } | |
3450 } | |
3451 } | |
3452 | |
3453 /* | |
3454 * The autocommands may have changed the name of the buffer, which may | |
3455 * be kept in fname, ffname and sfname. | |
3456 */ | |
3457 if (buf_ffname) | |
3458 ffname = buf->b_ffname; | |
3459 if (buf_sfname) | |
3460 sfname = buf->b_sfname; | |
3461 if (buf_fname_f) | |
3462 fname = buf->b_ffname; | |
3463 if (buf_fname_s) | |
3464 fname = buf->b_sfname; | |
3465 } | |
3466 #endif | |
3467 | |
3468 #ifdef FEAT_NETBEANS_INTG | |
2210 | 3469 if (netbeans_active() && isNetbeansBuffer(buf)) |
7 | 3470 { |
3471 if (whole) | |
3472 { | |
3473 /* | |
3474 * b_changed can be 0 after an undo, but we still need to write | |
3475 * the buffer to NetBeans. | |
3476 */ | |
3477 if (buf->b_changed || isNetbeansModified(buf)) | |
3478 { | |
33 | 3479 --no_wait_return; /* may wait for return now */ |
3480 msg_scroll = msg_save; | |
3481 netbeans_save_buffer(buf); /* no error checking... */ | |
7 | 3482 return retval; |
3483 } | |
3484 else | |
3485 { | |
3486 errnum = (char_u *)"E656: "; | |
1653 | 3487 errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers"); |
7 | 3488 buffer = NULL; |
3489 goto fail; | |
3490 } | |
3491 } | |
3492 else | |
3493 { | |
3494 errnum = (char_u *)"E657: "; | |
3495 errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers"); | |
3496 buffer = NULL; | |
3497 goto fail; | |
3498 } | |
3499 } | |
3500 #endif | |
3501 | |
3502 if (shortmess(SHM_OVER) && !exiting) | |
3503 msg_scroll = FALSE; /* overwrite previous file message */ | |
3504 else | |
3505 msg_scroll = TRUE; /* don't overwrite previous file message */ | |
3506 if (!filtering) | |
3507 filemess(buf, | |
3508 #ifndef UNIX | |
3509 sfname, | |
3510 #else | |
3511 fname, | |
3512 #endif | |
3513 (char_u *)"", 0); /* show that we are busy */ | |
3514 msg_scroll = FALSE; /* always overwrite the file message now */ | |
3515 | |
3516 buffer = alloc(BUFSIZE); | |
3517 if (buffer == NULL) /* can't allocate big buffer, use small | |
3518 * one (to be able to write when out of | |
3519 * memory) */ | |
3520 { | |
3521 buffer = smallbuf; | |
3522 bufsize = SMBUFSIZE; | |
3523 } | |
3524 else | |
3525 bufsize = BUFSIZE; | |
3526 | |
3527 /* | |
3528 * Get information about original file (if there is one). | |
3529 */ | |
3530 #if defined(UNIX) && !defined(ARCHIE) | |
1438 | 3531 st_old.st_dev = 0; |
3532 st_old.st_ino = 0; | |
7 | 3533 perm = -1; |
3534 if (mch_stat((char *)fname, &st_old) < 0) | |
3535 newfile = TRUE; | |
3536 else | |
3537 { | |
3538 perm = st_old.st_mode; | |
3539 if (!S_ISREG(st_old.st_mode)) /* not a file */ | |
3540 { | |
3541 if (S_ISDIR(st_old.st_mode)) | |
3542 { | |
3543 errnum = (char_u *)"E502: "; | |
3544 errmsg = (char_u *)_("is a directory"); | |
3545 goto fail; | |
3546 } | |
3547 if (mch_nodetype(fname) != NODE_WRITABLE) | |
3548 { | |
3549 errnum = (char_u *)"E503: "; | |
3550 errmsg = (char_u *)_("is not a file or writable device"); | |
3551 goto fail; | |
3552 } | |
3553 /* It's a device of some kind (or a fifo) which we can write to | |
3554 * but for which we can't make a backup. */ | |
3555 device = TRUE; | |
3556 newfile = TRUE; | |
3557 perm = -1; | |
3558 } | |
3559 } | |
3560 #else /* !UNIX */ | |
3561 /* | |
3562 * Check for a writable device name. | |
3563 */ | |
3564 c = mch_nodetype(fname); | |
3565 if (c == NODE_OTHER) | |
3566 { | |
3567 errnum = (char_u *)"E503: "; | |
3568 errmsg = (char_u *)_("is not a file or writable device"); | |
3569 goto fail; | |
3570 } | |
3571 if (c == NODE_WRITABLE) | |
3572 { | |
1004 | 3573 # if defined(MSDOS) || defined(MSWIN) || defined(OS2) |
3574 /* MS-Windows allows opening a device, but we will probably get stuck | |
3575 * trying to write to it. */ | |
3576 if (!p_odev) | |
3577 { | |
3578 errnum = (char_u *)"E796: "; | |
3579 errmsg = (char_u *)_("writing to device disabled with 'opendevice' option"); | |
3580 goto fail; | |
3581 } | |
3582 # endif | |
7 | 3583 device = TRUE; |
3584 newfile = TRUE; | |
3585 perm = -1; | |
3586 } | |
3587 else | |
3588 { | |
3589 perm = mch_getperm(fname); | |
3590 if (perm < 0) | |
3591 newfile = TRUE; | |
3592 else if (mch_isdir(fname)) | |
3593 { | |
3594 errnum = (char_u *)"E502: "; | |
3595 errmsg = (char_u *)_("is a directory"); | |
3596 goto fail; | |
3597 } | |
3598 if (overwriting) | |
3599 (void)mch_stat((char *)fname, &st_old); | |
3600 } | |
3601 #endif /* !UNIX */ | |
3602 | |
3603 if (!device && !newfile) | |
3604 { | |
3605 /* | |
3606 * Check if the file is really writable (when renaming the file to | |
3607 * make a backup we won't discover it later). | |
3608 */ | |
1303 | 3609 file_readonly = check_file_readonly(fname, (int)perm); |
3610 | |
7 | 3611 if (!forceit && file_readonly) |
3612 { | |
3613 if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
3614 { | |
3615 errnum = (char_u *)"E504: "; | |
3616 errmsg = (char_u *)_(err_readonly); | |
3617 } | |
3618 else | |
3619 { | |
3620 errnum = (char_u *)"E505: "; | |
3621 errmsg = (char_u *)_("is read-only (add ! to override)"); | |
3622 } | |
3623 goto fail; | |
3624 } | |
3625 | |
3626 /* | |
3627 * Check if the timestamp hasn't changed since reading the file. | |
3628 */ | |
3629 if (overwriting) | |
3630 { | |
3631 retval = check_mtime(buf, &st_old); | |
3632 if (retval == FAIL) | |
3633 goto fail; | |
3634 } | |
3635 } | |
3636 | |
3637 #ifdef HAVE_ACL | |
3638 /* | |
3639 * For systems that support ACL: get the ACL from the original file. | |
3640 */ | |
3641 if (!newfile) | |
3642 acl = mch_get_acl(fname); | |
3643 #endif | |
3644 | |
3645 /* | |
3646 * If 'backupskip' is not empty, don't make a backup for some files. | |
3647 */ | |
3648 dobackup = (p_wb || p_bk || *p_pm != NUL); | |
3649 #ifdef FEAT_WILDIGN | |
3650 if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) | |
3651 dobackup = FALSE; | |
3652 #endif | |
3653 | |
3654 /* | |
3655 * Save the value of got_int and reset it. We don't want a previous | |
3656 * interruption cancel writing, only hitting CTRL-C while writing should | |
3657 * abort it. | |
3658 */ | |
3659 prev_got_int = got_int; | |
3660 got_int = FALSE; | |
3661 | |
3662 /* Mark the buffer as 'being saved' to prevent changed buffer warnings */ | |
3663 buf->b_saving = TRUE; | |
3664 | |
3665 /* | |
3666 * If we are not appending or filtering, the file exists, and the | |
3667 * 'writebackup', 'backup' or 'patchmode' option is set, need a backup. | |
3668 * When 'patchmode' is set also make a backup when appending. | |
3669 * | |
3670 * Do not make any backup, if 'writebackup' and 'backup' are both switched | |
3671 * off. This helps when editing large files on almost-full disks. | |
3672 */ | |
3673 if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) | |
3674 { | |
3675 #if defined(UNIX) || defined(WIN32) | |
3676 struct stat st; | |
3677 #endif | |
3678 | |
3679 if ((bkc_flags & BKC_YES) || append) /* "yes" */ | |
3680 backup_copy = TRUE; | |
3681 #if defined(UNIX) || defined(WIN32) | |
3682 else if ((bkc_flags & BKC_AUTO)) /* "auto" */ | |
3683 { | |
3684 int i; | |
3685 | |
3686 # ifdef UNIX | |
3687 /* | |
3688 * Don't rename the file when: | |
3689 * - it's a hard link | |
3690 * - it's a symbolic link | |
3691 * - we don't have write permission in the directory | |
3692 * - we can't set the owner/group of the new file | |
3693 */ | |
3694 if (st_old.st_nlink > 1 | |
3695 || mch_lstat((char *)fname, &st) < 0 | |
3696 || st.st_dev != st_old.st_dev | |
557 | 3697 || st.st_ino != st_old.st_ino |
3698 # ifndef HAVE_FCHOWN | |
3699 || st.st_uid != st_old.st_uid | |
3700 || st.st_gid != st_old.st_gid | |
3701 # endif | |
3702 ) | |
7 | 3703 backup_copy = TRUE; |
3704 else | |
696 | 3705 # else |
3706 # ifdef WIN32 | |
3707 /* On NTFS file systems hard links are possible. */ | |
3708 if (mch_is_linked(fname)) | |
3709 backup_copy = TRUE; | |
3710 else | |
3711 # endif | |
7 | 3712 # endif |
3713 { | |
3714 /* | |
3715 * Check if we can create a file and set the owner/group to | |
3716 * the ones from the original file. | |
3717 * First find a file name that doesn't exist yet (use some | |
3718 * arbitrary numbers). | |
3719 */ | |
3720 STRCPY(IObuff, fname); | |
3721 for (i = 4913; ; i += 123) | |
3722 { | |
3723 sprintf((char *)gettail(IObuff), "%d", i); | |
557 | 3724 if (mch_lstat((char *)IObuff, &st) < 0) |
7 | 3725 break; |
3726 } | |
557 | 3727 fd = mch_open((char *)IObuff, |
3728 O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); | |
7 | 3729 if (fd < 0) /* can't write in directory */ |
3730 backup_copy = TRUE; | |
3731 else | |
3732 { | |
3733 # ifdef UNIX | |
557 | 3734 # ifdef HAVE_FCHOWN |
1757 | 3735 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 3736 # endif |
7 | 3737 if (mch_stat((char *)IObuff, &st) < 0 |
3738 || st.st_uid != st_old.st_uid | |
3739 || st.st_gid != st_old.st_gid | |
1877 | 3740 || (long)st.st_mode != perm) |
7 | 3741 backup_copy = TRUE; |
3742 # endif | |
567 | 3743 /* Close the file before removing it, on MS-Windows we |
3744 * can't delete an open file. */ | |
3745 close(fd); | |
7 | 3746 mch_remove(IObuff); |
2523
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3747 # ifdef MSWIN |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3748 /* MS-Windows may trigger a virus scanner to open the |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3749 * file, we can't delete it then. Keep trying for half a |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3750 * second. */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3751 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3752 int try; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3753 |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3754 for (try = 0; try < 10; ++try) |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3755 { |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3756 if (mch_lstat((char *)IObuff, &st) < 0) |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3757 break; |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3758 ui_delay(50L, TRUE); /* wait 50 msec */ |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3759 mch_remove(IObuff); |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3760 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3761 } |
0dffdd8f8871
Fixed: on MS-Windows sometimes files with number 4913 or higher are left
Bram Moolenaar <bram@vim.org>
parents:
2482
diff
changeset
|
3762 # endif |
7 | 3763 } |
3764 } | |
3765 } | |
3766 | |
3767 # ifdef UNIX | |
3768 /* | |
3769 * Break symlinks and/or hardlinks if we've been asked to. | |
3770 */ | |
3771 if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK)) | |
3772 { | |
3773 int lstat_res; | |
3774 | |
3775 lstat_res = mch_lstat((char *)fname, &st); | |
3776 | |
3777 /* Symlinks. */ | |
3778 if ((bkc_flags & BKC_BREAKSYMLINK) | |
3779 && lstat_res == 0 | |
3780 && st.st_ino != st_old.st_ino) | |
3781 backup_copy = FALSE; | |
3782 | |
3783 /* Hardlinks. */ | |
3784 if ((bkc_flags & BKC_BREAKHARDLINK) | |
3785 && st_old.st_nlink > 1 | |
3786 && (lstat_res != 0 || st.st_ino == st_old.st_ino)) | |
3787 backup_copy = FALSE; | |
3788 } | |
3789 #endif | |
3790 | |
3791 #endif | |
3792 | |
3793 /* make sure we have a valid backup extension to use */ | |
3794 if (*p_bex == NUL) | |
3795 { | |
3796 #ifdef RISCOS | |
3797 backup_ext = (char_u *)"/bak"; | |
3798 #else | |
3799 backup_ext = (char_u *)".bak"; | |
3800 #endif | |
3801 } | |
3802 else | |
3803 backup_ext = p_bex; | |
3804 | |
3805 if (backup_copy | |
3806 && (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0) | |
3807 { | |
3808 int bfd; | |
3809 char_u *copybuf, *wp; | |
3810 int some_error = FALSE; | |
3811 struct stat st_new; | |
3812 char_u *dirp; | |
3813 char_u *rootname; | |
344 | 3814 #if defined(UNIX) && !defined(SHORT_FNAME) |
7 | 3815 int did_set_shortname; |
3816 #endif | |
3817 | |
3818 copybuf = alloc(BUFSIZE + 1); | |
3819 if (copybuf == NULL) | |
3820 { | |
3821 some_error = TRUE; /* out of memory */ | |
3822 goto nobackup; | |
3823 } | |
3824 | |
3825 /* | |
3826 * Try to make the backup in each directory in the 'bdir' option. | |
3827 * | |
3828 * Unix semantics has it, that we may have a writable file, | |
3829 * that cannot be recreated with a simple open(..., O_CREAT, ) e.g: | |
3830 * - the directory is not writable, | |
3831 * - the file may be a symbolic link, | |
3832 * - the file may belong to another user/group, etc. | |
3833 * | |
3834 * For these reasons, the existing writable file must be truncated | |
3835 * and reused. Creation of a backup COPY will be attempted. | |
3836 */ | |
3837 dirp = p_bdir; | |
3838 while (*dirp) | |
3839 { | |
3840 #ifdef UNIX | |
3841 st_new.st_ino = 0; | |
3842 st_new.st_dev = 0; | |
3843 st_new.st_gid = 0; | |
3844 #endif | |
3845 | |
3846 /* | |
3847 * Isolate one directory name, using an entry in 'bdir'. | |
3848 */ | |
3849 (void)copy_option_part(&dirp, copybuf, BUFSIZE, ","); | |
3850 rootname = get_file_in_dir(fname, copybuf); | |
3851 if (rootname == NULL) | |
3852 { | |
3853 some_error = TRUE; /* out of memory */ | |
3854 goto nobackup; | |
3855 } | |
3856 | |
344 | 3857 #if defined(UNIX) && !defined(SHORT_FNAME) |
7 | 3858 did_set_shortname = FALSE; |
3859 #endif | |
3860 | |
3861 /* | |
3862 * May try twice if 'shortname' not set. | |
3863 */ | |
3864 for (;;) | |
3865 { | |
3866 /* | |
3867 * Make backup file name. | |
3868 */ | |
3869 backup = buf_modname( | |
3870 #ifdef SHORT_FNAME | |
3871 TRUE, | |
3872 #else | |
3873 (buf->b_p_sn || buf->b_shortname), | |
3874 #endif | |
3875 rootname, backup_ext, FALSE); | |
3876 if (backup == NULL) | |
3877 { | |
3878 vim_free(rootname); | |
3879 some_error = TRUE; /* out of memory */ | |
3880 goto nobackup; | |
3881 } | |
3882 | |
3883 /* | |
3884 * Check if backup file already exists. | |
3885 */ | |
3886 if (mch_stat((char *)backup, &st_new) >= 0) | |
3887 { | |
3888 #ifdef UNIX | |
3889 /* | |
3890 * Check if backup file is same as original file. | |
3891 * May happen when modname() gave the same file back. | |
3892 * E.g. silly link, or file name-length reached. | |
3893 * If we don't check here, we either ruin the file | |
3894 * when copying or erase it after writing. jw. | |
3895 */ | |
3896 if (st_new.st_dev == st_old.st_dev | |
3897 && st_new.st_ino == st_old.st_ino) | |
3898 { | |
3899 vim_free(backup); | |
3900 backup = NULL; /* no backup file to delete */ | |
3901 # ifndef SHORT_FNAME | |
3902 /* | |
3903 * may try again with 'shortname' set | |
3904 */ | |
3905 if (!(buf->b_shortname || buf->b_p_sn)) | |
3906 { | |
3907 buf->b_shortname = TRUE; | |
3908 did_set_shortname = TRUE; | |
3909 continue; | |
3910 } | |
3911 /* setting shortname didn't help */ | |
3912 if (did_set_shortname) | |
3913 buf->b_shortname = FALSE; | |
3914 # endif | |
3915 break; | |
3916 } | |
3917 #endif | |
3918 | |
3919 /* | |
3920 * If we are not going to keep the backup file, don't | |
3921 * delete an existing one, try to use another name. | |
3922 * Change one character, just before the extension. | |
3923 */ | |
3924 if (!p_bk) | |
3925 { | |
3926 wp = backup + STRLEN(backup) - 1 | |
3927 - STRLEN(backup_ext); | |
3928 if (wp < backup) /* empty file name ??? */ | |
3929 wp = backup; | |
3930 *wp = 'z'; | |
3931 while (*wp > 'a' | |
3932 && mch_stat((char *)backup, &st_new) >= 0) | |
3933 --*wp; | |
3934 /* They all exist??? Must be something wrong. */ | |
3935 if (*wp == 'a') | |
3936 { | |
3937 vim_free(backup); | |
3938 backup = NULL; | |
3939 } | |
3940 } | |
3941 } | |
3942 break; | |
3943 } | |
3944 vim_free(rootname); | |
3945 | |
3946 /* | |
3947 * Try to create the backup file | |
3948 */ | |
3949 if (backup != NULL) | |
3950 { | |
3951 /* remove old backup, if present */ | |
3952 mch_remove(backup); | |
3953 /* Open with O_EXCL to avoid the file being created while | |
3954 * we were sleeping (symlink hacker attack?) */ | |
3955 bfd = mch_open((char *)backup, | |
557 | 3956 O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW, |
3957 perm & 0777); | |
7 | 3958 if (bfd < 0) |
3959 { | |
3960 vim_free(backup); | |
3961 backup = NULL; | |
3962 } | |
3963 else | |
3964 { | |
3965 /* set file protection same as original file, but | |
3966 * strip s-bit */ | |
3967 (void)mch_setperm(backup, perm & 0777); | |
3968 | |
3969 #ifdef UNIX | |
3970 /* | |
3971 * Try to set the group of the backup same as the | |
3972 * original file. If this fails, set the protection | |
3973 * bits for the group same as the protection bits for | |
3974 * others. | |
3975 */ | |
557 | 3976 if (st_new.st_gid != st_old.st_gid |
7 | 3977 # ifdef HAVE_FCHOWN /* sequent-ptx lacks fchown() */ |
557 | 3978 && fchown(bfd, (uid_t)-1, st_old.st_gid) != 0 |
7 | 3979 # endif |
3980 ) | |
3981 mch_setperm(backup, | |
3982 (perm & 0707) | ((perm & 07) << 3)); | |
1583 | 3983 # ifdef HAVE_SELINUX |
3984 mch_copy_sec(fname, backup); | |
3985 # endif | |
7 | 3986 #endif |
3987 | |
3988 /* | |
3989 * copy the file. | |
3990 */ | |
3991 write_info.bw_fd = bfd; | |
3992 write_info.bw_buf = copybuf; | |
3993 #ifdef HAS_BW_FLAGS | |
3994 write_info.bw_flags = FIO_NOCONVERT; | |
3995 #endif | |
3996 while ((write_info.bw_len = vim_read(fd, copybuf, | |
3997 BUFSIZE)) > 0) | |
3998 { | |
3999 if (buf_write_bytes(&write_info) == FAIL) | |
4000 { | |
4001 errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)"); | |
4002 break; | |
4003 } | |
4004 ui_breakcheck(); | |
4005 if (got_int) | |
4006 { | |
4007 errmsg = (char_u *)_(e_interr); | |
4008 break; | |
4009 } | |
4010 } | |
4011 | |
4012 if (close(bfd) < 0 && errmsg == NULL) | |
4013 errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)"); | |
4014 if (write_info.bw_len < 0) | |
4015 errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)"); | |
4016 #ifdef UNIX | |
4017 set_file_time(backup, st_old.st_atime, st_old.st_mtime); | |
4018 #endif | |
4019 #ifdef HAVE_ACL | |
4020 mch_set_acl(backup, acl); | |
4021 #endif | |
1583 | 4022 #ifdef HAVE_SELINUX |
4023 mch_copy_sec(fname, backup); | |
4024 #endif | |
7 | 4025 break; |
4026 } | |
4027 } | |
4028 } | |
4029 nobackup: | |
4030 close(fd); /* ignore errors for closing read file */ | |
4031 vim_free(copybuf); | |
4032 | |
4033 if (backup == NULL && errmsg == NULL) | |
4034 errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)"); | |
4035 /* ignore errors when forceit is TRUE */ | |
4036 if ((some_error || errmsg != NULL) && !forceit) | |
4037 { | |
4038 retval = FAIL; | |
4039 goto fail; | |
4040 } | |
4041 errmsg = NULL; | |
4042 } | |
4043 else | |
4044 { | |
4045 char_u *dirp; | |
4046 char_u *p; | |
4047 char_u *rootname; | |
4048 | |
4049 /* | |
4050 * Make a backup by renaming the original file. | |
4051 */ | |
4052 /* | |
4053 * If 'cpoptions' includes the "W" flag, we don't want to | |
4054 * overwrite a read-only file. But rename may be possible | |
4055 * anyway, thus we need an extra check here. | |
4056 */ | |
4057 if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) | |
4058 { | |
4059 errnum = (char_u *)"E504: "; | |
4060 errmsg = (char_u *)_(err_readonly); | |
4061 goto fail; | |
4062 } | |
4063 | |
4064 /* | |
4065 * | |
4066 * Form the backup file name - change path/fo.o.h to | |
4067 * path/fo.o.h.bak Try all directories in 'backupdir', first one | |
4068 * that works is used. | |
4069 */ | |
4070 dirp = p_bdir; | |
4071 while (*dirp) | |
4072 { | |
4073 /* | |
4074 * Isolate one directory name and make the backup file name. | |
4075 */ | |
4076 (void)copy_option_part(&dirp, IObuff, IOSIZE, ","); | |
4077 rootname = get_file_in_dir(fname, IObuff); | |
4078 if (rootname == NULL) | |
4079 backup = NULL; | |
4080 else | |
4081 { | |
4082 backup = buf_modname( | |
4083 #ifdef SHORT_FNAME | |
4084 TRUE, | |
4085 #else | |
4086 (buf->b_p_sn || buf->b_shortname), | |
4087 #endif | |
4088 rootname, backup_ext, FALSE); | |
4089 vim_free(rootname); | |
4090 } | |
4091 | |
4092 if (backup != NULL) | |
4093 { | |
4094 /* | |
4095 * If we are not going to keep the backup file, don't | |
4096 * delete an existing one, try to use another name. | |
4097 * Change one character, just before the extension. | |
4098 */ | |
4099 if (!p_bk && mch_getperm(backup) >= 0) | |
4100 { | |
4101 p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext); | |
4102 if (p < backup) /* empty file name ??? */ | |
4103 p = backup; | |
4104 *p = 'z'; | |
4105 while (*p > 'a' && mch_getperm(backup) >= 0) | |
4106 --*p; | |
4107 /* They all exist??? Must be something wrong! */ | |
4108 if (*p == 'a') | |
4109 { | |
4110 vim_free(backup); | |
4111 backup = NULL; | |
4112 } | |
4113 } | |
4114 } | |
4115 if (backup != NULL) | |
4116 { | |
4117 /* | |
531 | 4118 * Delete any existing backup and move the current version |
4119 * to the backup. For safety, we don't remove the backup | |
4120 * until the write has finished successfully. And if the | |
4121 * 'backup' option is set, leave it around. | |
7 | 4122 */ |
4123 /* | |
4124 * If the renaming of the original file to the backup file | |
4125 * works, quit here. | |
4126 */ | |
4127 if (vim_rename(fname, backup) == 0) | |
4128 break; | |
4129 | |
4130 vim_free(backup); /* don't do the rename below */ | |
4131 backup = NULL; | |
4132 } | |
4133 } | |
4134 if (backup == NULL && !forceit) | |
4135 { | |
4136 errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)"); | |
4137 goto fail; | |
4138 } | |
4139 } | |
4140 } | |
4141 | |
4142 #if defined(UNIX) && !defined(ARCHIE) | |
4143 /* When using ":w!" and the file was read-only: make it writable */ | |
4144 if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid() | |
4145 && vim_strchr(p_cpo, CPO_FWRITE) == NULL) | |
4146 { | |
4147 perm |= 0200; | |
4148 (void)mch_setperm(fname, perm); | |
4149 made_writable = TRUE; | |
4150 } | |
4151 #endif | |
4152 | |
819 | 4153 /* When using ":w!" and writing to the current file, 'readonly' makes no |
164 | 4154 * sense, reset it, unless 'Z' appears in 'cpoptions'. */ |
4155 if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) | |
7 | 4156 { |
4157 buf->b_p_ro = FALSE; | |
4158 #ifdef FEAT_TITLE | |
4159 need_maketitle = TRUE; /* set window title later */ | |
4160 #endif | |
4161 #ifdef FEAT_WINDOWS | |
4162 status_redraw_all(); /* redraw status lines later */ | |
4163 #endif | |
4164 } | |
4165 | |
4166 if (end > buf->b_ml.ml_line_count) | |
4167 end = buf->b_ml.ml_line_count; | |
4168 if (buf->b_ml.ml_flags & ML_EMPTY) | |
4169 start = end + 1; | |
4170 | |
4171 /* | |
4172 * If the original file is being overwritten, there is a small chance that | |
4173 * we crash in the middle of writing. Therefore the file is preserved now. | |
4174 * This makes all block numbers positive so that recovery does not need | |
4175 * the original file. | |
4176 * Don't do this if there is a backup file and we are exiting. | |
4177 */ | |
39 | 4178 if (reset_changed && !newfile && overwriting |
7 | 4179 && !(exiting && backup != NULL)) |
4180 { | |
4181 ml_preserve(buf, FALSE); | |
4182 if (got_int) | |
4183 { | |
4184 errmsg = (char_u *)_(e_interr); | |
4185 goto restore_backup; | |
4186 } | |
4187 } | |
4188 | |
4189 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
4190 /* | |
4191 * Before risking to lose the original file verify if there's | |
4192 * a resource fork to preserve, and if cannot be done warn | |
4193 * the users. This happens when overwriting without backups. | |
4194 */ | |
4195 if (backup == NULL && overwriting && !append) | |
4196 if (mch_has_resource_fork(fname)) | |
4197 { | |
4198 errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)"); | |
4199 goto restore_backup; | |
4200 } | |
4201 #endif | |
4202 | |
4203 #ifdef VMS | |
4204 vms_remove_version(fname); /* remove version */ | |
4205 #endif | |
1651 | 4206 /* Default: write the file directly. May write to a temp file for |
7 | 4207 * multi-byte conversion. */ |
4208 wfname = fname; | |
4209 | |
4210 #ifdef FEAT_MBYTE | |
4211 /* Check for forced 'fileencoding' from "++opt=val" argument. */ | |
4212 if (eap != NULL && eap->force_enc != 0) | |
4213 { | |
4214 fenc = eap->cmd + eap->force_enc; | |
4215 fenc = enc_canonize(fenc); | |
4216 fenc_tofree = fenc; | |
4217 } | |
4218 else | |
4219 fenc = buf->b_p_fenc; | |
4220 | |
4221 /* | |
1948 | 4222 * Check if the file needs to be converted. |
7 | 4223 */ |
1948 | 4224 converted = need_conversion(fenc); |
7 | 4225 |
4226 /* | |
4227 * Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or | |
4228 * Latin1 to Unicode conversion. This is handled in buf_write_bytes(). | |
4229 * Prepare the flags for it and allocate bw_conv_buf when needed. | |
4230 */ | |
4231 if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0)) | |
4232 { | |
4233 wb_flags = get_fio_flags(fenc); | |
4234 if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) | |
4235 { | |
4236 /* Need to allocate a buffer to translate into. */ | |
4237 if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) | |
4238 write_info.bw_conv_buflen = bufsize * 2; | |
4239 else /* FIO_UCS4 */ | |
4240 write_info.bw_conv_buflen = bufsize * 4; | |
4241 write_info.bw_conv_buf | |
4242 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4243 if (write_info.bw_conv_buf == NULL) | |
4244 end = 0; | |
4245 } | |
4246 } | |
4247 | |
4248 # ifdef WIN3264 | |
4249 if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0) | |
4250 { | |
4251 /* Convert UTF-8 -> UCS-2 and UCS-2 -> DBCS. Worst-case * 4: */ | |
4252 write_info.bw_conv_buflen = bufsize * 4; | |
4253 write_info.bw_conv_buf | |
4254 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4255 if (write_info.bw_conv_buf == NULL) | |
4256 end = 0; | |
4257 } | |
4258 # endif | |
4259 | |
4260 # ifdef MACOS_X | |
4261 if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0) | |
4262 { | |
4263 write_info.bw_conv_buflen = bufsize * 3; | |
4264 write_info.bw_conv_buf | |
4265 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4266 if (write_info.bw_conv_buf == NULL) | |
4267 end = 0; | |
4268 } | |
4269 # endif | |
4270 | |
4271 # if defined(FEAT_EVAL) || defined(USE_ICONV) | |
4272 if (converted && wb_flags == 0) | |
4273 { | |
4274 # ifdef USE_ICONV | |
4275 /* | |
4276 * Use iconv() conversion when conversion is needed and it's not done | |
4277 * internally. | |
4278 */ | |
4279 write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, | |
4280 enc_utf8 ? (char_u *)"utf-8" : p_enc); | |
4281 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
4282 { | |
4283 /* We're going to use iconv(), allocate a buffer to convert in. */ | |
4284 write_info.bw_conv_buflen = bufsize * ICONV_MULT; | |
4285 write_info.bw_conv_buf | |
4286 = lalloc((long_u)write_info.bw_conv_buflen, TRUE); | |
4287 if (write_info.bw_conv_buf == NULL) | |
4288 end = 0; | |
4289 write_info.bw_first = TRUE; | |
4290 } | |
4291 # ifdef FEAT_EVAL | |
4292 else | |
4293 # endif | |
4294 # endif | |
4295 | |
4296 # ifdef FEAT_EVAL | |
4297 /* | |
4298 * When the file needs to be converted with 'charconvert' after | |
4299 * writing, write to a temp file instead and let the conversion | |
4300 * overwrite the original file. | |
4301 */ | |
4302 if (*p_ccv != NUL) | |
4303 { | |
4304 wfname = vim_tempname('w'); | |
4305 if (wfname == NULL) /* Can't write without a tempfile! */ | |
4306 { | |
4307 errmsg = (char_u *)_("E214: Can't find temp file for writing"); | |
4308 goto restore_backup; | |
4309 } | |
4310 } | |
4311 # endif | |
4312 } | |
4313 # endif | |
4314 if (converted && wb_flags == 0 | |
4315 # ifdef USE_ICONV | |
4316 && write_info.bw_iconv_fd == (iconv_t)-1 | |
4317 # endif | |
4318 # ifdef FEAT_EVAL | |
4319 && wfname == fname | |
4320 # endif | |
4321 ) | |
4322 { | |
4323 if (!forceit) | |
4324 { | |
4325 errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)"); | |
4326 goto restore_backup; | |
4327 } | |
4328 notconverted = TRUE; | |
4329 } | |
4330 #endif | |
4331 | |
4332 /* | |
4333 * Open the file "wfname" for writing. | |
4334 * We may try to open the file twice: If we can't write to the | |
4335 * file and forceit is TRUE we delete the existing file and try to create | |
4336 * a new one. If this still fails we may have lost the original file! | |
4337 * (this may happen when the user reached his quotum for number of files). | |
4338 * Appending will fail if the file does not exist and forceit is FALSE. | |
4339 */ | |
4340 while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append | |
4341 ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) | |
4342 : (O_CREAT | O_TRUNC)) | |
216 | 4343 , perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4344 { |
4345 /* | |
4346 * A forced write will try to create a new file if the old one is | |
4347 * still readonly. This may also happen when the directory is | |
4348 * read-only. In that case the mch_remove() will fail. | |
4349 */ | |
4350 if (errmsg == NULL) | |
4351 { | |
4352 #ifdef UNIX | |
4353 struct stat st; | |
4354 | |
4355 /* Don't delete the file when it's a hard or symbolic link. */ | |
4356 if ((!newfile && st_old.st_nlink > 1) | |
4357 || (mch_lstat((char *)fname, &st) == 0 | |
4358 && (st.st_dev != st_old.st_dev | |
4359 || st.st_ino != st_old.st_ino))) | |
4360 errmsg = (char_u *)_("E166: Can't open linked file for writing"); | |
4361 else | |
4362 #endif | |
4363 { | |
4364 errmsg = (char_u *)_("E212: Can't open file for writing"); | |
4365 if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL | |
4366 && perm >= 0) | |
4367 { | |
4368 #ifdef UNIX | |
4369 /* we write to the file, thus it should be marked | |
4370 writable after all */ | |
4371 if (!(perm & 0200)) | |
4372 made_writable = TRUE; | |
4373 perm |= 0200; | |
4374 if (st_old.st_uid != getuid() || st_old.st_gid != getgid()) | |
4375 perm &= 0777; | |
4376 #endif | |
4377 if (!append) /* don't remove when appending */ | |
4378 mch_remove(wfname); | |
4379 continue; | |
4380 } | |
4381 } | |
4382 } | |
4383 | |
4384 restore_backup: | |
4385 { | |
4386 struct stat st; | |
4387 | |
4388 /* | |
4389 * If we failed to open the file, we don't need a backup. Throw it | |
4390 * away. If we moved or removed the original file try to put the | |
4391 * backup in its place. | |
4392 */ | |
4393 if (backup != NULL && wfname == fname) | |
4394 { | |
4395 if (backup_copy) | |
4396 { | |
4397 /* | |
4398 * There is a small chance that we removed the original, | |
4399 * try to move the copy in its place. | |
4400 * This may not work if the vim_rename() fails. | |
4401 * In that case we leave the copy around. | |
4402 */ | |
4403 /* If file does not exist, put the copy in its place */ | |
4404 if (mch_stat((char *)fname, &st) < 0) | |
4405 vim_rename(backup, fname); | |
4406 /* if original file does exist throw away the copy */ | |
4407 if (mch_stat((char *)fname, &st) >= 0) | |
4408 mch_remove(backup); | |
4409 } | |
4410 else | |
4411 { | |
4412 /* try to put the original file back */ | |
4413 vim_rename(backup, fname); | |
4414 } | |
4415 } | |
4416 | |
4417 /* if original file no longer exists give an extra warning */ | |
4418 if (!newfile && mch_stat((char *)fname, &st) < 0) | |
4419 end = 0; | |
4420 } | |
4421 | |
4422 #ifdef FEAT_MBYTE | |
4423 if (wfname != fname) | |
4424 vim_free(wfname); | |
4425 #endif | |
4426 goto fail; | |
4427 } | |
4428 errmsg = NULL; | |
4429 | |
4430 #if defined(MACOS_CLASSIC) || defined(WIN3264) | |
4431 /* TODO: Is it need for MACOS_X? (Dany) */ | |
4432 /* | |
4433 * On macintosh copy the original files attributes (i.e. the backup) | |
1201 | 4434 * This is done in order to preserve the resource fork and the |
4435 * Finder attribute (label, comments, custom icons, file creator) | |
7 | 4436 */ |
4437 if (backup != NULL && overwriting && !append) | |
4438 { | |
4439 if (backup_copy) | |
4440 (void)mch_copy_file_attribute(wfname, backup); | |
4441 else | |
4442 (void)mch_copy_file_attribute(backup, wfname); | |
4443 } | |
4444 | |
4445 if (!overwriting && !append) | |
4446 { | |
4447 if (buf->b_ffname != NULL) | |
4448 (void)mch_copy_file_attribute(buf->b_ffname, wfname); | |
1201 | 4449 /* Should copy resource fork */ |
7 | 4450 } |
4451 #endif | |
4452 | |
4453 write_info.bw_fd = fd; | |
4454 | |
4455 #ifdef FEAT_CRYPT | |
2267 | 4456 if (*buf->b_p_key != NUL && !filtering) |
7 | 4457 { |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4458 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
|
4459 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
|
4460 |
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4461 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
|
4462 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
|
4463 end = 0; |
2180
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4464 else |
f60a0c9cbe6c
Add the blowfish encryption patch from Mohsin Ahmed. Needs more work.
Bram Moolenaar <bram@vim.org>
parents:
2147
diff
changeset
|
4465 { |
2239
732cb7b31956
Crypt the text in the undo file if the file itself is crypted.
Bram Moolenaar <bram@vim.org>
parents:
2238
diff
changeset
|
4466 /* 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
|
4467 * 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
|
4468 * 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
|
4469 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
|
4470 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
|
4471 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
|
4472 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
|
4473 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
|
4474 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
|
4475 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
|
4476 } |
7 | 4477 } |
4478 #endif | |
4479 | |
4480 write_info.bw_buf = buffer; | |
4481 nchars = 0; | |
4482 | |
4483 /* use "++bin", "++nobin" or 'binary' */ | |
4484 if (eap != NULL && eap->force_bin != 0) | |
4485 write_bin = (eap->force_bin == FORCE_BIN); | |
4486 else | |
4487 write_bin = buf->b_p_bin; | |
4488 | |
4489 #ifdef FEAT_MBYTE | |
4490 /* | |
4491 * The BOM is written just after the encryption magic number. | |
24 | 4492 * Skip it when appending and the file already existed, the BOM only makes |
4493 * sense at the start of the file. | |
7 | 4494 */ |
24 | 4495 if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) |
7 | 4496 { |
4497 write_info.bw_len = make_bom(buffer, fenc); | |
4498 if (write_info.bw_len > 0) | |
4499 { | |
4500 /* don't convert, do encryption */ | |
4501 write_info.bw_flags = FIO_NOCONVERT | wb_flags; | |
4502 if (buf_write_bytes(&write_info) == FAIL) | |
4503 end = 0; | |
4504 else | |
4505 nchars += write_info.bw_len; | |
4506 } | |
4507 } | |
1947 | 4508 write_info.bw_start_lnum = start; |
7 | 4509 #endif |
4510 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4511 #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
|
4512 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
|
4513 && !filtering && reset_changed); |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4514 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
|
4515 /* 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
|
4516 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
|
4517 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
4518 |
7 | 4519 write_info.bw_len = bufsize; |
4520 #ifdef HAS_BW_FLAGS | |
4521 write_info.bw_flags = wb_flags; | |
4522 #endif | |
4523 fileformat = get_fileformat_force(buf, eap); | |
4524 s = buffer; | |
4525 len = 0; | |
4526 for (lnum = start; lnum <= end; ++lnum) | |
4527 { | |
4528 /* | |
4529 * The next while loop is done once for each character written. | |
4530 * Keep it fast! | |
4531 */ | |
4532 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
|
4533 #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
|
4534 if (write_undo_file) |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
4535 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
|
4536 #endif |
7 | 4537 while ((c = *++ptr) != NUL) |
4538 { | |
4539 if (c == NL) | |
4540 *s = NUL; /* replace newlines with NULs */ | |
4541 else if (c == CAR && fileformat == EOL_MAC) | |
4542 *s = NL; /* Mac: replace CRs with NLs */ | |
4543 else | |
4544 *s = c; | |
4545 ++s; | |
4546 if (++len != bufsize) | |
4547 continue; | |
4548 if (buf_write_bytes(&write_info) == FAIL) | |
4549 { | |
4550 end = 0; /* write error: break loop */ | |
4551 break; | |
4552 } | |
4553 nchars += bufsize; | |
4554 s = buffer; | |
4555 len = 0; | |
1947 | 4556 #ifdef FEAT_MBYTE |
4557 write_info.bw_start_lnum = lnum; | |
4558 #endif | |
7 | 4559 } |
4560 /* write failed or last line has no EOL: stop here */ | |
4561 if (end == 0 | |
4562 || (lnum == end | |
4563 && write_bin | |
4564 && (lnum == write_no_eol_lnum | |
4565 || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) | |
4566 { | |
4567 ++lnum; /* written the line, count it */ | |
4568 no_eol = TRUE; | |
4569 break; | |
4570 } | |
4571 if (fileformat == EOL_UNIX) | |
4572 *s++ = NL; | |
4573 else | |
4574 { | |
4575 *s++ = CAR; /* EOL_MAC or EOL_DOS: write CR */ | |
4576 if (fileformat == EOL_DOS) /* write CR-NL */ | |
4577 { | |
4578 if (++len == bufsize) | |
4579 { | |
4580 if (buf_write_bytes(&write_info) == FAIL) | |
4581 { | |
4582 end = 0; /* write error: break loop */ | |
4583 break; | |
4584 } | |
4585 nchars += bufsize; | |
4586 s = buffer; | |
4587 len = 0; | |
4588 } | |
4589 *s++ = NL; | |
4590 } | |
4591 } | |
4592 if (++len == bufsize && end) | |
4593 { | |
4594 if (buf_write_bytes(&write_info) == FAIL) | |
4595 { | |
4596 end = 0; /* write error: break loop */ | |
4597 break; | |
4598 } | |
4599 nchars += bufsize; | |
4600 s = buffer; | |
4601 len = 0; | |
4602 | |
4603 ui_breakcheck(); | |
4604 if (got_int) | |
4605 { | |
4606 end = 0; /* Interrupted, break loop */ | |
4607 break; | |
4608 } | |
4609 } | |
4610 #ifdef VMS | |
4611 /* | |
4612 * On VMS there is a problem: newlines get added when writing blocks | |
4613 * at a time. Fix it by writing a line at a time. | |
4614 * This is much slower! | |
22 | 4615 * Explanation: VAX/DECC RTL insists that records in some RMS |
4616 * structures end with a newline (carriage return) character, and if | |
4617 * they don't it adds one. | |
4618 * With other RMS structures it works perfect without this fix. | |
7 | 4619 */ |
1431 | 4620 if (buf->b_fab_rfm == FAB$C_VFC |
4621 || ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0)) | |
22 | 4622 { |
4623 int b2write; | |
4624 | |
4625 buf->b_fab_mrs = (buf->b_fab_mrs == 0 | |
4626 ? MIN(4096, bufsize) | |
4627 : MIN(buf->b_fab_mrs, bufsize)); | |
4628 | |
4629 b2write = len; | |
4630 while (b2write > 0) | |
7 | 4631 { |
22 | 4632 write_info.bw_len = MIN(b2write, buf->b_fab_mrs); |
4633 if (buf_write_bytes(&write_info) == FAIL) | |
4634 { | |
4635 end = 0; | |
4636 break; | |
4637 } | |
4638 b2write -= MIN(b2write, buf->b_fab_mrs); | |
7 | 4639 } |
4640 write_info.bw_len = bufsize; | |
4641 nchars += len; | |
4642 s = buffer; | |
4643 len = 0; | |
4644 } | |
4645 #endif | |
4646 } | |
4647 if (len > 0 && end > 0) | |
4648 { | |
4649 write_info.bw_len = len; | |
4650 if (buf_write_bytes(&write_info) == FAIL) | |
4651 end = 0; /* write error */ | |
4652 nchars += len; | |
4653 } | |
4654 | |
4655 #if defined(UNIX) && defined(HAVE_FSYNC) | |
4656 /* On many journalling file systems there is a bug that causes both the | |
4657 * original and the backup file to be lost when halting the system right | |
4658 * after writing the file. That's because only the meta-data is | |
4659 * journalled. Syncing the file slows down the system, but assures it has | |
10 | 4660 * been written to disk and we don't lose it. |
4661 * For a device do try the fsync() but don't complain if it does not work | |
36 | 4662 * (could be a pipe). |
4663 * If the 'fsync' option is FALSE, don't fsync(). Useful for laptops. */ | |
4664 if (p_fs && fsync(fd) != 0 && !device) | |
7 | 4665 { |
4666 errmsg = (char_u *)_("E667: Fsync failed"); | |
4667 end = 0; | |
4668 } | |
4669 #endif | |
4670 | |
1583 | 4671 #ifdef HAVE_SELINUX |
4672 /* Probably need to set the security context. */ | |
4673 if (!backup_copy) | |
4674 mch_copy_sec(backup, wfname); | |
4675 #endif | |
4676 | |
557 | 4677 #ifdef UNIX |
4678 /* When creating a new file, set its owner/group to that of the original | |
4679 * file. Get the new device and inode number. */ | |
4680 if (backup != NULL && !backup_copy) | |
4681 { | |
4682 # ifdef HAVE_FCHOWN | |
4683 struct stat st; | |
4684 | |
4685 /* don't change the owner when it's already OK, some systems remove | |
4686 * permission or ACL stuff */ | |
4687 if (mch_stat((char *)wfname, &st) < 0 | |
4688 || st.st_uid != st_old.st_uid | |
4689 || st.st_gid != st_old.st_gid) | |
4690 { | |
1757 | 4691 ignored = fchown(fd, st_old.st_uid, st_old.st_gid); |
557 | 4692 if (perm >= 0) /* set permission again, may have changed */ |
4693 (void)mch_setperm(wfname, perm); | |
4694 } | |
4695 # endif | |
4696 buf_setino(buf); | |
4697 } | |
1873 | 4698 else if (!buf->b_dev_valid) |
617 | 4699 /* Set the inode when creating a new file. */ |
4700 buf_setino(buf); | |
557 | 4701 #endif |
4702 | |
7 | 4703 if (close(fd) != 0) |
4704 { | |
4705 errmsg = (char_u *)_("E512: Close failed"); | |
4706 end = 0; | |
4707 } | |
4708 | |
4709 #ifdef UNIX | |
4710 if (made_writable) | |
4711 perm &= ~0200; /* reset 'w' bit for security reasons */ | |
4712 #endif | |
4713 if (perm >= 0) /* set perm. of new file same as old file */ | |
4714 (void)mch_setperm(wfname, perm); | |
4715 #ifdef RISCOS | |
4716 if (!append && !filtering) | |
4717 /* Set the filetype after writing the file. */ | |
4718 mch_set_filetype(wfname, buf->b_p_oft); | |
4719 #endif | |
4720 #ifdef HAVE_ACL | |
4721 /* Probably need to set the ACL before changing the user (can't set the | |
4722 * ACL on a file the user doesn't own). */ | |
4723 if (!backup_copy) | |
4724 mch_set_acl(wfname, acl); | |
4725 #endif | |
2267 | 4726 #ifdef FEAT_CRYPT |
4727 if (wb_flags & FIO_ENCRYPTED) | |
4728 crypt_pop_state(); | |
4729 #endif | |
7 | 4730 |
4731 | |
4732 #if defined(FEAT_MBYTE) && defined(FEAT_EVAL) | |
4733 if (wfname != fname) | |
4734 { | |
4735 /* | |
4736 * The file was written to a temp file, now it needs to be converted | |
4737 * with 'charconvert' to (overwrite) the output file. | |
4738 */ | |
4739 if (end != 0) | |
4740 { | |
4741 if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc, | |
4742 wfname, fname) == FAIL) | |
4743 { | |
4744 write_info.bw_conv_error = TRUE; | |
4745 end = 0; | |
4746 } | |
4747 } | |
4748 mch_remove(wfname); | |
4749 vim_free(wfname); | |
4750 } | |
4751 #endif | |
4752 | |
4753 if (end == 0) | |
4754 { | |
4755 if (errmsg == NULL) | |
4756 { | |
4757 #ifdef FEAT_MBYTE | |
4758 if (write_info.bw_conv_error) | |
1947 | 4759 { |
4760 if (write_info.bw_conv_error_lnum == 0) | |
4761 errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)"); | |
4762 else | |
4763 { | |
4764 errmsg_allocated = TRUE; | |
4765 errmsg = alloc(300); | |
4766 vim_snprintf((char *)errmsg, 300, _("E513: write error, conversion failed in line %ld (make 'fenc' empty to override)"), | |
4767 (long)write_info.bw_conv_error_lnum); | |
4768 } | |
4769 } | |
7 | 4770 else |
4771 #endif | |
4772 if (got_int) | |
4773 errmsg = (char_u *)_(e_interr); | |
4774 else | |
4775 errmsg = (char_u *)_("E514: write error (file system full?)"); | |
4776 } | |
4777 | |
4778 /* | |
4779 * If we have a backup file, try to put it in place of the new file, | |
1698 | 4780 * because the new file is probably corrupt. This avoids losing the |
7 | 4781 * original file when trying to make a backup when writing the file a |
4782 * second time. | |
4783 * When "backup_copy" is set we need to copy the backup over the new | |
4784 * file. Otherwise rename the backup file. | |
4785 * If this is OK, don't give the extra warning message. | |
4786 */ | |
4787 if (backup != NULL) | |
4788 { | |
4789 if (backup_copy) | |
4790 { | |
4791 /* This may take a while, if we were interrupted let the user | |
4792 * know we got the message. */ | |
4793 if (got_int) | |
4794 { | |
4795 MSG(_(e_interr)); | |
4796 out_flush(); | |
4797 } | |
4798 if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0) | |
4799 { | |
4800 if ((write_info.bw_fd = mch_open((char *)fname, | |
194 | 4801 O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA, |
4802 perm & 0777)) >= 0) | |
7 | 4803 { |
4804 /* copy the file. */ | |
4805 write_info.bw_buf = smallbuf; | |
4806 #ifdef HAS_BW_FLAGS | |
4807 write_info.bw_flags = FIO_NOCONVERT; | |
4808 #endif | |
4809 while ((write_info.bw_len = vim_read(fd, smallbuf, | |
4810 SMBUFSIZE)) > 0) | |
4811 if (buf_write_bytes(&write_info) == FAIL) | |
4812 break; | |
4813 | |
4814 if (close(write_info.bw_fd) >= 0 | |
4815 && write_info.bw_len == 0) | |
4816 end = 1; /* success */ | |
4817 } | |
4818 close(fd); /* ignore errors for closing read file */ | |
4819 } | |
4820 } | |
4821 else | |
4822 { | |
4823 if (vim_rename(backup, fname) == 0) | |
4824 end = 1; | |
4825 } | |
4826 } | |
4827 goto fail; | |
4828 } | |
4829 | |
4830 lnum -= start; /* compute number of written lines */ | |
4831 --no_wait_return; /* may wait for return now */ | |
4832 | |
4833 #if !(defined(UNIX) || defined(VMS)) | |
4834 fname = sfname; /* use shortname now, for the messages */ | |
4835 #endif | |
4836 if (!filtering) | |
4837 { | |
4838 msg_add_fname(buf, fname); /* put fname in IObuff with quotes */ | |
4839 c = FALSE; | |
4840 #ifdef FEAT_MBYTE | |
4841 if (write_info.bw_conv_error) | |
4842 { | |
4843 STRCAT(IObuff, _(" CONVERSION ERROR")); | |
4844 c = TRUE; | |
1947 | 4845 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
|
4846 vim_snprintf_add((char *)IObuff, IOSIZE, _(" in line %ld;"), |
1947 | 4847 (long)write_info.bw_conv_error_lnum); |
7 | 4848 } |
4849 else if (notconverted) | |
4850 { | |
4851 STRCAT(IObuff, _("[NOT converted]")); | |
4852 c = TRUE; | |
4853 } | |
4854 else if (converted) | |
4855 { | |
4856 STRCAT(IObuff, _("[converted]")); | |
4857 c = TRUE; | |
4858 } | |
4859 #endif | |
4860 if (device) | |
4861 { | |
4862 STRCAT(IObuff, _("[Device]")); | |
4863 c = TRUE; | |
4864 } | |
4865 else if (newfile) | |
4866 { | |
4867 STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]")); | |
4868 c = TRUE; | |
4869 } | |
4870 if (no_eol) | |
4871 { | |
4872 msg_add_eol(); | |
4873 c = TRUE; | |
4874 } | |
4875 /* may add [unix/dos/mac] */ | |
4876 if (msg_add_fileformat(fileformat)) | |
4877 c = TRUE; | |
4878 #ifdef FEAT_CRYPT | |
4879 if (wb_flags & FIO_ENCRYPTED) | |
4880 { | |
4881 STRCAT(IObuff, _("[crypted]")); | |
4882 c = TRUE; | |
4883 } | |
4884 #endif | |
4885 msg_add_lines(c, (long)lnum, nchars); /* add line/char count */ | |
4886 if (!shortmess(SHM_WRITE)) | |
4887 { | |
4888 if (append) | |
4889 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended")); | |
4890 else | |
4891 STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written")); | |
4892 } | |
4893 | |
679 | 4894 set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0); |
7 | 4895 } |
4896 | |
39 | 4897 /* When written everything correctly: reset 'modified'. Unless not |
4898 * writing to the original file and '+' is not in 'cpoptions'. */ | |
589 | 4899 if (reset_changed && whole && !append |
7 | 4900 #ifdef FEAT_MBYTE |
4901 && !write_info.bw_conv_error | |
4902 #endif | |
39 | 4903 && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL) |
4904 ) | |
7 | 4905 { |
4906 unchanged(buf, TRUE); | |
4907 u_unchanged(buf); | |
2281
e41433ea71df
Added ":earlier 1f" and ":later 1f".
Bram Moolenaar <bram@vim.org>
parents:
2280
diff
changeset
|
4908 u_update_save_nr(buf); |
7 | 4909 } |
4910 | |
4911 /* | |
4912 * If written to the current file, update the timestamp of the swap file | |
4913 * and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. | |
4914 */ | |
4915 if (overwriting) | |
4916 { | |
4917 ml_timestamp(buf); | |
589 | 4918 if (append) |
4919 buf->b_flags &= ~BF_NEW; | |
4920 else | |
4921 buf->b_flags &= ~BF_WRITE_MASK; | |
7 | 4922 } |
4923 | |
4924 /* | |
4925 * If we kept a backup until now, and we are in patch mode, then we make | |
4926 * the backup file our 'original' file. | |
4927 */ | |
4928 if (*p_pm && dobackup) | |
4929 { | |
4930 char *org = (char *)buf_modname( | |
4931 #ifdef SHORT_FNAME | |
4932 TRUE, | |
4933 #else | |
4934 (buf->b_p_sn || buf->b_shortname), | |
4935 #endif | |
4936 fname, p_pm, FALSE); | |
4937 | |
4938 if (backup != NULL) | |
4939 { | |
4940 struct stat st; | |
4941 | |
4942 /* | |
4943 * If the original file does not exist yet | |
4944 * the current backup file becomes the original file | |
4945 */ | |
4946 if (org == NULL) | |
4947 EMSG(_("E205: Patchmode: can't save original file")); | |
4948 else if (mch_stat(org, &st) < 0) | |
4949 { | |
4950 vim_rename(backup, (char_u *)org); | |
4951 vim_free(backup); /* don't delete the file */ | |
4952 backup = NULL; | |
4953 #ifdef UNIX | |
4954 set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime); | |
4955 #endif | |
4956 } | |
4957 } | |
4958 /* | |
4959 * If there is no backup file, remember that a (new) file was | |
4960 * created. | |
4961 */ | |
4962 else | |
4963 { | |
4964 int empty_fd; | |
4965 | |
4966 if (org == NULL | |
557 | 4967 || (empty_fd = mch_open(org, |
4968 O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW, | |
216 | 4969 perm < 0 ? 0666 : (perm & 0777))) < 0) |
7 | 4970 EMSG(_("E206: patchmode: can't touch empty original file")); |
4971 else | |
4972 close(empty_fd); | |
4973 } | |
4974 if (org != NULL) | |
4975 { | |
4976 mch_setperm((char_u *)org, mch_getperm(fname) & 0777); | |
4977 vim_free(org); | |
4978 } | |
4979 } | |
4980 | |
4981 /* | |
4982 * Remove the backup unless 'backup' option is set | |
4983 */ | |
4984 if (!p_bk && backup != NULL && mch_remove(backup) != 0) | |
4985 EMSG(_("E207: Can't delete backup file")); | |
4986 | |
4987 #ifdef FEAT_SUN_WORKSHOP | |
4988 if (usingSunWorkShop) | |
4989 workshop_file_saved((char *) ffname); | |
4990 #endif | |
4991 | |
4992 goto nofail; | |
4993 | |
4994 /* | |
4995 * Finish up. We get here either after failure or success. | |
4996 */ | |
4997 fail: | |
4998 --no_wait_return; /* may wait for return now */ | |
4999 nofail: | |
5000 | |
5001 /* Done saving, we accept changed buffer warnings again */ | |
5002 buf->b_saving = FALSE; | |
5003 | |
5004 vim_free(backup); | |
5005 if (buffer != smallbuf) | |
5006 vim_free(buffer); | |
5007 #ifdef FEAT_MBYTE | |
5008 vim_free(fenc_tofree); | |
5009 vim_free(write_info.bw_conv_buf); | |
5010 # ifdef USE_ICONV | |
5011 if (write_info.bw_iconv_fd != (iconv_t)-1) | |
5012 { | |
5013 iconv_close(write_info.bw_iconv_fd); | |
5014 write_info.bw_iconv_fd = (iconv_t)-1; | |
5015 } | |
5016 # endif | |
5017 #endif | |
5018 #ifdef HAVE_ACL | |
5019 mch_free_acl(acl); | |
5020 #endif | |
5021 | |
5022 if (errmsg != NULL) | |
5023 { | |
835 | 5024 int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0; |
7 | 5025 |
5026 attr = hl_attr(HLF_E); /* set highlight for error messages */ | |
5027 msg_add_fname(buf, | |
5028 #ifndef UNIX | |
5029 sfname | |
5030 #else | |
5031 fname | |
5032 #endif | |
5033 ); /* put file name in IObuff with quotes */ | |
5034 if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE) | |
5035 IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL; | |
5036 /* If the error message has the form "is ...", put the error number in | |
5037 * front of the file name. */ | |
5038 if (errnum != NULL) | |
5039 { | |
1620 | 5040 STRMOVE(IObuff + numlen, IObuff); |
7 | 5041 mch_memmove(IObuff, errnum, (size_t)numlen); |
5042 } | |
5043 STRCAT(IObuff, errmsg); | |
5044 emsg(IObuff); | |
1947 | 5045 if (errmsg_allocated) |
5046 vim_free(errmsg); | |
7 | 5047 |
5048 retval = FAIL; | |
5049 if (end == 0) | |
5050 { | |
5051 MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"), | |
5052 attr | MSG_HIST); | |
5053 MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"), | |
5054 attr | MSG_HIST); | |
5055 | |
5056 /* Update the timestamp to avoid an "overwrite changed file" | |
5057 * prompt when writing again. */ | |
5058 if (mch_stat((char *)fname, &st_old) >= 0) | |
5059 { | |
5060 buf_store_time(buf, &st_old, fname); | |
5061 buf->b_mtime_read = buf->b_mtime; | |
5062 } | |
5063 } | |
5064 } | |
5065 msg_scroll = msg_save; | |
5066 | |
2214
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5067 #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
|
5068 /* |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5069 * 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
|
5070 * file. |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5071 */ |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5072 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
|
5073 { |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5074 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
|
5075 |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5076 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
|
5077 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
|
5078 } |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5079 #endif |
f8222d1f9a73
Included patch for persistent undo. Lots of changes and added test.
Bram Moolenaar <bram@vim.org>
parents:
2210
diff
changeset
|
5080 |
7 | 5081 #ifdef FEAT_AUTOCMD |
5082 #ifdef FEAT_EVAL | |
5083 if (!should_abort(retval)) | |
5084 #else | |
5085 if (!got_int) | |
5086 #endif | |
5087 { | |
5088 aco_save_T aco; | |
5089 | |
5090 write_no_eol_lnum = 0; /* in case it was set by the previous read */ | |
5091 | |
5092 /* | |
5093 * Apply POST autocommands. | |
5094 * Careful: The autocommands may call buf_write() recursively! | |
5095 */ | |
5096 aucmd_prepbuf(&aco, buf); | |
5097 | |
5098 if (append) | |
5099 apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, | |
5100 FALSE, curbuf, eap); | |
5101 else if (filtering) | |
5102 apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, | |
5103 FALSE, curbuf, eap); | |
5104 else if (reset_changed && whole) | |
5105 apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, | |
5106 FALSE, curbuf, eap); | |
5107 else | |
5108 apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, | |
5109 FALSE, curbuf, eap); | |
5110 | |
5111 /* restore curwin/curbuf and a few other things */ | |
5112 aucmd_restbuf(&aco); | |
5113 | |
5114 #ifdef FEAT_EVAL | |
5115 if (aborting()) /* autocmds may abort script processing */ | |
5116 retval = FALSE; | |
5117 #endif | |
5118 } | |
5119 #endif | |
5120 | |
5121 got_int |= prev_got_int; | |
5122 | |
5123 #ifdef MACOS_CLASSIC /* TODO: Is it need for MACOS_X? (Dany) */ | |
5124 /* Update machine specific information. */ | |
5125 mch_post_buffer_write(buf); | |
5126 #endif | |
5127 return retval; | |
5128 } | |
5129 | |
5130 /* | |
633 | 5131 * Set the name of the current buffer. Use when the buffer doesn't have a |
5132 * name and a ":r" or ":w" command with a file name is used. | |
5133 */ | |
5134 static int | |
5135 set_rw_fname(fname, sfname) | |
5136 char_u *fname; | |
5137 char_u *sfname; | |
5138 { | |
5139 #ifdef FEAT_AUTOCMD | |
1905 | 5140 buf_T *buf = curbuf; |
5141 | |
633 | 5142 /* It's like the unnamed buffer is deleted.... */ |
5143 if (curbuf->b_p_bl) | |
5144 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf); | |
5145 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf); | |
5146 # ifdef FEAT_EVAL | |
5147 if (aborting()) /* autocmds may abort script processing */ | |
5148 return FAIL; | |
5149 # endif | |
1905 | 5150 if (curbuf != buf) |
5151 { | |
5152 /* We are in another buffer now, don't do the renaming. */ | |
5153 EMSG(_(e_auchangedbuf)); | |
5154 return FAIL; | |
5155 } | |
633 | 5156 #endif |
5157 | |
5158 if (setfname(curbuf, fname, sfname, FALSE) == OK) | |
5159 curbuf->b_flags |= BF_NOTEDITED; | |
5160 | |
5161 #ifdef FEAT_AUTOCMD | |
5162 /* ....and a new named one is created */ | |
5163 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf); | |
5164 if (curbuf->b_p_bl) | |
5165 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf); | |
5166 # ifdef FEAT_EVAL | |
5167 if (aborting()) /* autocmds may abort script processing */ | |
5168 return FAIL; | |
5169 # endif | |
5170 | |
5171 /* Do filetype detection now if 'filetype' is empty. */ | |
5172 if (*curbuf->b_p_ft == NUL) | |
5173 { | |
819 | 5174 if (au_has_group((char_u *)"filetypedetect")) |
676 | 5175 (void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE); |
717 | 5176 do_modelines(0); |
633 | 5177 } |
5178 #endif | |
5179 | |
5180 return OK; | |
5181 } | |
5182 | |
5183 /* | |
7 | 5184 * Put file name into IObuff with quotes. |
5185 */ | |
33 | 5186 void |
7 | 5187 msg_add_fname(buf, fname) |
5188 buf_T *buf; | |
5189 char_u *fname; | |
5190 { | |
5191 if (fname == NULL) | |
5192 fname = (char_u *)"-stdin-"; | |
5193 home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE); | |
5194 IObuff[0] = '"'; | |
5195 STRCAT(IObuff, "\" "); | |
5196 } | |
5197 | |
5198 /* | |
5199 * Append message for text mode to IObuff. | |
5200 * Return TRUE if something appended. | |
5201 */ | |
5202 static int | |
5203 msg_add_fileformat(eol_type) | |
5204 int eol_type; | |
5205 { | |
5206 #ifndef USE_CRNL | |
5207 if (eol_type == EOL_DOS) | |
5208 { | |
5209 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]")); | |
5210 return TRUE; | |
5211 } | |
5212 #endif | |
5213 #ifndef USE_CR | |
5214 if (eol_type == EOL_MAC) | |
5215 { | |
5216 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]")); | |
5217 return TRUE; | |
5218 } | |
5219 #endif | |
5220 #if defined(USE_CRNL) || defined(USE_CR) | |
5221 if (eol_type == EOL_UNIX) | |
5222 { | |
5223 STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]")); | |
5224 return TRUE; | |
5225 } | |
5226 #endif | |
5227 return FALSE; | |
5228 } | |
5229 | |
5230 /* | |
5231 * Append line and character count to IObuff. | |
5232 */ | |
33 | 5233 void |
7 | 5234 msg_add_lines(insert_space, lnum, nchars) |
5235 int insert_space; | |
5236 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
|
5237 off_t nchars; |
7 | 5238 { |
5239 char_u *p; | |
5240 | |
5241 p = IObuff + STRLEN(IObuff); | |
5242 | |
5243 if (insert_space) | |
5244 *p++ = ' '; | |
5245 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
|
5246 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
|
5247 #ifdef LONG_LONG_OFF_T |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5248 "%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
|
5249 #else |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5250 /* 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
|
5251 "%ldL, %ldC", lnum, (long)nchars |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5252 #endif |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5253 ); |
7 | 5254 else |
5255 { | |
5256 if (lnum == 1) | |
5257 STRCPY(p, _("1 line, ")); | |
5258 else | |
5259 sprintf((char *)p, _("%ld lines, "), lnum); | |
5260 p += STRLEN(p); | |
5261 if (nchars == 1) | |
5262 STRCPY(p, _("1 character")); | |
5263 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
|
5264 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
|
5265 #ifdef LONG_LONG_OFF_T |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5266 _("%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
|
5267 #else |
2261
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5268 /* 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
|
5269 _("%ld characters"), (long)nchars |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5270 #endif |
06a7438b3ef7
Avoid compiler warnings on Mac 10.6.
Bram Moolenaar <bram@vim.org>
parents:
2258
diff
changeset
|
5271 ); |
7 | 5272 } |
5273 } | |
5274 | |
5275 /* | |
5276 * Append message for missing line separator to IObuff. | |
5277 */ | |
5278 static void | |
5279 msg_add_eol() | |
5280 { | |
5281 STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); | |
5282 } | |
5283 | |
5284 /* | |
5285 * Check modification time of file, before writing to it. | |
5286 * The size isn't checked, because using a tool like "gzip" takes care of | |
5287 * using the same timestamp but can't set the size. | |
5288 */ | |
5289 static int | |
5290 check_mtime(buf, st) | |
5291 buf_T *buf; | |
5292 struct stat *st; | |
5293 { | |
5294 if (buf->b_mtime_read != 0 | |
5295 && time_differs((long)st->st_mtime, buf->b_mtime_read)) | |
5296 { | |
5297 msg_scroll = TRUE; /* don't overwrite messages here */ | |
5298 msg_silent = 0; /* must give this prompt */ | |
5299 /* don't use emsg() here, don't want to flush the buffers */ | |
5300 MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"), | |
5301 hl_attr(HLF_E)); | |
5302 if (ask_yesno((char_u *)_("Do you really want to write to it"), | |
5303 TRUE) == 'n') | |
5304 return FAIL; | |
5305 msg_scroll = FALSE; /* always overwrite the file message now */ | |
5306 } | |
5307 return OK; | |
5308 } | |
5309 | |
5310 static int | |
5311 time_differs(t1, t2) | |
5312 long t1, t2; | |
5313 { | |
5314 #if defined(__linux__) || defined(MSDOS) || defined(MSWIN) | |
5315 /* On a FAT filesystem, esp. under Linux, there are only 5 bits to store | |
5316 * the seconds. Since the roundoff is done when flushing the inode, the | |
5317 * time may change unexpectedly by one second!!! */ | |
5318 return (t1 - t2 > 1 || t2 - t1 > 1); | |
5319 #else | |
5320 return (t1 != t2); | |
5321 #endif | |
5322 } | |
5323 | |
5324 /* | |
5325 * Call write() to write a number of bytes to the file. | |
5326 * Also handles encryption and 'encoding' conversion. | |
5327 * | |
5328 * Return FAIL for failure, OK otherwise. | |
5329 */ | |
5330 static int | |
5331 buf_write_bytes(ip) | |
5332 struct bw_info *ip; | |
5333 { | |
5334 int wlen; | |
5335 char_u *buf = ip->bw_buf; /* data to write */ | |
5336 int len = ip->bw_len; /* length of data */ | |
5337 #ifdef HAS_BW_FLAGS | |
5338 int flags = ip->bw_flags; /* extra flags */ | |
5339 #endif | |
5340 | |
5341 #ifdef FEAT_MBYTE | |
5342 /* | |
5343 * Skip conversion when writing the crypt magic number or the BOM. | |
5344 */ | |
5345 if (!(flags & FIO_NOCONVERT)) | |
5346 { | |
5347 char_u *p; | |
5348 unsigned c; | |
5349 int n; | |
5350 | |
5351 if (flags & FIO_UTF8) | |
5352 { | |
5353 /* | |
5354 * Convert latin1 in the buffer to UTF-8 in the file. | |
5355 */ | |
5356 p = ip->bw_conv_buf; /* translate to buffer */ | |
5357 for (wlen = 0; wlen < len; ++wlen) | |
5358 p += utf_char2bytes(buf[wlen], p); | |
5359 buf = ip->bw_conv_buf; | |
5360 len = (int)(p - ip->bw_conv_buf); | |
5361 } | |
5362 else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) | |
5363 { | |
5364 /* | |
5365 * Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or | |
5366 * Latin1 chars in the file. | |
5367 */ | |
5368 if (flags & FIO_LATIN1) | |
5369 p = buf; /* translate in-place (can only get shorter) */ | |
5370 else | |
5371 p = ip->bw_conv_buf; /* translate to buffer */ | |
5372 for (wlen = 0; wlen < len; wlen += n) | |
5373 { | |
5374 if (wlen == 0 && ip->bw_restlen != 0) | |
5375 { | |
5376 int l; | |
5377 | |
5378 /* Use remainder of previous call. Append the start of | |
5379 * buf[] to get a full sequence. Might still be too | |
5380 * short! */ | |
5381 l = CONV_RESTLEN - ip->bw_restlen; | |
5382 if (l > len) | |
5383 l = len; | |
5384 mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l); | |
474 | 5385 n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l); |
7 | 5386 if (n > ip->bw_restlen + len) |
5387 { | |
5388 /* We have an incomplete byte sequence at the end to | |
5389 * be written. We can't convert it without the | |
5390 * remaining bytes. Keep them for the next call. */ | |
5391 if (ip->bw_restlen + len > CONV_RESTLEN) | |
5392 return FAIL; | |
5393 ip->bw_restlen += len; | |
5394 break; | |
5395 } | |
5396 if (n > 1) | |
5397 c = utf_ptr2char(ip->bw_rest); | |
5398 else | |
5399 c = ip->bw_rest[0]; | |
5400 if (n >= ip->bw_restlen) | |
5401 { | |
5402 n -= ip->bw_restlen; | |
5403 ip->bw_restlen = 0; | |
5404 } | |
5405 else | |
5406 { | |
5407 ip->bw_restlen -= n; | |
5408 mch_memmove(ip->bw_rest, ip->bw_rest + n, | |
5409 (size_t)ip->bw_restlen); | |
5410 n = 0; | |
5411 } | |
5412 } | |
5413 else | |
5414 { | |
474 | 5415 n = utf_ptr2len_len(buf + wlen, len - wlen); |
7 | 5416 if (n > len - wlen) |
5417 { | |
5418 /* We have an incomplete byte sequence at the end to | |
5419 * be written. We can't convert it without the | |
5420 * remaining bytes. Keep them for the next call. */ | |
5421 if (len - wlen > CONV_RESTLEN) | |
5422 return FAIL; | |
5423 ip->bw_restlen = len - wlen; | |
5424 mch_memmove(ip->bw_rest, buf + wlen, | |
5425 (size_t)ip->bw_restlen); | |
5426 break; | |
5427 } | |
5428 if (n > 1) | |
5429 c = utf_ptr2char(buf + wlen); | |
5430 else | |
5431 c = buf[wlen]; | |
5432 } | |
5433 | |
1947 | 5434 if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) |
5435 { | |
5436 ip->bw_conv_error = TRUE; | |
5437 ip->bw_conv_error_lnum = ip->bw_start_lnum; | |
5438 } | |
5439 if (c == NL) | |
5440 ++ip->bw_start_lnum; | |
7 | 5441 } |
5442 if (flags & FIO_LATIN1) | |
5443 len = (int)(p - buf); | |
5444 else | |
5445 { | |
5446 buf = ip->bw_conv_buf; | |
5447 len = (int)(p - ip->bw_conv_buf); | |
5448 } | |
5449 } | |
5450 | |
5451 # ifdef WIN3264 | |
5452 else if (flags & FIO_CODEPAGE) | |
5453 { | |
5454 /* | |
5455 * Convert UTF-8 or codepage to UCS-2 and then to MS-Windows | |
5456 * codepage. | |
5457 */ | |
5458 char_u *from; | |
5459 size_t fromlen; | |
5460 char_u *to; | |
5461 int u8c; | |
5462 BOOL bad = FALSE; | |
5463 int needed; | |
5464 | |
5465 if (ip->bw_restlen > 0) | |
5466 { | |
5467 /* Need to concatenate the remainder of the previous call and | |
5468 * the bytes of the current call. Use the end of the | |
5469 * conversion buffer for this. */ | |
5470 fromlen = len + ip->bw_restlen; | |
5471 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5472 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5473 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5474 } | |
5475 else | |
5476 { | |
5477 from = buf; | |
5478 fromlen = len; | |
5479 } | |
5480 | |
5481 to = ip->bw_conv_buf; | |
5482 if (enc_utf8) | |
5483 { | |
5484 /* Convert from UTF-8 to UCS-2, to the start of the buffer. | |
5485 * The buffer has been allocated to be big enough. */ | |
5486 while (fromlen > 0) | |
5487 { | |
835 | 5488 n = (int)utf_ptr2len_len(from, (int)fromlen); |
7 | 5489 if (n > (int)fromlen) /* incomplete byte sequence */ |
5490 break; | |
5491 u8c = utf_ptr2char(from); | |
5492 *to++ = (u8c & 0xff); | |
5493 *to++ = (u8c >> 8); | |
5494 fromlen -= n; | |
5495 from += n; | |
5496 } | |
5497 | |
5498 /* Copy remainder to ip->bw_rest[] to be used for the next | |
5499 * call. */ | |
5500 if (fromlen > CONV_RESTLEN) | |
5501 { | |
5502 /* weird overlong sequence */ | |
5503 ip->bw_conv_error = TRUE; | |
5504 return FAIL; | |
5505 } | |
5506 mch_memmove(ip->bw_rest, from, fromlen); | |
835 | 5507 ip->bw_restlen = (int)fromlen; |
7 | 5508 } |
5509 else | |
5510 { | |
5511 /* Convert from enc_codepage to UCS-2, to the start of the | |
5512 * buffer. The buffer has been allocated to be big enough. */ | |
5513 ip->bw_restlen = 0; | |
5514 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5515 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen, |
7 | 5516 NULL, 0); |
5517 if (needed == 0) | |
5518 { | |
5519 /* When conversion fails there may be a trailing byte. */ | |
5520 needed = MultiByteToWideChar(enc_codepage, | |
1142 | 5521 MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1, |
7 | 5522 NULL, 0); |
5523 if (needed == 0) | |
5524 { | |
5525 /* Conversion doesn't work. */ | |
5526 ip->bw_conv_error = TRUE; | |
5527 return FAIL; | |
5528 } | |
5529 /* Save the trailing byte for the next call. */ | |
5530 ip->bw_rest[0] = from[fromlen - 1]; | |
5531 ip->bw_restlen = 1; | |
5532 } | |
5533 needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS, | |
1142 | 5534 (LPCSTR)from, (int)(fromlen - ip->bw_restlen), |
7 | 5535 (LPWSTR)to, needed); |
5536 if (needed == 0) | |
5537 { | |
5538 /* Safety check: Conversion doesn't work. */ | |
5539 ip->bw_conv_error = TRUE; | |
5540 return FAIL; | |
5541 } | |
5542 to += needed * 2; | |
5543 } | |
5544 | |
5545 fromlen = to - ip->bw_conv_buf; | |
5546 buf = to; | |
5547 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5548 if (FIO_GET_CP(flags) == CP_UTF8) | |
5549 { | |
5550 /* Convert from UCS-2 to UTF-8, using the remainder of the | |
5551 * conversion buffer. Fails when out of space. */ | |
5552 for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2) | |
5553 { | |
5554 u8c = *from++; | |
5555 u8c += (*from++ << 8); | |
5556 to += utf_char2bytes(u8c, to); | |
5557 if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen) | |
5558 { | |
5559 ip->bw_conv_error = TRUE; | |
5560 return FAIL; | |
5561 } | |
5562 } | |
835 | 5563 len = (int)(to - buf); |
7 | 5564 } |
5565 else | |
5566 #endif | |
5567 { | |
5568 /* Convert from UCS-2 to the codepage, using the remainder of | |
5569 * the conversion buffer. If the conversion uses the default | |
5570 * character "0", the data doesn't fit in this encoding, so | |
5571 * fail. */ | |
5572 len = WideCharToMultiByte(FIO_GET_CP(flags), 0, | |
5573 (LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR), | |
1142 | 5574 (LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0, |
5575 &bad); | |
7 | 5576 if (bad) |
5577 { | |
5578 ip->bw_conv_error = TRUE; | |
5579 return FAIL; | |
5580 } | |
5581 } | |
5582 } | |
5583 # endif | |
5584 | |
765 | 5585 # ifdef MACOS_CONVERT |
7 | 5586 else if (flags & FIO_MACROMAN) |
5587 { | |
5588 /* | |
5589 * Convert UTF-8 or latin1 to Apple MacRoman. | |
5590 */ | |
5591 char_u *from; | |
5592 size_t fromlen; | |
5593 | |
5594 if (ip->bw_restlen > 0) | |
5595 { | |
5596 /* Need to concatenate the remainder of the previous call and | |
5597 * the bytes of the current call. Use the end of the | |
5598 * conversion buffer for this. */ | |
5599 fromlen = len + ip->bw_restlen; | |
5600 from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; | |
5601 mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen); | |
5602 mch_memmove(from + ip->bw_restlen, buf, (size_t)len); | |
5603 } | |
5604 else | |
5605 { | |
5606 from = buf; | |
5607 fromlen = len; | |
5608 } | |
5609 | |
18 | 5610 if (enc2macroman(from, fromlen, |
5611 ip->bw_conv_buf, &len, ip->bw_conv_buflen, | |
5612 ip->bw_rest, &ip->bw_restlen) == FAIL) | |
7 | 5613 { |
5614 ip->bw_conv_error = TRUE; | |
5615 return FAIL; | |
5616 } | |
5617 buf = ip->bw_conv_buf; | |
5618 } | |
5619 # endif | |
5620 | |
5621 # ifdef USE_ICONV | |
5622 if (ip->bw_iconv_fd != (iconv_t)-1) | |
5623 { | |
5624 const char *from; | |
5625 size_t fromlen; | |
5626 char *to; | |
5627 size_t tolen; | |
5628 | |
5629 /* Convert with iconv(). */ | |
5630 if (ip->bw_restlen > 0) | |
5631 { | |
1836 | 5632 char *fp; |
5633 | |
7 | 5634 /* Need to concatenate the remainder of the previous call and |
5635 * the bytes of the current call. Use the end of the | |
5636 * conversion buffer for this. */ | |
5637 fromlen = len + ip->bw_restlen; | |
1836 | 5638 fp = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; |
5639 mch_memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); | |
5640 mch_memmove(fp + ip->bw_restlen, buf, (size_t)len); | |
5641 from = fp; | |
7 | 5642 tolen = ip->bw_conv_buflen - fromlen; |
5643 } | |
5644 else | |
5645 { | |
5646 from = (const char *)buf; | |
5647 fromlen = len; | |
5648 tolen = ip->bw_conv_buflen; | |
5649 } | |
5650 to = (char *)ip->bw_conv_buf; | |
5651 | |
5652 if (ip->bw_first) | |
5653 { | |
5654 size_t save_len = tolen; | |
5655 | |
5656 /* output the initial shift state sequence */ | |
5657 (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); | |
5658 | |
5659 /* There is a bug in iconv() on Linux (which appears to be | |
5660 * wide-spread) which sets "to" to NULL and messes up "tolen". | |
5661 */ | |
5662 if (to == NULL) | |
5663 { | |
5664 to = (char *)ip->bw_conv_buf; | |
5665 tolen = save_len; | |
5666 } | |
5667 ip->bw_first = FALSE; | |
5668 } | |
5669 | |
5670 /* | |
5671 * If iconv() has an error or there is not enough room, fail. | |
5672 */ | |
5673 if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen) | |
5674 == (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL) | |
5675 || fromlen > CONV_RESTLEN) | |
5676 { | |
5677 ip->bw_conv_error = TRUE; | |
5678 return FAIL; | |
5679 } | |
5680 | |
5681 /* copy remainder to ip->bw_rest[] to be used for the next call. */ | |
5682 if (fromlen > 0) | |
5683 mch_memmove(ip->bw_rest, (void *)from, fromlen); | |
5684 ip->bw_restlen = (int)fromlen; | |
5685 | |
5686 buf = ip->bw_conv_buf; | |
5687 len = (int)((char_u *)to - ip->bw_conv_buf); | |
5688 } | |
5689 # endif | |
5690 } | |
5691 #endif /* FEAT_MBYTE */ | |
5692 | |
5693 #ifdef FEAT_CRYPT | |
5694 if (flags & FIO_ENCRYPTED) /* encrypt the data */ | |
2244 | 5695 crypt_encode(buf, len, buf); |
7 | 5696 #endif |
5697 | |
5698 /* Repeat the write(), it may be interrupted by a signal. */ | |
1142 | 5699 while (len > 0) |
7 | 5700 { |
5701 wlen = vim_write(ip->bw_fd, buf, len); | |
5702 if (wlen <= 0) /* error! */ | |
5703 return FAIL; | |
5704 len -= wlen; | |
5705 buf += wlen; | |
5706 } | |
5707 return OK; | |
5708 } | |
5709 | |
5710 #ifdef FEAT_MBYTE | |
5711 /* | |
5712 * Convert a Unicode character to bytes. | |
1947 | 5713 * Return TRUE for an error, FALSE when it's OK. |
7 | 5714 */ |
5715 static int | |
5716 ucs2bytes(c, pp, flags) | |
5717 unsigned c; /* in: character */ | |
5718 char_u **pp; /* in/out: pointer to result */ | |
5719 int flags; /* FIO_ flags */ | |
5720 { | |
5721 char_u *p = *pp; | |
5722 int error = FALSE; | |
5723 int cc; | |
5724 | |
5725 | |
5726 if (flags & FIO_UCS4) | |
5727 { | |
5728 if (flags & FIO_ENDIAN_L) | |
5729 { | |
5730 *p++ = c; | |
5731 *p++ = (c >> 8); | |
5732 *p++ = (c >> 16); | |
5733 *p++ = (c >> 24); | |
5734 } | |
5735 else | |
5736 { | |
5737 *p++ = (c >> 24); | |
5738 *p++ = (c >> 16); | |
5739 *p++ = (c >> 8); | |
5740 *p++ = c; | |
5741 } | |
5742 } | |
5743 else if (flags & (FIO_UCS2 | FIO_UTF16)) | |
5744 { | |
5745 if (c >= 0x10000) | |
5746 { | |
5747 if (flags & FIO_UTF16) | |
5748 { | |
5749 /* Make two words, ten bits of the character in each. First | |
5750 * word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff */ | |
5751 c -= 0x10000; | |
5752 if (c >= 0x100000) | |
5753 error = TRUE; | |
5754 cc = ((c >> 10) & 0x3ff) + 0xd800; | |
5755 if (flags & FIO_ENDIAN_L) | |
5756 { | |
5757 *p++ = cc; | |
5758 *p++ = ((unsigned)cc >> 8); | |
5759 } | |
5760 else | |
5761 { | |
5762 *p++ = ((unsigned)cc >> 8); | |
5763 *p++ = cc; | |
5764 } | |
5765 c = (c & 0x3ff) + 0xdc00; | |
5766 } | |
5767 else | |
5768 error = TRUE; | |
5769 } | |
5770 if (flags & FIO_ENDIAN_L) | |
5771 { | |
5772 *p++ = c; | |
5773 *p++ = (c >> 8); | |
5774 } | |
5775 else | |
5776 { | |
5777 *p++ = (c >> 8); | |
5778 *p++ = c; | |
5779 } | |
5780 } | |
5781 else /* Latin1 */ | |
5782 { | |
5783 if (c >= 0x100) | |
5784 { | |
5785 error = TRUE; | |
5786 *p++ = 0xBF; | |
5787 } | |
5788 else | |
5789 *p++ = c; | |
5790 } | |
5791 | |
5792 *pp = p; | |
5793 return error; | |
5794 } | |
5795 | |
5796 /* | |
1948 | 5797 * Return TRUE if file encoding "fenc" requires conversion from or to |
5798 * 'encoding'. | |
7 | 5799 */ |
5800 static int | |
1948 | 5801 need_conversion(fenc) |
5802 char_u *fenc; | |
7 | 5803 { |
1948 | 5804 int same_encoding; |
5805 int enc_flags; | |
5806 int fenc_flags; | |
5807 | |
5808 if (*fenc == NUL || STRCMP(p_enc, fenc) == 0) | |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5809 { |
1948 | 5810 same_encoding = TRUE; |
2217
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5811 fenc_flags = 0; |
120502692d82
Improve the MS-Windows installer.
Bram Moolenaar <bram@vim.org>
parents:
2215
diff
changeset
|
5812 } |
1948 | 5813 else |
5814 { | |
5815 /* Ignore difference between "ansi" and "latin1", "ucs-4" and | |
5816 * "ucs-4be", etc. */ | |
5817 enc_flags = get_fio_flags(p_enc); | |
5818 fenc_flags = get_fio_flags(fenc); | |
5819 same_encoding = (enc_flags != 0 && fenc_flags == enc_flags); | |
5820 } | |
5821 if (same_encoding) | |
5822 { | |
5823 /* Specified encoding matches with 'encoding'. This requires | |
5824 * conversion when 'encoding' is Unicode but not UTF-8. */ | |
5825 return enc_unicode != 0; | |
5826 } | |
5827 | |
5828 /* Encodings differ. However, conversion is not needed when 'enc' is any | |
5829 * Unicode encoding and the file is UTF-8. */ | |
5830 return !(enc_utf8 && fenc_flags == FIO_UTF8); | |
7 | 5831 } |
5832 | |
5833 /* | |
5834 * Check "ptr" for a unicode encoding and return the FIO_ flags needed for the | |
5835 * internal conversion. | |
5836 * if "ptr" is an empty string, use 'encoding'. | |
5837 */ | |
5838 static int | |
5839 get_fio_flags(ptr) | |
5840 char_u *ptr; | |
5841 { | |
5842 int prop; | |
5843 | |
5844 if (*ptr == NUL) | |
5845 ptr = p_enc; | |
5846 | |
5847 prop = enc_canon_props(ptr); | |
5848 if (prop & ENC_UNICODE) | |
5849 { | |
5850 if (prop & ENC_2BYTE) | |
5851 { | |
5852 if (prop & ENC_ENDIAN_L) | |
5853 return FIO_UCS2 | FIO_ENDIAN_L; | |
5854 return FIO_UCS2; | |
5855 } | |
5856 if (prop & ENC_4BYTE) | |
5857 { | |
5858 if (prop & ENC_ENDIAN_L) | |
5859 return FIO_UCS4 | FIO_ENDIAN_L; | |
5860 return FIO_UCS4; | |
5861 } | |
5862 if (prop & ENC_2WORD) | |
5863 { | |
5864 if (prop & ENC_ENDIAN_L) | |
5865 return FIO_UTF16 | FIO_ENDIAN_L; | |
5866 return FIO_UTF16; | |
5867 } | |
5868 return FIO_UTF8; | |
5869 } | |
5870 if (prop & ENC_LATIN1) | |
5871 return FIO_LATIN1; | |
5872 /* must be ENC_DBCS, requires iconv() */ | |
5873 return 0; | |
5874 } | |
5875 | |
5876 #ifdef WIN3264 | |
5877 /* | |
5878 * Check "ptr" for a MS-Windows codepage name and return the FIO_ flags needed | |
5879 * for the conversion MS-Windows can do for us. Also accept "utf-8". | |
5880 * Used for conversion between 'encoding' and 'fileencoding'. | |
5881 */ | |
5882 static int | |
5883 get_win_fio_flags(ptr) | |
5884 char_u *ptr; | |
5885 { | |
5886 int cp; | |
5887 | |
5888 /* Cannot do this when 'encoding' is not utf-8 and not a codepage. */ | |
5889 if (!enc_utf8 && enc_codepage <= 0) | |
5890 return 0; | |
5891 | |
5892 cp = encname2codepage(ptr); | |
5893 if (cp == 0) | |
5894 { | |
5895 # ifdef CP_UTF8 /* VC 4.1 doesn't define CP_UTF8 */ | |
5896 if (STRCMP(ptr, "utf-8") == 0) | |
5897 cp = CP_UTF8; | |
5898 else | |
5899 # endif | |
5900 return 0; | |
5901 } | |
5902 return FIO_PUT_CP(cp) | FIO_CODEPAGE; | |
5903 } | |
5904 #endif | |
5905 | |
5906 #ifdef MACOS_X | |
5907 /* | |
5908 * Check "ptr" for a Carbon supported encoding and return the FIO_ flags | |
5909 * needed for the internal conversion to/from utf-8 or latin1. | |
5910 */ | |
5911 static int | |
5912 get_mac_fio_flags(ptr) | |
5913 char_u *ptr; | |
5914 { | |
5915 if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0) | |
5916 && (enc_canon_props(ptr) & ENC_MACROMAN)) | |
5917 return FIO_MACROMAN; | |
5918 return 0; | |
5919 } | |
5920 #endif | |
5921 | |
5922 /* | |
5923 * Check for a Unicode BOM (Byte Order Mark) at the start of p[size]. | |
5924 * "size" must be at least 2. | |
5925 * Return the name of the encoding and set "*lenp" to the length. | |
5926 * Returns NULL when no BOM found. | |
5927 */ | |
5928 static char_u * | |
5929 check_for_bom(p, size, lenp, flags) | |
5930 char_u *p; | |
5931 long size; | |
5932 int *lenp; | |
5933 int flags; | |
5934 { | |
5935 char *name = NULL; | |
5936 int len = 2; | |
5937 | |
5938 if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf | |
1688 | 5939 && (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0)) |
7 | 5940 { |
5941 name = "utf-8"; /* EF BB BF */ | |
5942 len = 3; | |
5943 } | |
5944 else if (p[0] == 0xff && p[1] == 0xfe) | |
5945 { | |
5946 if (size >= 4 && p[2] == 0 && p[3] == 0 | |
5947 && (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L))) | |
5948 { | |
5949 name = "ucs-4le"; /* FF FE 00 00 */ | |
5950 len = 4; | |
5951 } | |
1735 | 5952 else if (flags == (FIO_UCS2 | FIO_ENDIAN_L)) |
7 | 5953 name = "ucs-2le"; /* FF FE */ |
1735 | 5954 else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L)) |
5955 /* utf-16le is preferred, it also works for ucs-2le text */ | |
7 | 5956 name = "utf-16le"; /* FF FE */ |
5957 } | |
5958 else if (p[0] == 0xfe && p[1] == 0xff | |
5959 && (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16)) | |
5960 { | |
1547 | 5961 /* Default to utf-16, it works also for ucs-2 text. */ |
5962 if (flags == FIO_UCS2) | |
5963 name = "ucs-2"; /* FE FF */ | |
5964 else | |
7 | 5965 name = "utf-16"; /* FE FF */ |
5966 } | |
5967 else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe | |
5968 && p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4)) | |
5969 { | |
5970 name = "ucs-4"; /* 00 00 FE FF */ | |
5971 len = 4; | |
5972 } | |
5973 | |
5974 *lenp = len; | |
5975 return (char_u *)name; | |
5976 } | |
5977 | |
5978 /* | |
5979 * Generate a BOM in "buf[4]" for encoding "name". | |
5980 * Return the length of the BOM (zero when no BOM). | |
5981 */ | |
5982 static int | |
5983 make_bom(buf, name) | |
5984 char_u *buf; | |
5985 char_u *name; | |
5986 { | |
5987 int flags; | |
5988 char_u *p; | |
5989 | |
5990 flags = get_fio_flags(name); | |
5991 | |
5992 /* Can't put a BOM in a non-Unicode file. */ | |
5993 if (flags == FIO_LATIN1 || flags == 0) | |
5994 return 0; | |
5995 | |
5996 if (flags == FIO_UTF8) /* UTF-8 */ | |
5997 { | |
5998 buf[0] = 0xef; | |
5999 buf[1] = 0xbb; | |
6000 buf[2] = 0xbf; | |
6001 return 3; | |
6002 } | |
6003 p = buf; | |
6004 (void)ucs2bytes(0xfeff, &p, flags); | |
6005 return (int)(p - buf); | |
6006 } | |
6007 #endif | |
6008 | |
1418 | 6009 #if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \ |
1471 | 6010 defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO) |
7 | 6011 /* |
6012 * Try to find a shortname by comparing the fullname with the current | |
6013 * directory. | |
1411 | 6014 * Returns "full_path" or pointer into "full_path" if shortened. |
6015 */ | |
6016 char_u * | |
6017 shorten_fname1(full_path) | |
6018 char_u *full_path; | |
6019 { | |
6020 char_u dirname[MAXPATHL]; | |
6021 char_u *p = full_path; | |
6022 | |
6023 if (mch_dirname(dirname, MAXPATHL) == OK) | |
6024 { | |
6025 p = shorten_fname(full_path, dirname); | |
6026 if (p == NULL || *p == NUL) | |
6027 p = full_path; | |
6028 } | |
6029 return p; | |
6030 } | |
1418 | 6031 #endif |
1411 | 6032 |
6033 /* | |
6034 * Try to find a shortname by comparing the fullname with the current | |
6035 * directory. | |
7 | 6036 * Returns NULL if not shorter name possible, pointer into "full_path" |
6037 * otherwise. | |
6038 */ | |
6039 char_u * | |
6040 shorten_fname(full_path, dir_name) | |
6041 char_u *full_path; | |
6042 char_u *dir_name; | |
6043 { | |
6044 int len; | |
6045 char_u *p; | |
6046 | |
6047 if (full_path == NULL) | |
6048 return NULL; | |
6049 len = (int)STRLEN(dir_name); | |
6050 if (fnamencmp(dir_name, full_path, len) == 0) | |
6051 { | |
6052 p = full_path + len; | |
6053 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
6054 /* | |
6055 * MSDOS: when a file is in the root directory, dir_name will end in a | |
6056 * slash, since C: by itself does not define a specific dir. In this | |
6057 * case p may already be correct. <negri> | |
6058 */ | |
6059 if (!((len > 2) && (*(p - 2) == ':'))) | |
6060 #endif | |
6061 { | |
6062 if (vim_ispathsep(*p)) | |
6063 ++p; | |
6064 #ifndef VMS /* the path separator is always part of the path */ | |
6065 else | |
6066 p = NULL; | |
6067 #endif | |
6068 } | |
6069 } | |
6070 #if defined(MSDOS) || defined(MSWIN) || defined(OS2) | |
6071 /* | |
6072 * When using a file in the current drive, remove the drive name: | |
6073 * "A:\dir\file" -> "\dir\file". This helps when moving a session file on | |
6074 * a floppy from "A:\dir" to "B:\dir". | |
6075 */ | |
6076 else if (len > 3 | |
6077 && TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0]) | |
6078 && full_path[1] == ':' | |
6079 && vim_ispathsep(full_path[2])) | |
6080 p = full_path + 2; | |
6081 #endif | |
6082 else | |
6083 p = NULL; | |
6084 return p; | |
6085 } | |
6086 | |
6087 /* | |
6088 * Shorten filenames for all buffers. | |
6089 * When "force" is TRUE: Use full path from now on for files currently being | |
6090 * edited, both for file name and swap file name. Try to shorten the file | |
6091 * names a bit, if safe to do so. | |
6092 * When "force" is FALSE: Only try to shorten absolute file names. | |
6093 * For buffers that have buftype "nofile" or "scratch": never change the file | |
6094 * name. | |
6095 */ | |
6096 void | |
6097 shorten_fnames(force) | |
6098 int force; | |
6099 { | |
6100 char_u dirname[MAXPATHL]; | |
6101 buf_T *buf; | |
6102 char_u *p; | |
6103 | |
6104 mch_dirname(dirname, MAXPATHL); | |
6105 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
6106 { | |
6107 if (buf->b_fname != NULL | |
6108 #ifdef FEAT_QUICKFIX | |
6109 && !bt_nofile(buf) | |
6110 #endif | |
6111 && !path_with_url(buf->b_fname) | |
6112 && (force | |
6113 || buf->b_sfname == NULL | |
6114 || mch_isFullName(buf->b_sfname))) | |
6115 { | |
6116 vim_free(buf->b_sfname); | |
6117 buf->b_sfname = NULL; | |
6118 p = shorten_fname(buf->b_ffname, dirname); | |
6119 if (p != NULL) | |
6120 { | |
6121 buf->b_sfname = vim_strsave(p); | |
6122 buf->b_fname = buf->b_sfname; | |
6123 } | |
6124 if (p == NULL || buf->b_fname == NULL) | |
6125 buf->b_fname = buf->b_ffname; | |
9 | 6126 } |
6127 | |
6128 /* Always make the swap file name a full path, a "nofile" buffer may | |
6129 * also have a swap file. */ | |
6130 mf_fullname(buf->b_ml.ml_mfp); | |
7 | 6131 } |
6132 #ifdef FEAT_WINDOWS | |
6133 status_redraw_all(); | |
672 | 6134 redraw_tabline = TRUE; |
7 | 6135 #endif |
6136 } | |
6137 | |
6138 #if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \ | |
6139 || defined(FEAT_GUI_MSWIN) \ | |
6140 || defined(FEAT_GUI_MAC) \ | |
6141 || defined(PROTO) | |
6142 /* | |
6143 * Shorten all filenames in "fnames[count]" by current directory. | |
6144 */ | |
6145 void | |
6146 shorten_filenames(fnames, count) | |
6147 char_u **fnames; | |
6148 int count; | |
6149 { | |
6150 int i; | |
6151 char_u dirname[MAXPATHL]; | |
6152 char_u *p; | |
6153 | |
6154 if (fnames == NULL || count < 1) | |
6155 return; | |
6156 mch_dirname(dirname, sizeof(dirname)); | |
6157 for (i = 0; i < count; ++i) | |
6158 { | |
6159 if ((p = shorten_fname(fnames[i], dirname)) != NULL) | |
6160 { | |
6161 /* shorten_fname() returns pointer in given "fnames[i]". If free | |
6162 * "fnames[i]" first, "p" becomes invalid. So we need to copy | |
6163 * "p" first then free fnames[i]. */ | |
6164 p = vim_strsave(p); | |
6165 vim_free(fnames[i]); | |
6166 fnames[i] = p; | |
6167 } | |
6168 } | |
6169 } | |
6170 #endif | |
6171 | |
6172 /* | |
1651 | 6173 * add extension to file name - change path/fo.o.h to path/fo.o.h.ext or |
7 | 6174 * fo_o_h.ext for MSDOS or when shortname option set. |
6175 * | |
6176 * Assumed that fname is a valid name found in the filesystem we assure that | |
6177 * the return value is a different name and ends in 'ext'. | |
6178 * "ext" MUST be at most 4 characters long if it starts with a dot, 3 | |
6179 * characters otherwise. | |
6180 * Space for the returned name is allocated, must be freed later. | |
6181 * Returns NULL when out of memory. | |
6182 */ | |
6183 char_u * | |
6184 modname(fname, ext, prepend_dot) | |
6185 char_u *fname, *ext; | |
6186 int prepend_dot; /* may prepend a '.' to file name */ | |
6187 { | |
6188 return buf_modname( | |
6189 #ifdef SHORT_FNAME | |
6190 TRUE, | |
6191 #else | |
6192 (curbuf->b_p_sn || curbuf->b_shortname), | |
6193 #endif | |
6194 fname, ext, prepend_dot); | |
6195 } | |
6196 | |
6197 char_u * | |
6198 buf_modname(shortname, fname, ext, prepend_dot) | |
6199 int shortname; /* use 8.3 file name */ | |
6200 char_u *fname, *ext; | |
6201 int prepend_dot; /* may prepend a '.' to file name */ | |
6202 { | |
6203 char_u *retval; | |
6204 char_u *s; | |
6205 char_u *e; | |
6206 char_u *ptr; | |
6207 int fnamelen, extlen; | |
6208 | |
6209 extlen = (int)STRLEN(ext); | |
6210 | |
6211 /* | |
6212 * If there is no file name we must get the name of the current directory | |
6213 * (we need the full path in case :cd is used). | |
6214 */ | |
6215 if (fname == NULL || *fname == NUL) | |
6216 { | |
6217 retval = alloc((unsigned)(MAXPATHL + extlen + 3)); | |
6218 if (retval == NULL) | |
6219 return NULL; | |
6220 if (mch_dirname(retval, MAXPATHL) == FAIL || | |
6221 (fnamelen = (int)STRLEN(retval)) == 0) | |
6222 { | |
6223 vim_free(retval); | |
6224 return NULL; | |
6225 } | |
39 | 6226 if (!after_pathsep(retval, retval + fnamelen)) |
7 | 6227 { |
6228 retval[fnamelen++] = PATHSEP; | |
6229 retval[fnamelen] = NUL; | |
6230 } | |
6231 #ifndef SHORT_FNAME | |
6232 prepend_dot = FALSE; /* nothing to prepend a dot to */ | |
6233 #endif | |
6234 } | |
6235 else | |
6236 { | |
6237 fnamelen = (int)STRLEN(fname); | |
6238 retval = alloc((unsigned)(fnamelen + extlen + 3)); | |
6239 if (retval == NULL) | |
6240 return NULL; | |
6241 STRCPY(retval, fname); | |
6242 #ifdef VMS | |
6243 vms_remove_version(retval); /* we do not need versions here */ | |
6244 #endif | |
6245 } | |
6246 | |
6247 /* | |
6248 * search backwards until we hit a '/', '\' or ':' replacing all '.' | |
6249 * by '_' for MSDOS or when shortname option set and ext starts with a dot. | |
6250 * Then truncate what is after the '/', '\' or ':' to 8 characters for | |
6251 * MSDOS and 26 characters for AMIGA, a lot more for UNIX. | |
6252 */ | |
391 | 6253 for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr)) |
7 | 6254 { |
6255 #ifndef RISCOS | |
6256 if (*ext == '.' | |
391 | 6257 # ifdef USE_LONG_FNAME |
7 | 6258 && (!USE_LONG_FNAME || shortname) |
391 | 6259 # else |
6260 # ifndef SHORT_FNAME | |
7 | 6261 && shortname |
391 | 6262 # endif |
7 | 6263 # endif |
6264 ) | |
6265 if (*ptr == '.') /* replace '.' by '_' */ | |
6266 *ptr = '_'; | |
391 | 6267 #endif |
7 | 6268 if (vim_ispathsep(*ptr)) |
391 | 6269 { |
6270 ++ptr; | |
7 | 6271 break; |
391 | 6272 } |
6273 } | |
7 | 6274 |
6275 /* the file name has at most BASENAMELEN characters. */ | |
6276 #ifndef SHORT_FNAME | |
6277 if (STRLEN(ptr) > (unsigned)BASENAMELEN) | |
6278 ptr[BASENAMELEN] = '\0'; | |
6279 #endif | |
6280 | |
6281 s = ptr + STRLEN(ptr); | |
6282 | |
6283 /* | |
6284 * For 8.3 file names we may have to reduce the length. | |
6285 */ | |
6286 #ifdef USE_LONG_FNAME | |
6287 if (!USE_LONG_FNAME || shortname) | |
6288 #else | |
6289 # ifndef SHORT_FNAME | |
6290 if (shortname) | |
6291 # endif | |
6292 #endif | |
6293 { | |
6294 /* | |
6295 * If there is no file name, or the file name ends in '/', and the | |
6296 * extension starts with '.', put a '_' before the dot, because just | |
6297 * ".ext" is invalid. | |
6298 */ | |
6299 if (fname == NULL || *fname == NUL | |
6300 || vim_ispathsep(fname[STRLEN(fname) - 1])) | |
6301 { | |
6302 #ifdef RISCOS | |
6303 if (*ext == '/') | |
6304 #else | |
6305 if (*ext == '.') | |
6306 #endif | |
6307 *s++ = '_'; | |
6308 } | |
6309 /* | |
6310 * If the extension starts with '.', truncate the base name at 8 | |
6311 * characters | |
6312 */ | |
6313 #ifdef RISCOS | |
6314 /* We normally use '/', but swap files are '_' */ | |
6315 else if (*ext == '/' || *ext == '_') | |
6316 #else | |
6317 else if (*ext == '.') | |
6318 #endif | |
6319 { | |
1877 | 6320 if ((size_t)(s - ptr) > (size_t)8) |
7 | 6321 { |
6322 s = ptr + 8; | |
6323 *s = '\0'; | |
6324 } | |
6325 } | |
6326 /* | |
6327 * If the extension doesn't start with '.', and the file name | |
6328 * doesn't have an extension yet, append a '.' | |
6329 */ | |
6330 #ifdef RISCOS | |
6331 else if ((e = vim_strchr(ptr, '/')) == NULL) | |
6332 *s++ = '/'; | |
6333 #else | |
6334 else if ((e = vim_strchr(ptr, '.')) == NULL) | |
6335 *s++ = '.'; | |
6336 #endif | |
6337 /* | |
6338 * If the extension doesn't start with '.', and there already is an | |
1201 | 6339 * extension, it may need to be truncated |
7 | 6340 */ |
6341 else if ((int)STRLEN(e) + extlen > 4) | |
6342 s = e + 4 - extlen; | |
6343 } | |
6344 #if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264) | |
6345 /* | |
6346 * If there is no file name, and the extension starts with '.', put a | |
6347 * '_' before the dot, because just ".ext" may be invalid if it's on a | |
6348 * FAT partition, and on HPFS it doesn't matter. | |
6349 */ | |
6350 else if ((fname == NULL || *fname == NUL) && *ext == '.') | |
6351 *s++ = '_'; | |
6352 #endif | |
6353 | |
6354 /* | |
1651 | 6355 * Append the extension. |
7 | 6356 * ext can start with '.' and cannot exceed 3 more characters. |
6357 */ | |
6358 STRCPY(s, ext); | |
6359 | |
6360 #ifndef SHORT_FNAME | |
6361 /* | |
6362 * Prepend the dot. | |
6363 */ | |
6364 if (prepend_dot && !shortname && *(e = gettail(retval)) != | |
6365 #ifdef RISCOS | |
6366 '/' | |
6367 #else | |
6368 '.' | |
6369 #endif | |
6370 #ifdef USE_LONG_FNAME | |
6371 && USE_LONG_FNAME | |
6372 #endif | |
6373 ) | |
6374 { | |
1620 | 6375 STRMOVE(e + 1, e); |
7 | 6376 #ifdef RISCOS |
6377 *e = '/'; | |
6378 #else | |
6379 *e = '.'; | |
6380 #endif | |
6381 } | |
6382 #endif | |
6383 | |
6384 /* | |
6385 * Check that, after appending the extension, the file name is really | |
6386 * different. | |
6387 */ | |
6388 if (fname != NULL && STRCMP(fname, retval) == 0) | |
6389 { | |
6390 /* we search for a character that can be replaced by '_' */ | |
6391 while (--s >= ptr) | |
6392 { | |
6393 if (*s != '_') | |
6394 { | |
6395 *s = '_'; | |
6396 break; | |
6397 } | |
6398 } | |
6399 if (s < ptr) /* fname was "________.<ext>", how tricky! */ | |
6400 *ptr = 'v'; | |
6401 } | |
6402 return retval; | |
6403 } | |
6404 | |
6405 /* | |
6406 * Like fgets(), but if the file line is too long, it is truncated and the | |
6407 * rest of the line is thrown away. Returns TRUE for end-of-file. | |
6408 */ | |
6409 int | |
6410 vim_fgets(buf, size, fp) | |
6411 char_u *buf; | |
6412 int size; | |
6413 FILE *fp; | |
6414 { | |
6415 char *eof; | |
6416 #define FGETS_SIZE 200 | |
6417 char tbuf[FGETS_SIZE]; | |
6418 | |
6419 buf[size - 2] = NUL; | |
6420 #ifdef USE_CR | |
6421 eof = fgets_cr((char *)buf, size, fp); | |
6422 #else | |
6423 eof = fgets((char *)buf, size, fp); | |
6424 #endif | |
6425 if (buf[size - 2] != NUL && buf[size - 2] != '\n') | |
6426 { | |
6427 buf[size - 1] = NUL; /* Truncate the line */ | |
6428 | |
6429 /* Now throw away the rest of the line: */ | |
6430 do | |
6431 { | |
6432 tbuf[FGETS_SIZE - 2] = NUL; | |
6433 #ifdef USE_CR | |
1757 | 6434 ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp); |
7 | 6435 #else |
1757 | 6436 ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp); |
7 | 6437 #endif |
6438 } while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n'); | |
6439 } | |
6440 return (eof == NULL); | |
6441 } | |
6442 | |
6443 #if defined(USE_CR) || defined(PROTO) | |
6444 /* | |
6445 * Like vim_fgets(), but accept any line terminator: CR, CR-LF or LF. | |
6446 * Returns TRUE for end-of-file. | |
6447 * Only used for the Mac, because it's much slower than vim_fgets(). | |
6448 */ | |
6449 int | |
6450 tag_fgets(buf, size, fp) | |
6451 char_u *buf; | |
6452 int size; | |
6453 FILE *fp; | |
6454 { | |
6455 int i = 0; | |
6456 int c; | |
6457 int eof = FALSE; | |
6458 | |
6459 for (;;) | |
6460 { | |
6461 c = fgetc(fp); | |
6462 if (c == EOF) | |
6463 { | |
6464 eof = TRUE; | |
6465 break; | |
6466 } | |
6467 if (c == '\r') | |
6468 { | |
6469 /* Always store a NL for end-of-line. */ | |
6470 if (i < size - 1) | |
6471 buf[i++] = '\n'; | |
6472 c = fgetc(fp); | |
6473 if (c != '\n') /* Macintosh format: single CR. */ | |
6474 ungetc(c, fp); | |
6475 break; | |
6476 } | |
6477 if (i < size - 1) | |
6478 buf[i++] = c; | |
6479 if (c == '\n') | |
6480 break; | |
6481 } | |
6482 buf[i] = NUL; | |
6483 return eof; | |
6484 } | |
6485 #endif | |
6486 | |
6487 /* | |
6488 * rename() only works if both files are on the same file system, this | |
6489 * function will (attempts to?) copy the file across if rename fails -- webb | |
6490 * Return -1 for failure, 0 for success. | |
6491 */ | |
6492 int | |
6493 vim_rename(from, to) | |
6494 char_u *from; | |
6495 char_u *to; | |
6496 { | |
6497 int fd_in; | |
6498 int fd_out; | |
6499 int n; | |
6500 char *errmsg = NULL; | |
6501 char *buffer; | |
6502 #ifdef AMIGA | |
6503 BPTR flock; | |
6504 #endif | |
6505 struct stat st; | |
194 | 6506 long perm; |
199 | 6507 #ifdef HAVE_ACL |
6508 vim_acl_T acl; /* ACL from original file */ | |
6509 #endif | |
1779 | 6510 #if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME) |
6511 int use_tmp_file = FALSE; | |
6512 #endif | |
7 | 6513 |
6514 /* | |
1779 | 6515 * When the names are identical, there is nothing to do. When they refer |
6516 * to the same file (ignoring case and slash/backslash differences) but | |
6517 * the file name differs we need to go through a temp file. | |
7 | 6518 */ |
6519 if (fnamecmp(from, to) == 0) | |
1779 | 6520 { |
6521 #ifdef CASE_INSENSITIVE_FILENAME | |
6522 if (STRCMP(gettail(from), gettail(to)) != 0) | |
6523 use_tmp_file = TRUE; | |
6524 else | |
6525 #endif | |
6526 return 0; | |
6527 } | |
7 | 6528 |
6529 /* | |
6530 * Fail if the "from" file doesn't exist. Avoids that "to" is deleted. | |
6531 */ | |
6532 if (mch_stat((char *)from, &st) < 0) | |
6533 return -1; | |
6534 | |
1778 | 6535 #ifdef UNIX |
6536 { | |
6537 struct stat st_to; | |
6538 | |
6539 /* It's possible for the source and destination to be the same file. | |
6540 * This happens when "from" and "to" differ in case and are on a FAT32 | |
6541 * filesystem. In that case go through a temp file name. */ | |
6542 if (mch_stat((char *)to, &st_to) >= 0 | |
6543 && st.st_dev == st_to.st_dev | |
6544 && st.st_ino == st_to.st_ino) | |
1779 | 6545 use_tmp_file = TRUE; |
6546 } | |
6547 #endif | |
6548 | |
6549 #if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME) | |
6550 if (use_tmp_file) | |
6551 { | |
6552 char tempname[MAXPATHL + 1]; | |
6553 | |
6554 /* | |
6555 * Find a name that doesn't exist and is in the same directory. | |
6556 * Rename "from" to "tempname" and then rename "tempname" to "to". | |
6557 */ | |
6558 if (STRLEN(from) >= MAXPATHL - 5) | |
6559 return -1; | |
6560 STRCPY(tempname, from); | |
6561 for (n = 123; n < 99999; ++n) | |
6562 { | |
6563 sprintf((char *)gettail((char_u *)tempname), "%d", n); | |
6564 if (mch_stat(tempname, &st) < 0) | |
6565 { | |
6566 if (mch_rename((char *)from, tempname) == 0) | |
1778 | 6567 { |
1779 | 6568 if (mch_rename(tempname, (char *)to) == 0) |
6569 return 0; | |
6570 /* Strange, the second step failed. Try moving the | |
6571 * file back and return failure. */ | |
6572 mch_rename(tempname, (char *)from); | |
1778 | 6573 return -1; |
6574 } | |
1779 | 6575 /* If it fails for one temp name it will most likely fail |
6576 * for any temp name, give up. */ | |
6577 return -1; | |
6578 } | |
6579 } | |
6580 return -1; | |
1778 | 6581 } |
6582 #endif | |
6583 | |
7 | 6584 /* |
6585 * Delete the "to" file, this is required on some systems to make the | |
6586 * mch_rename() work, on other systems it makes sure that we don't have | |
6587 * two files when the mch_rename() fails. | |
6588 */ | |
6589 | |
6590 #ifdef AMIGA | |
6591 /* | |
6592 * With MSDOS-compatible filesystems (crossdos, messydos) it is possible | |
6593 * that the name of the "to" file is the same as the "from" file, even | |
1201 | 6594 * though the names are different. To avoid the chance of accidentally |
7 | 6595 * deleting the "from" file (horror!) we lock it during the remove. |
6596 * | |
6597 * When used for making a backup before writing the file: This should not | |
6598 * happen with ":w", because startscript() should detect this problem and | |
6599 * set buf->b_shortname, causing modname() to return a correct ".bak" file | |
6600 * name. This problem does exist with ":w filename", but then the | |
6601 * original file will be somewhere else so the backup isn't really | |
6602 * important. If autoscripting is off the rename may fail. | |
6603 */ | |
6604 flock = Lock((UBYTE *)from, (long)ACCESS_READ); | |
6605 #endif | |
6606 mch_remove(to); | |
6607 #ifdef AMIGA | |
6608 if (flock) | |
6609 UnLock(flock); | |
6610 #endif | |
6611 | |
6612 /* | |
6613 * First try a normal rename, return if it works. | |
6614 */ | |
6615 if (mch_rename((char *)from, (char *)to) == 0) | |
6616 return 0; | |
6617 | |
6618 /* | |
6619 * Rename() failed, try copying the file. | |
6620 */ | |
194 | 6621 perm = mch_getperm(from); |
199 | 6622 #ifdef HAVE_ACL |
6623 /* For systems that support ACL: get the ACL from the original file. */ | |
6624 acl = mch_get_acl(from); | |
6625 #endif | |
7 | 6626 fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0); |
6627 if (fd_in == -1) | |
1651 | 6628 { |
6629 #ifdef HAVE_ACL | |
6630 mch_free_acl(acl); | |
6631 #endif | |
7 | 6632 return -1; |
1651 | 6633 } |
194 | 6634 |
6635 /* Create the new file with same permissions as the original. */ | |
557 | 6636 fd_out = mch_open((char *)to, |
6637 O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm); | |
7 | 6638 if (fd_out == -1) |
6639 { | |
6640 close(fd_in); | |
1651 | 6641 #ifdef HAVE_ACL |
6642 mch_free_acl(acl); | |
6643 #endif | |
7 | 6644 return -1; |
6645 } | |
6646 | |
6647 buffer = (char *)alloc(BUFSIZE); | |
6648 if (buffer == NULL) | |
6649 { | |
1651 | 6650 close(fd_out); |
7 | 6651 close(fd_in); |
1651 | 6652 #ifdef HAVE_ACL |
6653 mch_free_acl(acl); | |
6654 #endif | |
7 | 6655 return -1; |
6656 } | |
6657 | |
6658 while ((n = vim_read(fd_in, buffer, BUFSIZE)) > 0) | |
6659 if (vim_write(fd_out, buffer, n) != n) | |
6660 { | |
6661 errmsg = _("E208: Error writing to \"%s\""); | |
6662 break; | |
6663 } | |
6664 | |
6665 vim_free(buffer); | |
6666 close(fd_in); | |
6667 if (close(fd_out) < 0) | |
6668 errmsg = _("E209: Error closing \"%s\""); | |
6669 if (n < 0) | |
6670 { | |
6671 errmsg = _("E210: Error reading \"%s\""); | |
6672 to = from; | |
6673 } | |
1201 | 6674 #ifndef UNIX /* for Unix mch_open() already set the permission */ |
194 | 6675 mch_setperm(to, perm); |
570 | 6676 #endif |
199 | 6677 #ifdef HAVE_ACL |
6678 mch_set_acl(to, acl); | |
1651 | 6679 mch_free_acl(acl); |
199 | 6680 #endif |
7 | 6681 if (errmsg != NULL) |
6682 { | |
6683 EMSG2(errmsg, to); | |
6684 return -1; | |
6685 } | |
6686 mch_remove(from); | |
6687 return 0; | |
6688 } | |
6689 | |
6690 static int already_warned = FALSE; | |
6691 | |
6692 /* | |
6693 * Check if any not hidden buffer has been changed. | |
6694 * Postpone the check if there are characters in the stuff buffer, a global | |
6695 * command is being executed, a mapping is being executed or an autocommand is | |
6696 * busy. | |
6697 * Returns TRUE if some message was written (screen should be redrawn and | |
6698 * cursor positioned). | |
6699 */ | |
6700 int | |
6701 check_timestamps(focus) | |
6702 int focus; /* called for GUI focus event */ | |
6703 { | |
6704 buf_T *buf; | |
6705 int didit = 0; | |
6706 int n; | |
6707 | |
6708 /* Don't check timestamps while system() or another low-level function may | |
6709 * cause us to lose and gain focus. */ | |
6710 if (no_check_timestamps > 0) | |
6711 return FALSE; | |
6712 | |
6713 /* Avoid doing a check twice. The OK/Reload dialog can cause a focus | |
6714 * event and we would keep on checking if the file is steadily growing. | |
6715 * Do check again after typing something. */ | |
6716 if (focus && did_check_timestamps) | |
6717 { | |
6718 need_check_timestamps = TRUE; | |
6719 return FALSE; | |
6720 } | |
6721 | |
6722 if (!stuff_empty() || global_busy || !typebuf_typed() | |
6723 #ifdef FEAT_AUTOCMD | |
1834 | 6724 || autocmd_busy || curbuf_lock > 0 || allbuf_lock > 0 |
7 | 6725 #endif |
6726 ) | |
6727 need_check_timestamps = TRUE; /* check later */ | |
6728 else | |
6729 { | |
6730 ++no_wait_return; | |
6731 did_check_timestamps = TRUE; | |
6732 already_warned = FALSE; | |
6733 for (buf = firstbuf; buf != NULL; ) | |
6734 { | |
6735 /* Only check buffers in a window. */ | |
6736 if (buf->b_nwindows > 0) | |
6737 { | |
6738 n = buf_check_timestamp(buf, focus); | |
6739 if (didit < n) | |
6740 didit = n; | |
6741 if (n > 0 && !buf_valid(buf)) | |
6742 { | |
6743 /* Autocommands have removed the buffer, start at the | |
6744 * first one again. */ | |
6745 buf = firstbuf; | |
6746 continue; | |
6747 } | |
6748 } | |
6749 buf = buf->b_next; | |
6750 } | |
6751 --no_wait_return; | |
6752 need_check_timestamps = FALSE; | |
6753 if (need_wait_return && didit == 2) | |
6754 { | |
6755 /* make sure msg isn't overwritten */ | |
6756 msg_puts((char_u *)"\n"); | |
6757 out_flush(); | |
6758 } | |
6759 } | |
6760 return didit; | |
6761 } | |
6762 | |
6763 /* | |
6764 * Move all the lines from buffer "frombuf" to buffer "tobuf". | |
6765 * Return OK or FAIL. When FAIL "tobuf" is incomplete and/or "frombuf" is not | |
6766 * empty. | |
6767 */ | |
6768 static int | |
6769 move_lines(frombuf, tobuf) | |
6770 buf_T *frombuf; | |
6771 buf_T *tobuf; | |
6772 { | |
6773 buf_T *tbuf = curbuf; | |
6774 int retval = OK; | |
6775 linenr_T lnum; | |
6776 char_u *p; | |
6777 | |
6778 /* Copy the lines in "frombuf" to "tobuf". */ | |
6779 curbuf = tobuf; | |
6780 for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum) | |
6781 { | |
6782 p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE)); | |
6783 if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL) | |
6784 { | |
6785 vim_free(p); | |
6786 retval = FAIL; | |
6787 break; | |
6788 } | |
6789 vim_free(p); | |
6790 } | |
6791 | |
6792 /* Delete all the lines in "frombuf". */ | |
6793 if (retval != FAIL) | |
6794 { | |
6795 curbuf = frombuf; | |
1055 | 6796 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum) |
6797 if (ml_delete(lnum, FALSE) == FAIL) | |
7 | 6798 { |
6799 /* Oops! We could try putting back the saved lines, but that | |
6800 * might fail again... */ | |
6801 retval = FAIL; | |
6802 break; | |
6803 } | |
6804 } | |
6805 | |
6806 curbuf = tbuf; | |
6807 return retval; | |
6808 } | |
6809 | |
6810 /* | |
6811 * Check if buffer "buf" has been changed. | |
6812 * Also check if the file for a new buffer unexpectedly appeared. | |
6813 * return 1 if a changed buffer was found. | |
6814 * return 2 if a message has been displayed. | |
6815 * return 0 otherwise. | |
6816 */ | |
6817 int | |
6818 buf_check_timestamp(buf, focus) | |
6819 buf_T *buf; | |
1877 | 6820 int focus UNUSED; /* called for GUI focus event */ |
7 | 6821 { |
6822 struct stat st; | |
6823 int stat_res; | |
6824 int retval = 0; | |
6825 char_u *path; | |
6826 char_u *tbuf; | |
6827 char *mesg = NULL; | |
188 | 6828 char *mesg2 = ""; |
7 | 6829 int helpmesg = FALSE; |
6830 int reload = FALSE; | |
6831 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6832 int can_reload = FALSE; | |
6833 #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
|
6834 off_t orig_size = buf->b_orig_size; |
7 | 6835 int orig_mode = buf->b_orig_mode; |
6836 #ifdef FEAT_GUI | |
6837 int save_mouse_correct = need_mouse_correct; | |
6838 #endif | |
6839 #ifdef FEAT_AUTOCMD | |
6840 static int busy = FALSE; | |
179 | 6841 int n; |
6842 char_u *s; | |
6843 #endif | |
6844 char *reason; | |
7 | 6845 |
6846 /* If there is no file name, the buffer is not loaded, 'buftype' is | |
6847 * set, we are in the middle of a save or being called recursively: ignore | |
6848 * this buffer. */ | |
6849 if (buf->b_ffname == NULL | |
6850 || buf->b_ml.ml_mfp == NULL | |
6851 #if defined(FEAT_QUICKFIX) | |
6852 || *buf->b_p_bt != NUL | |
6853 #endif | |
6854 || buf->b_saving | |
6855 #ifdef FEAT_AUTOCMD | |
6856 || busy | |
6857 #endif | |
33 | 6858 #ifdef FEAT_NETBEANS_INTG |
6859 || isNetbeansBuffer(buf) | |
6860 #endif | |
7 | 6861 ) |
6862 return 0; | |
6863 | |
6864 if ( !(buf->b_flags & BF_NOTEDITED) | |
6865 && buf->b_mtime != 0 | |
6866 && ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0 | |
6867 || time_differs((long)st.st_mtime, buf->b_mtime) | |
6868 #ifdef HAVE_ST_MODE | |
6869 || (int)st.st_mode != buf->b_orig_mode | |
6870 #else | |
6871 || mch_getperm(buf->b_ffname) != buf->b_orig_mode | |
6872 #endif | |
6873 )) | |
6874 { | |
6875 retval = 1; | |
6876 | |
628 | 6877 /* set b_mtime to stop further warnings (e.g., when executing |
6878 * FileChangedShell autocmd) */ | |
7 | 6879 if (stat_res < 0) |
6880 { | |
6881 buf->b_mtime = 0; | |
6882 buf->b_orig_size = 0; | |
6883 buf->b_orig_mode = 0; | |
6884 } | |
6885 else | |
6886 buf_store_time(buf, &st, buf->b_ffname); | |
6887 | |
6888 /* Don't do anything for a directory. Might contain the file | |
6889 * explorer. */ | |
6890 if (mch_isdir(buf->b_fname)) | |
6891 ; | |
6892 | |
6893 /* | |
6894 * If 'autoread' is set, the buffer has no changes and the file still | |
6895 * exists, reload the buffer. Use the buffer-local option value if it | |
6896 * was set, the global option value otherwise. | |
6897 */ | |
6898 else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar) | |
6899 && !bufIsChanged(buf) && stat_res >= 0) | |
6900 reload = TRUE; | |
6901 else | |
6902 { | |
179 | 6903 if (stat_res < 0) |
6904 reason = "deleted"; | |
6905 else if (bufIsChanged(buf)) | |
6906 reason = "conflict"; | |
6907 else if (orig_size != buf->b_orig_size || buf_contents_changed(buf)) | |
6908 reason = "changed"; | |
6909 else if (orig_mode != buf->b_orig_mode) | |
6910 reason = "mode"; | |
6911 else | |
6912 reason = "time"; | |
6913 | |
7 | 6914 #ifdef FEAT_AUTOCMD |
6915 /* | |
6916 * Only give the warning if there are no FileChangedShell | |
6917 * autocommands. | |
6918 * Avoid being called recursively by setting "busy". | |
6919 */ | |
6920 busy = TRUE; | |
532 | 6921 # ifdef FEAT_EVAL |
179 | 6922 set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1); |
6923 set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1); | |
532 | 6924 # endif |
1834 | 6925 ++allbuf_lock; |
7 | 6926 n = apply_autocmds(EVENT_FILECHANGEDSHELL, |
6927 buf->b_fname, buf->b_fname, FALSE, buf); | |
1834 | 6928 --allbuf_lock; |
7 | 6929 busy = FALSE; |
6930 if (n) | |
6931 { | |
6932 if (!buf_valid(buf)) | |
6933 EMSG(_("E246: FileChangedShell autocommand deleted buffer")); | |
532 | 6934 # ifdef FEAT_EVAL |
179 | 6935 s = get_vim_var_str(VV_FCS_CHOICE); |
6936 if (STRCMP(s, "reload") == 0 && *reason != 'd') | |
6937 reload = TRUE; | |
6938 else if (STRCMP(s, "ask") == 0) | |
6939 n = FALSE; | |
6940 else | |
532 | 6941 # endif |
179 | 6942 return 2; |
7 | 6943 } |
179 | 6944 if (!n) |
7 | 6945 #endif |
6946 { | |
179 | 6947 if (*reason == 'd') |
6948 mesg = _("E211: File \"%s\" no longer available"); | |
7 | 6949 else |
6950 { | |
6951 helpmesg = TRUE; | |
6952 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6953 can_reload = TRUE; | |
6954 #endif | |
6955 /* | |
6956 * Check if the file contents really changed to avoid | |
6957 * giving a warning when only the timestamp was set (e.g., | |
6958 * checked out of CVS). Always warn when the buffer was | |
6959 * changed. | |
6960 */ | |
179 | 6961 if (reason[2] == 'n') |
6962 { | |
7 | 6963 mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well"); |
179 | 6964 mesg2 = _("See \":help W12\" for more info."); |
6965 } | |
6966 else if (reason[1] == 'h') | |
6967 { | |
7 | 6968 mesg = _("W11: Warning: File \"%s\" has changed since editing started"); |
179 | 6969 mesg2 = _("See \":help W11\" for more info."); |
6970 } | |
6971 else if (*reason == 'm') | |
6972 { | |
7 | 6973 mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started"); |
179 | 6974 mesg2 = _("See \":help W16\" for more info."); |
6975 } | |
1913 | 6976 else |
6977 /* Only timestamp changed, store it to avoid a warning | |
6978 * in check_mtime() later. */ | |
6979 buf->b_mtime_read = buf->b_mtime; | |
7 | 6980 } |
6981 } | |
6982 } | |
6983 | |
6984 } | |
6985 else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W) | |
6986 && vim_fexists(buf->b_ffname)) | |
6987 { | |
6988 retval = 1; | |
6989 mesg = _("W13: Warning: File \"%s\" has been created after editing started"); | |
6990 buf->b_flags |= BF_NEW_W; | |
6991 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) | |
6992 can_reload = TRUE; | |
6993 #endif | |
6994 } | |
6995 | |
6996 if (mesg != NULL) | |
6997 { | |
6998 path = home_replace_save(buf, buf->b_fname); | |
6999 if (path != NULL) | |
7000 { | |
179 | 7001 if (!helpmesg) |
7 | 7002 mesg2 = ""; |
7003 tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg) | |
7004 + STRLEN(mesg2) + 2)); | |
7005 sprintf((char *)tbuf, mesg, path); | |
1848 | 7006 #ifdef FEAT_EVAL |
7007 /* Set warningmsg here, before the unimportant and output-specific | |
7008 * mesg2 has been appended. */ | |
7009 set_vim_var_string(VV_WARNINGMSG, tbuf, -1); | |
7010 #endif | |
7 | 7011 #if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG) |
7012 if (can_reload) | |
7013 { | |
7014 if (*mesg2 != NUL) | |
7015 { | |
7016 STRCAT(tbuf, "\n"); | |
7017 STRCAT(tbuf, mesg2); | |
7018 } | |
7019 if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf, | |
7020 (char_u *)_("&OK\n&Load File"), 1, NULL) == 2) | |
7021 reload = TRUE; | |
7022 } | |
7023 else | |
7024 #endif | |
7025 if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned) | |
7026 { | |
7027 if (*mesg2 != NUL) | |
7028 { | |
7029 STRCAT(tbuf, "; "); | |
7030 STRCAT(tbuf, mesg2); | |
7031 } | |
7032 EMSG(tbuf); | |
7033 retval = 2; | |
7034 } | |
7035 else | |
7036 { | |
8 | 7037 # ifdef FEAT_AUTOCMD |
7 | 7038 if (!autocmd_busy) |
8 | 7039 # endif |
7 | 7040 { |
7041 msg_start(); | |
7042 msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST); | |
7043 if (*mesg2 != NUL) | |
7044 msg_puts_attr((char_u *)mesg2, | |
7045 hl_attr(HLF_W) + MSG_HIST); | |
7046 msg_clr_eos(); | |
7047 (void)msg_end(); | |
7048 if (emsg_silent == 0) | |
7049 { | |
7050 out_flush(); | |
8 | 7051 # ifdef FEAT_GUI |
7 | 7052 if (!focus) |
8 | 7053 # endif |
7 | 7054 /* give the user some time to think about it */ |
7055 ui_delay(1000L, TRUE); | |
7056 | |
7057 /* don't redraw and erase the message */ | |
7058 redraw_cmdline = FALSE; | |
7059 } | |
7060 } | |
7061 already_warned = TRUE; | |
7062 } | |
7063 | |
7064 vim_free(path); | |
7065 vim_free(tbuf); | |
7066 } | |
7067 } | |
7068 | |
7069 if (reload) | |
311 | 7070 /* Reload the buffer. */ |
628 | 7071 buf_reload(buf, orig_mode); |
7 | 7072 |
765 | 7073 #ifdef FEAT_AUTOCMD |
1620 | 7074 /* Trigger FileChangedShell when the file was changed in any way. */ |
7075 if (buf_valid(buf) && retval != 0) | |
765 | 7076 (void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST, |
7077 buf->b_fname, buf->b_fname, FALSE, buf); | |
7078 #endif | |
7 | 7079 #ifdef FEAT_GUI |
7080 /* restore this in case an autocommand has set it; it would break | |
7081 * 'mousefocus' */ | |
7082 need_mouse_correct = save_mouse_correct; | |
7083 #endif | |
7084 | |
7085 return retval; | |
7086 } | |
7087 | |
311 | 7088 /* |
7089 * Reload a buffer that is already loaded. | |
7090 * Used when the file was changed outside of Vim. | |
628 | 7091 * "orig_mode" is buf->b_orig_mode before the need for reloading was detected. |
7092 * buf->b_orig_mode may have been reset already. | |
311 | 7093 */ |
7094 void | |
628 | 7095 buf_reload(buf, orig_mode) |
311 | 7096 buf_T *buf; |
628 | 7097 int orig_mode; |
311 | 7098 { |
7099 exarg_T ea; | |
7100 pos_T old_cursor; | |
7101 linenr_T old_topline; | |
7102 int old_ro = buf->b_p_ro; | |
7103 buf_T *savebuf; | |
7104 int saved = OK; | |
7105 aco_save_T aco; | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7106 int flags = READ_NEW; |
311 | 7107 |
7108 /* set curwin/curbuf for "buf" and save some things */ | |
7109 aucmd_prepbuf(&aco, buf); | |
7110 | |
7111 /* We only want to read the text from the file, not reset the syntax | |
7112 * highlighting, clear marks, diff status, etc. Force the fileformat | |
7113 * and encoding to be the same. */ | |
7114 if (prep_exarg(&ea, buf) == OK) | |
7115 { | |
7116 old_cursor = curwin->w_cursor; | |
7117 old_topline = curwin->w_topline; | |
7118 | |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7119 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7120 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7121 /* Save all the text, so that the reload can be undone. |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7122 * Sync first so that this is a separate undo-able action. */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7123 u_sync(FALSE); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7124 saved = u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7125 flags |= READ_KEEP_UNDO; |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7126 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7127 |
311 | 7128 /* |
7129 * To behave like when a new file is edited (matters for | |
7130 * BufReadPost autocommands) we first need to delete the current | |
7131 * buffer contents. But if reading the file fails we should keep | |
7132 * the old contents. Can't use memory only, the file might be | |
7133 * too big. Use a hidden buffer to move the buffer contents to. | |
7134 */ | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7135 if (bufempty() || saved == FAIL) |
311 | 7136 savebuf = NULL; |
7137 else | |
7138 { | |
7139 /* Allocate a buffer without putting it in the buffer list. */ | |
7140 savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY); | |
837 | 7141 if (savebuf != NULL && buf == curbuf) |
311 | 7142 { |
7143 /* Open the memline. */ | |
7144 curbuf = savebuf; | |
7145 curwin->w_buffer = savebuf; | |
625 | 7146 saved = ml_open(curbuf); |
311 | 7147 curbuf = buf; |
7148 curwin->w_buffer = buf; | |
7149 } | |
837 | 7150 if (savebuf == NULL || saved == FAIL || buf != curbuf |
311 | 7151 || move_lines(buf, savebuf) == FAIL) |
7152 { | |
7153 EMSG2(_("E462: Could not prepare for reloading \"%s\""), | |
7154 buf->b_fname); | |
7155 saved = FAIL; | |
7156 } | |
7157 } | |
7158 | |
7159 if (saved == OK) | |
7160 { | |
7161 curbuf->b_flags |= BF_CHECK_RO; /* check for RO again */ | |
7162 #ifdef FEAT_AUTOCMD | |
7163 keep_filetype = TRUE; /* don't detect 'filetype' */ | |
7164 #endif | |
7165 if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0, | |
7166 (linenr_T)0, | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7167 (linenr_T)MAXLNUM, &ea, flags) == FAIL) |
311 | 7168 { |
7169 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) | |
7170 if (!aborting()) | |
7171 #endif | |
7172 EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname); | |
837 | 7173 if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf) |
311 | 7174 { |
7175 /* Put the text back from the save buffer. First | |
7176 * delete any lines that readfile() added. */ | |
7177 while (!bufempty()) | |
837 | 7178 if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL) |
311 | 7179 break; |
7180 (void)move_lines(savebuf, buf); | |
7181 } | |
7182 } | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7183 else if (buf == curbuf) /* "buf" still valid */ |
311 | 7184 { |
7185 /* Mark the buffer as unmodified and free undo info. */ | |
7186 unchanged(buf, TRUE); | |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7187 if ((flags & READ_KEEP_UNDO) == 0) |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7188 { |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7189 u_blockfree(buf); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7190 u_clearall(buf); |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7191 } |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7192 else |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7193 { |
2394
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7194 /* Mark all undo states as changed. */ |
a3aca345aafa
Add the 'undoreload' option to be able to undo a file reload.
Bram Moolenaar <bram@vim.org>
parents:
2360
diff
changeset
|
7195 u_unchanged(curbuf); |
2482
88751831fa0a
When undoing a reload, move the cursor to the first changed line.
Bram Moolenaar <bram@vim.org>
parents:
2410
diff
changeset
|
7196 } |
311 | 7197 } |
7198 } | |
7199 vim_free(ea.cmd); | |
7200 | |
837 | 7201 if (savebuf != NULL && buf_valid(savebuf)) |
311 | 7202 wipe_buffer(savebuf, FALSE); |
7203 | |
7204 #ifdef FEAT_DIFF | |
7205 /* Invalidate diff info if necessary. */ | |
837 | 7206 diff_invalidate(curbuf); |
311 | 7207 #endif |
7208 | |
7209 /* Restore the topline and cursor position and check it (lines may | |
7210 * have been removed). */ | |
7211 if (old_topline > curbuf->b_ml.ml_line_count) | |
7212 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
7213 else | |
7214 curwin->w_topline = old_topline; | |
7215 curwin->w_cursor = old_cursor; | |
7216 check_cursor(); | |
7217 update_topline(); | |
7218 #ifdef FEAT_AUTOCMD | |
7219 keep_filetype = FALSE; | |
7220 #endif | |
7221 #ifdef FEAT_FOLDING | |
7222 { | |
1863 | 7223 win_T *wp; |
7224 tabpage_T *tp; | |
311 | 7225 |
7226 /* Update folds unless they are defined manually. */ | |
1863 | 7227 FOR_ALL_TAB_WINDOWS(tp, wp) |
311 | 7228 if (wp->w_buffer == curwin->w_buffer |
7229 && !foldmethodIsManual(wp)) | |
7230 foldUpdateAll(wp); | |
7231 } | |
7232 #endif | |
7233 /* If the mode didn't change and 'readonly' was set, keep the old | |
7234 * value; the user probably used the ":view" command. But don't | |
7235 * reset it, might have had a read error. */ | |
7236 if (orig_mode == curbuf->b_orig_mode) | |
7237 curbuf->b_p_ro |= old_ro; | |
7238 } | |
7239 | |
7240 /* restore curwin/curbuf and a few other things */ | |
7241 aucmd_restbuf(&aco); | |
7242 /* Careful: autocommands may have made "buf" invalid! */ | |
7243 } | |
7244 | |
7 | 7245 void |
7246 buf_store_time(buf, st, fname) | |
7247 buf_T *buf; | |
7248 struct stat *st; | |
1877 | 7249 char_u *fname UNUSED; |
7 | 7250 { |
7251 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
|
7252 buf->b_orig_size = st->st_size; |
7 | 7253 #ifdef HAVE_ST_MODE |
7254 buf->b_orig_mode = (int)st->st_mode; | |
7255 #else | |
7256 buf->b_orig_mode = mch_getperm(fname); | |
7257 #endif | |
7258 } | |
7259 | |
7260 /* | |
7261 * Adjust the line with missing eol, used for the next write. | |
7262 * Used for do_filter(), when the input lines for the filter are deleted. | |
7263 */ | |
7264 void | |
7265 write_lnum_adjust(offset) | |
7266 linenr_T offset; | |
7267 { | |
167 | 7268 if (write_no_eol_lnum != 0) /* only if there is a missing eol */ |
7 | 7269 write_no_eol_lnum += offset; |
7270 } | |
7271 | |
7272 #if defined(TEMPDIRNAMES) || defined(PROTO) | |
7273 static long temp_count = 0; /* Temp filename counter. */ | |
7274 | |
7275 /* | |
7276 * Delete the temp directory and all files it contains. | |
7277 */ | |
7278 void | |
7279 vim_deltempdir() | |
7280 { | |
7281 char_u **files; | |
7282 int file_count; | |
7283 int i; | |
7284 | |
7285 if (vim_tempdir != NULL) | |
7286 { | |
7287 sprintf((char *)NameBuff, "%s*", vim_tempdir); | |
7288 if (gen_expand_wildcards(1, &NameBuff, &file_count, &files, | |
7289 EW_DIR|EW_FILE|EW_SILENT) == OK) | |
7290 { | |
7291 for (i = 0; i < file_count; ++i) | |
7292 mch_remove(files[i]); | |
7293 FreeWild(file_count, files); | |
7294 } | |
7295 gettail(NameBuff)[-1] = NUL; | |
7296 (void)mch_rmdir(NameBuff); | |
7297 | |
7298 vim_free(vim_tempdir); | |
7299 vim_tempdir = NULL; | |
7300 } | |
7301 } | |
7302 #endif | |
7303 | |
2006 | 7304 #ifdef TEMPDIRNAMES |
7 | 7305 /* |
1997 | 7306 * Directory "tempdir" was created. Expand this name to a full path and put |
7307 * it in "vim_tempdir". This avoids that using ":cd" would confuse us. | |
7308 * "tempdir" must be no longer than MAXPATHL. | |
7309 */ | |
7310 static void | |
7311 vim_settempdir(tempdir) | |
7312 char_u *tempdir; | |
7313 { | |
7314 char_u *buf; | |
7315 | |
7316 buf = alloc((unsigned)MAXPATHL + 2); | |
7317 if (buf != NULL) | |
7318 { | |
7319 if (vim_FullName(tempdir, buf, MAXPATHL, FALSE) == FAIL) | |
7320 STRCPY(buf, tempdir); | |
7321 # ifdef __EMX__ | |
7322 if (vim_strchr(buf, '/') != NULL) | |
7323 STRCAT(buf, "/"); | |
7324 else | |
7325 # endif | |
7326 add_pathsep(buf); | |
7327 vim_tempdir = vim_strsave(buf); | |
7328 vim_free(buf); | |
7329 } | |
7330 } | |
2006 | 7331 #endif |
1997 | 7332 |
7333 /* | |
7 | 7334 * vim_tempname(): Return a unique name that can be used for a temp file. |
7335 * | |
7336 * The temp file is NOT created. | |
7337 * | |
7338 * The returned pointer is to allocated memory. | |
7339 * The returned pointer is NULL if no valid name was found. | |
7340 */ | |
7341 char_u * | |
7342 vim_tempname(extra_char) | |
1877 | 7343 int extra_char UNUSED; /* char to use in the name instead of '?' */ |
7 | 7344 { |
7345 #ifdef USE_TMPNAM | |
7346 char_u itmp[L_tmpnam]; /* use tmpnam() */ | |
7347 #else | |
7348 char_u itmp[TEMPNAMELEN]; | |
7349 #endif | |
7350 | |
7351 #ifdef TEMPDIRNAMES | |
7352 static char *(tempdirs[]) = {TEMPDIRNAMES}; | |
7353 int i; | |
7354 # ifndef EEXIST | |
7355 struct stat st; | |
7356 # endif | |
7357 | |
7358 /* | |
7359 * This will create a directory for private use by this instance of Vim. | |
7360 * This is done once, and the same directory is used for all temp files. | |
7361 * This method avoids security problems because of symlink attacks et al. | |
7362 * It's also a bit faster, because we only need to check for an existing | |
7363 * file when creating the directory and not for each temp file. | |
7364 */ | |
7365 if (vim_tempdir == NULL) | |
7366 { | |
7367 /* | |
7368 * Try the entries in TEMPDIRNAMES to create the temp directory. | |
7369 */ | |
1877 | 7370 for (i = 0; i < (int)(sizeof(tempdirs) / sizeof(char *)); ++i) |
7 | 7371 { |
2047
85da03763130
updated for version 7.2.333
Bram Moolenaar <bram@zimbu.org>
parents:
2028
diff
changeset
|
7372 # ifndef HAVE_MKDTEMP |
1997 | 7373 size_t itmplen; |
7374 long nr; | |
7375 long off; | |
7376 # endif | |
7377 | |
7 | 7378 /* expand $TMP, leave room for "/v1100000/999999999" */ |
7379 expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20); | |
7380 if (mch_isdir(itmp)) /* directory exists */ | |
7381 { | |
7382 # ifdef __EMX__ | |
7383 /* If $TMP contains a forward slash (perhaps using bash or | |
7384 * tcsh), don't add a backslash, use a forward slash! | |
7385 * Adding 2 backslashes didn't work. */ | |
7386 if (vim_strchr(itmp, '/') != NULL) | |
7387 STRCAT(itmp, "/"); | |
7388 else | |
7389 # endif | |
7390 add_pathsep(itmp); | |
1997 | 7391 |
7392 # ifdef HAVE_MKDTEMP | |
7393 /* Leave room for filename */ | |
7394 STRCAT(itmp, "vXXXXXX"); | |
7395 if (mkdtemp((char *)itmp) != NULL) | |
7396 vim_settempdir(itmp); | |
7397 # else | |
7 | 7398 /* Get an arbitrary number of up to 6 digits. When it's |
7399 * unlikely that it already exists it will be faster, | |
7400 * otherwise it doesn't matter. The use of mkdir() avoids any | |
7401 * security problems because of the predictable number. */ | |
7402 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
|
7403 itmplen = STRLEN(itmp); |
7 | 7404 |
7405 /* Try up to 10000 different values until we find a name that | |
7406 * doesn't exist. */ | |
7407 for (off = 0; off < 10000L; ++off) | |
7408 { | |
7409 int r; | |
1997 | 7410 # if defined(UNIX) || defined(VMS) |
7 | 7411 mode_t umask_save; |
1997 | 7412 # endif |
7413 | |
7414 sprintf((char *)itmp + itmplen, "v%ld", nr + off); | |
7415 # ifndef EEXIST | |
7 | 7416 /* If mkdir() does not set errno to EEXIST, check for |
7417 * existing file here. There is a race condition then, | |
7418 * although it's fail-safe. */ | |
7419 if (mch_stat((char *)itmp, &st) >= 0) | |
7420 continue; | |
1997 | 7421 # endif |
7422 # if defined(UNIX) || defined(VMS) | |
7 | 7423 /* Make sure the umask doesn't remove the executable bit. |
7424 * "repl" has been reported to use "177". */ | |
7425 umask_save = umask(077); | |
1997 | 7426 # endif |
7 | 7427 r = vim_mkdir(itmp, 0700); |
1997 | 7428 # if defined(UNIX) || defined(VMS) |
7 | 7429 (void)umask(umask_save); |
1997 | 7430 # endif |
7 | 7431 if (r == 0) |
7432 { | |
1997 | 7433 vim_settempdir(itmp); |
7 | 7434 break; |
7435 } | |
1997 | 7436 # ifdef EEXIST |
7 | 7437 /* If the mkdir() didn't fail because the file/dir exists, |
7438 * we probably can't create any dir here, try another | |
7439 * place. */ | |
7440 if (errno != EEXIST) | |
1997 | 7441 # endif |
7 | 7442 break; |
7443 } | |
1997 | 7444 # endif /* HAVE_MKDTEMP */ |
7 | 7445 if (vim_tempdir != NULL) |
7446 break; | |
7447 } | |
7448 } | |
7449 } | |
7450 | |
7451 if (vim_tempdir != NULL) | |
7452 { | |
7453 /* There is no need to check if the file exists, because we own the | |
7454 * directory and nobody else creates a file in it. */ | |
7455 sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++); | |
7456 return vim_strsave(itmp); | |
7457 } | |
7458 | |
7459 return NULL; | |
7460 | |
7461 #else /* TEMPDIRNAMES */ | |
7462 | |
7463 # ifdef WIN3264 | |
7464 char szTempFile[_MAX_PATH + 1]; | |
7465 char buf4[4]; | |
7466 char_u *retval; | |
7467 char_u *p; | |
7468 | |
7469 STRCPY(itmp, ""); | |
7470 if (GetTempPath(_MAX_PATH, szTempFile) == 0) | |
7471 szTempFile[0] = NUL; /* GetTempPath() failed, use current dir */ | |
7472 strcpy(buf4, "VIM"); | |
7473 buf4[2] = extra_char; /* make it "VIa", "VIb", etc. */ | |
7474 if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0) | |
7475 return NULL; | |
7476 /* GetTempFileName() will create the file, we don't want that */ | |
7477 (void)DeleteFile(itmp); | |
7478 | |
7479 /* Backslashes in a temp file name cause problems when filtering with | |
7480 * "sh". NOTE: This also checks 'shellcmdflag' to help those people who | |
7481 * didn't set 'shellslash'. */ | |
7482 retval = vim_strsave(itmp); | |
7483 if (*p_shcf == '-' || p_ssl) | |
7484 for (p = retval; *p; ++p) | |
7485 if (*p == '\\') | |
7486 *p = '/'; | |
7487 return retval; | |
7488 | |
7489 # else /* WIN3264 */ | |
7490 | |
7491 # ifdef USE_TMPNAM | |
7492 /* tmpnam() will make its own name */ | |
7493 if (*tmpnam((char *)itmp) == NUL) | |
7494 return NULL; | |
7495 # else | |
7496 char_u *p; | |
7497 | |
7498 # ifdef VMS_TEMPNAM | |
7499 /* mktemp() is not working on VMS. It seems to be | |
7500 * a do-nothing function. Therefore we use tempnam(). | |
7501 */ | |
7502 sprintf((char *)itmp, "VIM%c", extra_char); | |
7503 p = (char_u *)tempnam("tmp:", (char *)itmp); | |
7504 if (p != NULL) | |
7505 { | |
7506 /* VMS will use '.LOG' if we don't explicitly specify an extension, | |
7507 * and VIM will then be unable to find the file later */ | |
7508 STRCPY(itmp, p); | |
7509 STRCAT(itmp, ".txt"); | |
7510 free(p); | |
7511 } | |
7512 else | |
7513 return NULL; | |
7514 # else | |
7515 STRCPY(itmp, TEMPNAME); | |
7516 if ((p = vim_strchr(itmp, '?')) != NULL) | |
7517 *p = extra_char; | |
7518 if (mktemp((char *)itmp) == NULL) | |
7519 return NULL; | |
7520 # endif | |
7521 # endif | |
7522 | |
7523 return vim_strsave(itmp); | |
7524 # endif /* WIN3264 */ | |
7525 #endif /* TEMPDIRNAMES */ | |
7526 } | |
7527 | |
7528 #if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) | |
7529 /* | |
7530 * Convert all backslashes in fname to forward slashes in-place. | |
7531 */ | |
7532 void | |
7533 forward_slash(fname) | |
7534 char_u *fname; | |
7535 { | |
7536 char_u *p; | |
7537 | |
7538 for (p = fname; *p != NUL; ++p) | |
7539 # ifdef FEAT_MBYTE | |
7540 /* The Big5 encoding can have '\' in the trail byte. */ | |
474 | 7541 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 7542 ++p; |
7543 else | |
7544 # endif | |
7545 if (*p == '\\') | |
7546 *p = '/'; | |
7547 } | |
7548 #endif | |
7549 | |
7550 | |
7551 /* | |
7552 * Code for automatic commands. | |
7553 * | |
7554 * Only included when "FEAT_AUTOCMD" has been defined. | |
7555 */ | |
7556 | |
7557 #if defined(FEAT_AUTOCMD) || defined(PROTO) | |
7558 | |
7559 /* | |
7560 * The autocommands are stored in a list for each event. | |
7561 * Autocommands for the same pattern, that are consecutive, are joined | |
7562 * together, to avoid having to match the pattern too often. | |
7563 * The result is an array of Autopat lists, which point to AutoCmd lists: | |
7564 * | |
7565 * first_autopat[0] --> Autopat.next --> Autopat.next --> NULL | |
7566 * Autopat.cmds Autopat.cmds | |
7567 * | | | |
7568 * V V | |
7569 * AutoCmd.next AutoCmd.next | |
7570 * | | | |
7571 * V V | |
7572 * AutoCmd.next NULL | |
7573 * | | |
7574 * V | |
7575 * NULL | |
7576 * | |
7577 * first_autopat[1] --> Autopat.next --> NULL | |
7578 * Autopat.cmds | |
7579 * | | |
7580 * V | |
7581 * AutoCmd.next | |
7582 * | | |
7583 * V | |
7584 * NULL | |
7585 * etc. | |
7586 * | |
7587 * The order of AutoCmds is important, this is the order in which they were | |
7588 * defined and will have to be executed. | |
7589 */ | |
7590 typedef struct AutoCmd | |
7591 { | |
7592 char_u *cmd; /* The command to be executed (NULL | |
7593 when command has been removed) */ | |
7594 char nested; /* If autocommands nest here */ | |
7595 char last; /* last command in list */ | |
7596 #ifdef FEAT_EVAL | |
7597 scid_T scriptID; /* script ID where defined */ | |
7598 #endif | |
7599 struct AutoCmd *next; /* Next AutoCmd in list */ | |
7600 } AutoCmd; | |
7601 | |
7602 typedef struct AutoPat | |
7603 { | |
7604 int group; /* group ID */ | |
7605 char_u *pat; /* pattern as typed (NULL when pattern | |
7606 has been removed) */ | |
7607 int patlen; /* strlen() of pat */ | |
153 | 7608 regprog_T *reg_prog; /* compiled regprog for pattern */ |
7 | 7609 char allow_dirs; /* Pattern may match whole path */ |
7610 char last; /* last pattern for apply_autocmds() */ | |
7611 AutoCmd *cmds; /* list of commands to do */ | |
7612 struct AutoPat *next; /* next AutoPat in AutoPat list */ | |
40 | 7613 int buflocal_nr; /* !=0 for buffer-local AutoPat */ |
7 | 7614 } AutoPat; |
7615 | |
7616 static struct event_name | |
7617 { | |
7618 char *name; /* event name */ | |
661 | 7619 event_T event; /* event number */ |
7 | 7620 } event_names[] = |
7621 { | |
7622 {"BufAdd", EVENT_BUFADD}, | |
7623 {"BufCreate", EVENT_BUFADD}, | |
7624 {"BufDelete", EVENT_BUFDELETE}, | |
7625 {"BufEnter", EVENT_BUFENTER}, | |
7626 {"BufFilePost", EVENT_BUFFILEPOST}, | |
7627 {"BufFilePre", EVENT_BUFFILEPRE}, | |
7628 {"BufHidden", EVENT_BUFHIDDEN}, | |
7629 {"BufLeave", EVENT_BUFLEAVE}, | |
7630 {"BufNew", EVENT_BUFNEW}, | |
7631 {"BufNewFile", EVENT_BUFNEWFILE}, | |
7632 {"BufRead", EVENT_BUFREADPOST}, | |
7633 {"BufReadCmd", EVENT_BUFREADCMD}, | |
7634 {"BufReadPost", EVENT_BUFREADPOST}, | |
7635 {"BufReadPre", EVENT_BUFREADPRE}, | |
7636 {"BufUnload", EVENT_BUFUNLOAD}, | |
7637 {"BufWinEnter", EVENT_BUFWINENTER}, | |
7638 {"BufWinLeave", EVENT_BUFWINLEAVE}, | |
7639 {"BufWipeout", EVENT_BUFWIPEOUT}, | |
7640 {"BufWrite", EVENT_BUFWRITEPRE}, | |
7641 {"BufWritePost", EVENT_BUFWRITEPOST}, | |
7642 {"BufWritePre", EVENT_BUFWRITEPRE}, | |
7643 {"BufWriteCmd", EVENT_BUFWRITECMD}, | |
7644 {"CmdwinEnter", EVENT_CMDWINENTER}, | |
7645 {"CmdwinLeave", EVENT_CMDWINLEAVE}, | |
12 | 7646 {"ColorScheme", EVENT_COLORSCHEME}, |
661 | 7647 {"CursorHold", EVENT_CURSORHOLD}, |
7648 {"CursorHoldI", EVENT_CURSORHOLDI}, | |
7649 {"CursorMoved", EVENT_CURSORMOVED}, | |
7650 {"CursorMovedI", EVENT_CURSORMOVEDI}, | |
7 | 7651 {"EncodingChanged", EVENT_ENCODINGCHANGED}, |
7652 {"FileEncoding", EVENT_ENCODINGCHANGED}, | |
7653 {"FileAppendPost", EVENT_FILEAPPENDPOST}, | |
7654 {"FileAppendPre", EVENT_FILEAPPENDPRE}, | |
7655 {"FileAppendCmd", EVENT_FILEAPPENDCMD}, | |
7656 {"FileChangedShell",EVENT_FILECHANGEDSHELL}, | |
765 | 7657 {"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST}, |
7 | 7658 {"FileChangedRO", EVENT_FILECHANGEDRO}, |
7659 {"FileReadPost", EVENT_FILEREADPOST}, | |
7660 {"FileReadPre", EVENT_FILEREADPRE}, | |
7661 {"FileReadCmd", EVENT_FILEREADCMD}, | |
7662 {"FileType", EVENT_FILETYPE}, | |
7663 {"FileWritePost", EVENT_FILEWRITEPOST}, | |
7664 {"FileWritePre", EVENT_FILEWRITEPRE}, | |
7665 {"FileWriteCmd", EVENT_FILEWRITECMD}, | |
7666 {"FilterReadPost", EVENT_FILTERREADPOST}, | |
7667 {"FilterReadPre", EVENT_FILTERREADPRE}, | |
7668 {"FilterWritePost", EVENT_FILTERWRITEPOST}, | |
7669 {"FilterWritePre", EVENT_FILTERWRITEPRE}, | |
7670 {"FocusGained", EVENT_FOCUSGAINED}, | |
7671 {"FocusLost", EVENT_FOCUSLOST}, | |
7672 {"FuncUndefined", EVENT_FUNCUNDEFINED}, | |
7673 {"GUIEnter", EVENT_GUIENTER}, | |
946 | 7674 {"GUIFailed", EVENT_GUIFAILED}, |
11 | 7675 {"InsertChange", EVENT_INSERTCHANGE}, |
7676 {"InsertEnter", EVENT_INSERTENTER}, | |
7677 {"InsertLeave", EVENT_INSERTLEAVE}, | |
434 | 7678 {"MenuPopup", EVENT_MENUPOPUP}, |
161 | 7679 {"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST}, |
7680 {"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE}, | |
7 | 7681 {"RemoteReply", EVENT_REMOTEREPLY}, |
574 | 7682 {"SessionLoadPost", EVENT_SESSIONLOADPOST}, |
723 | 7683 {"ShellCmdPost", EVENT_SHELLCMDPOST}, |
7684 {"ShellFilterPost", EVENT_SHELLFILTERPOST}, | |
715 | 7685 {"SourcePre", EVENT_SOURCEPRE}, |
1061 | 7686 {"SourceCmd", EVENT_SOURCECMD}, |
649 | 7687 {"SpellFileMissing",EVENT_SPELLFILEMISSING}, |
7 | 7688 {"StdinReadPost", EVENT_STDINREADPOST}, |
7689 {"StdinReadPre", EVENT_STDINREADPRE}, | |
674 | 7690 {"SwapExists", EVENT_SWAPEXISTS}, |
7 | 7691 {"Syntax", EVENT_SYNTAX}, |
676 | 7692 {"TabEnter", EVENT_TABENTER}, |
7693 {"TabLeave", EVENT_TABLEAVE}, | |
7 | 7694 {"TermChanged", EVENT_TERMCHANGED}, |
7695 {"TermResponse", EVENT_TERMRESPONSE}, | |
7696 {"User", EVENT_USER}, | |
7697 {"VimEnter", EVENT_VIMENTER}, | |
7698 {"VimLeave", EVENT_VIMLEAVE}, | |
7699 {"VimLeavePre", EVENT_VIMLEAVEPRE}, | |
7700 {"WinEnter", EVENT_WINENTER}, | |
7701 {"WinLeave", EVENT_WINLEAVE}, | |
765 | 7702 {"VimResized", EVENT_VIMRESIZED}, |
661 | 7703 {NULL, (event_T)0} |
7 | 7704 }; |
7705 | |
7706 static AutoPat *first_autopat[NUM_EVENTS] = | |
7707 { | |
7708 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7709 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7710 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
7711 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, | |
18 | 7712 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
7713 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
7 | 7714 }; |
7715 | |
7716 /* | |
7717 * struct used to keep status while executing autocommands for an event. | |
7718 */ | |
7719 typedef struct AutoPatCmd | |
7720 { | |
7721 AutoPat *curpat; /* next AutoPat to examine */ | |
7722 AutoCmd *nextcmd; /* next AutoCmd to execute */ | |
7723 int group; /* group being used */ | |
7724 char_u *fname; /* fname to match with */ | |
7725 char_u *sfname; /* sfname to match with */ | |
7726 char_u *tail; /* tail of fname */ | |
661 | 7727 event_T event; /* current event */ |
40 | 7728 int arg_bufnr; /* initially equal to <abuf>, set to zero when |
7729 buf is deleted */ | |
7730 struct AutoPatCmd *next; /* chain of active apc-s for auto-invalidation*/ | |
7 | 7731 } AutoPatCmd; |
7732 | |
297 | 7733 static AutoPatCmd *active_apc_list = NULL; /* stack of active autocommands */ |
40 | 7734 |
7 | 7735 /* |
7736 * augroups stores a list of autocmd group names. | |
7737 */ | |
297 | 7738 static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL}; |
7 | 7739 #define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i]) |
7740 | |
7741 /* | |
7742 * The ID of the current group. Group 0 is the default one. | |
7743 */ | |
7744 static int current_augroup = AUGROUP_DEFAULT; | |
7745 | |
7746 static int au_need_clean = FALSE; /* need to delete marked patterns */ | |
7747 | |
661 | 7748 static void show_autocmd __ARGS((AutoPat *ap, event_T event)); |
7 | 7749 static void au_remove_pat __ARGS((AutoPat *ap)); |
7750 static void au_remove_cmds __ARGS((AutoPat *ap)); | |
7751 static void au_cleanup __ARGS((void)); | |
7752 static int au_new_group __ARGS((char_u *name)); | |
7753 static void au_del_group __ARGS((char_u *name)); | |
661 | 7754 static event_T event_name2nr __ARGS((char_u *start, char_u **end)); |
7755 static char_u *event_nr2name __ARGS((event_T event)); | |
7 | 7756 static char_u *find_end_event __ARGS((char_u *arg, int have_group)); |
661 | 7757 static int event_ignored __ARGS((event_T event)); |
7 | 7758 static int au_get_grouparg __ARGS((char_u **argp)); |
661 | 7759 static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group)); |
7 | 7760 static char_u *getnextac __ARGS((int c, void *cookie, int indent)); |
661 | 7761 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 | 7762 static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last)); |
7763 | |
40 | 7764 |
661 | 7765 static event_T last_event; |
7 | 7766 static int last_group; |
1410 | 7767 static int autocmd_blocked = 0; /* block all autocmds */ |
7 | 7768 |
7769 /* | |
7770 * Show the autocommands for one AutoPat. | |
7771 */ | |
7772 static void | |
7773 show_autocmd(ap, event) | |
7774 AutoPat *ap; | |
661 | 7775 event_T event; |
7 | 7776 { |
7777 AutoCmd *ac; | |
7778 | |
7779 /* Check for "got_int" (here and at various places below), which is set | |
7780 * when "q" has been hit for the "--more--" prompt */ | |
7781 if (got_int) | |
7782 return; | |
7783 if (ap->pat == NULL) /* pattern has been removed */ | |
7784 return; | |
7785 | |
7786 msg_putchar('\n'); | |
7787 if (got_int) | |
7788 return; | |
7789 if (event != last_event || ap->group != last_group) | |
7790 { | |
7791 if (ap->group != AUGROUP_DEFAULT) | |
7792 { | |
7793 if (AUGROUP_NAME(ap->group) == NULL) | |
7794 msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E)); | |
7795 else | |
7796 msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T)); | |
7797 msg_puts((char_u *)" "); | |
7798 } | |
7799 msg_puts_attr(event_nr2name(event), hl_attr(HLF_T)); | |
7800 last_event = event; | |
7801 last_group = ap->group; | |
7802 msg_putchar('\n'); | |
7803 if (got_int) | |
7804 return; | |
7805 } | |
7806 msg_col = 4; | |
7807 msg_outtrans(ap->pat); | |
7808 | |
7809 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7810 { | |
7811 if (ac->cmd != NULL) /* skip removed commands */ | |
7812 { | |
7813 if (msg_col >= 14) | |
7814 msg_putchar('\n'); | |
7815 msg_col = 14; | |
7816 if (got_int) | |
7817 return; | |
7818 msg_outtrans(ac->cmd); | |
500 | 7819 #ifdef FEAT_EVAL |
7820 if (p_verbose > 0) | |
7821 last_set_msg(ac->scriptID); | |
7822 #endif | |
7 | 7823 if (got_int) |
7824 return; | |
7825 if (ac->next != NULL) | |
7826 { | |
7827 msg_putchar('\n'); | |
7828 if (got_int) | |
7829 return; | |
7830 } | |
7831 } | |
7832 } | |
7833 } | |
7834 | |
7835 /* | |
7836 * Mark an autocommand pattern for deletion. | |
7837 */ | |
7838 static void | |
7839 au_remove_pat(ap) | |
7840 AutoPat *ap; | |
7841 { | |
7842 vim_free(ap->pat); | |
7843 ap->pat = NULL; | |
40 | 7844 ap->buflocal_nr = -1; |
7 | 7845 au_need_clean = TRUE; |
7846 } | |
7847 | |
7848 /* | |
7849 * Mark all commands for a pattern for deletion. | |
7850 */ | |
7851 static void | |
7852 au_remove_cmds(ap) | |
7853 AutoPat *ap; | |
7854 { | |
7855 AutoCmd *ac; | |
7856 | |
7857 for (ac = ap->cmds; ac != NULL; ac = ac->next) | |
7858 { | |
7859 vim_free(ac->cmd); | |
7860 ac->cmd = NULL; | |
7861 } | |
7862 au_need_clean = TRUE; | |
7863 } | |
7864 | |
7865 /* | |
7866 * Cleanup autocommands and patterns that have been deleted. | |
7867 * This is only done when not executing autocommands. | |
7868 */ | |
7869 static void | |
7870 au_cleanup() | |
7871 { | |
7872 AutoPat *ap, **prev_ap; | |
7873 AutoCmd *ac, **prev_ac; | |
661 | 7874 event_T event; |
7 | 7875 |
7876 if (autocmd_busy || !au_need_clean) | |
7877 return; | |
7878 | |
7879 /* loop over all events */ | |
661 | 7880 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7881 event = (event_T)((int)event + 1)) | |
7 | 7882 { |
7883 /* loop over all autocommand patterns */ | |
7884 prev_ap = &(first_autopat[(int)event]); | |
7885 for (ap = *prev_ap; ap != NULL; ap = *prev_ap) | |
7886 { | |
7887 /* loop over all commands for this pattern */ | |
7888 prev_ac = &(ap->cmds); | |
7889 for (ac = *prev_ac; ac != NULL; ac = *prev_ac) | |
7890 { | |
7891 /* remove the command if the pattern is to be deleted or when | |
7892 * the command has been marked for deletion */ | |
7893 if (ap->pat == NULL || ac->cmd == NULL) | |
7894 { | |
7895 *prev_ac = ac->next; | |
7896 vim_free(ac->cmd); | |
7897 vim_free(ac); | |
7898 } | |
7899 else | |
7900 prev_ac = &(ac->next); | |
7901 } | |
7902 | |
7903 /* remove the pattern if it has been marked for deletion */ | |
7904 if (ap->pat == NULL) | |
7905 { | |
7906 *prev_ap = ap->next; | |
153 | 7907 vim_free(ap->reg_prog); |
7 | 7908 vim_free(ap); |
7909 } | |
7910 else | |
7911 prev_ap = &(ap->next); | |
7912 } | |
7913 } | |
7914 | |
7915 au_need_clean = FALSE; | |
7916 } | |
7917 | |
7918 /* | |
40 | 7919 * Called when buffer is freed, to remove/invalidate related buffer-local |
7920 * autocmds. | |
7921 */ | |
7922 void | |
7923 aubuflocal_remove(buf) | |
7924 buf_T *buf; | |
7925 { | |
7926 AutoPat *ap; | |
661 | 7927 event_T event; |
40 | 7928 AutoPatCmd *apc; |
7929 | |
7930 /* invalidate currently executing autocommands */ | |
7931 for (apc = active_apc_list; apc; apc = apc->next) | |
7932 if (buf->b_fnum == apc->arg_bufnr) | |
7933 apc->arg_bufnr = 0; | |
7934 | |
7935 /* invalidate buflocals looping through events */ | |
661 | 7936 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
7937 event = (event_T)((int)event + 1)) | |
40 | 7938 /* loop over all autocommand patterns */ |
7939 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
7940 if (ap->buflocal_nr == buf->b_fnum) | |
7941 { | |
7942 au_remove_pat(ap); | |
7943 if (p_verbose >= 6) | |
292 | 7944 { |
7945 verbose_enter(); | |
40 | 7946 smsg((char_u *) |
7947 _("auto-removing autocommand: %s <buffer=%d>"), | |
7948 event_nr2name(event), buf->b_fnum); | |
292 | 7949 verbose_leave(); |
7950 } | |
40 | 7951 } |
7952 au_cleanup(); | |
7953 } | |
7954 | |
7955 /* | |
7 | 7956 * Add an autocmd group name. |
7957 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
7958 */ | |
7959 static int | |
7960 au_new_group(name) | |
7961 char_u *name; | |
7962 { | |
7963 int i; | |
7964 | |
7965 i = au_find_group(name); | |
7966 if (i == AUGROUP_ERROR) /* the group doesn't exist yet, add it */ | |
7967 { | |
7968 /* First try using a free entry. */ | |
7969 for (i = 0; i < augroups.ga_len; ++i) | |
7970 if (AUGROUP_NAME(i) == NULL) | |
7971 break; | |
7972 if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL) | |
7973 return AUGROUP_ERROR; | |
7974 | |
7975 AUGROUP_NAME(i) = vim_strsave(name); | |
7976 if (AUGROUP_NAME(i) == NULL) | |
7977 return AUGROUP_ERROR; | |
7978 if (i == augroups.ga_len) | |
7979 ++augroups.ga_len; | |
7980 } | |
7981 | |
7982 return i; | |
7983 } | |
7984 | |
7985 static void | |
7986 au_del_group(name) | |
7987 char_u *name; | |
7988 { | |
7989 int i; | |
7990 | |
7991 i = au_find_group(name); | |
7992 if (i == AUGROUP_ERROR) /* the group doesn't exist */ | |
7993 EMSG2(_("E367: No such group: \"%s\""), name); | |
7994 else | |
7995 { | |
7996 vim_free(AUGROUP_NAME(i)); | |
7997 AUGROUP_NAME(i) = NULL; | |
7998 } | |
7999 } | |
8000 | |
8001 /* | |
8002 * Find the ID of an autocmd group name. | |
8003 * Return it's ID. Returns AUGROUP_ERROR (< 0) for error. | |
8004 */ | |
8005 static int | |
8006 au_find_group(name) | |
8007 char_u *name; | |
8008 { | |
8009 int i; | |
8010 | |
8011 for (i = 0; i < augroups.ga_len; ++i) | |
8012 if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0) | |
8013 return i; | |
8014 return AUGROUP_ERROR; | |
8015 } | |
8016 | |
461 | 8017 /* |
8018 * Return TRUE if augroup "name" exists. | |
8019 */ | |
8020 int | |
8021 au_has_group(name) | |
8022 char_u *name; | |
8023 { | |
8024 return au_find_group(name) != AUGROUP_ERROR; | |
8025 } | |
8026 | |
7 | 8027 /* |
8028 * ":augroup {name}". | |
8029 */ | |
8030 void | |
8031 do_augroup(arg, del_group) | |
8032 char_u *arg; | |
8033 int del_group; | |
8034 { | |
8035 int i; | |
8036 | |
8037 if (del_group) | |
8038 { | |
8039 if (*arg == NUL) | |
8040 EMSG(_(e_argreq)); | |
8041 else | |
8042 au_del_group(arg); | |
8043 } | |
8044 else if (STRICMP(arg, "end") == 0) /* ":aug end": back to group 0 */ | |
8045 current_augroup = AUGROUP_DEFAULT; | |
8046 else if (*arg) /* ":aug xxx": switch to group xxx */ | |
8047 { | |
8048 i = au_new_group(arg); | |
8049 if (i != AUGROUP_ERROR) | |
8050 current_augroup = i; | |
8051 } | |
8052 else /* ":aug": list the group names */ | |
8053 { | |
8054 msg_start(); | |
8055 for (i = 0; i < augroups.ga_len; ++i) | |
8056 { | |
8057 if (AUGROUP_NAME(i) != NULL) | |
8058 { | |
8059 msg_puts(AUGROUP_NAME(i)); | |
8060 msg_puts((char_u *)" "); | |
8061 } | |
8062 } | |
8063 msg_clr_eos(); | |
8064 msg_end(); | |
8065 } | |
8066 } | |
8067 | |
355 | 8068 #if defined(EXITFREE) || defined(PROTO) |
8069 void | |
8070 free_all_autocmds() | |
8071 { | |
8072 for (current_augroup = -1; current_augroup < augroups.ga_len; | |
8073 ++current_augroup) | |
8074 do_autocmd((char_u *)"", TRUE); | |
8075 ga_clear_strings(&augroups); | |
8076 } | |
8077 #endif | |
8078 | |
7 | 8079 /* |
8080 * Return the event number for event name "start". | |
8081 * Return NUM_EVENTS if the event name was not found. | |
8082 * Return a pointer to the next event name in "end". | |
8083 */ | |
661 | 8084 static event_T |
7 | 8085 event_name2nr(start, end) |
8086 char_u *start; | |
8087 char_u **end; | |
8088 { | |
8089 char_u *p; | |
8090 int i; | |
8091 int len; | |
8092 | |
8093 /* the event name ends with end of line, a blank or a comma */ | |
8094 for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p) | |
8095 ; | |
8096 for (i = 0; event_names[i].name != NULL; ++i) | |
8097 { | |
8098 len = (int)STRLEN(event_names[i].name); | |
8099 if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0) | |
8100 break; | |
8101 } | |
8102 if (*p == ',') | |
8103 ++p; | |
8104 *end = p; | |
8105 if (event_names[i].name == NULL) | |
8106 return NUM_EVENTS; | |
8107 return event_names[i].event; | |
8108 } | |
8109 | |
8110 /* | |
8111 * Return the name for event "event". | |
8112 */ | |
8113 static char_u * | |
8114 event_nr2name(event) | |
661 | 8115 event_T event; |
7 | 8116 { |
8117 int i; | |
8118 | |
8119 for (i = 0; event_names[i].name != NULL; ++i) | |
8120 if (event_names[i].event == event) | |
8121 return (char_u *)event_names[i].name; | |
8122 return (char_u *)"Unknown"; | |
8123 } | |
8124 | |
8125 /* | |
8126 * Scan over the events. "*" stands for all events. | |
8127 */ | |
8128 static char_u * | |
8129 find_end_event(arg, have_group) | |
8130 char_u *arg; | |
8131 int have_group; /* TRUE when group name was found */ | |
8132 { | |
8133 char_u *pat; | |
8134 char_u *p; | |
8135 | |
8136 if (*arg == '*') | |
8137 { | |
8138 if (arg[1] && !vim_iswhite(arg[1])) | |
8139 { | |
8140 EMSG2(_("E215: Illegal character after *: %s"), arg); | |
8141 return NULL; | |
8142 } | |
8143 pat = arg + 1; | |
8144 } | |
8145 else | |
8146 { | |
8147 for (pat = arg; *pat && !vim_iswhite(*pat); pat = p) | |
8148 { | |
8149 if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS) | |
8150 { | |
8151 if (have_group) | |
8152 EMSG2(_("E216: No such event: %s"), pat); | |
8153 else | |
8154 EMSG2(_("E216: No such group or event: %s"), pat); | |
8155 return NULL; | |
8156 } | |
8157 } | |
8158 } | |
8159 return pat; | |
8160 } | |
8161 | |
8162 /* | |
8163 * Return TRUE if "event" is included in 'eventignore'. | |
8164 */ | |
8165 static int | |
8166 event_ignored(event) | |
661 | 8167 event_T event; |
7 | 8168 { |
8169 char_u *p = p_ei; | |
8170 | |
844 | 8171 while (*p != NUL) |
8172 { | |
8173 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8174 return TRUE; | |
7 | 8175 if (event_name2nr(p, &p) == event) |
8176 return TRUE; | |
844 | 8177 } |
7 | 8178 |
8179 return FALSE; | |
8180 } | |
8181 | |
8182 /* | |
8183 * Return OK when the contents of p_ei is valid, FAIL otherwise. | |
8184 */ | |
8185 int | |
8186 check_ei() | |
8187 { | |
8188 char_u *p = p_ei; | |
8189 | |
8190 while (*p) | |
844 | 8191 { |
8192 if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ',')) | |
8193 { | |
8194 p += 3; | |
8195 if (*p == ',') | |
8196 ++p; | |
8197 } | |
8198 else if (event_name2nr(p, &p) == NUM_EVENTS) | |
7 | 8199 return FAIL; |
844 | 8200 } |
7 | 8201 |
8202 return OK; | |
8203 } | |
8204 | |
123 | 8205 # if defined(FEAT_SYN_HL) || defined(PROTO) |
8206 | |
8207 /* | |
8208 * Add "what" to 'eventignore' to skip loading syntax highlighting for every | |
8209 * buffer loaded into the window. "what" must start with a comma. | |
8210 * Returns the old value of 'eventignore' in allocated memory. | |
8211 */ | |
8212 char_u * | |
8213 au_event_disable(what) | |
8214 char *what; | |
8215 { | |
8216 char_u *new_ei; | |
8217 char_u *save_ei; | |
8218 | |
8219 save_ei = vim_strsave(p_ei); | |
8220 if (save_ei != NULL) | |
8221 { | |
557 | 8222 new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what))); |
123 | 8223 if (new_ei != NULL) |
8224 { | |
2095
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8225 if (*what == ',' && *p_ei == NUL) |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8226 STRCPY(new_ei, what + 1); |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8227 else |
8aa3cd045aef
updated for version 7.2.379
Bram Moolenaar <bram@zimbu.org>
parents:
2047
diff
changeset
|
8228 STRCAT(new_ei, what); |
694 | 8229 set_string_option_direct((char_u *)"ei", -1, new_ei, |
8230 OPT_FREE, SID_NONE); | |
123 | 8231 vim_free(new_ei); |
8232 } | |
8233 } | |
8234 return save_ei; | |
8235 } | |
8236 | |
8237 void | |
8238 au_event_restore(old_ei) | |
8239 char_u *old_ei; | |
8240 { | |
8241 if (old_ei != NULL) | |
8242 { | |
694 | 8243 set_string_option_direct((char_u *)"ei", -1, old_ei, |
8244 OPT_FREE, SID_NONE); | |
123 | 8245 vim_free(old_ei); |
8246 } | |
8247 } | |
8248 # endif /* FEAT_SYN_HL */ | |
8249 | |
7 | 8250 /* |
8251 * do_autocmd() -- implements the :autocmd command. Can be used in the | |
8252 * following ways: | |
8253 * | |
8254 * :autocmd <event> <pat> <cmd> Add <cmd> to the list of commands that | |
8255 * will be automatically executed for <event> | |
8256 * when editing a file matching <pat>, in | |
8257 * the current group. | |
8258 * :autocmd <event> <pat> Show the auto-commands associated with | |
8259 * <event> and <pat>. | |
8260 * :autocmd <event> Show the auto-commands associated with | |
8261 * <event>. | |
8262 * :autocmd Show all auto-commands. | |
8263 * :autocmd! <event> <pat> <cmd> Remove all auto-commands associated with | |
8264 * <event> and <pat>, and add the command | |
8265 * <cmd>, for the current group. | |
8266 * :autocmd! <event> <pat> Remove all auto-commands associated with | |
8267 * <event> and <pat> for the current group. | |
8268 * :autocmd! <event> Remove all auto-commands associated with | |
8269 * <event> for the current group. | |
8270 * :autocmd! Remove ALL auto-commands for the current | |
8271 * group. | |
8272 * | |
8273 * Multiple events and patterns may be given separated by commas. Here are | |
8274 * some examples: | |
8275 * :autocmd bufread,bufenter *.c,*.h set tw=0 smartindent noic | |
8276 * :autocmd bufleave * set tw=79 nosmartindent ic infercase | |
8277 * | |
8278 * :autocmd * *.c show all autocommands for *.c files. | |
609 | 8279 * |
8280 * Mostly a {group} argument can optionally appear before <event>. | |
7 | 8281 */ |
8282 void | |
8283 do_autocmd(arg, forceit) | |
8284 char_u *arg; | |
8285 int forceit; | |
8286 { | |
8287 char_u *pat; | |
8288 char_u *envpat = NULL; | |
8289 char_u *cmd; | |
661 | 8290 event_T event; |
7 | 8291 int need_free = FALSE; |
8292 int nested = FALSE; | |
8293 int group; | |
8294 | |
8295 /* | |
8296 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8297 */ | |
8298 group = au_get_grouparg(&arg); | |
8299 if (arg == NULL) /* out of memory */ | |
8300 return; | |
8301 | |
8302 /* | |
8303 * Scan over the events. | |
8304 * If we find an illegal name, return here, don't do anything. | |
8305 */ | |
8306 pat = find_end_event(arg, group != AUGROUP_ALL); | |
8307 if (pat == NULL) | |
8308 return; | |
8309 | |
8310 /* | |
8311 * Scan over the pattern. Put a NUL at the end. | |
8312 */ | |
8313 pat = skipwhite(pat); | |
8314 cmd = pat; | |
8315 while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\')) | |
8316 cmd++; | |
8317 if (*cmd) | |
8318 *cmd++ = NUL; | |
8319 | |
8320 /* Expand environment variables in the pattern. Set 'shellslash', we want | |
8321 * forward slashes here. */ | |
8322 if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL) | |
8323 { | |
8324 #ifdef BACKSLASH_IN_FILENAME | |
8325 int p_ssl_save = p_ssl; | |
8326 | |
8327 p_ssl = TRUE; | |
8328 #endif | |
8329 envpat = expand_env_save(pat); | |
8330 #ifdef BACKSLASH_IN_FILENAME | |
8331 p_ssl = p_ssl_save; | |
8332 #endif | |
8333 if (envpat != NULL) | |
8334 pat = envpat; | |
8335 } | |
8336 | |
8337 /* | |
8338 * Check for "nested" flag. | |
8339 */ | |
8340 cmd = skipwhite(cmd); | |
8341 if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6])) | |
8342 { | |
8343 nested = TRUE; | |
8344 cmd = skipwhite(cmd + 6); | |
8345 } | |
8346 | |
8347 /* | |
8348 * Find the start of the commands. | |
8349 * Expand <sfile> in it. | |
8350 */ | |
8351 if (*cmd != NUL) | |
8352 { | |
8353 cmd = expand_sfile(cmd); | |
8354 if (cmd == NULL) /* some error */ | |
8355 return; | |
8356 need_free = TRUE; | |
8357 } | |
8358 | |
8359 /* | |
8360 * Print header when showing autocommands. | |
8361 */ | |
8362 if (!forceit && *cmd == NUL) | |
8363 { | |
8364 /* Highlight title */ | |
8365 MSG_PUTS_TITLE(_("\n--- Auto-Commands ---")); | |
8366 } | |
8367 | |
8368 /* | |
8369 * Loop over the events. | |
8370 */ | |
661 | 8371 last_event = (event_T)-1; /* for listing the event name */ |
7 | 8372 last_group = AUGROUP_ERROR; /* for listing the group name */ |
8373 if (*arg == '*' || *arg == NUL) | |
8374 { | |
661 | 8375 for (event = (event_T)0; (int)event < (int)NUM_EVENTS; |
8376 event = (event_T)((int)event + 1)) | |
7 | 8377 if (do_autocmd_event(event, pat, |
8378 nested, cmd, forceit, group) == FAIL) | |
8379 break; | |
8380 } | |
8381 else | |
8382 { | |
8383 while (*arg && !vim_iswhite(*arg)) | |
8384 if (do_autocmd_event(event_name2nr(arg, &arg), pat, | |
8385 nested, cmd, forceit, group) == FAIL) | |
8386 break; | |
8387 } | |
8388 | |
8389 if (need_free) | |
8390 vim_free(cmd); | |
8391 vim_free(envpat); | |
8392 } | |
8393 | |
8394 /* | |
8395 * Find the group ID in a ":autocmd" or ":doautocmd" argument. | |
8396 * The "argp" argument is advanced to the following argument. | |
8397 * | |
8398 * Returns the group ID, AUGROUP_ERROR for error (out of memory). | |
8399 */ | |
8400 static int | |
8401 au_get_grouparg(argp) | |
8402 char_u **argp; | |
8403 { | |
8404 char_u *group_name; | |
8405 char_u *p; | |
8406 char_u *arg = *argp; | |
8407 int group = AUGROUP_ALL; | |
8408 | |
8409 p = skiptowhite(arg); | |
8410 if (p > arg) | |
8411 { | |
8412 group_name = vim_strnsave(arg, (int)(p - arg)); | |
8413 if (group_name == NULL) /* out of memory */ | |
8414 return AUGROUP_ERROR; | |
8415 group = au_find_group(group_name); | |
8416 if (group == AUGROUP_ERROR) | |
8417 group = AUGROUP_ALL; /* no match, use all groups */ | |
8418 else | |
8419 *argp = skipwhite(p); /* match, skip over group name */ | |
8420 vim_free(group_name); | |
8421 } | |
8422 return group; | |
8423 } | |
8424 | |
8425 /* | |
8426 * do_autocmd() for one event. | |
8427 * If *pat == NUL do for all patterns. | |
8428 * If *cmd == NUL show entries. | |
8429 * If forceit == TRUE delete entries. | |
8430 * If group is not AUGROUP_ALL, only use this group. | |
8431 */ | |
8432 static int | |
8433 do_autocmd_event(event, pat, nested, cmd, forceit, group) | |
661 | 8434 event_T event; |
7 | 8435 char_u *pat; |
8436 int nested; | |
8437 char_u *cmd; | |
8438 int forceit; | |
8439 int group; | |
8440 { | |
8441 AutoPat *ap; | |
8442 AutoPat **prev_ap; | |
8443 AutoCmd *ac; | |
8444 AutoCmd **prev_ac; | |
8445 int brace_level; | |
8446 char_u *endpat; | |
8447 int findgroup; | |
8448 int allgroups; | |
8449 int patlen; | |
856 | 8450 int is_buflocal; |
40 | 8451 int buflocal_nr; |
8452 char_u buflocal_pat[25]; /* for "<buffer=X>" */ | |
7 | 8453 |
8454 if (group == AUGROUP_ALL) | |
8455 findgroup = current_augroup; | |
8456 else | |
8457 findgroup = group; | |
8458 allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL); | |
8459 | |
8460 /* | |
8461 * Show or delete all patterns for an event. | |
8462 */ | |
8463 if (*pat == NUL) | |
8464 { | |
8465 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
8466 { | |
8467 if (forceit) /* delete the AutoPat, if it's in the current group */ | |
8468 { | |
8469 if (ap->group == findgroup) | |
8470 au_remove_pat(ap); | |
8471 } | |
8472 else if (group == AUGROUP_ALL || ap->group == group) | |
8473 show_autocmd(ap, event); | |
8474 } | |
8475 } | |
8476 | |
8477 /* | |
8478 * Loop through all the specified patterns. | |
8479 */ | |
8480 for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat)) | |
8481 { | |
8482 /* | |
8483 * Find end of the pattern. | |
8484 * Watch out for a comma in braces, like "*.\{obj,o\}". | |
8485 */ | |
8486 brace_level = 0; | |
8487 for (endpat = pat; *endpat && (*endpat != ',' || brace_level | |
8488 || endpat[-1] == '\\'); ++endpat) | |
8489 { | |
8490 if (*endpat == '{') | |
8491 brace_level++; | |
8492 else if (*endpat == '}') | |
8493 brace_level--; | |
8494 } | |
8495 if (pat == endpat) /* ignore single comma */ | |
8496 continue; | |
8497 patlen = (int)(endpat - pat); | |
8498 | |
8499 /* | |
40 | 8500 * detect special <buflocal[=X]> buffer-local patterns |
8501 */ | |
8502 is_buflocal = FALSE; | |
8503 buflocal_nr = 0; | |
8504 | |
8505 if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0 | |
8506 && pat[patlen - 1] == '>') | |
8507 { | |
8508 /* Error will be printed only for addition. printing and removing | |
8509 * will proceed silently. */ | |
8510 is_buflocal = TRUE; | |
8511 if (patlen == 8) | |
8512 buflocal_nr = curbuf->b_fnum; | |
8513 else if (patlen > 9 && pat[7] == '=') | |
8514 { | |
8515 /* <buffer=abuf> */ | |
8516 if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13)) | |
8517 buflocal_nr = autocmd_bufnr; | |
8518 /* <buffer=123> */ | |
8519 else if (skipdigits(pat + 8) == pat + patlen - 1) | |
8520 buflocal_nr = atoi((char *)pat + 8); | |
8521 } | |
8522 } | |
8523 | |
8524 if (is_buflocal) | |
8525 { | |
8526 /* normalize pat into standard "<buffer>#N" form */ | |
8527 sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr); | |
8528 pat = buflocal_pat; /* can modify pat and patlen */ | |
835 | 8529 patlen = (int)STRLEN(buflocal_pat); /* but not endpat */ |
40 | 8530 } |
8531 | |
8532 /* | |
7 | 8533 * Find AutoPat entries with this pattern. |
8534 */ | |
8535 prev_ap = &first_autopat[(int)event]; | |
8536 while ((ap = *prev_ap) != NULL) | |
8537 { | |
8538 if (ap->pat != NULL) | |
8539 { | |
8540 /* Accept a pattern when: | |
8541 * - a group was specified and it's that group, or a group was | |
8542 * not specified and it's the current group, or a group was | |
8543 * not specified and we are listing | |
8544 * - the length of the pattern matches | |
40 | 8545 * - the pattern matches. |
8546 * For <buffer[=X]>, this condition works because we normalize | |
8547 * all buffer-local patterns. | |
7 | 8548 */ |
8549 if ((allgroups || ap->group == findgroup) | |
8550 && ap->patlen == patlen | |
8551 && STRNCMP(pat, ap->pat, patlen) == 0) | |
8552 { | |
8553 /* | |
8554 * Remove existing autocommands. | |
8555 * If adding any new autocmd's for this AutoPat, don't | |
8556 * delete the pattern from the autopat list, append to | |
8557 * this list. | |
8558 */ | |
8559 if (forceit) | |
8560 { | |
8561 if (*cmd != NUL && ap->next == NULL) | |
8562 { | |
8563 au_remove_cmds(ap); | |
8564 break; | |
8565 } | |
8566 au_remove_pat(ap); | |
8567 } | |
8568 | |
8569 /* | |
40 | 8570 * Show autocmd's for this autopat, or buflocals <buffer=X> |
7 | 8571 */ |
8572 else if (*cmd == NUL) | |
8573 show_autocmd(ap, event); | |
8574 | |
8575 /* | |
8576 * Add autocmd to this autopat, if it's the last one. | |
8577 */ | |
8578 else if (ap->next == NULL) | |
8579 break; | |
8580 } | |
8581 } | |
8582 prev_ap = &ap->next; | |
8583 } | |
8584 | |
8585 /* | |
8586 * Add a new command. | |
8587 */ | |
8588 if (*cmd != NUL) | |
8589 { | |
8590 /* | |
8591 * If the pattern we want to add a command to does appear at the | |
8592 * end of the list (or not is not in the list at all), add the | |
8593 * pattern at the end of the list. | |
8594 */ | |
8595 if (ap == NULL) | |
8596 { | |
40 | 8597 /* refuse to add buffer-local ap if buffer number is invalid */ |
8598 if (is_buflocal && (buflocal_nr == 0 | |
8599 || buflist_findnr(buflocal_nr) == NULL)) | |
8600 { | |
8601 EMSGN(_("E680: <buffer=%d>: invalid buffer number "), | |
8602 buflocal_nr); | |
8603 return FAIL; | |
8604 } | |
8605 | |
7 | 8606 ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat)); |
8607 if (ap == NULL) | |
8608 return FAIL; | |
8609 ap->pat = vim_strnsave(pat, patlen); | |
8610 ap->patlen = patlen; | |
8611 if (ap->pat == NULL) | |
8612 { | |
8613 vim_free(ap); | |
8614 return FAIL; | |
8615 } | |
40 | 8616 |
8617 if (is_buflocal) | |
8618 { | |
8619 ap->buflocal_nr = buflocal_nr; | |
153 | 8620 ap->reg_prog = NULL; |
40 | 8621 } |
8622 else | |
7 | 8623 { |
153 | 8624 char_u *reg_pat; |
8625 | |
40 | 8626 ap->buflocal_nr = 0; |
153 | 8627 reg_pat = file_pat_to_reg_pat(pat, endpat, |
40 | 8628 &ap->allow_dirs, TRUE); |
153 | 8629 if (reg_pat != NULL) |
8630 ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC); | |
355 | 8631 vim_free(reg_pat); |
153 | 8632 if (reg_pat == NULL || ap->reg_prog == NULL) |
40 | 8633 { |
8634 vim_free(ap->pat); | |
8635 vim_free(ap); | |
8636 return FAIL; | |
8637 } | |
7 | 8638 } |
8639 ap->cmds = NULL; | |
8640 *prev_ap = ap; | |
8641 ap->next = NULL; | |
8642 if (group == AUGROUP_ALL) | |
8643 ap->group = current_augroup; | |
8644 else | |
8645 ap->group = group; | |
8646 } | |
8647 | |
8648 /* | |
8649 * Add the autocmd at the end of the AutoCmd list. | |
8650 */ | |
8651 prev_ac = &(ap->cmds); | |
8652 while ((ac = *prev_ac) != NULL) | |
8653 prev_ac = &ac->next; | |
8654 ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd)); | |
8655 if (ac == NULL) | |
8656 return FAIL; | |
8657 ac->cmd = vim_strsave(cmd); | |
8658 #ifdef FEAT_EVAL | |
8659 ac->scriptID = current_SID; | |
8660 #endif | |
8661 if (ac->cmd == NULL) | |
8662 { | |
8663 vim_free(ac); | |
8664 return FAIL; | |
8665 } | |
8666 ac->next = NULL; | |
8667 *prev_ac = ac; | |
8668 ac->nested = nested; | |
8669 } | |
8670 } | |
8671 | |
8672 au_cleanup(); /* may really delete removed patterns/commands now */ | |
8673 return OK; | |
8674 } | |
8675 | |
8676 /* | |
8677 * Implementation of ":doautocmd [group] event [fname]". | |
8678 * Return OK for success, FAIL for failure; | |
8679 */ | |
8680 int | |
8681 do_doautocmd(arg, do_msg) | |
8682 char_u *arg; | |
8683 int do_msg; /* give message for no matching autocmds? */ | |
8684 { | |
8685 char_u *fname; | |
8686 int nothing_done = TRUE; | |
8687 int group; | |
8688 | |
8689 /* | |
8690 * Check for a legal group name. If not, use AUGROUP_ALL. | |
8691 */ | |
8692 group = au_get_grouparg(&arg); | |
8693 if (arg == NULL) /* out of memory */ | |
8694 return FAIL; | |
8695 | |
8696 if (*arg == '*') | |
8697 { | |
8698 EMSG(_("E217: Can't execute autocommands for ALL events")); | |
8699 return FAIL; | |
8700 } | |
8701 | |
8702 /* | |
8703 * Scan over the events. | |
8704 * If we find an illegal name, return here, don't do anything. | |
8705 */ | |
8706 fname = find_end_event(arg, group != AUGROUP_ALL); | |
8707 if (fname == NULL) | |
8708 return FAIL; | |
8709 | |
8710 fname = skipwhite(fname); | |
8711 | |
8712 /* | |
8713 * Loop over the events. | |
8714 */ | |
8715 while (*arg && !vim_iswhite(*arg)) | |
8716 if (apply_autocmds_group(event_name2nr(arg, &arg), | |
8717 fname, NULL, TRUE, group, curbuf, NULL)) | |
8718 nothing_done = FALSE; | |
8719 | |
8720 if (nothing_done && do_msg) | |
8721 MSG(_("No matching autocommands")); | |
8722 | |
8723 #ifdef FEAT_EVAL | |
8724 return aborting() ? FAIL : OK; | |
8725 #else | |
8726 return OK; | |
8727 #endif | |
8728 } | |
8729 | |
8730 /* | |
8731 * ":doautoall": execute autocommands for each loaded buffer. | |
8732 */ | |
8733 void | |
8734 ex_doautoall(eap) | |
8735 exarg_T *eap; | |
8736 { | |
8737 int retval; | |
8738 aco_save_T aco; | |
8739 buf_T *buf; | |
8740 | |
8741 /* | |
8742 * This is a bit tricky: For some commands curwin->w_buffer needs to be | |
8743 * equal to curbuf, but for some buffers there may not be a window. | |
8744 * So we change the buffer for the current window for a moment. This | |
8745 * gives problems when the autocommands make changes to the list of | |
8746 * buffers or windows... | |
8747 */ | |
8748 for (buf = firstbuf; buf != NULL; buf = buf->b_next) | |
8749 { | |
1665 | 8750 if (buf->b_ml.ml_mfp != NULL) |
7 | 8751 { |
8752 /* find a window for this buffer and save some values */ | |
8753 aucmd_prepbuf(&aco, buf); | |
8754 | |
8755 /* execute the autocommands for this buffer */ | |
8756 retval = do_doautocmd(eap->arg, FALSE); | |
1113 | 8757 |
8758 /* Execute the modeline settings, but don't set window-local | |
8759 * options if we are using the current window for another buffer. */ | |
1906 | 8760 do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); |
7 | 8761 |
8762 /* restore the current window */ | |
8763 aucmd_restbuf(&aco); | |
8764 | |
8765 /* stop if there is some error or buffer was deleted */ | |
8766 if (retval == FAIL || !buf_valid(buf)) | |
8767 break; | |
8768 } | |
8769 } | |
8770 | |
8771 check_cursor(); /* just in case lines got deleted */ | |
8772 } | |
8773 | |
8774 /* | |
8775 * Prepare for executing autocommands for (hidden) buffer "buf". | |
1906 | 8776 * Search for a visible window containing the current buffer. If there isn't |
8777 * one then use "aucmd_win". | |
7 | 8778 * Set "curbuf" and "curwin" to match "buf". |
934 | 8779 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8780 */ |
8781 void | |
8782 aucmd_prepbuf(aco, buf) | |
8783 aco_save_T *aco; /* structure to save values in */ | |
8784 buf_T *buf; /* new curbuf */ | |
8785 { | |
8786 win_T *win; | |
1906 | 8787 #ifdef FEAT_WINDOWS |
8788 int save_ea; | |
8789 #endif | |
7 | 8790 |
8791 /* Find a window that is for the new buffer */ | |
8792 if (buf == curbuf) /* be quick when buf is curbuf */ | |
8793 win = curwin; | |
8794 else | |
8795 #ifdef FEAT_WINDOWS | |
8796 for (win = firstwin; win != NULL; win = win->w_next) | |
8797 if (win->w_buffer == buf) | |
8798 break; | |
8799 #else | |
8800 win = NULL; | |
8801 #endif | |
8802 | |
1906 | 8803 /* Allocate "aucmd_win" when needed. If this fails (out of memory) fall |
8804 * back to using the current window. */ | |
8805 if (win == NULL && aucmd_win == NULL) | |
8806 { | |
8807 win_alloc_aucmd_win(); | |
8808 if (aucmd_win == NULL) | |
8809 win = curwin; | |
8810 } | |
1944 | 8811 if (win == NULL && aucmd_win_used) |
8812 /* Strange recursive autocommand, fall back to using the current | |
8813 * window. Expect a few side effects... */ | |
8814 win = curwin; | |
1906 | 8815 |
8816 aco->save_curwin = curwin; | |
8817 aco->save_curbuf = curbuf; | |
7 | 8818 if (win != NULL) |
8819 { | |
1906 | 8820 /* There is a window for "buf" in the current tab page, make it the |
8821 * curwin. This is preferred, it has the least side effects (esp. if | |
8822 * "buf" is curbuf). */ | |
1944 | 8823 aco->use_aucmd_win = FALSE; |
7 | 8824 curwin = win; |
8825 } | |
8826 else | |
8827 { | |
1906 | 8828 /* There is no window for "buf", use "aucmd_win". To minimize the side |
8829 * effects, insert it in a the current tab page. | |
8830 * Anything related to a window (e.g., setting folds) may have | |
8831 * unexpected results. */ | |
1944 | 8832 aco->use_aucmd_win = TRUE; |
8833 aucmd_win_used = TRUE; | |
1918 | 8834 aucmd_win->w_buffer = buf; |
2258
772bfca06c18
Fix crash when using ":grep".
Bram Moolenaar <bram@vim.org>
parents:
2244
diff
changeset
|
8835 aucmd_win->w_s = &buf->b_s; |
7 | 8836 ++buf->b_nwindows; |
1918 | 8837 win_init_empty(aucmd_win); /* set cursor and topline to safe values */ |
1944 | 8838 vim_free(aucmd_win->w_localdir); |
8839 aucmd_win->w_localdir = NULL; | |
8840 | |
8841 /* Make sure w_localdir and globaldir are NULL to avoid a chdir() in | |
8842 * win_enter_ext(). */ | |
8843 aucmd_win->w_localdir = NULL; | |
8844 aco->globaldir = globaldir; | |
8845 globaldir = NULL; | |
8846 | |
7 | 8847 |
1906 | 8848 #ifdef FEAT_WINDOWS |
1923 | 8849 /* Split the current window, put the aucmd_win in the upper half. |
8850 * We don't want the BufEnter or WinEnter autocommands. */ | |
8851 block_autocmds(); | |
1906 | 8852 make_snapshot(SNAP_AUCMD_IDX); |
8853 save_ea = p_ea; | |
8854 p_ea = FALSE; | |
8855 (void)win_split_ins(0, WSP_TOP, aucmd_win, 0); | |
8856 (void)win_comp_pos(); /* recompute window positions */ | |
8857 p_ea = save_ea; | |
1923 | 8858 unblock_autocmds(); |
1906 | 8859 #endif |
1918 | 8860 curwin = aucmd_win; |
1906 | 8861 } |
7 | 8862 curbuf = buf; |
1906 | 8863 aco->new_curwin = curwin; |
8864 aco->new_curbuf = curbuf; | |
7 | 8865 } |
8866 | |
8867 /* | |
8868 * Cleanup after executing autocommands for a (hidden) buffer. | |
8869 * Restore the window as it was (if possible). | |
934 | 8870 * When FEAT_AUTOCMD is not defined another version is used, see below. |
7 | 8871 */ |
8872 void | |
8873 aucmd_restbuf(aco) | |
8874 aco_save_T *aco; /* structure holding saved values */ | |
8875 { | |
1906 | 8876 #ifdef FEAT_WINDOWS |
8877 int dummy; | |
8878 #endif | |
8879 | |
1944 | 8880 if (aco->use_aucmd_win) |
1906 | 8881 { |
8882 --curbuf->b_nwindows; | |
8883 #ifdef FEAT_WINDOWS | |
8884 /* Find "aucmd_win", it can't be closed, but it may be in another tab | |
1923 | 8885 * page. Do not trigger autocommands here. */ |
8886 block_autocmds(); | |
1906 | 8887 if (curwin != aucmd_win) |
8888 { | |
8889 tabpage_T *tp; | |
8890 win_T *wp; | |
8891 | |
8892 FOR_ALL_TAB_WINDOWS(tp, wp) | |
8893 { | |
8894 if (wp == aucmd_win) | |
8895 { | |
8896 if (tp != curtab) | |
8897 goto_tabpage_tp(tp); | |
8898 win_goto(aucmd_win); | |
8899 break; | |
8900 } | |
8901 } | |
8902 } | |
8903 | |
8904 /* Remove the window and frame from the tree of frames. */ | |
8905 (void)winframe_remove(curwin, &dummy, NULL); | |
8906 win_remove(curwin, NULL); | |
1944 | 8907 aucmd_win_used = FALSE; |
1906 | 8908 last_status(FALSE); /* may need to remove last status line */ |
8909 restore_snapshot(SNAP_AUCMD_IDX, FALSE); | |
8910 (void)win_comp_pos(); /* recompute window positions */ | |
1923 | 8911 unblock_autocmds(); |
1906 | 8912 |
8913 if (win_valid(aco->save_curwin)) | |
8914 curwin = aco->save_curwin; | |
8915 else | |
8916 /* Hmm, original window disappeared. Just use the first one. */ | |
8917 curwin = firstwin; | |
8918 # ifdef FEAT_EVAL | |
8919 vars_clear(&aucmd_win->w_vars.dv_hashtab); /* free all w: variables */ | |
2001 | 8920 hash_init(&aucmd_win->w_vars.dv_hashtab); /* re-use the hashtab */ |
1906 | 8921 # endif |
8922 #else | |
8923 curwin = aco->save_curwin; | |
8924 #endif | |
8925 curbuf = curwin->w_buffer; | |
8926 | |
1944 | 8927 vim_free(globaldir); |
8928 globaldir = aco->globaldir; | |
8929 | |
1906 | 8930 /* the buffer contents may have changed */ |
8931 check_cursor(); | |
8932 if (curwin->w_topline > curbuf->b_ml.ml_line_count) | |
8933 { | |
8934 curwin->w_topline = curbuf->b_ml.ml_line_count; | |
8935 #ifdef FEAT_DIFF | |
8936 curwin->w_topfill = 0; | |
8937 #endif | |
8938 } | |
8939 #if defined(FEAT_GUI) | |
8940 /* Hide the scrollbars from the aucmd_win and update. */ | |
8941 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_LEFT], FALSE); | |
8942 gui_mch_enable_scrollbar(&aucmd_win->w_scrollbars[SBAR_RIGHT], FALSE); | |
8943 gui_may_update_scrollbars(); | |
8944 #endif | |
8945 } | |
8946 else | |
7 | 8947 { |
8948 /* restore curwin */ | |
8949 #ifdef FEAT_WINDOWS | |
8950 if (win_valid(aco->save_curwin)) | |
8951 #endif | |
8952 { | |
1906 | 8953 /* Restore the buffer which was previously edited by curwin, if |
1944 | 8954 * it was changed, we are still the same window and the buffer is |
1906 | 8955 * valid. */ |
7 | 8956 if (curwin == aco->new_curwin |
1906 | 8957 && curbuf != aco->new_curbuf |
8958 && buf_valid(aco->new_curbuf) | |
8959 && aco->new_curbuf->b_ml.ml_mfp != NULL) | |
7 | 8960 { |
8961 --curbuf->b_nwindows; | |
1906 | 8962 curbuf = aco->new_curbuf; |
7 | 8963 curwin->w_buffer = curbuf; |
8964 ++curbuf->b_nwindows; | |
8965 } | |
8966 | |
8967 curwin = aco->save_curwin; | |
8968 curbuf = curwin->w_buffer; | |
8969 } | |
8970 } | |
8971 } | |
8972 | |
8973 static int autocmd_nested = FALSE; | |
8974 | |
8975 /* | |
8976 * Execute autocommands for "event" and file name "fname". | |
8977 * Return TRUE if some commands were executed. | |
8978 */ | |
8979 int | |
8980 apply_autocmds(event, fname, fname_io, force, buf) | |
661 | 8981 event_T event; |
7 | 8982 char_u *fname; /* NULL or empty means use actual file name */ |
8983 char_u *fname_io; /* fname to use for <afile> on cmdline */ | |
8984 int force; /* when TRUE, ignore autocmd_busy */ | |
8985 buf_T *buf; /* buffer for <abuf> */ | |
8986 { | |
8987 return apply_autocmds_group(event, fname, fname_io, force, | |
8988 AUGROUP_ALL, buf, NULL); | |
8989 } | |
8990 | |
8991 /* | |
8992 * Like apply_autocmds(), but with extra "eap" argument. This takes care of | |
8993 * setting v:filearg. | |
8994 */ | |
8995 static int | |
8996 apply_autocmds_exarg(event, fname, fname_io, force, buf, eap) | |
661 | 8997 event_T event; |
7 | 8998 char_u *fname; |
8999 char_u *fname_io; | |
9000 int force; | |
9001 buf_T *buf; | |
9002 exarg_T *eap; | |
9003 { | |
9004 return apply_autocmds_group(event, fname, fname_io, force, | |
9005 AUGROUP_ALL, buf, eap); | |
9006 } | |
9007 | |
9008 /* | |
9009 * Like apply_autocmds(), but handles the caller's retval. If the script | |
9010 * processing is being aborted or if retval is FAIL when inside a try | |
9011 * conditional, no autocommands are executed. If otherwise the autocommands | |
9012 * cause the script to be aborted, retval is set to FAIL. | |
9013 */ | |
9014 int | |
9015 apply_autocmds_retval(event, fname, fname_io, force, buf, retval) | |
661 | 9016 event_T event; |
7 | 9017 char_u *fname; /* NULL or empty means use actual file name */ |
9018 char_u *fname_io; /* fname to use for <afile> on cmdline */ | |
9019 int force; /* when TRUE, ignore autocmd_busy */ | |
9020 buf_T *buf; /* buffer for <abuf> */ | |
9021 int *retval; /* pointer to caller's retval */ | |
9022 { | |
9023 int did_cmd; | |
9024 | |
532 | 9025 #ifdef FEAT_EVAL |
7 | 9026 if (should_abort(*retval)) |
9027 return FALSE; | |
532 | 9028 #endif |
7 | 9029 |
9030 did_cmd = apply_autocmds_group(event, fname, fname_io, force, | |
9031 AUGROUP_ALL, buf, NULL); | |
532 | 9032 if (did_cmd |
9033 #ifdef FEAT_EVAL | |
9034 && aborting() | |
9035 #endif | |
9036 ) | |
7 | 9037 *retval = FAIL; |
9038 return did_cmd; | |
9039 } | |
9040 | |
609 | 9041 /* |
9042 * Return TRUE when there is a CursorHold autocommand defined. | |
9043 */ | |
7 | 9044 int |
9045 has_cursorhold() | |
9046 { | |
661 | 9047 return (first_autopat[(int)(get_real_state() == NORMAL_BUSY |
9048 ? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL); | |
7 | 9049 } |
609 | 9050 |
9051 /* | |
9052 * Return TRUE if the CursorHold event can be triggered. | |
9053 */ | |
9054 int | |
9055 trigger_cursorhold() | |
9056 { | |
661 | 9057 int state; |
9058 | |
978 | 9059 if (!did_cursorhold && has_cursorhold() && !Recording |
9060 #ifdef FEAT_INS_EXPAND | |
9061 && !ins_compl_active() | |
9062 #endif | |
9063 ) | |
661 | 9064 { |
9065 state = get_real_state(); | |
9066 if (state == NORMAL_BUSY || (state & INSERT) != 0) | |
9067 return TRUE; | |
9068 } | |
9069 return FALSE; | |
609 | 9070 } |
661 | 9071 |
9072 /* | |
9073 * Return TRUE when there is a CursorMoved autocommand defined. | |
9074 */ | |
9075 int | |
9076 has_cursormoved() | |
9077 { | |
9078 return (first_autopat[(int)EVENT_CURSORMOVED] != NULL); | |
9079 } | |
9080 | |
9081 /* | |
9082 * Return TRUE when there is a CursorMovedI autocommand defined. | |
9083 */ | |
9084 int | |
9085 has_cursormovedI() | |
9086 { | |
9087 return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL); | |
9088 } | |
7 | 9089 |
9090 static int | |
9091 apply_autocmds_group(event, fname, fname_io, force, group, buf, eap) | |
661 | 9092 event_T event; |
7 | 9093 char_u *fname; /* NULL or empty means use actual file name */ |
9094 char_u *fname_io; /* fname to use for <afile> on cmdline, NULL means | |
9095 use fname */ | |
9096 int force; /* when TRUE, ignore autocmd_busy */ | |
9097 int group; /* group ID, or AUGROUP_ALL */ | |
9098 buf_T *buf; /* buffer for <abuf> */ | |
9099 exarg_T *eap; /* command arguments */ | |
9100 { | |
9101 char_u *sfname = NULL; /* short file name */ | |
9102 char_u *tail; | |
9103 int save_changed; | |
9104 buf_T *old_curbuf; | |
9105 int retval = FALSE; | |
9106 char_u *save_sourcing_name; | |
9107 linenr_T save_sourcing_lnum; | |
9108 char_u *save_autocmd_fname; | |
1723 | 9109 int save_autocmd_fname_full; |
7 | 9110 int save_autocmd_bufnr; |
9111 char_u *save_autocmd_match; | |
9112 int save_autocmd_busy; | |
9113 int save_autocmd_nested; | |
9114 static int nesting = 0; | |
9115 AutoPatCmd patcmd; | |
9116 AutoPat *ap; | |
9117 #ifdef FEAT_EVAL | |
9118 scid_T save_current_SID; | |
9119 void *save_funccalp; | |
9120 char_u *save_cmdarg; | |
9121 long save_cmdbang; | |
9122 #endif | |
9123 static int filechangeshell_busy = FALSE; | |
170 | 9124 #ifdef FEAT_PROFILE |
9125 proftime_T wait_time; | |
9126 #endif | |
7 | 9127 |
9128 /* | |
9129 * Quickly return if there are no autocommands for this event or | |
9130 * autocommands are blocked. | |
9131 */ | |
1410 | 9132 if (first_autopat[(int)event] == NULL || autocmd_blocked > 0) |
40 | 9133 goto BYPASS_AU; |
7 | 9134 |
9135 /* | |
9136 * When autocommands are busy, new autocommands are only executed when | |
9137 * explicitly enabled with the "nested" flag. | |
9138 */ | |
9139 if (autocmd_busy && !(force || autocmd_nested)) | |
40 | 9140 goto BYPASS_AU; |
7 | 9141 |
9142 #ifdef FEAT_EVAL | |
9143 /* | |
1201 | 9144 * Quickly return when immediately aborting on error, or when an interrupt |
7 | 9145 * occurred or an exception was thrown but not caught. |
9146 */ | |
9147 if (aborting()) | |
40 | 9148 goto BYPASS_AU; |
7 | 9149 #endif |
9150 | |
9151 /* | |
9152 * FileChangedShell never nests, because it can create an endless loop. | |
9153 */ | |
765 | 9154 if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL |
9155 || event == EVENT_FILECHANGEDSHELLPOST)) | |
40 | 9156 goto BYPASS_AU; |
7 | 9157 |
9158 /* | |
9159 * Ignore events in 'eventignore'. | |
9160 */ | |
9161 if (event_ignored(event)) | |
40 | 9162 goto BYPASS_AU; |
7 | 9163 |
9164 /* | |
9165 * Allow nesting of autocommands, but restrict the depth, because it's | |
9166 * possible to create an endless loop. | |
9167 */ | |
9168 if (nesting == 10) | |
9169 { | |
9170 EMSG(_("E218: autocommand nesting too deep")); | |
40 | 9171 goto BYPASS_AU; |
7 | 9172 } |
9173 | |
9174 /* | |
9175 * Check if these autocommands are disabled. Used when doing ":all" or | |
9176 * ":ball". | |
9177 */ | |
9178 if ( (autocmd_no_enter | |
9179 && (event == EVENT_WINENTER || event == EVENT_BUFENTER)) | |
9180 || (autocmd_no_leave | |
9181 && (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE))) | |
40 | 9182 goto BYPASS_AU; |
7 | 9183 |
9184 /* | |
9185 * Save the autocmd_* variables and info about the current buffer. | |
9186 */ | |
9187 save_autocmd_fname = autocmd_fname; | |
1723 | 9188 save_autocmd_fname_full = autocmd_fname_full; |
7 | 9189 save_autocmd_bufnr = autocmd_bufnr; |
9190 save_autocmd_match = autocmd_match; | |
9191 save_autocmd_busy = autocmd_busy; | |
9192 save_autocmd_nested = autocmd_nested; | |
9193 save_changed = curbuf->b_changed; | |
9194 old_curbuf = curbuf; | |
9195 | |
9196 /* | |
9197 * Set the file name to be used for <afile>. | |
1471 | 9198 * Make a copy to avoid that changing a buffer name or directory makes it |
9199 * invalid. | |
7 | 9200 */ |
9201 if (fname_io == NULL) | |
9202 { | |
9203 if (fname != NULL && *fname != NUL) | |
9204 autocmd_fname = fname; | |
9205 else if (buf != NULL) | |
1723 | 9206 autocmd_fname = buf->b_ffname; |
7 | 9207 else |
9208 autocmd_fname = NULL; | |
9209 } | |
9210 else | |
9211 autocmd_fname = fname_io; | |
1471 | 9212 if (autocmd_fname != NULL) |
1723 | 9213 autocmd_fname = vim_strsave(autocmd_fname); |
9214 autocmd_fname_full = FALSE; /* call FullName_save() later */ | |
7 | 9215 |
9216 /* | |
9217 * Set the buffer number to be used for <abuf>. | |
9218 */ | |
9219 if (buf == NULL) | |
9220 autocmd_bufnr = 0; | |
9221 else | |
9222 autocmd_bufnr = buf->b_fnum; | |
9223 | |
9224 /* | |
9225 * When the file name is NULL or empty, use the file name of buffer "buf". | |
9226 * Always use the full path of the file name to match with, in case | |
9227 * "allow_dirs" is set. | |
9228 */ | |
9229 if (fname == NULL || *fname == NUL) | |
9230 { | |
9231 if (buf == NULL) | |
9232 fname = NULL; | |
9233 else | |
9234 { | |
9235 #ifdef FEAT_SYN_HL | |
9236 if (event == EVENT_SYNTAX) | |
9237 fname = buf->b_p_syn; | |
9238 else | |
9239 #endif | |
9240 if (event == EVENT_FILETYPE) | |
9241 fname = buf->b_p_ft; | |
9242 else | |
9243 { | |
9244 if (buf->b_sfname != NULL) | |
9245 sfname = vim_strsave(buf->b_sfname); | |
9246 fname = buf->b_ffname; | |
9247 } | |
9248 } | |
9249 if (fname == NULL) | |
9250 fname = (char_u *)""; | |
9251 fname = vim_strsave(fname); /* make a copy, so we can change it */ | |
9252 } | |
9253 else | |
9254 { | |
9255 sfname = vim_strsave(fname); | |
1867 | 9256 /* Don't try expanding FileType, Syntax, FuncUndefined, WindowID or |
9257 * QuickFixCmd* */ | |
161 | 9258 if (event == EVENT_FILETYPE |
9259 || event == EVENT_SYNTAX | |
1867 | 9260 || event == EVENT_FUNCUNDEFINED |
161 | 9261 || event == EVENT_REMOTEREPLY |
649 | 9262 || event == EVENT_SPELLFILEMISSING |
161 | 9263 || event == EVENT_QUICKFIXCMDPRE |
9264 || event == EVENT_QUICKFIXCMDPOST) | |
7 | 9265 fname = vim_strsave(fname); |
9266 else | |
9267 fname = FullName_save(fname, FALSE); | |
9268 } | |
9269 if (fname == NULL) /* out of memory */ | |
9270 { | |
9271 vim_free(sfname); | |
40 | 9272 retval = FALSE; |
9273 goto BYPASS_AU; | |
7 | 9274 } |
9275 | |
9276 #ifdef BACKSLASH_IN_FILENAME | |
9277 /* | |
9278 * Replace all backslashes with forward slashes. This makes the | |
9279 * autocommand patterns portable between Unix and MS-DOS. | |
9280 */ | |
9281 if (sfname != NULL) | |
9282 forward_slash(sfname); | |
9283 forward_slash(fname); | |
9284 #endif | |
9285 | |
9286 #ifdef VMS | |
9287 /* remove version for correct match */ | |
9288 if (sfname != NULL) | |
9289 vms_remove_version(sfname); | |
9290 vms_remove_version(fname); | |
9291 #endif | |
9292 | |
9293 /* | |
9294 * Set the name to be used for <amatch>. | |
9295 */ | |
9296 autocmd_match = fname; | |
9297 | |
9298 | |
9299 /* Don't redraw while doing auto commands. */ | |
9300 ++RedrawingDisabled; | |
9301 save_sourcing_name = sourcing_name; | |
9302 sourcing_name = NULL; /* don't free this one */ | |
9303 save_sourcing_lnum = sourcing_lnum; | |
9304 sourcing_lnum = 0; /* no line number here */ | |
9305 | |
9306 #ifdef FEAT_EVAL | |
9307 save_current_SID = current_SID; | |
9308 | |
170 | 9309 # ifdef FEAT_PROFILE |
788 | 9310 if (do_profiling == PROF_YES) |
170 | 9311 prof_child_enter(&wait_time); /* doesn't count for the caller itself */ |
9312 # endif | |
9313 | |
7 | 9314 /* Don't use local function variables, if called from a function */ |
9315 save_funccalp = save_funccal(); | |
9316 #endif | |
9317 | |
9318 /* | |
9319 * When starting to execute autocommands, save the search patterns. | |
9320 */ | |
9321 if (!autocmd_busy) | |
9322 { | |
9323 save_search_patterns(); | |
9324 saveRedobuff(); | |
9325 did_filetype = keep_filetype; | |
9326 } | |
9327 | |
9328 /* | |
9329 * Note that we are applying autocmds. Some commands need to know. | |
9330 */ | |
9331 autocmd_busy = TRUE; | |
9332 filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL); | |
9333 ++nesting; /* see matching decrement below */ | |
9334 | |
9335 /* Remember that FileType was triggered. Used for did_filetype(). */ | |
9336 if (event == EVENT_FILETYPE) | |
9337 did_filetype = TRUE; | |
9338 | |
9339 tail = gettail(fname); | |
9340 | |
9341 /* Find first autocommand that matches */ | |
9342 patcmd.curpat = first_autopat[(int)event]; | |
9343 patcmd.nextcmd = NULL; | |
9344 patcmd.group = group; | |
9345 patcmd.fname = fname; | |
9346 patcmd.sfname = sfname; | |
9347 patcmd.tail = tail; | |
9348 patcmd.event = event; | |
40 | 9349 patcmd.arg_bufnr = autocmd_bufnr; |
9350 patcmd.next = NULL; | |
7 | 9351 auto_next_pat(&patcmd, FALSE); |
9352 | |
9353 /* found one, start executing the autocommands */ | |
9354 if (patcmd.curpat != NULL) | |
9355 { | |
40 | 9356 /* add to active_apc_list */ |
9357 patcmd.next = active_apc_list; | |
9358 active_apc_list = &patcmd; | |
9359 | |
7 | 9360 #ifdef FEAT_EVAL |
9361 /* set v:cmdarg (only when there is a matching pattern) */ | |
9362 save_cmdbang = get_vim_var_nr(VV_CMDBANG); | |
9363 if (eap != NULL) | |
9364 { | |
9365 save_cmdarg = set_cmdarg(eap, NULL); | |
9366 set_vim_var_nr(VV_CMDBANG, (long)eap->forceit); | |
9367 } | |
9368 else | |
9369 save_cmdarg = NULL; /* avoid gcc warning */ | |
9370 #endif | |
9371 retval = TRUE; | |
9372 /* mark the last pattern, to avoid an endless loop when more patterns | |
9373 * are added when executing autocommands */ | |
9374 for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next) | |
9375 ap->last = FALSE; | |
9376 ap->last = TRUE; | |
9377 check_lnums(TRUE); /* make sure cursor and topline are valid */ | |
9378 do_cmdline(NULL, getnextac, (void *)&patcmd, | |
9379 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT); | |
9380 #ifdef FEAT_EVAL | |
9381 if (eap != NULL) | |
9382 { | |
9383 (void)set_cmdarg(NULL, save_cmdarg); | |
9384 set_vim_var_nr(VV_CMDBANG, save_cmdbang); | |
9385 } | |
9386 #endif | |
40 | 9387 /* delete from active_apc_list */ |
9388 if (active_apc_list == &patcmd) /* just in case */ | |
9389 active_apc_list = patcmd.next; | |
7 | 9390 } |
9391 | |
9392 --RedrawingDisabled; | |
9393 autocmd_busy = save_autocmd_busy; | |
9394 filechangeshell_busy = FALSE; | |
9395 autocmd_nested = save_autocmd_nested; | |
9396 vim_free(sourcing_name); | |
9397 sourcing_name = save_sourcing_name; | |
9398 sourcing_lnum = save_sourcing_lnum; | |
1471 | 9399 vim_free(autocmd_fname); |
7 | 9400 autocmd_fname = save_autocmd_fname; |
1723 | 9401 autocmd_fname_full = save_autocmd_fname_full; |
7 | 9402 autocmd_bufnr = save_autocmd_bufnr; |
9403 autocmd_match = save_autocmd_match; | |
9404 #ifdef FEAT_EVAL | |
9405 current_SID = save_current_SID; | |
9406 restore_funccal(save_funccalp); | |
170 | 9407 # ifdef FEAT_PROFILE |
788 | 9408 if (do_profiling == PROF_YES) |
170 | 9409 prof_child_exit(&wait_time); |
9410 # endif | |
7 | 9411 #endif |
9412 vim_free(fname); | |
9413 vim_free(sfname); | |
9414 --nesting; /* see matching increment above */ | |
9415 | |
9416 /* | |
9417 * When stopping to execute autocommands, restore the search patterns and | |
9418 * the redo buffer. | |
9419 */ | |
9420 if (!autocmd_busy) | |
9421 { | |
9422 restore_search_patterns(); | |
9423 restoreRedobuff(); | |
9424 did_filetype = FALSE; | |
9425 } | |
9426 | |
9427 /* | |
9428 * Some events don't set or reset the Changed flag. | |
9429 * Check if still in the same buffer! | |
9430 */ | |
9431 if (curbuf == old_curbuf | |
9432 && (event == EVENT_BUFREADPOST | |
9433 || event == EVENT_BUFWRITEPOST | |
9434 || event == EVENT_FILEAPPENDPOST | |
9435 || event == EVENT_VIMLEAVE | |
9436 || event == EVENT_VIMLEAVEPRE)) | |
9437 { | |
9438 #ifdef FEAT_TITLE | |
9439 if (curbuf->b_changed != save_changed) | |
9440 need_maketitle = TRUE; | |
9441 #endif | |
9442 curbuf->b_changed = save_changed; | |
9443 } | |
9444 | |
9445 au_cleanup(); /* may really delete removed patterns/commands now */ | |
40 | 9446 |
9447 BYPASS_AU: | |
9448 /* When wiping out a buffer make sure all its buffer-local autocommands | |
9449 * are deleted. */ | |
9450 if (event == EVENT_BUFWIPEOUT && buf != NULL) | |
9451 aubuflocal_remove(buf); | |
9452 | |
7 | 9453 return retval; |
9454 } | |
9455 | |
1410 | 9456 # ifdef FEAT_EVAL |
9457 static char_u *old_termresponse = NULL; | |
9458 # endif | |
9459 | |
9460 /* | |
9461 * Block triggering autocommands until unblock_autocmd() is called. | |
9462 * Can be used recursively, so long as it's symmetric. | |
9463 */ | |
9464 void | |
9465 block_autocmds() | |
9466 { | |
9467 # ifdef FEAT_EVAL | |
9468 /* Remember the value of v:termresponse. */ | |
9469 if (autocmd_blocked == 0) | |
9470 old_termresponse = get_vim_var_str(VV_TERMRESPONSE); | |
9471 # endif | |
9472 ++autocmd_blocked; | |
9473 } | |
9474 | |
9475 void | |
9476 unblock_autocmds() | |
9477 { | |
9478 --autocmd_blocked; | |
9479 | |
9480 # ifdef FEAT_EVAL | |
9481 /* When v:termresponse was set while autocommands were blocked, trigger | |
9482 * the autocommands now. Esp. useful when executing a shell command | |
9483 * during startup (vimdiff). */ | |
9484 if (autocmd_blocked == 0 | |
9485 && get_vim_var_str(VV_TERMRESPONSE) != old_termresponse) | |
9486 apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf); | |
9487 # endif | |
9488 } | |
9489 | |
7 | 9490 /* |
9491 * Find next autocommand pattern that matches. | |
9492 */ | |
9493 static void | |
9494 auto_next_pat(apc, stop_at_last) | |
9495 AutoPatCmd *apc; | |
9496 int stop_at_last; /* stop when 'last' flag is set */ | |
9497 { | |
9498 AutoPat *ap; | |
9499 AutoCmd *cp; | |
9500 char_u *name; | |
9501 char *s; | |
9502 | |
9503 vim_free(sourcing_name); | |
9504 sourcing_name = NULL; | |
9505 | |
9506 for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next) | |
9507 { | |
9508 apc->curpat = NULL; | |
9509 | |
1723 | 9510 /* Only use a pattern when it has not been removed, has commands and |
40 | 9511 * the group matches. For buffer-local autocommands only check the |
9512 * buffer number. */ | |
7 | 9513 if (ap->pat != NULL && ap->cmds != NULL |
9514 && (apc->group == AUGROUP_ALL || apc->group == ap->group)) | |
9515 { | |
40 | 9516 /* execution-condition */ |
9517 if (ap->buflocal_nr == 0 | |
153 | 9518 ? (match_file_pat(NULL, ap->reg_prog, apc->fname, |
9519 apc->sfname, apc->tail, ap->allow_dirs)) | |
40 | 9520 : ap->buflocal_nr == apc->arg_bufnr) |
7 | 9521 { |
9522 name = event_nr2name(apc->event); | |
9523 s = _("%s Auto commands for \"%s\""); | |
9524 sourcing_name = alloc((unsigned)(STRLEN(s) | |
9525 + STRLEN(name) + ap->patlen + 1)); | |
9526 if (sourcing_name != NULL) | |
9527 { | |
9528 sprintf((char *)sourcing_name, s, | |
9529 (char *)name, (char *)ap->pat); | |
9530 if (p_verbose >= 8) | |
292 | 9531 { |
9532 verbose_enter(); | |
273 | 9533 smsg((char_u *)_("Executing %s"), sourcing_name); |
292 | 9534 verbose_leave(); |
9535 } | |
7 | 9536 } |
9537 | |
9538 apc->curpat = ap; | |
9539 apc->nextcmd = ap->cmds; | |
9540 /* mark last command */ | |
9541 for (cp = ap->cmds; cp->next != NULL; cp = cp->next) | |
9542 cp->last = FALSE; | |
9543 cp->last = TRUE; | |
9544 } | |
9545 line_breakcheck(); | |
9546 if (apc->curpat != NULL) /* found a match */ | |
9547 break; | |
9548 } | |
9549 if (stop_at_last && ap->last) | |
9550 break; | |
9551 } | |
9552 } | |
9553 | |
9554 /* | |
9555 * Get next autocommand command. | |
9556 * Called by do_cmdline() to get the next line for ":if". | |
9557 * Returns allocated string, or NULL for end of autocommands. | |
9558 */ | |
9559 static char_u * | |
9560 getnextac(c, cookie, indent) | |
1880 | 9561 int c UNUSED; |
7 | 9562 void *cookie; |
1880 | 9563 int indent UNUSED; |
7 | 9564 { |
9565 AutoPatCmd *acp = (AutoPatCmd *)cookie; | |
9566 char_u *retval; | |
9567 AutoCmd *ac; | |
9568 | |
9569 /* Can be called again after returning the last line. */ | |
9570 if (acp->curpat == NULL) | |
9571 return NULL; | |
9572 | |
9573 /* repeat until we find an autocommand to execute */ | |
9574 for (;;) | |
9575 { | |
9576 /* skip removed commands */ | |
9577 while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL) | |
9578 if (acp->nextcmd->last) | |
9579 acp->nextcmd = NULL; | |
9580 else | |
9581 acp->nextcmd = acp->nextcmd->next; | |
9582 | |
9583 if (acp->nextcmd != NULL) | |
9584 break; | |
9585 | |
9586 /* at end of commands, find next pattern that matches */ | |
9587 if (acp->curpat->last) | |
9588 acp->curpat = NULL; | |
9589 else | |
9590 acp->curpat = acp->curpat->next; | |
9591 if (acp->curpat != NULL) | |
9592 auto_next_pat(acp, TRUE); | |
9593 if (acp->curpat == NULL) | |
9594 return NULL; | |
9595 } | |
9596 | |
9597 ac = acp->nextcmd; | |
9598 | |
9599 if (p_verbose >= 9) | |
9600 { | |
292 | 9601 verbose_enter_scroll(); |
273 | 9602 smsg((char_u *)_("autocommand %s"), ac->cmd); |
7 | 9603 msg_puts((char_u *)"\n"); /* don't overwrite this either */ |
292 | 9604 verbose_leave_scroll(); |
7 | 9605 } |
9606 retval = vim_strsave(ac->cmd); | |
9607 autocmd_nested = ac->nested; | |
9608 #ifdef FEAT_EVAL | |
9609 current_SID = ac->scriptID; | |
9610 #endif | |
9611 if (ac->last) | |
9612 acp->nextcmd = NULL; | |
9613 else | |
9614 acp->nextcmd = ac->next; | |
9615 return retval; | |
9616 } | |
9617 | |
9618 /* | |
9619 * Return TRUE if there is a matching autocommand for "fname". | |
40 | 9620 * To account for buffer-local autocommands, function needs to know |
9621 * in which buffer the file will be opened. | |
7 | 9622 */ |
9623 int | |
40 | 9624 has_autocmd(event, sfname, buf) |
661 | 9625 event_T event; |
7 | 9626 char_u *sfname; |
40 | 9627 buf_T *buf; |
7 | 9628 { |
9629 AutoPat *ap; | |
9630 char_u *fname; | |
9631 char_u *tail = gettail(sfname); | |
9632 int retval = FALSE; | |
9633 | |
9634 fname = FullName_save(sfname, FALSE); | |
9635 if (fname == NULL) | |
9636 return FALSE; | |
9637 | |
9638 #ifdef BACKSLASH_IN_FILENAME | |
9639 /* | |
9640 * Replace all backslashes with forward slashes. This makes the | |
9641 * autocommand patterns portable between Unix and MS-DOS. | |
9642 */ | |
9643 sfname = vim_strsave(sfname); | |
9644 if (sfname != NULL) | |
9645 forward_slash(sfname); | |
9646 forward_slash(fname); | |
9647 #endif | |
9648 | |
9649 for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next) | |
9650 if (ap->pat != NULL && ap->cmds != NULL | |
856 | 9651 && (ap->buflocal_nr == 0 |
153 | 9652 ? match_file_pat(NULL, ap->reg_prog, |
9653 fname, sfname, tail, ap->allow_dirs) | |
856 | 9654 : buf != NULL && ap->buflocal_nr == buf->b_fnum |
40 | 9655 )) |
7 | 9656 { |
9657 retval = TRUE; | |
9658 break; | |
9659 } | |
9660 | |
9661 vim_free(fname); | |
9662 #ifdef BACKSLASH_IN_FILENAME | |
9663 vim_free(sfname); | |
9664 #endif | |
9665 | |
9666 return retval; | |
9667 } | |
9668 | |
9669 #if defined(FEAT_CMDL_COMPL) || defined(PROTO) | |
9670 /* | |
9671 * Function given to ExpandGeneric() to obtain the list of autocommand group | |
9672 * names. | |
9673 */ | |
9674 char_u * | |
9675 get_augroup_name(xp, idx) | |
1880 | 9676 expand_T *xp UNUSED; |
7 | 9677 int idx; |
9678 { | |
9679 if (idx == augroups.ga_len) /* add "END" add the end */ | |
9680 return (char_u *)"END"; | |
9681 if (idx >= augroups.ga_len) /* end of list */ | |
9682 return NULL; | |
9683 if (AUGROUP_NAME(idx) == NULL) /* skip deleted entries */ | |
9684 return (char_u *)""; | |
9685 return AUGROUP_NAME(idx); /* return a name */ | |
9686 } | |
9687 | |
9688 static int include_groups = FALSE; | |
9689 | |
9690 char_u * | |
9691 set_context_in_autocmd(xp, arg, doautocmd) | |
9692 expand_T *xp; | |
9693 char_u *arg; | |
1733 | 9694 int doautocmd; /* TRUE for :doauto*, FALSE for :autocmd */ |
7 | 9695 { |
9696 char_u *p; | |
9697 int group; | |
9698 | |
9699 /* check for a group name, skip it if present */ | |
9700 include_groups = FALSE; | |
9701 p = arg; | |
9702 group = au_get_grouparg(&arg); | |
9703 if (group == AUGROUP_ERROR) | |
9704 return NULL; | |
9705 /* If there only is a group name that's what we expand. */ | |
9706 if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1])) | |
9707 { | |
9708 arg = p; | |
9709 group = AUGROUP_ALL; | |
9710 } | |
9711 | |
9712 /* skip over event name */ | |
9713 for (p = arg; *p != NUL && !vim_iswhite(*p); ++p) | |
9714 if (*p == ',') | |
9715 arg = p + 1; | |
9716 if (*p == NUL) | |
9717 { | |
9718 if (group == AUGROUP_ALL) | |
9719 include_groups = TRUE; | |
9720 xp->xp_context = EXPAND_EVENTS; /* expand event name */ | |
9721 xp->xp_pattern = arg; | |
9722 return NULL; | |
9723 } | |
9724 | |
9725 /* skip over pattern */ | |
9726 arg = skipwhite(p); | |
9727 while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\')) | |
9728 arg++; | |
9729 if (*arg) | |
9730 return arg; /* expand (next) command */ | |
9731 | |
9732 if (doautocmd) | |
9733 xp->xp_context = EXPAND_FILES; /* expand file names */ | |
9734 else | |
9735 xp->xp_context = EXPAND_NOTHING; /* pattern is not expanded */ | |
9736 return NULL; | |
9737 } | |
9738 | |
9739 /* | |
9740 * Function given to ExpandGeneric() to obtain the list of event names. | |
9741 */ | |
9742 char_u * | |
9743 get_event_name(xp, idx) | |
1880 | 9744 expand_T *xp UNUSED; |
7 | 9745 int idx; |
9746 { | |
9747 if (idx < augroups.ga_len) /* First list group names, if wanted */ | |
9748 { | |
9749 if (!include_groups || AUGROUP_NAME(idx) == NULL) | |
9750 return (char_u *)""; /* skip deleted entries */ | |
9751 return AUGROUP_NAME(idx); /* return a name */ | |
9752 } | |
9753 return (char_u *)event_names[idx - augroups.ga_len].name; | |
9754 } | |
9755 | |
9756 #endif /* FEAT_CMDL_COMPL */ | |
9757 | |
9758 /* | |
615 | 9759 * Return TRUE if autocmd is supported. |
9760 */ | |
9761 int | |
9762 autocmd_supported(name) | |
9763 char_u *name; | |
9764 { | |
9765 char_u *p; | |
9766 | |
9767 return (event_name2nr(name, &p) != NUM_EVENTS); | |
9768 } | |
9769 | |
9770 /* | |
612 | 9771 * Return TRUE if an autocommand is defined for a group, event and |
9772 * pattern: The group can be omitted to accept any group. "event" and "pattern" | |
9773 * can be NULL to accept any event and pattern. "pattern" can be NULL to accept | |
9774 * any pattern. Buffer-local patterns <buffer> or <buffer=N> are accepted. | |
9775 * Used for: | |
9776 * exists("#Group") or | |
9777 * exists("#Group#Event") or | |
9778 * exists("#Group#Event#pat") or | |
9779 * exists("#Event") or | |
9780 * exists("#Event#pat") | |
7 | 9781 */ |
9782 int | |
612 | 9783 au_exists(arg) |
9784 char_u *arg; | |
7 | 9785 { |
612 | 9786 char_u *arg_save; |
9787 char_u *pattern = NULL; | |
7 | 9788 char_u *event_name; |
9789 char_u *p; | |
661 | 9790 event_T event; |
7 | 9791 AutoPat *ap; |
40 | 9792 buf_T *buflocal_buf = NULL; |
612 | 9793 int group; |
9794 int retval = FALSE; | |
9795 | |
615 | 9796 /* Make a copy so that we can change the '#' chars to a NUL. */ |
612 | 9797 arg_save = vim_strsave(arg); |
9798 if (arg_save == NULL) | |
9799 return FALSE; | |
615 | 9800 p = vim_strchr(arg_save, '#'); |
612 | 9801 if (p != NULL) |
9802 *p++ = NUL; | |
9803 | |
9804 /* First, look for an autocmd group name */ | |
9805 group = au_find_group(arg_save); | |
9806 if (group == AUGROUP_ERROR) | |
9807 { | |
9808 /* Didn't match a group name, assume the first argument is an event. */ | |
9809 group = AUGROUP_ALL; | |
9810 event_name = arg_save; | |
9811 } | |
9812 else | |
9813 { | |
9814 if (p == NULL) | |
9815 { | |
9816 /* "Group": group name is present and it's recognized */ | |
9817 retval = TRUE; | |
9818 goto theend; | |
9819 } | |
9820 | |
9821 /* Must be "Group#Event" or "Group#Event#pat". */ | |
9822 event_name = p; | |
9823 p = vim_strchr(event_name, '#'); | |
9824 if (p != NULL) | |
9825 *p++ = NUL; /* "Group#Event#pat" */ | |
9826 } | |
9827 | |
9828 pattern = p; /* "pattern" is NULL when there is no pattern */ | |
7 | 9829 |
9830 /* find the index (enum) for the event name */ | |
9831 event = event_name2nr(event_name, &p); | |
9832 | |
9833 /* return FALSE if the event name is not recognized */ | |
612 | 9834 if (event == NUM_EVENTS) |
9835 goto theend; | |
7 | 9836 |
9837 /* Find the first autocommand for this event. | |
9838 * If there isn't any, return FALSE; | |
9839 * If there is one and no pattern given, return TRUE; */ | |
9840 ap = first_autopat[(int)event]; | |
9841 if (ap == NULL) | |
612 | 9842 goto theend; |
7 | 9843 |
40 | 9844 /* if pattern is "<buffer>", special handling is needed which uses curbuf */ |
9845 /* for pattern "<buffer=N>, fnamecmp() will work fine */ | |
1962 | 9846 if (pattern != NULL && STRICMP(pattern, "<buffer>") == 0) |
40 | 9847 buflocal_buf = curbuf; |
9848 | |
7 | 9849 /* Check if there is an autocommand with the given pattern. */ |
9850 for ( ; ap != NULL; ap = ap->next) | |
40 | 9851 /* only use a pattern when it has not been removed and has commands. */ |
9852 /* For buffer-local autocommands, fnamecmp() works fine. */ | |
7 | 9853 if (ap->pat != NULL && ap->cmds != NULL |
612 | 9854 && (group == AUGROUP_ALL || ap->group == group) |
1962 | 9855 && (pattern == NULL |
9856 || (buflocal_buf == NULL | |
9857 ? fnamecmp(ap->pat, pattern) == 0 | |
9858 : ap->buflocal_nr == buflocal_buf->b_fnum))) | |
612 | 9859 { |
9860 retval = TRUE; | |
9861 break; | |
9862 } | |
9863 | |
9864 theend: | |
9865 vim_free(arg_save); | |
9866 return retval; | |
7 | 9867 } |
40 | 9868 |
934 | 9869 #else /* FEAT_AUTOCMD */ |
9870 | |
9871 /* | |
9872 * Prepare for executing commands for (hidden) buffer "buf". | |
9873 * This is the non-autocommand version, it simply saves "curbuf" and sets | |
9874 * "curbuf" and "curwin" to match "buf". | |
9875 */ | |
9876 void | |
9877 aucmd_prepbuf(aco, buf) | |
9878 aco_save_T *aco; /* structure to save values in */ | |
9879 buf_T *buf; /* new curbuf */ | |
9880 { | |
1906 | 9881 aco->save_curbuf = curbuf; |
9882 --curbuf->b_nwindows; | |
934 | 9883 curbuf = buf; |
9884 curwin->w_buffer = buf; | |
1906 | 9885 ++curbuf->b_nwindows; |
934 | 9886 } |
9887 | |
9888 /* | |
9889 * Restore after executing commands for a (hidden) buffer. | |
9890 * This is the non-autocommand version. | |
9891 */ | |
9892 void | |
9893 aucmd_restbuf(aco) | |
9894 aco_save_T *aco; /* structure holding saved values */ | |
9895 { | |
1906 | 9896 --curbuf->b_nwindows; |
9897 curbuf = aco->save_curbuf; | |
934 | 9898 curwin->w_buffer = curbuf; |
1906 | 9899 ++curbuf->b_nwindows; |
934 | 9900 } |
9901 | |
7 | 9902 #endif /* FEAT_AUTOCMD */ |
9903 | |
934 | 9904 |
7 | 9905 #if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO) |
9906 /* | |
153 | 9907 * Try matching a filename with a "pattern" ("prog" is NULL), or use the |
9908 * precompiled regprog "prog" ("pattern" is NULL). That avoids calling | |
9909 * vim_regcomp() often. | |
7 | 9910 * Used for autocommands and 'wildignore'. |
9911 * Returns TRUE if there is a match, FALSE otherwise. | |
9912 */ | |
9913 int | |
153 | 9914 match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs) |
7 | 9915 char_u *pattern; /* pattern to match with */ |
153 | 9916 regprog_T *prog; /* pre-compiled regprog or NULL */ |
7 | 9917 char_u *fname; /* full path of file name */ |
9918 char_u *sfname; /* short file name or NULL */ | |
9919 char_u *tail; /* tail of path */ | |
9920 int allow_dirs; /* allow matching with dir */ | |
9921 { | |
9922 regmatch_T regmatch; | |
9923 int result = FALSE; | |
9924 #ifdef FEAT_OSFILETYPE | |
9925 int no_pattern = FALSE; /* TRUE if check is filetype only */ | |
9926 char_u *type_start; | |
9927 char_u c; | |
9928 int match = FALSE; | |
9929 #endif | |
9930 | |
9931 #ifdef CASE_INSENSITIVE_FILENAME | |
9932 regmatch.rm_ic = TRUE; /* Always ignore case */ | |
9933 #else | |
9934 regmatch.rm_ic = FALSE; /* Don't ever ignore case */ | |
9935 #endif | |
9936 #ifdef FEAT_OSFILETYPE | |
9937 if (*pattern == '<') | |
9938 { | |
9939 /* There is a filetype condition specified with this pattern. | |
9940 * Check the filetype matches first. If not, don't bother with the | |
9941 * pattern (set regprog to NULL). | |
9942 * Always use magic for the regexp. | |
9943 */ | |
9944 | |
9945 for (type_start = pattern + 1; (c = *pattern); pattern++) | |
9946 { | |
9947 if ((c == ';' || c == '>') && match == FALSE) | |
9948 { | |
9949 *pattern = NUL; /* Terminate the string */ | |
9950 match = mch_check_filetype(fname, type_start); | |
9951 *pattern = c; /* Restore the terminator */ | |
9952 type_start = pattern + 1; | |
9953 } | |
9954 if (c == '>') | |
9955 break; | |
9956 } | |
9957 | |
9958 /* (c should never be NUL, but check anyway) */ | |
9959 if (match == FALSE || c == NUL) | |
9960 regmatch.regprog = NULL; /* Doesn't match - don't check pat. */ | |
9961 else if (*pattern == NUL) | |
9962 { | |
9963 regmatch.regprog = NULL; /* Vim will try to free regprog later */ | |
9964 no_pattern = TRUE; /* Always matches - don't check pat. */ | |
9965 } | |
9966 else | |
9967 regmatch.regprog = vim_regcomp(pattern + 1, RE_MAGIC); | |
9968 } | |
9969 else | |
9970 #endif | |
153 | 9971 { |
9972 if (prog != NULL) | |
9973 regmatch.regprog = prog; | |
9974 else | |
9975 regmatch.regprog = vim_regcomp(pattern, RE_MAGIC); | |
9976 } | |
7 | 9977 |
9978 /* | |
9979 * Try for a match with the pattern with: | |
9980 * 1. the full file name, when the pattern has a '/'. | |
9981 * 2. the short file name, when the pattern has a '/'. | |
9982 * 3. the tail of the file name, when the pattern has no '/'. | |
9983 */ | |
9984 if ( | |
9985 #ifdef FEAT_OSFILETYPE | |
9986 /* If the check is for a filetype only and we don't care | |
9987 * about the path then skip all the regexp stuff. | |
9988 */ | |
9989 no_pattern || | |
9990 #endif | |
9991 (regmatch.regprog != NULL | |
9992 && ((allow_dirs | |
9993 && (vim_regexec(®match, fname, (colnr_T)0) | |
9994 || (sfname != NULL | |
9995 && vim_regexec(®match, sfname, (colnr_T)0)))) | |
9996 || (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0))))) | |
9997 result = TRUE; | |
9998 | |
153 | 9999 if (prog == NULL) |
10000 vim_free(regmatch.regprog); | |
7 | 10001 return result; |
10002 } | |
10003 #endif | |
10004 | |
10005 #if defined(FEAT_WILDIGN) || defined(PROTO) | |
10006 /* | |
10007 * Return TRUE if a file matches with a pattern in "list". | |
10008 * "list" is a comma-separated list of patterns, like 'wildignore'. | |
10009 * "sfname" is the short file name or NULL, "ffname" the long file name. | |
10010 */ | |
10011 int | |
10012 match_file_list(list, sfname, ffname) | |
10013 char_u *list; | |
10014 char_u *sfname; | |
10015 char_u *ffname; | |
10016 { | |
10017 char_u buf[100]; | |
10018 char_u *tail; | |
10019 char_u *regpat; | |
10020 char allow_dirs; | |
10021 int match; | |
10022 char_u *p; | |
10023 | |
10024 tail = gettail(sfname); | |
10025 | |
10026 /* try all patterns in 'wildignore' */ | |
10027 p = list; | |
10028 while (*p) | |
10029 { | |
10030 copy_option_part(&p, buf, 100, ","); | |
10031 regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE); | |
10032 if (regpat == NULL) | |
10033 break; | |
153 | 10034 match = match_file_pat(regpat, NULL, ffname, sfname, |
10035 tail, (int)allow_dirs); | |
7 | 10036 vim_free(regpat); |
10037 if (match) | |
10038 return TRUE; | |
10039 } | |
10040 return FALSE; | |
10041 } | |
10042 #endif | |
10043 | |
10044 /* | |
10045 * Convert the given pattern "pat" which has shell style wildcards in it, into | |
10046 * a regular expression, and return the result in allocated memory. If there | |
10047 * is a directory path separator to be matched, then TRUE is put in | |
10048 * allow_dirs, otherwise FALSE is put there -- webb. | |
10049 * Handle backslashes before special characters, like "\*" and "\ ". | |
10050 * | |
10051 * If FEAT_OSFILETYPE defined then pass initial <type> through unchanged. Eg: | |
10052 * '<html>myfile' becomes '<html>^myfile$' -- leonard. | |
10053 * | |
10054 * Returns NULL when out of memory. | |
10055 */ | |
10056 char_u * | |
10057 file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash) | |
10058 char_u *pat; | |
10059 char_u *pat_end; /* first char after pattern or NULL */ | |
10060 char *allow_dirs; /* Result passed back out in here */ | |
1877 | 10061 int no_bslash UNUSED; /* Don't use a backward slash as pathsep */ |
7 | 10062 { |
10063 int size; | |
10064 char_u *endp; | |
10065 char_u *reg_pat; | |
10066 char_u *p; | |
10067 int i; | |
10068 int nested = 0; | |
10069 int add_dollar = TRUE; | |
10070 #ifdef FEAT_OSFILETYPE | |
10071 int check_length = 0; | |
10072 #endif | |
10073 | |
10074 if (allow_dirs != NULL) | |
10075 *allow_dirs = FALSE; | |
10076 if (pat_end == NULL) | |
10077 pat_end = pat + STRLEN(pat); | |
10078 | |
10079 #ifdef FEAT_OSFILETYPE | |
10080 /* Find out how much of the string is the filetype check */ | |
10081 if (*pat == '<') | |
10082 { | |
10083 /* Count chars until the next '>' */ | |
10084 for (p = pat + 1; p < pat_end && *p != '>'; p++) | |
10085 ; | |
10086 if (p < pat_end) | |
10087 { | |
10088 /* Pattern is of the form <.*>.* */ | |
10089 check_length = p - pat + 1; | |
10090 if (p + 1 >= pat_end) | |
10091 { | |
10092 /* The 'pattern' is a filetype check ONLY */ | |
10093 reg_pat = (char_u *)alloc(check_length + 1); | |
10094 if (reg_pat != NULL) | |
10095 { | |
10096 mch_memmove(reg_pat, pat, (size_t)check_length); | |
10097 reg_pat[check_length] = NUL; | |
10098 } | |
10099 return reg_pat; | |
10100 } | |
10101 } | |
10102 /* else: there was no closing '>' - assume it was a normal pattern */ | |
10103 | |
10104 } | |
10105 pat += check_length; | |
10106 size = 2 + check_length; | |
10107 #else | |
10108 size = 2; /* '^' at start, '$' at end */ | |
10109 #endif | |
10110 | |
10111 for (p = pat; p < pat_end; p++) | |
10112 { | |
10113 switch (*p) | |
10114 { | |
10115 case '*': | |
10116 case '.': | |
10117 case ',': | |
10118 case '{': | |
10119 case '}': | |
10120 case '~': | |
10121 size += 2; /* extra backslash */ | |
10122 break; | |
10123 #ifdef BACKSLASH_IN_FILENAME | |
10124 case '\\': | |
10125 case '/': | |
10126 size += 4; /* could become "[\/]" */ | |
10127 break; | |
10128 #endif | |
10129 default: | |
10130 size++; | |
10131 # ifdef FEAT_MBYTE | |
474 | 10132 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10133 { |
10134 ++p; | |
10135 ++size; | |
10136 } | |
10137 # endif | |
10138 break; | |
10139 } | |
10140 } | |
10141 reg_pat = alloc(size + 1); | |
10142 if (reg_pat == NULL) | |
10143 return NULL; | |
10144 | |
10145 #ifdef FEAT_OSFILETYPE | |
10146 /* Copy the type check in to the start. */ | |
10147 if (check_length) | |
10148 mch_memmove(reg_pat, pat - check_length, (size_t)check_length); | |
10149 i = check_length; | |
10150 #else | |
10151 i = 0; | |
10152 #endif | |
10153 | |
10154 if (pat[0] == '*') | |
10155 while (pat[0] == '*' && pat < pat_end - 1) | |
10156 pat++; | |
10157 else | |
10158 reg_pat[i++] = '^'; | |
10159 endp = pat_end - 1; | |
10160 if (*endp == '*') | |
10161 { | |
10162 while (endp - pat > 0 && *endp == '*') | |
10163 endp--; | |
10164 add_dollar = FALSE; | |
10165 } | |
10166 for (p = pat; *p && nested >= 0 && p <= endp; p++) | |
10167 { | |
10168 switch (*p) | |
10169 { | |
10170 case '*': | |
10171 reg_pat[i++] = '.'; | |
10172 reg_pat[i++] = '*'; | |
444 | 10173 while (p[1] == '*') /* "**" matches like "*" */ |
10174 ++p; | |
7 | 10175 break; |
10176 case '.': | |
10177 #ifdef RISCOS | |
10178 if (allow_dirs != NULL) | |
10179 *allow_dirs = TRUE; | |
10180 /* FALLTHROUGH */ | |
10181 #endif | |
10182 case '~': | |
10183 reg_pat[i++] = '\\'; | |
10184 reg_pat[i++] = *p; | |
10185 break; | |
10186 case '?': | |
10187 #ifdef RISCOS | |
10188 case '#': | |
10189 #endif | |
10190 reg_pat[i++] = '.'; | |
10191 break; | |
10192 case '\\': | |
10193 if (p[1] == NUL) | |
10194 break; | |
10195 #ifdef BACKSLASH_IN_FILENAME | |
10196 if (!no_bslash) | |
10197 { | |
10198 /* translate: | |
10199 * "\x" to "\\x" e.g., "dir\file" | |
10200 * "\*" to "\\.*" e.g., "dir\*.c" | |
10201 * "\?" to "\\." e.g., "dir\??.c" | |
10202 * "\+" to "\+" e.g., "fileX\+.c" | |
10203 */ | |
10204 if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?') | |
10205 && p[1] != '+') | |
10206 { | |
10207 reg_pat[i++] = '['; | |
10208 reg_pat[i++] = '\\'; | |
10209 reg_pat[i++] = '/'; | |
10210 reg_pat[i++] = ']'; | |
10211 if (allow_dirs != NULL) | |
10212 *allow_dirs = TRUE; | |
10213 break; | |
10214 } | |
10215 } | |
10216 #endif | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10217 /* Undo escaping from ExpandEscape(): |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10218 * foo\?bar -> foo?bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10219 * foo\%bar -> foo%bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10220 * foo\,bar -> foo,bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10221 * foo\ bar -> foo bar |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10222 * 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
|
10223 * regexp. */ |
7 | 10224 if (*++p == '?' |
10225 #ifdef BACKSLASH_IN_FILENAME | |
10226 && no_bslash | |
10227 #endif | |
10228 ) | |
10229 reg_pat[i++] = '?'; | |
10230 else | |
2243
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10231 if (*p == ',' || *p == '%' || *p == '#' || *p == ' ') |
03a5f2897db3
Fix completion of file names with '%' and '*'.
Bram Moolenaar <bram@vim.org>
parents:
2241
diff
changeset
|
10232 reg_pat[i++] = *p; |
7 | 10233 else |
10234 { | |
10235 if (allow_dirs != NULL && vim_ispathsep(*p) | |
10236 #ifdef BACKSLASH_IN_FILENAME | |
10237 && (!no_bslash || *p != '\\') | |
10238 #endif | |
10239 ) | |
10240 *allow_dirs = TRUE; | |
10241 reg_pat[i++] = '\\'; | |
10242 reg_pat[i++] = *p; | |
10243 } | |
10244 break; | |
10245 #ifdef BACKSLASH_IN_FILENAME | |
10246 case '/': | |
10247 reg_pat[i++] = '['; | |
10248 reg_pat[i++] = '\\'; | |
10249 reg_pat[i++] = '/'; | |
10250 reg_pat[i++] = ']'; | |
10251 if (allow_dirs != NULL) | |
10252 *allow_dirs = TRUE; | |
10253 break; | |
10254 #endif | |
10255 case '{': | |
10256 reg_pat[i++] = '\\'; | |
10257 reg_pat[i++] = '('; | |
10258 nested++; | |
10259 break; | |
10260 case '}': | |
10261 reg_pat[i++] = '\\'; | |
10262 reg_pat[i++] = ')'; | |
10263 --nested; | |
10264 break; | |
10265 case ',': | |
10266 if (nested) | |
10267 { | |
10268 reg_pat[i++] = '\\'; | |
10269 reg_pat[i++] = '|'; | |
10270 } | |
10271 else | |
10272 reg_pat[i++] = ','; | |
10273 break; | |
10274 default: | |
10275 # ifdef FEAT_MBYTE | |
474 | 10276 if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1) |
7 | 10277 reg_pat[i++] = *p++; |
10278 else | |
10279 # endif | |
10280 if (allow_dirs != NULL && vim_ispathsep(*p)) | |
10281 *allow_dirs = TRUE; | |
10282 reg_pat[i++] = *p; | |
10283 break; | |
10284 } | |
10285 } | |
10286 if (add_dollar) | |
10287 reg_pat[i++] = '$'; | |
10288 reg_pat[i] = NUL; | |
10289 if (nested != 0) | |
10290 { | |
10291 if (nested < 0) | |
10292 EMSG(_("E219: Missing {.")); | |
10293 else | |
10294 EMSG(_("E220: Missing }.")); | |
10295 vim_free(reg_pat); | |
10296 reg_pat = NULL; | |
10297 } | |
10298 return reg_pat; | |
10299 } |